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