2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2002, 2005 Mike McCormack for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #define NONAMELESSUNION
31 #include "wine/debug.h"
37 #include "propvarutil.h"
38 #include "msiserver.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(msi
);
60 } PROPERTYSECTIONHEADER
;
82 static HRESULT (WINAPI
*pPropVariantChangeType
)
83 (PROPVARIANT
*ppropvarDest
, REFPROPVARIANT propvarSrc
,
84 PROPVAR_CHANGE_FLAGS flags
, VARTYPE vt
);
86 #define SECT_HDR_SIZE (sizeof(PROPERTYSECTIONHEADER))
88 static const WCHAR szSumInfo
[] = { 5 ,'S','u','m','m','a','r','y',
89 'I','n','f','o','r','m','a','t','i','o','n',0 };
91 static void free_prop( PROPVARIANT
*prop
)
93 if (prop
->vt
== VT_LPSTR
)
94 msi_free( prop
->u
.pszVal
);
98 static void MSI_CloseSummaryInfo( MSIOBJECTHDR
*arg
)
100 MSISUMMARYINFO
*si
= (MSISUMMARYINFO
*) arg
;
103 for( i
= 0; i
< MSI_MAX_PROPS
; i
++ )
104 free_prop( &si
->property
[i
] );
105 IStorage_Release( si
->storage
);
108 static UINT
get_type( UINT uiProperty
)
126 case PID_LASTPRINTED
:
128 case PID_LASTSAVE_DTM
:
140 static UINT
get_property_count( const PROPVARIANT
*property
)
146 for( i
= 0; i
< MSI_MAX_PROPS
; i
++ )
147 if( property
[i
].vt
!= VT_EMPTY
)
152 static UINT
propvar_changetype(PROPVARIANT
*changed
, PROPVARIANT
*property
, VARTYPE vt
)
155 HMODULE propsys
= LoadLibraryA("propsys.dll");
156 pPropVariantChangeType
= (void *)GetProcAddress(propsys
, "PropVariantChangeType");
158 if (!pPropVariantChangeType
)
160 ERR("PropVariantChangeType function missing!\n");
161 return ERROR_FUNCTION_FAILED
;
164 hr
= pPropVariantChangeType(changed
, property
, 0, vt
);
165 return (hr
== S_OK
) ? ERROR_SUCCESS
: ERROR_FUNCTION_FAILED
;
168 /* FIXME: doesn't deal with endian conversion */
169 static void read_properties_from_data( PROPVARIANT
*prop
, LPBYTE data
, DWORD sz
)
174 PROPERTY_DATA
*propdata
;
175 PROPVARIANT property
, *ptr
;
177 PROPERTYIDOFFSET
*idofs
;
178 PROPERTYSECTIONHEADER
*section_hdr
;
180 section_hdr
= (PROPERTYSECTIONHEADER
*) &data
[0];
181 idofs
= (PROPERTYIDOFFSET
*) &data
[SECT_HDR_SIZE
];
183 /* now set all the properties */
184 for( i
= 0; i
< section_hdr
->cProperties
; i
++ )
186 if( idofs
[i
].propid
>= MSI_MAX_PROPS
)
188 ERR("Unknown property ID %d\n", idofs
[i
].propid
);
192 type
= get_type( idofs
[i
].propid
);
193 if( type
== VT_EMPTY
)
195 ERR("propid %d has unknown type\n", idofs
[i
].propid
);
199 propdata
= (PROPERTY_DATA
*) &data
[ idofs
[i
].dwOffset
];
201 /* check we don't run off the end of the data */
202 size
= sz
- idofs
[i
].dwOffset
- sizeof(DWORD
);
203 if( sizeof(DWORD
) > size
||
204 ( propdata
->type
== VT_FILETIME
&& sizeof(FILETIME
) > size
) ||
205 ( propdata
->type
== VT_LPSTR
&& (propdata
->u
.str
.len
+ sizeof(DWORD
)) > size
) )
207 ERR("not enough data\n");
211 property
.vt
= propdata
->type
;
212 if( propdata
->type
== VT_LPSTR
)
214 LPSTR str
= msi_alloc( propdata
->u
.str
.len
);
215 memcpy( str
, propdata
->u
.str
.str
, propdata
->u
.str
.len
);
216 str
[ propdata
->u
.str
.len
- 1 ] = 0;
217 property
.u
.pszVal
= str
;
219 else if( propdata
->type
== VT_FILETIME
)
220 property
.u
.filetime
= propdata
->u
.ft
;
221 else if( propdata
->type
== VT_I2
)
222 property
.u
.iVal
= propdata
->u
.i2
;
223 else if( propdata
->type
== VT_I4
)
224 property
.u
.lVal
= propdata
->u
.i4
;
226 /* check the type is the same as we expect */
227 if( type
!= propdata
->type
)
229 propvar_changetype(&changed
, &property
, type
);
235 prop
[ idofs
[i
].propid
] = *ptr
;
239 static UINT
load_summary_info( MSISUMMARYINFO
*si
, IStream
*stm
)
241 UINT ret
= ERROR_FUNCTION_FAILED
;
242 PROPERTYSETHEADER set_hdr
;
243 FORMATIDOFFSET format_hdr
;
244 PROPERTYSECTIONHEADER section_hdr
;
250 TRACE("%p %p\n", si
, stm
);
252 /* read the header */
254 r
= IStream_Read( stm
, &set_hdr
, sz
, &count
);
255 if( FAILED(r
) || count
!= sz
)
258 if( set_hdr
.wByteOrder
!= 0xfffe )
260 ERR("property set not big-endian %04X\n", set_hdr
.wByteOrder
);
264 sz
= sizeof format_hdr
;
265 r
= IStream_Read( stm
, &format_hdr
, sz
, &count
);
266 if( FAILED(r
) || count
!= sz
)
269 /* check the format id is correct */
270 if( !IsEqualGUID( &FMTID_SummaryInformation
, &format_hdr
.fmtid
) )
273 /* seek to the location of the section */
274 ofs
.QuadPart
= format_hdr
.dwOffset
;
275 r
= IStream_Seek( stm
, ofs
, STREAM_SEEK_SET
, NULL
);
279 /* read the section itself */
281 r
= IStream_Read( stm
, §ion_hdr
, sz
, &count
);
282 if( FAILED(r
) || count
!= sz
)
285 if( section_hdr
.cProperties
> MSI_MAX_PROPS
)
287 ERR("too many properties %d\n", section_hdr
.cProperties
);
291 data
= msi_alloc( section_hdr
.cbSection
);
295 memcpy( data
, §ion_hdr
, SECT_HDR_SIZE
);
297 /* read all the data in one go */
298 sz
= section_hdr
.cbSection
- SECT_HDR_SIZE
;
299 r
= IStream_Read( stm
, &data
[ SECT_HDR_SIZE
], sz
, &count
);
300 if( SUCCEEDED(r
) && count
== sz
)
301 read_properties_from_data( si
->property
, data
, sz
+ SECT_HDR_SIZE
);
303 ERR("failed to read properties %d %d\n", count
, sz
);
309 static DWORD
write_dword( LPBYTE data
, DWORD ofs
, DWORD val
)
313 data
[ofs
++] = val
&0xff;
314 data
[ofs
++] = (val
>>8)&0xff;
315 data
[ofs
++] = (val
>>16)&0xff;
316 data
[ofs
++] = (val
>>24)&0xff;
321 static DWORD
write_filetime( LPBYTE data
, DWORD ofs
, const FILETIME
*ft
)
323 write_dword( data
, ofs
, ft
->dwLowDateTime
);
324 write_dword( data
, ofs
+ 4, ft
->dwHighDateTime
);
328 static DWORD
write_string( LPBYTE data
, DWORD ofs
, LPCSTR str
)
330 DWORD len
= lstrlenA( str
) + 1;
331 write_dword( data
, ofs
, len
);
333 memcpy( &data
[ofs
+ 4], str
, len
);
334 return (7 + len
) & ~3;
337 static UINT
write_property_to_data( const PROPVARIANT
*prop
, LPBYTE data
)
341 if( prop
->vt
== VT_EMPTY
)
345 sz
+= write_dword( data
, sz
, prop
->vt
);
349 sz
+= write_dword( data
, sz
, prop
->u
.iVal
);
352 sz
+= write_dword( data
, sz
, prop
->u
.lVal
);
355 sz
+= write_filetime( data
, sz
, &prop
->u
.filetime
);
358 sz
+= write_string( data
, sz
, prop
->u
.pszVal
);
364 static UINT
save_summary_info( const MSISUMMARYINFO
* si
, IStream
*stm
)
366 UINT ret
= ERROR_FUNCTION_FAILED
;
367 PROPERTYSETHEADER set_hdr
;
368 FORMATIDOFFSET format_hdr
;
369 PROPERTYSECTIONHEADER section_hdr
;
370 PROPERTYIDOFFSET idofs
[MSI_MAX_PROPS
];
376 /* write the header */
378 memset( &set_hdr
, 0, sz
);
379 set_hdr
.wByteOrder
= 0xfffe;
381 set_hdr
.dwOSVer
= 0x00020005; /* build 5, platform id 2 */
382 /* set_hdr.clsID is {00000000-0000-0000-0000-000000000000} */
383 set_hdr
.reserved
= 1;
384 r
= IStream_Write( stm
, &set_hdr
, sz
, &count
);
385 if( FAILED(r
) || count
!= sz
)
388 /* write the format header */
389 sz
= sizeof format_hdr
;
390 format_hdr
.fmtid
= FMTID_SummaryInformation
;
391 format_hdr
.dwOffset
= sizeof format_hdr
+ sizeof set_hdr
;
392 r
= IStream_Write( stm
, &format_hdr
, sz
, &count
);
393 if( FAILED(r
) || count
!= sz
)
396 /* add up how much space the data will take and calculate the offsets */
397 section_hdr
.cbSection
= sizeof section_hdr
;
398 section_hdr
.cbSection
+= (get_property_count( si
->property
) * sizeof idofs
[0]);
399 section_hdr
.cProperties
= 0;
401 for( i
= 0; i
< MSI_MAX_PROPS
; i
++ )
403 sz
= write_property_to_data( &si
->property
[i
], NULL
);
406 idofs
[ section_hdr
.cProperties
].propid
= i
;
407 idofs
[ section_hdr
.cProperties
].dwOffset
= section_hdr
.cbSection
;
408 section_hdr
.cProperties
++;
409 section_hdr
.cbSection
+= sz
;
412 data
= msi_alloc_zero( section_hdr
.cbSection
);
415 memcpy( &data
[sz
], §ion_hdr
, sizeof section_hdr
);
416 sz
+= sizeof section_hdr
;
418 memcpy( &data
[sz
], idofs
, section_hdr
.cProperties
* sizeof idofs
[0] );
419 sz
+= section_hdr
.cProperties
* sizeof idofs
[0];
421 /* write out the data */
422 for( i
= 0; i
< MSI_MAX_PROPS
; i
++ )
423 sz
+= write_property_to_data( &si
->property
[i
], &data
[sz
] );
425 r
= IStream_Write( stm
, data
, sz
, &count
);
427 if( FAILED(r
) || count
!= sz
)
430 return ERROR_SUCCESS
;
433 MSISUMMARYINFO
*MSI_GetSummaryInformationW( IStorage
*stg
, UINT uiUpdateCount
)
440 TRACE("%p %d\n", stg
, uiUpdateCount
);
442 si
= alloc_msiobject( MSIHANDLETYPE_SUMMARYINFO
,
443 sizeof (MSISUMMARYINFO
), MSI_CloseSummaryInfo
);
447 memset( &si
->property
, 0, sizeof si
->property
);
448 si
->update_count
= uiUpdateCount
;
449 IStorage_AddRef( stg
);
452 /* read the stream... if we fail, we'll start with an empty property set */
453 grfMode
= STGM_READ
| STGM_SHARE_EXCLUSIVE
;
454 r
= IStorage_OpenStream( si
->storage
, szSumInfo
, 0, grfMode
, 0, &stm
);
457 load_summary_info( si
, stm
);
458 IStream_Release( stm
);
464 UINT WINAPI
MsiGetSummaryInformationW( MSIHANDLE hDatabase
,
465 LPCWSTR szDatabase
, UINT uiUpdateCount
, MSIHANDLE
*pHandle
)
469 UINT ret
= ERROR_FUNCTION_FAILED
;
471 TRACE("%ld %s %d %p\n", hDatabase
, debugstr_w(szDatabase
),
472 uiUpdateCount
, pHandle
);
475 return ERROR_INVALID_PARAMETER
;
479 ret
= MSI_OpenDatabaseW( szDatabase
, NULL
, &db
);
480 if( ret
!= ERROR_SUCCESS
)
485 db
= msihandle2msiinfo( hDatabase
, MSIHANDLETYPE_DATABASE
);
489 IWineMsiRemoteDatabase
*remote_database
;
491 remote_database
= (IWineMsiRemoteDatabase
*)msi_get_remote( hDatabase
);
492 if ( !remote_database
)
493 return ERROR_INVALID_HANDLE
;
495 hr
= IWineMsiRemoteDatabase_GetSummaryInformation( remote_database
,
496 uiUpdateCount
, pHandle
);
497 IWineMsiRemoteDatabase_Release( remote_database
);
501 if (HRESULT_FACILITY(hr
) == FACILITY_WIN32
)
502 return HRESULT_CODE(hr
);
504 return ERROR_FUNCTION_FAILED
;
507 return ERROR_SUCCESS
;
511 si
= MSI_GetSummaryInformationW( db
->storage
, uiUpdateCount
);
514 *pHandle
= alloc_msihandle( &si
->hdr
);
518 ret
= ERROR_NOT_ENOUGH_MEMORY
;
519 msiobj_release( &si
->hdr
);
523 msiobj_release( &db
->hdr
);
528 UINT WINAPI
MsiGetSummaryInformationA(MSIHANDLE hDatabase
,
529 LPCSTR szDatabase
, UINT uiUpdateCount
, MSIHANDLE
*pHandle
)
531 LPWSTR szwDatabase
= NULL
;
534 TRACE("%ld %s %d %p\n", hDatabase
, debugstr_a(szDatabase
),
535 uiUpdateCount
, pHandle
);
539 szwDatabase
= strdupAtoW( szDatabase
);
541 return ERROR_FUNCTION_FAILED
;
544 ret
= MsiGetSummaryInformationW(hDatabase
, szwDatabase
, uiUpdateCount
, pHandle
);
546 msi_free( szwDatabase
);
551 UINT WINAPI
MsiSummaryInfoGetPropertyCount(MSIHANDLE hSummaryInfo
, PUINT pCount
)
555 TRACE("%ld %p\n", hSummaryInfo
, pCount
);
557 si
= msihandle2msiinfo( hSummaryInfo
, MSIHANDLETYPE_SUMMARYINFO
);
559 return ERROR_INVALID_HANDLE
;
562 *pCount
= get_property_count( si
->property
);
563 msiobj_release( &si
->hdr
);
565 return ERROR_SUCCESS
;
568 static UINT
get_prop( MSIHANDLE handle
, UINT uiProperty
, UINT
*puiDataType
,
569 INT
*piValue
, FILETIME
*pftValue
, awstring
*str
, DWORD
*pcchValueBuf
)
573 UINT ret
= ERROR_SUCCESS
;
575 TRACE("%ld %d %p %p %p %p %p\n", handle
, uiProperty
, puiDataType
,
576 piValue
, pftValue
, str
, pcchValueBuf
);
578 if ( uiProperty
>= MSI_MAX_PROPS
)
580 if (puiDataType
) *puiDataType
= VT_EMPTY
;
581 return ERROR_UNKNOWN_PROPERTY
;
584 si
= msihandle2msiinfo( handle
, MSIHANDLETYPE_SUMMARYINFO
);
586 return ERROR_INVALID_HANDLE
;
588 prop
= &si
->property
[uiProperty
];
591 *puiDataType
= prop
->vt
;
597 *piValue
= prop
->u
.iVal
;
601 *piValue
= prop
->u
.lVal
;
610 len
= MultiByteToWideChar( CP_ACP
, 0, prop
->u
.pszVal
, -1,
611 str
->str
.w
, *pcchValueBuf
);
616 len
= lstrlenA( prop
->u
.pszVal
);
618 lstrcpynA(str
->str
.a
, prop
->u
.pszVal
, *pcchValueBuf
);
620 if (len
>= *pcchValueBuf
)
621 ret
= ERROR_MORE_DATA
;
627 *pftValue
= prop
->u
.filetime
;
632 FIXME("Unknown property variant type\n");
635 msiobj_release( &si
->hdr
);
639 LPWSTR
msi_suminfo_dup_string( MSISUMMARYINFO
*si
, UINT uiProperty
)
643 if ( uiProperty
>= MSI_MAX_PROPS
)
645 prop
= &si
->property
[uiProperty
];
646 if( prop
->vt
!= VT_LPSTR
)
648 return strdupAtoW( prop
->u
.pszVal
);
651 LPWSTR
msi_get_suminfo_product( IStorage
*stg
)
656 si
= MSI_GetSummaryInformationW( stg
, 0 );
659 ERR("no summary information!\n");
662 prod
= msi_suminfo_dup_string( si
, PID_REVNUMBER
);
663 msiobj_release( &si
->hdr
);
667 UINT WINAPI
MsiSummaryInfoGetPropertyA(
668 MSIHANDLE handle
, UINT uiProperty
, PUINT puiDataType
, LPINT piValue
,
669 FILETIME
*pftValue
, LPSTR szValueBuf
, LPDWORD pcchValueBuf
)
673 TRACE("%ld %d %p %p %p %p %p\n", handle
, uiProperty
, puiDataType
,
674 piValue
, pftValue
, szValueBuf
, pcchValueBuf
);
677 str
.str
.a
= szValueBuf
;
679 return get_prop( handle
, uiProperty
, puiDataType
, piValue
,
680 pftValue
, &str
, pcchValueBuf
);
683 UINT WINAPI
MsiSummaryInfoGetPropertyW(
684 MSIHANDLE handle
, UINT uiProperty
, PUINT puiDataType
, LPINT piValue
,
685 FILETIME
*pftValue
, LPWSTR szValueBuf
, LPDWORD pcchValueBuf
)
689 TRACE("%ld %d %p %p %p %p %p\n", handle
, uiProperty
, puiDataType
,
690 piValue
, pftValue
, szValueBuf
, pcchValueBuf
);
693 str
.str
.w
= szValueBuf
;
695 return get_prop( handle
, uiProperty
, puiDataType
, piValue
,
696 pftValue
, &str
, pcchValueBuf
);
699 static UINT
set_prop( MSIHANDLE handle
, UINT uiProperty
, UINT uiDataType
,
700 INT iValue
, FILETIME
* pftValue
, awcstring
*str
)
704 UINT type
, len
, ret
= ERROR_SUCCESS
;
706 TRACE("%ld %u %u %i %p %p\n", handle
, uiProperty
, uiDataType
,
707 iValue
, pftValue
, str
);
709 type
= get_type( uiProperty
);
710 if( type
== VT_EMPTY
|| type
!= uiDataType
)
711 return ERROR_DATATYPE_MISMATCH
;
713 if( uiDataType
== VT_LPSTR
&& !str
->str
.w
)
714 return ERROR_INVALID_PARAMETER
;
716 if( uiDataType
== VT_FILETIME
&& !pftValue
)
717 return ERROR_INVALID_PARAMETER
;
719 si
= msihandle2msiinfo( handle
, MSIHANDLETYPE_SUMMARYINFO
);
721 return ERROR_INVALID_HANDLE
;
723 prop
= &si
->property
[uiProperty
];
725 if( prop
->vt
== VT_EMPTY
)
727 if( !si
->update_count
)
729 ret
= ERROR_FUNCTION_FAILED
;
734 else if( prop
->vt
!= type
)
742 prop
->u
.lVal
= iValue
;
745 prop
->u
.iVal
= iValue
;
748 prop
->u
.filetime
= *pftValue
;
753 len
= WideCharToMultiByte( CP_ACP
, 0, str
->str
.w
, -1,
754 NULL
, 0, NULL
, NULL
);
755 prop
->u
.pszVal
= msi_alloc( len
);
756 WideCharToMultiByte( CP_ACP
, 0, str
->str
.w
, -1,
757 prop
->u
.pszVal
, len
, NULL
, NULL
);
761 len
= lstrlenA( str
->str
.a
) + 1;
762 prop
->u
.pszVal
= msi_alloc( len
);
763 lstrcpyA( prop
->u
.pszVal
, str
->str
.a
);
769 msiobj_release( &si
->hdr
);
773 UINT WINAPI
MsiSummaryInfoSetPropertyW( MSIHANDLE handle
, UINT uiProperty
,
774 UINT uiDataType
, INT iValue
, FILETIME
* pftValue
, LPCWSTR szValue
)
778 TRACE("%ld %u %u %i %p %s\n", handle
, uiProperty
, uiDataType
,
779 iValue
, pftValue
, debugstr_w(szValue
) );
783 return set_prop( handle
, uiProperty
, uiDataType
, iValue
, pftValue
, &str
);
786 UINT WINAPI
MsiSummaryInfoSetPropertyA( MSIHANDLE handle
, UINT uiProperty
,
787 UINT uiDataType
, INT iValue
, FILETIME
* pftValue
, LPCSTR szValue
)
791 TRACE("%ld %u %u %i %p %s\n", handle
, uiProperty
, uiDataType
,
792 iValue
, pftValue
, debugstr_a(szValue
) );
796 return set_prop( handle
, uiProperty
, uiDataType
, iValue
, pftValue
, &str
);
799 UINT WINAPI
MsiSummaryInfoPersist( MSIHANDLE handle
)
805 UINT ret
= ERROR_FUNCTION_FAILED
;
807 TRACE("%ld\n", handle
);
809 si
= msihandle2msiinfo( handle
, MSIHANDLETYPE_SUMMARYINFO
);
811 return ERROR_INVALID_HANDLE
;
813 grfMode
= STGM_CREATE
| STGM_READWRITE
| STGM_SHARE_EXCLUSIVE
;
814 r
= IStorage_CreateStream( si
->storage
, szSumInfo
, grfMode
, 0, 0, &stm
);
817 ret
= save_summary_info( si
, stm
);
818 IStream_Release( stm
);
820 msiobj_release( &si
->hdr
);