msi/tests: automation: Add missing VariantClear.
[wine/wine-kai.git] / dlls / msi / package.c
blob04633f2ea40af979c971632d33a6f5e762cdcd3c
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2004 Aric Stewart for CodeWeavers
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.
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.
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
21 #define NONAMELESSUNION
22 #define NONAMELESSSTRUCT
23 #define COBJMACROS
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"
47 #include "msipriv.h"
48 #include "msiserver.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(msi);
52 static void MSI_FreePackage( MSIOBJECTHDR *arg)
54 MSIPACKAGE *package= (MSIPACKAGE*) arg;
56 if( package->dialog )
57 msi_dialog_destroy( package->dialog );
59 msiobj_release( &package->db->hdr );
60 ACTION_free_package_structures(package);
63 static UINT create_temp_property_table(MSIPACKAGE *package)
65 MSIQUERY *view = NULL;
66 UINT rc;
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};
78 rc = MSI_DatabaseOpenViewW(package->db, CreateSql, &view);
79 if (rc != ERROR_SUCCESS)
80 return rc;
82 rc = MSI_ViewExecute(view, 0);
83 MSI_ViewClose(view);
84 msiobj_release(&view->hdr);
85 return rc;
88 UINT msi_clone_properties(MSIPACKAGE *package)
90 MSIQUERY *view = NULL;
91 UINT rc;
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};
103 /* clone the existing properties */
104 rc = MSI_DatabaseOpenViewW(package->db, Query, &view);
105 if (rc != ERROR_SUCCESS)
106 return rc;
108 rc = MSI_ViewExecute(view, 0);
109 if (rc != ERROR_SUCCESS)
111 MSI_ViewClose(view);
112 msiobj_release(&view->hdr);
113 return rc;
116 while (1)
118 MSIRECORD *row;
119 MSIQUERY *view2;
121 rc = MSI_ViewFetch(view, &row);
122 if (rc != ERROR_SUCCESS)
123 break;
125 rc = MSI_DatabaseOpenViewW(package->db, Insert, &view2);
126 if (rc!= ERROR_SUCCESS)
127 continue;
129 rc = MSI_ViewExecute(view2, row);
130 MSI_ViewClose(view2);
131 msiobj_release(&view2->hdr);
133 if (rc == ERROR_SUCCESS)
134 msiobj_release(&row->hdr);
137 MSI_ViewClose(view);
138 msiobj_release(&view->hdr);
140 return rc;
144 * set_installed_prop
146 * Sets the "Installed" property to indicate that
147 * the product is installed for the current user.
149 static UINT set_installed_prop( MSIPACKAGE *package )
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;
157 r = MSIREG_OpenUninstallKey( package->ProductCode, &hkey, FALSE );
158 if (r == ERROR_SUCCESS)
160 RegCloseKey( hkey );
161 MSI_SetPropertyW( package, szInstalled, val );
164 return r;
167 static UINT set_user_sid_prop( MSIPACKAGE *package )
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;
176 static const WCHAR user_sid[] = {'U','s','e','r','S','I','D',0};
178 size = 0;
179 GetUserNameW( NULL, &size );
181 user_name = msi_alloc( (size + 1) * sizeof(WCHAR) );
182 if (!user_name)
183 return ERROR_OUTOFMEMORY;
185 if (!GetUserNameW( user_name, &size ))
186 goto done;
188 size = 0;
189 dom_size = 0;
190 LookupAccountNameW( NULL, user_name, NULL, &size, NULL, &dom_size, &use );
192 psid = msi_alloc( size );
193 dom = msi_alloc( dom_size*sizeof (WCHAR) );
194 if (!psid || !dom)
196 r = ERROR_OUTOFMEMORY;
197 goto done;
200 if (!LookupAccountNameW( NULL, user_name, psid, &size, dom, &dom_size, &use ))
201 goto done;
203 if (!ConvertSidToStringSidW( psid, &sid_str ))
204 goto done;
206 r = MSI_SetPropertyW( package, user_sid, sid_str );
208 done:
209 LocalFree( sid_str );
210 msi_free( dom );
211 msi_free( psid );
212 msi_free( user_name );
214 return r;
217 static LPWSTR get_fusion_filename(MSIPACKAGE *package)
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];
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
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
240 res = RegOpenKeyExW(HKEY_LOCAL_MACHINE, sub, 0, KEY_ENUMERATE_SUB_KEYS, &netsetup);
241 if (res != ERROR_SUCCESS)
242 return NULL;
244 ver[0] = '\0';
245 size = MAX_PATH;
246 while (RegEnumKeyExW(netsetup, index, name, &size, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
248 index++;
249 if (lstrcmpW(ver, name) < 0)
250 lstrcpyW(ver, name);
253 RegCloseKey(netsetup);
255 if (!index)
256 return NULL;
258 GetWindowsDirectoryW(windir, MAX_PATH);
260 size = lstrlenW(windir) + lstrlenW(subdir) + lstrlenW(ver) +lstrlenW(fusion) + 3;
261 file = msi_alloc(size * sizeof(WCHAR));
262 if (!file)
263 return NULL;
265 lstrcpyW(file, windir);
266 lstrcatW(file, backslash);
267 lstrcatW(file, subdir);
268 lstrcatW(file, ver);
269 lstrcatW(file, backslash);
270 lstrcatW(file, fusion);
272 return file;
275 typedef struct tagLANGANDCODEPAGE
277 WORD wLanguage;
278 WORD wCodePage;
279 } LANGANDCODEPAGE;
281 static void set_msi_assembly_prop(MSIPACKAGE *package)
283 UINT val_len;
284 DWORD size, handle;
285 LPVOID version = NULL;
286 WCHAR buf[MAX_PATH];
287 LPWSTR fusion, verstr;
288 LANGANDCODEPAGE *translate;
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
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
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
303 fusion = get_fusion_filename(package);
304 if (!fusion)
305 return;
307 size = GetFileVersionInfoSizeW(fusion, &handle);
308 if (!size) return;
310 version = msi_alloc(size);
311 if (!version) return;
313 if (!GetFileVersionInfoW(fusion, handle, size, version))
314 goto done;
316 if (!VerQueryValueW(version, translation, (LPVOID *)&translate, &val_len))
317 goto done;
319 sprintfW(buf, verfmt, translate[0].wLanguage, translate[0].wCodePage);
321 if (!VerQueryValueW(version, buf, (LPVOID *)&verstr, &val_len))
322 goto done;
324 if (!val_len || !verstr)
325 goto done;
327 MSI_SetPropertyW(package, netasm, verstr);
329 done:
330 msi_free(fusion);
331 msi_free(version);
335 * There are a whole slew of these we need to set
338 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/msi/setup/properties.asp
340 static VOID set_installer_properties(MSIPACKAGE *package)
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;
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 };
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
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
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};
441 * Other things that probably should be set:
443 * SystemLanguageID ComputerName UserLanguageID LogonUser VirtualMemory
444 * ShellAdvSupport DefaultUIFont PackagecodeChanging
445 * ProductState CaptionHeight BorderTop BorderSide TextHeight
446 * RedirectedDllSupport
449 SHGetFolderPathW(NULL,CSIDL_PROGRAM_FILES_COMMON,NULL,0,pth);
450 strcatW(pth,cszbs);
451 MSI_SetPropertyW(package, CFF, pth);
453 SHGetFolderPathW(NULL,CSIDL_PROGRAM_FILES,NULL,0,pth);
454 strcatW(pth,cszbs);
455 MSI_SetPropertyW(package, PFF, pth);
457 SHGetFolderPathW(NULL,CSIDL_COMMON_APPDATA,NULL,0,pth);
458 strcatW(pth,cszbs);
459 MSI_SetPropertyW(package, CADF, pth);
461 SHGetFolderPathW(NULL,CSIDL_FAVORITES,NULL,0,pth);
462 strcatW(pth,cszbs);
463 MSI_SetPropertyW(package, FaF, pth);
465 SHGetFolderPathW(NULL,CSIDL_FONTS,NULL,0,pth);
466 strcatW(pth,cszbs);
467 MSI_SetPropertyW(package, FoF, pth);
469 SHGetFolderPathW(NULL,CSIDL_SENDTO,NULL,0,pth);
470 strcatW(pth,cszbs);
471 MSI_SetPropertyW(package, SendTF, pth);
473 SHGetFolderPathW(NULL,CSIDL_STARTMENU,NULL,0,pth);
474 strcatW(pth,cszbs);
475 MSI_SetPropertyW(package, SMF, pth);
477 SHGetFolderPathW(NULL,CSIDL_STARTUP,NULL,0,pth);
478 strcatW(pth,cszbs);
479 MSI_SetPropertyW(package, StF, pth);
481 SHGetFolderPathW(NULL,CSIDL_TEMPLATES,NULL,0,pth);
482 strcatW(pth,cszbs);
483 MSI_SetPropertyW(package, TemplF, pth);
485 SHGetFolderPathW(NULL,CSIDL_DESKTOP,NULL,0,pth);
486 strcatW(pth,cszbs);
487 MSI_SetPropertyW(package, DF, pth);
489 SHGetFolderPathW(NULL,CSIDL_PROGRAMS,NULL,0,pth);
490 strcatW(pth,cszbs);
491 MSI_SetPropertyW(package, PMF, pth);
493 SHGetFolderPathW(NULL,CSIDL_ADMINTOOLS,NULL,0,pth);
494 strcatW(pth,cszbs);
495 MSI_SetPropertyW(package, ATF, pth);
497 SHGetFolderPathW(NULL,CSIDL_APPDATA,NULL,0,pth);
498 strcatW(pth,cszbs);
499 MSI_SetPropertyW(package, ADF, pth);
501 SHGetFolderPathW(NULL,CSIDL_SYSTEM,NULL,0,pth);
502 strcatW(pth,cszbs);
503 MSI_SetPropertyW(package, SF, pth);
504 MSI_SetPropertyW(package, SF16, pth);
506 SHGetFolderPathW(NULL,CSIDL_LOCAL_APPDATA,NULL,0,pth);
507 strcatW(pth,cszbs);
508 MSI_SetPropertyW(package, LADF, pth);
510 SHGetFolderPathW(NULL,CSIDL_MYPICTURES,NULL,0,pth);
511 strcatW(pth,cszbs);
512 MSI_SetPropertyW(package, MPF, pth);
514 SHGetFolderPathW(NULL,CSIDL_PERSONAL,NULL,0,pth);
515 strcatW(pth,cszbs);
516 MSI_SetPropertyW(package, PF, pth);
518 SHGetFolderPathW(NULL,CSIDL_WINDOWS,NULL,0,pth);
519 strcatW(pth,cszbs);
520 MSI_SetPropertyW(package, WF, pth);
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);
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);
534 GetTempPathW(MAX_PATH,pth);
535 MSI_SetPropertyW(package, TF, pth);
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);
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)
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;
559 sprintfW(verstr,szFormat,OSVersion.dwBuildNumber);
560 MSI_SetPropertyW(package,szWinBuild,verstr);
561 /* just fudge this */
562 MSI_SetPropertyW(package,szSPL,szSix);
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 );
569 GetSystemInfo( &sys_info );
570 if (sys_info.u.s.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL)
572 sprintfW( bufstr, szScreenFormat, sys_info.wProcessorLevel );
573 MSI_SetPropertyW( package, szIntel, bufstr );
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);
586 /* USERNAME and COMPANYNAME */
587 res = RegOpenKeyW( HKEY_LOCAL_MACHINE, szCurrentVersion, &hkey );
588 if (res != ERROR_SUCCESS)
589 return;
591 check = msi_dup_property( package, szUSERNAME );
592 if (!check)
594 LPWSTR user = msi_reg_get_val_str( hkey, szRegisteredUser );
595 MSI_SetPropertyW( package, szUSERNAME, user );
596 msi_free( user );
599 msi_free( check );
601 check = msi_dup_property( package, szCOMPANYNAME );
602 if (!check)
604 LPWSTR company = msi_reg_get_val_str( hkey, szRegisteredOrg );
605 MSI_SetPropertyW( package, szCOMPANYNAME, company );
606 msi_free( company );
609 if ( set_user_sid_prop( package ) != ERROR_SUCCESS)
610 ERR("Failed to set the UserSID property\n");
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());
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());
628 set_msi_assembly_prop( package );
630 msi_free( check );
631 CloseHandle( hkey );
634 static UINT msi_load_summary_properties( MSIPACKAGE *package )
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};
645 if (!hdb) {
646 ERR("Unable to allocate handle\n");
647 return 0;
649 rc = MsiGetSummaryInformationW( hdb, NULL, 0, &suminfo );
650 MsiCloseHandle(hdb);
651 if (rc != ERROR_SUCCESS)
653 ERR("Unable to open Summary Information\n");
654 return rc;
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");
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)
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 );
681 else
682 WARN("Unable to query rev number, %d\n", rc);
684 MsiCloseHandle(suminfo);
685 return ERROR_SUCCESS;
688 static MSIPACKAGE *msi_alloc_package( void )
690 MSIPACKAGE *package;
692 package = alloc_msiobject( MSIHANDLETYPE_PACKAGE, sizeof (MSIPACKAGE),
693 MSI_FreePackage );
694 if( package )
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 );
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;
720 return package;
723 static UINT msi_load_admin_properties(MSIPACKAGE *package)
725 BYTE *data;
726 UINT r, sz;
728 static const WCHAR stmname[] = {'A','d','m','i','n','P','r','o','p','e','r','t','i','e','s',0};
730 r = read_stream_data(package->db->storage, stmname, FALSE, &data, &sz);
731 if (r != ERROR_SUCCESS)
732 return r;
734 r = msi_parse_command_line(package, (WCHAR *)data);
736 msi_free(data);
737 return r;
740 MSIPACKAGE *MSI_CreatePackage( MSIDATABASE *db, LPCWSTR base_url )
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];
749 TRACE("%p\n", db);
751 package = msi_alloc_package();
752 if (package)
754 msiobj_addref( &db->hdr );
755 package->db = db;
757 package->WordCount = 0;
758 package->PackagePath = strdupW( db->path );
759 package->BaseURL = strdupW( base_url );
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);
767 package->ProductCode = msi_dup_property( package, szProductCode );
768 set_installed_prop( package );
769 msi_load_summary_properties( package );
771 if (package->WordCount & MSIWORDCOUNT_ADMINISTRATIVE)
772 msi_load_admin_properties( package );
775 return package;
779 * copy_package_to_temp [internal]
781 * copy the msi file to a temp file to prevent locking a CD
782 * with a multi disc install
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.
788 static LPCWSTR copy_package_to_temp( LPCWSTR szPackage, LPWSTR filename )
790 WCHAR path[MAX_PATH];
791 static const WCHAR szMSI[] = {'m','s','i',0};
793 GetTempPathW( MAX_PATH, path );
794 GetTempFileNameW( path, szMSI, 0, filename );
796 if( !CopyFileW( szPackage, filename, FALSE ) )
798 DeleteFileW( filename );
799 ERR("failed to copy package %s\n", debugstr_w(szPackage) );
800 return szPackage;
803 TRACE("Opening relocated package %s\n", debugstr_w( filename ));
804 return filename;
807 LPCWSTR msi_download_file( LPCWSTR szUrl, LPWSTR filename )
809 LPINTERNET_CACHE_ENTRY_INFOW cache_entry;
810 DWORD size = 0;
811 HRESULT hr;
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
817 GetUrlCacheEntryInfoW( szUrl, NULL, &size );
818 if ( GetLastError() != ERROR_FILE_NOT_FOUND )
820 cache_entry = HeapAlloc( GetProcessHeap(), 0, size );
821 if ( !GetUrlCacheEntryInfoW( szUrl, cache_entry, &size ) )
823 HeapFree( GetProcessHeap(), 0, cache_entry );
824 return szUrl;
827 lstrcpyW( filename, cache_entry->lpszLocalFileName );
828 HeapFree( GetProcessHeap(), 0, cache_entry );
829 return filename;
832 hr = URLDownloadToCacheFileW( NULL, szUrl, filename, MAX_PATH, 0, NULL );
833 if ( FAILED(hr) )
834 return szUrl;
836 return filename;
839 UINT MSI_OpenPackageW(LPCWSTR szPackage, MSIPACKAGE **pPackage)
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;
852 TRACE("%s %p\n", debugstr_w(szPackage), pPackage);
854 if( szPackage[0] == '#' )
856 handle = atoiW(&szPackage[1]);
857 db = msihandle2msiinfo( handle, MSIHANDLETYPE_DATABASE );
858 if( !db )
860 IWineMsiRemoteDatabase *remote_database;
862 remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( handle );
863 if ( !remote_database )
864 return ERROR_INVALID_HANDLE;
866 IWineMsiRemoteDatabase_Release( remote_database );
867 WARN("MsiOpenPackage not allowed during a custom action!\n");
869 return ERROR_FUNCTION_FAILED;
872 else
874 if ( UrlIsW( szPackage, URLIS_URL ) )
876 file = msi_download_file( szPackage, temppath );
878 base_url = strdupW( szPackage );
879 if ( !base_url )
880 return ERROR_OUTOFMEMORY;
882 ptr = strrchrW( base_url, '/' );
883 if (ptr) *(ptr + 1) = '\0';
885 else
886 file = copy_package_to_temp( szPackage, temppath );
888 r = MSI_OpenDatabaseW( file, MSIDBOPEN_READONLY, &db );
889 if( r != ERROR_SUCCESS )
891 if (GetLastError() == ERROR_FILE_NOT_FOUND)
892 msi_ui_error( 4, MB_OK | MB_ICONWARNING );
893 if (file != szPackage)
894 DeleteFileW( file );
896 return r;
900 package = MSI_CreatePackage( db, base_url );
901 msi_free( base_url );
902 msiobj_release( &db->hdr );
903 if( !package )
905 if (file != szPackage)
906 DeleteFileW( file );
907 return ERROR_FUNCTION_FAILED;
910 if( file != szPackage )
911 track_tempfile( package, file );
913 if( szPackage[0] != '#' )
915 MSI_SetPropertyW( package, OriginalDatabase, szPackage );
916 MSI_SetPropertyW( package, Database, szPackage );
918 else
920 MSI_SetPropertyW( package, OriginalDatabase, db->path );
921 MSI_SetPropertyW( package, Database, db->path );
924 *pPackage = package;
926 return ERROR_SUCCESS;
929 UINT WINAPI MsiOpenPackageExW(LPCWSTR szPackage, DWORD dwOptions, MSIHANDLE *phPackage)
931 MSIPACKAGE *package = NULL;
932 UINT ret;
934 TRACE("%s %08x %p\n", debugstr_w(szPackage), dwOptions, phPackage );
936 if( szPackage == NULL )
937 return ERROR_INVALID_PARAMETER;
939 if( dwOptions )
940 FIXME("dwOptions %08x not supported\n", dwOptions);
942 ret = MSI_OpenPackageW( szPackage, &package );
943 if( ret == ERROR_SUCCESS )
945 *phPackage = alloc_msihandle( &package->hdr );
946 if (! *phPackage)
947 ret = ERROR_NOT_ENOUGH_MEMORY;
948 msiobj_release( &package->hdr );
951 return ret;
954 UINT WINAPI MsiOpenPackageW(LPCWSTR szPackage, MSIHANDLE *phPackage)
956 return MsiOpenPackageExW( szPackage, 0, phPackage );
959 UINT WINAPI MsiOpenPackageExA(LPCSTR szPackage, DWORD dwOptions, MSIHANDLE *phPackage)
961 LPWSTR szwPack = NULL;
962 UINT ret;
964 if( szPackage )
966 szwPack = strdupAtoW( szPackage );
967 if( !szwPack )
968 return ERROR_OUTOFMEMORY;
971 ret = MsiOpenPackageExW( szwPack, dwOptions, phPackage );
973 msi_free( szwPack );
975 return ret;
978 UINT WINAPI MsiOpenPackageA(LPCSTR szPackage, MSIHANDLE *phPackage)
980 return MsiOpenPackageExA( szPackage, 0, phPackage );
983 MSIHANDLE WINAPI MsiGetActiveDatabase(MSIHANDLE hInstall)
985 MSIPACKAGE *package;
986 MSIHANDLE handle = 0;
987 IWineMsiRemotePackage *remote_package;
989 TRACE("(%ld)\n",hInstall);
991 package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE);
992 if( package)
994 handle = alloc_msihandle( &package->db->hdr );
995 msiobj_release( &package->hdr );
997 else if ((remote_package = (IWineMsiRemotePackage *)msi_get_remote( hInstall )))
999 IWineMsiRemotePackage_GetActiveDatabase(remote_package, &handle);
1000 IWineMsiRemotePackage_Release(remote_package);
1003 return handle;
1006 INT MSI_ProcessMessage( MSIPACKAGE *package, INSTALLMESSAGE eMessageType,
1007 MSIRECORD *record)
1009 static const WCHAR szActionData[] =
1010 {'A','c','t','i','o','n','D','a','t','a',0};
1011 static const WCHAR szSetProgress[] =
1012 {'S','e','t','P','r','o','g','r','e','s','s',0};
1013 static const WCHAR szActionText[] =
1014 {'A','c','t','i','o','n','T','e','x','t',0};
1015 DWORD log_type = 0;
1016 LPWSTR message;
1017 DWORD sz;
1018 DWORD total_size = 0;
1019 INT i;
1020 INT rc;
1021 char *msg;
1022 int len;
1024 TRACE("%x\n", eMessageType);
1025 rc = 0;
1027 if ((eMessageType & 0xff000000) == INSTALLMESSAGE_ERROR)
1028 log_type |= INSTALLLOGMODE_ERROR;
1029 if ((eMessageType & 0xff000000) == INSTALLMESSAGE_WARNING)
1030 log_type |= INSTALLLOGMODE_WARNING;
1031 if ((eMessageType & 0xff000000) == INSTALLMESSAGE_USER)
1032 log_type |= INSTALLLOGMODE_USER;
1033 if ((eMessageType & 0xff000000) == INSTALLMESSAGE_INFO)
1034 log_type |= INSTALLLOGMODE_INFO;
1035 if ((eMessageType & 0xff000000) == INSTALLMESSAGE_COMMONDATA)
1036 log_type |= INSTALLLOGMODE_COMMONDATA;
1037 if ((eMessageType & 0xff000000) == INSTALLMESSAGE_ACTIONSTART)
1038 log_type |= INSTALLLOGMODE_ACTIONSTART;
1039 if ((eMessageType & 0xff000000) == INSTALLMESSAGE_ACTIONDATA)
1040 log_type |= INSTALLLOGMODE_ACTIONDATA;
1041 /* just a guess */
1042 if ((eMessageType & 0xff000000) == INSTALLMESSAGE_PROGRESS)
1043 log_type |= 0x800;
1045 if ((eMessageType & 0xff000000) == INSTALLMESSAGE_ACTIONSTART)
1047 static const WCHAR template_s[]=
1048 {'A','c','t','i','o','n',' ','%','s',':',' ','%','s','.',' ',0};
1049 static const WCHAR format[] =
1050 {'H','H','\'',':','\'','m','m','\'',':','\'','s','s',0};
1051 WCHAR timet[0x100];
1052 LPCWSTR action_text, action;
1053 LPWSTR deformatted = NULL;
1055 GetTimeFormatW(LOCALE_USER_DEFAULT, 0, NULL, format, timet, 0x100);
1057 action = MSI_RecordGetString(record, 1);
1058 action_text = MSI_RecordGetString(record, 2);
1060 if (!action || !action_text)
1061 return IDOK;
1063 deformat_string(package, action_text, &deformatted);
1065 len = strlenW(timet) + strlenW(action) + strlenW(template_s);
1066 if (deformatted)
1067 len += strlenW(deformatted);
1068 message = msi_alloc(len*sizeof(WCHAR));
1069 sprintfW(message, template_s, timet, action);
1070 if (deformatted)
1071 strcatW(message, deformatted);
1072 msi_free(deformatted);
1074 else
1076 INT msg_field=1;
1077 message = msi_alloc(1*sizeof (WCHAR));
1078 message[0]=0;
1079 msg_field = MSI_RecordGetFieldCount(record);
1080 for (i = 1; i <= msg_field; i++)
1082 LPWSTR tmp;
1083 WCHAR number[3];
1084 static const WCHAR format[] = { '%','i',':',' ',0};
1085 static const WCHAR space[] = { ' ',0};
1086 sz = 0;
1087 MSI_RecordGetStringW(record,i,NULL,&sz);
1088 sz+=4;
1089 total_size+=sz*sizeof(WCHAR);
1090 tmp = msi_alloc(sz*sizeof(WCHAR));
1091 message = msi_realloc(message,total_size*sizeof (WCHAR));
1093 MSI_RecordGetStringW(record,i,tmp,&sz);
1095 if (msg_field > 1)
1097 sprintfW(number,format,i);
1098 strcatW(message,number);
1100 strcatW(message,tmp);
1101 if (msg_field > 1)
1102 strcatW(message,space);
1104 msi_free(tmp);
1108 TRACE("(%p %x %x %s)\n", gUIHandlerA, gUIFilter, log_type,
1109 debugstr_w(message));
1111 /* convert it to ASCII */
1112 len = WideCharToMultiByte( CP_ACP, 0, message, -1,
1113 NULL, 0, NULL, NULL );
1114 msg = msi_alloc( len );
1115 WideCharToMultiByte( CP_ACP, 0, message, -1,
1116 msg, len, NULL, NULL );
1118 if (gUIHandlerA && (gUIFilter & log_type))
1120 rc = gUIHandlerA(gUIContext,eMessageType,msg);
1123 if ((!rc) && (gszLogFile[0]) && !((eMessageType & 0xff000000) ==
1124 INSTALLMESSAGE_PROGRESS))
1126 DWORD write;
1127 HANDLE log_file = CreateFileW(gszLogFile,GENERIC_WRITE, 0, NULL,
1128 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1130 if (log_file != INVALID_HANDLE_VALUE)
1132 SetFilePointer(log_file,0, NULL, FILE_END);
1133 WriteFile(log_file,msg,strlen(msg),&write,NULL);
1134 WriteFile(log_file,"\n",1,&write,NULL);
1135 CloseHandle(log_file);
1138 msi_free( msg );
1140 msi_free( message);
1142 switch (eMessageType & 0xff000000)
1144 case INSTALLMESSAGE_ACTIONDATA:
1145 /* FIXME: format record here instead of in ui_actiondata to get the
1146 * correct action data for external scripts */
1147 ControlEvent_FireSubscribedEvent(package, szActionData, record);
1148 break;
1149 case INSTALLMESSAGE_ACTIONSTART:
1151 MSIRECORD *uirow;
1152 LPWSTR deformated;
1153 LPCWSTR action_text = MSI_RecordGetString(record, 2);
1155 deformat_string(package, action_text, &deformated);
1156 uirow = MSI_CreateRecord(1);
1157 MSI_RecordSetStringW(uirow, 1, deformated);
1158 TRACE("INSTALLMESSAGE_ACTIONSTART: %s\n", debugstr_w(deformated));
1159 msi_free(deformated);
1161 ControlEvent_FireSubscribedEvent(package, szActionText, uirow);
1163 msiobj_release(&uirow->hdr);
1164 break;
1166 case INSTALLMESSAGE_PROGRESS:
1167 ControlEvent_FireSubscribedEvent(package, szSetProgress, record);
1168 break;
1171 return ERROR_SUCCESS;
1174 INT WINAPI MsiProcessMessage( MSIHANDLE hInstall, INSTALLMESSAGE eMessageType,
1175 MSIHANDLE hRecord)
1177 UINT ret = ERROR_INVALID_HANDLE;
1178 MSIPACKAGE *package = NULL;
1179 MSIRECORD *record = NULL;
1181 package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE );
1182 if( !package )
1184 HRESULT hr;
1185 IWineMsiRemotePackage *remote_package;
1187 remote_package = (IWineMsiRemotePackage *)msi_get_remote( hInstall );
1188 if (!remote_package)
1189 return ERROR_INVALID_HANDLE;
1191 hr = IWineMsiRemotePackage_ProcessMessage( remote_package, eMessageType, hRecord );
1193 IWineMsiRemotePackage_Release( remote_package );
1195 if (FAILED(hr))
1197 if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
1198 return HRESULT_CODE(hr);
1200 return ERROR_FUNCTION_FAILED;
1203 return ERROR_SUCCESS;
1206 record = msihandle2msiinfo( hRecord, MSIHANDLETYPE_RECORD );
1207 if( !record )
1208 goto out;
1210 ret = MSI_ProcessMessage( package, eMessageType, record );
1212 out:
1213 msiobj_release( &package->hdr );
1214 if( record )
1215 msiobj_release( &record->hdr );
1217 return ret;
1220 /* property code */
1222 UINT WINAPI MsiSetPropertyA( MSIHANDLE hInstall, LPCSTR szName, LPCSTR szValue )
1224 LPWSTR szwName = NULL, szwValue = NULL;
1225 UINT r = ERROR_OUTOFMEMORY;
1227 szwName = strdupAtoW( szName );
1228 if( szName && !szwName )
1229 goto end;
1231 szwValue = strdupAtoW( szValue );
1232 if( szValue && !szwValue )
1233 goto end;
1235 r = MsiSetPropertyW( hInstall, szwName, szwValue);
1237 end:
1238 msi_free( szwName );
1239 msi_free( szwValue );
1241 return r;
1244 UINT MSI_SetPropertyW( MSIPACKAGE *package, LPCWSTR szName, LPCWSTR szValue)
1246 MSIQUERY *view;
1247 MSIRECORD *row = NULL;
1248 UINT rc;
1249 DWORD sz = 0;
1250 WCHAR Query[1024];
1252 static const WCHAR Insert[] = {
1253 'I','N','S','E','R','T',' ','i','n','t','o',' ',
1254 '`','_','P','r','o','p','e','r','t','y','`',' ','(',
1255 '`','_','P','r','o','p','e','r','t','y','`',',',
1256 '`','V','a','l','u','e','`',')',' ','V','A','L','U','E','S'
1257 ,' ','(','?',',','?',')',0};
1258 static const WCHAR Update[] = {
1259 'U','P','D','A','T','E',' ','`','_','P','r','o','p','e','r','t','y','`',
1260 ' ','s','e','t',' ','`','V','a','l','u','e','`',' ','=',' ','?',' ',
1261 'w','h','e','r','e',' ','`','_','P','r','o','p','e','r','t','y','`',
1262 ' ','=',' ','\'','%','s','\'',0};
1263 static const WCHAR Delete[] = {
1264 'D','E','L','E','T','E',' ','F','R','O','M',' ',
1265 '`','_','P','r','o','p','e','r','t','y','`',' ','W','H','E','R','E',' ',
1266 '`','_','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',0};
1268 TRACE("%p %s %s\n", package, debugstr_w(szName), debugstr_w(szValue));
1270 if (!szName)
1271 return ERROR_INVALID_PARAMETER;
1273 /* this one is weird... */
1274 if (!szName[0])
1275 return szValue ? ERROR_FUNCTION_FAILED : ERROR_SUCCESS;
1277 rc = MSI_GetPropertyW(package, szName, 0, &sz);
1278 if (!szValue || !*szValue)
1280 sprintfW(Query, Delete, szName);
1282 else if (rc == ERROR_MORE_DATA || rc == ERROR_SUCCESS)
1284 sprintfW(Query, Update, szName);
1286 row = MSI_CreateRecord(1);
1287 MSI_RecordSetStringW(row, 1, szValue);
1289 else
1291 strcpyW(Query, Insert);
1293 row = MSI_CreateRecord(2);
1294 MSI_RecordSetStringW(row, 1, szName);
1295 MSI_RecordSetStringW(row, 2, szValue);
1298 rc = MSI_DatabaseOpenViewW(package->db, Query, &view);
1299 if (rc == ERROR_SUCCESS)
1301 rc = MSI_ViewExecute(view, row);
1302 MSI_ViewClose(view);
1303 msiobj_release(&view->hdr);
1306 msiobj_release(&row->hdr);
1308 return rc;
1311 UINT WINAPI MsiSetPropertyW( MSIHANDLE hInstall, LPCWSTR szName, LPCWSTR szValue)
1313 MSIPACKAGE *package;
1314 UINT ret;
1316 package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE);
1317 if( !package )
1319 HRESULT hr;
1320 BSTR name, value;
1321 IWineMsiRemotePackage *remote_package;
1323 remote_package = (IWineMsiRemotePackage *)msi_get_remote( hInstall );
1324 if (!remote_package)
1325 return ERROR_INVALID_HANDLE;
1327 name = SysAllocString( szName );
1328 value = SysAllocString( szValue );
1329 if (!name || !value)
1331 SysFreeString( name );
1332 SysFreeString( value );
1333 IWineMsiRemotePackage_Release( remote_package );
1334 return ERROR_OUTOFMEMORY;
1337 hr = IWineMsiRemotePackage_SetProperty( remote_package, name, value );
1339 SysFreeString( name );
1340 SysFreeString( value );
1341 IWineMsiRemotePackage_Release( remote_package );
1343 if (FAILED(hr))
1345 if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
1346 return HRESULT_CODE(hr);
1348 return ERROR_FUNCTION_FAILED;
1351 return ERROR_SUCCESS;
1354 ret = MSI_SetPropertyW( package, szName, szValue);
1355 msiobj_release( &package->hdr );
1356 return ret;
1359 static MSIRECORD *MSI_GetPropertyRow( MSIPACKAGE *package, LPCWSTR name )
1361 static const WCHAR query[]= {
1362 'S','E','L','E','C','T',' ','`','V','a','l','u','e','`',' ',
1363 'F','R','O','M',' ' ,'`','_','P','r','o','p','e','r','t','y','`',
1364 ' ','W','H','E','R','E',' ' ,'`','_','P','r','o','p','e','r','t','y','`',
1365 '=','\'','%','s','\'',0};
1367 if (!name || !*name)
1368 return NULL;
1370 return MSI_QueryGetRecord( package->db, query, name );
1373 /* internal function, not compatible with MsiGetPropertyW */
1374 UINT MSI_GetPropertyW( MSIPACKAGE *package, LPCWSTR szName,
1375 LPWSTR szValueBuf, LPDWORD pchValueBuf )
1377 MSIRECORD *row;
1378 UINT rc = ERROR_FUNCTION_FAILED;
1380 row = MSI_GetPropertyRow( package, szName );
1382 if (*pchValueBuf > 0)
1383 szValueBuf[0] = 0;
1385 if (row)
1387 rc = MSI_RecordGetStringW(row, 1, szValueBuf, pchValueBuf);
1388 msiobj_release(&row->hdr);
1391 if (rc == ERROR_SUCCESS)
1392 TRACE("returning %s for property %s\n", debugstr_w(szValueBuf),
1393 debugstr_w(szName));
1394 else if (rc == ERROR_MORE_DATA)
1395 TRACE("need %d sized buffer for %s\n", *pchValueBuf,
1396 debugstr_w(szName));
1397 else
1399 *pchValueBuf = 0;
1400 TRACE("property %s not found\n", debugstr_w(szName));
1403 return rc;
1406 LPWSTR msi_dup_property(MSIPACKAGE *package, LPCWSTR prop)
1408 DWORD sz = 0;
1409 LPWSTR str;
1410 UINT r;
1412 r = MSI_GetPropertyW(package, prop, NULL, &sz);
1413 if (r != ERROR_SUCCESS && r != ERROR_MORE_DATA)
1414 return NULL;
1416 sz++;
1417 str = msi_alloc(sz * sizeof(WCHAR));
1418 r = MSI_GetPropertyW(package, prop, str, &sz);
1419 if (r != ERROR_SUCCESS)
1421 msi_free(str);
1422 str = NULL;
1425 return str;
1428 int msi_get_property_int(MSIPACKAGE *package, LPCWSTR prop, int def)
1430 LPWSTR str = msi_dup_property(package, prop);
1431 int val = str ? atoiW(str) : def;
1432 msi_free(str);
1433 return val;
1436 static UINT MSI_GetProperty( MSIHANDLE handle, LPCWSTR name,
1437 awstring *szValueBuf, LPDWORD pchValueBuf )
1439 static const WCHAR empty[] = {0};
1440 MSIPACKAGE *package;
1441 MSIRECORD *row = NULL;
1442 UINT r = ERROR_FUNCTION_FAILED;
1443 LPCWSTR val = NULL;
1445 TRACE("%lu %s %p %p\n", handle, debugstr_w(name),
1446 szValueBuf->str.w, pchValueBuf );
1448 if (!name)
1449 return ERROR_INVALID_PARAMETER;
1451 package = msihandle2msiinfo( handle, MSIHANDLETYPE_PACKAGE );
1452 if (!package)
1454 HRESULT hr;
1455 IWineMsiRemotePackage *remote_package;
1456 LPWSTR value = NULL;
1457 BSTR bname;
1458 DWORD len;
1460 remote_package = (IWineMsiRemotePackage *)msi_get_remote( handle );
1461 if (!remote_package)
1462 return ERROR_INVALID_HANDLE;
1464 bname = SysAllocString( name );
1465 if (!bname)
1467 IWineMsiRemotePackage_Release( remote_package );
1468 return ERROR_OUTOFMEMORY;
1471 len = 0;
1472 hr = IWineMsiRemotePackage_GetProperty( remote_package, bname, NULL, &len );
1473 if (FAILED(hr))
1474 goto done;
1476 len++;
1477 value = msi_alloc(len * sizeof(WCHAR));
1478 if (!value)
1480 r = ERROR_OUTOFMEMORY;
1481 goto done;
1484 hr = IWineMsiRemotePackage_GetProperty( remote_package, bname, (BSTR *)value, &len );
1485 if (FAILED(hr))
1486 goto done;
1488 r = msi_strcpy_to_awstring( value, szValueBuf, pchValueBuf );
1490 /* Bug required by Adobe installers */
1491 if (!szValueBuf->unicode && !szValueBuf->str.a)
1492 *pchValueBuf *= sizeof(WCHAR);
1494 done:
1495 IWineMsiRemotePackage_Release(remote_package);
1496 SysFreeString(bname);
1497 msi_free(value);
1499 if (FAILED(hr))
1501 if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
1502 return HRESULT_CODE(hr);
1504 return ERROR_FUNCTION_FAILED;
1507 return r;
1510 row = MSI_GetPropertyRow( package, name );
1511 if (row)
1512 val = MSI_RecordGetString( row, 1 );
1514 if (!val)
1515 val = empty;
1517 r = msi_strcpy_to_awstring( val, szValueBuf, pchValueBuf );
1519 if (row)
1520 msiobj_release( &row->hdr );
1521 msiobj_release( &package->hdr );
1523 return r;
1526 UINT WINAPI MsiGetPropertyA( MSIHANDLE hInstall, LPCSTR szName,
1527 LPSTR szValueBuf, LPDWORD pchValueBuf )
1529 awstring val;
1530 LPWSTR name;
1531 UINT r;
1533 val.unicode = FALSE;
1534 val.str.a = szValueBuf;
1536 name = strdupAtoW( szName );
1537 if (szName && !name)
1538 return ERROR_OUTOFMEMORY;
1540 r = MSI_GetProperty( hInstall, name, &val, pchValueBuf );
1541 msi_free( name );
1542 return r;
1545 UINT WINAPI MsiGetPropertyW( MSIHANDLE hInstall, LPCWSTR szName,
1546 LPWSTR szValueBuf, LPDWORD pchValueBuf )
1548 awstring val;
1550 val.unicode = TRUE;
1551 val.str.w = szValueBuf;
1553 return MSI_GetProperty( hInstall, szName, &val, pchValueBuf );
1556 typedef struct _msi_remote_package_impl {
1557 const IWineMsiRemotePackageVtbl *lpVtbl;
1558 MSIHANDLE package;
1559 LONG refs;
1560 } msi_remote_package_impl;
1562 static inline msi_remote_package_impl* mrp_from_IWineMsiRemotePackage( IWineMsiRemotePackage* iface )
1564 return (msi_remote_package_impl*) iface;
1567 static HRESULT WINAPI mrp_QueryInterface( IWineMsiRemotePackage *iface,
1568 REFIID riid,LPVOID *ppobj)
1570 if( IsEqualCLSID( riid, &IID_IUnknown ) ||
1571 IsEqualCLSID( riid, &IID_IWineMsiRemotePackage ) )
1573 IUnknown_AddRef( iface );
1574 *ppobj = iface;
1575 return S_OK;
1578 return E_NOINTERFACE;
1581 static ULONG WINAPI mrp_AddRef( IWineMsiRemotePackage *iface )
1583 msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
1585 return InterlockedIncrement( &This->refs );
1588 static ULONG WINAPI mrp_Release( IWineMsiRemotePackage *iface )
1590 msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
1591 ULONG r;
1593 r = InterlockedDecrement( &This->refs );
1594 if (r == 0)
1596 MsiCloseHandle( This->package );
1597 msi_free( This );
1599 return r;
1602 static HRESULT WINAPI mrp_SetMsiHandle( IWineMsiRemotePackage *iface, MSIHANDLE handle )
1604 msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
1605 This->package = handle;
1606 return S_OK;
1609 HRESULT WINAPI mrp_GetActiveDatabase( IWineMsiRemotePackage *iface, MSIHANDLE *handle )
1611 msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
1612 IWineMsiRemoteDatabase *rdb = NULL;
1613 HRESULT hr;
1614 MSIHANDLE hdb;
1616 hr = create_msi_remote_database( NULL, (LPVOID *)&rdb );
1617 if (FAILED(hr) || !rdb)
1619 ERR("Failed to create remote database\n");
1620 return hr;
1623 hdb = MsiGetActiveDatabase(This->package);
1625 hr = IWineMsiRemoteDatabase_SetMsiHandle( rdb, hdb );
1626 if (FAILED(hr))
1628 ERR("Failed to set the database handle\n");
1629 return hr;
1632 *handle = alloc_msi_remote_handle( (IUnknown *)rdb );
1633 return S_OK;
1636 HRESULT WINAPI mrp_GetProperty( IWineMsiRemotePackage *iface, BSTR property, BSTR *value, DWORD *size )
1638 msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
1639 UINT r;
1641 r = MsiGetPropertyW(This->package, (LPWSTR)property, (LPWSTR)value, size);
1642 if (r != ERROR_SUCCESS)
1643 return HRESULT_FROM_WIN32(r);
1645 return S_OK;
1648 HRESULT WINAPI mrp_SetProperty( IWineMsiRemotePackage *iface, BSTR property, BSTR value )
1650 msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
1651 UINT r = MsiSetPropertyW(This->package, (LPWSTR)property, (LPWSTR)value);
1652 return HRESULT_FROM_WIN32(r);
1655 HRESULT WINAPI mrp_ProcessMessage( IWineMsiRemotePackage *iface, INSTALLMESSAGE message, MSIHANDLE record )
1657 msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
1658 UINT r = MsiProcessMessage(This->package, message, record);
1659 return HRESULT_FROM_WIN32(r);
1662 HRESULT WINAPI mrp_DoAction( IWineMsiRemotePackage *iface, BSTR action )
1664 msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
1665 UINT r = MsiDoActionW(This->package, (LPWSTR)action);
1666 return HRESULT_FROM_WIN32(r);
1669 HRESULT WINAPI mrp_Sequence( IWineMsiRemotePackage *iface, BSTR table, int sequence )
1671 msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
1672 UINT r = MsiSequenceW(This->package, (LPWSTR)table, sequence);
1673 return HRESULT_FROM_WIN32(r);
1676 HRESULT WINAPI mrp_GetTargetPath( IWineMsiRemotePackage *iface, BSTR folder, BSTR *value, DWORD *size )
1678 msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
1679 UINT r = MsiGetTargetPathW(This->package, (LPWSTR)folder, (LPWSTR)value, size);
1680 return HRESULT_FROM_WIN32(r);
1683 HRESULT WINAPI mrp_SetTargetPath( IWineMsiRemotePackage *iface, BSTR folder, BSTR value)
1685 msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
1686 UINT r = MsiSetTargetPathW(This->package, (LPWSTR)folder, (LPWSTR)value);
1687 return HRESULT_FROM_WIN32(r);
1690 HRESULT WINAPI mrp_GetSourcePath( IWineMsiRemotePackage *iface, BSTR folder, BSTR *value, DWORD *size )
1692 msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
1693 UINT r = MsiGetSourcePathW(This->package, (LPWSTR)folder, (LPWSTR)value, size);
1694 return HRESULT_FROM_WIN32(r);
1697 HRESULT WINAPI mrp_GetMode( IWineMsiRemotePackage *iface, MSIRUNMODE mode, BOOL *ret )
1699 msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
1700 *ret = MsiGetMode(This->package, mode);
1701 return S_OK;
1704 HRESULT WINAPI mrp_GetFeatureState( IWineMsiRemotePackage *iface, BSTR feature,
1705 INSTALLSTATE *installed, INSTALLSTATE *action )
1707 msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
1708 UINT r = MsiGetFeatureStateW(This->package, (LPWSTR)feature, installed, action);
1709 return HRESULT_FROM_WIN32(r);
1712 HRESULT WINAPI mrp_SetFeatureState( IWineMsiRemotePackage *iface, BSTR feature, INSTALLSTATE state )
1714 msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
1715 UINT r = MsiSetFeatureStateW(This->package, (LPWSTR)feature, state);
1716 return HRESULT_FROM_WIN32(r);
1719 HRESULT WINAPI mrp_GetComponentState( IWineMsiRemotePackage *iface, BSTR component,
1720 INSTALLSTATE *installed, INSTALLSTATE *action )
1722 msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
1723 UINT r = MsiGetComponentStateW(This->package, (LPWSTR)component, installed, action);
1724 return HRESULT_FROM_WIN32(r);
1727 HRESULT WINAPI mrp_SetComponentState( IWineMsiRemotePackage *iface, BSTR component, INSTALLSTATE state )
1729 msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
1730 UINT r = MsiSetComponentStateW(This->package, (LPWSTR)component, state);
1731 return HRESULT_FROM_WIN32(r);
1734 HRESULT WINAPI mrp_GetLanguage( IWineMsiRemotePackage *iface, LANGID *language )
1736 msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
1737 *language = MsiGetLanguage(This->package);
1738 return S_OK;
1741 HRESULT WINAPI mrp_SetInstallLevel( IWineMsiRemotePackage *iface, int level )
1743 msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
1744 UINT r = MsiSetInstallLevel(This->package, level);
1745 return HRESULT_FROM_WIN32(r);
1748 HRESULT WINAPI mrp_FormatRecord( IWineMsiRemotePackage *iface, MSIHANDLE record,
1749 BSTR value, DWORD *size )
1751 msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
1752 UINT r = MsiFormatRecordW(This->package, record, (LPWSTR)value, size);
1753 return HRESULT_FROM_WIN32(r);
1756 HRESULT WINAPI mrp_EvaluateCondition( IWineMsiRemotePackage *iface, BSTR condition )
1758 msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
1759 UINT r = MsiEvaluateConditionW(This->package, (LPWSTR)condition);
1760 return HRESULT_FROM_WIN32(r);
1763 static const IWineMsiRemotePackageVtbl msi_remote_package_vtbl =
1765 mrp_QueryInterface,
1766 mrp_AddRef,
1767 mrp_Release,
1768 mrp_SetMsiHandle,
1769 mrp_GetActiveDatabase,
1770 mrp_GetProperty,
1771 mrp_SetProperty,
1772 mrp_ProcessMessage,
1773 mrp_DoAction,
1774 mrp_Sequence,
1775 mrp_GetTargetPath,
1776 mrp_SetTargetPath,
1777 mrp_GetSourcePath,
1778 mrp_GetMode,
1779 mrp_GetFeatureState,
1780 mrp_SetFeatureState,
1781 mrp_GetComponentState,
1782 mrp_SetComponentState,
1783 mrp_GetLanguage,
1784 mrp_SetInstallLevel,
1785 mrp_FormatRecord,
1786 mrp_EvaluateCondition,
1789 HRESULT create_msi_remote_package( IUnknown *pOuter, LPVOID *ppObj )
1791 msi_remote_package_impl* This;
1793 This = msi_alloc( sizeof *This );
1794 if (!This)
1795 return E_OUTOFMEMORY;
1797 This->lpVtbl = &msi_remote_package_vtbl;
1798 This->package = 0;
1799 This->refs = 1;
1801 *ppObj = This;
1803 return S_OK;
1806 UINT msi_package_add_info(MSIPACKAGE *package, DWORD context, DWORD options,
1807 LPCWSTR property, LPWSTR value)
1809 MSISOURCELISTINFO *info;
1811 info = msi_alloc(sizeof(MSISOURCELISTINFO));
1812 if (!info)
1813 return ERROR_OUTOFMEMORY;
1815 info->context = context;
1816 info->options = options;
1817 info->property = property;
1818 info->value = strdupW(value);
1819 list_add_head(&package->sourcelist_info, &info->entry);
1821 return ERROR_SUCCESS;
1824 UINT msi_package_add_media_disk(MSIPACKAGE *package, DWORD context, DWORD options,
1825 DWORD disk_id, LPWSTR volume_label, LPWSTR disk_prompt)
1827 MSIMEDIADISK *disk;
1829 disk = msi_alloc(sizeof(MSIMEDIADISK));
1830 if (!disk)
1831 return ERROR_OUTOFMEMORY;
1833 disk->context = context;
1834 disk->options = options;
1835 disk->disk_id = disk_id;
1836 disk->volume_label = strdupW(volume_label);
1837 disk->disk_prompt = strdupW(disk_prompt);
1838 list_add_head(&package->sourcelist_media, &disk->entry);
1840 return ERROR_SUCCESS;