| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| 1 /** | 1 /** |
| 2 * \file record.cpp Implementation of Session class. | 2 * \file record.cpp Implementation of Session class. |
| 3 */ | 3 */ |
| 4 | 4 |
| 5 #include "record.h" | 5 #include "record.h" |
| 6 #include "msiquery.h" | 6 #include "msiquery.h" |
| 7 | 7 |
| 8 //------------------------------------------------------------------------------ ----------- | 8 //------------------------------------------------------------------------------ ----------- |
| 9 // Record | 9 // Record |
| 10 //------------------------------------------------------------------------------ ----------- | 10 //------------------------------------------------------------------------------ ----------- |
| 11 Record::Record( unsigned int n_fields ) | 11 Record::Record( unsigned int n_fields ) |
| 12 : n_fields( n_fields ) | |
| 13 { | 12 { |
| 14 _handle = MsiCreateRecord( n_fields ) ; | 13 _handle = MsiCreateRecord( n_fields ) ; |
| 15 if ( ! _handle ) | 14 if ( ! _handle ) |
| 16 { | 15 { |
| 17 throw std::runtime_error( "Failed to create record" ) ; | 16 throw std::runtime_error( "Failed to create record" ) ; |
| 18 } | 17 } |
| 19 } | 18 } |
| 20 | 19 |
| 21 Record::~Record() | 20 Record::~Record() |
| 22 { | 21 { |
| 23 MsiCloseHandle( _handle ) ; | 22 if ( _handle != 0 ) |
| 23 { | |
| 24 MsiCloseHandle( _handle ) ; | |
| 25 } | |
| 24 } | 26 } |
| 25 | 27 |
| 26 void | 28 void Record::only_non_null() |
| 27 Record::assign_string( unsigned int field_index, std::wstring value ) | |
| 28 { | 29 { |
| 29 MsiRecordSetString( _handle, field_index, value.c_str() ) ; | 30 if ( _handle == 0 ) |
| 31 { | |
| 32 throw std::runtime_error( "Operation only permitted for non-null objects" ) ; | |
| 33 } | |
| 34 } | |
| 35 | |
| 36 void Record::assign_string( unsigned int field_index, const char *value ) | |
| 37 { | |
| 38 only_non_null() ; | |
| 39 MsiRecordSetStringA( _handle, field_index, value ) ; | |
| 40 } | |
| 41 | |
| 42 void Record::assign_string( unsigned int field_index, const wchar_t *value ) | |
| 43 { | |
| 44 only_non_null() ; | |
| 45 MsiRecordSetStringW( _handle, field_index, value ) ; | |
| 46 } | |
| 47 | |
| 48 /** | |
| 49 * \par Implementation | |
| 50 * - MSDN [MsiRecordGetString](http://msdn.microsoft.com/en-us/library/aa3703 68%28v=vs.85%29.aspx) | |
| 51 */ | |
| 52 std::wstring Record::value_string( unsigned int field_index ) | |
| 53 { | |
| 54 static wchar_t initial_buffer[ 256 ] = L"" ; | |
| 55 DWORD length = 255 ; // one less than the buffer length to hold a terminating null character | |
| 56 UINT x = MsiRecordGetStringW( _handle, field_index, initial_buffer, & length ) ; | |
| 57 if ( x == ERROR_SUCCESS ) | |
| 58 { | |
| 59 return std::wstring( initial_buffer ) ; | |
| 60 } | |
| 61 if ( x == ERROR_MORE_DATA ) | |
| 62 { | |
| 63 // Future: handle longer strings. | |
| 64 throw std::runtime_error( "Record strings longer than 255 not supported yet" ) ; | |
| 65 } | |
| 66 throw std::runtime_error( "Error retrieving string from record" ) ; | |
| 67 } | |
| 68 | |
| 69 size_t Record::n_fields() const | |
| 70 { | |
| 71 unsigned int x = MsiRecordGetFieldCount( _handle ) ; | |
| 72 if ( x == 0xFFFFFFFF ) | |
|
Oleksandr
2014/03/12 19:26:04
We use INVALID_HANDLE_VALUE pretty much everywhere
Eric
2014/03/17 12:26:46
This is the return value as documented on the MsiR
| |
| 73 { | |
| 74 throw std::runtime_error( "Invalid handle" ) ; | |
| 75 } | |
| 76 return x ; | |
| 30 } | 77 } |
| OLD | NEW |