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