Release 1.5.29.
[wine] / dlls / msi / package.c
1 /*
2  * Implementation of the Microsoft Installer (msi.dll)
3  *
4  * Copyright 2004 Aric Stewart 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 #define NONAMELESSUNION
22 #define NONAMELESSSTRUCT
23 #define COBJMACROS
24
25 #include <stdarg.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winreg.h"
29 #include "winnls.h"
30 #include "shlwapi.h"
31 #include "wingdi.h"
32 #include "wine/debug.h"
33 #include "msi.h"
34 #include "msiquery.h"
35 #include "objidl.h"
36 #include "wincrypt.h"
37 #include "winuser.h"
38 #include "wininet.h"
39 #include "winver.h"
40 #include "urlmon.h"
41 #include "shlobj.h"
42 #include "wine/unicode.h"
43 #include "objbase.h"
44 #include "msidefs.h"
45 #include "sddl.h"
46
47 #include "msipriv.h"
48 #include "msiserver.h"
49
50 WINE_DEFAULT_DEBUG_CHANNEL(msi);
51
52 static void remove_tracked_tempfiles( MSIPACKAGE *package )
53 {
54     struct list *item, *cursor;
55
56     LIST_FOR_EACH_SAFE( item, cursor, &package->tempfiles )
57     {
58         MSITEMPFILE *temp = LIST_ENTRY( item, MSITEMPFILE, entry );
59
60         list_remove( &temp->entry );
61         TRACE("deleting temp file %s\n", debugstr_w( temp->Path ));
62         DeleteFileW( temp->Path );
63         msi_free( temp->Path );
64         msi_free( temp );
65     }
66 }
67
68 static void free_feature( MSIFEATURE *feature )
69 {
70     struct list *item, *cursor;
71
72     LIST_FOR_EACH_SAFE( item, cursor, &feature->Children )
73     {
74         FeatureList *fl = LIST_ENTRY( item, FeatureList, entry );
75         list_remove( &fl->entry );
76         msi_free( fl );
77     }
78
79     LIST_FOR_EACH_SAFE( item, cursor, &feature->Components )
80     {
81         ComponentList *cl = LIST_ENTRY( item, ComponentList, entry );
82         list_remove( &cl->entry );
83         msi_free( cl );
84     }
85     msi_free( feature->Feature );
86     msi_free( feature->Feature_Parent );
87     msi_free( feature->Directory );
88     msi_free( feature->Description );
89     msi_free( feature->Title );
90     msi_free( feature );
91 }
92
93 static void free_folder( MSIFOLDER *folder )
94 {
95     struct list *item, *cursor;
96
97     LIST_FOR_EACH_SAFE( item, cursor, &folder->children )
98     {
99         FolderList *fl = LIST_ENTRY( item, FolderList, entry );
100         list_remove( &fl->entry );
101         msi_free( fl );
102     }
103     msi_free( folder->Parent );
104     msi_free( folder->Directory );
105     msi_free( folder->TargetDefault );
106     msi_free( folder->SourceLongPath );
107     msi_free( folder->SourceShortPath );
108     msi_free( folder->ResolvedTarget );
109     msi_free( folder->ResolvedSource );
110     msi_free( folder );
111 }
112
113 static void free_extension( MSIEXTENSION *ext )
114 {
115     struct list *item, *cursor;
116
117     LIST_FOR_EACH_SAFE( item, cursor, &ext->verbs )
118     {
119         MSIVERB *verb = LIST_ENTRY( item, MSIVERB, entry );
120
121         list_remove( &verb->entry );
122         msi_free( verb->Verb );
123         msi_free( verb->Command );
124         msi_free( verb->Argument );
125         msi_free( verb );
126     }
127
128     msi_free( ext->Extension );
129     msi_free( ext->ProgIDText );
130     msi_free( ext );
131 }
132
133 static void free_assembly( MSIASSEMBLY *assembly )
134 {
135     msi_free( assembly->feature );
136     msi_free( assembly->manifest );
137     msi_free( assembly->application );
138     msi_free( assembly->display_name );
139     if (assembly->tempdir) RemoveDirectoryW( assembly->tempdir );
140     msi_free( assembly->tempdir );
141     msi_free( assembly );
142 }
143
144 void msi_free_action_script( MSIPACKAGE *package, UINT script )
145 {
146     UINT i;
147     for (i = 0; i < package->script->ActionCount[script]; i++)
148         msi_free( package->script->Actions[script][i] );
149
150     msi_free( package->script->Actions[script] );
151     package->script->Actions[script] = NULL;
152     package->script->ActionCount[script] = 0;
153 }
154
155 static void free_package_structures( MSIPACKAGE *package )
156 {
157     struct list *item, *cursor;
158
159     LIST_FOR_EACH_SAFE( item, cursor, &package->features )
160     {
161         MSIFEATURE *feature = LIST_ENTRY( item, MSIFEATURE, entry );
162         list_remove( &feature->entry );
163         free_feature( feature );
164     }
165
166     LIST_FOR_EACH_SAFE( item, cursor, &package->folders )
167     {
168         MSIFOLDER *folder = LIST_ENTRY( item, MSIFOLDER, entry );
169         list_remove( &folder->entry );
170         free_folder( folder );
171     }
172
173     LIST_FOR_EACH_SAFE( item, cursor, &package->components )
174     {
175         MSICOMPONENT *comp = LIST_ENTRY( item, MSICOMPONENT, entry );
176
177         list_remove( &comp->entry );
178         msi_free( comp->Component );
179         msi_free( comp->ComponentId );
180         msi_free( comp->Directory );
181         msi_free( comp->Condition );
182         msi_free( comp->KeyPath );
183         msi_free( comp->FullKeypath );
184         if (comp->assembly) free_assembly( comp->assembly );
185         msi_free( comp );
186     }
187
188     LIST_FOR_EACH_SAFE( item, cursor, &package->files )
189     {
190         MSIFILE *file = LIST_ENTRY( item, MSIFILE, entry );
191
192         list_remove( &file->entry );
193         msi_free( file->File );
194         msi_free( file->FileName );
195         msi_free( file->ShortName );
196         msi_free( file->LongName );
197         msi_free( file->Version );
198         msi_free( file->Language );
199         msi_free( file->TargetPath );
200         msi_free( file );
201     }
202
203     /* clean up extension, progid, class and verb structures */
204     LIST_FOR_EACH_SAFE( item, cursor, &package->classes )
205     {
206         MSICLASS *cls = LIST_ENTRY( item, MSICLASS, entry );
207
208         list_remove( &cls->entry );
209         msi_free( cls->clsid );
210         msi_free( cls->Context );
211         msi_free( cls->Description );
212         msi_free( cls->FileTypeMask );
213         msi_free( cls->IconPath );
214         msi_free( cls->DefInprocHandler );
215         msi_free( cls->DefInprocHandler32 );
216         msi_free( cls->Argument );
217         msi_free( cls->ProgIDText );
218         msi_free( cls );
219     }
220
221     LIST_FOR_EACH_SAFE( item, cursor, &package->extensions )
222     {
223         MSIEXTENSION *ext = LIST_ENTRY( item, MSIEXTENSION, entry );
224
225         list_remove( &ext->entry );
226         free_extension( ext );
227     }
228
229     LIST_FOR_EACH_SAFE( item, cursor, &package->progids )
230     {
231         MSIPROGID *progid = LIST_ENTRY( item, MSIPROGID, entry );
232
233         list_remove( &progid->entry );
234         msi_free( progid->ProgID );
235         msi_free( progid->Description );
236         msi_free( progid->IconPath );
237         msi_free( progid );
238     }
239
240     LIST_FOR_EACH_SAFE( item, cursor, &package->mimes )
241     {
242         MSIMIME *mt = LIST_ENTRY( item, MSIMIME, entry );
243
244         list_remove( &mt->entry );
245         msi_free( mt->suffix );
246         msi_free( mt->clsid );
247         msi_free( mt->ContentType );
248         msi_free( mt );
249     }
250
251     LIST_FOR_EACH_SAFE( item, cursor, &package->appids )
252     {
253         MSIAPPID *appid = LIST_ENTRY( item, MSIAPPID, entry );
254
255         list_remove( &appid->entry );
256         msi_free( appid->AppID );
257         msi_free( appid->RemoteServerName );
258         msi_free( appid->LocalServer );
259         msi_free( appid->ServiceParameters );
260         msi_free( appid->DllSurrogate );
261         msi_free( appid );
262     }
263
264     LIST_FOR_EACH_SAFE( item, cursor, &package->sourcelist_info )
265     {
266         MSISOURCELISTINFO *info = LIST_ENTRY( item, MSISOURCELISTINFO, entry );
267
268         list_remove( &info->entry );
269         msi_free( info->value );
270         msi_free( info );
271     }
272
273     LIST_FOR_EACH_SAFE( item, cursor, &package->sourcelist_media )
274     {
275         MSIMEDIADISK *info = LIST_ENTRY( item, MSIMEDIADISK, entry );
276
277         list_remove( &info->entry );
278         msi_free( info->volume_label );
279         msi_free( info->disk_prompt );
280         msi_free( info );
281     }
282
283     if (package->script)
284     {
285         INT i;
286         UINT j;
287
288         for (i = 0; i < SCRIPT_MAX; i++)
289             msi_free_action_script( package, i );
290
291         for (j = 0; j < package->script->UniqueActionsCount; j++)
292             msi_free( package->script->UniqueActions[j] );
293
294         msi_free( package->script->UniqueActions );
295         msi_free( package->script );
296     }
297
298     LIST_FOR_EACH_SAFE( item, cursor, &package->binaries )
299     {
300         MSIBINARY *binary = LIST_ENTRY( item, MSIBINARY, entry );
301
302         list_remove( &binary->entry );
303         if (binary->module)
304             FreeLibrary( binary->module );
305         if (!DeleteFileW( binary->tmpfile ))
306             ERR("failed to delete %s (%u)\n", debugstr_w(binary->tmpfile), GetLastError());
307         msi_free( binary->source );
308         msi_free( binary->tmpfile );
309         msi_free( binary );
310     }
311
312     LIST_FOR_EACH_SAFE( item, cursor, &package->cabinet_streams )
313     {
314         MSICABINETSTREAM *cab = LIST_ENTRY( item, MSICABINETSTREAM, entry );
315
316         list_remove( &cab->entry );
317         IStorage_Release( cab->storage );
318         msi_free( cab->stream );
319         msi_free( cab );
320     }
321
322     LIST_FOR_EACH_SAFE( item, cursor, &package->patches )
323     {
324         MSIPATCHINFO *patch = LIST_ENTRY( item, MSIPATCHINFO, entry );
325
326         list_remove( &patch->entry );
327         if (patch->delete_on_close && !DeleteFileW( patch->localfile ))
328         {
329             ERR("failed to delete %s (%u)\n", debugstr_w(patch->localfile), GetLastError());
330         }
331         msi_free_patchinfo( patch );
332     }
333
334     msi_free( package->BaseURL );
335     msi_free( package->PackagePath );
336     msi_free( package->ProductCode );
337     msi_free( package->ActionFormat );
338     msi_free( package->LastAction );
339     msi_free( package->langids );
340
341     remove_tracked_tempfiles(package);
342
343     /* cleanup control event subscriptions */
344     ControlEvent_CleanupSubscriptions( package );
345 }
346
347 static void MSI_FreePackage( MSIOBJECTHDR *arg)
348 {
349     MSIPACKAGE *package = (MSIPACKAGE *)arg;
350
351     msi_destroy_assembly_caches( package );
352
353     if( package->dialog )
354         msi_dialog_destroy( package->dialog );
355
356     msiobj_release( &package->db->hdr );
357     free_package_structures(package);
358     CloseHandle( package->log_file );
359
360     if (package->delete_on_close) DeleteFileW( package->localfile );
361     msi_free( package->localfile );
362 }
363
364 static UINT create_temp_property_table(MSIPACKAGE *package)
365 {
366     static const WCHAR query[] = {
367         'C','R','E','A','T','E',' ','T','A','B','L','E',' ',
368         '`','_','P','r','o','p','e','r','t','y','`',' ','(',' ',
369         '`','_','P','r','o','p','e','r','t','y','`',' ',
370         'C','H','A','R','(','5','6',')',' ','N','O','T',' ','N','U','L','L',' ',
371         'T','E','M','P','O','R','A','R','Y',',',' ',
372         '`','V','a','l','u','e','`',' ','C','H','A','R','(','9','8',')',' ',
373         'N','O','T',' ','N','U','L','L',' ','T','E','M','P','O','R','A','R','Y',
374         ' ','P','R','I','M','A','R','Y',' ','K','E','Y',' ',
375         '`','_','P','r','o','p','e','r','t','y','`',')',' ','H','O','L','D',0};
376     MSIQUERY *view;
377     UINT rc;
378
379     rc = MSI_DatabaseOpenViewW(package->db, query, &view);
380     if (rc != ERROR_SUCCESS)
381         return rc;
382
383     rc = MSI_ViewExecute(view, 0);
384     MSI_ViewClose(view);
385     msiobj_release(&view->hdr);
386     return rc;
387 }
388
389 UINT msi_clone_properties(MSIPACKAGE *package)
390 {
391     static const WCHAR query_select[] = {
392         'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
393         '`','P','r','o','p','e','r','t','y','`',0};
394     static const WCHAR query_insert[] = {
395         'I','N','S','E','R','T',' ','I','N','T','O',' ',
396         '`','_','P','r','o','p','e','r','t','y','`',' ',
397         '(','`','_','P','r','o','p','e','r','t','y','`',',','`','V','a','l','u','e','`',')',' ',
398         'V','A','L','U','E','S',' ','(','?',',','?',')',0};
399     static const WCHAR query_update[] = {
400         'U','P','D','A','T','E',' ','`','_','P','r','o','p','e','r','t','y','`',' ',
401         'S','E','T',' ','`','V','a','l','u','e','`',' ','=',' ','?',' ',
402         'W','H','E','R','E',' ','`','_','P','r','o','p','e','r','t','y','`',' ','=',' ','?',0};
403     MSIQUERY *view_select;
404     UINT rc;
405
406     rc = MSI_DatabaseOpenViewW( package->db, query_select, &view_select );
407     if (rc != ERROR_SUCCESS)
408         return rc;
409
410     rc = MSI_ViewExecute( view_select, 0 );
411     if (rc != ERROR_SUCCESS)
412     {
413         MSI_ViewClose( view_select );
414         msiobj_release( &view_select->hdr );
415         return rc;
416     }
417
418     while (1)
419     {
420         MSIQUERY *view_insert, *view_update;
421         MSIRECORD *rec_select;
422
423         rc = MSI_ViewFetch( view_select, &rec_select );
424         if (rc != ERROR_SUCCESS)
425             break;
426
427         rc = MSI_DatabaseOpenViewW( package->db, query_insert, &view_insert );
428         if (rc != ERROR_SUCCESS)
429         {
430             msiobj_release( &rec_select->hdr );
431             continue;
432         }
433
434         rc = MSI_ViewExecute( view_insert, rec_select );
435         MSI_ViewClose( view_insert );
436         msiobj_release( &view_insert->hdr );
437         if (rc != ERROR_SUCCESS)
438         {
439             MSIRECORD *rec_update;
440
441             TRACE("insert failed, trying update\n");
442
443             rc = MSI_DatabaseOpenViewW( package->db, query_update, &view_update );
444             if (rc != ERROR_SUCCESS)
445             {
446                 WARN("open view failed %u\n", rc);
447                 msiobj_release( &rec_select->hdr );
448                 continue;
449             }
450
451             rec_update = MSI_CreateRecord( 2 );
452             MSI_RecordCopyField( rec_select, 1, rec_update, 2 );
453             MSI_RecordCopyField( rec_select, 2, rec_update, 1 );
454             rc = MSI_ViewExecute( view_update, rec_update );
455             if (rc != ERROR_SUCCESS)
456                 WARN("update failed %u\n", rc);
457
458             MSI_ViewClose( view_update );
459             msiobj_release( &view_update->hdr );
460             msiobj_release( &rec_update->hdr );
461         }
462
463         msiobj_release( &rec_select->hdr );
464     }
465
466     MSI_ViewClose( view_select );
467     msiobj_release( &view_select->hdr );
468     return rc;
469 }
470
471 /*
472  * set_installed_prop
473  *
474  * Sets the "Installed" property to indicate that
475  *  the product is installed for the current user.
476  */
477 static UINT set_installed_prop( MSIPACKAGE *package )
478 {
479     HKEY hkey;
480     UINT r;
481
482     if (!package->ProductCode) return ERROR_FUNCTION_FAILED;
483
484     r = MSIREG_OpenUninstallKey( package->ProductCode, package->platform, &hkey, FALSE );
485     if (r == ERROR_SUCCESS)
486     {
487         RegCloseKey( hkey );
488         msi_set_property( package->db, szInstalled, szOne, -1 );
489     }
490     return r;
491 }
492
493 static UINT set_user_sid_prop( MSIPACKAGE *package )
494 {
495     SID_NAME_USE use;
496     LPWSTR user_name;
497     LPWSTR sid_str = NULL, dom = NULL;
498     DWORD size, dom_size;
499     PSID psid = NULL;
500     UINT r = ERROR_FUNCTION_FAILED;
501
502     size = 0;
503     GetUserNameW( NULL, &size );
504
505     user_name = msi_alloc( (size + 1) * sizeof(WCHAR) );
506     if (!user_name)
507         return ERROR_OUTOFMEMORY;
508
509     if (!GetUserNameW( user_name, &size ))
510         goto done;
511
512     size = 0;
513     dom_size = 0;
514     LookupAccountNameW( NULL, user_name, NULL, &size, NULL, &dom_size, &use );
515
516     psid = msi_alloc( size );
517     dom = msi_alloc( dom_size*sizeof (WCHAR) );
518     if (!psid || !dom)
519     {
520         r = ERROR_OUTOFMEMORY;
521         goto done;
522     }
523
524     if (!LookupAccountNameW( NULL, user_name, psid, &size, dom, &dom_size, &use ))
525         goto done;
526
527     if (!ConvertSidToStringSidW( psid, &sid_str ))
528         goto done;
529
530     r = msi_set_property( package->db, szUserSID, sid_str, -1 );
531
532 done:
533     LocalFree( sid_str );
534     msi_free( dom );
535     msi_free( psid );
536     msi_free( user_name );
537
538     return r;
539 }
540
541 static LPWSTR get_fusion_filename(MSIPACKAGE *package)
542 {
543     HKEY netsetup;
544     LONG res;
545     LPWSTR file = NULL;
546     DWORD index = 0, size;
547     WCHAR ver[MAX_PATH];
548     WCHAR name[MAX_PATH];
549     WCHAR windir[MAX_PATH];
550
551     static const WCHAR fusion[] = {'f','u','s','i','o','n','.','d','l','l',0};
552     static const WCHAR sub[] = {
553         'S','o','f','t','w','a','r','e','\\',
554         'M','i','c','r','o','s','o','f','t','\\',
555         'N','E','T',' ','F','r','a','m','e','w','o','r','k',' ','S','e','t','u','p','\\',
556         'N','D','P',0
557     };
558     static const WCHAR subdir[] = {
559         'M','i','c','r','o','s','o','f','t','.','N','E','T','\\',
560         'F','r','a','m','e','w','o','r','k','\\',0
561     };
562
563     res = RegOpenKeyExW(HKEY_LOCAL_MACHINE, sub, 0, KEY_ENUMERATE_SUB_KEYS, &netsetup);
564     if (res != ERROR_SUCCESS)
565         return NULL;
566
567     GetWindowsDirectoryW(windir, MAX_PATH);
568
569     ver[0] = '\0';
570     size = MAX_PATH;
571     while (RegEnumKeyExW(netsetup, index, name, &size, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
572     {
573         index++;
574
575         /* verify existence of fusion.dll .Net 3.0 does not install a new one */
576         if (strcmpW( ver, name ) < 0)
577         {
578             LPWSTR check;
579             size = lstrlenW(windir) + lstrlenW(subdir) + lstrlenW(name) +lstrlenW(fusion) + 3;
580             check = msi_alloc(size * sizeof(WCHAR));
581
582             if (!check)
583             {
584                 msi_free(file);
585                 return NULL;
586             }
587
588             lstrcpyW(check, windir);
589             lstrcatW(check, szBackSlash);
590             lstrcatW(check, subdir);
591             lstrcatW(check, name);
592             lstrcatW(check, szBackSlash);
593             lstrcatW(check, fusion);
594
595             if(GetFileAttributesW(check) != INVALID_FILE_ATTRIBUTES)
596             {
597                 msi_free(file);
598                 file = check;
599                 lstrcpyW(ver, name);
600             }
601             else
602                 msi_free(check);
603         }
604     }
605
606     RegCloseKey(netsetup);
607     return file;
608 }
609
610 typedef struct tagLANGANDCODEPAGE
611 {
612   WORD wLanguage;
613   WORD wCodePage;
614 } LANGANDCODEPAGE;
615
616 static void set_msi_assembly_prop(MSIPACKAGE *package)
617 {
618     UINT val_len;
619     DWORD size, handle;
620     LPVOID version = NULL;
621     WCHAR buf[MAX_PATH];
622     LPWSTR fusion, verstr;
623     LANGANDCODEPAGE *translate;
624
625     static const WCHAR netasm[] = {
626         'M','s','i','N','e','t','A','s','s','e','m','b','l','y','S','u','p','p','o','r','t',0
627     };
628     static const WCHAR translation[] = {
629         '\\','V','a','r','F','i','l','e','I','n','f','o',
630         '\\','T','r','a','n','s','l','a','t','i','o','n',0
631     };
632     static const WCHAR verfmt[] = {
633         '\\','S','t','r','i','n','g','F','i','l','e','I','n','f','o',
634         '\\','%','0','4','x','%','0','4','x',
635         '\\','P','r','o','d','u','c','t','V','e','r','s','i','o','n',0
636     };
637
638     fusion = get_fusion_filename(package);
639     if (!fusion)
640         return;
641
642     size = GetFileVersionInfoSizeW(fusion, &handle);
643     if (!size)
644         goto done;
645
646     version = msi_alloc(size);
647     if (!version)
648         goto done;
649
650     if (!GetFileVersionInfoW(fusion, handle, size, version))
651         goto done;
652
653     if (!VerQueryValueW(version, translation, (LPVOID *)&translate, &val_len))
654         goto done;
655
656     sprintfW(buf, verfmt, translate[0].wLanguage, translate[0].wCodePage);
657
658     if (!VerQueryValueW(version, buf, (LPVOID *)&verstr, &val_len))
659         goto done;
660
661     if (!val_len || !verstr)
662         goto done;
663
664     msi_set_property( package->db, netasm, verstr, -1 );
665
666 done:
667     msi_free(fusion);
668     msi_free(version);
669 }
670
671 static VOID set_installer_properties(MSIPACKAGE *package)
672 {
673     WCHAR *ptr;
674     OSVERSIONINFOEXW OSVersion;
675     MEMORYSTATUSEX msex;
676     DWORD verval, len;
677     WCHAR pth[MAX_PATH], verstr[11], bufstr[22];
678     HDC dc;
679     HKEY hkey;
680     LPWSTR username, companyname;
681     SYSTEM_INFO sys_info;
682     SYSTEMTIME systemtime;
683     LANGID langid;
684
685     static const WCHAR szCommonFilesFolder[] = {'C','o','m','m','o','n','F','i','l','e','s','F','o','l','d','e','r',0};
686     static const WCHAR szProgramFilesFolder[] = {'P','r','o','g','r','a','m','F','i','l','e','s','F','o','l','d','e','r',0};
687     static const WCHAR szCommonAppDataFolder[] = {'C','o','m','m','o','n','A','p','p','D','a','t','a','F','o','l','d','e','r',0};
688     static const WCHAR szFavoritesFolder[] = {'F','a','v','o','r','i','t','e','s','F','o','l','d','e','r',0};
689     static const WCHAR szFontsFolder[] = {'F','o','n','t','s','F','o','l','d','e','r',0};
690     static const WCHAR szSendToFolder[] = {'S','e','n','d','T','o','F','o','l','d','e','r',0};
691     static const WCHAR szStartMenuFolder[] = {'S','t','a','r','t','M','e','n','u','F','o','l','d','e','r',0};
692     static const WCHAR szStartupFolder[] = {'S','t','a','r','t','u','p','F','o','l','d','e','r',0};
693     static const WCHAR szTemplateFolder[] = {'T','e','m','p','l','a','t','e','F','o','l','d','e','r',0};
694     static const WCHAR szDesktopFolder[] = {'D','e','s','k','t','o','p','F','o','l','d','e','r',0};
695     static const WCHAR szProgramMenuFolder[] = {'P','r','o','g','r','a','m','M','e','n','u','F','o','l','d','e','r',0};
696     static const WCHAR szAdminToolsFolder[] = {'A','d','m','i','n','T','o','o','l','s','F','o','l','d','e','r',0};
697     static const WCHAR szSystemFolder[] = {'S','y','s','t','e','m','F','o','l','d','e','r',0};
698     static const WCHAR szSystem16Folder[] = {'S','y','s','t','e','m','1','6','F','o','l','d','e','r',0};
699     static const WCHAR szLocalAppDataFolder[] = {'L','o','c','a','l','A','p','p','D','a','t','a','F','o','l','d','e','r',0};
700     static const WCHAR szMyPicturesFolder[] = {'M','y','P','i','c','t','u','r','e','s','F','o','l','d','e','r',0};
701     static const WCHAR szPersonalFolder[] = {'P','e','r','s','o','n','a','l','F','o','l','d','e','r',0};
702     static const WCHAR szWindowsVolume[] = {'W','i','n','d','o','w','s','V','o','l','u','m','e',0};
703     static const WCHAR szPrivileged[] = {'P','r','i','v','i','l','e','g','e','d',0};
704     static const WCHAR szVersion9x[] = {'V','e','r','s','i','o','n','9','X',0};
705     static const WCHAR szVersionNT[] = {'V','e','r','s','i','o','n','N','T',0};
706     static const WCHAR szMsiNTProductType[] = {'M','s','i','N','T','P','r','o','d','u','c','t','T','y','p','e',0};
707     static const WCHAR szFormat[] = {'%','u',0};
708     static const WCHAR szFormat2[] = {'%','u','.','%','u',0};
709     static const WCHAR szWindowsBuild[] = {'W','i','n','d','o','w','s','B','u','i','l','d',0};
710     static const WCHAR szServicePackLevel[] = {'S','e','r','v','i','c','e','P','a','c','k','L','e','v','e','l',0};
711     static const WCHAR szVersionMsi[] = { 'V','e','r','s','i','o','n','M','s','i',0 };
712     static const WCHAR szVersionDatabase[] = { 'V','e','r','s','i','o','n','D','a','t','a','b','a','s','e',0 };
713     static const WCHAR szPhysicalMemory[] = { 'P','h','y','s','i','c','a','l','M','e','m','o','r','y',0 };
714     static const WCHAR szScreenX[] = {'S','c','r','e','e','n','X',0};
715     static const WCHAR szScreenY[] = {'S','c','r','e','e','n','Y',0};
716     static const WCHAR szColorBits[] = {'C','o','l','o','r','B','i','t','s',0};
717     static const WCHAR szIntFormat[] = {'%','d',0};
718     static const WCHAR szMsiAMD64[] = { 'M','s','i','A','M','D','6','4',0 };
719     static const WCHAR szMsix64[] = { 'M','s','i','x','6','4',0 };
720     static const WCHAR szSystem64Folder[] = { 'S','y','s','t','e','m','6','4','F','o','l','d','e','r',0 };
721     static const WCHAR szCommonFiles64Folder[] = { 'C','o','m','m','o','n','F','i','l','e','s','6','4','F','o','l','d','e','r',0 };
722     static const WCHAR szProgramFiles64Folder[] = { 'P','r','o','g','r','a','m','F','i','l','e','s','6','4','F','o','l','d','e','r',0 };
723     static const WCHAR szVersionNT64[] = { 'V','e','r','s','i','o','n','N','T','6','4',0 };
724     static const WCHAR szUserInfo[] = {
725         'S','O','F','T','W','A','R','E','\\',
726         'M','i','c','r','o','s','o','f','t','\\',
727         'M','S',' ','S','e','t','u','p',' ','(','A','C','M','E',')','\\',
728         'U','s','e','r',' ','I','n','f','o',0
729     };
730     static const WCHAR szDefName[] = { 'D','e','f','N','a','m','e',0 };
731     static const WCHAR szDefCompany[] = { 'D','e','f','C','o','m','p','a','n','y',0 };
732     static const WCHAR szCurrentVersion[] = {
733         'S','O','F','T','W','A','R','E','\\',
734         'M','i','c','r','o','s','o','f','t','\\',
735         'W','i','n','d','o','w','s',' ','N','T','\\',
736         'C','u','r','r','e','n','t','V','e','r','s','i','o','n',0
737     };
738     static const WCHAR szRegisteredUser[] = {'R','e','g','i','s','t','e','r','e','d','O','w','n','e','r',0};
739     static const WCHAR szRegisteredOrganization[] = {
740         'R','e','g','i','s','t','e','r','e','d','O','r','g','a','n','i','z','a','t','i','o','n',0
741     };
742     static const WCHAR szUSERNAME[] = {'U','S','E','R','N','A','M','E',0};
743     static const WCHAR szCOMPANYNAME[] = {'C','O','M','P','A','N','Y','N','A','M','E',0};
744     static const WCHAR szDate[] = {'D','a','t','e',0};
745     static const WCHAR szTime[] = {'T','i','m','e',0};
746     static const WCHAR szUserLanguageID[] = {'U','s','e','r','L','a','n','g','u','a','g','e','I','D',0};
747     static const WCHAR szSystemLangID[] = {'S','y','s','t','e','m','L','a','n','g','u','a','g','e','I','D',0};
748     static const WCHAR szProductState[] = {'P','r','o','d','u','c','t','S','t','a','t','e',0};
749     static const WCHAR szLogonUser[] = {'L','o','g','o','n','U','s','e','r',0};
750     static const WCHAR szNetHoodFolder[] = {'N','e','t','H','o','o','d','F','o','l','d','e','r',0};
751     static const WCHAR szPrintHoodFolder[] = {'P','r','i','n','t','H','o','o','d','F','o','l','d','e','r',0};
752     static const WCHAR szRecentFolder[] = {'R','e','c','e','n','t','F','o','l','d','e','r',0};
753     static const WCHAR szComputerName[] = {'C','o','m','p','u','t','e','r','N','a','m','e',0};
754     static const WCHAR szBrowseProperty[] = {'_','B','r','o','w','s','e','P','r','o','p','e','r','t','y',0};
755     static const WCHAR szInstallDir[] = {'I','N','S','T','A','L','L','D','I','R',0};
756
757     /*
758      * Other things that probably should be set:
759      *
760      * VirtualMemory ShellAdvSupport DefaultUIFont PackagecodeChanging
761      * CaptionHeight BorderTop BorderSide TextHeight RedirectedDllSupport
762      */
763
764     SHGetFolderPathW(NULL, CSIDL_COMMON_APPDATA, NULL, 0, pth);
765     strcatW(pth, szBackSlash);
766     msi_set_property( package->db, szCommonAppDataFolder, pth, -1 );
767
768     SHGetFolderPathW(NULL, CSIDL_FAVORITES, NULL, 0, pth);
769     strcatW(pth, szBackSlash);
770     msi_set_property( package->db, szFavoritesFolder, pth, -1 );
771
772     SHGetFolderPathW(NULL, CSIDL_FONTS, NULL, 0, pth);
773     strcatW(pth, szBackSlash);
774     msi_set_property( package->db, szFontsFolder, pth, -1 );
775
776     SHGetFolderPathW(NULL, CSIDL_SENDTO, NULL, 0, pth);
777     strcatW(pth, szBackSlash);
778     msi_set_property( package->db, szSendToFolder, pth, -1 );
779
780     SHGetFolderPathW(NULL, CSIDL_STARTMENU, NULL, 0, pth);
781     strcatW(pth, szBackSlash);
782     msi_set_property( package->db, szStartMenuFolder, pth, -1 );
783
784     SHGetFolderPathW(NULL, CSIDL_STARTUP, NULL, 0, pth);
785     strcatW(pth, szBackSlash);
786     msi_set_property( package->db, szStartupFolder, pth, -1 );
787
788     SHGetFolderPathW(NULL, CSIDL_TEMPLATES, NULL, 0, pth);
789     strcatW(pth, szBackSlash);
790     msi_set_property( package->db, szTemplateFolder, pth, -1 );
791
792     SHGetFolderPathW(NULL, CSIDL_DESKTOP, NULL, 0, pth);
793     strcatW(pth, szBackSlash);
794     msi_set_property( package->db, szDesktopFolder, pth, -1 );
795
796     /* FIXME: set to AllUsers profile path if ALLUSERS is set */
797     SHGetFolderPathW(NULL, CSIDL_PROGRAMS, NULL, 0, pth);
798     strcatW(pth, szBackSlash);
799     msi_set_property( package->db, szProgramMenuFolder, pth, -1 );
800
801     SHGetFolderPathW(NULL, CSIDL_ADMINTOOLS, NULL, 0, pth);
802     strcatW(pth, szBackSlash);
803     msi_set_property( package->db, szAdminToolsFolder, pth, -1 );
804
805     SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, 0, pth);
806     strcatW(pth, szBackSlash);
807     msi_set_property( package->db, szAppDataFolder, pth, -1 );
808
809     SHGetFolderPathW(NULL, CSIDL_SYSTEM, NULL, 0, pth);
810     strcatW(pth, szBackSlash);
811     msi_set_property( package->db, szSystemFolder, pth, -1 );
812     msi_set_property( package->db, szSystem16Folder, pth, -1 );
813
814     SHGetFolderPathW(NULL, CSIDL_LOCAL_APPDATA, NULL, 0, pth);
815     strcatW(pth, szBackSlash);
816     msi_set_property( package->db, szLocalAppDataFolder, pth, -1 );
817
818     SHGetFolderPathW(NULL, CSIDL_MYPICTURES, NULL, 0, pth);
819     strcatW(pth, szBackSlash);
820     msi_set_property( package->db, szMyPicturesFolder, pth, -1 );
821
822     SHGetFolderPathW(NULL, CSIDL_PERSONAL, NULL, 0, pth);
823     strcatW(pth, szBackSlash);
824     msi_set_property( package->db, szPersonalFolder, pth, -1 );
825
826     SHGetFolderPathW(NULL, CSIDL_WINDOWS, NULL, 0, pth);
827     strcatW(pth, szBackSlash);
828     msi_set_property( package->db, szWindowsFolder, pth, -1 );
829     
830     SHGetFolderPathW(NULL, CSIDL_PRINTHOOD, NULL, 0, pth);
831     strcatW(pth, szBackSlash);
832     msi_set_property( package->db, szPrintHoodFolder, pth, -1 );
833
834     SHGetFolderPathW(NULL, CSIDL_NETHOOD, NULL, 0, pth);
835     strcatW(pth, szBackSlash);
836     msi_set_property( package->db, szNetHoodFolder, pth, -1 );
837
838     SHGetFolderPathW(NULL, CSIDL_RECENT, NULL, 0, pth);
839     strcatW(pth, szBackSlash);
840     msi_set_property( package->db, szRecentFolder, pth, -1 );
841
842     /* Physical Memory is specified in MB. Using total amount. */
843     msex.dwLength = sizeof(msex);
844     GlobalMemoryStatusEx( &msex );
845     len = sprintfW( bufstr, szIntFormat, (int)(msex.ullTotalPhys / 1024 / 1024) );
846     msi_set_property( package->db, szPhysicalMemory, bufstr, len );
847
848     SHGetFolderPathW(NULL, CSIDL_WINDOWS, NULL, 0, pth);
849     ptr = strchrW(pth,'\\');
850     if (ptr) *(ptr + 1) = 0;
851     msi_set_property( package->db, szWindowsVolume, pth, -1 );
852     
853     len = GetTempPathW(MAX_PATH, pth);
854     msi_set_property( package->db, szTempFolder, pth, len );
855
856     /* in a wine environment the user is always admin and privileged */
857     msi_set_property( package->db, szAdminUser, szOne, -1 );
858     msi_set_property( package->db, szPrivileged, szOne, -1 );
859
860     /* set the os things */
861     OSVersion.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEXW);
862     GetVersionExW((OSVERSIONINFOW *)&OSVersion);
863     verval = OSVersion.dwMinorVersion + OSVersion.dwMajorVersion * 100;
864     len = sprintfW( verstr, szFormat, verval );
865     switch (OSVersion.dwPlatformId)
866     {
867         case VER_PLATFORM_WIN32_WINDOWS:    
868             msi_set_property( package->db, szVersion9x, verstr, len );
869             break;
870         case VER_PLATFORM_WIN32_NT:
871             msi_set_property( package->db, szVersionNT, verstr, len );
872             len = sprintfW( bufstr, szFormat,OSVersion.wProductType );
873             msi_set_property( package->db, szMsiNTProductType, bufstr, len );
874             break;
875     }
876     len = sprintfW( bufstr, szFormat, OSVersion.dwBuildNumber );
877     msi_set_property( package->db, szWindowsBuild, bufstr, len );
878     len = sprintfW( bufstr, szFormat, OSVersion.wServicePackMajor );
879     msi_set_property( package->db, szServicePackLevel, bufstr, len );
880
881     len = sprintfW( bufstr, szFormat2, MSI_MAJORVERSION, MSI_MINORVERSION );
882     msi_set_property( package->db, szVersionMsi, bufstr, len );
883     len = sprintfW( bufstr, szFormat, MSI_MAJORVERSION * 100 );
884     msi_set_property( package->db, szVersionDatabase, bufstr, len );
885
886     GetNativeSystemInfo( &sys_info );
887     len = sprintfW( bufstr, szIntFormat, sys_info.wProcessorLevel );
888     msi_set_property( package->db, szIntel, bufstr, len );
889     if (sys_info.u.s.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL)
890     {
891         GetSystemDirectoryW( pth, MAX_PATH );
892         PathAddBackslashW( pth );
893         msi_set_property( package->db, szSystemFolder, pth, -1 );
894
895         SHGetFolderPathW( NULL, CSIDL_PROGRAM_FILES, NULL, 0, pth );
896         PathAddBackslashW( pth );
897         msi_set_property( package->db, szProgramFilesFolder, pth, -1 );
898
899         SHGetFolderPathW( NULL, CSIDL_PROGRAM_FILES_COMMON, NULL, 0, pth );
900         PathAddBackslashW( pth );
901         msi_set_property( package->db, szCommonFilesFolder, pth, -1 );
902     }
903     else if (sys_info.u.s.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
904     {
905         msi_set_property( package->db, szMsiAMD64, bufstr, -1 );
906         msi_set_property( package->db, szMsix64, bufstr, -1 );
907         msi_set_property( package->db, szVersionNT64, verstr, -1 );
908
909         GetSystemDirectoryW( pth, MAX_PATH );
910         PathAddBackslashW( pth );
911         msi_set_property( package->db, szSystem64Folder, pth, -1 );
912
913         GetSystemWow64DirectoryW( pth, MAX_PATH );
914         PathAddBackslashW( pth );
915         msi_set_property( package->db, szSystemFolder, pth, -1 );
916
917         SHGetFolderPathW( NULL, CSIDL_PROGRAM_FILES, NULL, 0, pth );
918         PathAddBackslashW( pth );
919         msi_set_property( package->db, szProgramFiles64Folder, pth, -1 );
920
921         SHGetFolderPathW( NULL, CSIDL_PROGRAM_FILESX86, NULL, 0, pth );
922         PathAddBackslashW( pth );
923         msi_set_property( package->db, szProgramFilesFolder, pth, -1 );
924
925         SHGetFolderPathW( NULL, CSIDL_PROGRAM_FILES_COMMON, NULL, 0, pth );
926         PathAddBackslashW( pth );
927         msi_set_property( package->db, szCommonFiles64Folder, pth, -1 );
928
929         SHGetFolderPathW( NULL, CSIDL_PROGRAM_FILES_COMMONX86, NULL, 0, pth );
930         PathAddBackslashW( pth );
931         msi_set_property( package->db, szCommonFilesFolder, pth, -1 );
932     }
933
934     /* Screen properties. */
935     dc = GetDC(0);
936     len = sprintfW( bufstr, szIntFormat, GetDeviceCaps(dc, HORZRES) );
937     msi_set_property( package->db, szScreenX, bufstr, len );
938     len = sprintfW( bufstr, szIntFormat, GetDeviceCaps(dc, VERTRES) );
939     msi_set_property( package->db, szScreenY, bufstr, len );
940     len = sprintfW( bufstr, szIntFormat, GetDeviceCaps(dc, BITSPIXEL) );
941     msi_set_property( package->db, szColorBits, bufstr, len );
942     ReleaseDC(0, dc);
943
944     /* USERNAME and COMPANYNAME */
945     username = msi_dup_property( package->db, szUSERNAME );
946     companyname = msi_dup_property( package->db, szCOMPANYNAME );
947
948     if ((!username || !companyname) &&
949         RegOpenKeyW( HKEY_CURRENT_USER, szUserInfo, &hkey ) == ERROR_SUCCESS)
950     {
951         if (!username &&
952             (username = msi_reg_get_val_str( hkey, szDefName )))
953             msi_set_property( package->db, szUSERNAME, username, -1 );
954         if (!companyname &&
955             (companyname = msi_reg_get_val_str( hkey, szDefCompany )))
956             msi_set_property( package->db, szCOMPANYNAME, companyname, -1 );
957         CloseHandle( hkey );
958     }
959     if ((!username || !companyname) &&
960         RegOpenKeyW( HKEY_LOCAL_MACHINE, szCurrentVersion, &hkey ) == ERROR_SUCCESS)
961     {
962         if (!username &&
963             (username = msi_reg_get_val_str( hkey, szRegisteredUser )))
964             msi_set_property( package->db, szUSERNAME, username, -1 );
965         if (!companyname &&
966             (companyname = msi_reg_get_val_str( hkey, szRegisteredOrganization )))
967             msi_set_property( package->db, szCOMPANYNAME, companyname, -1 );
968         CloseHandle( hkey );
969     }
970     msi_free( username );
971     msi_free( companyname );
972
973     if ( set_user_sid_prop( package ) != ERROR_SUCCESS)
974         ERR("Failed to set the UserSID property\n");
975
976     /* Date and time properties */
977     GetSystemTime( &systemtime );
978     if (GetDateFormatW( LOCALE_USER_DEFAULT, DATE_SHORTDATE, &systemtime,
979                         NULL, bufstr, sizeof(bufstr)/sizeof(bufstr[0]) ))
980         msi_set_property( package->db, szDate, bufstr, -1 );
981     else
982         ERR("Couldn't set Date property: GetDateFormat failed with error %d\n", GetLastError());
983
984     if (GetTimeFormatW( LOCALE_USER_DEFAULT,
985                         TIME_FORCE24HOURFORMAT | TIME_NOTIMEMARKER,
986                         &systemtime, NULL, bufstr,
987                         sizeof(bufstr)/sizeof(bufstr[0]) ))
988         msi_set_property( package->db, szTime, bufstr, -1 );
989     else
990         ERR("Couldn't set Time property: GetTimeFormat failed with error %d\n", GetLastError());
991
992     set_msi_assembly_prop( package );
993
994     langid = GetUserDefaultLangID();
995     len = sprintfW( bufstr, szIntFormat, langid );
996     msi_set_property( package->db, szUserLanguageID, bufstr, len );
997
998     langid = GetSystemDefaultLangID();
999     len = sprintfW( bufstr, szIntFormat, langid );
1000     msi_set_property( package->db, szSystemLangID, bufstr, len );
1001
1002     len = sprintfW( bufstr, szIntFormat, MsiQueryProductStateW(package->ProductCode) );
1003     msi_set_property( package->db, szProductState, bufstr, len );
1004
1005     len = 0;
1006     if (!GetUserNameW( NULL, &len ) && GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1007     {
1008         WCHAR *username;
1009         if ((username = msi_alloc( len * sizeof(WCHAR) )))
1010         {
1011             if (GetUserNameW( username, &len ))
1012                 msi_set_property( package->db, szLogonUser, username, len - 1 );
1013             msi_free( username );
1014         }
1015     }
1016     len = 0;
1017     if (!GetComputerNameW( NULL, &len ) && GetLastError() == ERROR_BUFFER_OVERFLOW)
1018     {
1019         WCHAR *computername;
1020         if ((computername = msi_alloc( len * sizeof(WCHAR) )))
1021         {
1022             if (GetComputerNameW( computername, &len ))
1023                 msi_set_property( package->db, szComputerName, computername, len - 1 );
1024             msi_free( computername );
1025         }
1026     }
1027     msi_set_property( package->db, szBrowseProperty, szInstallDir, -1 );
1028 }
1029
1030 static UINT msi_load_summary_properties( MSIPACKAGE *package )
1031 {
1032     UINT rc;
1033     MSIHANDLE suminfo;
1034     MSIHANDLE hdb = alloc_msihandle( &package->db->hdr );
1035     INT count;
1036     DWORD len;
1037     LPWSTR package_code;
1038     static const WCHAR szPackageCode[] = {
1039         'P','a','c','k','a','g','e','C','o','d','e',0};
1040
1041     if (!hdb) {
1042         ERR("Unable to allocate handle\n");
1043         return ERROR_OUTOFMEMORY;
1044     }
1045
1046     rc = MsiGetSummaryInformationW( hdb, NULL, 0, &suminfo );
1047     MsiCloseHandle(hdb);
1048     if (rc != ERROR_SUCCESS)
1049     {
1050         ERR("Unable to open Summary Information\n");
1051         return rc;
1052     }
1053
1054     rc = MsiSummaryInfoGetPropertyW( suminfo, PID_PAGECOUNT, NULL,
1055                                      &count, NULL, NULL, NULL );
1056     if (rc != ERROR_SUCCESS)
1057     {
1058         WARN("Unable to query page count: %d\n", rc);
1059         goto done;
1060     }
1061
1062     /* load package code property */
1063     len = 0;
1064     rc = MsiSummaryInfoGetPropertyW( suminfo, PID_REVNUMBER, NULL,
1065                                      NULL, NULL, NULL, &len );
1066     if (rc != ERROR_MORE_DATA)
1067     {
1068         WARN("Unable to query revision number: %d\n", rc);
1069         rc = ERROR_FUNCTION_FAILED;
1070         goto done;
1071     }
1072
1073     len++;
1074     package_code = msi_alloc( len * sizeof(WCHAR) );
1075     rc = MsiSummaryInfoGetPropertyW( suminfo, PID_REVNUMBER, NULL,
1076                                      NULL, NULL, package_code, &len );
1077     if (rc != ERROR_SUCCESS)
1078     {
1079         WARN("Unable to query rev number: %d\n", rc);
1080         goto done;
1081     }
1082
1083     msi_set_property( package->db, szPackageCode, package_code, len );
1084     msi_free( package_code );
1085
1086     /* load package attributes */
1087     count = 0;
1088     MsiSummaryInfoGetPropertyW( suminfo, PID_WORDCOUNT, NULL,
1089                                 &count, NULL, NULL, NULL );
1090     package->WordCount = count;
1091
1092 done:
1093     MsiCloseHandle(suminfo);
1094     return rc;
1095 }
1096
1097 static MSIPACKAGE *msi_alloc_package( void )
1098 {
1099     MSIPACKAGE *package;
1100
1101     package = alloc_msiobject( MSIHANDLETYPE_PACKAGE, sizeof (MSIPACKAGE),
1102                                MSI_FreePackage );
1103     if( package )
1104     {
1105         list_init( &package->components );
1106         list_init( &package->features );
1107         list_init( &package->files );
1108         list_init( &package->filepatches );
1109         list_init( &package->tempfiles );
1110         list_init( &package->folders );
1111         list_init( &package->subscriptions );
1112         list_init( &package->appids );
1113         list_init( &package->classes );
1114         list_init( &package->mimes );
1115         list_init( &package->extensions );
1116         list_init( &package->progids );
1117         list_init( &package->RunningActions );
1118         list_init( &package->sourcelist_info );
1119         list_init( &package->sourcelist_media );
1120         list_init( &package->patches );
1121         list_init( &package->binaries );
1122         list_init( &package->cabinet_streams );
1123     }
1124
1125     return package;
1126 }
1127
1128 static UINT msi_load_admin_properties(MSIPACKAGE *package)
1129 {
1130     BYTE *data;
1131     UINT r, sz;
1132
1133     static const WCHAR stmname[] = {'A','d','m','i','n','P','r','o','p','e','r','t','i','e','s',0};
1134
1135     r = read_stream_data(package->db->storage, stmname, FALSE, &data, &sz);
1136     if (r != ERROR_SUCCESS)
1137         return r;
1138
1139     r = msi_parse_command_line(package, (WCHAR *)data, TRUE);
1140
1141     msi_free(data);
1142     return r;
1143 }
1144
1145 void msi_adjust_privilege_properties( MSIPACKAGE *package )
1146 {
1147     /* FIXME: this should depend on the user's privileges */
1148     if (msi_get_property_int( package->db, szAllUsers, 0 ) == 2)
1149     {
1150         TRACE("resetting ALLUSERS property from 2 to 1\n");
1151         msi_set_property( package->db, szAllUsers, szOne, -1 );
1152     }
1153     msi_set_property( package->db, szAdminUser, szOne, -1 );
1154 }
1155
1156 MSIPACKAGE *MSI_CreatePackage( MSIDATABASE *db, LPCWSTR base_url )
1157 {
1158     static const WCHAR fmtW[] = {'%','u',0};
1159     MSIPACKAGE *package;
1160     WCHAR uilevel[11];
1161     int len;
1162     UINT r;
1163
1164     TRACE("%p\n", db);
1165
1166     package = msi_alloc_package();
1167     if (package)
1168     {
1169         msiobj_addref( &db->hdr );
1170         package->db = db;
1171
1172         package->WordCount = 0;
1173         package->PackagePath = strdupW( db->path );
1174         package->BaseURL = strdupW( base_url );
1175
1176         create_temp_property_table( package );
1177         msi_clone_properties( package );
1178         msi_adjust_privilege_properties( package );
1179
1180         package->ProductCode = msi_dup_property( package->db, szProductCode );
1181         package->script = msi_alloc_zero( sizeof(MSISCRIPT) );
1182
1183         set_installed_prop( package );
1184         set_installer_properties( package );
1185
1186         package->ui_level = gUILevel;
1187         len = sprintfW( uilevel, fmtW, gUILevel & INSTALLUILEVEL_MASK );
1188         msi_set_property( package->db, szUILevel, uilevel, len );
1189
1190         r = msi_load_summary_properties( package );
1191         if (r != ERROR_SUCCESS)
1192         {
1193             msiobj_release( &package->hdr );
1194             return NULL;
1195         }
1196
1197         if (package->WordCount & msidbSumInfoSourceTypeAdminImage)
1198             msi_load_admin_properties( package );
1199
1200         package->log_file = INVALID_HANDLE_VALUE;
1201     }
1202     return package;
1203 }
1204
1205 UINT msi_download_file( LPCWSTR szUrl, LPWSTR filename )
1206 {
1207     LPINTERNET_CACHE_ENTRY_INFOW cache_entry;
1208     DWORD size = 0;
1209     HRESULT hr;
1210
1211     /* call will always fail, because size is 0,
1212      * but will return ERROR_FILE_NOT_FOUND first
1213      * if the file doesn't exist
1214      */
1215     GetUrlCacheEntryInfoW( szUrl, NULL, &size );
1216     if ( GetLastError() != ERROR_FILE_NOT_FOUND )
1217     {
1218         cache_entry = msi_alloc( size );
1219         if ( !GetUrlCacheEntryInfoW( szUrl, cache_entry, &size ) )
1220         {
1221             UINT error = GetLastError();
1222             msi_free( cache_entry );
1223             return error;
1224         }
1225
1226         lstrcpyW( filename, cache_entry->lpszLocalFileName );
1227         msi_free( cache_entry );
1228         return ERROR_SUCCESS;
1229     }
1230
1231     hr = URLDownloadToCacheFileW( NULL, szUrl, filename, MAX_PATH, 0, NULL );
1232     if ( FAILED(hr) )
1233     {
1234         WARN("failed to download %s to cache file\n", debugstr_w(szUrl));
1235         return ERROR_FUNCTION_FAILED;
1236     }
1237
1238     return ERROR_SUCCESS;
1239 }
1240
1241 UINT msi_create_empty_local_file( LPWSTR path, LPCWSTR suffix )
1242 {
1243     static const WCHAR szInstaller[] = {
1244         '\\','I','n','s','t','a','l','l','e','r','\\',0};
1245     static const WCHAR fmt[] = {'%','x',0};
1246     DWORD time, len, i, offset;
1247     HANDLE handle;
1248
1249     time = GetTickCount();
1250     GetWindowsDirectoryW( path, MAX_PATH );
1251     strcatW( path, szInstaller );
1252     CreateDirectoryW( path, NULL );
1253
1254     len = strlenW(path);
1255     for (i = 0; i < 0x10000; i++)
1256     {
1257         offset = snprintfW( path + len, MAX_PATH - len, fmt, (time + i) & 0xffff );
1258         memcpy( path + len + offset, suffix, (strlenW( suffix ) + 1) * sizeof(WCHAR) );
1259         handle = CreateFileW( path, GENERIC_WRITE, 0, NULL,
1260                               CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0 );
1261         if (handle != INVALID_HANDLE_VALUE)
1262         {
1263             CloseHandle(handle);
1264             break;
1265         }
1266         if (GetLastError() != ERROR_FILE_EXISTS &&
1267             GetLastError() != ERROR_SHARING_VIOLATION)
1268             return ERROR_FUNCTION_FAILED;
1269     }
1270
1271     return ERROR_SUCCESS;
1272 }
1273
1274 static enum platform parse_platform( WCHAR *str )
1275 {
1276     if (!str[0] || !strcmpW( str, szIntel )) return PLATFORM_INTEL;
1277     else if (!strcmpW( str, szIntel64 )) return PLATFORM_INTEL64;
1278     else if (!strcmpW( str, szX64 ) || !strcmpW( str, szAMD64 )) return PLATFORM_X64;
1279     else if (!strcmpW( str, szARM )) return PLATFORM_ARM;
1280     return PLATFORM_UNKNOWN;
1281 }
1282
1283 static UINT msi_parse_summary( MSISUMMARYINFO *si, MSIPACKAGE *package )
1284 {
1285     WCHAR *template, *p, *q, *platform;
1286     DWORD i, count;
1287
1288     package->version = msi_suminfo_get_int32( si, PID_PAGECOUNT );
1289     TRACE("version: %d\n", package->version);
1290
1291     template = msi_suminfo_dup_string( si, PID_TEMPLATE );
1292     if (!template)
1293         return ERROR_SUCCESS; /* native accepts missing template property */
1294
1295     TRACE("template: %s\n", debugstr_w(template));
1296
1297     p = strchrW( template, ';' );
1298     if (!p)
1299     {
1300         WARN("invalid template string %s\n", debugstr_w(template));
1301         msi_free( template );
1302         return ERROR_PATCH_PACKAGE_INVALID;
1303     }
1304     *p = 0;
1305     platform = template;
1306     if ((q = strchrW( platform, ',' ))) *q = 0;
1307     package->platform = parse_platform( platform );
1308     while (package->platform == PLATFORM_UNKNOWN && q)
1309     {
1310         platform = q + 1;
1311         if ((q = strchrW( platform, ',' ))) *q = 0;
1312         package->platform = parse_platform( platform );
1313     }
1314     if (package->platform == PLATFORM_UNKNOWN)
1315     {
1316         WARN("unknown platform %s\n", debugstr_w(template));
1317         msi_free( template );
1318         return ERROR_INSTALL_PLATFORM_UNSUPPORTED;
1319     }
1320     p++;
1321     if (!*p)
1322     {
1323         msi_free( template );
1324         return ERROR_SUCCESS;
1325     }
1326     count = 1;
1327     for (q = p; (q = strchrW( q, ',' )); q++) count++;
1328
1329     package->langids = msi_alloc( count * sizeof(LANGID) );
1330     if (!package->langids)
1331     {
1332         msi_free( template );
1333         return ERROR_OUTOFMEMORY;
1334     }
1335
1336     i = 0;
1337     while (*p)
1338     {
1339         q = strchrW( p, ',' );
1340         if (q) *q = 0;
1341         package->langids[i] = atoiW( p );
1342         if (!q) break;
1343         p = q + 1;
1344         i++;
1345     }
1346     package->num_langids = i + 1;
1347
1348     msi_free( template );
1349     return ERROR_SUCCESS;
1350 }
1351
1352 static UINT validate_package( MSIPACKAGE *package )
1353 {
1354     UINT i;
1355
1356     if (package->platform == PLATFORM_INTEL64)
1357         return ERROR_INSTALL_PLATFORM_UNSUPPORTED;
1358 #ifndef __arm__
1359     if (package->platform == PLATFORM_ARM)
1360         return ERROR_INSTALL_PLATFORM_UNSUPPORTED;
1361 #endif
1362     if (package->platform == PLATFORM_X64)
1363     {
1364         if (!is_64bit && !is_wow64)
1365             return ERROR_INSTALL_PLATFORM_UNSUPPORTED;
1366         if (package->version < 200)
1367             return ERROR_INSTALL_PACKAGE_INVALID;
1368     }
1369     if (!package->num_langids)
1370     {
1371         return ERROR_SUCCESS;
1372     }
1373     for (i = 0; i < package->num_langids; i++)
1374     {
1375         LANGID langid = package->langids[i];
1376
1377         if (PRIMARYLANGID( langid ) == LANG_NEUTRAL)
1378         {
1379             langid = MAKELANGID( PRIMARYLANGID( GetSystemDefaultLangID() ), SUBLANGID( langid ) );
1380         }
1381         if (SUBLANGID( langid ) == SUBLANG_NEUTRAL)
1382         {
1383             langid = MAKELANGID( PRIMARYLANGID( langid ), SUBLANGID( GetSystemDefaultLangID() ) );
1384         }
1385         if (IsValidLocale( langid, LCID_INSTALLED ))
1386             return ERROR_SUCCESS;
1387     }
1388     return ERROR_INSTALL_LANGUAGE_UNSUPPORTED;
1389 }
1390
1391 int msi_track_tempfile( MSIPACKAGE *package, const WCHAR *path )
1392 {
1393     MSITEMPFILE *temp;
1394
1395     TRACE("%s\n", debugstr_w(path));
1396
1397     LIST_FOR_EACH_ENTRY( temp, &package->tempfiles, MSITEMPFILE, entry )
1398     {
1399         if (!strcmpW( path, temp->Path )) return 0;
1400     }
1401     if (!(temp = msi_alloc_zero( sizeof (MSITEMPFILE) ))) return -1;
1402     list_add_head( &package->tempfiles, &temp->entry );
1403     temp->Path = strdupW( path );
1404     return 0;
1405 }
1406
1407 static WCHAR *get_product_code( MSIDATABASE *db )
1408 {
1409     static const WCHAR query[] = {
1410         'S','E','L','E','C','T',' ','`','V','a','l','u','e','`',' ',
1411         'F','R','O','M',' ','`','P','r','o','p','e','r','t','y','`',' ',
1412         'W','H','E','R','E',' ','`','P','r','o','p','e','r','t','y','`','=',
1413         '\'','P','r','o','d','u','c','t','C','o','d','e','\'',0};
1414     MSIQUERY *view;
1415     MSIRECORD *rec;
1416     WCHAR *ret = NULL;
1417
1418     if (MSI_DatabaseOpenViewW( db, query, &view ) != ERROR_SUCCESS)
1419     {
1420         return NULL;
1421     }
1422     if (MSI_ViewExecute( view, 0 ) != ERROR_SUCCESS)
1423     {
1424         MSI_ViewClose( view );
1425         msiobj_release( &view->hdr );
1426         return NULL;
1427     }
1428     if (MSI_ViewFetch( view, &rec ) == ERROR_SUCCESS)
1429     {
1430         ret = strdupW( MSI_RecordGetString( rec, 1 ) );
1431         msiobj_release( &rec->hdr );
1432     }
1433     MSI_ViewClose( view );
1434     msiobj_release( &view->hdr );
1435     return ret;
1436 }
1437
1438 static UINT get_registered_local_package( const WCHAR *product, const WCHAR *package, WCHAR *localfile )
1439 {
1440     MSIINSTALLCONTEXT context;
1441     HKEY product_key, props_key;
1442     WCHAR *registered_package = NULL, unsquashed[GUID_SIZE];
1443     UINT r;
1444
1445     r = msi_locate_product( product, &context );
1446     if (r != ERROR_SUCCESS)
1447         return r;
1448
1449     r = MSIREG_OpenProductKey( product, NULL, context, &product_key, FALSE );
1450     if (r != ERROR_SUCCESS)
1451         return r;
1452
1453     r = MSIREG_OpenInstallProps( product, context, NULL, &props_key, FALSE );
1454     if (r != ERROR_SUCCESS)
1455     {
1456         RegCloseKey( product_key );
1457         return r;
1458     }
1459     r = ERROR_FUNCTION_FAILED;
1460     registered_package = msi_reg_get_val_str( product_key, INSTALLPROPERTY_PACKAGECODEW );
1461     if (!registered_package)
1462         goto done;
1463
1464     unsquash_guid( registered_package, unsquashed );
1465     if (!strcmpiW( package, unsquashed ))
1466     {
1467         WCHAR *filename = msi_reg_get_val_str( props_key, INSTALLPROPERTY_LOCALPACKAGEW );
1468         if (!filename)
1469             goto done;
1470
1471         strcpyW( localfile, filename );
1472         msi_free( filename );
1473         r = ERROR_SUCCESS;
1474     }
1475 done:
1476     msi_free( registered_package );
1477     RegCloseKey( props_key );
1478     RegCloseKey( product_key );
1479     return r;
1480 }
1481
1482 static WCHAR *get_package_code( MSIDATABASE *db )
1483 {
1484     WCHAR *ret;
1485     MSISUMMARYINFO *si;
1486
1487     if (!(si = MSI_GetSummaryInformationW( db->storage, 0 )))
1488     {
1489         WARN("failed to load summary info\n");
1490         return NULL;
1491     }
1492     ret = msi_suminfo_dup_string( si, PID_REVNUMBER );
1493     msiobj_release( &si->hdr );
1494     return ret;
1495 }
1496
1497 static UINT get_local_package( const WCHAR *filename, WCHAR *localfile )
1498 {
1499     WCHAR *product_code, *package_code;
1500     MSIDATABASE *db;
1501     UINT r;
1502
1503     if ((r = MSI_OpenDatabaseW( filename, MSIDBOPEN_READONLY, &db )) != ERROR_SUCCESS)
1504     {
1505         if (GetFileAttributesW( filename ) == INVALID_FILE_ATTRIBUTES)
1506             return ERROR_FILE_NOT_FOUND;
1507         return r;
1508     }
1509     if (!(product_code = get_product_code( db )))
1510     {
1511         msiobj_release( &db->hdr );
1512         return ERROR_INSTALL_PACKAGE_INVALID;
1513     }
1514     if (!(package_code = get_package_code( db )))
1515     {
1516         msi_free( product_code );
1517         msiobj_release( &db->hdr );
1518         return ERROR_INSTALL_PACKAGE_INVALID;
1519     }
1520     r = get_registered_local_package( product_code, package_code, localfile );
1521     msi_free( package_code );
1522     msi_free( product_code );
1523     msiobj_release( &db->hdr );
1524     return r;
1525 }
1526
1527 UINT msi_set_original_database_property( MSIDATABASE *db, const WCHAR *package )
1528 {
1529     UINT r;
1530
1531     if (UrlIsW( package, URLIS_URL ))
1532         r = msi_set_property( db, szOriginalDatabase, package, -1 );
1533     else if (package[0] == '#')
1534         r = msi_set_property( db, szOriginalDatabase, db->path, -1 );
1535     else
1536     {
1537         DWORD len;
1538         WCHAR *path;
1539
1540         if (!(len = GetFullPathNameW( package, 0, NULL, NULL ))) return GetLastError();
1541         if (!(path = msi_alloc( len * sizeof(WCHAR) ))) return ERROR_OUTOFMEMORY;
1542         len = GetFullPathNameW( package, len, path, NULL );
1543         r = msi_set_property( db, szOriginalDatabase, path, len );
1544         msi_free( path );
1545     }
1546     return r;
1547 }
1548
1549 UINT MSI_OpenPackageW(LPCWSTR szPackage, MSIPACKAGE **pPackage)
1550 {
1551     static const WCHAR dotmsi[] = {'.','m','s','i',0};
1552     MSIDATABASE *db;
1553     MSIPACKAGE *package;
1554     MSIHANDLE handle;
1555     LPWSTR ptr, base_url = NULL;
1556     UINT r;
1557     WCHAR localfile[MAX_PATH], cachefile[MAX_PATH];
1558     LPCWSTR file = szPackage;
1559     DWORD index = 0;
1560     MSISUMMARYINFO *si;
1561     BOOL delete_on_close = FALSE;
1562
1563     TRACE("%s %p\n", debugstr_w(szPackage), pPackage);
1564
1565     localfile[0] = 0;
1566     if( szPackage[0] == '#' )
1567     {
1568         handle = atoiW(&szPackage[1]);
1569         db = msihandle2msiinfo( handle, MSIHANDLETYPE_DATABASE );
1570         if( !db )
1571         {
1572             IWineMsiRemoteDatabase *remote_database;
1573
1574             remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( handle );
1575             if ( !remote_database )
1576                 return ERROR_INVALID_HANDLE;
1577
1578             IWineMsiRemoteDatabase_Release( remote_database );
1579             WARN("MsiOpenPackage not allowed during a custom action!\n");
1580
1581             return ERROR_FUNCTION_FAILED;
1582         }
1583     }
1584     else
1585     {
1586         if ( UrlIsW( szPackage, URLIS_URL ) )
1587         {
1588             r = msi_download_file( szPackage, cachefile );
1589             if (r != ERROR_SUCCESS)
1590                 return r;
1591
1592             file = cachefile;
1593
1594             base_url = strdupW( szPackage );
1595             if (!base_url)
1596                 return ERROR_OUTOFMEMORY;
1597
1598             ptr = strrchrW( base_url, '/' );
1599             if (ptr) *(ptr + 1) = '\0';
1600         }
1601         r = get_local_package( file, localfile );
1602         if (r != ERROR_SUCCESS || GetFileAttributesW( localfile ) == INVALID_FILE_ATTRIBUTES)
1603         {
1604             r = msi_create_empty_local_file( localfile, dotmsi );
1605             if (r != ERROR_SUCCESS)
1606             {
1607                 msi_free ( base_url );
1608                 return r;
1609             }
1610
1611             if (!CopyFileW( file, localfile, FALSE ))
1612             {
1613                 r = GetLastError();
1614                 WARN("unable to copy package %s to %s (%u)\n", debugstr_w(file), debugstr_w(localfile), r);
1615                 DeleteFileW( localfile );
1616                 msi_free ( base_url );
1617                 return r;
1618             }
1619             delete_on_close = TRUE;
1620         }
1621         TRACE("opening package %s\n", debugstr_w( localfile ));
1622         r = MSI_OpenDatabaseW( localfile, MSIDBOPEN_TRANSACT, &db );
1623         if (r != ERROR_SUCCESS)
1624         {
1625             msi_free ( base_url );
1626             return r;
1627         }
1628     }
1629     package = MSI_CreatePackage( db, base_url );
1630     msi_free( base_url );
1631     msiobj_release( &db->hdr );
1632     if (!package) return ERROR_INSTALL_PACKAGE_INVALID;
1633     package->localfile = strdupW( localfile );
1634     package->delete_on_close = delete_on_close;
1635
1636     si = MSI_GetSummaryInformationW( db->storage, 0 );
1637     if (!si)
1638     {
1639         WARN("failed to load summary info\n");
1640         msiobj_release( &package->hdr );
1641         return ERROR_INSTALL_PACKAGE_INVALID;
1642     }
1643     r = msi_parse_summary( si, package );
1644     msiobj_release( &si->hdr );
1645     if (r != ERROR_SUCCESS)
1646     {
1647         WARN("failed to parse summary info %u\n", r);
1648         msiobj_release( &package->hdr );
1649         return r;
1650     }
1651     r = validate_package( package );
1652     if (r != ERROR_SUCCESS)
1653     {
1654         msiobj_release( &package->hdr );
1655         return r;
1656     }
1657     msi_set_property( package->db, szDatabase, db->path, -1 );
1658     msi_set_context( package );
1659
1660     while (1)
1661     {
1662         WCHAR patch_code[GUID_SIZE];
1663         r = MsiEnumPatchesExW( package->ProductCode, NULL, package->Context,
1664                                MSIPATCHSTATE_APPLIED, index, patch_code, NULL, NULL, NULL, NULL );
1665         if (r != ERROR_SUCCESS)
1666             break;
1667
1668         TRACE("found registered patch %s\n", debugstr_w(patch_code));
1669
1670         r = msi_apply_registered_patch( package, patch_code );
1671         if (r != ERROR_SUCCESS)
1672         {
1673             ERR("registered patch failed to apply %u\n", r);
1674             msiobj_release( &package->hdr );
1675             return r;
1676         }
1677         index++;
1678     }
1679     if (index)
1680     {
1681         msi_clone_properties( package );
1682         msi_adjust_privilege_properties( package );
1683     }
1684     r = msi_set_original_database_property( package->db, szPackage );
1685     if (r != ERROR_SUCCESS)
1686     {
1687         msiobj_release( &package->hdr );
1688         return r;
1689     }
1690     if (gszLogFile)
1691         package->log_file = CreateFileW( gszLogFile, GENERIC_WRITE, FILE_SHARE_WRITE, NULL,
1692                                          OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
1693     *pPackage = package;
1694     return ERROR_SUCCESS;
1695 }
1696
1697 UINT WINAPI MsiOpenPackageExW(LPCWSTR szPackage, DWORD dwOptions, MSIHANDLE *phPackage)
1698 {
1699     MSIPACKAGE *package = NULL;
1700     UINT ret;
1701
1702     TRACE("%s %08x %p\n", debugstr_w(szPackage), dwOptions, phPackage );
1703
1704     if( !szPackage || !phPackage )
1705         return ERROR_INVALID_PARAMETER;
1706
1707     if ( !*szPackage )
1708     {
1709         FIXME("Should create an empty database and package\n");
1710         return ERROR_FUNCTION_FAILED;
1711     }
1712
1713     if( dwOptions )
1714         FIXME("dwOptions %08x not supported\n", dwOptions);
1715
1716     ret = MSI_OpenPackageW( szPackage, &package );
1717     if( ret == ERROR_SUCCESS )
1718     {
1719         *phPackage = alloc_msihandle( &package->hdr );
1720         if (! *phPackage)
1721             ret = ERROR_NOT_ENOUGH_MEMORY;
1722         msiobj_release( &package->hdr );
1723     }
1724
1725     return ret;
1726 }
1727
1728 UINT WINAPI MsiOpenPackageW(LPCWSTR szPackage, MSIHANDLE *phPackage)
1729 {
1730     return MsiOpenPackageExW( szPackage, 0, phPackage );
1731 }
1732
1733 UINT WINAPI MsiOpenPackageExA(LPCSTR szPackage, DWORD dwOptions, MSIHANDLE *phPackage)
1734 {
1735     LPWSTR szwPack = NULL;
1736     UINT ret;
1737
1738     if( szPackage )
1739     {
1740         szwPack = strdupAtoW( szPackage );
1741         if( !szwPack )
1742             return ERROR_OUTOFMEMORY;
1743     }
1744
1745     ret = MsiOpenPackageExW( szwPack, dwOptions, phPackage );
1746
1747     msi_free( szwPack );
1748
1749     return ret;
1750 }
1751
1752 UINT WINAPI MsiOpenPackageA(LPCSTR szPackage, MSIHANDLE *phPackage)
1753 {
1754     return MsiOpenPackageExA( szPackage, 0, phPackage );
1755 }
1756
1757 MSIHANDLE WINAPI MsiGetActiveDatabase(MSIHANDLE hInstall)
1758 {
1759     MSIPACKAGE *package;
1760     MSIHANDLE handle = 0;
1761     IUnknown *remote_unk;
1762     IWineMsiRemotePackage *remote_package;
1763
1764     TRACE("(%d)\n",hInstall);
1765
1766     package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE);
1767     if( package)
1768     {
1769         handle = alloc_msihandle( &package->db->hdr );
1770         msiobj_release( &package->hdr );
1771     }
1772     else if ((remote_unk = msi_get_remote(hInstall)))
1773     {
1774         if (IUnknown_QueryInterface(remote_unk, &IID_IWineMsiRemotePackage,
1775                                         (LPVOID *)&remote_package) == S_OK)
1776         {
1777             IWineMsiRemotePackage_GetActiveDatabase(remote_package, &handle);
1778             IWineMsiRemotePackage_Release(remote_package);
1779         }
1780         else
1781         {
1782             WARN("remote handle %d is not a package\n", hInstall);
1783         }
1784         IUnknown_Release(remote_unk);
1785     }
1786
1787     return handle;
1788 }
1789
1790 INT MSI_ProcessMessage( MSIPACKAGE *package, INSTALLMESSAGE eMessageType, MSIRECORD *record )
1791 {
1792     static const WCHAR szActionData[] = {'A','c','t','i','o','n','D','a','t','a',0};
1793     static const WCHAR szSetProgress[] = {'S','e','t','P','r','o','g','r','e','s','s',0};
1794     static const WCHAR szActionText[] = {'A','c','t','i','o','n','T','e','x','t',0};
1795     MSIRECORD *uirow;
1796     LPWSTR deformated, message;
1797     DWORD i, len, total_len, log_type = 0;
1798     INT rc = 0;
1799     char *msg;
1800
1801     TRACE("%x\n", eMessageType);
1802
1803     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_FATALEXIT)
1804         log_type |= INSTALLLOGMODE_FATALEXIT;
1805     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_ERROR)
1806         log_type |= INSTALLLOGMODE_ERROR;
1807     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_WARNING)
1808         log_type |= INSTALLLOGMODE_WARNING;
1809     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_USER)
1810         log_type |= INSTALLLOGMODE_USER;
1811     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_INFO)
1812         log_type |= INSTALLLOGMODE_INFO;
1813     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_RESOLVESOURCE)
1814         log_type |= INSTALLLOGMODE_RESOLVESOURCE;
1815     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_OUTOFDISKSPACE)
1816         log_type |= INSTALLLOGMODE_OUTOFDISKSPACE;
1817     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_COMMONDATA)
1818         log_type |= INSTALLLOGMODE_COMMONDATA;
1819     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_ACTIONSTART)
1820         log_type |= INSTALLLOGMODE_ACTIONSTART;
1821     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_ACTIONDATA)
1822         log_type |= INSTALLLOGMODE_ACTIONDATA;
1823     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_PROGRESS)
1824         log_type |= INSTALLLOGMODE_PROGRESS;
1825     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_INITIALIZE)
1826         log_type |= INSTALLLOGMODE_INITIALIZE;
1827     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_TERMINATE)
1828         log_type |= INSTALLLOGMODE_TERMINATE;
1829     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_SHOWDIALOG)
1830         log_type |= INSTALLLOGMODE_SHOWDIALOG;
1831
1832     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_ACTIONSTART)
1833     {
1834         static const WCHAR template_s[]=
1835             {'A','c','t','i','o','n',' ','%','s',':',' ','%','s','.',' ',0};
1836         static const WCHAR format[] = 
1837             {'H','H','\'',':','\'','m','m','\'',':','\'','s','s',0};
1838         WCHAR timet[0x100];
1839         LPCWSTR action_text, action;
1840         LPWSTR deformatted = NULL;
1841
1842         GetTimeFormatW(LOCALE_USER_DEFAULT, 0, NULL, format, timet, 0x100);
1843
1844         action = MSI_RecordGetString(record, 1);
1845         action_text = MSI_RecordGetString(record, 2);
1846
1847         if (!action || !action_text)
1848             return IDOK;
1849
1850         deformat_string(package, action_text, &deformatted);
1851
1852         len = strlenW(timet) + strlenW(action) + strlenW(template_s);
1853         if (deformatted)
1854             len += strlenW(deformatted);
1855         message = msi_alloc(len*sizeof(WCHAR));
1856         sprintfW(message, template_s, timet, action);
1857         if (deformatted)
1858             strcatW(message, deformatted);
1859         msi_free(deformatted);
1860     }
1861     else
1862     {
1863         static const WCHAR format[] = {'%','u',':',' ',0};
1864         UINT count = MSI_RecordGetFieldCount( record );
1865         WCHAR *p;
1866
1867         total_len = 1;
1868         for (i = 1; i <= count; i++)
1869         {
1870             len = 0;
1871             MSI_RecordGetStringW( record, i, NULL, &len );
1872             total_len += len + 13;
1873         }
1874         p = message = msi_alloc( total_len * sizeof(WCHAR) );
1875         if (!p) return ERROR_OUTOFMEMORY;
1876
1877         for (i = 1; i <= count; i++)
1878         {
1879             if (count > 1)
1880             {
1881                 len = sprintfW( p, format, i );
1882                 total_len -= len;
1883                 p += len;
1884             }
1885             len = total_len;
1886             MSI_RecordGetStringW( record, i, p, &len );
1887             total_len -= len;
1888             p += len;
1889             if (count > 1 && total_len)
1890             {
1891                 *p++ = ' ';
1892                 total_len--;
1893             }
1894         }
1895         p[0] = 0;
1896     }
1897
1898     TRACE("%p %p %p %x %x %s\n", gUIHandlerA, gUIHandlerW, gUIHandlerRecord,
1899           gUIFilter, log_type, debugstr_w(message));
1900
1901     /* convert it to ASCII */
1902     len = WideCharToMultiByte( CP_ACP, 0, message, -1, NULL, 0, NULL, NULL );
1903     msg = msi_alloc( len );
1904     WideCharToMultiByte( CP_ACP, 0, message, -1, msg, len, NULL, NULL );
1905
1906     if (gUIHandlerW && (gUIFilter & log_type))
1907     {
1908         rc = gUIHandlerW( gUIContext, eMessageType, message );
1909     }
1910     else if (gUIHandlerA && (gUIFilter & log_type))
1911     {
1912         rc = gUIHandlerA( gUIContext, eMessageType, msg );
1913     }
1914     else if (gUIHandlerRecord && (gUIFilter & log_type))
1915     {
1916         MSIHANDLE rec = MsiCreateRecord( 1 );
1917         MsiRecordSetStringW( rec, 0, message );
1918         rc = gUIHandlerRecord( gUIContext, eMessageType, rec );
1919         MsiCloseHandle( rec );
1920     }
1921
1922     if (!rc && package->log_file != INVALID_HANDLE_VALUE &&
1923         (eMessageType & 0xff000000) != INSTALLMESSAGE_PROGRESS)
1924     {
1925         DWORD written;
1926         WriteFile( package->log_file, msg, len - 1, &written, NULL );
1927         WriteFile( package->log_file, "\n", 1, &written, NULL );
1928     }
1929     msi_free( msg );
1930     msi_free( message );
1931
1932     switch (eMessageType & 0xff000000)
1933     {
1934     case INSTALLMESSAGE_ACTIONDATA:
1935         deformat_string(package, MSI_RecordGetString(record, 2), &deformated);
1936         uirow = MSI_CreateRecord(1);
1937         MSI_RecordSetStringW(uirow, 1, deformated);
1938         msi_free(deformated);
1939
1940         ControlEvent_FireSubscribedEvent(package, szActionData, uirow);
1941         msiobj_release(&uirow->hdr);
1942
1943         if (package->action_progress_increment)
1944         {
1945             uirow = MSI_CreateRecord(2);
1946             MSI_RecordSetInteger(uirow, 1, 2);
1947             MSI_RecordSetInteger(uirow, 2, package->action_progress_increment);
1948             ControlEvent_FireSubscribedEvent(package, szSetProgress, uirow);
1949             msiobj_release(&uirow->hdr);
1950         }
1951         break;
1952
1953     case INSTALLMESSAGE_ACTIONSTART:
1954         deformat_string(package, MSI_RecordGetString(record, 2), &deformated);
1955         uirow = MSI_CreateRecord(1);
1956         MSI_RecordSetStringW(uirow, 1, deformated);
1957         msi_free(deformated);
1958
1959         ControlEvent_FireSubscribedEvent(package, szActionText, uirow);
1960
1961         msiobj_release(&uirow->hdr);
1962         break;
1963
1964     case INSTALLMESSAGE_PROGRESS:
1965         ControlEvent_FireSubscribedEvent(package, szSetProgress, record);
1966         break;
1967     }
1968
1969     return ERROR_SUCCESS;
1970 }
1971
1972 INT WINAPI MsiProcessMessage( MSIHANDLE hInstall, INSTALLMESSAGE eMessageType,
1973                               MSIHANDLE hRecord)
1974 {
1975     UINT ret = ERROR_INVALID_HANDLE;
1976     MSIPACKAGE *package = NULL;
1977     MSIRECORD *record = NULL;
1978
1979     package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE );
1980     if( !package )
1981     {
1982         HRESULT hr;
1983         IWineMsiRemotePackage *remote_package;
1984
1985         remote_package = (IWineMsiRemotePackage *)msi_get_remote( hInstall );
1986         if (!remote_package)
1987             return ERROR_INVALID_HANDLE;
1988
1989         hr = IWineMsiRemotePackage_ProcessMessage( remote_package, eMessageType, hRecord );
1990
1991         IWineMsiRemotePackage_Release( remote_package );
1992
1993         if (FAILED(hr))
1994         {
1995             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
1996                 return HRESULT_CODE(hr);
1997
1998             return ERROR_FUNCTION_FAILED;
1999         }
2000
2001         return ERROR_SUCCESS;
2002     }
2003
2004     record = msihandle2msiinfo( hRecord, MSIHANDLETYPE_RECORD );
2005     if( !record )
2006         goto out;
2007
2008     ret = MSI_ProcessMessage( package, eMessageType, record );
2009
2010 out:
2011     msiobj_release( &package->hdr );
2012     if( record )
2013         msiobj_release( &record->hdr );
2014
2015     return ret;
2016 }
2017
2018 /* property code */
2019
2020 UINT WINAPI MsiSetPropertyA( MSIHANDLE hInstall, LPCSTR szName, LPCSTR szValue )
2021 {
2022     LPWSTR szwName = NULL, szwValue = NULL;
2023     UINT r = ERROR_OUTOFMEMORY;
2024
2025     szwName = strdupAtoW( szName );
2026     if( szName && !szwName )
2027         goto end;
2028
2029     szwValue = strdupAtoW( szValue );
2030     if( szValue && !szwValue )
2031         goto end;
2032
2033     r = MsiSetPropertyW( hInstall, szwName, szwValue);
2034
2035 end:
2036     msi_free( szwName );
2037     msi_free( szwValue );
2038
2039     return r;
2040 }
2041
2042 void msi_reset_folders( MSIPACKAGE *package, BOOL source )
2043 {
2044     MSIFOLDER *folder;
2045
2046     LIST_FOR_EACH_ENTRY( folder, &package->folders, MSIFOLDER, entry )
2047     {
2048         if ( source )
2049         {
2050             msi_free( folder->ResolvedSource );
2051             folder->ResolvedSource = NULL;
2052         }
2053         else
2054         {
2055             msi_free( folder->ResolvedTarget );
2056             folder->ResolvedTarget = NULL;
2057         }
2058     }
2059 }
2060
2061 UINT msi_set_property( MSIDATABASE *db, const WCHAR *name, const WCHAR *value, int len )
2062 {
2063     static const WCHAR insert_query[] = {
2064         'I','N','S','E','R','T',' ','I','N','T','O',' ',
2065         '`','_','P','r','o','p','e','r','t','y','`',' ',
2066         '(','`','_','P','r','o','p','e','r','t','y','`',',','`','V','a','l','u','e','`',')',' ',
2067         'V','A','L','U','E','S',' ','(','?',',','?',')',0};
2068     static const WCHAR update_query[] = {
2069         'U','P','D','A','T','E',' ','`','_','P','r','o','p','e','r','t','y','`',' ',
2070         'S','E','T',' ','`','V','a','l','u','e','`',' ','=',' ','?',' ','W','H','E','R','E',' ',
2071         '`','_','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',0};
2072     static const WCHAR delete_query[] = {
2073         'D','E','L','E','T','E',' ','F','R','O','M',' ',
2074         '`','_','P','r','o','p','e','r','t','y','`',' ','W','H','E','R','E',' ',
2075         '`','_','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',0};
2076     MSIQUERY *view;
2077     MSIRECORD *row = NULL;
2078     DWORD sz = 0;
2079     WCHAR query[1024];
2080     UINT rc;
2081
2082     TRACE("%p %s %s %d\n", db, debugstr_w(name), debugstr_wn(value, len), len);
2083
2084     if (!name)
2085         return ERROR_INVALID_PARAMETER;
2086
2087     /* this one is weird... */
2088     if (!name[0])
2089         return value ? ERROR_FUNCTION_FAILED : ERROR_SUCCESS;
2090
2091     if (value && len < 0) len = strlenW( value );
2092
2093     rc = msi_get_property( db, name, 0, &sz );
2094     if (!value || (!*value && !len))
2095     {
2096         sprintfW( query, delete_query, name );
2097     }
2098     else if (rc == ERROR_MORE_DATA || rc == ERROR_SUCCESS)
2099     {
2100         sprintfW( query, update_query, name );
2101         row = MSI_CreateRecord(1);
2102         msi_record_set_string( row, 1, value, len );
2103     }
2104     else
2105     {
2106         strcpyW( query, insert_query );
2107         row = MSI_CreateRecord(2);
2108         msi_record_set_string( row, 1, name, -1 );
2109         msi_record_set_string( row, 2, value, len );
2110     }
2111
2112     rc = MSI_DatabaseOpenViewW(db, query, &view);
2113     if (rc == ERROR_SUCCESS)
2114     {
2115         rc = MSI_ViewExecute(view, row);
2116         MSI_ViewClose(view);
2117         msiobj_release(&view->hdr);
2118     }
2119     if (row) msiobj_release(&row->hdr);
2120     return rc;
2121 }
2122
2123 UINT WINAPI MsiSetPropertyW( MSIHANDLE hInstall, LPCWSTR szName, LPCWSTR szValue)
2124 {
2125     MSIPACKAGE *package;
2126     UINT ret;
2127
2128     package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE);
2129     if( !package )
2130     {
2131         HRESULT hr;
2132         BSTR name = NULL, value = NULL;
2133         IWineMsiRemotePackage *remote_package;
2134
2135         remote_package = (IWineMsiRemotePackage *)msi_get_remote( hInstall );
2136         if (!remote_package)
2137             return ERROR_INVALID_HANDLE;
2138
2139         name = SysAllocString( szName );
2140         value = SysAllocString( szValue );
2141         if ((!name && szName) || (!value && szValue))
2142         {
2143             SysFreeString( name );
2144             SysFreeString( value );
2145             IWineMsiRemotePackage_Release( remote_package );
2146             return ERROR_OUTOFMEMORY;
2147         }
2148
2149         hr = IWineMsiRemotePackage_SetProperty( remote_package, name, value );
2150
2151         SysFreeString( name );
2152         SysFreeString( value );
2153         IWineMsiRemotePackage_Release( remote_package );
2154
2155         if (FAILED(hr))
2156         {
2157             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
2158                 return HRESULT_CODE(hr);
2159
2160             return ERROR_FUNCTION_FAILED;
2161         }
2162
2163         return ERROR_SUCCESS;
2164     }
2165
2166     ret = msi_set_property( package->db, szName, szValue, -1 );
2167     if (ret == ERROR_SUCCESS && !strcmpW( szName, szSourceDir ))
2168         msi_reset_folders( package, TRUE );
2169
2170     msiobj_release( &package->hdr );
2171     return ret;
2172 }
2173
2174 static MSIRECORD *msi_get_property_row( MSIDATABASE *db, LPCWSTR name )
2175 {
2176     static const WCHAR query[]= {
2177         'S','E','L','E','C','T',' ','`','V','a','l','u','e','`',' ',
2178         'F','R','O','M',' ' ,'`','_','P','r','o','p','e','r','t','y','`',' ',
2179         'W','H','E','R','E',' ' ,'`','_','P','r','o','p','e','r','t','y','`','=','?',0};
2180     MSIRECORD *rec, *row = NULL;
2181     MSIQUERY *view;
2182     UINT r;
2183
2184     if (!name || !*name)
2185         return NULL;
2186
2187     rec = MSI_CreateRecord(1);
2188     if (!rec)
2189         return NULL;
2190
2191     MSI_RecordSetStringW(rec, 1, name);
2192
2193     r = MSI_DatabaseOpenViewW(db, query, &view);
2194     if (r == ERROR_SUCCESS)
2195     {
2196         MSI_ViewExecute(view, rec);
2197         MSI_ViewFetch(view, &row);
2198         MSI_ViewClose(view);
2199         msiobj_release(&view->hdr);
2200     }
2201     msiobj_release(&rec->hdr);
2202     return row;
2203 }
2204
2205 /* internal function, not compatible with MsiGetPropertyW */
2206 UINT msi_get_property( MSIDATABASE *db, LPCWSTR szName,
2207                        LPWSTR szValueBuf, LPDWORD pchValueBuf )
2208 {
2209     MSIRECORD *row;
2210     UINT rc = ERROR_FUNCTION_FAILED;
2211
2212     row = msi_get_property_row( db, szName );
2213
2214     if (*pchValueBuf > 0)
2215         szValueBuf[0] = 0;
2216
2217     if (row)
2218     {
2219         rc = MSI_RecordGetStringW(row, 1, szValueBuf, pchValueBuf);
2220         msiobj_release(&row->hdr);
2221     }
2222
2223     if (rc == ERROR_SUCCESS)
2224         TRACE("returning %s for property %s\n", debugstr_wn(szValueBuf, *pchValueBuf),
2225             debugstr_w(szName));
2226     else if (rc == ERROR_MORE_DATA)
2227         TRACE("need %d sized buffer for %s\n", *pchValueBuf,
2228             debugstr_w(szName));
2229     else
2230     {
2231         *pchValueBuf = 0;
2232         TRACE("property %s not found\n", debugstr_w(szName));
2233     }
2234
2235     return rc;
2236 }
2237
2238 LPWSTR msi_dup_property(MSIDATABASE *db, LPCWSTR prop)
2239 {
2240     DWORD sz = 0;
2241     LPWSTR str;
2242     UINT r;
2243
2244     r = msi_get_property(db, prop, NULL, &sz);
2245     if (r != ERROR_SUCCESS && r != ERROR_MORE_DATA)
2246         return NULL;
2247
2248     sz++;
2249     str = msi_alloc(sz * sizeof(WCHAR));
2250     r = msi_get_property(db, prop, str, &sz);
2251     if (r != ERROR_SUCCESS)
2252     {
2253         msi_free(str);
2254         str = NULL;
2255     }
2256
2257     return str;
2258 }
2259
2260 int msi_get_property_int( MSIDATABASE *db, LPCWSTR prop, int def )
2261 {
2262     LPWSTR str = msi_dup_property( db, prop );
2263     int val = str ? atoiW(str) : def;
2264     msi_free(str);
2265     return val;
2266 }
2267
2268 static UINT MSI_GetProperty( MSIHANDLE handle, LPCWSTR name,
2269                              awstring *szValueBuf, LPDWORD pchValueBuf )
2270 {
2271     MSIPACKAGE *package;
2272     MSIRECORD *row = NULL;
2273     UINT r = ERROR_FUNCTION_FAILED;
2274     LPCWSTR val = NULL;
2275     DWORD len = 0;
2276
2277     TRACE("%u %s %p %p\n", handle, debugstr_w(name),
2278           szValueBuf->str.w, pchValueBuf );
2279
2280     if (!name)
2281         return ERROR_INVALID_PARAMETER;
2282
2283     package = msihandle2msiinfo( handle, MSIHANDLETYPE_PACKAGE );
2284     if (!package)
2285     {
2286         HRESULT hr;
2287         IWineMsiRemotePackage *remote_package;
2288         LPWSTR value = NULL;
2289         BSTR bname;
2290
2291         remote_package = (IWineMsiRemotePackage *)msi_get_remote( handle );
2292         if (!remote_package)
2293             return ERROR_INVALID_HANDLE;
2294
2295         bname = SysAllocString( name );
2296         if (!bname)
2297         {
2298             IWineMsiRemotePackage_Release( remote_package );
2299             return ERROR_OUTOFMEMORY;
2300         }
2301
2302         hr = IWineMsiRemotePackage_GetProperty( remote_package, bname, NULL, &len );
2303         if (FAILED(hr))
2304             goto done;
2305
2306         len++;
2307         value = msi_alloc(len * sizeof(WCHAR));
2308         if (!value)
2309         {
2310             r = ERROR_OUTOFMEMORY;
2311             goto done;
2312         }
2313
2314         hr = IWineMsiRemotePackage_GetProperty( remote_package, bname, value, &len );
2315         if (FAILED(hr))
2316             goto done;
2317
2318         r = msi_strcpy_to_awstring( value, len, szValueBuf, pchValueBuf );
2319
2320         /* Bug required by Adobe installers */
2321         if (!szValueBuf->unicode && !szValueBuf->str.a)
2322             *pchValueBuf *= sizeof(WCHAR);
2323
2324 done:
2325         IWineMsiRemotePackage_Release(remote_package);
2326         SysFreeString(bname);
2327         msi_free(value);
2328
2329         if (FAILED(hr))
2330         {
2331             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
2332                 return HRESULT_CODE(hr);
2333
2334             return ERROR_FUNCTION_FAILED;
2335         }
2336
2337         return r;
2338     }
2339
2340     row = msi_get_property_row( package->db, name );
2341     if (row)
2342         val = msi_record_get_string( row, 1, (int *)&len );
2343
2344     if (!val)
2345         val = szEmpty;
2346
2347     r = msi_strcpy_to_awstring( val, len, szValueBuf, pchValueBuf );
2348
2349     if (row)
2350         msiobj_release( &row->hdr );
2351     msiobj_release( &package->hdr );
2352
2353     return r;
2354 }
2355
2356 UINT WINAPI MsiGetPropertyA( MSIHANDLE hInstall, LPCSTR szName,
2357                              LPSTR szValueBuf, LPDWORD pchValueBuf )
2358 {
2359     awstring val;
2360     LPWSTR name;
2361     UINT r;
2362
2363     val.unicode = FALSE;
2364     val.str.a = szValueBuf;
2365
2366     name = strdupAtoW( szName );
2367     if (szName && !name)
2368         return ERROR_OUTOFMEMORY;
2369
2370     r = MSI_GetProperty( hInstall, name, &val, pchValueBuf );
2371     msi_free( name );
2372     return r;
2373 }
2374
2375 UINT WINAPI MsiGetPropertyW( MSIHANDLE hInstall, LPCWSTR szName,
2376                              LPWSTR szValueBuf, LPDWORD pchValueBuf )
2377 {
2378     awstring val;
2379
2380     val.unicode = TRUE;
2381     val.str.w = szValueBuf;
2382
2383     return MSI_GetProperty( hInstall, szName, &val, pchValueBuf );
2384 }
2385
2386 typedef struct _msi_remote_package_impl {
2387     IWineMsiRemotePackage IWineMsiRemotePackage_iface;
2388     MSIHANDLE package;
2389     LONG refs;
2390 } msi_remote_package_impl;
2391
2392 static inline msi_remote_package_impl *impl_from_IWineMsiRemotePackage( IWineMsiRemotePackage *iface )
2393 {
2394     return CONTAINING_RECORD(iface, msi_remote_package_impl, IWineMsiRemotePackage_iface);
2395 }
2396
2397 static HRESULT WINAPI mrp_QueryInterface( IWineMsiRemotePackage *iface,
2398                 REFIID riid,LPVOID *ppobj)
2399 {
2400     if( IsEqualCLSID( riid, &IID_IUnknown ) ||
2401         IsEqualCLSID( riid, &IID_IWineMsiRemotePackage ) )
2402     {
2403         IWineMsiRemotePackage_AddRef( iface );
2404         *ppobj = iface;
2405         return S_OK;
2406     }
2407
2408     return E_NOINTERFACE;
2409 }
2410
2411 static ULONG WINAPI mrp_AddRef( IWineMsiRemotePackage *iface )
2412 {
2413     msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2414
2415     return InterlockedIncrement( &This->refs );
2416 }
2417
2418 static ULONG WINAPI mrp_Release( IWineMsiRemotePackage *iface )
2419 {
2420     msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2421     ULONG r;
2422
2423     r = InterlockedDecrement( &This->refs );
2424     if (r == 0)
2425     {
2426         MsiCloseHandle( This->package );
2427         msi_free( This );
2428     }
2429     return r;
2430 }
2431
2432 static HRESULT WINAPI mrp_SetMsiHandle( IWineMsiRemotePackage *iface, MSIHANDLE handle )
2433 {
2434     msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2435     This->package = handle;
2436     return S_OK;
2437 }
2438
2439 static HRESULT WINAPI mrp_GetActiveDatabase( IWineMsiRemotePackage *iface, MSIHANDLE *handle )
2440 {
2441     msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2442     IWineMsiRemoteDatabase *rdb = NULL;
2443     HRESULT hr;
2444     MSIHANDLE hdb;
2445
2446     hr = create_msi_remote_database( NULL, (LPVOID *)&rdb );
2447     if (FAILED(hr) || !rdb)
2448     {
2449         ERR("Failed to create remote database\n");
2450         return hr;
2451     }
2452
2453     hdb = MsiGetActiveDatabase(This->package);
2454
2455     hr = IWineMsiRemoteDatabase_SetMsiHandle( rdb, hdb );
2456     if (FAILED(hr))
2457     {
2458         ERR("Failed to set the database handle\n");
2459         return hr;
2460     }
2461
2462     *handle = alloc_msi_remote_handle( (IUnknown *)rdb );
2463     return S_OK;
2464 }
2465
2466 static HRESULT WINAPI mrp_GetProperty( IWineMsiRemotePackage *iface, BSTR property, BSTR value, DWORD *size )
2467 {
2468     msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2469     UINT r = MsiGetPropertyW(This->package, property, value, size);
2470     if (r != ERROR_SUCCESS) return HRESULT_FROM_WIN32(r);
2471     return S_OK;
2472 }
2473
2474 static HRESULT WINAPI mrp_SetProperty( IWineMsiRemotePackage *iface, BSTR property, BSTR value )
2475 {
2476     msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2477     UINT r = MsiSetPropertyW(This->package, property, value);
2478     return HRESULT_FROM_WIN32(r);
2479 }
2480
2481 static HRESULT WINAPI mrp_ProcessMessage( IWineMsiRemotePackage *iface, INSTALLMESSAGE message, MSIHANDLE record )
2482 {
2483     msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2484     UINT r = MsiProcessMessage(This->package, message, record);
2485     return HRESULT_FROM_WIN32(r);
2486 }
2487
2488 static HRESULT WINAPI mrp_DoAction( IWineMsiRemotePackage *iface, BSTR action )
2489 {
2490     msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2491     UINT r = MsiDoActionW(This->package, action);
2492     return HRESULT_FROM_WIN32(r);
2493 }
2494
2495 static HRESULT WINAPI mrp_Sequence( IWineMsiRemotePackage *iface, BSTR table, int sequence )
2496 {
2497     msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2498     UINT r = MsiSequenceW(This->package, table, sequence);
2499     return HRESULT_FROM_WIN32(r);
2500 }
2501
2502 static HRESULT WINAPI mrp_GetTargetPath( IWineMsiRemotePackage *iface, BSTR folder, BSTR value, DWORD *size )
2503 {
2504     msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2505     UINT r = MsiGetTargetPathW(This->package, folder, value, size);
2506     return HRESULT_FROM_WIN32(r);
2507 }
2508
2509 static HRESULT WINAPI mrp_SetTargetPath( IWineMsiRemotePackage *iface, BSTR folder, BSTR value)
2510 {
2511     msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2512     UINT r = MsiSetTargetPathW(This->package, folder, value);
2513     return HRESULT_FROM_WIN32(r);
2514 }
2515
2516 static HRESULT WINAPI mrp_GetSourcePath( IWineMsiRemotePackage *iface, BSTR folder, BSTR value, DWORD *size )
2517 {
2518     msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2519     UINT r = MsiGetSourcePathW(This->package, folder, value, size);
2520     return HRESULT_FROM_WIN32(r);
2521 }
2522
2523 static HRESULT WINAPI mrp_GetMode( IWineMsiRemotePackage *iface, MSIRUNMODE mode, BOOL *ret )
2524 {
2525     msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2526     *ret = MsiGetMode(This->package, mode);
2527     return S_OK;
2528 }
2529
2530 static HRESULT WINAPI mrp_SetMode( IWineMsiRemotePackage *iface, MSIRUNMODE mode, BOOL state )
2531 {
2532     msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2533     UINT r = MsiSetMode(This->package, mode, state);
2534     return HRESULT_FROM_WIN32(r);
2535 }
2536
2537 static HRESULT WINAPI mrp_GetFeatureState( IWineMsiRemotePackage *iface, BSTR feature,
2538                                     INSTALLSTATE *installed, INSTALLSTATE *action )
2539 {
2540     msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2541     UINT r = MsiGetFeatureStateW(This->package, feature, installed, action);
2542     return HRESULT_FROM_WIN32(r);
2543 }
2544
2545 static HRESULT WINAPI mrp_SetFeatureState( IWineMsiRemotePackage *iface, BSTR feature, INSTALLSTATE state )
2546 {
2547     msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2548     UINT r = MsiSetFeatureStateW(This->package, feature, state);
2549     return HRESULT_FROM_WIN32(r);
2550 }
2551
2552 static HRESULT WINAPI mrp_GetComponentState( IWineMsiRemotePackage *iface, BSTR component,
2553                                       INSTALLSTATE *installed, INSTALLSTATE *action )
2554 {
2555     msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2556     UINT r = MsiGetComponentStateW(This->package, component, installed, action);
2557     return HRESULT_FROM_WIN32(r);
2558 }
2559
2560 static HRESULT WINAPI mrp_SetComponentState( IWineMsiRemotePackage *iface, BSTR component, INSTALLSTATE state )
2561 {
2562     msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2563     UINT r = MsiSetComponentStateW(This->package, component, state);
2564     return HRESULT_FROM_WIN32(r);
2565 }
2566
2567 static HRESULT WINAPI mrp_GetLanguage( IWineMsiRemotePackage *iface, LANGID *language )
2568 {
2569     msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2570     *language = MsiGetLanguage(This->package);
2571     return S_OK;
2572 }
2573
2574 static HRESULT WINAPI mrp_SetInstallLevel( IWineMsiRemotePackage *iface, int level )
2575 {
2576     msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2577     UINT r = MsiSetInstallLevel(This->package, level);
2578     return HRESULT_FROM_WIN32(r);
2579 }
2580
2581 static HRESULT WINAPI mrp_FormatRecord( IWineMsiRemotePackage *iface, MSIHANDLE record,
2582                                         BSTR *value)
2583 {
2584     DWORD size = 0;
2585     msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2586     UINT r = MsiFormatRecordW(This->package, record, NULL, &size);
2587     if (r == ERROR_SUCCESS)
2588     {
2589         *value = SysAllocStringLen(NULL, size);
2590         if (!*value)
2591             return E_OUTOFMEMORY;
2592         size++;
2593         r = MsiFormatRecordW(This->package, record, *value, &size);
2594     }
2595     return HRESULT_FROM_WIN32(r);
2596 }
2597
2598 static HRESULT WINAPI mrp_EvaluateCondition( IWineMsiRemotePackage *iface, BSTR condition )
2599 {
2600     msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2601     UINT r = MsiEvaluateConditionW(This->package, condition);
2602     return HRESULT_FROM_WIN32(r);
2603 }
2604
2605 static HRESULT WINAPI mrp_GetFeatureCost( IWineMsiRemotePackage *iface, BSTR feature,
2606                                           INT cost_tree, INSTALLSTATE state, INT *cost )
2607 {
2608     msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2609     UINT r = MsiGetFeatureCostW(This->package, feature, cost_tree, state, cost);
2610     return HRESULT_FROM_WIN32(r);
2611 }
2612
2613 static HRESULT WINAPI mrp_EnumComponentCosts( IWineMsiRemotePackage *iface, BSTR component,
2614                                               DWORD index, INSTALLSTATE state, BSTR drive,
2615                                               DWORD *buflen, INT *cost, INT *temp )
2616 {
2617     msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2618     UINT r = MsiEnumComponentCostsW(This->package, component, index, state, drive, buflen, cost, temp);
2619     return HRESULT_FROM_WIN32(r);
2620 }
2621
2622 static const IWineMsiRemotePackageVtbl msi_remote_package_vtbl =
2623 {
2624     mrp_QueryInterface,
2625     mrp_AddRef,
2626     mrp_Release,
2627     mrp_SetMsiHandle,
2628     mrp_GetActiveDatabase,
2629     mrp_GetProperty,
2630     mrp_SetProperty,
2631     mrp_ProcessMessage,
2632     mrp_DoAction,
2633     mrp_Sequence,
2634     mrp_GetTargetPath,
2635     mrp_SetTargetPath,
2636     mrp_GetSourcePath,
2637     mrp_GetMode,
2638     mrp_SetMode,
2639     mrp_GetFeatureState,
2640     mrp_SetFeatureState,
2641     mrp_GetComponentState,
2642     mrp_SetComponentState,
2643     mrp_GetLanguage,
2644     mrp_SetInstallLevel,
2645     mrp_FormatRecord,
2646     mrp_EvaluateCondition,
2647     mrp_GetFeatureCost,
2648     mrp_EnumComponentCosts
2649 };
2650
2651 HRESULT create_msi_remote_package( IUnknown *pOuter, LPVOID *ppObj )
2652 {
2653     msi_remote_package_impl* This;
2654
2655     This = msi_alloc( sizeof *This );
2656     if (!This)
2657         return E_OUTOFMEMORY;
2658
2659     This->IWineMsiRemotePackage_iface.lpVtbl = &msi_remote_package_vtbl;
2660     This->package = 0;
2661     This->refs = 1;
2662
2663     *ppObj = This;
2664
2665     return S_OK;
2666 }
2667
2668 UINT msi_package_add_info(MSIPACKAGE *package, DWORD context, DWORD options,
2669                           LPCWSTR property, LPWSTR value)
2670 {
2671     MSISOURCELISTINFO *info;
2672
2673     LIST_FOR_EACH_ENTRY( info, &package->sourcelist_info, MSISOURCELISTINFO, entry )
2674     {
2675         if (!strcmpW( info->value, value )) return ERROR_SUCCESS;
2676     }
2677
2678     info = msi_alloc(sizeof(MSISOURCELISTINFO));
2679     if (!info)
2680         return ERROR_OUTOFMEMORY;
2681
2682     info->context = context;
2683     info->options = options;
2684     info->property = property;
2685     info->value = strdupW(value);
2686     list_add_head(&package->sourcelist_info, &info->entry);
2687
2688     return ERROR_SUCCESS;
2689 }
2690
2691 UINT msi_package_add_media_disk(MSIPACKAGE *package, DWORD context, DWORD options,
2692                                 DWORD disk_id, LPWSTR volume_label, LPWSTR disk_prompt)
2693 {
2694     MSIMEDIADISK *disk;
2695
2696     LIST_FOR_EACH_ENTRY( disk, &package->sourcelist_media, MSIMEDIADISK, entry )
2697     {
2698         if (disk->disk_id == disk_id) return ERROR_SUCCESS;
2699     }
2700
2701     disk = msi_alloc(sizeof(MSIMEDIADISK));
2702     if (!disk)
2703         return ERROR_OUTOFMEMORY;
2704
2705     disk->context = context;
2706     disk->options = options;
2707     disk->disk_id = disk_id;
2708     disk->volume_label = strdupW(volume_label);
2709     disk->disk_prompt = strdupW(disk_prompt);
2710     list_add_head(&package->sourcelist_media, &disk->entry);
2711
2712     return ERROR_SUCCESS;
2713 }