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