winecoreaudio: Make wodMessage signature match WINEMM_msgFunc32 typedef.
[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 MSI_FreePackage( MSIOBJECTHDR *arg)
53 {
54     MSIPACKAGE *package= (MSIPACKAGE*) arg;
55
56     if( package->dialog )
57         msi_dialog_destroy( package->dialog );
58
59     msiobj_release( &package->db->hdr );
60     ACTION_free_package_structures(package);
61 }
62
63 static UINT create_temp_property_table(MSIPACKAGE *package)
64 {
65     MSIQUERY *view = NULL;
66     UINT rc;
67
68     static const WCHAR CreateSql[] = {
69        'C','R','E','A','T','E',' ','T','A','B','L','E',' ',
70        '`','_','P','r','o','p','e','r','t','y','`',' ','(',' ',
71        '`','_','P','r','o','p','e','r','t','y','`',' ',
72        'C','H','A','R','(','5','6',')',' ','N','O','T',' ','N','U','L','L',' ',
73        'T','E','M','P','O','R','A','R','Y',',',' ',
74        '`','V','a','l','u','e','`',' ','C','H','A','R','(','9','8',')',' ',
75        'N','O','T',' ','N','U','L','L',' ','T','E','M','P','O','R','A','R','Y',
76        ' ','P','R','I','M','A','R','Y',' ','K','E','Y',' ',
77        '`','_','P','r','o','p','e','r','t','y','`',')',' ','H','O','L','D',0};
78
79     rc = MSI_DatabaseOpenViewW(package->db, CreateSql, &view);
80     if (rc != ERROR_SUCCESS)
81         return rc;
82
83     rc = MSI_ViewExecute(view, 0);
84     MSI_ViewClose(view);
85     msiobj_release(&view->hdr);
86     return rc;
87 }
88
89 UINT msi_clone_properties(MSIPACKAGE *package)
90 {
91     MSIQUERY *view = NULL;
92     UINT rc;
93
94     static const WCHAR Query[] = {
95        'S','E','L','E','C','T',' ','*',' ',
96        'F','R','O','M',' ','`','P','r','o','p','e','r','t','y','`',0};
97     static const WCHAR Insert[] = {
98        'I','N','S','E','R','T',' ','i','n','t','o',' ',
99        '`','_','P','r','o','p','e','r','t','y','`',' ',
100        '(','`','_','P','r','o','p','e','r','t','y','`',',',
101        '`','V','a','l','u','e','`',')',' ',
102        'V','A','L','U','E','S',' ','(','?',',','?',')',0};
103
104     /* clone the existing properties */
105     rc = MSI_DatabaseOpenViewW(package->db, Query, &view);
106     if (rc != ERROR_SUCCESS)
107         return rc;
108
109     rc = MSI_ViewExecute(view, 0);
110     if (rc != ERROR_SUCCESS)
111     {
112         MSI_ViewClose(view);
113         msiobj_release(&view->hdr);
114         return rc;
115     }
116
117     while (1)
118     {
119         MSIRECORD *row;
120         MSIQUERY *view2;
121
122         rc = MSI_ViewFetch(view, &row);
123         if (rc != ERROR_SUCCESS)
124             break;
125
126         rc = MSI_DatabaseOpenViewW(package->db, Insert, &view2);
127         if (rc != ERROR_SUCCESS)
128         {
129             msiobj_release(&row->hdr);
130             continue;
131         }
132
133         MSI_ViewExecute(view2, row);
134         MSI_ViewClose(view2);
135         msiobj_release(&view2->hdr);
136         msiobj_release(&row->hdr);
137     }
138
139     MSI_ViewClose(view);
140     msiobj_release(&view->hdr);
141
142     return rc;
143 }
144
145 /*
146  * set_installed_prop
147  *
148  * Sets the "Installed" property to indicate that
149  *  the product is installed for the current user.
150  */
151 static UINT set_installed_prop( MSIPACKAGE *package )
152 {
153     static const WCHAR szInstalled[] = {
154         'I','n','s','t','a','l','l','e','d',0 };
155     WCHAR val[2] = { '1', 0 };
156     HKEY hkey = 0;
157     UINT r;
158
159     r = MSIREG_OpenUninstallKey( package->ProductCode, &hkey, FALSE );
160     if (r == ERROR_SUCCESS)
161     {
162         RegCloseKey( hkey );
163         MSI_SetPropertyW( package, szInstalled, val );
164     }
165
166     return r;
167 }
168
169 static UINT set_user_sid_prop( MSIPACKAGE *package )
170 {
171     SID_NAME_USE use;
172     LPWSTR user_name;
173     LPWSTR sid_str = NULL, dom = NULL;
174     DWORD size, dom_size;
175     PSID psid = NULL;
176     UINT r = ERROR_FUNCTION_FAILED;
177
178     static const WCHAR user_sid[] = {'U','s','e','r','S','I','D',0};
179
180     size = 0;
181     GetUserNameW( NULL, &size );
182
183     user_name = msi_alloc( (size + 1) * sizeof(WCHAR) );
184     if (!user_name)
185         return ERROR_OUTOFMEMORY;
186
187     if (!GetUserNameW( user_name, &size ))
188         goto done;
189
190     size = 0;
191     dom_size = 0;
192     LookupAccountNameW( NULL, user_name, NULL, &size, NULL, &dom_size, &use );
193
194     psid = msi_alloc( size );
195     dom = msi_alloc( dom_size*sizeof (WCHAR) );
196     if (!psid || !dom)
197     {
198         r = ERROR_OUTOFMEMORY;
199         goto done;
200     }
201
202     if (!LookupAccountNameW( NULL, user_name, psid, &size, dom, &dom_size, &use ))
203         goto done;
204
205     if (!ConvertSidToStringSidW( psid, &sid_str ))
206         goto done;
207
208     r = MSI_SetPropertyW( package, user_sid, sid_str );
209
210 done:
211     LocalFree( sid_str );
212     msi_free( dom );
213     msi_free( psid );
214     msi_free( user_name );
215
216     return r;
217 }
218
219 static LPWSTR get_fusion_filename(MSIPACKAGE *package)
220 {
221     HKEY netsetup;
222     LONG res;
223     LPWSTR file = NULL;
224     DWORD index = 0, size;
225     WCHAR ver[MAX_PATH];
226     WCHAR name[MAX_PATH];
227     WCHAR windir[MAX_PATH];
228
229     static const WCHAR backslash[] = {'\\',0};
230     static const WCHAR fusion[] = {'f','u','s','i','o','n','.','d','l','l',0};
231     static const WCHAR sub[] = {
232         'S','o','f','t','w','a','r','e','\\',
233         'M','i','c','r','o','s','o','f','t','\\',
234         'N','E','T',' ','F','r','a','m','e','w','o','r','k',' ','S','e','t','u','p','\\',
235         'N','D','P',0
236     };
237     static const WCHAR subdir[] = {
238         'M','i','c','r','o','s','o','f','t','.','N','E','T','\\',
239         'F','r','a','m','e','w','o','r','k','\\',0
240     };
241
242     res = RegOpenKeyExW(HKEY_LOCAL_MACHINE, sub, 0, KEY_ENUMERATE_SUB_KEYS, &netsetup);
243     if (res != ERROR_SUCCESS)
244         return NULL;
245
246     GetWindowsDirectoryW(windir, MAX_PATH);
247
248     ver[0] = '\0';
249     size = MAX_PATH;
250     while (RegEnumKeyExW(netsetup, index, name, &size, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
251     {
252         index++;
253
254         /* verify existence of fusion.dll .Net 3.0 does not install a new one */
255         if (lstrcmpW(ver, name) < 0)
256         {
257             LPWSTR check;
258             size = lstrlenW(windir) + lstrlenW(subdir) + lstrlenW(name) +lstrlenW(fusion) + 3;
259             check = msi_alloc(size * sizeof(WCHAR));
260
261             if (!check)
262             {
263                 msi_free(file);
264                 return NULL;
265             }
266
267             lstrcpyW(check, windir);
268             lstrcatW(check, backslash);
269             lstrcatW(check, subdir);
270             lstrcatW(check, name);
271             lstrcatW(check, backslash);
272             lstrcatW(check, fusion);
273
274             if(GetFileAttributesW(check) != INVALID_FILE_ATTRIBUTES)
275             {
276                 msi_free(file);
277                 file = check;
278                 lstrcpyW(ver, name);
279             }
280             else
281                 msi_free(check);
282         }
283     }
284
285     RegCloseKey(netsetup);
286     return file;
287 }
288
289 typedef struct tagLANGANDCODEPAGE
290 {
291   WORD wLanguage;
292   WORD wCodePage;
293 } LANGANDCODEPAGE;
294
295 static void set_msi_assembly_prop(MSIPACKAGE *package)
296 {
297     UINT val_len;
298     DWORD size, handle;
299     LPVOID version = NULL;
300     WCHAR buf[MAX_PATH];
301     LPWSTR fusion, verstr;
302     LANGANDCODEPAGE *translate;
303
304     static const WCHAR netasm[] = {
305         'M','s','i','N','e','t','A','s','s','e','m','b','l','y','S','u','p','p','o','r','t',0
306     };
307     static const WCHAR translation[] = {
308         '\\','V','a','r','F','i','l','e','I','n','f','o',
309         '\\','T','r','a','n','s','l','a','t','i','o','n',0
310     };
311     static const WCHAR verfmt[] = {
312         '\\','S','t','r','i','n','g','F','i','l','e','I','n','f','o',
313         '\\','%','0','4','x','%','0','4','x',
314         '\\','P','r','o','d','u','c','t','V','e','r','s','i','o','n',0
315     };
316
317     fusion = get_fusion_filename(package);
318     if (!fusion)
319         return;
320
321     size = GetFileVersionInfoSizeW(fusion, &handle);
322     if (!size) return;
323
324     version = msi_alloc(size);
325     if (!version) return;
326
327     if (!GetFileVersionInfoW(fusion, handle, size, version))
328         goto done;
329
330     if (!VerQueryValueW(version, translation, (LPVOID *)&translate, &val_len))
331         goto done;
332
333     sprintfW(buf, verfmt, translate[0].wLanguage, translate[0].wCodePage);
334
335     if (!VerQueryValueW(version, buf, (LPVOID *)&verstr, &val_len))
336         goto done;
337
338     if (!val_len || !verstr)
339         goto done;
340
341     MSI_SetPropertyW(package, netasm, verstr);
342
343 done:
344     msi_free(fusion);
345     msi_free(version);
346 }
347
348 static VOID set_installer_properties(MSIPACKAGE *package)
349 {
350     WCHAR pth[MAX_PATH];
351     WCHAR *ptr;
352     OSVERSIONINFOEXW OSVersion;
353     MEMORYSTATUSEX msex;
354     DWORD verval;
355     WCHAR verstr[10], bufstr[20];
356     HDC dc;
357     HKEY hkey;
358     LPWSTR username, companyname;
359     SYSTEM_INFO sys_info;
360     SYSTEMTIME systemtime;
361     LANGID langid;
362
363     static const WCHAR cszbs[]={'\\',0};
364     static const WCHAR CFF[] = 
365 {'C','o','m','m','o','n','F','i','l','e','s','F','o','l','d','e','r',0};
366     static const WCHAR PFF[] = 
367 {'P','r','o','g','r','a','m','F','i','l','e','s','F','o','l','d','e','r',0};
368     static const WCHAR CADF[] = 
369 {'C','o','m','m','o','n','A','p','p','D','a','t','a','F','o','l','d','e','r',0};
370     static const WCHAR FaF[] = 
371 {'F','a','v','o','r','i','t','e','s','F','o','l','d','e','r',0};
372     static const WCHAR FoF[] = 
373 {'F','o','n','t','s','F','o','l','d','e','r',0};
374     static const WCHAR SendTF[] = 
375 {'S','e','n','d','T','o','F','o','l','d','e','r',0};
376     static const WCHAR SMF[] = 
377 {'S','t','a','r','t','M','e','n','u','F','o','l','d','e','r',0};
378     static const WCHAR StF[] = 
379 {'S','t','a','r','t','u','p','F','o','l','d','e','r',0};
380     static const WCHAR TemplF[] = 
381 {'T','e','m','p','l','a','t','e','F','o','l','d','e','r',0};
382     static const WCHAR DF[] = 
383 {'D','e','s','k','t','o','p','F','o','l','d','e','r',0};
384     static const WCHAR PMF[] = 
385 {'P','r','o','g','r','a','m','M','e','n','u','F','o','l','d','e','r',0};
386     static const WCHAR ATF[] = 
387 {'A','d','m','i','n','T','o','o','l','s','F','o','l','d','e','r',0};
388     static const WCHAR ADF[] = 
389 {'A','p','p','D','a','t','a','F','o','l','d','e','r',0};
390     static const WCHAR SF[] = 
391 {'S','y','s','t','e','m','F','o','l','d','e','r',0};
392     static const WCHAR SF16[] = 
393 {'S','y','s','t','e','m','1','6','F','o','l','d','e','r',0};
394     static const WCHAR LADF[] = 
395 {'L','o','c','a','l','A','p','p','D','a','t','a','F','o','l','d','e','r',0};
396     static const WCHAR MPF[] = 
397 {'M','y','P','i','c','t','u','r','e','s','F','o','l','d','e','r',0};
398     static const WCHAR PF[] = 
399 {'P','e','r','s','o','n','a','l','F','o','l','d','e','r',0};
400     static const WCHAR WF[] = 
401 {'W','i','n','d','o','w','s','F','o','l','d','e','r',0};
402     static const WCHAR WV[] = 
403 {'W','i','n','d','o','w','s','V','o','l','u','m','e',0};
404     static const WCHAR TF[]=
405 {'T','e','m','p','F','o','l','d','e','r',0};
406     static const WCHAR szAdminUser[] =
407 {'A','d','m','i','n','U','s','e','r',0};
408     static const WCHAR szPriv[] =
409 {'P','r','i','v','i','l','e','g','e','d',0};
410     static const WCHAR szOne[] =
411 {'1',0};
412     static const WCHAR v9x[] = { 'V','e','r','s','i','o','n','9','X',0 };
413     static const WCHAR vNT[] = { 'V','e','r','s','i','o','n','N','T',0 };
414     static const WCHAR szMsiNTProductType[] = { 'M','s','i','N','T','P','r','o','d','u','c','t','T','y','p','e',0 };
415     static const WCHAR szFormat[] = {'%','l','i',0};
416     static const WCHAR szWinBuild[] =
417 {'W','i','n','d','o','w','s','B','u','i','l','d', 0 };
418     static const WCHAR szSPL[] = 
419 {'S','e','r','v','i','c','e','P','a','c','k','L','e','v','e','l',0 };
420     static const WCHAR szSix[] = {'6',0 };
421
422     static const WCHAR szVersionMsi[] = { 'V','e','r','s','i','o','n','M','s','i',0 };
423     static const WCHAR szVersionDatabase[] = { 'V','e','r','s','i','o','n','D','a','t','a','b','a','s','e',0 };
424     static const WCHAR szPhysicalMemory[] = { 'P','h','y','s','i','c','a','l','M','e','m','o','r','y',0 };
425     static const WCHAR szFormat2[] = {'%','l','i','.','%','l','i',0};
426 /* Screen properties */
427     static const WCHAR szScreenX[] = {'S','c','r','e','e','n','X',0};
428     static const WCHAR szScreenY[] = {'S','c','r','e','e','n','Y',0};
429     static const WCHAR szColorBits[] = {'C','o','l','o','r','B','i','t','s',0};
430     static const WCHAR szIntFormat[] = {'%','d',0};
431     static const WCHAR szIntel[] = { 'I','n','t','e','l',0 };
432     static const WCHAR szUserInfo[] = {
433         'S','O','F','T','W','A','R','E','\\',
434         'M','i','c','r','o','s','o','f','t','\\',
435         'M','S',' ','S','e','t','u','p',' ','(','A','C','M','E',')','\\',
436         'U','s','e','r',' ','I','n','f','o',0
437     };
438     static const WCHAR szDefName[] = { 'D','e','f','N','a','m','e',0 };
439     static const WCHAR szDefCompany[] = { 'D','e','f','C','o','m','p','a','n','y',0 };
440     static const WCHAR szCurrentVersion[] = {
441         'S','O','F','T','W','A','R','E','\\',
442         'M','i','c','r','o','s','o','f','t','\\',
443         'W','i','n','d','o','w','s',' ','N','T','\\',
444         'C','u','r','r','e','n','t','V','e','r','s','i','o','n',0
445     };
446     static const WCHAR szRegisteredUser[] = {'R','e','g','i','s','t','e','r','e','d','O','w','n','e','r',0};
447     static const WCHAR szRegisteredOrg[] = {
448         'R','e','g','i','s','t','e','r','e','d','O','r','g','a','n','i','z','a','t','i','o','n',0
449     };
450     static const WCHAR szUSERNAME[] = {'U','S','E','R','N','A','M','E',0};
451     static const WCHAR szCOMPANYNAME[] = {'C','O','M','P','A','N','Y','N','A','M','E',0};
452     static const WCHAR szDate[] = {'D','a','t','e',0};
453     static const WCHAR szTime[] = {'T','i','m','e',0};
454     static const WCHAR szUserLangID[] = {'U','s','e','r','L','a','n','g','u','a','g','e','I','D',0};
455     static const WCHAR szSystemLangID[] = {'S','y','s','t','e','m','L','a','n','g','u','a','g','e','I','D',0};
456     static const WCHAR szProductState[] = {'P','r','o','d','u','c','t','S','t','a','t','e',0};
457
458     /*
459      * Other things that probably should be set:
460      *
461      * ComputerName LogonUser VirtualMemory
462      * ShellAdvSupport DefaultUIFont PackagecodeChanging
463      * CaptionHeight BorderTop BorderSide TextHeight
464      * RedirectedDllSupport
465      */
466
467     SHGetFolderPathW(NULL,CSIDL_PROGRAM_FILES_COMMON,NULL,0,pth);
468     strcatW(pth,cszbs);
469     MSI_SetPropertyW(package, CFF, pth);
470
471     SHGetFolderPathW(NULL,CSIDL_PROGRAM_FILES,NULL,0,pth);
472     strcatW(pth,cszbs);
473     MSI_SetPropertyW(package, PFF, pth);
474
475     SHGetFolderPathW(NULL,CSIDL_COMMON_APPDATA,NULL,0,pth);
476     strcatW(pth,cszbs);
477     MSI_SetPropertyW(package, CADF, pth);
478
479     SHGetFolderPathW(NULL,CSIDL_FAVORITES,NULL,0,pth);
480     strcatW(pth,cszbs);
481     MSI_SetPropertyW(package, FaF, pth);
482
483     SHGetFolderPathW(NULL,CSIDL_FONTS,NULL,0,pth);
484     strcatW(pth,cszbs);
485     MSI_SetPropertyW(package, FoF, pth);
486
487     SHGetFolderPathW(NULL,CSIDL_SENDTO,NULL,0,pth);
488     strcatW(pth,cszbs);
489     MSI_SetPropertyW(package, SendTF, pth);
490
491     SHGetFolderPathW(NULL,CSIDL_STARTMENU,NULL,0,pth);
492     strcatW(pth,cszbs);
493     MSI_SetPropertyW(package, SMF, pth);
494
495     SHGetFolderPathW(NULL,CSIDL_STARTUP,NULL,0,pth);
496     strcatW(pth,cszbs);
497     MSI_SetPropertyW(package, StF, pth);
498
499     SHGetFolderPathW(NULL,CSIDL_TEMPLATES,NULL,0,pth);
500     strcatW(pth,cszbs);
501     MSI_SetPropertyW(package, TemplF, pth);
502
503     SHGetFolderPathW(NULL,CSIDL_DESKTOP,NULL,0,pth);
504     strcatW(pth,cszbs);
505     MSI_SetPropertyW(package, DF, pth);
506
507     SHGetFolderPathW(NULL,CSIDL_PROGRAMS,NULL,0,pth);
508     strcatW(pth,cszbs);
509     MSI_SetPropertyW(package, PMF, pth);
510
511     SHGetFolderPathW(NULL,CSIDL_ADMINTOOLS,NULL,0,pth);
512     strcatW(pth,cszbs);
513     MSI_SetPropertyW(package, ATF, pth);
514
515     SHGetFolderPathW(NULL,CSIDL_APPDATA,NULL,0,pth);
516     strcatW(pth,cszbs);
517     MSI_SetPropertyW(package, ADF, pth);
518
519     SHGetFolderPathW(NULL,CSIDL_SYSTEM,NULL,0,pth);
520     strcatW(pth,cszbs);
521     MSI_SetPropertyW(package, SF, pth);
522     MSI_SetPropertyW(package, SF16, pth);
523
524     SHGetFolderPathW(NULL,CSIDL_LOCAL_APPDATA,NULL,0,pth);
525     strcatW(pth,cszbs);
526     MSI_SetPropertyW(package, LADF, pth);
527
528     SHGetFolderPathW(NULL,CSIDL_MYPICTURES,NULL,0,pth);
529     strcatW(pth,cszbs);
530     MSI_SetPropertyW(package, MPF, pth);
531
532     SHGetFolderPathW(NULL,CSIDL_PERSONAL,NULL,0,pth);
533     strcatW(pth,cszbs);
534     MSI_SetPropertyW(package, PF, pth);
535
536     SHGetFolderPathW(NULL,CSIDL_WINDOWS,NULL,0,pth);
537     strcatW(pth,cszbs);
538     MSI_SetPropertyW(package, WF, pth);
539     
540     /* Physical Memory is specified in MB. Using total amount. */
541     msex.dwLength = sizeof(msex);
542     GlobalMemoryStatusEx( &msex );
543     sprintfW( bufstr, szIntFormat, (int)(msex.ullTotalPhys/1024/1024));
544     MSI_SetPropertyW(package, szPhysicalMemory, bufstr);
545
546     SHGetFolderPathW(NULL,CSIDL_WINDOWS,NULL,0,pth);
547     ptr = strchrW(pth,'\\');
548     if (ptr)
549         *(ptr+1) = 0;
550     MSI_SetPropertyW(package, WV, pth);
551     
552     GetTempPathW(MAX_PATH,pth);
553     MSI_SetPropertyW(package, TF, pth);
554
555
556     /* in a wine environment the user is always admin and privileged */
557     MSI_SetPropertyW(package,szAdminUser,szOne);
558     MSI_SetPropertyW(package,szPriv,szOne);
559
560     /* set the os things */
561     OSVersion.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEXW);
562     GetVersionExW((OSVERSIONINFOW *)&OSVersion);
563     verval = OSVersion.dwMinorVersion+OSVersion.dwMajorVersion*100;
564     sprintfW(verstr,szFormat,verval);
565     switch (OSVersion.dwPlatformId)
566     {
567         case VER_PLATFORM_WIN32_WINDOWS:    
568             MSI_SetPropertyW(package,v9x,verstr);
569             break;
570         case VER_PLATFORM_WIN32_NT:
571             MSI_SetPropertyW(package,vNT,verstr);
572             sprintfW(verstr,szFormat,OSVersion.wProductType);
573             MSI_SetPropertyW(package,szMsiNTProductType,verstr);
574             break;
575     }
576     sprintfW(verstr,szFormat,OSVersion.dwBuildNumber);
577     MSI_SetPropertyW(package,szWinBuild,verstr);
578     /* just fudge this */
579     MSI_SetPropertyW(package,szSPL,szSix);
580
581     sprintfW( bufstr, szFormat2, MSI_MAJORVERSION, MSI_MINORVERSION);
582     MSI_SetPropertyW( package, szVersionMsi, bufstr );
583     sprintfW( bufstr, szFormat, MSI_MAJORVERSION * 100);
584     MSI_SetPropertyW( package, szVersionDatabase, bufstr );
585
586     GetSystemInfo( &sys_info );
587     if (sys_info.u.s.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL)
588     {
589         sprintfW( bufstr, szIntFormat, sys_info.wProcessorLevel );
590         MSI_SetPropertyW( package, szIntel, bufstr );
591     }
592
593     /* Screen properties. */
594     dc = GetDC(0);
595     sprintfW( bufstr, szIntFormat, GetDeviceCaps( dc, HORZRES ) );
596     MSI_SetPropertyW( package, szScreenX, bufstr );
597     sprintfW( bufstr, szIntFormat, GetDeviceCaps( dc, VERTRES ));
598     MSI_SetPropertyW( package, szScreenY, bufstr );
599     sprintfW( bufstr, szIntFormat, GetDeviceCaps( dc, BITSPIXEL ));
600     MSI_SetPropertyW( package, szColorBits, bufstr );
601     ReleaseDC(0, dc);
602
603     /* USERNAME and COMPANYNAME */
604     username = msi_dup_property( package, szUSERNAME );
605     companyname = msi_dup_property( package, szCOMPANYNAME );
606
607     if ((!username || !companyname) &&
608         RegOpenKeyW( HKEY_CURRENT_USER, szUserInfo, &hkey ) == ERROR_SUCCESS)
609     {
610         if (!username &&
611             (username = msi_reg_get_val_str( hkey, szDefName )))
612             MSI_SetPropertyW( package, szUSERNAME, username );
613         if (!companyname &&
614             (companyname = msi_reg_get_val_str( hkey, szDefCompany )))
615             MSI_SetPropertyW( package, szCOMPANYNAME, companyname );
616         CloseHandle( hkey );
617     }
618     if ((!username || !companyname) &&
619         RegOpenKeyW( HKEY_LOCAL_MACHINE, szCurrentVersion, &hkey ) == ERROR_SUCCESS)
620     {
621         if (!username &&
622             (username = msi_reg_get_val_str( hkey, szRegisteredUser )))
623             MSI_SetPropertyW( package, szUSERNAME, username );
624         if (!companyname &&
625             (companyname = msi_reg_get_val_str( hkey, szRegisteredOrg )))
626             MSI_SetPropertyW( package, szCOMPANYNAME, companyname );
627         CloseHandle( hkey );
628     }
629     msi_free( username );
630     msi_free( companyname );
631
632     if ( set_user_sid_prop( package ) != ERROR_SUCCESS)
633         ERR("Failed to set the UserSID property\n");
634
635     /* Date and time properties */
636     GetSystemTime( &systemtime );
637     if (GetDateFormatW( LOCALE_USER_DEFAULT, DATE_SHORTDATE, &systemtime,
638                         NULL, bufstr, sizeof(bufstr)/sizeof(bufstr[0]) ))
639         MSI_SetPropertyW( package, szDate, bufstr );
640     else
641         ERR("Couldn't set Date property: GetDateFormat failed with error %d\n", GetLastError());
642
643     if (GetTimeFormatW( LOCALE_USER_DEFAULT,
644                         TIME_FORCE24HOURFORMAT | TIME_NOTIMEMARKER,
645                         &systemtime, NULL, bufstr,
646                         sizeof(bufstr)/sizeof(bufstr[0]) ))
647         MSI_SetPropertyW( package, szTime, bufstr );
648     else
649         ERR("Couldn't set Time property: GetTimeFormat failed with error %d\n", GetLastError());
650
651     set_msi_assembly_prop( package );
652
653     langid = GetUserDefaultLangID();
654     sprintfW(bufstr, szIntFormat, langid);
655
656     MSI_SetPropertyW( package, szUserLangID, bufstr );
657
658     langid = GetSystemDefaultLangID();
659     sprintfW(bufstr, szIntFormat, langid);
660
661     MSI_SetPropertyW( package, szSystemLangID, bufstr );
662
663     sprintfW(bufstr, szIntFormat, MsiQueryProductStateW(package->ProductCode));
664     MSI_SetPropertyW( package, szProductState, bufstr );
665 }
666
667 static UINT msi_load_summary_properties( MSIPACKAGE *package )
668 {
669     UINT rc;
670     MSIHANDLE suminfo;
671     MSIHANDLE hdb = alloc_msihandle( &package->db->hdr );
672     INT count;
673     DWORD len;
674     LPWSTR package_code;
675     static const WCHAR szPackageCode[] = {
676         'P','a','c','k','a','g','e','C','o','d','e',0};
677
678     if (!hdb) {
679         ERR("Unable to allocate handle\n");
680         return ERROR_OUTOFMEMORY;
681     }
682
683     rc = MsiGetSummaryInformationW( hdb, NULL, 0, &suminfo );
684     MsiCloseHandle(hdb);
685     if (rc != ERROR_SUCCESS)
686     {
687         ERR("Unable to open Summary Information\n");
688         return rc;
689     }
690
691     rc = MsiSummaryInfoGetPropertyW( suminfo, PID_PAGECOUNT, NULL,
692                                      &count, NULL, NULL, NULL );
693     if (rc != ERROR_SUCCESS)
694     {
695         WARN("Unable to query page count: %d\n", rc);
696         goto done;
697     }
698
699     /* load package code property */
700     len = 0;
701     rc = MsiSummaryInfoGetPropertyW( suminfo, PID_REVNUMBER, NULL,
702                                      NULL, NULL, NULL, &len );
703     if (rc != ERROR_MORE_DATA)
704     {
705         WARN("Unable to query revision number: %d\n", rc);
706         rc = ERROR_FUNCTION_FAILED;
707         goto done;
708     }
709
710     len++;
711     package_code = msi_alloc( len * sizeof(WCHAR) );
712     rc = MsiSummaryInfoGetPropertyW( suminfo, PID_REVNUMBER, NULL,
713                                      NULL, NULL, package_code, &len );
714     if (rc != ERROR_SUCCESS)
715     {
716         WARN("Unable to query rev number: %d\n", rc);
717         goto done;
718     }
719
720     MSI_SetPropertyW( package, szPackageCode, package_code );
721     msi_free( package_code );
722
723     /* load package attributes */
724     count = 0;
725     MsiSummaryInfoGetPropertyW( suminfo, PID_WORDCOUNT, NULL,
726                                 &count, NULL, NULL, NULL );
727     package->WordCount = count;
728
729 done:
730     MsiCloseHandle(suminfo);
731     return rc;
732 }
733
734 static MSIPACKAGE *msi_alloc_package( void )
735 {
736     MSIPACKAGE *package;
737
738     package = alloc_msiobject( MSIHANDLETYPE_PACKAGE, sizeof (MSIPACKAGE),
739                                MSI_FreePackage );
740     if( package )
741     {
742         list_init( &package->components );
743         list_init( &package->features );
744         list_init( &package->files );
745         list_init( &package->tempfiles );
746         list_init( &package->folders );
747         list_init( &package->subscriptions );
748         list_init( &package->appids );
749         list_init( &package->classes );
750         list_init( &package->mimes );
751         list_init( &package->extensions );
752         list_init( &package->progids );
753         list_init( &package->RunningActions );
754         list_init( &package->sourcelist_info );
755         list_init( &package->sourcelist_media );
756
757         package->patch = NULL;
758         package->ActionFormat = NULL;
759         package->LastAction = NULL;
760         package->dialog = NULL;
761         package->next_dialog = NULL;
762         package->scheduled_action_running = FALSE;
763         package->commit_action_running = FALSE;
764         package->rollback_action_running = FALSE;
765     }
766
767     return package;
768 }
769
770 static UINT msi_load_admin_properties(MSIPACKAGE *package)
771 {
772     BYTE *data;
773     UINT r, sz;
774
775     static const WCHAR stmname[] = {'A','d','m','i','n','P','r','o','p','e','r','t','i','e','s',0};
776
777     r = read_stream_data(package->db->storage, stmname, FALSE, &data, &sz);
778     if (r != ERROR_SUCCESS)
779         return r;
780
781     r = msi_parse_command_line(package, (WCHAR *)data, TRUE);
782
783     msi_free(data);
784     return r;
785 }
786
787 MSIPACKAGE *MSI_CreatePackage( MSIDATABASE *db, LPCWSTR base_url )
788 {
789     static const WCHAR szLevel[] = { 'U','I','L','e','v','e','l',0 };
790     static const WCHAR szpi[] = {'%','i',0};
791     static const WCHAR szProductCode[] = {
792         'P','r','o','d','u','c','t','C','o','d','e',0};
793     MSIPACKAGE *package;
794     WCHAR uilevel[10];
795     UINT r;
796
797     TRACE("%p\n", db);
798
799     package = msi_alloc_package();
800     if (package)
801     {
802         msiobj_addref( &db->hdr );
803         package->db = db;
804
805         package->WordCount = 0;
806         package->PackagePath = strdupW( db->path );
807         package->BaseURL = strdupW( base_url );
808
809         create_temp_property_table( package );
810         msi_clone_properties( package );
811
812         package->ProductCode = msi_dup_property( package, szProductCode );
813         set_installed_prop( package );
814         set_installer_properties( package );
815
816         sprintfW(uilevel,szpi,gUILevel);
817         MSI_SetPropertyW(package, szLevel, uilevel);
818
819         r = msi_load_summary_properties( package );
820         if (r != ERROR_SUCCESS)
821         {
822             msiobj_release( &package->hdr );
823             return NULL;
824         }
825
826         if (package->WordCount & msidbSumInfoSourceTypeAdminImage)
827             msi_load_admin_properties( package );
828     }
829
830     return package;
831 }
832
833 /*
834  * copy_package_to_temp   [internal]
835  *
836  * copy the msi file to a temp file to prevent locking a CD
837  * with a multi disc install 
838  *
839  * FIXME: I think this is wrong, and instead of copying the package,
840  *        we should read all the tables to memory, then open the
841  *        database to read binary streams on demand.
842  */ 
843 static LPCWSTR copy_package_to_temp( LPCWSTR szPackage, LPWSTR filename )
844 {
845     WCHAR path[MAX_PATH];
846     static const WCHAR szMSI[] = {'m','s','i',0};
847
848     GetTempPathW( MAX_PATH, path );
849     GetTempFileNameW( path, szMSI, 0, filename );
850
851     if( !CopyFileW( szPackage, filename, FALSE ) )
852     {
853         DeleteFileW( filename );
854         ERR("failed to copy package %s\n", debugstr_w(szPackage) );
855         return szPackage;
856     }
857
858     TRACE("Opening relocated package %s\n", debugstr_w( filename ));
859     return filename;
860 }
861
862 LPCWSTR msi_download_file( LPCWSTR szUrl, LPWSTR filename )
863 {
864     LPINTERNET_CACHE_ENTRY_INFOW cache_entry;
865     DWORD size = 0;
866     HRESULT hr;
867
868     /* call will always fail, becase size is 0,
869      * but will return ERROR_FILE_NOT_FOUND first
870      * if the file doesn't exist
871      */
872     GetUrlCacheEntryInfoW( szUrl, NULL, &size );
873     if ( GetLastError() != ERROR_FILE_NOT_FOUND )
874     {
875         cache_entry = HeapAlloc( GetProcessHeap(), 0, size );
876         if ( !GetUrlCacheEntryInfoW( szUrl, cache_entry, &size ) )
877         {
878             HeapFree( GetProcessHeap(), 0, cache_entry );
879             return szUrl;
880         }
881
882         lstrcpyW( filename, cache_entry->lpszLocalFileName );
883         HeapFree( GetProcessHeap(), 0, cache_entry );
884         return filename;
885     }
886
887     hr = URLDownloadToCacheFileW( NULL, szUrl, filename, MAX_PATH, 0, NULL );
888     if ( FAILED(hr) )
889         return szUrl;
890
891     return filename;
892 }
893
894 UINT MSI_OpenPackageW(LPCWSTR szPackage, MSIPACKAGE **pPackage)
895 {
896     static const WCHAR OriginalDatabase[] =
897         {'O','r','i','g','i','n','a','l','D','a','t','a','b','a','s','e',0};
898     static const WCHAR Database[] = {'D','A','T','A','B','A','S','E',0};
899     MSIDATABASE *db = NULL;
900     MSIPACKAGE *package;
901     MSIHANDLE handle;
902     LPWSTR ptr, base_url = NULL;
903     UINT r;
904     WCHAR temppath[MAX_PATH];
905     LPCWSTR file = szPackage;
906
907     TRACE("%s %p\n", debugstr_w(szPackage), pPackage);
908
909     if( szPackage[0] == '#' )
910     {
911         handle = atoiW(&szPackage[1]);
912         db = msihandle2msiinfo( handle, MSIHANDLETYPE_DATABASE );
913         if( !db )
914         {
915             IWineMsiRemoteDatabase *remote_database;
916
917             remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( handle );
918             if ( !remote_database )
919                 return ERROR_INVALID_HANDLE;
920
921             IWineMsiRemoteDatabase_Release( remote_database );
922             WARN("MsiOpenPackage not allowed during a custom action!\n");
923
924             return ERROR_FUNCTION_FAILED;
925         }
926     }
927     else
928     {
929         if ( UrlIsW( szPackage, URLIS_URL ) )
930         {
931             file = msi_download_file( szPackage, temppath );
932
933             base_url = strdupW( szPackage );
934             if ( !base_url )
935                 return ERROR_OUTOFMEMORY;
936
937             ptr = strrchrW( base_url, '/' );
938             if (ptr) *(ptr + 1) = '\0';
939         }
940         else
941             file = copy_package_to_temp( szPackage, temppath );
942
943         r = MSI_OpenDatabaseW( file, MSIDBOPEN_READONLY, &db );
944         if( r != ERROR_SUCCESS )
945         {
946             if (file != szPackage)
947                 DeleteFileW( file );
948
949             if (GetFileAttributesW(szPackage) == INVALID_FILE_ATTRIBUTES)
950                 return ERROR_FILE_NOT_FOUND;
951
952             return r;
953         }
954     }
955
956     package = MSI_CreatePackage( db, base_url );
957     msi_free( base_url );
958     msiobj_release( &db->hdr );
959     if( !package )
960     {
961         if (file != szPackage)
962             DeleteFileW( file );
963
964         return ERROR_INSTALL_PACKAGE_INVALID;
965     }
966
967     if( file != szPackage )
968         track_tempfile( package, file );
969
970     MSI_SetPropertyW( package, Database, db->path );
971
972     if( UrlIsW( szPackage, URLIS_URL ) )
973         MSI_SetPropertyW( package, OriginalDatabase, szPackage );
974     else if( szPackage[0] == '#' )
975         MSI_SetPropertyW( package, OriginalDatabase, db->path );
976     else
977     {
978         WCHAR fullpath[MAX_PATH];
979
980         GetFullPathNameW( szPackage, MAX_PATH, fullpath, NULL );
981         MSI_SetPropertyW( package, OriginalDatabase, fullpath );
982     }
983
984     *pPackage = package;
985
986     return ERROR_SUCCESS;
987 }
988
989 UINT WINAPI MsiOpenPackageExW(LPCWSTR szPackage, DWORD dwOptions, MSIHANDLE *phPackage)
990 {
991     MSIPACKAGE *package = NULL;
992     UINT ret;
993
994     TRACE("%s %08x %p\n", debugstr_w(szPackage), dwOptions, phPackage );
995
996     if( !szPackage || !phPackage )
997         return ERROR_INVALID_PARAMETER;
998
999     if ( !*szPackage )
1000     {
1001         FIXME("Should create an empty database and package\n");
1002         return ERROR_FUNCTION_FAILED;
1003     }
1004
1005     if( dwOptions )
1006         FIXME("dwOptions %08x not supported\n", dwOptions);
1007
1008     ret = MSI_OpenPackageW( szPackage, &package );
1009     if( ret == ERROR_SUCCESS )
1010     {
1011         *phPackage = alloc_msihandle( &package->hdr );
1012         if (! *phPackage)
1013             ret = ERROR_NOT_ENOUGH_MEMORY;
1014         msiobj_release( &package->hdr );
1015     }
1016
1017     return ret;
1018 }
1019
1020 UINT WINAPI MsiOpenPackageW(LPCWSTR szPackage, MSIHANDLE *phPackage)
1021 {
1022     return MsiOpenPackageExW( szPackage, 0, phPackage );
1023 }
1024
1025 UINT WINAPI MsiOpenPackageExA(LPCSTR szPackage, DWORD dwOptions, MSIHANDLE *phPackage)
1026 {
1027     LPWSTR szwPack = NULL;
1028     UINT ret;
1029
1030     if( szPackage )
1031     {
1032         szwPack = strdupAtoW( szPackage );
1033         if( !szwPack )
1034             return ERROR_OUTOFMEMORY;
1035     }
1036
1037     ret = MsiOpenPackageExW( szwPack, dwOptions, phPackage );
1038
1039     msi_free( szwPack );
1040
1041     return ret;
1042 }
1043
1044 UINT WINAPI MsiOpenPackageA(LPCSTR szPackage, MSIHANDLE *phPackage)
1045 {
1046     return MsiOpenPackageExA( szPackage, 0, phPackage );
1047 }
1048
1049 MSIHANDLE WINAPI MsiGetActiveDatabase(MSIHANDLE hInstall)
1050 {
1051     MSIPACKAGE *package;
1052     MSIHANDLE handle = 0;
1053     IWineMsiRemotePackage *remote_package;
1054
1055     TRACE("(%d)\n",hInstall);
1056
1057     package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE);
1058     if( package)
1059     {
1060         handle = alloc_msihandle( &package->db->hdr );
1061         msiobj_release( &package->hdr );
1062     }
1063     else if ((remote_package = (IWineMsiRemotePackage *)msi_get_remote( hInstall )))
1064     {
1065         IWineMsiRemotePackage_GetActiveDatabase(remote_package, &handle);
1066         IWineMsiRemotePackage_Release(remote_package);
1067     }
1068
1069     return handle;
1070 }
1071
1072 INT MSI_ProcessMessage( MSIPACKAGE *package, INSTALLMESSAGE eMessageType,
1073                                MSIRECORD *record)
1074 {
1075     static const WCHAR szActionData[] =
1076         {'A','c','t','i','o','n','D','a','t','a',0};
1077     static const WCHAR szSetProgress[] =
1078         {'S','e','t','P','r','o','g','r','e','s','s',0};
1079     static const WCHAR szActionText[] =
1080         {'A','c','t','i','o','n','T','e','x','t',0};
1081     DWORD log_type = 0;
1082     LPWSTR message;
1083     DWORD sz;
1084     DWORD total_size = 0;
1085     INT i;
1086     INT rc;
1087     char *msg;
1088     int len;
1089
1090     TRACE("%x\n", eMessageType);
1091     rc = 0;
1092
1093     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_ERROR)
1094         log_type |= INSTALLLOGMODE_ERROR;
1095     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_WARNING)
1096         log_type |= INSTALLLOGMODE_WARNING;
1097     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_USER)
1098         log_type |= INSTALLLOGMODE_USER;
1099     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_INFO)
1100         log_type |= INSTALLLOGMODE_INFO;
1101     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_COMMONDATA)
1102         log_type |= INSTALLLOGMODE_COMMONDATA;
1103     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_ACTIONSTART)
1104         log_type |= INSTALLLOGMODE_ACTIONSTART;
1105     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_ACTIONDATA)
1106         log_type |= INSTALLLOGMODE_ACTIONDATA;
1107     /* just a guess */
1108     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_PROGRESS)
1109         log_type |= 0x800;
1110
1111     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_ACTIONSTART)
1112     {
1113         static const WCHAR template_s[]=
1114             {'A','c','t','i','o','n',' ','%','s',':',' ','%','s','.',' ',0};
1115         static const WCHAR format[] = 
1116             {'H','H','\'',':','\'','m','m','\'',':','\'','s','s',0};
1117         WCHAR timet[0x100];
1118         LPCWSTR action_text, action;
1119         LPWSTR deformatted = NULL;
1120
1121         GetTimeFormatW(LOCALE_USER_DEFAULT, 0, NULL, format, timet, 0x100);
1122
1123         action = MSI_RecordGetString(record, 1);
1124         action_text = MSI_RecordGetString(record, 2);
1125
1126         if (!action || !action_text)
1127             return IDOK;
1128
1129         deformat_string(package, action_text, &deformatted);
1130
1131         len = strlenW(timet) + strlenW(action) + strlenW(template_s);
1132         if (deformatted)
1133             len += strlenW(deformatted);
1134         message = msi_alloc(len*sizeof(WCHAR));
1135         sprintfW(message, template_s, timet, action);
1136         if (deformatted)
1137             strcatW(message, deformatted);
1138         msi_free(deformatted);
1139     }
1140     else
1141     {
1142         INT msg_field=1;
1143         message = msi_alloc(1*sizeof (WCHAR));
1144         message[0]=0;
1145         msg_field = MSI_RecordGetFieldCount(record);
1146         for (i = 1; i <= msg_field; i++)
1147         {
1148             LPWSTR tmp;
1149             WCHAR number[3];
1150             static const WCHAR format[] = { '%','i',':',' ',0};
1151             static const WCHAR space[] = { ' ',0};
1152             sz = 0;
1153             MSI_RecordGetStringW(record,i,NULL,&sz);
1154             sz+=4;
1155             total_size+=sz*sizeof(WCHAR);
1156             tmp = msi_alloc(sz*sizeof(WCHAR));
1157             message = msi_realloc(message,total_size*sizeof (WCHAR));
1158
1159             MSI_RecordGetStringW(record,i,tmp,&sz);
1160
1161             if (msg_field > 1)
1162             {
1163                 sprintfW(number,format,i);
1164                 strcatW(message,number);
1165             }
1166             strcatW(message,tmp);
1167             if (msg_field > 1)
1168                 strcatW(message,space);
1169
1170             msi_free(tmp);
1171         }
1172     }
1173
1174     TRACE("(%p %x %x %s)\n", gUIHandlerA, gUIFilter, log_type,
1175                              debugstr_w(message));
1176
1177     /* convert it to ASCII */
1178     len = WideCharToMultiByte( CP_ACP, 0, message, -1,
1179                                NULL, 0, NULL, NULL );
1180     msg = msi_alloc( len );
1181     WideCharToMultiByte( CP_ACP, 0, message, -1,
1182                          msg, len, NULL, NULL );
1183
1184     if (gUIHandlerA && (gUIFilter & log_type))
1185     {
1186         rc = gUIHandlerA(gUIContext,eMessageType,msg);
1187     }
1188
1189     if ((!rc) && (gszLogFile[0]) && !((eMessageType & 0xff000000) ==
1190                                       INSTALLMESSAGE_PROGRESS))
1191     {
1192         DWORD write;
1193         HANDLE log_file = CreateFileW(gszLogFile,GENERIC_WRITE, 0, NULL,
1194                                   OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1195
1196         if (log_file != INVALID_HANDLE_VALUE)
1197         {
1198             SetFilePointer(log_file,0, NULL, FILE_END);
1199             WriteFile(log_file,msg,strlen(msg),&write,NULL);
1200             WriteFile(log_file,"\n",1,&write,NULL);
1201             CloseHandle(log_file);
1202         }
1203     }
1204     msi_free( msg );
1205
1206     msi_free( message);
1207
1208     switch (eMessageType & 0xff000000)
1209     {
1210     case INSTALLMESSAGE_ACTIONDATA:
1211         /* FIXME: format record here instead of in ui_actiondata to get the
1212          * correct action data for external scripts */
1213         ControlEvent_FireSubscribedEvent(package, szActionData, record);
1214         break;
1215     case INSTALLMESSAGE_ACTIONSTART:
1216     {
1217         MSIRECORD *uirow;
1218         LPWSTR deformated;
1219         LPCWSTR action_text = MSI_RecordGetString(record, 2);
1220
1221         deformat_string(package, action_text, &deformated);
1222         uirow = MSI_CreateRecord(1);
1223         MSI_RecordSetStringW(uirow, 1, deformated);
1224         TRACE("INSTALLMESSAGE_ACTIONSTART: %s\n", debugstr_w(deformated));
1225         msi_free(deformated);
1226
1227         ControlEvent_FireSubscribedEvent(package, szActionText, uirow);
1228
1229         msiobj_release(&uirow->hdr);
1230         break;
1231     }
1232     case INSTALLMESSAGE_PROGRESS:
1233         ControlEvent_FireSubscribedEvent(package, szSetProgress, record);
1234         break;
1235     }
1236
1237     return ERROR_SUCCESS;
1238 }
1239
1240 INT WINAPI MsiProcessMessage( MSIHANDLE hInstall, INSTALLMESSAGE eMessageType,
1241                               MSIHANDLE hRecord)
1242 {
1243     UINT ret = ERROR_INVALID_HANDLE;
1244     MSIPACKAGE *package = NULL;
1245     MSIRECORD *record = NULL;
1246
1247     package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE );
1248     if( !package )
1249     {
1250         HRESULT hr;
1251         IWineMsiRemotePackage *remote_package;
1252
1253         remote_package = (IWineMsiRemotePackage *)msi_get_remote( hInstall );
1254         if (!remote_package)
1255             return ERROR_INVALID_HANDLE;
1256
1257         hr = IWineMsiRemotePackage_ProcessMessage( remote_package, eMessageType, hRecord );
1258
1259         IWineMsiRemotePackage_Release( remote_package );
1260
1261         if (FAILED(hr))
1262         {
1263             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
1264                 return HRESULT_CODE(hr);
1265
1266             return ERROR_FUNCTION_FAILED;
1267         }
1268
1269         return ERROR_SUCCESS;
1270     }
1271
1272     record = msihandle2msiinfo( hRecord, MSIHANDLETYPE_RECORD );
1273     if( !record )
1274         goto out;
1275
1276     ret = MSI_ProcessMessage( package, eMessageType, record );
1277
1278 out:
1279     msiobj_release( &package->hdr );
1280     if( record )
1281         msiobj_release( &record->hdr );
1282
1283     return ret;
1284 }
1285
1286 /* property code */
1287
1288 UINT WINAPI MsiSetPropertyA( MSIHANDLE hInstall, LPCSTR szName, LPCSTR szValue )
1289 {
1290     LPWSTR szwName = NULL, szwValue = NULL;
1291     UINT r = ERROR_OUTOFMEMORY;
1292
1293     szwName = strdupAtoW( szName );
1294     if( szName && !szwName )
1295         goto end;
1296
1297     szwValue = strdupAtoW( szValue );
1298     if( szValue && !szwValue )
1299         goto end;
1300
1301     r = MsiSetPropertyW( hInstall, szwName, szwValue);
1302
1303 end:
1304     msi_free( szwName );
1305     msi_free( szwValue );
1306
1307     return r;
1308 }
1309
1310 UINT MSI_SetPropertyW( MSIPACKAGE *package, LPCWSTR szName, LPCWSTR szValue)
1311 {
1312     MSIQUERY *view;
1313     MSIRECORD *row = NULL;
1314     UINT rc;
1315     DWORD sz = 0;
1316     WCHAR Query[1024];
1317
1318     static const WCHAR Insert[] = {
1319         'I','N','S','E','R','T',' ','i','n','t','o',' ',
1320         '`','_','P','r','o','p','e','r','t','y','`',' ','(',
1321         '`','_','P','r','o','p','e','r','t','y','`',',',
1322         '`','V','a','l','u','e','`',')',' ','V','A','L','U','E','S'
1323         ,' ','(','?',',','?',')',0};
1324     static const WCHAR Update[] = {
1325         'U','P','D','A','T','E',' ','`','_','P','r','o','p','e','r','t','y','`',
1326         ' ','s','e','t',' ','`','V','a','l','u','e','`',' ','=',' ','?',' ',
1327         'w','h','e','r','e',' ','`','_','P','r','o','p','e','r','t','y','`',
1328         ' ','=',' ','\'','%','s','\'',0};
1329     static const WCHAR Delete[] = {
1330         'D','E','L','E','T','E',' ','F','R','O','M',' ',
1331         '`','_','P','r','o','p','e','r','t','y','`',' ','W','H','E','R','E',' ',
1332         '`','_','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',0};
1333
1334     TRACE("%p %s %s\n", package, debugstr_w(szName), debugstr_w(szValue));
1335
1336     if (!szName)
1337         return ERROR_INVALID_PARAMETER;
1338
1339     /* this one is weird... */
1340     if (!szName[0])
1341         return szValue ? ERROR_FUNCTION_FAILED : ERROR_SUCCESS;
1342
1343     rc = MSI_GetPropertyW(package, szName, 0, &sz);
1344     if (!szValue || !*szValue)
1345     {
1346         sprintfW(Query, Delete, szName);
1347     }
1348     else if (rc == ERROR_MORE_DATA || rc == ERROR_SUCCESS)
1349     {
1350         sprintfW(Query, Update, szName);
1351
1352         row = MSI_CreateRecord(1);
1353         MSI_RecordSetStringW(row, 1, szValue);
1354     }
1355     else
1356     {
1357         strcpyW(Query, Insert);
1358
1359         row = MSI_CreateRecord(2);
1360         MSI_RecordSetStringW(row, 1, szName);
1361         MSI_RecordSetStringW(row, 2, szValue);
1362     }
1363
1364     rc = MSI_DatabaseOpenViewW(package->db, Query, &view);
1365     if (rc == ERROR_SUCCESS)
1366     {
1367         rc = MSI_ViewExecute(view, row);
1368         MSI_ViewClose(view);
1369         msiobj_release(&view->hdr);
1370     }
1371
1372     if (row)
1373       msiobj_release(&row->hdr);
1374
1375     if (rc == ERROR_SUCCESS && (!lstrcmpW(szName, cszSourceDir)))
1376         msi_reset_folders(package, TRUE);
1377
1378     return rc;
1379 }
1380
1381 UINT WINAPI MsiSetPropertyW( MSIHANDLE hInstall, LPCWSTR szName, LPCWSTR szValue)
1382 {
1383     MSIPACKAGE *package;
1384     UINT ret;
1385
1386     package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE);
1387     if( !package )
1388     {
1389         HRESULT hr;
1390         BSTR name = NULL, value = NULL;
1391         IWineMsiRemotePackage *remote_package;
1392
1393         remote_package = (IWineMsiRemotePackage *)msi_get_remote( hInstall );
1394         if (!remote_package)
1395             return ERROR_INVALID_HANDLE;
1396
1397         name = SysAllocString( szName );
1398         value = SysAllocString( szValue );
1399         if ((!name && szName) || (!value && szValue))
1400         {
1401             SysFreeString( name );
1402             SysFreeString( value );
1403             IWineMsiRemotePackage_Release( remote_package );
1404             return ERROR_OUTOFMEMORY;
1405         }
1406
1407         hr = IWineMsiRemotePackage_SetProperty( remote_package, name, value );
1408
1409         SysFreeString( name );
1410         SysFreeString( value );
1411         IWineMsiRemotePackage_Release( remote_package );
1412
1413         if (FAILED(hr))
1414         {
1415             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
1416                 return HRESULT_CODE(hr);
1417
1418             return ERROR_FUNCTION_FAILED;
1419         }
1420
1421         return ERROR_SUCCESS;
1422     }
1423
1424     ret = MSI_SetPropertyW( package, szName, szValue);
1425     msiobj_release( &package->hdr );
1426     return ret;
1427 }
1428
1429 static MSIRECORD *MSI_GetPropertyRow( MSIPACKAGE *package, LPCWSTR name )
1430 {
1431     MSIQUERY *view;
1432     MSIRECORD *rec, *row = NULL;
1433     UINT r;
1434
1435     static const WCHAR query[]= {
1436         'S','E','L','E','C','T',' ','`','V','a','l','u','e','`',' ',
1437         'F','R','O','M',' ' ,'`','_','P','r','o','p','e','r','t','y','`',
1438         ' ','W','H','E','R','E',' ' ,'`','_','P','r','o','p','e','r','t','y','`',
1439         '=','?',0};
1440
1441     if (!name || !*name)
1442         return NULL;
1443
1444     rec = MSI_CreateRecord(1);
1445     if (!rec)
1446         return NULL;
1447
1448     MSI_RecordSetStringW(rec, 1, name);
1449
1450     r = MSI_DatabaseOpenViewW(package->db, query, &view);
1451     if (r == ERROR_SUCCESS)
1452     {
1453         MSI_ViewExecute(view, rec);
1454         MSI_ViewFetch(view, &row);
1455         MSI_ViewClose(view);
1456         msiobj_release(&view->hdr);
1457     }
1458
1459     msiobj_release(&rec->hdr);
1460     return row;
1461 }
1462
1463 /* internal function, not compatible with MsiGetPropertyW */
1464 UINT MSI_GetPropertyW( MSIPACKAGE *package, LPCWSTR szName, 
1465                        LPWSTR szValueBuf, LPDWORD pchValueBuf )
1466 {
1467     MSIRECORD *row;
1468     UINT rc = ERROR_FUNCTION_FAILED;
1469
1470     row = MSI_GetPropertyRow( package, szName );
1471
1472     if (*pchValueBuf > 0)
1473         szValueBuf[0] = 0;
1474
1475     if (row)
1476     {
1477         rc = MSI_RecordGetStringW(row, 1, szValueBuf, pchValueBuf);
1478         msiobj_release(&row->hdr);
1479     }
1480
1481     if (rc == ERROR_SUCCESS)
1482         TRACE("returning %s for property %s\n", debugstr_w(szValueBuf),
1483             debugstr_w(szName));
1484     else if (rc == ERROR_MORE_DATA)
1485         TRACE("need %d sized buffer for %s\n", *pchValueBuf,
1486             debugstr_w(szName));
1487     else
1488     {
1489         *pchValueBuf = 0;
1490         TRACE("property %s not found\n", debugstr_w(szName));
1491     }
1492
1493     return rc;
1494 }
1495
1496 LPWSTR msi_dup_property(MSIPACKAGE *package, LPCWSTR prop)
1497 {
1498     DWORD sz = 0;
1499     LPWSTR str;
1500     UINT r;
1501
1502     r = MSI_GetPropertyW(package, prop, NULL, &sz);
1503     if (r != ERROR_SUCCESS && r != ERROR_MORE_DATA)
1504         return NULL;
1505
1506     sz++;
1507     str = msi_alloc(sz * sizeof(WCHAR));
1508     r = MSI_GetPropertyW(package, prop, str, &sz);
1509     if (r != ERROR_SUCCESS)
1510     {
1511         msi_free(str);
1512         str = NULL;
1513     }
1514
1515     return str;
1516 }
1517
1518 int msi_get_property_int(MSIPACKAGE *package, LPCWSTR prop, int def)
1519 {
1520     LPWSTR str = msi_dup_property(package, prop);
1521     int val = str ? atoiW(str) : def;
1522     msi_free(str);
1523     return val;
1524 }
1525
1526 static UINT MSI_GetProperty( MSIHANDLE handle, LPCWSTR name,
1527                              awstring *szValueBuf, LPDWORD pchValueBuf )
1528 {
1529     static const WCHAR empty[] = {0};
1530     MSIPACKAGE *package;
1531     MSIRECORD *row = NULL;
1532     UINT r = ERROR_FUNCTION_FAILED;
1533     LPCWSTR val = NULL;
1534
1535     TRACE("%u %s %p %p\n", handle, debugstr_w(name),
1536           szValueBuf->str.w, pchValueBuf );
1537
1538     if (!name)
1539         return ERROR_INVALID_PARAMETER;
1540
1541     package = msihandle2msiinfo( handle, MSIHANDLETYPE_PACKAGE );
1542     if (!package)
1543     {
1544         HRESULT hr;
1545         IWineMsiRemotePackage *remote_package;
1546         LPWSTR value = NULL;
1547         BSTR bname;
1548         DWORD len;
1549
1550         remote_package = (IWineMsiRemotePackage *)msi_get_remote( handle );
1551         if (!remote_package)
1552             return ERROR_INVALID_HANDLE;
1553
1554         bname = SysAllocString( name );
1555         if (!bname)
1556         {
1557             IWineMsiRemotePackage_Release( remote_package );
1558             return ERROR_OUTOFMEMORY;
1559         }
1560
1561         len = 0;
1562         hr = IWineMsiRemotePackage_GetProperty( remote_package, bname, NULL, &len );
1563         if (FAILED(hr))
1564             goto done;
1565
1566         len++;
1567         value = msi_alloc(len * sizeof(WCHAR));
1568         if (!value)
1569         {
1570             r = ERROR_OUTOFMEMORY;
1571             goto done;
1572         }
1573
1574         hr = IWineMsiRemotePackage_GetProperty( remote_package, bname, (BSTR *)value, &len );
1575         if (FAILED(hr))
1576             goto done;
1577
1578         r = msi_strcpy_to_awstring( value, szValueBuf, pchValueBuf );
1579
1580         /* Bug required by Adobe installers */
1581         if (!szValueBuf->unicode && !szValueBuf->str.a)
1582             *pchValueBuf *= sizeof(WCHAR);
1583
1584 done:
1585         IWineMsiRemotePackage_Release(remote_package);
1586         SysFreeString(bname);
1587         msi_free(value);
1588
1589         if (FAILED(hr))
1590         {
1591             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
1592                 return HRESULT_CODE(hr);
1593
1594             return ERROR_FUNCTION_FAILED;
1595         }
1596
1597         return r;
1598     }
1599
1600     row = MSI_GetPropertyRow( package, name );
1601     if (row)
1602         val = MSI_RecordGetString( row, 1 );
1603
1604     if (!val)
1605         val = empty;
1606
1607     r = msi_strcpy_to_awstring( val, szValueBuf, pchValueBuf );
1608
1609     if (row)
1610         msiobj_release( &row->hdr );
1611     msiobj_release( &package->hdr );
1612
1613     return r;
1614 }
1615
1616 UINT WINAPI MsiGetPropertyA( MSIHANDLE hInstall, LPCSTR szName,
1617                              LPSTR szValueBuf, LPDWORD pchValueBuf )
1618 {
1619     awstring val;
1620     LPWSTR name;
1621     UINT r;
1622
1623     val.unicode = FALSE;
1624     val.str.a = szValueBuf;
1625
1626     name = strdupAtoW( szName );
1627     if (szName && !name)
1628         return ERROR_OUTOFMEMORY;
1629
1630     r = MSI_GetProperty( hInstall, name, &val, pchValueBuf );
1631     msi_free( name );
1632     return r;
1633 }
1634
1635 UINT WINAPI MsiGetPropertyW( MSIHANDLE hInstall, LPCWSTR szName,
1636                              LPWSTR szValueBuf, LPDWORD pchValueBuf )
1637 {
1638     awstring val;
1639
1640     val.unicode = TRUE;
1641     val.str.w = szValueBuf;
1642
1643     return MSI_GetProperty( hInstall, szName, &val, pchValueBuf );
1644 }
1645
1646 typedef struct _msi_remote_package_impl {
1647     const IWineMsiRemotePackageVtbl *lpVtbl;
1648     MSIHANDLE package;
1649     LONG refs;
1650 } msi_remote_package_impl;
1651
1652 static inline msi_remote_package_impl* mrp_from_IWineMsiRemotePackage( IWineMsiRemotePackage* iface )
1653 {
1654     return (msi_remote_package_impl*) iface;
1655 }
1656
1657 static HRESULT WINAPI mrp_QueryInterface( IWineMsiRemotePackage *iface,
1658                 REFIID riid,LPVOID *ppobj)
1659 {
1660     if( IsEqualCLSID( riid, &IID_IUnknown ) ||
1661         IsEqualCLSID( riid, &IID_IWineMsiRemotePackage ) )
1662     {
1663         IUnknown_AddRef( iface );
1664         *ppobj = iface;
1665         return S_OK;
1666     }
1667
1668     return E_NOINTERFACE;
1669 }
1670
1671 static ULONG WINAPI mrp_AddRef( IWineMsiRemotePackage *iface )
1672 {
1673     msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
1674
1675     return InterlockedIncrement( &This->refs );
1676 }
1677
1678 static ULONG WINAPI mrp_Release( IWineMsiRemotePackage *iface )
1679 {
1680     msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
1681     ULONG r;
1682
1683     r = InterlockedDecrement( &This->refs );
1684     if (r == 0)
1685     {
1686         MsiCloseHandle( This->package );
1687         msi_free( This );
1688     }
1689     return r;
1690 }
1691
1692 static HRESULT WINAPI mrp_SetMsiHandle( IWineMsiRemotePackage *iface, MSIHANDLE handle )
1693 {
1694     msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
1695     This->package = handle;
1696     return S_OK;
1697 }
1698
1699 static HRESULT WINAPI mrp_GetActiveDatabase( IWineMsiRemotePackage *iface, MSIHANDLE *handle )
1700 {
1701     msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
1702     IWineMsiRemoteDatabase *rdb = NULL;
1703     HRESULT hr;
1704     MSIHANDLE hdb;
1705
1706     hr = create_msi_remote_database( NULL, (LPVOID *)&rdb );
1707     if (FAILED(hr) || !rdb)
1708     {
1709         ERR("Failed to create remote database\n");
1710         return hr;
1711     }
1712
1713     hdb = MsiGetActiveDatabase(This->package);
1714
1715     hr = IWineMsiRemoteDatabase_SetMsiHandle( rdb, hdb );
1716     if (FAILED(hr))
1717     {
1718         ERR("Failed to set the database handle\n");
1719         return hr;
1720     }
1721
1722     *handle = alloc_msi_remote_handle( (IUnknown *)rdb );
1723     return S_OK;
1724 }
1725
1726 static HRESULT WINAPI mrp_GetProperty( IWineMsiRemotePackage *iface, BSTR property, BSTR *value, DWORD *size )
1727 {
1728     msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
1729     UINT r;
1730
1731     r = MsiGetPropertyW(This->package, (LPWSTR)property, (LPWSTR)value, size);
1732     if (r != ERROR_SUCCESS)
1733         return HRESULT_FROM_WIN32(r);
1734
1735     return S_OK;
1736 }
1737
1738 static HRESULT WINAPI mrp_SetProperty( IWineMsiRemotePackage *iface, BSTR property, BSTR value )
1739 {
1740     msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
1741     UINT r = MsiSetPropertyW(This->package, property, value);
1742     return HRESULT_FROM_WIN32(r);
1743 }
1744
1745 static HRESULT WINAPI mrp_ProcessMessage( IWineMsiRemotePackage *iface, INSTALLMESSAGE message, MSIHANDLE record )
1746 {
1747     msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
1748     UINT r = MsiProcessMessage(This->package, message, record);
1749     return HRESULT_FROM_WIN32(r);
1750 }
1751
1752 static HRESULT WINAPI mrp_DoAction( IWineMsiRemotePackage *iface, BSTR action )
1753 {
1754     msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
1755     UINT r = MsiDoActionW(This->package, action);
1756     return HRESULT_FROM_WIN32(r);
1757 }
1758
1759 static HRESULT WINAPI mrp_Sequence( IWineMsiRemotePackage *iface, BSTR table, int sequence )
1760 {
1761     msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
1762     UINT r = MsiSequenceW(This->package, table, sequence);
1763     return HRESULT_FROM_WIN32(r);
1764 }
1765
1766 static HRESULT WINAPI mrp_GetTargetPath( IWineMsiRemotePackage *iface, BSTR folder, BSTR *value, DWORD *size )
1767 {
1768     msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
1769     UINT r = MsiGetTargetPathW(This->package, (LPWSTR)folder, (LPWSTR)value, size);
1770     return HRESULT_FROM_WIN32(r);
1771 }
1772
1773 static HRESULT WINAPI mrp_SetTargetPath( IWineMsiRemotePackage *iface, BSTR folder, BSTR value)
1774 {
1775     msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
1776     UINT r = MsiSetTargetPathW(This->package, folder, value);
1777     return HRESULT_FROM_WIN32(r);
1778 }
1779
1780 static HRESULT WINAPI mrp_GetSourcePath( IWineMsiRemotePackage *iface, BSTR folder, BSTR *value, DWORD *size )
1781 {
1782     msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
1783     UINT r = MsiGetSourcePathW(This->package, (LPWSTR)folder, (LPWSTR)value, size);
1784     return HRESULT_FROM_WIN32(r);
1785 }
1786
1787 static HRESULT WINAPI mrp_GetMode( IWineMsiRemotePackage *iface, MSIRUNMODE mode, BOOL *ret )
1788 {
1789     msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
1790     *ret = MsiGetMode(This->package, mode);
1791     return S_OK;
1792 }
1793
1794 static HRESULT WINAPI mrp_GetFeatureState( IWineMsiRemotePackage *iface, BSTR feature,
1795                                     INSTALLSTATE *installed, INSTALLSTATE *action )
1796 {
1797     msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
1798     UINT r = MsiGetFeatureStateW(This->package, feature, installed, action);
1799     return HRESULT_FROM_WIN32(r);
1800 }
1801
1802 static HRESULT WINAPI mrp_SetFeatureState( IWineMsiRemotePackage *iface, BSTR feature, INSTALLSTATE state )
1803 {
1804     msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
1805     UINT r = MsiSetFeatureStateW(This->package, feature, state);
1806     return HRESULT_FROM_WIN32(r);
1807 }
1808
1809 static HRESULT WINAPI mrp_GetComponentState( IWineMsiRemotePackage *iface, BSTR component,
1810                                       INSTALLSTATE *installed, INSTALLSTATE *action )
1811 {
1812     msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
1813     UINT r = MsiGetComponentStateW(This->package, component, installed, action);
1814     return HRESULT_FROM_WIN32(r);
1815 }
1816
1817 static HRESULT WINAPI mrp_SetComponentState( IWineMsiRemotePackage *iface, BSTR component, INSTALLSTATE state )
1818 {
1819     msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
1820     UINT r = MsiSetComponentStateW(This->package, component, state);
1821     return HRESULT_FROM_WIN32(r);
1822 }
1823
1824 static HRESULT WINAPI mrp_GetLanguage( IWineMsiRemotePackage *iface, LANGID *language )
1825 {
1826     msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
1827     *language = MsiGetLanguage(This->package);
1828     return S_OK;
1829 }
1830
1831 static HRESULT WINAPI mrp_SetInstallLevel( IWineMsiRemotePackage *iface, int level )
1832 {
1833     msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
1834     UINT r = MsiSetInstallLevel(This->package, level);
1835     return HRESULT_FROM_WIN32(r);
1836 }
1837
1838 static HRESULT WINAPI mrp_FormatRecord( IWineMsiRemotePackage *iface, MSIHANDLE record,
1839                                         BSTR *value)
1840 {
1841     DWORD size = 0;
1842     msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
1843     UINT r = MsiFormatRecordW(This->package, record, NULL, &size);
1844     if (r == ERROR_SUCCESS)
1845     {
1846         *value = SysAllocStringLen(NULL, size);
1847         if (!*value)
1848             return E_OUTOFMEMORY;
1849         size++;
1850         r = MsiFormatRecordW(This->package, record, *value, &size);
1851     }
1852     return HRESULT_FROM_WIN32(r);
1853 }
1854
1855 static HRESULT WINAPI mrp_EvaluateCondition( IWineMsiRemotePackage *iface, BSTR condition )
1856 {
1857     msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
1858     UINT r = MsiEvaluateConditionW(This->package, condition);
1859     return HRESULT_FROM_WIN32(r);
1860 }
1861
1862 static const IWineMsiRemotePackageVtbl msi_remote_package_vtbl =
1863 {
1864     mrp_QueryInterface,
1865     mrp_AddRef,
1866     mrp_Release,
1867     mrp_SetMsiHandle,
1868     mrp_GetActiveDatabase,
1869     mrp_GetProperty,
1870     mrp_SetProperty,
1871     mrp_ProcessMessage,
1872     mrp_DoAction,
1873     mrp_Sequence,
1874     mrp_GetTargetPath,
1875     mrp_SetTargetPath,
1876     mrp_GetSourcePath,
1877     mrp_GetMode,
1878     mrp_GetFeatureState,
1879     mrp_SetFeatureState,
1880     mrp_GetComponentState,
1881     mrp_SetComponentState,
1882     mrp_GetLanguage,
1883     mrp_SetInstallLevel,
1884     mrp_FormatRecord,
1885     mrp_EvaluateCondition,
1886 };
1887
1888 HRESULT create_msi_remote_package( IUnknown *pOuter, LPVOID *ppObj )
1889 {
1890     msi_remote_package_impl* This;
1891
1892     This = msi_alloc( sizeof *This );
1893     if (!This)
1894         return E_OUTOFMEMORY;
1895
1896     This->lpVtbl = &msi_remote_package_vtbl;
1897     This->package = 0;
1898     This->refs = 1;
1899
1900     *ppObj = This;
1901
1902     return S_OK;
1903 }
1904
1905 UINT msi_package_add_info(MSIPACKAGE *package, DWORD context, DWORD options,
1906                           LPCWSTR property, LPWSTR value)
1907 {
1908     MSISOURCELISTINFO *info;
1909
1910     info = msi_alloc(sizeof(MSISOURCELISTINFO));
1911     if (!info)
1912         return ERROR_OUTOFMEMORY;
1913
1914     info->context = context;
1915     info->options = options;
1916     info->property = property;
1917     info->value = strdupW(value);
1918     list_add_head(&package->sourcelist_info, &info->entry);
1919
1920     return ERROR_SUCCESS;
1921 }
1922
1923 UINT msi_package_add_media_disk(MSIPACKAGE *package, DWORD context, DWORD options,
1924                                 DWORD disk_id, LPWSTR volume_label, LPWSTR disk_prompt)
1925 {
1926     MSIMEDIADISK *disk;
1927
1928     disk = msi_alloc(sizeof(MSIMEDIADISK));
1929     if (!disk)
1930         return ERROR_OUTOFMEMORY;
1931
1932     disk->context = context;
1933     disk->options = options;
1934     disk->disk_id = disk_id;
1935     disk->volume_label = strdupW(volume_label);
1936     disk->disk_prompt = strdupW(disk_prompt);
1937     list_add_head(&package->sourcelist_media, &disk->entry);
1938
1939     return ERROR_SUCCESS;
1940 }