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