| OLD | NEW | 
|---|
| 1 /** | 1 /** | 
| 2  * \file session.cpp Implementation of Session class. | 2 * \file session.cpp Implementation of Session class. | 
| 3  */ | 3 */ | 
| 4 | 4 | 
|  | 5 #include "installer-lib.h" | 
| 5 #include "session.h" | 6 #include "session.h" | 
| 6 #include "property.h" | 7 #include "property.h" | 
| 7 #include "msiquery.h" | 8 #include "msiquery.h" | 
| 8 | 9 | 
| 9 //------------------------------------------------------------------------------
    ----------- | 10 //------------------------------------------------------------------------------
    ----------- | 
|  | 11 // Message | 
|  | 12 //------------------------------------------------------------------------------
    ----------- | 
|  | 13 Message::Message( std::string message, INSTALLMESSAGE message_type ) | 
|  | 14   : r( 1 ), message_type( message_type ) | 
|  | 15 { | 
|  | 16   r.assign_string( 0, message ) ; | 
|  | 17 } | 
|  | 18 | 
|  | 19 Message::Message( std::wstring message, INSTALLMESSAGE message_type ) | 
|  | 20   : r( 1 ), message_type( message_type ) | 
|  | 21 { | 
|  | 22   r.assign_string( 0, message ) ; | 
|  | 23 } | 
|  | 24 | 
|  | 25 //------------------------------------------------------------------------------
    ----------- | 
| 10 // Session | 26 // Session | 
| 11 //------------------------------------------------------------------------------
    ----------- | 27 //------------------------------------------------------------------------------
    ----------- | 
| 12 Session::Session( MSIHANDLE handle, std::wstring name ) | 28 Session::Session( MSIHANDLE handle, std::string name ) | 
| 13   : handle( handle ), | 29   : handle( handle ), | 
| 14   log_prefix( name + L": " ) | 30   log_prefix( name + ": " ) | 
| 15 { | 31 { | 
| 16   log( L"Entering custom action" ) ; | 32   log_prefix_w.assign( name.begin(), name.end() ) ; | 
|  | 33   log_prefix_w += L": " ; | 
|  | 34   log_noexcept( "Entering custom action" ) ; | 
|  | 35 } | 
|  | 36 | 
|  | 37 Session::~Session() | 
|  | 38 { | 
|  | 39   log_noexcept( "Exiting custom action" ) ; | 
| 17 } | 40 } | 
| 18 | 41 | 
| 19 /** | 42 /** | 
| 20  * \par Implementation Notes | 43 * A message for the installation log. | 
| 21  *    The session handle doesn't need to be closed. | 44 * | 
| 22  *    It's provided as an argument to the custom action at the outset, and we do
     not manage its life cycle. | 45 * Writing to the installation log uses MsiProcessMessage just like interactive d
    ialog boxes do. | 
| 23  */ | 46 * | 
| 24 Session::~Session() | 47 * This class is not exposed outside this compilation unit because everything it 
    can do is already exposed by the log functions. | 
|  | 48 */ | 
|  | 49 struct Log_Message | 
|  | 50   : public Message | 
| 25 { | 51 { | 
| 26   log( L"Exiting custom action" ) ; | 52   Log_Message ( std::wstring message ) | 
|  | 53     : Message( message, INSTALLMESSAGE_INFO ) | 
|  | 54   {} | 
|  | 55 | 
|  | 56   Log_Message ( std::string message ) | 
|  | 57     : Message( message, INSTALLMESSAGE_INFO ) | 
|  | 58   {} | 
|  | 59 } ; | 
|  | 60 | 
|  | 61 void Session::log( std::string message ) | 
|  | 62 { | 
|  | 63   write_message( Log_Message( log_prefix + message ) ) ; | 
| 27 } | 64 } | 
| 28 | 65 | 
| 29 /** |  | 
| 30  * \par Implementation Notes |  | 
| 31  *    To write to the installation log, we use a call to MsiProcessMessage with 
    message type INSTALLMESSAGE_INFO. |  | 
| 32  *    The text to be written needs to go in a "record" (yes, a database record) 
    that acts as an argument vector. |  | 
| 33  *    For the message type we're using, we need only a record with a single fiel
    d. |  | 
| 34  * |  | 
| 35  * \sa MSDN "MsiProcessMessage function" |  | 
| 36  *    http://msdn.microsoft.com/en-us/library/windows/desktop/aa370354%28v=vs.85
    %29.aspx |  | 
| 37  *    MsiProcessMessage is mostly for user interaction with message boxes, but i
    t's also the access to the installation log. |  | 
| 38  */ |  | 
| 39 void Session::log( std::wstring message ) | 66 void Session::log( std::wstring message ) | 
| 40 { | 67 { | 
| 41   Record r = Record( 1 ); | 68   write_message( Log_Message( log_prefix_w + message ) ) ; | 
| 42   r.assign_string( 0, log_prefix + message ); |  | 
| 43   int e = MsiProcessMessage( handle, INSTALLMESSAGE_INFO, r.handle() ) ; |  | 
| 44   if ( e != IDOK ) |  | 
| 45   { |  | 
| 46     throw std::runtime_error( "Did not succeed writing to log." ) ; |  | 
| 47   } |  | 
| 48 } | 69 } | 
| 49 | 70 | 
| 50 Immediate_Session::Immediate_Session( MSIHANDLE handle, std::wstring name ) | 71 void Session::log_noexcept( std::string message ) | 
|  | 72 { | 
|  | 73   write_message_noexcept( Log_Message( log_prefix + message ) ) ; | 
|  | 74 } | 
|  | 75 | 
|  | 76 int Session::write_message( Message & m ) | 
|  | 77 { | 
|  | 78   int x = write_message_noexcept( m ) ; | 
|  | 79   if ( x == -1 ) | 
|  | 80   { | 
|  | 81     throw windows_api_error( "MsiProcessMessage", x, "attempt to write to log fi
    le" ) ; | 
|  | 82   } | 
|  | 83   return x ; | 
|  | 84 } | 
|  | 85 | 
|  | 86 int Session::write_message_noexcept( Message & m ) | 
|  | 87 { | 
|  | 88   return MsiProcessMessage( handle, m.message_type, m.r.handle() ) ; | 
|  | 89 } | 
|  | 90 | 
|  | 91 //------------------------------------------------------------------------------
    ----------- | 
|  | 92 // Immediate_Session | 
|  | 93 //------------------------------------------------------------------------------
    ----------- | 
|  | 94 Immediate_Session::Immediate_Session( MSIHANDLE handle, std::string name ) | 
| 51   : Session( handle, name ) | 95   : Session( handle, name ) | 
| 52 { | 96 {} | 
| 53 } |  | 
| OLD | NEW | 
|---|