OLD | NEW |
1 /** | 1 /** |
2 * \file property.cpp Implementation of Property class etc. | 2 * \file property.cpp Implementation of Property class etc. |
3 */ | 3 */ |
4 | 4 |
5 #include "installer-lib.h" | 5 #include "installer-lib.h" |
6 #include "property.h" | 6 #include "property.h" |
7 #include "session.h" | 7 #include "session.h" |
8 #include <msiquery.h> | 8 #include <msiquery.h> |
9 #include <memory> | 9 #include <memory> |
10 | 10 |
11 //------------------------------------------------------------------------------
----------- | 11 //------------------------------------------------------------------------------
----------- |
12 // Property | 12 // Property |
13 //------------------------------------------------------------------------------
----------- | 13 //------------------------------------------------------------------------------
----------- |
14 Property::Property( Session & session, std::wstring name ) | 14 Property::Property(Session& session, std::wstring name) |
15 // VSE 2012 shows an IntelliSense error here. Ignore it. The compiler properly
sees the 'friend' declaration. | 15 // VSE 2012 shows an IntelliSense error here. Ignore it. The compiler properly
sees the 'friend' declaration. |
16 : handle( session.handle ), name( name ) | 16 : handle(session.handle), name(name) |
17 {} | 17 {} |
18 | 18 |
19 /** | 19 /** |
20 * \par Implementation | 20 * \par Implementation |
21 * The center of the implementation is the <a href="http://msdn.microsoft.com/en-
us/library/windows/desktop/aa370134%28v=vs.85%29.aspx">MsiGetProperty function</
a>. | 21 * The center of the implementation is the <a href="http://msdn.microsoft.com/en-
us/library/windows/desktop/aa370134%28v=vs.85%29.aspx">MsiGetProperty function</
a>. |
22 */ | 22 */ |
23 Property::operator std::wstring() const | 23 Property::operator std::wstring() const |
24 { | 24 { |
25 /* | 25 /* |
26 * The first call gets the size, but also the actual value if it's short enough
. | 26 * The first call gets the size, but also the actual value if it's short enough
. |
27 * A second call, if necessary, allocates a sufficiently-long buffer and then g
ets the full value. | 27 * A second call, if necessary, allocates a sufficiently-long buffer and then g
ets the full value. |
28 * We use only a modest fixed-size buffer for the first step, because we handle
arbitrary-length property values in a second step. | 28 * We use only a modest fixed-size buffer for the first step, because we handle
arbitrary-length property values in a second step. |
29 */ | 29 */ |
30 // This buffer allocates on the stack, so we don't want it too large; 64 chara
cters is enough for most properties anyway. | 30 // This buffer allocates on the stack, so we don't want it too large; 64 chara
cters is enough for most properties anyway. |
31 WCHAR buffer1[ 64 ] = { L'\0' } ; | 31 WCHAR buffer1[64] = { L'\0' }; |
32 DWORD length = sizeof( buffer1 ) / sizeof( WCHAR ) ; | 32 DWORD length = sizeof(buffer1) / sizeof(WCHAR); |
33 UINT x = MsiGetPropertyW( handle, name.c_str(), buffer1, & length ) ; | 33 UINT x = MsiGetPropertyW(handle, name.c_str(), buffer1, & length); |
34 switch ( x ) | 34 switch (x) |
35 { | 35 { |
36 case ERROR_SUCCESS: | 36 case ERROR_SUCCESS: |
37 // This call might succeed, which means the return value was short enough to
fit into the buffer. | 37 // This call might succeed, which means the return value was short enough
to fit into the buffer. |
38 return std::wstring( buffer1, length ) ; | 38 return std::wstring(buffer1, length); |
39 case ERROR_MORE_DATA: | 39 case ERROR_MORE_DATA: |
40 // Do nothing yet. | 40 // Do nothing yet. |
41 break ; | 41 break; |
42 default: | 42 default: |
43 throw WindowsApiError( "MsiGetPropertyW", x, "fixed buffer" ) ; | 43 throw WindowsApiError("MsiGetPropertyW", x, "fixed buffer"); |
44 } | 44 } |
45 // Assert we received ERROR_MORE_DATA | 45 // Assert we received ERROR_MORE_DATA |
46 // unique_ptr handles deallocation transparently | 46 // unique_ptr handles deallocation transparently |
47 std::unique_ptr< WCHAR[] > buffer2( new WCHAR[ length ] ) ; | 47 std::unique_ptr<WCHAR[]> buffer2(new WCHAR[length]); |
48 x = MsiGetPropertyW( handle, name.c_str(), buffer2.get(), & length ) ; | 48 x = MsiGetPropertyW(handle, name.c_str(), buffer2.get(), & length); |
49 switch ( x ) | 49 switch (x) |
50 { | 50 { |
51 case ERROR_SUCCESS: | 51 case ERROR_SUCCESS: |
52 return std::wstring( buffer2.get(), length ) ; | 52 return std::wstring(buffer2.get(), length); |
53 default: | 53 default: |
54 throw WindowsApiError( "MsiGetPropertyW", x, "allocated buffer" ) ; | 54 throw WindowsApiError("MsiGetPropertyW", x, "allocated buffer"); |
55 } | 55 } |
56 } | 56 } |
57 | 57 |
58 /** | 58 /** |
59 * \par Implementation | 59 * \par Implementation |
60 * The center of the implementation is the <a href="http://msdn.microsoft.com/en-
us/library/windows/desktop/aa370391%28v=vs.85%29.aspx">MsiSetProperty function</
a>. | 60 * The center of the implementation is the <a href="http://msdn.microsoft.com/en-
us/library/windows/desktop/aa370391%28v=vs.85%29.aspx">MsiSetProperty function</
a>. |
61 */ | 61 */ |
62 void Property::operator=( const std::wstring & value ) | 62 void Property::operator=(const std::wstring& value) |
63 { | 63 { |
64 UINT x = MsiSetPropertyW( handle, name.c_str(), value.c_str() ) ; | 64 UINT x = MsiSetPropertyW(handle, name.c_str(), value.c_str()); |
65 if ( x != ERROR_SUCCESS ) | 65 if (x != ERROR_SUCCESS) |
66 { | 66 { |
67 throw WindowsApiError( "MsiSetPropertyW", x ) ; | 67 throw WindowsApiError("MsiSetPropertyW", x); |
68 } | 68 } |
69 } | 69 } |
OLD | NEW |