msi: Add another MsiGetFileVersionTest.
[wine] / dlls / msi / suminfo.c
1 /*
2  * Implementation of the Microsoft Installer (msi.dll)
3  *
4  * Copyright 2002, 2005 Mike McCormack for CodeWeavers
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <stdarg.h>
22
23 #define COBJMACROS
24 #define NONAMELESSUNION
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winreg.h"
29 #include "winnls.h"
30 #include "shlwapi.h"
31 #include "wine/debug.h"
32 #include "msi.h"
33 #include "msiquery.h"
34 #include "msidefs.h"
35 #include "msipriv.h"
36 #include "objidl.h"
37 #include "propvarutil.h"
38 #include "msiserver.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(msi);
41
42 #include "pshpack1.h"
43
44 typedef struct { 
45     WORD wByteOrder;
46     WORD wFormat;
47     DWORD dwOSVer;
48     CLSID clsID;
49     DWORD reserved;
50 } PROPERTYSETHEADER;
51
52 typedef struct { 
53     FMTID fmtid;
54     DWORD dwOffset;
55 } FORMATIDOFFSET;
56
57 typedef struct { 
58     DWORD cbSection;
59     DWORD cProperties;
60 } PROPERTYSECTIONHEADER; 
61  
62 typedef struct { 
63     DWORD propid;
64     DWORD dwOffset;
65 } PROPERTYIDOFFSET; 
66
67 typedef struct {
68     DWORD type;
69     union {
70         INT i4;
71         SHORT i2;
72         FILETIME ft;
73         struct {
74             DWORD len;
75             BYTE str[1];
76         } str;
77     } u;
78 } PROPERTY_DATA;
79  
80 #include "poppack.h"
81
82 static HRESULT (WINAPI *pPropVariantChangeType)
83     (PROPVARIANT *ppropvarDest, REFPROPVARIANT propvarSrc,
84      PROPVAR_CHANGE_FLAGS flags, VARTYPE vt);
85
86 #define SECT_HDR_SIZE (sizeof(PROPERTYSECTIONHEADER))
87
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 };
90
91 static void free_prop( PROPVARIANT *prop )
92 {
93     if (prop->vt == VT_LPSTR )
94         msi_free( prop->u.pszVal );
95     prop->vt = VT_EMPTY;
96 }
97
98 static void MSI_CloseSummaryInfo( MSIOBJECTHDR *arg )
99 {
100     MSISUMMARYINFO *si = (MSISUMMARYINFO *) arg;
101     DWORD i;
102
103     for( i = 0; i < MSI_MAX_PROPS; i++ )
104         free_prop( &si->property[i] );
105     IStorage_Release( si->storage );
106 }
107
108 static UINT get_type( UINT uiProperty )
109 {
110     switch( uiProperty )
111     {
112     case PID_CODEPAGE:
113          return VT_I2;
114
115     case PID_SUBJECT:
116     case PID_AUTHOR:
117     case PID_KEYWORDS:
118     case PID_COMMENTS:
119     case PID_TEMPLATE:
120     case PID_LASTAUTHOR:
121     case PID_REVNUMBER:
122     case PID_APPNAME:
123     case PID_TITLE:
124          return VT_LPSTR;
125
126     case PID_LASTPRINTED:
127     case PID_CREATE_DTM:
128     case PID_LASTSAVE_DTM:
129          return VT_FILETIME;
130
131     case PID_WORDCOUNT:
132     case PID_CHARCOUNT:
133     case PID_SECURITY:
134     case PID_PAGECOUNT:
135          return VT_I4;
136     }
137     return VT_EMPTY;
138 }
139
140 static UINT get_property_count( const PROPVARIANT *property )
141 {
142     UINT i, n = 0;
143
144     if( !property )
145         return n;
146     for( i = 0; i < MSI_MAX_PROPS; i++ )
147         if( property[i].vt != VT_EMPTY )
148             n++;
149     return n;
150 }
151
152 static UINT propvar_changetype(PROPVARIANT *changed, PROPVARIANT *property, VARTYPE vt)
153 {
154     HRESULT hr;
155     HMODULE propsys = LoadLibraryA("propsys.dll");
156     pPropVariantChangeType = (void *)GetProcAddress(propsys, "PropVariantChangeType");
157
158     if (!pPropVariantChangeType)
159     {
160         ERR("PropVariantChangeType function missing!\n");
161         return ERROR_FUNCTION_FAILED;
162     }
163
164     hr = pPropVariantChangeType(changed, property, 0, vt);
165     return (hr == S_OK) ? ERROR_SUCCESS : ERROR_FUNCTION_FAILED;
166 }
167
168 /* FIXME: doesn't deal with endian conversion */
169 static void read_properties_from_data( PROPVARIANT *prop, LPBYTE data, DWORD sz )
170 {
171     UINT type;
172     DWORD i;
173     int size;
174     PROPERTY_DATA *propdata;
175     PROPVARIANT property, *ptr;
176     PROPVARIANT changed;
177     PROPERTYIDOFFSET *idofs;
178     PROPERTYSECTIONHEADER *section_hdr;
179
180     section_hdr = (PROPERTYSECTIONHEADER*) &data[0];
181     idofs = (PROPERTYIDOFFSET*) &data[SECT_HDR_SIZE];
182
183     /* now set all the properties */
184     for( i = 0; i < section_hdr->cProperties; i++ )
185     {
186         if( idofs[i].propid >= MSI_MAX_PROPS )
187         {
188             ERR("Unknown property ID %d\n", idofs[i].propid );
189             break;
190         }
191
192         type = get_type( idofs[i].propid );
193         if( type == VT_EMPTY )
194         {
195             ERR("propid %d has unknown type\n", idofs[i].propid);
196             break;
197         }
198
199         propdata = (PROPERTY_DATA*) &data[ idofs[i].dwOffset ];
200
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 ) )
206         {
207             ERR("not enough data\n");
208             break;
209         }
210
211         property.vt = propdata->type;
212         if( propdata->type == VT_LPSTR )
213         {
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;
218         }
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;
225
226         /* check the type is the same as we expect */
227         if( type != propdata->type )
228         {
229             propvar_changetype(&changed, &property, type);
230             ptr = &changed;
231         }
232         else
233             ptr = &property;
234
235         prop[ idofs[i].propid ] = *ptr;
236     }
237 }
238
239 static UINT load_summary_info( MSISUMMARYINFO *si, IStream *stm )
240 {
241     UINT ret = ERROR_FUNCTION_FAILED;
242     PROPERTYSETHEADER set_hdr;
243     FORMATIDOFFSET format_hdr;
244     PROPERTYSECTIONHEADER section_hdr;
245     LPBYTE data = NULL;
246     LARGE_INTEGER ofs;
247     ULONG count, sz;
248     HRESULT r;
249
250     TRACE("%p %p\n", si, stm);
251
252     /* read the header */
253     sz = sizeof set_hdr;
254     r = IStream_Read( stm, &set_hdr, sz, &count );
255     if( FAILED(r) || count != sz )
256         return ret;
257
258     if( set_hdr.wByteOrder != 0xfffe )
259     {
260         ERR("property set not big-endian %04X\n", set_hdr.wByteOrder);
261         return ret;
262     }
263
264     sz = sizeof format_hdr;
265     r = IStream_Read( stm, &format_hdr, sz, &count );
266     if( FAILED(r) || count != sz )
267         return ret;
268
269     /* check the format id is correct */
270     if( !IsEqualGUID( &FMTID_SummaryInformation, &format_hdr.fmtid ) )
271         return ret;
272
273     /* seek to the location of the section */
274     ofs.QuadPart = format_hdr.dwOffset;
275     r = IStream_Seek( stm, ofs, STREAM_SEEK_SET, NULL );
276     if( FAILED(r) )
277         return ret;
278
279     /* read the section itself */
280     sz = SECT_HDR_SIZE;
281     r = IStream_Read( stm, &section_hdr, sz, &count );
282     if( FAILED(r) || count != sz )
283         return ret;
284
285     if( section_hdr.cProperties > MSI_MAX_PROPS )
286     {
287         ERR("too many properties %d\n", section_hdr.cProperties);
288         return ret;
289     }
290
291     data = msi_alloc( section_hdr.cbSection);
292     if( !data )
293         return ret;
294
295     memcpy( data, &section_hdr, SECT_HDR_SIZE );
296
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 );
302     else
303         ERR("failed to read properties %d %d\n", count, sz);
304
305     msi_free( data );
306     return ret;
307 }
308
309 static DWORD write_dword( LPBYTE data, DWORD ofs, DWORD val )
310 {
311     if( data )
312     {
313         data[ofs++] = val&0xff;
314         data[ofs++] = (val>>8)&0xff;
315         data[ofs++] = (val>>16)&0xff;
316         data[ofs++] = (val>>24)&0xff;
317     }
318     return 4;
319 }
320
321 static DWORD write_filetime( LPBYTE data, DWORD ofs, const FILETIME *ft )
322 {
323     write_dword( data, ofs, ft->dwLowDateTime );
324     write_dword( data, ofs + 4, ft->dwHighDateTime );
325     return 8;
326 }
327
328 static DWORD write_string( LPBYTE data, DWORD ofs, LPCSTR str )
329 {
330     DWORD len = lstrlenA( str ) + 1;
331     write_dword( data, ofs, len );
332     if( data )
333         memcpy( &data[ofs + 4], str, len );
334     return (7 + len) & ~3;
335 }
336
337 static UINT write_property_to_data( const PROPVARIANT *prop, LPBYTE data )
338 {
339     DWORD sz = 0;
340
341     if( prop->vt == VT_EMPTY )
342         return sz;
343
344     /* add the type */
345     sz += write_dword( data, sz, prop->vt );
346     switch( prop->vt )
347     {
348     case VT_I2:
349         sz += write_dword( data, sz, prop->u.iVal );
350         break;
351     case VT_I4:
352         sz += write_dword( data, sz, prop->u.lVal );
353         break;
354     case VT_FILETIME:
355         sz += write_filetime( data, sz, &prop->u.filetime );
356         break;
357     case VT_LPSTR:
358         sz += write_string( data, sz, prop->u.pszVal );
359         break;
360     }
361     return sz;
362 }
363
364 static UINT save_summary_info( const MSISUMMARYINFO * si, IStream *stm )
365 {
366     UINT ret = ERROR_FUNCTION_FAILED;
367     PROPERTYSETHEADER set_hdr;
368     FORMATIDOFFSET format_hdr;
369     PROPERTYSECTIONHEADER section_hdr;
370     PROPERTYIDOFFSET idofs[MSI_MAX_PROPS];
371     LPBYTE data = NULL;
372     ULONG count, sz;
373     HRESULT r;
374     int i;
375
376     /* write the header */
377     sz = sizeof set_hdr;
378     memset( &set_hdr, 0, sz );
379     set_hdr.wByteOrder = 0xfffe;
380     set_hdr.wFormat = 0;
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 )
386         return ret;
387
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 )
394         return ret;
395
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;
400     for( i = 0; i < MSI_MAX_PROPS; i++ )
401     {
402         sz = write_property_to_data( &si->property[i], NULL );
403         if( !sz )
404             continue;
405         idofs[ section_hdr.cProperties ].propid = i;
406         idofs[ section_hdr.cProperties ].dwOffset = section_hdr.cbSection;
407         section_hdr.cProperties++;
408         section_hdr.cbSection += sz;
409     }
410
411     data = msi_alloc_zero( section_hdr.cbSection );
412
413     sz = 0;
414     memcpy( &data[sz], &section_hdr, sizeof section_hdr );
415     sz += sizeof section_hdr;
416
417     memcpy( &data[sz], idofs, section_hdr.cProperties * sizeof idofs[0] );
418     sz += section_hdr.cProperties * sizeof idofs[0];
419
420     /* write out the data */
421     for( i = 0; i < MSI_MAX_PROPS; i++ )
422         sz += write_property_to_data( &si->property[i], &data[sz] );
423
424     r = IStream_Write( stm, data, sz, &count );
425     msi_free( data );
426     if( FAILED(r) || count != sz )
427         return ret;
428
429     return ERROR_SUCCESS;
430 }
431
432 MSISUMMARYINFO *MSI_GetSummaryInformationW( IStorage *stg, UINT uiUpdateCount )
433 {
434     IStream *stm = NULL;
435     MSISUMMARYINFO *si;
436     DWORD grfMode;
437     HRESULT r;
438
439     TRACE("%p %d\n", stg, uiUpdateCount );
440
441     si = alloc_msiobject( MSIHANDLETYPE_SUMMARYINFO, 
442                   sizeof (MSISUMMARYINFO), MSI_CloseSummaryInfo );
443     if( !si )
444         return si;
445
446     memset( &si->property, 0, sizeof si->property );
447     si->update_count = uiUpdateCount;
448     IStorage_AddRef( stg );
449     si->storage = stg;
450
451     /* read the stream... if we fail, we'll start with an empty property set */
452     grfMode = STGM_READ | STGM_SHARE_EXCLUSIVE;
453     r = IStorage_OpenStream( si->storage, szSumInfo, 0, grfMode, 0, &stm );
454     if( SUCCEEDED(r) )
455     {
456         load_summary_info( si, stm );
457         IStream_Release( stm );
458     }
459
460     return si;
461 }
462
463 UINT WINAPI MsiGetSummaryInformationW( MSIHANDLE hDatabase, 
464               LPCWSTR szDatabase, UINT uiUpdateCount, MSIHANDLE *pHandle )
465 {
466     MSISUMMARYINFO *si;
467     MSIDATABASE *db;
468     UINT ret = ERROR_FUNCTION_FAILED;
469
470     TRACE("%ld %s %d %p\n", hDatabase, debugstr_w(szDatabase),
471            uiUpdateCount, pHandle);
472
473     if( !pHandle )
474         return ERROR_INVALID_PARAMETER;
475
476     if( szDatabase )
477     {
478         ret = MSI_OpenDatabaseW( szDatabase, NULL, &db );
479         if( ret != ERROR_SUCCESS )
480             return ret;
481     }
482     else
483     {
484         db = msihandle2msiinfo( hDatabase, MSIHANDLETYPE_DATABASE );
485         if( !db )
486         {
487             HRESULT hr;
488             IWineMsiRemoteDatabase *remote_database;
489
490             remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( hDatabase );
491             if ( !remote_database )
492                 return ERROR_INVALID_HANDLE;
493
494             hr = IWineMsiRemoteDatabase_GetSummaryInformation( remote_database,
495                                                                uiUpdateCount, pHandle );
496             IWineMsiRemoteDatabase_Release( remote_database );
497
498             if (FAILED(hr))
499             {
500                 if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
501                     return HRESULT_CODE(hr);
502
503                 return ERROR_FUNCTION_FAILED;
504             }
505
506             return ERROR_SUCCESS;
507         }
508     }
509
510     si = MSI_GetSummaryInformationW( db->storage, uiUpdateCount );
511     if (si)
512     {
513         *pHandle = alloc_msihandle( &si->hdr );
514         if( *pHandle )
515             ret = ERROR_SUCCESS;
516         else
517             ret = ERROR_NOT_ENOUGH_MEMORY;
518         msiobj_release( &si->hdr );
519     }
520
521     if( db )
522         msiobj_release( &db->hdr );
523
524     return ret;
525 }
526
527 UINT WINAPI MsiGetSummaryInformationA(MSIHANDLE hDatabase, 
528               LPCSTR szDatabase, UINT uiUpdateCount, MSIHANDLE *pHandle)
529 {
530     LPWSTR szwDatabase = NULL;
531     UINT ret;
532
533     TRACE("%ld %s %d %p\n", hDatabase, debugstr_a(szDatabase), 
534           uiUpdateCount, pHandle);
535
536     if( szDatabase )
537     {
538         szwDatabase = strdupAtoW( szDatabase );
539         if( !szwDatabase )
540             return ERROR_FUNCTION_FAILED;
541     }
542
543     ret = MsiGetSummaryInformationW(hDatabase, szwDatabase, uiUpdateCount, pHandle);
544
545     msi_free( szwDatabase );
546
547     return ret;
548 }
549
550 UINT WINAPI MsiSummaryInfoGetPropertyCount(MSIHANDLE hSummaryInfo, PUINT pCount)
551 {
552     MSISUMMARYINFO *si;
553
554     TRACE("%ld %p\n", hSummaryInfo, pCount);
555
556     si = msihandle2msiinfo( hSummaryInfo, MSIHANDLETYPE_SUMMARYINFO );
557     if( !si )
558         return ERROR_INVALID_HANDLE;
559
560     if( pCount )
561         *pCount = get_property_count( si->property );
562     msiobj_release( &si->hdr );
563
564     return ERROR_SUCCESS;
565 }
566
567 static UINT get_prop( MSIHANDLE handle, UINT uiProperty, UINT *puiDataType,
568           INT *piValue, FILETIME *pftValue, awstring *str, DWORD *pcchValueBuf)
569 {
570     MSISUMMARYINFO *si;
571     PROPVARIANT *prop;
572     UINT ret = ERROR_SUCCESS;
573
574     TRACE("%ld %d %p %p %p %p %p\n", handle, uiProperty, puiDataType,
575           piValue, pftValue, str, pcchValueBuf);
576
577     if ( uiProperty >= MSI_MAX_PROPS )
578     {
579         if (puiDataType) *puiDataType = VT_EMPTY;
580         return ERROR_UNKNOWN_PROPERTY;
581     }
582
583     si = msihandle2msiinfo( handle, MSIHANDLETYPE_SUMMARYINFO );
584     if( !si )
585         return ERROR_INVALID_HANDLE;
586
587     prop = &si->property[uiProperty];
588
589     if( puiDataType )
590         *puiDataType = prop->vt;
591
592     switch( prop->vt )
593     {
594     case VT_I2:
595         if( piValue )
596             *piValue = prop->u.iVal;
597         break;
598     case VT_I4:
599         if( piValue )
600             *piValue = prop->u.lVal;
601         break;
602     case VT_LPSTR:
603         if( pcchValueBuf )
604         {
605             DWORD len = 0;
606
607             if( str->unicode )
608             {
609                 len = MultiByteToWideChar( CP_ACP, 0, prop->u.pszVal, -1,
610                                            str->str.w, *pcchValueBuf );
611                 len--;
612             }
613             else
614             {
615                 len = lstrlenA( prop->u.pszVal );
616                 if( str->str.a )
617                     lstrcpynA(str->str.a, prop->u.pszVal, *pcchValueBuf );
618             }
619             if (len >= *pcchValueBuf)
620                 ret = ERROR_MORE_DATA;
621             *pcchValueBuf = len;
622         }
623         break;
624     case VT_FILETIME:
625         if( pftValue )
626             *pftValue = prop->u.filetime;
627         break;
628     case VT_EMPTY:
629         break;
630     default:
631         FIXME("Unknown property variant type\n");
632         break;
633     }
634     msiobj_release( &si->hdr );
635     return ret;
636 }
637
638 LPWSTR msi_suminfo_dup_string( MSISUMMARYINFO *si, UINT uiProperty )
639 {
640     PROPVARIANT *prop;
641
642     if ( uiProperty >= MSI_MAX_PROPS )
643         return NULL;
644     prop = &si->property[uiProperty];
645     if( prop->vt != VT_LPSTR )
646         return NULL;
647     return strdupAtoW( prop->u.pszVal );
648 }
649
650 LPWSTR msi_get_suminfo_product( IStorage *stg )
651 {
652     MSISUMMARYINFO *si;
653     LPWSTR prod;
654
655     si = MSI_GetSummaryInformationW( stg, 0 );
656     if (!si)
657     {
658         ERR("no summary information!\n");
659         return NULL;
660     }
661     prod = msi_suminfo_dup_string( si, PID_REVNUMBER );
662     msiobj_release( &si->hdr );
663     return prod;
664 }
665
666 UINT WINAPI MsiSummaryInfoGetPropertyA(
667       MSIHANDLE handle, UINT uiProperty, PUINT puiDataType, LPINT piValue,
668       FILETIME *pftValue, LPSTR szValueBuf, LPDWORD pcchValueBuf)
669 {
670     awstring str;
671
672     TRACE("%ld %d %p %p %p %p %p\n", handle, uiProperty, puiDataType,
673           piValue, pftValue, szValueBuf, pcchValueBuf );
674
675     str.unicode = FALSE;
676     str.str.a = szValueBuf;
677
678     return get_prop( handle, uiProperty, puiDataType, piValue,
679                      pftValue, &str, pcchValueBuf );
680 }
681
682 UINT WINAPI MsiSummaryInfoGetPropertyW(
683       MSIHANDLE handle, UINT uiProperty, PUINT puiDataType, LPINT piValue,
684       FILETIME *pftValue, LPWSTR szValueBuf, LPDWORD pcchValueBuf)
685 {
686     awstring str;
687
688     TRACE("%ld %d %p %p %p %p %p\n", handle, uiProperty, puiDataType,
689           piValue, pftValue, szValueBuf, pcchValueBuf );
690
691     str.unicode = TRUE;
692     str.str.w = szValueBuf;
693
694     return get_prop( handle, uiProperty, puiDataType, piValue,
695                      pftValue, &str, pcchValueBuf );
696 }
697
698 static UINT set_prop( MSIHANDLE handle, UINT uiProperty, UINT uiDataType,
699                INT iValue, FILETIME* pftValue, awcstring *str )
700 {
701     MSISUMMARYINFO *si;
702     PROPVARIANT *prop;
703     UINT type, len, ret = ERROR_SUCCESS;
704
705     TRACE("%ld %u %u %i %p %p\n", handle, uiProperty, uiDataType,
706           iValue, pftValue, str );
707
708     type = get_type( uiProperty );
709     if( type == VT_EMPTY || type != uiDataType )
710         return ERROR_DATATYPE_MISMATCH;
711
712     if( uiDataType == VT_LPSTR && !str->str.w )
713         return ERROR_INVALID_PARAMETER;
714
715     if( uiDataType == VT_FILETIME && !pftValue )
716         return ERROR_INVALID_PARAMETER;
717
718     si = msihandle2msiinfo( handle, MSIHANDLETYPE_SUMMARYINFO );
719     if( !si )
720         return ERROR_INVALID_HANDLE;
721
722     prop = &si->property[uiProperty];
723
724     if( prop->vt == VT_EMPTY )
725     {
726         if( !si->update_count )
727         {
728             ret = ERROR_FUNCTION_FAILED;
729             goto end;
730         }
731         si->update_count--;
732     }
733     else if( prop->vt != type )
734         goto end;
735
736     free_prop( prop );
737     prop->vt = type;
738     switch( type )
739     {
740     case VT_I4:
741         prop->u.lVal = iValue;
742         break;
743     case VT_I2:
744         prop->u.iVal = iValue;
745         break;
746     case VT_FILETIME:
747         prop->u.filetime = *pftValue;
748         break;
749     case VT_LPSTR:
750         if( str->unicode )
751         {
752             len = WideCharToMultiByte( CP_ACP, 0, str->str.w, -1,
753                                        NULL, 0, NULL, NULL );
754             prop->u.pszVal = msi_alloc( len );
755             WideCharToMultiByte( CP_ACP, 0, str->str.w, -1,
756                                  prop->u.pszVal, len, NULL, NULL );
757         }
758         else
759         {
760             len = lstrlenA( str->str.a ) + 1;
761             prop->u.pszVal = msi_alloc( len );
762             lstrcpyA( prop->u.pszVal, str->str.a );
763         }
764         break;
765     }
766
767 end:
768     msiobj_release( &si->hdr );
769     return ret;
770 }
771
772 UINT WINAPI MsiSummaryInfoSetPropertyW( MSIHANDLE handle, UINT uiProperty,
773                UINT uiDataType, INT iValue, FILETIME* pftValue, LPCWSTR szValue )
774 {
775     awcstring str;
776
777     TRACE("%ld %u %u %i %p %s\n", handle, uiProperty, uiDataType,
778           iValue, pftValue, debugstr_w(szValue) );
779
780     str.unicode = TRUE;
781     str.str.w = szValue;
782     return set_prop( handle, uiProperty, uiDataType, iValue, pftValue, &str );
783 }
784
785 UINT WINAPI MsiSummaryInfoSetPropertyA( MSIHANDLE handle, UINT uiProperty,
786                UINT uiDataType, INT iValue, FILETIME* pftValue, LPCSTR szValue )
787 {
788     awcstring str;
789
790     TRACE("%ld %u %u %i %p %s\n", handle, uiProperty, uiDataType,
791           iValue, pftValue, debugstr_a(szValue) );
792
793     str.unicode = FALSE;
794     str.str.a = szValue;
795     return set_prop( handle, uiProperty, uiDataType, iValue, pftValue, &str );
796 }
797
798 UINT WINAPI MsiSummaryInfoPersist( MSIHANDLE handle )
799 {
800     IStream *stm = NULL;
801     MSISUMMARYINFO *si;
802     DWORD grfMode;
803     HRESULT r;
804     UINT ret = ERROR_FUNCTION_FAILED;
805
806     TRACE("%ld\n", handle );
807
808     si = msihandle2msiinfo( handle, MSIHANDLETYPE_SUMMARYINFO );
809     if( !si )
810         return ERROR_INVALID_HANDLE;
811
812     grfMode = STGM_CREATE | STGM_READWRITE | STGM_SHARE_EXCLUSIVE;
813     r = IStorage_CreateStream( si->storage, szSumInfo, grfMode, 0, 0, &stm );
814     if( SUCCEEDED(r) )
815     {
816         ret = save_summary_info( si, stm );
817         IStream_Release( stm );
818     }
819     msiobj_release( &si->hdr );
820
821     return ret;
822 }