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