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