quartz: Remove extraneous parentheses (LLVM/Clang).
[wine.git] / dlls / msi / package.c
blob49acdb77f775a13298f7bb66b94b613525814c80
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 remove_tracked_tempfiles( MSIPACKAGE *package )
54 struct list *item, *cursor;
56 LIST_FOR_EACH_SAFE( item, cursor, &package->tempfiles )
58 MSITEMPFILE *temp = LIST_ENTRY( item, MSITEMPFILE, entry );
60 list_remove( &temp->entry );
61 TRACE("deleting temp file %s\n", debugstr_w( temp->Path ));
62 DeleteFileW( temp->Path );
63 msi_free( temp->Path );
64 msi_free( temp );
68 static void free_feature( MSIFEATURE *feature )
70 struct list *item, *cursor;
72 LIST_FOR_EACH_SAFE( item, cursor, &feature->Children )
74 FeatureList *fl = LIST_ENTRY( item, FeatureList, entry );
75 list_remove( &fl->entry );
76 msi_free( fl );
79 LIST_FOR_EACH_SAFE( item, cursor, &feature->Components )
81 ComponentList *cl = LIST_ENTRY( item, ComponentList, entry );
82 list_remove( &cl->entry );
83 msi_free( cl );
85 msi_free( feature->Feature );
86 msi_free( feature->Feature_Parent );
87 msi_free( feature->Directory );
88 msi_free( feature->Description );
89 msi_free( feature->Title );
90 msi_free( feature );
93 static void free_extension( MSIEXTENSION *ext )
95 struct list *item, *cursor;
97 LIST_FOR_EACH_SAFE( item, cursor, &ext->verbs )
99 MSIVERB *verb = LIST_ENTRY( item, MSIVERB, entry );
101 list_remove( &verb->entry );
102 msi_free( verb->Verb );
103 msi_free( verb->Command );
104 msi_free( verb->Argument );
105 msi_free( verb );
108 msi_free( ext->Extension );
109 msi_free( ext->ProgIDText );
110 msi_free( ext );
113 static void free_assembly( MSIASSEMBLY *assembly )
115 msi_free( assembly->feature );
116 msi_free( assembly->manifest );
117 msi_free( assembly->application );
118 msi_free( assembly->display_name );
119 if (assembly->tempdir) RemoveDirectoryW( assembly->tempdir );
120 msi_free( assembly->tempdir );
121 msi_free( assembly );
124 static void free_package_structures( MSIPACKAGE *package )
126 INT i;
127 struct list *item, *cursor;
129 TRACE("Freeing package action data\n");
131 remove_tracked_tempfiles(package);
133 LIST_FOR_EACH_SAFE( item, cursor, &package->features )
135 MSIFEATURE *feature = LIST_ENTRY( item, MSIFEATURE, entry );
136 list_remove( &feature->entry );
137 free_feature( feature );
140 LIST_FOR_EACH_SAFE( item, cursor, &package->folders )
142 MSIFOLDER *folder = LIST_ENTRY( item, MSIFOLDER, entry );
144 list_remove( &folder->entry );
145 msi_free( folder->Parent );
146 msi_free( folder->Directory );
147 msi_free( folder->TargetDefault );
148 msi_free( folder->SourceLongPath );
149 msi_free( folder->SourceShortPath );
150 msi_free( folder->ResolvedTarget );
151 msi_free( folder->ResolvedSource );
152 msi_free( folder->Property );
153 msi_free( folder );
156 LIST_FOR_EACH_SAFE( item, cursor, &package->components )
158 MSICOMPONENT *comp = LIST_ENTRY( item, MSICOMPONENT, entry );
160 list_remove( &comp->entry );
161 msi_free( comp->Component );
162 msi_free( comp->ComponentId );
163 msi_free( comp->Directory );
164 msi_free( comp->Condition );
165 msi_free( comp->KeyPath );
166 msi_free( comp->FullKeypath );
167 if (comp->assembly) free_assembly( comp->assembly );
168 msi_free( comp );
171 LIST_FOR_EACH_SAFE( item, cursor, &package->files )
173 MSIFILE *file = LIST_ENTRY( item, MSIFILE, entry );
175 list_remove( &file->entry );
176 msi_free( file->File );
177 msi_free( file->FileName );
178 msi_free( file->ShortName );
179 msi_free( file->LongName );
180 msi_free( file->Version );
181 msi_free( file->Language );
182 msi_free( file->TargetPath );
183 msi_free( file );
186 /* clean up extension, progid, class and verb structures */
187 LIST_FOR_EACH_SAFE( item, cursor, &package->classes )
189 MSICLASS *cls = LIST_ENTRY( item, MSICLASS, entry );
191 list_remove( &cls->entry );
192 msi_free( cls->clsid );
193 msi_free( cls->Context );
194 msi_free( cls->Description );
195 msi_free( cls->FileTypeMask );
196 msi_free( cls->IconPath );
197 msi_free( cls->DefInprocHandler );
198 msi_free( cls->DefInprocHandler32 );
199 msi_free( cls->Argument );
200 msi_free( cls->ProgIDText );
201 msi_free( cls );
204 LIST_FOR_EACH_SAFE( item, cursor, &package->extensions )
206 MSIEXTENSION *ext = LIST_ENTRY( item, MSIEXTENSION, entry );
208 list_remove( &ext->entry );
209 free_extension( ext );
212 LIST_FOR_EACH_SAFE( item, cursor, &package->progids )
214 MSIPROGID *progid = LIST_ENTRY( item, MSIPROGID, entry );
216 list_remove( &progid->entry );
217 msi_free( progid->ProgID );
218 msi_free( progid->Description );
219 msi_free( progid->IconPath );
220 msi_free( progid );
223 LIST_FOR_EACH_SAFE( item, cursor, &package->mimes )
225 MSIMIME *mt = LIST_ENTRY( item, MSIMIME, entry );
227 list_remove( &mt->entry );
228 msi_free( mt->suffix );
229 msi_free( mt->clsid );
230 msi_free( mt->ContentType );
231 msi_free( mt );
234 LIST_FOR_EACH_SAFE( item, cursor, &package->appids )
236 MSIAPPID *appid = LIST_ENTRY( item, MSIAPPID, entry );
238 list_remove( &appid->entry );
239 msi_free( appid->AppID );
240 msi_free( appid->RemoteServerName );
241 msi_free( appid->LocalServer );
242 msi_free( appid->ServiceParameters );
243 msi_free( appid->DllSurrogate );
244 msi_free( appid );
247 LIST_FOR_EACH_SAFE( item, cursor, &package->sourcelist_info )
249 MSISOURCELISTINFO *info = LIST_ENTRY( item, MSISOURCELISTINFO, entry );
251 list_remove( &info->entry );
252 msi_free( info->value );
253 msi_free( info );
256 LIST_FOR_EACH_SAFE( item, cursor, &package->sourcelist_media )
258 MSIMEDIADISK *info = LIST_ENTRY( item, MSIMEDIADISK, entry );
260 list_remove( &info->entry );
261 msi_free( info->volume_label );
262 msi_free( info->disk_prompt );
263 msi_free( info );
266 if (package->script)
268 for (i = 0; i < TOTAL_SCRIPTS; i++)
269 msi_free_action_script( package, i );
271 for (i = 0; i < package->script->UniqueActionsCount; i++)
272 msi_free( package->script->UniqueActions[i] );
274 msi_free( package->script->UniqueActions );
275 msi_free( package->script );
278 LIST_FOR_EACH_SAFE( item, cursor, &package->patches )
280 MSIPATCHINFO *patch = LIST_ENTRY( item, MSIPATCHINFO, entry );
282 list_remove( &patch->entry );
283 msi_free( patch->patchcode );
284 msi_free( patch->transforms );
285 msi_free( patch->localfile );
286 msi_free( patch );
289 LIST_FOR_EACH_SAFE( item, cursor, &package->binaries )
291 MSIBINARY *binary = LIST_ENTRY( item, MSIBINARY, entry );
293 list_remove( &binary->entry );
294 if (binary->module)
295 FreeLibrary( binary->module );
296 if (!DeleteFileW( binary->tmpfile ))
297 ERR("failed to delete %s (%u)\n", debugstr_w(binary->tmpfile), GetLastError());
298 msi_free( binary->source );
299 msi_free( binary->tmpfile );
300 msi_free( binary );
303 msi_free( package->BaseURL );
304 msi_free( package->PackagePath );
305 msi_free( package->ProductCode );
306 msi_free( package->ActionFormat );
307 msi_free( package->LastAction );
308 msi_free( package->langids );
310 /* cleanup control event subscriptions */
311 ControlEvent_CleanupSubscriptions( package );
314 static void MSI_FreePackage( MSIOBJECTHDR *arg)
316 UINT i;
317 MSIPACKAGE *package = (MSIPACKAGE *)arg;
319 if( package->dialog )
320 msi_dialog_destroy( package->dialog );
322 msiobj_release( &package->db->hdr );
323 free_package_structures(package);
324 CloseHandle( package->log_file );
326 for (i = 0; i < CLR_VERSION_MAX; i++)
327 if (package->cache_net[i]) IAssemblyCache_Release( package->cache_net[i] );
328 if (package->cache_sxs) IAssemblyCache_Release( package->cache_sxs );
331 static UINT create_temp_property_table(MSIPACKAGE *package)
333 MSIQUERY *view = NULL;
334 UINT rc;
336 static const WCHAR CreateSql[] = {
337 'C','R','E','A','T','E',' ','T','A','B','L','E',' ',
338 '`','_','P','r','o','p','e','r','t','y','`',' ','(',' ',
339 '`','_','P','r','o','p','e','r','t','y','`',' ',
340 'C','H','A','R','(','5','6',')',' ','N','O','T',' ','N','U','L','L',' ',
341 'T','E','M','P','O','R','A','R','Y',',',' ',
342 '`','V','a','l','u','e','`',' ','C','H','A','R','(','9','8',')',' ',
343 'N','O','T',' ','N','U','L','L',' ','T','E','M','P','O','R','A','R','Y',
344 ' ','P','R','I','M','A','R','Y',' ','K','E','Y',' ',
345 '`','_','P','r','o','p','e','r','t','y','`',')',' ','H','O','L','D',0};
347 rc = MSI_DatabaseOpenViewW(package->db, CreateSql, &view);
348 if (rc != ERROR_SUCCESS)
349 return rc;
351 rc = MSI_ViewExecute(view, 0);
352 MSI_ViewClose(view);
353 msiobj_release(&view->hdr);
354 return rc;
357 UINT msi_clone_properties(MSIPACKAGE *package)
359 MSIQUERY *view_select = NULL;
360 UINT rc;
362 static const WCHAR query_select[] = {
363 'S','E','L','E','C','T',' ','*',' ',
364 'F','R','O','M',' ','`','P','r','o','p','e','r','t','y','`',0};
365 static const WCHAR query_insert[] = {
366 'I','N','S','E','R','T',' ','i','n','t','o',' ',
367 '`','_','P','r','o','p','e','r','t','y','`',' ',
368 '(','`','_','P','r','o','p','e','r','t','y','`',',',
369 '`','V','a','l','u','e','`',')',' ',
370 'V','A','L','U','E','S',' ','(','?',',','?',')',0};
371 static const WCHAR query_update[] = {
372 'U','P','D','A','T','E',' ','`','_','P','r','o','p','e','r','t','y','`',' ',
373 'S','E','T',' ','`','V','a','l','u','e','`',' ','=',' ','?',' ',
374 'W','H','E','R','E',' ','`','_','P','r','o','p','e','r','t','y','`',' ','=',' ','?',0};
376 rc = MSI_DatabaseOpenViewW( package->db, query_select, &view_select );
377 if (rc != ERROR_SUCCESS)
378 return rc;
380 rc = MSI_ViewExecute( view_select, 0 );
381 if (rc != ERROR_SUCCESS)
383 MSI_ViewClose( view_select );
384 msiobj_release( &view_select->hdr );
385 return rc;
388 while (1)
390 MSIQUERY *view_insert, *view_update;
391 MSIRECORD *rec_select;
393 rc = MSI_ViewFetch( view_select, &rec_select );
394 if (rc != ERROR_SUCCESS)
395 break;
397 rc = MSI_DatabaseOpenViewW( package->db, query_insert, &view_insert );
398 if (rc != ERROR_SUCCESS)
400 msiobj_release( &rec_select->hdr );
401 continue;
404 rc = MSI_ViewExecute( view_insert, rec_select );
405 MSI_ViewClose( view_insert );
406 msiobj_release( &view_insert->hdr );
407 if (rc != ERROR_SUCCESS)
409 MSIRECORD *rec_update;
411 TRACE("insert failed, trying update\n");
413 rc = MSI_DatabaseOpenViewW( package->db, query_update, &view_update );
414 if (rc != ERROR_SUCCESS)
416 WARN("open view failed %u\n", rc);
417 msiobj_release( &rec_select->hdr );
418 continue;
421 rec_update = MSI_CreateRecord( 2 );
422 MSI_RecordCopyField( rec_select, 1, rec_update, 2 );
423 MSI_RecordCopyField( rec_select, 2, rec_update, 1 );
424 rc = MSI_ViewExecute( view_update, rec_update );
425 if (rc != ERROR_SUCCESS)
426 WARN("update failed %u\n", rc);
428 MSI_ViewClose( view_update );
429 msiobj_release( &view_update->hdr );
430 msiobj_release( &rec_update->hdr );
433 msiobj_release( &rec_select->hdr );
436 MSI_ViewClose( view_select );
437 msiobj_release( &view_select->hdr );
438 return rc;
442 * set_installed_prop
444 * Sets the "Installed" property to indicate that
445 * the product is installed for the current user.
447 static UINT set_installed_prop( MSIPACKAGE *package )
449 HKEY hkey;
450 UINT r;
452 if (!package->ProductCode) return ERROR_FUNCTION_FAILED;
454 r = MSIREG_OpenUninstallKey( package->ProductCode, package->platform, &hkey, FALSE );
455 if (r == ERROR_SUCCESS)
457 RegCloseKey( hkey );
458 msi_set_property( package->db, szInstalled, szOne );
460 return r;
463 static UINT set_user_sid_prop( MSIPACKAGE *package )
465 SID_NAME_USE use;
466 LPWSTR user_name;
467 LPWSTR sid_str = NULL, dom = NULL;
468 DWORD size, dom_size;
469 PSID psid = NULL;
470 UINT r = ERROR_FUNCTION_FAILED;
472 size = 0;
473 GetUserNameW( NULL, &size );
475 user_name = msi_alloc( (size + 1) * sizeof(WCHAR) );
476 if (!user_name)
477 return ERROR_OUTOFMEMORY;
479 if (!GetUserNameW( user_name, &size ))
480 goto done;
482 size = 0;
483 dom_size = 0;
484 LookupAccountNameW( NULL, user_name, NULL, &size, NULL, &dom_size, &use );
486 psid = msi_alloc( size );
487 dom = msi_alloc( dom_size*sizeof (WCHAR) );
488 if (!psid || !dom)
490 r = ERROR_OUTOFMEMORY;
491 goto done;
494 if (!LookupAccountNameW( NULL, user_name, psid, &size, dom, &dom_size, &use ))
495 goto done;
497 if (!ConvertSidToStringSidW( psid, &sid_str ))
498 goto done;
500 r = msi_set_property( package->db, szUserSID, sid_str );
502 done:
503 LocalFree( sid_str );
504 msi_free( dom );
505 msi_free( psid );
506 msi_free( user_name );
508 return r;
511 static LPWSTR get_fusion_filename(MSIPACKAGE *package)
513 HKEY netsetup;
514 LONG res;
515 LPWSTR file = NULL;
516 DWORD index = 0, size;
517 WCHAR ver[MAX_PATH];
518 WCHAR name[MAX_PATH];
519 WCHAR windir[MAX_PATH];
521 static const WCHAR fusion[] = {'f','u','s','i','o','n','.','d','l','l',0};
522 static const WCHAR sub[] = {
523 'S','o','f','t','w','a','r','e','\\',
524 'M','i','c','r','o','s','o','f','t','\\',
525 'N','E','T',' ','F','r','a','m','e','w','o','r','k',' ','S','e','t','u','p','\\',
526 'N','D','P',0
528 static const WCHAR subdir[] = {
529 'M','i','c','r','o','s','o','f','t','.','N','E','T','\\',
530 'F','r','a','m','e','w','o','r','k','\\',0
533 res = RegOpenKeyExW(HKEY_LOCAL_MACHINE, sub, 0, KEY_ENUMERATE_SUB_KEYS, &netsetup);
534 if (res != ERROR_SUCCESS)
535 return NULL;
537 GetWindowsDirectoryW(windir, MAX_PATH);
539 ver[0] = '\0';
540 size = MAX_PATH;
541 while (RegEnumKeyExW(netsetup, index, name, &size, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
543 index++;
545 /* verify existence of fusion.dll .Net 3.0 does not install a new one */
546 if (strcmpW( ver, name ) < 0)
548 LPWSTR check;
549 size = lstrlenW(windir) + lstrlenW(subdir) + lstrlenW(name) +lstrlenW(fusion) + 3;
550 check = msi_alloc(size * sizeof(WCHAR));
552 if (!check)
554 msi_free(file);
555 return NULL;
558 lstrcpyW(check, windir);
559 lstrcatW(check, szBackSlash);
560 lstrcatW(check, subdir);
561 lstrcatW(check, name);
562 lstrcatW(check, szBackSlash);
563 lstrcatW(check, fusion);
565 if(GetFileAttributesW(check) != INVALID_FILE_ATTRIBUTES)
567 msi_free(file);
568 file = check;
569 lstrcpyW(ver, name);
571 else
572 msi_free(check);
576 RegCloseKey(netsetup);
577 return file;
580 typedef struct tagLANGANDCODEPAGE
582 WORD wLanguage;
583 WORD wCodePage;
584 } LANGANDCODEPAGE;
586 static void set_msi_assembly_prop(MSIPACKAGE *package)
588 UINT val_len;
589 DWORD size, handle;
590 LPVOID version = NULL;
591 WCHAR buf[MAX_PATH];
592 LPWSTR fusion, verstr;
593 LANGANDCODEPAGE *translate;
595 static const WCHAR netasm[] = {
596 'M','s','i','N','e','t','A','s','s','e','m','b','l','y','S','u','p','p','o','r','t',0
598 static const WCHAR translation[] = {
599 '\\','V','a','r','F','i','l','e','I','n','f','o',
600 '\\','T','r','a','n','s','l','a','t','i','o','n',0
602 static const WCHAR verfmt[] = {
603 '\\','S','t','r','i','n','g','F','i','l','e','I','n','f','o',
604 '\\','%','0','4','x','%','0','4','x',
605 '\\','P','r','o','d','u','c','t','V','e','r','s','i','o','n',0
608 fusion = get_fusion_filename(package);
609 if (!fusion)
610 return;
612 size = GetFileVersionInfoSizeW(fusion, &handle);
613 if (!size) return;
615 version = msi_alloc(size);
616 if (!version) return;
618 if (!GetFileVersionInfoW(fusion, handle, size, version))
619 goto done;
621 if (!VerQueryValueW(version, translation, (LPVOID *)&translate, &val_len))
622 goto done;
624 sprintfW(buf, verfmt, translate[0].wLanguage, translate[0].wCodePage);
626 if (!VerQueryValueW(version, buf, (LPVOID *)&verstr, &val_len))
627 goto done;
629 if (!val_len || !verstr)
630 goto done;
632 msi_set_property(package->db, netasm, verstr);
634 done:
635 msi_free(fusion);
636 msi_free(version);
639 static VOID set_installer_properties(MSIPACKAGE *package)
641 WCHAR pth[MAX_PATH];
642 WCHAR *ptr;
643 OSVERSIONINFOEXW OSVersion;
644 MEMORYSTATUSEX msex;
645 DWORD verval, len;
646 WCHAR verstr[10], bufstr[20];
647 HDC dc;
648 HKEY hkey;
649 LPWSTR username, companyname;
650 SYSTEM_INFO sys_info;
651 SYSTEMTIME systemtime;
652 LANGID langid;
654 static const WCHAR szCommonFilesFolder[] = {'C','o','m','m','o','n','F','i','l','e','s','F','o','l','d','e','r',0};
655 static const WCHAR szProgramFilesFolder[] = {'P','r','o','g','r','a','m','F','i','l','e','s','F','o','l','d','e','r',0};
656 static const WCHAR szCommonAppDataFolder[] = {'C','o','m','m','o','n','A','p','p','D','a','t','a','F','o','l','d','e','r',0};
657 static const WCHAR szFavoritesFolder[] = {'F','a','v','o','r','i','t','e','s','F','o','l','d','e','r',0};
658 static const WCHAR szFontsFolder[] = {'F','o','n','t','s','F','o','l','d','e','r',0};
659 static const WCHAR szSendToFolder[] = {'S','e','n','d','T','o','F','o','l','d','e','r',0};
660 static const WCHAR szStartMenuFolder[] = {'S','t','a','r','t','M','e','n','u','F','o','l','d','e','r',0};
661 static const WCHAR szStartupFolder[] = {'S','t','a','r','t','u','p','F','o','l','d','e','r',0};
662 static const WCHAR szTemplateFolder[] = {'T','e','m','p','l','a','t','e','F','o','l','d','e','r',0};
663 static const WCHAR szDesktopFolder[] = {'D','e','s','k','t','o','p','F','o','l','d','e','r',0};
664 static const WCHAR szProgramMenuFolder[] = {'P','r','o','g','r','a','m','M','e','n','u','F','o','l','d','e','r',0};
665 static const WCHAR szAdminToolsFolder[] = {'A','d','m','i','n','T','o','o','l','s','F','o','l','d','e','r',0};
666 static const WCHAR szAppDataFolder[] = {'A','p','p','D','a','t','a','F','o','l','d','e','r',0};
667 static const WCHAR szSystemFolder[] = {'S','y','s','t','e','m','F','o','l','d','e','r',0};
668 static const WCHAR szSystem16Folder[] = {'S','y','s','t','e','m','1','6','F','o','l','d','e','r',0};
669 static const WCHAR szLocalAppDataFolder[] = {'L','o','c','a','l','A','p','p','D','a','t','a','F','o','l','d','e','r',0};
670 static const WCHAR szMyPicturesFolder[] = {'M','y','P','i','c','t','u','r','e','s','F','o','l','d','e','r',0};
671 static const WCHAR szPersonalFolder[] = {'P','e','r','s','o','n','a','l','F','o','l','d','e','r',0};
672 static const WCHAR szWindowsFolder[] = {'W','i','n','d','o','w','s','F','o','l','d','e','r',0};
673 static const WCHAR szWindowsVolume[] = {'W','i','n','d','o','w','s','V','o','l','u','m','e',0};
674 static const WCHAR szTempFolder[]= {'T','e','m','p','F','o','l','d','e','r',0};
675 static const WCHAR szPrivileged[] = {'P','r','i','v','i','l','e','g','e','d',0};
676 static const WCHAR szVersion9x[] = {'V','e','r','s','i','o','n','9','X',0};
677 static const WCHAR szVersionNT[] = {'V','e','r','s','i','o','n','N','T',0};
678 static const WCHAR szMsiNTProductType[] = {'M','s','i','N','T','P','r','o','d','u','c','t','T','y','p','e',0};
679 static const WCHAR szFormat[] = {'%','l','i',0};
680 static const WCHAR szWindowsBuild[] = {'W','i','n','d','o','w','s','B','u','i','l','d',0};
681 static const WCHAR szServicePackLevel[] = {'S','e','r','v','i','c','e','P','a','c','k','L','e','v','e','l',0};
682 static const WCHAR szSix[] = {'6',0 };
683 static const WCHAR szVersionMsi[] = { 'V','e','r','s','i','o','n','M','s','i',0 };
684 static const WCHAR szVersionDatabase[] = { 'V','e','r','s','i','o','n','D','a','t','a','b','a','s','e',0 };
685 static const WCHAR szPhysicalMemory[] = { 'P','h','y','s','i','c','a','l','M','e','m','o','r','y',0 };
686 static const WCHAR szFormat2[] = {'%','l','i','.','%','l','i',0};
687 static const WCHAR szScreenX[] = {'S','c','r','e','e','n','X',0};
688 static const WCHAR szScreenY[] = {'S','c','r','e','e','n','Y',0};
689 static const WCHAR szColorBits[] = {'C','o','l','o','r','B','i','t','s',0};
690 static const WCHAR szIntFormat[] = {'%','d',0};
691 static const WCHAR szMsiAMD64[] = { 'M','s','i','A','M','D','6','4',0 };
692 static const WCHAR szMsix64[] = { 'M','s','i','x','6','4',0 };
693 static const WCHAR szSystem64Folder[] = { 'S','y','s','t','e','m','6','4','F','o','l','d','e','r',0 };
694 static const WCHAR szCommonFiles64Folder[] = { 'C','o','m','m','o','n','F','i','l','e','s','6','4','F','o','l','d','e','r',0 };
695 static const WCHAR szProgramFiles64Folder[] = { 'P','r','o','g','r','a','m','F','i','l','e','s','6','4','F','o','l','d','e','r',0 };
696 static const WCHAR szVersionNT64[] = { 'V','e','r','s','i','o','n','N','T','6','4',0 };
697 static const WCHAR szUserInfo[] = {
698 'S','O','F','T','W','A','R','E','\\',
699 'M','i','c','r','o','s','o','f','t','\\',
700 'M','S',' ','S','e','t','u','p',' ','(','A','C','M','E',')','\\',
701 'U','s','e','r',' ','I','n','f','o',0
703 static const WCHAR szDefName[] = { 'D','e','f','N','a','m','e',0 };
704 static const WCHAR szDefCompany[] = { 'D','e','f','C','o','m','p','a','n','y',0 };
705 static const WCHAR szCurrentVersion[] = {
706 'S','O','F','T','W','A','R','E','\\',
707 'M','i','c','r','o','s','o','f','t','\\',
708 'W','i','n','d','o','w','s',' ','N','T','\\',
709 'C','u','r','r','e','n','t','V','e','r','s','i','o','n',0
711 static const WCHAR szRegisteredUser[] = {'R','e','g','i','s','t','e','r','e','d','O','w','n','e','r',0};
712 static const WCHAR szRegisteredOrganization[] = {
713 'R','e','g','i','s','t','e','r','e','d','O','r','g','a','n','i','z','a','t','i','o','n',0
715 static const WCHAR szUSERNAME[] = {'U','S','E','R','N','A','M','E',0};
716 static const WCHAR szCOMPANYNAME[] = {'C','O','M','P','A','N','Y','N','A','M','E',0};
717 static const WCHAR szDate[] = {'D','a','t','e',0};
718 static const WCHAR szTime[] = {'T','i','m','e',0};
719 static const WCHAR szUserLanguageID[] = {'U','s','e','r','L','a','n','g','u','a','g','e','I','D',0};
720 static const WCHAR szSystemLangID[] = {'S','y','s','t','e','m','L','a','n','g','u','a','g','e','I','D',0};
721 static const WCHAR szProductState[] = {'P','r','o','d','u','c','t','S','t','a','t','e',0};
722 static const WCHAR szLogonUser[] = {'L','o','g','o','n','U','s','e','r',0};
723 static const WCHAR szNetHoodFolder[] = {'N','e','t','H','o','o','d','F','o','l','d','e','r',0};
724 static const WCHAR szPrintHoodFolder[] = {'P','r','i','n','t','H','o','o','d','F','o','l','d','e','r',0};
725 static const WCHAR szRecentFolder[] = {'R','e','c','e','n','t','F','o','l','d','e','r',0};
728 * Other things that probably should be set:
730 * ComputerName VirtualMemory
731 * ShellAdvSupport DefaultUIFont PackagecodeChanging
732 * CaptionHeight BorderTop BorderSide TextHeight
733 * RedirectedDllSupport
736 SHGetFolderPathW(NULL, CSIDL_COMMON_APPDATA, NULL, 0, pth);
737 strcatW(pth, szBackSlash);
738 msi_set_property(package->db, szCommonAppDataFolder, pth);
740 SHGetFolderPathW(NULL, CSIDL_FAVORITES, NULL, 0, pth);
741 strcatW(pth, szBackSlash);
742 msi_set_property(package->db, szFavoritesFolder, pth);
744 SHGetFolderPathW(NULL, CSIDL_FONTS, NULL, 0, pth);
745 strcatW(pth, szBackSlash);
746 msi_set_property(package->db, szFontsFolder, pth);
748 SHGetFolderPathW(NULL, CSIDL_SENDTO, NULL, 0, pth);
749 strcatW(pth, szBackSlash);
750 msi_set_property(package->db, szSendToFolder, pth);
752 SHGetFolderPathW(NULL, CSIDL_STARTMENU, NULL, 0, pth);
753 strcatW(pth, szBackSlash);
754 msi_set_property(package->db, szStartMenuFolder, pth);
756 SHGetFolderPathW(NULL, CSIDL_STARTUP, NULL, 0, pth);
757 strcatW(pth, szBackSlash);
758 msi_set_property(package->db, szStartupFolder, pth);
760 SHGetFolderPathW(NULL, CSIDL_TEMPLATES, NULL, 0, pth);
761 strcatW(pth, szBackSlash);
762 msi_set_property(package->db, szTemplateFolder, pth);
764 SHGetFolderPathW(NULL, CSIDL_DESKTOP, NULL, 0, pth);
765 strcatW(pth, szBackSlash);
766 msi_set_property(package->db, szDesktopFolder, pth);
768 /* FIXME: set to AllUsers profile path if ALLUSERS is set */
769 SHGetFolderPathW(NULL, CSIDL_PROGRAMS, NULL, 0, pth);
770 strcatW(pth, szBackSlash);
771 msi_set_property(package->db, szProgramMenuFolder, pth);
773 SHGetFolderPathW(NULL, CSIDL_ADMINTOOLS, NULL, 0, pth);
774 strcatW(pth, szBackSlash);
775 msi_set_property(package->db, szAdminToolsFolder, pth);
777 SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, 0, pth);
778 strcatW(pth, szBackSlash);
779 msi_set_property(package->db, szAppDataFolder, pth);
781 SHGetFolderPathW(NULL, CSIDL_SYSTEM, NULL, 0, pth);
782 strcatW(pth, szBackSlash);
783 msi_set_property(package->db, szSystemFolder, pth);
784 msi_set_property(package->db, szSystem16Folder, pth);
786 SHGetFolderPathW(NULL, CSIDL_LOCAL_APPDATA, NULL, 0, pth);
787 strcatW(pth, szBackSlash);
788 msi_set_property(package->db, szLocalAppDataFolder, pth);
790 SHGetFolderPathW(NULL, CSIDL_MYPICTURES, NULL, 0, pth);
791 strcatW(pth, szBackSlash);
792 msi_set_property(package->db, szMyPicturesFolder, pth);
794 SHGetFolderPathW(NULL, CSIDL_PERSONAL, NULL, 0, pth);
795 strcatW(pth, szBackSlash);
796 msi_set_property(package->db, szPersonalFolder, pth);
798 SHGetFolderPathW(NULL, CSIDL_WINDOWS, NULL, 0, pth);
799 strcatW(pth, szBackSlash);
800 msi_set_property(package->db, szWindowsFolder, pth);
802 SHGetFolderPathW(NULL, CSIDL_PRINTHOOD, NULL, 0, pth);
803 strcatW(pth, szBackSlash);
804 msi_set_property(package->db, szPrintHoodFolder, pth);
806 SHGetFolderPathW(NULL, CSIDL_NETHOOD, NULL, 0, pth);
807 strcatW(pth, szBackSlash);
808 msi_set_property(package->db, szNetHoodFolder, pth);
810 SHGetFolderPathW(NULL, CSIDL_RECENT, NULL, 0, pth);
811 strcatW(pth, szBackSlash);
812 msi_set_property(package->db, szRecentFolder, pth);
814 /* Physical Memory is specified in MB. Using total amount. */
815 msex.dwLength = sizeof(msex);
816 GlobalMemoryStatusEx( &msex );
817 sprintfW( bufstr, szIntFormat, (int)(msex.ullTotalPhys / 1024 / 1024) );
818 msi_set_property(package->db, szPhysicalMemory, bufstr);
820 SHGetFolderPathW(NULL, CSIDL_WINDOWS, NULL, 0, pth);
821 ptr = strchrW(pth,'\\');
822 if (ptr) *(ptr + 1) = 0;
823 msi_set_property(package->db, szWindowsVolume, pth);
825 GetTempPathW(MAX_PATH,pth);
826 msi_set_property(package->db, szTempFolder, pth);
828 /* in a wine environment the user is always admin and privileged */
829 msi_set_property(package->db, szAdminUser, szOne);
830 msi_set_property(package->db, szPrivileged, szOne);
832 /* set the os things */
833 OSVersion.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEXW);
834 GetVersionExW((OSVERSIONINFOW *)&OSVersion);
835 verval = OSVersion.dwMinorVersion + OSVersion.dwMajorVersion * 100;
836 sprintfW(verstr, szFormat, verval);
837 switch (OSVersion.dwPlatformId)
839 case VER_PLATFORM_WIN32_WINDOWS:
840 msi_set_property(package->db, szVersion9x, verstr);
841 break;
842 case VER_PLATFORM_WIN32_NT:
843 msi_set_property(package->db, szVersionNT, verstr);
844 sprintfW(verstr, szFormat,OSVersion.wProductType);
845 msi_set_property(package->db, szMsiNTProductType, verstr);
846 break;
848 sprintfW(verstr, szFormat, OSVersion.dwBuildNumber);
849 msi_set_property(package->db, szWindowsBuild, verstr);
850 /* just fudge this */
851 msi_set_property(package->db, szServicePackLevel, szSix);
853 sprintfW( bufstr, szFormat2, MSI_MAJORVERSION, MSI_MINORVERSION);
854 msi_set_property( package->db, szVersionMsi, bufstr );
855 sprintfW( bufstr, szFormat, MSI_MAJORVERSION * 100);
856 msi_set_property( package->db, szVersionDatabase, bufstr );
858 GetNativeSystemInfo( &sys_info );
859 sprintfW( bufstr, szIntFormat, sys_info.wProcessorLevel );
860 if (sys_info.u.s.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL)
862 msi_set_property( package->db, szIntel, bufstr );
864 GetSystemDirectoryW( pth, MAX_PATH );
865 PathAddBackslashW( pth );
866 msi_set_property( package->db, szSystemFolder, pth );
868 SHGetFolderPathW( NULL, CSIDL_PROGRAM_FILES, NULL, 0, pth );
869 PathAddBackslashW( pth );
870 msi_set_property( package->db, szProgramFilesFolder, pth );
872 SHGetFolderPathW( NULL, CSIDL_PROGRAM_FILES_COMMON, NULL, 0, pth );
873 PathAddBackslashW( pth );
874 msi_set_property( package->db, szCommonFilesFolder, pth );
876 else if (sys_info.u.s.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
878 msi_set_property( package->db, szMsiAMD64, bufstr );
879 msi_set_property( package->db, szMsix64, bufstr );
880 msi_set_property( package->db, szVersionNT64, verstr );
882 GetSystemDirectoryW( pth, MAX_PATH );
883 PathAddBackslashW( pth );
884 msi_set_property( package->db, szSystem64Folder, pth );
886 GetSystemWow64DirectoryW( pth, MAX_PATH );
887 PathAddBackslashW( pth );
888 msi_set_property( package->db, szSystemFolder, pth );
890 SHGetFolderPathW( NULL, CSIDL_PROGRAM_FILES, NULL, 0, pth );
891 PathAddBackslashW( pth );
892 msi_set_property( package->db, szProgramFiles64Folder, pth );
894 SHGetFolderPathW( NULL, CSIDL_PROGRAM_FILESX86, NULL, 0, pth );
895 PathAddBackslashW( pth );
896 msi_set_property( package->db, szProgramFilesFolder, pth );
898 SHGetFolderPathW( NULL, CSIDL_PROGRAM_FILES_COMMON, NULL, 0, pth );
899 PathAddBackslashW( pth );
900 msi_set_property( package->db, szCommonFiles64Folder, pth );
902 SHGetFolderPathW( NULL, CSIDL_PROGRAM_FILES_COMMONX86, NULL, 0, pth );
903 PathAddBackslashW( pth );
904 msi_set_property( package->db, szCommonFilesFolder, pth );
907 /* Screen properties. */
908 dc = GetDC(0);
909 sprintfW( bufstr, szIntFormat, GetDeviceCaps( dc, HORZRES ) );
910 msi_set_property( package->db, szScreenX, bufstr );
911 sprintfW( bufstr, szIntFormat, GetDeviceCaps( dc, VERTRES ));
912 msi_set_property( package->db, szScreenY, bufstr );
913 sprintfW( bufstr, szIntFormat, GetDeviceCaps( dc, BITSPIXEL ));
914 msi_set_property( package->db, szColorBits, bufstr );
915 ReleaseDC(0, dc);
917 /* USERNAME and COMPANYNAME */
918 username = msi_dup_property( package->db, szUSERNAME );
919 companyname = msi_dup_property( package->db, szCOMPANYNAME );
921 if ((!username || !companyname) &&
922 RegOpenKeyW( HKEY_CURRENT_USER, szUserInfo, &hkey ) == ERROR_SUCCESS)
924 if (!username &&
925 (username = msi_reg_get_val_str( hkey, szDefName )))
926 msi_set_property( package->db, szUSERNAME, username );
927 if (!companyname &&
928 (companyname = msi_reg_get_val_str( hkey, szDefCompany )))
929 msi_set_property( package->db, szCOMPANYNAME, companyname );
930 CloseHandle( hkey );
932 if ((!username || !companyname) &&
933 RegOpenKeyW( HKEY_LOCAL_MACHINE, szCurrentVersion, &hkey ) == ERROR_SUCCESS)
935 if (!username &&
936 (username = msi_reg_get_val_str( hkey, szRegisteredUser )))
937 msi_set_property( package->db, szUSERNAME, username );
938 if (!companyname &&
939 (companyname = msi_reg_get_val_str( hkey, szRegisteredOrganization )))
940 msi_set_property( package->db, szCOMPANYNAME, companyname );
941 CloseHandle( hkey );
943 msi_free( username );
944 msi_free( companyname );
946 if ( set_user_sid_prop( package ) != ERROR_SUCCESS)
947 ERR("Failed to set the UserSID property\n");
949 /* Date and time properties */
950 GetSystemTime( &systemtime );
951 if (GetDateFormatW( LOCALE_USER_DEFAULT, DATE_SHORTDATE, &systemtime,
952 NULL, bufstr, sizeof(bufstr)/sizeof(bufstr[0]) ))
953 msi_set_property( package->db, szDate, bufstr );
954 else
955 ERR("Couldn't set Date property: GetDateFormat failed with error %d\n", GetLastError());
957 if (GetTimeFormatW( LOCALE_USER_DEFAULT,
958 TIME_FORCE24HOURFORMAT | TIME_NOTIMEMARKER,
959 &systemtime, NULL, bufstr,
960 sizeof(bufstr)/sizeof(bufstr[0]) ))
961 msi_set_property( package->db, szTime, bufstr );
962 else
963 ERR("Couldn't set Time property: GetTimeFormat failed with error %d\n", GetLastError());
965 set_msi_assembly_prop( package );
967 langid = GetUserDefaultLangID();
968 sprintfW(bufstr, szIntFormat, langid);
969 msi_set_property( package->db, szUserLanguageID, bufstr );
971 langid = GetSystemDefaultLangID();
972 sprintfW(bufstr, szIntFormat, langid);
973 msi_set_property( package->db, szSystemLangID, bufstr );
975 sprintfW(bufstr, szIntFormat, MsiQueryProductStateW(package->ProductCode));
976 msi_set_property( package->db, szProductState, bufstr );
978 len = 0;
979 if (!GetUserNameW( NULL, &len ) && GetLastError() == ERROR_MORE_DATA)
981 WCHAR *username;
982 if ((username = msi_alloc( len * sizeof(WCHAR) )))
984 if (GetUserNameW( username, &len ))
985 msi_set_property( package->db, szLogonUser, username );
986 msi_free( username );
991 static UINT msi_load_summary_properties( MSIPACKAGE *package )
993 UINT rc;
994 MSIHANDLE suminfo;
995 MSIHANDLE hdb = alloc_msihandle( &package->db->hdr );
996 INT count;
997 DWORD len;
998 LPWSTR package_code;
999 static const WCHAR szPackageCode[] = {
1000 'P','a','c','k','a','g','e','C','o','d','e',0};
1002 if (!hdb) {
1003 ERR("Unable to allocate handle\n");
1004 return ERROR_OUTOFMEMORY;
1007 rc = MsiGetSummaryInformationW( hdb, NULL, 0, &suminfo );
1008 MsiCloseHandle(hdb);
1009 if (rc != ERROR_SUCCESS)
1011 ERR("Unable to open Summary Information\n");
1012 return rc;
1015 rc = MsiSummaryInfoGetPropertyW( suminfo, PID_PAGECOUNT, NULL,
1016 &count, NULL, NULL, NULL );
1017 if (rc != ERROR_SUCCESS)
1019 WARN("Unable to query page count: %d\n", rc);
1020 goto done;
1023 /* load package code property */
1024 len = 0;
1025 rc = MsiSummaryInfoGetPropertyW( suminfo, PID_REVNUMBER, NULL,
1026 NULL, NULL, NULL, &len );
1027 if (rc != ERROR_MORE_DATA)
1029 WARN("Unable to query revision number: %d\n", rc);
1030 rc = ERROR_FUNCTION_FAILED;
1031 goto done;
1034 len++;
1035 package_code = msi_alloc( len * sizeof(WCHAR) );
1036 rc = MsiSummaryInfoGetPropertyW( suminfo, PID_REVNUMBER, NULL,
1037 NULL, NULL, package_code, &len );
1038 if (rc != ERROR_SUCCESS)
1040 WARN("Unable to query rev number: %d\n", rc);
1041 goto done;
1044 msi_set_property( package->db, szPackageCode, package_code );
1045 msi_free( package_code );
1047 /* load package attributes */
1048 count = 0;
1049 MsiSummaryInfoGetPropertyW( suminfo, PID_WORDCOUNT, NULL,
1050 &count, NULL, NULL, NULL );
1051 package->WordCount = count;
1053 done:
1054 MsiCloseHandle(suminfo);
1055 return rc;
1058 static MSIPACKAGE *msi_alloc_package( void )
1060 MSIPACKAGE *package;
1062 package = alloc_msiobject( MSIHANDLETYPE_PACKAGE, sizeof (MSIPACKAGE),
1063 MSI_FreePackage );
1064 if( package )
1066 list_init( &package->components );
1067 list_init( &package->features );
1068 list_init( &package->files );
1069 list_init( &package->filepatches );
1070 list_init( &package->tempfiles );
1071 list_init( &package->folders );
1072 list_init( &package->subscriptions );
1073 list_init( &package->appids );
1074 list_init( &package->classes );
1075 list_init( &package->mimes );
1076 list_init( &package->extensions );
1077 list_init( &package->progids );
1078 list_init( &package->RunningActions );
1079 list_init( &package->sourcelist_info );
1080 list_init( &package->sourcelist_media );
1081 list_init( &package->patches );
1082 list_init( &package->binaries );
1085 return package;
1088 static UINT msi_load_admin_properties(MSIPACKAGE *package)
1090 BYTE *data;
1091 UINT r, sz;
1093 static const WCHAR stmname[] = {'A','d','m','i','n','P','r','o','p','e','r','t','i','e','s',0};
1095 r = read_stream_data(package->db->storage, stmname, FALSE, &data, &sz);
1096 if (r != ERROR_SUCCESS)
1097 return r;
1099 r = msi_parse_command_line(package, (WCHAR *)data, TRUE);
1101 msi_free(data);
1102 return r;
1105 void msi_adjust_privilege_properties( MSIPACKAGE *package )
1107 /* FIXME: this should depend on the user's privileges */
1108 if (msi_get_property_int( package->db, szAllUsers, 0 ) == 2)
1110 TRACE("resetting ALLUSERS property from 2 to 1\n");
1111 msi_set_property( package->db, szAllUsers, szOne );
1113 msi_set_property( package->db, szAdminUser, szOne );
1116 MSIPACKAGE *MSI_CreatePackage( MSIDATABASE *db, LPCWSTR base_url )
1118 static const WCHAR szLevel[] = { 'U','I','L','e','v','e','l',0 };
1119 static const WCHAR szpi[] = {'%','i',0};
1120 MSIPACKAGE *package;
1121 WCHAR uilevel[10];
1122 UINT r;
1124 TRACE("%p\n", db);
1126 package = msi_alloc_package();
1127 if (package)
1129 msiobj_addref( &db->hdr );
1130 package->db = db;
1132 package->WordCount = 0;
1133 package->PackagePath = strdupW( db->path );
1134 package->BaseURL = strdupW( base_url );
1136 create_temp_property_table( package );
1137 msi_clone_properties( package );
1138 msi_adjust_privilege_properties( package );
1140 package->ProductCode = msi_dup_property( package->db, szProductCode );
1141 package->script = msi_alloc_zero( sizeof(MSISCRIPT) );
1143 set_installed_prop( package );
1144 set_installer_properties( package );
1146 sprintfW(uilevel,szpi,gUILevel);
1147 msi_set_property(package->db, szLevel, uilevel);
1149 r = msi_load_summary_properties( package );
1150 if (r != ERROR_SUCCESS)
1152 msiobj_release( &package->hdr );
1153 return NULL;
1156 if (package->WordCount & msidbSumInfoSourceTypeAdminImage)
1157 msi_load_admin_properties( package );
1159 package->log_file = INVALID_HANDLE_VALUE;
1162 return package;
1166 * copy_package_to_temp [internal]
1168 * copy the msi file to a temp file to prevent locking a CD
1169 * with a multi disc install
1171 * FIXME: I think this is wrong, and instead of copying the package,
1172 * we should read all the tables to memory, then open the
1173 * database to read binary streams on demand.
1175 static UINT copy_package_to_temp( LPCWSTR szPackage, LPWSTR filename )
1177 WCHAR path[MAX_PATH];
1179 GetTempPathW( MAX_PATH, path );
1180 GetTempFileNameW( path, szMsi, 0, filename );
1182 if( !CopyFileW( szPackage, filename, FALSE ) )
1184 UINT error = GetLastError();
1185 if ( error == ERROR_FILE_NOT_FOUND )
1186 ERR("can't find %s\n", debugstr_w(szPackage));
1187 else
1188 ERR("failed to copy package %s to %s (%u)\n", debugstr_w(szPackage), debugstr_w(filename), error);
1189 DeleteFileW( filename );
1190 return error;
1193 return ERROR_SUCCESS;
1196 UINT msi_download_file( LPCWSTR szUrl, LPWSTR filename )
1198 LPINTERNET_CACHE_ENTRY_INFOW cache_entry;
1199 DWORD size = 0;
1200 HRESULT hr;
1202 /* call will always fail, becase size is 0,
1203 * but will return ERROR_FILE_NOT_FOUND first
1204 * if the file doesn't exist
1206 GetUrlCacheEntryInfoW( szUrl, NULL, &size );
1207 if ( GetLastError() != ERROR_FILE_NOT_FOUND )
1209 cache_entry = msi_alloc( size );
1210 if ( !GetUrlCacheEntryInfoW( szUrl, cache_entry, &size ) )
1212 UINT error = GetLastError();
1213 msi_free( cache_entry );
1214 return error;
1217 lstrcpyW( filename, cache_entry->lpszLocalFileName );
1218 msi_free( cache_entry );
1219 return ERROR_SUCCESS;
1222 hr = URLDownloadToCacheFileW( NULL, szUrl, filename, MAX_PATH, 0, NULL );
1223 if ( FAILED(hr) )
1225 WARN("failed to download %s to cache file\n", debugstr_w(szUrl));
1226 return ERROR_FUNCTION_FAILED;
1229 return ERROR_SUCCESS;
1232 UINT msi_get_local_package_name( LPWSTR path, LPCWSTR suffix )
1234 static const WCHAR szInstaller[] = {
1235 '\\','I','n','s','t','a','l','l','e','r','\\',0};
1236 static const WCHAR fmt[] = {'%','x',0};
1237 DWORD time, len, i, offset;
1238 HANDLE handle;
1240 time = GetTickCount();
1241 GetWindowsDirectoryW( path, MAX_PATH );
1242 strcatW( path, szInstaller );
1243 CreateDirectoryW( path, NULL );
1245 len = strlenW(path);
1246 for (i = 0; i < 0x10000; i++)
1248 offset = snprintfW( path + len, MAX_PATH - len, fmt, (time + i) & 0xffff );
1249 memcpy( path + len + offset, suffix, (strlenW( suffix ) + 1) * sizeof(WCHAR) );
1250 handle = CreateFileW( path, GENERIC_WRITE, 0, NULL,
1251 CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0 );
1252 if (handle != INVALID_HANDLE_VALUE)
1254 CloseHandle(handle);
1255 break;
1257 if (GetLastError() != ERROR_FILE_EXISTS &&
1258 GetLastError() != ERROR_SHARING_VIOLATION)
1259 return ERROR_FUNCTION_FAILED;
1262 return ERROR_SUCCESS;
1265 static UINT apply_registered_patch( MSIPACKAGE *package, LPCWSTR patch_code )
1267 UINT r;
1268 DWORD len;
1269 WCHAR patch_file[MAX_PATH];
1270 MSIDATABASE *patch_db;
1271 MSIPATCHINFO *patch_info;
1272 MSISUMMARYINFO *si;
1274 len = sizeof(patch_file) / sizeof(WCHAR);
1275 r = MsiGetPatchInfoExW( patch_code, package->ProductCode, NULL, package->Context,
1276 INSTALLPROPERTY_LOCALPACKAGEW, patch_file, &len );
1277 if (r != ERROR_SUCCESS)
1279 ERR("failed to get patch filename %u\n", r);
1280 return r;
1283 r = MSI_OpenDatabaseW( patch_file, MSIDBOPEN_READONLY + MSIDBOPEN_PATCHFILE, &patch_db );
1284 if (r != ERROR_SUCCESS)
1286 ERR("failed to open patch database %s\n", debugstr_w( patch_file ));
1287 return r;
1290 si = MSI_GetSummaryInformationW( patch_db->storage, 0 );
1291 if (!si)
1293 msiobj_release( &patch_db->hdr );
1294 return ERROR_FUNCTION_FAILED;
1297 r = msi_parse_patch_summary( si, &patch_info );
1298 msiobj_release( &si->hdr );
1299 if (r != ERROR_SUCCESS)
1301 ERR("failed to parse patch summary %u\n", r);
1302 msiobj_release( &patch_db->hdr );
1303 return r;
1306 patch_info->localfile = strdupW( patch_file );
1307 if (!patch_info->localfile)
1309 msiobj_release( &patch_db->hdr );
1310 return ERROR_OUTOFMEMORY;
1313 r = msi_apply_patch_db( package, patch_db, patch_info );
1314 msiobj_release( &patch_db->hdr );
1315 if (r != ERROR_SUCCESS)
1317 ERR("failed to apply patch %u\n", r);
1318 msi_free( patch_info->patchcode );
1319 msi_free( patch_info->transforms );
1320 msi_free( patch_info->localfile );
1321 msi_free( patch_info );
1323 return r;
1326 static UINT msi_parse_summary( MSISUMMARYINFO *si, MSIPACKAGE *package )
1328 WCHAR *template, *p, *q;
1329 DWORD i, count;
1331 package->version = msi_suminfo_get_int32( si, PID_PAGECOUNT );
1332 TRACE("version: %d\n", package->version);
1334 template = msi_suminfo_dup_string( si, PID_TEMPLATE );
1335 if (!template)
1336 return ERROR_SUCCESS; /* native accepts missing template property */
1338 TRACE("template: %s\n", debugstr_w(template));
1340 p = strchrW( template, ';' );
1341 if (!p)
1343 WARN("invalid template string %s\n", debugstr_w(template));
1344 msi_free( template );
1345 return ERROR_PATCH_PACKAGE_INVALID;
1347 *p = 0;
1348 if (!template[0] || !strcmpW( template, szIntel ))
1349 package->platform = PLATFORM_INTEL;
1350 else if (!strcmpW( template, szIntel64 ))
1351 package->platform = PLATFORM_INTEL64;
1352 else if (!strcmpW( template, szX64 ) || !strcmpW( template, szAMD64 ))
1353 package->platform = PLATFORM_X64;
1354 else
1356 WARN("unknown platform %s\n", debugstr_w(template));
1357 msi_free( template );
1358 return ERROR_INSTALL_PLATFORM_UNSUPPORTED;
1360 p++;
1361 if (!*p)
1363 msi_free( template );
1364 return ERROR_SUCCESS;
1366 count = 1;
1367 for (q = p; (q = strchrW( q, ',' )); q++) count++;
1369 package->langids = msi_alloc( count * sizeof(LANGID) );
1370 if (!package->langids)
1372 msi_free( template );
1373 return ERROR_OUTOFMEMORY;
1376 i = 0;
1377 while (*p)
1379 q = strchrW( p, ',' );
1380 if (q) *q = 0;
1381 package->langids[i] = atoiW( p );
1382 if (!q) break;
1383 p = q + 1;
1384 i++;
1386 package->num_langids = i + 1;
1388 msi_free( template );
1389 return ERROR_SUCCESS;
1392 static UINT validate_package( MSIPACKAGE *package )
1394 BOOL is_wow64;
1395 UINT i;
1397 IsWow64Process( GetCurrentProcess(), &is_wow64 );
1398 if (package->platform == PLATFORM_X64)
1400 if (!is_64bit && !is_wow64)
1401 return ERROR_INSTALL_PLATFORM_UNSUPPORTED;
1402 if (package->version < 200)
1403 return ERROR_INSTALL_PACKAGE_INVALID;
1405 if (!package->num_langids)
1407 return ERROR_SUCCESS;
1409 for (i = 0; i < package->num_langids; i++)
1411 LANGID langid = package->langids[i];
1413 if (PRIMARYLANGID( langid ) == LANG_NEUTRAL)
1415 langid = MAKELANGID( PRIMARYLANGID( GetSystemDefaultLangID() ), SUBLANGID( langid ) );
1417 if (SUBLANGID( langid ) == SUBLANG_NEUTRAL)
1419 langid = MAKELANGID( PRIMARYLANGID( langid ), SUBLANGID( GetSystemDefaultLangID() ) );
1421 if (IsValidLocale( langid, LCID_INSTALLED ))
1422 return ERROR_SUCCESS;
1424 return ERROR_INSTALL_LANGUAGE_UNSUPPORTED;
1427 UINT MSI_OpenPackageW(LPCWSTR szPackage, MSIPACKAGE **pPackage)
1429 static const WCHAR Database[] = {'D','A','T','A','B','A','S','E',0};
1430 static const WCHAR dotmsi[] = {'.','m','s','i',0};
1431 MSIDATABASE *db = NULL;
1432 MSIPACKAGE *package;
1433 MSIHANDLE handle;
1434 LPWSTR ptr, base_url = NULL;
1435 UINT r;
1436 WCHAR temppath[MAX_PATH], localfile[MAX_PATH], cachefile[MAX_PATH];
1437 LPCWSTR file = szPackage;
1438 DWORD index = 0;
1439 MSISUMMARYINFO *si;
1441 TRACE("%s %p\n", debugstr_w(szPackage), pPackage);
1443 if( szPackage[0] == '#' )
1445 handle = atoiW(&szPackage[1]);
1446 db = msihandle2msiinfo( handle, MSIHANDLETYPE_DATABASE );
1447 if( !db )
1449 IWineMsiRemoteDatabase *remote_database;
1451 remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( handle );
1452 if ( !remote_database )
1453 return ERROR_INVALID_HANDLE;
1455 IWineMsiRemoteDatabase_Release( remote_database );
1456 WARN("MsiOpenPackage not allowed during a custom action!\n");
1458 return ERROR_FUNCTION_FAILED;
1461 else
1463 if ( UrlIsW( szPackage, URLIS_URL ) )
1465 r = msi_download_file( szPackage, cachefile );
1466 if ( r != ERROR_SUCCESS )
1467 return r;
1469 r = copy_package_to_temp( cachefile, temppath );
1470 if ( r != ERROR_SUCCESS )
1471 return r;
1473 file = temppath;
1475 base_url = strdupW( szPackage );
1476 if ( !base_url )
1477 return ERROR_OUTOFMEMORY;
1479 ptr = strrchrW( base_url, '/' );
1480 if (ptr) *(ptr + 1) = '\0';
1482 else
1484 r = copy_package_to_temp( szPackage, temppath );
1485 if ( r != ERROR_SUCCESS )
1486 return r;
1488 file = temppath;
1491 r = msi_get_local_package_name( localfile, dotmsi );
1492 if (r != ERROR_SUCCESS)
1493 return r;
1495 TRACE("Copying to local package %s\n", debugstr_w(localfile));
1497 if (!CopyFileW( file, localfile, FALSE ))
1499 ERR("Unable to copy package (%s -> %s) (error %u)\n",
1500 debugstr_w(file), debugstr_w(localfile), GetLastError());
1501 return GetLastError();
1504 TRACE("Opening relocated package %s\n", debugstr_w( file ));
1506 /* transforms that add binary streams require that we open the database
1507 * read/write, which is safe because we always create a copy that is thrown
1508 * away when we're done.
1510 r = MSI_OpenDatabaseW( file, MSIDBOPEN_TRANSACT, &db );
1511 if( r != ERROR_SUCCESS )
1513 if (file != szPackage)
1514 DeleteFileW( file );
1516 if (GetFileAttributesW(szPackage) == INVALID_FILE_ATTRIBUTES)
1517 return ERROR_FILE_NOT_FOUND;
1519 return r;
1522 db->localfile = strdupW( localfile );
1525 package = MSI_CreatePackage( db, base_url );
1526 msi_free( base_url );
1527 msiobj_release( &db->hdr );
1528 if( !package )
1530 if (file != szPackage)
1531 DeleteFileW( file );
1533 return ERROR_INSTALL_PACKAGE_INVALID;
1536 if( file != szPackage )
1537 track_tempfile( package, file );
1539 si = MSI_GetSummaryInformationW( db->storage, 0 );
1540 if (!si)
1542 WARN("failed to load summary info %u\n", r);
1543 msiobj_release( &package->hdr );
1544 return ERROR_INSTALL_PACKAGE_INVALID;
1547 r = msi_parse_summary( si, package );
1548 msiobj_release( &si->hdr );
1549 if (r != ERROR_SUCCESS)
1551 WARN("failed to parse summary info %u\n", r);
1552 msiobj_release( &package->hdr );
1553 return r;
1556 r = validate_package( package );
1557 if (r != ERROR_SUCCESS)
1559 msiobj_release( &package->hdr );
1560 return r;
1562 msi_set_property( package->db, Database, db->path );
1564 if( UrlIsW( szPackage, URLIS_URL ) )
1565 msi_set_property( package->db, szOriginalDatabase, szPackage );
1566 else if( szPackage[0] == '#' )
1567 msi_set_property( package->db, szOriginalDatabase, db->path );
1568 else
1570 WCHAR fullpath[MAX_PATH];
1572 GetFullPathNameW( szPackage, MAX_PATH, fullpath, NULL );
1573 msi_set_property( package->db, szOriginalDatabase, fullpath );
1576 msi_set_context( package );
1578 while (1)
1580 WCHAR patch_code[GUID_SIZE];
1581 r = MsiEnumPatchesExW( package->ProductCode, NULL, package->Context,
1582 MSIPATCHSTATE_APPLIED, index, patch_code, NULL, NULL, NULL, NULL );
1583 if (r != ERROR_SUCCESS)
1584 break;
1586 TRACE("found registered patch %s\n", debugstr_w(patch_code));
1588 r = apply_registered_patch( package, patch_code );
1589 if (r != ERROR_SUCCESS)
1591 ERR("registered patch failed to apply %u\n", r);
1592 msiobj_release( &package->hdr );
1593 return r;
1596 index++;
1599 if (index)
1601 msi_clone_properties( package );
1602 msi_adjust_privilege_properties( package );
1605 if (gszLogFile)
1606 package->log_file = CreateFileW( gszLogFile, GENERIC_WRITE, FILE_SHARE_WRITE, NULL,
1607 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
1609 *pPackage = package;
1610 return ERROR_SUCCESS;
1613 UINT WINAPI MsiOpenPackageExW(LPCWSTR szPackage, DWORD dwOptions, MSIHANDLE *phPackage)
1615 MSIPACKAGE *package = NULL;
1616 UINT ret;
1618 TRACE("%s %08x %p\n", debugstr_w(szPackage), dwOptions, phPackage );
1620 if( !szPackage || !phPackage )
1621 return ERROR_INVALID_PARAMETER;
1623 if ( !*szPackage )
1625 FIXME("Should create an empty database and package\n");
1626 return ERROR_FUNCTION_FAILED;
1629 if( dwOptions )
1630 FIXME("dwOptions %08x not supported\n", dwOptions);
1632 ret = MSI_OpenPackageW( szPackage, &package );
1633 if( ret == ERROR_SUCCESS )
1635 *phPackage = alloc_msihandle( &package->hdr );
1636 if (! *phPackage)
1637 ret = ERROR_NOT_ENOUGH_MEMORY;
1638 msiobj_release( &package->hdr );
1641 return ret;
1644 UINT WINAPI MsiOpenPackageW(LPCWSTR szPackage, MSIHANDLE *phPackage)
1646 return MsiOpenPackageExW( szPackage, 0, phPackage );
1649 UINT WINAPI MsiOpenPackageExA(LPCSTR szPackage, DWORD dwOptions, MSIHANDLE *phPackage)
1651 LPWSTR szwPack = NULL;
1652 UINT ret;
1654 if( szPackage )
1656 szwPack = strdupAtoW( szPackage );
1657 if( !szwPack )
1658 return ERROR_OUTOFMEMORY;
1661 ret = MsiOpenPackageExW( szwPack, dwOptions, phPackage );
1663 msi_free( szwPack );
1665 return ret;
1668 UINT WINAPI MsiOpenPackageA(LPCSTR szPackage, MSIHANDLE *phPackage)
1670 return MsiOpenPackageExA( szPackage, 0, phPackage );
1673 MSIHANDLE WINAPI MsiGetActiveDatabase(MSIHANDLE hInstall)
1675 MSIPACKAGE *package;
1676 MSIHANDLE handle = 0;
1677 IUnknown *remote_unk;
1678 IWineMsiRemotePackage *remote_package;
1680 TRACE("(%d)\n",hInstall);
1682 package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE);
1683 if( package)
1685 handle = alloc_msihandle( &package->db->hdr );
1686 msiobj_release( &package->hdr );
1688 else if ((remote_unk = msi_get_remote(hInstall)))
1690 if (IUnknown_QueryInterface(remote_unk, &IID_IWineMsiRemotePackage,
1691 (LPVOID *)&remote_package) == S_OK)
1693 IWineMsiRemotePackage_GetActiveDatabase(remote_package, &handle);
1694 IWineMsiRemotePackage_Release(remote_package);
1696 else
1698 WARN("remote handle %d is not a package\n", hInstall);
1700 IUnknown_Release(remote_unk);
1703 return handle;
1706 INT MSI_ProcessMessage( MSIPACKAGE *package, INSTALLMESSAGE eMessageType, MSIRECORD *record )
1708 static const WCHAR szActionData[] =
1709 {'A','c','t','i','o','n','D','a','t','a',0};
1710 static const WCHAR szSetProgress[] =
1711 {'S','e','t','P','r','o','g','r','e','s','s',0};
1712 static const WCHAR szActionText[] =
1713 {'A','c','t','i','o','n','T','e','x','t',0};
1714 LPWSTR message;
1715 DWORD i, len, total_len, log_type = 0;
1716 INT rc = 0;
1717 char *msg;
1719 TRACE("%x\n", eMessageType);
1721 if ((eMessageType & 0xff000000) == INSTALLMESSAGE_ERROR)
1722 log_type |= INSTALLLOGMODE_ERROR;
1723 if ((eMessageType & 0xff000000) == INSTALLMESSAGE_WARNING)
1724 log_type |= INSTALLLOGMODE_WARNING;
1725 if ((eMessageType & 0xff000000) == INSTALLMESSAGE_USER)
1726 log_type |= INSTALLLOGMODE_USER;
1727 if ((eMessageType & 0xff000000) == INSTALLMESSAGE_INFO)
1728 log_type |= INSTALLLOGMODE_INFO;
1729 if ((eMessageType & 0xff000000) == INSTALLMESSAGE_COMMONDATA)
1730 log_type |= INSTALLLOGMODE_COMMONDATA;
1731 if ((eMessageType & 0xff000000) == INSTALLMESSAGE_ACTIONSTART)
1732 log_type |= INSTALLLOGMODE_ACTIONSTART;
1733 if ((eMessageType & 0xff000000) == INSTALLMESSAGE_ACTIONDATA)
1734 log_type |= INSTALLLOGMODE_ACTIONDATA;
1735 /* just a guess */
1736 if ((eMessageType & 0xff000000) == INSTALLMESSAGE_PROGRESS)
1737 log_type |= 0x800;
1739 if ((eMessageType & 0xff000000) == INSTALLMESSAGE_ACTIONSTART)
1741 static const WCHAR template_s[]=
1742 {'A','c','t','i','o','n',' ','%','s',':',' ','%','s','.',' ',0};
1743 static const WCHAR format[] =
1744 {'H','H','\'',':','\'','m','m','\'',':','\'','s','s',0};
1745 WCHAR timet[0x100];
1746 LPCWSTR action_text, action;
1747 LPWSTR deformatted = NULL;
1749 GetTimeFormatW(LOCALE_USER_DEFAULT, 0, NULL, format, timet, 0x100);
1751 action = MSI_RecordGetString(record, 1);
1752 action_text = MSI_RecordGetString(record, 2);
1754 if (!action || !action_text)
1755 return IDOK;
1757 deformat_string(package, action_text, &deformatted);
1759 len = strlenW(timet) + strlenW(action) + strlenW(template_s);
1760 if (deformatted)
1761 len += strlenW(deformatted);
1762 message = msi_alloc(len*sizeof(WCHAR));
1763 sprintfW(message, template_s, timet, action);
1764 if (deformatted)
1765 strcatW(message, deformatted);
1766 msi_free(deformatted);
1768 else
1770 static const WCHAR format[] = {'%','u',':',' ',0};
1771 UINT count = MSI_RecordGetFieldCount( record );
1772 WCHAR *p;
1774 total_len = 1;
1775 for (i = 1; i <= count; i++)
1777 len = 0;
1778 MSI_RecordGetStringW( record, i, NULL, &len );
1779 total_len += len + 13;
1781 p = message = msi_alloc( total_len * sizeof(WCHAR) );
1782 if (!p) return ERROR_OUTOFMEMORY;
1784 for (i = 1; i <= count; i++)
1786 if (count > 1)
1788 len = sprintfW( p, format, i );
1789 total_len -= len;
1790 p += len;
1792 len = total_len;
1793 MSI_RecordGetStringW( record, i, p, &len );
1794 total_len -= len;
1795 p += len;
1796 if (count > 1 && total_len)
1798 *p++ = ' ';
1799 total_len--;
1802 p[0] = 0;
1805 TRACE("%p %p %p %x %x %s\n", gUIHandlerA, gUIHandlerW, gUIHandlerRecord,
1806 gUIFilter, log_type, debugstr_w(message));
1808 /* convert it to ASCII */
1809 len = WideCharToMultiByte( CP_ACP, 0, message, -1, NULL, 0, NULL, NULL );
1810 msg = msi_alloc( len );
1811 WideCharToMultiByte( CP_ACP, 0, message, -1, msg, len, NULL, NULL );
1813 if (gUIHandlerW && (gUIFilter & log_type))
1815 rc = gUIHandlerW( gUIContext, eMessageType, message );
1817 else if (gUIHandlerA && (gUIFilter & log_type))
1819 rc = gUIHandlerA( gUIContext, eMessageType, msg );
1821 else if (gUIHandlerRecord && (gUIFilter & log_type))
1823 MSIHANDLE rec = MsiCreateRecord( 1 );
1824 MsiRecordSetStringW( rec, 0, message );
1825 rc = gUIHandlerRecord( gUIContext, eMessageType, rec );
1826 MsiCloseHandle( rec );
1829 if (!rc && package->log_file != INVALID_HANDLE_VALUE &&
1830 (eMessageType & 0xff000000) != INSTALLMESSAGE_PROGRESS)
1832 DWORD written;
1833 WriteFile( package->log_file, msg, len - 1, &written, NULL );
1834 WriteFile( package->log_file, "\n", 1, &written, NULL );
1836 msi_free( msg );
1837 msi_free( message );
1839 switch (eMessageType & 0xff000000)
1841 case INSTALLMESSAGE_ACTIONDATA:
1842 /* FIXME: format record here instead of in ui_actiondata to get the
1843 * correct action data for external scripts */
1844 ControlEvent_FireSubscribedEvent(package, szActionData, record);
1845 break;
1846 case INSTALLMESSAGE_ACTIONSTART:
1848 MSIRECORD *uirow;
1849 LPWSTR deformated;
1850 LPCWSTR action_text = MSI_RecordGetString(record, 2);
1852 deformat_string(package, action_text, &deformated);
1853 uirow = MSI_CreateRecord(1);
1854 MSI_RecordSetStringW(uirow, 1, deformated);
1855 TRACE("INSTALLMESSAGE_ACTIONSTART: %s\n", debugstr_w(deformated));
1856 msi_free(deformated);
1858 ControlEvent_FireSubscribedEvent(package, szActionText, uirow);
1860 msiobj_release(&uirow->hdr);
1861 break;
1863 case INSTALLMESSAGE_PROGRESS:
1864 ControlEvent_FireSubscribedEvent(package, szSetProgress, record);
1865 break;
1868 return ERROR_SUCCESS;
1871 INT WINAPI MsiProcessMessage( MSIHANDLE hInstall, INSTALLMESSAGE eMessageType,
1872 MSIHANDLE hRecord)
1874 UINT ret = ERROR_INVALID_HANDLE;
1875 MSIPACKAGE *package = NULL;
1876 MSIRECORD *record = NULL;
1878 package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE );
1879 if( !package )
1881 HRESULT hr;
1882 IWineMsiRemotePackage *remote_package;
1884 remote_package = (IWineMsiRemotePackage *)msi_get_remote( hInstall );
1885 if (!remote_package)
1886 return ERROR_INVALID_HANDLE;
1888 hr = IWineMsiRemotePackage_ProcessMessage( remote_package, eMessageType, hRecord );
1890 IWineMsiRemotePackage_Release( remote_package );
1892 if (FAILED(hr))
1894 if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
1895 return HRESULT_CODE(hr);
1897 return ERROR_FUNCTION_FAILED;
1900 return ERROR_SUCCESS;
1903 record = msihandle2msiinfo( hRecord, MSIHANDLETYPE_RECORD );
1904 if( !record )
1905 goto out;
1907 ret = MSI_ProcessMessage( package, eMessageType, record );
1909 out:
1910 msiobj_release( &package->hdr );
1911 if( record )
1912 msiobj_release( &record->hdr );
1914 return ret;
1917 /* property code */
1919 UINT WINAPI MsiSetPropertyA( MSIHANDLE hInstall, LPCSTR szName, LPCSTR szValue )
1921 LPWSTR szwName = NULL, szwValue = NULL;
1922 UINT r = ERROR_OUTOFMEMORY;
1924 szwName = strdupAtoW( szName );
1925 if( szName && !szwName )
1926 goto end;
1928 szwValue = strdupAtoW( szValue );
1929 if( szValue && !szwValue )
1930 goto end;
1932 r = MsiSetPropertyW( hInstall, szwName, szwValue);
1934 end:
1935 msi_free( szwName );
1936 msi_free( szwValue );
1938 return r;
1941 void msi_reset_folders( MSIPACKAGE *package, BOOL source )
1943 MSIFOLDER *folder;
1945 LIST_FOR_EACH_ENTRY( folder, &package->folders, MSIFOLDER, entry )
1947 if ( source )
1949 msi_free( folder->ResolvedSource );
1950 folder->ResolvedSource = NULL;
1952 else
1954 msi_free( folder->ResolvedTarget );
1955 folder->ResolvedTarget = NULL;
1960 UINT msi_set_property( MSIDATABASE *db, LPCWSTR szName, LPCWSTR szValue )
1962 MSIQUERY *view;
1963 MSIRECORD *row = NULL;
1964 UINT rc;
1965 DWORD sz = 0;
1966 WCHAR Query[1024];
1968 static const WCHAR Insert[] = {
1969 'I','N','S','E','R','T',' ','i','n','t','o',' ',
1970 '`','_','P','r','o','p','e','r','t','y','`',' ','(',
1971 '`','_','P','r','o','p','e','r','t','y','`',',',
1972 '`','V','a','l','u','e','`',')',' ','V','A','L','U','E','S'
1973 ,' ','(','?',',','?',')',0};
1974 static const WCHAR Update[] = {
1975 'U','P','D','A','T','E',' ','`','_','P','r','o','p','e','r','t','y','`',
1976 ' ','s','e','t',' ','`','V','a','l','u','e','`',' ','=',' ','?',' ',
1977 'w','h','e','r','e',' ','`','_','P','r','o','p','e','r','t','y','`',
1978 ' ','=',' ','\'','%','s','\'',0};
1979 static const WCHAR Delete[] = {
1980 'D','E','L','E','T','E',' ','F','R','O','M',' ',
1981 '`','_','P','r','o','p','e','r','t','y','`',' ','W','H','E','R','E',' ',
1982 '`','_','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',0};
1984 TRACE("%p %s %s\n", db, debugstr_w(szName), debugstr_w(szValue));
1986 if (!szName)
1987 return ERROR_INVALID_PARAMETER;
1989 /* this one is weird... */
1990 if (!szName[0])
1991 return szValue ? ERROR_FUNCTION_FAILED : ERROR_SUCCESS;
1993 rc = msi_get_property(db, szName, 0, &sz);
1994 if (!szValue || !*szValue)
1996 sprintfW(Query, Delete, szName);
1998 else if (rc == ERROR_MORE_DATA || rc == ERROR_SUCCESS)
2000 sprintfW(Query, Update, szName);
2002 row = MSI_CreateRecord(1);
2003 MSI_RecordSetStringW(row, 1, szValue);
2005 else
2007 strcpyW(Query, Insert);
2009 row = MSI_CreateRecord(2);
2010 MSI_RecordSetStringW(row, 1, szName);
2011 MSI_RecordSetStringW(row, 2, szValue);
2014 rc = MSI_DatabaseOpenViewW(db, Query, &view);
2015 if (rc == ERROR_SUCCESS)
2017 rc = MSI_ViewExecute(view, row);
2018 MSI_ViewClose(view);
2019 msiobj_release(&view->hdr);
2022 if (row)
2023 msiobj_release(&row->hdr);
2025 return rc;
2028 UINT WINAPI MsiSetPropertyW( MSIHANDLE hInstall, LPCWSTR szName, LPCWSTR szValue)
2030 MSIPACKAGE *package;
2031 UINT ret;
2033 package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE);
2034 if( !package )
2036 HRESULT hr;
2037 BSTR name = NULL, value = NULL;
2038 IWineMsiRemotePackage *remote_package;
2040 remote_package = (IWineMsiRemotePackage *)msi_get_remote( hInstall );
2041 if (!remote_package)
2042 return ERROR_INVALID_HANDLE;
2044 name = SysAllocString( szName );
2045 value = SysAllocString( szValue );
2046 if ((!name && szName) || (!value && szValue))
2048 SysFreeString( name );
2049 SysFreeString( value );
2050 IWineMsiRemotePackage_Release( remote_package );
2051 return ERROR_OUTOFMEMORY;
2054 hr = IWineMsiRemotePackage_SetProperty( remote_package, name, value );
2056 SysFreeString( name );
2057 SysFreeString( value );
2058 IWineMsiRemotePackage_Release( remote_package );
2060 if (FAILED(hr))
2062 if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
2063 return HRESULT_CODE(hr);
2065 return ERROR_FUNCTION_FAILED;
2068 return ERROR_SUCCESS;
2071 ret = msi_set_property( package->db, szName, szValue );
2072 if (ret == ERROR_SUCCESS && !strcmpW( szName, cszSourceDir ))
2073 msi_reset_folders( package, TRUE );
2075 msiobj_release( &package->hdr );
2076 return ret;
2079 static MSIRECORD *msi_get_property_row( MSIDATABASE *db, LPCWSTR name )
2081 MSIQUERY *view;
2082 MSIRECORD *rec, *row = NULL;
2083 UINT r;
2085 static const WCHAR query[]= {
2086 'S','E','L','E','C','T',' ','`','V','a','l','u','e','`',' ',
2087 'F','R','O','M',' ' ,'`','_','P','r','o','p','e','r','t','y','`',
2088 ' ','W','H','E','R','E',' ' ,'`','_','P','r','o','p','e','r','t','y','`',
2089 '=','?',0};
2091 if (!name || !*name)
2092 return NULL;
2094 rec = MSI_CreateRecord(1);
2095 if (!rec)
2096 return NULL;
2098 MSI_RecordSetStringW(rec, 1, name);
2100 r = MSI_DatabaseOpenViewW(db, query, &view);
2101 if (r == ERROR_SUCCESS)
2103 MSI_ViewExecute(view, rec);
2104 MSI_ViewFetch(view, &row);
2105 MSI_ViewClose(view);
2106 msiobj_release(&view->hdr);
2109 msiobj_release(&rec->hdr);
2110 return row;
2113 /* internal function, not compatible with MsiGetPropertyW */
2114 UINT msi_get_property( MSIDATABASE *db, LPCWSTR szName,
2115 LPWSTR szValueBuf, LPDWORD pchValueBuf )
2117 MSIRECORD *row;
2118 UINT rc = ERROR_FUNCTION_FAILED;
2120 row = msi_get_property_row( db, szName );
2122 if (*pchValueBuf > 0)
2123 szValueBuf[0] = 0;
2125 if (row)
2127 rc = MSI_RecordGetStringW(row, 1, szValueBuf, pchValueBuf);
2128 msiobj_release(&row->hdr);
2131 if (rc == ERROR_SUCCESS)
2132 TRACE("returning %s for property %s\n", debugstr_w(szValueBuf),
2133 debugstr_w(szName));
2134 else if (rc == ERROR_MORE_DATA)
2135 TRACE("need %d sized buffer for %s\n", *pchValueBuf,
2136 debugstr_w(szName));
2137 else
2139 *pchValueBuf = 0;
2140 TRACE("property %s not found\n", debugstr_w(szName));
2143 return rc;
2146 LPWSTR msi_dup_property(MSIDATABASE *db, LPCWSTR prop)
2148 DWORD sz = 0;
2149 LPWSTR str;
2150 UINT r;
2152 r = msi_get_property(db, prop, NULL, &sz);
2153 if (r != ERROR_SUCCESS && r != ERROR_MORE_DATA)
2154 return NULL;
2156 sz++;
2157 str = msi_alloc(sz * sizeof(WCHAR));
2158 r = msi_get_property(db, prop, str, &sz);
2159 if (r != ERROR_SUCCESS)
2161 msi_free(str);
2162 str = NULL;
2165 return str;
2168 int msi_get_property_int( MSIDATABASE *db, LPCWSTR prop, int def )
2170 LPWSTR str = msi_dup_property( db, prop );
2171 int val = str ? atoiW(str) : def;
2172 msi_free(str);
2173 return val;
2176 static UINT MSI_GetProperty( MSIHANDLE handle, LPCWSTR name,
2177 awstring *szValueBuf, LPDWORD pchValueBuf )
2179 MSIPACKAGE *package;
2180 MSIRECORD *row = NULL;
2181 UINT r = ERROR_FUNCTION_FAILED;
2182 LPCWSTR val = NULL;
2184 TRACE("%u %s %p %p\n", handle, debugstr_w(name),
2185 szValueBuf->str.w, pchValueBuf );
2187 if (!name)
2188 return ERROR_INVALID_PARAMETER;
2190 package = msihandle2msiinfo( handle, MSIHANDLETYPE_PACKAGE );
2191 if (!package)
2193 HRESULT hr;
2194 IWineMsiRemotePackage *remote_package;
2195 LPWSTR value = NULL;
2196 BSTR bname;
2197 DWORD len;
2199 remote_package = (IWineMsiRemotePackage *)msi_get_remote( handle );
2200 if (!remote_package)
2201 return ERROR_INVALID_HANDLE;
2203 bname = SysAllocString( name );
2204 if (!bname)
2206 IWineMsiRemotePackage_Release( remote_package );
2207 return ERROR_OUTOFMEMORY;
2210 len = 0;
2211 hr = IWineMsiRemotePackage_GetProperty( remote_package, bname, NULL, &len );
2212 if (FAILED(hr))
2213 goto done;
2215 len++;
2216 value = msi_alloc(len * sizeof(WCHAR));
2217 if (!value)
2219 r = ERROR_OUTOFMEMORY;
2220 goto done;
2223 hr = IWineMsiRemotePackage_GetProperty( remote_package, bname, (BSTR *)value, &len );
2224 if (FAILED(hr))
2225 goto done;
2227 r = msi_strcpy_to_awstring( value, szValueBuf, pchValueBuf );
2229 /* Bug required by Adobe installers */
2230 if (!szValueBuf->unicode && !szValueBuf->str.a)
2231 *pchValueBuf *= sizeof(WCHAR);
2233 done:
2234 IWineMsiRemotePackage_Release(remote_package);
2235 SysFreeString(bname);
2236 msi_free(value);
2238 if (FAILED(hr))
2240 if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
2241 return HRESULT_CODE(hr);
2243 return ERROR_FUNCTION_FAILED;
2246 return r;
2249 row = msi_get_property_row( package->db, name );
2250 if (row)
2251 val = MSI_RecordGetString( row, 1 );
2253 if (!val)
2254 val = szEmpty;
2256 r = msi_strcpy_to_awstring( val, szValueBuf, pchValueBuf );
2258 if (row)
2259 msiobj_release( &row->hdr );
2260 msiobj_release( &package->hdr );
2262 return r;
2265 UINT WINAPI MsiGetPropertyA( MSIHANDLE hInstall, LPCSTR szName,
2266 LPSTR szValueBuf, LPDWORD pchValueBuf )
2268 awstring val;
2269 LPWSTR name;
2270 UINT r;
2272 val.unicode = FALSE;
2273 val.str.a = szValueBuf;
2275 name = strdupAtoW( szName );
2276 if (szName && !name)
2277 return ERROR_OUTOFMEMORY;
2279 r = MSI_GetProperty( hInstall, name, &val, pchValueBuf );
2280 msi_free( name );
2281 return r;
2284 UINT WINAPI MsiGetPropertyW( MSIHANDLE hInstall, LPCWSTR szName,
2285 LPWSTR szValueBuf, LPDWORD pchValueBuf )
2287 awstring val;
2289 val.unicode = TRUE;
2290 val.str.w = szValueBuf;
2292 return MSI_GetProperty( hInstall, szName, &val, pchValueBuf );
2295 typedef struct _msi_remote_package_impl {
2296 IWineMsiRemotePackage IWineMsiRemotePackage_iface;
2297 MSIHANDLE package;
2298 LONG refs;
2299 } msi_remote_package_impl;
2301 static inline msi_remote_package_impl *impl_from_IWineMsiRemotePackage( IWineMsiRemotePackage *iface )
2303 return CONTAINING_RECORD(iface, msi_remote_package_impl, IWineMsiRemotePackage_iface);
2306 static HRESULT WINAPI mrp_QueryInterface( IWineMsiRemotePackage *iface,
2307 REFIID riid,LPVOID *ppobj)
2309 if( IsEqualCLSID( riid, &IID_IUnknown ) ||
2310 IsEqualCLSID( riid, &IID_IWineMsiRemotePackage ) )
2312 IUnknown_AddRef( iface );
2313 *ppobj = iface;
2314 return S_OK;
2317 return E_NOINTERFACE;
2320 static ULONG WINAPI mrp_AddRef( IWineMsiRemotePackage *iface )
2322 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2324 return InterlockedIncrement( &This->refs );
2327 static ULONG WINAPI mrp_Release( IWineMsiRemotePackage *iface )
2329 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2330 ULONG r;
2332 r = InterlockedDecrement( &This->refs );
2333 if (r == 0)
2335 MsiCloseHandle( This->package );
2336 msi_free( This );
2338 return r;
2341 static HRESULT WINAPI mrp_SetMsiHandle( IWineMsiRemotePackage *iface, MSIHANDLE handle )
2343 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2344 This->package = handle;
2345 return S_OK;
2348 static HRESULT WINAPI mrp_GetActiveDatabase( IWineMsiRemotePackage *iface, MSIHANDLE *handle )
2350 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2351 IWineMsiRemoteDatabase *rdb = NULL;
2352 HRESULT hr;
2353 MSIHANDLE hdb;
2355 hr = create_msi_remote_database( NULL, (LPVOID *)&rdb );
2356 if (FAILED(hr) || !rdb)
2358 ERR("Failed to create remote database\n");
2359 return hr;
2362 hdb = MsiGetActiveDatabase(This->package);
2364 hr = IWineMsiRemoteDatabase_SetMsiHandle( rdb, hdb );
2365 if (FAILED(hr))
2367 ERR("Failed to set the database handle\n");
2368 return hr;
2371 *handle = alloc_msi_remote_handle( (IUnknown *)rdb );
2372 return S_OK;
2375 static HRESULT WINAPI mrp_GetProperty( IWineMsiRemotePackage *iface, BSTR property, BSTR *value, DWORD *size )
2377 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2378 UINT r;
2380 r = MsiGetPropertyW(This->package, (LPWSTR)property, (LPWSTR)value, size);
2381 if (r != ERROR_SUCCESS)
2382 return HRESULT_FROM_WIN32(r);
2384 return S_OK;
2387 static HRESULT WINAPI mrp_SetProperty( IWineMsiRemotePackage *iface, BSTR property, BSTR value )
2389 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2390 UINT r = MsiSetPropertyW(This->package, property, value);
2391 return HRESULT_FROM_WIN32(r);
2394 static HRESULT WINAPI mrp_ProcessMessage( IWineMsiRemotePackage *iface, INSTALLMESSAGE message, MSIHANDLE record )
2396 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2397 UINT r = MsiProcessMessage(This->package, message, record);
2398 return HRESULT_FROM_WIN32(r);
2401 static HRESULT WINAPI mrp_DoAction( IWineMsiRemotePackage *iface, BSTR action )
2403 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2404 UINT r = MsiDoActionW(This->package, action);
2405 return HRESULT_FROM_WIN32(r);
2408 static HRESULT WINAPI mrp_Sequence( IWineMsiRemotePackage *iface, BSTR table, int sequence )
2410 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2411 UINT r = MsiSequenceW(This->package, table, sequence);
2412 return HRESULT_FROM_WIN32(r);
2415 static HRESULT WINAPI mrp_GetTargetPath( IWineMsiRemotePackage *iface, BSTR folder, BSTR *value, DWORD *size )
2417 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2418 UINT r = MsiGetTargetPathW(This->package, (LPWSTR)folder, (LPWSTR)value, size);
2419 return HRESULT_FROM_WIN32(r);
2422 static HRESULT WINAPI mrp_SetTargetPath( IWineMsiRemotePackage *iface, BSTR folder, BSTR value)
2424 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2425 UINT r = MsiSetTargetPathW(This->package, folder, value);
2426 return HRESULT_FROM_WIN32(r);
2429 static HRESULT WINAPI mrp_GetSourcePath( IWineMsiRemotePackage *iface, BSTR folder, BSTR *value, DWORD *size )
2431 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2432 UINT r = MsiGetSourcePathW(This->package, (LPWSTR)folder, (LPWSTR)value, size);
2433 return HRESULT_FROM_WIN32(r);
2436 static HRESULT WINAPI mrp_GetMode( IWineMsiRemotePackage *iface, MSIRUNMODE mode, BOOL *ret )
2438 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2439 *ret = MsiGetMode(This->package, mode);
2440 return S_OK;
2443 static HRESULT WINAPI mrp_SetMode( IWineMsiRemotePackage *iface, MSIRUNMODE mode, BOOL state )
2445 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2446 UINT r = MsiSetMode(This->package, mode, state);
2447 return HRESULT_FROM_WIN32(r);
2450 static HRESULT WINAPI mrp_GetFeatureState( IWineMsiRemotePackage *iface, BSTR feature,
2451 INSTALLSTATE *installed, INSTALLSTATE *action )
2453 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2454 UINT r = MsiGetFeatureStateW(This->package, feature, installed, action);
2455 return HRESULT_FROM_WIN32(r);
2458 static HRESULT WINAPI mrp_SetFeatureState( IWineMsiRemotePackage *iface, BSTR feature, INSTALLSTATE state )
2460 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2461 UINT r = MsiSetFeatureStateW(This->package, feature, state);
2462 return HRESULT_FROM_WIN32(r);
2465 static HRESULT WINAPI mrp_GetComponentState( IWineMsiRemotePackage *iface, BSTR component,
2466 INSTALLSTATE *installed, INSTALLSTATE *action )
2468 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2469 UINT r = MsiGetComponentStateW(This->package, component, installed, action);
2470 return HRESULT_FROM_WIN32(r);
2473 static HRESULT WINAPI mrp_SetComponentState( IWineMsiRemotePackage *iface, BSTR component, INSTALLSTATE state )
2475 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2476 UINT r = MsiSetComponentStateW(This->package, component, state);
2477 return HRESULT_FROM_WIN32(r);
2480 static HRESULT WINAPI mrp_GetLanguage( IWineMsiRemotePackage *iface, LANGID *language )
2482 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2483 *language = MsiGetLanguage(This->package);
2484 return S_OK;
2487 static HRESULT WINAPI mrp_SetInstallLevel( IWineMsiRemotePackage *iface, int level )
2489 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2490 UINT r = MsiSetInstallLevel(This->package, level);
2491 return HRESULT_FROM_WIN32(r);
2494 static HRESULT WINAPI mrp_FormatRecord( IWineMsiRemotePackage *iface, MSIHANDLE record,
2495 BSTR *value)
2497 DWORD size = 0;
2498 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2499 UINT r = MsiFormatRecordW(This->package, record, NULL, &size);
2500 if (r == ERROR_SUCCESS)
2502 *value = SysAllocStringLen(NULL, size);
2503 if (!*value)
2504 return E_OUTOFMEMORY;
2505 size++;
2506 r = MsiFormatRecordW(This->package, record, *value, &size);
2508 return HRESULT_FROM_WIN32(r);
2511 static HRESULT WINAPI mrp_EvaluateCondition( IWineMsiRemotePackage *iface, BSTR condition )
2513 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2514 UINT r = MsiEvaluateConditionW(This->package, condition);
2515 return HRESULT_FROM_WIN32(r);
2518 static HRESULT WINAPI mrp_GetFeatureCost( IWineMsiRemotePackage *iface, BSTR feature,
2519 INT cost_tree, INSTALLSTATE state, INT *cost )
2521 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2522 UINT r = MsiGetFeatureCostW(This->package, feature, cost_tree, state, cost);
2523 return HRESULT_FROM_WIN32(r);
2526 static const IWineMsiRemotePackageVtbl msi_remote_package_vtbl =
2528 mrp_QueryInterface,
2529 mrp_AddRef,
2530 mrp_Release,
2531 mrp_SetMsiHandle,
2532 mrp_GetActiveDatabase,
2533 mrp_GetProperty,
2534 mrp_SetProperty,
2535 mrp_ProcessMessage,
2536 mrp_DoAction,
2537 mrp_Sequence,
2538 mrp_GetTargetPath,
2539 mrp_SetTargetPath,
2540 mrp_GetSourcePath,
2541 mrp_GetMode,
2542 mrp_SetMode,
2543 mrp_GetFeatureState,
2544 mrp_SetFeatureState,
2545 mrp_GetComponentState,
2546 mrp_SetComponentState,
2547 mrp_GetLanguage,
2548 mrp_SetInstallLevel,
2549 mrp_FormatRecord,
2550 mrp_EvaluateCondition,
2551 mrp_GetFeatureCost,
2554 HRESULT create_msi_remote_package( IUnknown *pOuter, LPVOID *ppObj )
2556 msi_remote_package_impl* This;
2558 This = msi_alloc( sizeof *This );
2559 if (!This)
2560 return E_OUTOFMEMORY;
2562 This->IWineMsiRemotePackage_iface.lpVtbl = &msi_remote_package_vtbl;
2563 This->package = 0;
2564 This->refs = 1;
2566 *ppObj = This;
2568 return S_OK;
2571 UINT msi_package_add_info(MSIPACKAGE *package, DWORD context, DWORD options,
2572 LPCWSTR property, LPWSTR value)
2574 MSISOURCELISTINFO *info;
2576 LIST_FOR_EACH_ENTRY( info, &package->sourcelist_info, MSISOURCELISTINFO, entry )
2578 if (!strcmpW( info->value, value )) return ERROR_SUCCESS;
2581 info = msi_alloc(sizeof(MSISOURCELISTINFO));
2582 if (!info)
2583 return ERROR_OUTOFMEMORY;
2585 info->context = context;
2586 info->options = options;
2587 info->property = property;
2588 info->value = strdupW(value);
2589 list_add_head(&package->sourcelist_info, &info->entry);
2591 return ERROR_SUCCESS;
2594 UINT msi_package_add_media_disk(MSIPACKAGE *package, DWORD context, DWORD options,
2595 DWORD disk_id, LPWSTR volume_label, LPWSTR disk_prompt)
2597 MSIMEDIADISK *disk;
2599 LIST_FOR_EACH_ENTRY( disk, &package->sourcelist_media, MSIMEDIADISK, entry )
2601 if (disk->disk_id == disk_id) return ERROR_SUCCESS;
2604 disk = msi_alloc(sizeof(MSIMEDIADISK));
2605 if (!disk)
2606 return ERROR_OUTOFMEMORY;
2608 disk->context = context;
2609 disk->options = options;
2610 disk->disk_id = disk_id;
2611 disk->volume_label = strdupW(volume_label);
2612 disk->disk_prompt = strdupW(disk_prompt);
2613 list_add_head(&package->sourcelist_media, &disk->entry);
2615 return ERROR_SUCCESS;