| Left: | ||
| Right: |
| 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 "session.h" | 5 #include "session.h" |
| 6 #include "property.h" | 6 #include "property.h" |
| 7 #include "msiquery.h" | 7 #include "msiquery.h" |
| 8 | 8 |
| 9 // Temporary for debugging | |
| 10 #include <sstream> | |
|
Oleksandr
2014/03/12 19:26:04
Not needed anymore, is it?
Eric
2014/03/17 12:26:46
It's used in Session::write_message.
| |
| 11 | |
| 12 | |
| 13 //------------------------------------------------------------------------------ ----------- | |
| 14 // Message | |
| 15 //------------------------------------------------------------------------------ ----------- | |
| 16 Message::Message( std::string message, INSTALLMESSAGE message_type ) | |
| 17 : r( 1 ), message_type( message_type ) | |
| 18 { | |
| 19 r.assign_string( 0, message ) ; | |
| 20 } | |
| 21 | |
| 22 Message::Message( std::wstring message, INSTALLMESSAGE message_type ) | |
| 23 : r( 1 ), message_type( message_type ) | |
| 24 { | |
| 25 r.assign_string( 0, message ) ; | |
| 26 } | |
| 27 | |
| 9 //------------------------------------------------------------------------------ ----------- | 28 //------------------------------------------------------------------------------ ----------- |
| 10 // Session | 29 // Session |
| 11 //------------------------------------------------------------------------------ ----------- | 30 //------------------------------------------------------------------------------ ----------- |
| 12 Session::Session( MSIHANDLE handle, std::wstring name ) | 31 Session::Session( MSIHANDLE handle, std::string name ) |
| 13 : handle( handle ), | 32 : handle( handle ), |
| 14 log_prefix( name + L": " ) | 33 log_prefix( name + ": " ) |
| 15 { | 34 { |
| 16 log( L"Entering custom action" ) ; | 35 log_prefix_w.assign( name.begin(), name.end() ) ; |
| 36 log_prefix_w += L": " ; | |
| 37 log_noexcept( "Entering custom action" ) ; | |
| 38 } | |
| 39 | |
| 40 Session::~Session() | |
| 41 { | |
| 42 log_noexcept( "Exiting custom action" ) ; | |
| 17 } | 43 } |
| 18 | 44 |
| 19 /** | 45 /** |
| 20 * \par Implementation Notes | 46 * A message for the installation log. |
| 21 * The session handle doesn't need to be closed. | 47 * |
| 22 * It's provided as an argument to the custom action at the outset, and we do not manage its life cycle. | 48 * Writing to the installation log uses MsiProcessMessage just like interactive dialog boxes do. |
| 49 * | |
| 50 * This class is not exposed outside this compilation unit because everything it can do is already exposed by the log functions. | |
| 23 */ | 51 */ |
| 24 Session::~Session() | 52 struct Log_Message |
| 53 : public Message | |
| 25 { | 54 { |
| 26 log( L"Exiting custom action" ) ; | 55 Log_Message ( std::wstring message ) |
| 56 : Message( message, INSTALLMESSAGE_INFO ) | |
| 57 {} | |
| 58 | |
| 59 Log_Message ( std::string message ) | |
| 60 : Message( message, INSTALLMESSAGE_INFO ) | |
| 61 {} | |
| 62 } ; | |
| 63 | |
| 64 void Session::log( std::string message ) | |
| 65 { | |
| 66 write_message( Log_Message( log_prefix + message ) ) ; | |
| 27 } | 67 } |
| 28 | 68 |
| 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 ) | 69 void Session::log( std::wstring message ) |
| 40 { | 70 { |
| 41 Record r = Record( 1 ); | 71 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 } | 72 } |
| 49 | 73 |
| 50 Immediate_Session::Immediate_Session( MSIHANDLE handle, std::wstring name ) | 74 void Session::log_noexcept( std::string message ) |
| 75 { | |
| 76 write_message_noexcept( Log_Message( log_prefix + message ) ) ; | |
| 77 } | |
| 78 | |
| 79 int Session::write_message( Message & m ) | |
| 80 { | |
| 81 int x = write_message_noexcept( m ) ; | |
| 82 if ( x == -1 ) | |
| 83 { | |
| 84 DWORD z = ::GetLastError(); | |
| 85 std::ostringstream s ; | |
| 86 s << "Error writing message. err = 0x" << std::hex << z ; | |
| 87 throw std::runtime_error( s.str() ) ; | |
| 88 } | |
| 89 return x ; | |
| 90 } | |
| 91 | |
| 92 int Session::write_message_noexcept( Message & m ) | |
| 93 { | |
| 94 return MsiProcessMessage( handle, m.message_type, m.r.handle() ) ; | |
| 95 } | |
| 96 | |
| 97 //------------------------------------------------------------------------------ ----------- | |
| 98 // Immediate_Session | |
| 99 //------------------------------------------------------------------------------ ----------- | |
| 100 Immediate_Session::Immediate_Session( MSIHANDLE handle, std::string name ) | |
| 51 : Session( handle, name ) | 101 : Session( handle, name ) |
| 52 { | 102 {} |
| 53 } | |
| OLD | NEW |