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