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