comctl32/tests: Initialize item mask on item insert (Valgrind).
[wine.git] / dlls / msi / package.c
blob5606d28a184d8891033f101f151e86ba2765d191
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 MSIPACKAGE *package= (MSIPACKAGE*) arg;
318 if( package->dialog )
319 msi_dialog_destroy( package->dialog );
321 msiobj_release( &package->db->hdr );
322 free_package_structures(package);
323 CloseHandle( package->log_file );
325 if (package->cache_net) IAssemblyCache_Release( package->cache_net );
326 if (package->cache_sxs) IAssemblyCache_Release( package->cache_sxs );
329 static UINT create_temp_property_table(MSIPACKAGE *package)
331 MSIQUERY *view = NULL;
332 UINT rc;
334 static const WCHAR CreateSql[] = {
335 'C','R','E','A','T','E',' ','T','A','B','L','E',' ',
336 '`','_','P','r','o','p','e','r','t','y','`',' ','(',' ',
337 '`','_','P','r','o','p','e','r','t','y','`',' ',
338 'C','H','A','R','(','5','6',')',' ','N','O','T',' ','N','U','L','L',' ',
339 'T','E','M','P','O','R','A','R','Y',',',' ',
340 '`','V','a','l','u','e','`',' ','C','H','A','R','(','9','8',')',' ',
341 'N','O','T',' ','N','U','L','L',' ','T','E','M','P','O','R','A','R','Y',
342 ' ','P','R','I','M','A','R','Y',' ','K','E','Y',' ',
343 '`','_','P','r','o','p','e','r','t','y','`',')',' ','H','O','L','D',0};
345 rc = MSI_DatabaseOpenViewW(package->db, CreateSql, &view);
346 if (rc != ERROR_SUCCESS)
347 return rc;
349 rc = MSI_ViewExecute(view, 0);
350 MSI_ViewClose(view);
351 msiobj_release(&view->hdr);
352 return rc;
355 UINT msi_clone_properties(MSIPACKAGE *package)
357 MSIQUERY *view_select = NULL;
358 UINT rc;
360 static const WCHAR query_select[] = {
361 'S','E','L','E','C','T',' ','*',' ',
362 'F','R','O','M',' ','`','P','r','o','p','e','r','t','y','`',0};
363 static const WCHAR query_insert[] = {
364 'I','N','S','E','R','T',' ','i','n','t','o',' ',
365 '`','_','P','r','o','p','e','r','t','y','`',' ',
366 '(','`','_','P','r','o','p','e','r','t','y','`',',',
367 '`','V','a','l','u','e','`',')',' ',
368 'V','A','L','U','E','S',' ','(','?',',','?',')',0};
369 static const WCHAR query_update[] = {
370 'U','P','D','A','T','E',' ','`','_','P','r','o','p','e','r','t','y','`',' ',
371 'S','E','T',' ','`','V','a','l','u','e','`',' ','=',' ','?',' ',
372 'W','H','E','R','E',' ','`','_','P','r','o','p','e','r','t','y','`',' ','=',' ','?',0};
374 rc = MSI_DatabaseOpenViewW( package->db, query_select, &view_select );
375 if (rc != ERROR_SUCCESS)
376 return rc;
378 rc = MSI_ViewExecute( view_select, 0 );
379 if (rc != ERROR_SUCCESS)
381 MSI_ViewClose( view_select );
382 msiobj_release( &view_select->hdr );
383 return rc;
386 while (1)
388 MSIQUERY *view_insert, *view_update;
389 MSIRECORD *rec_select;
391 rc = MSI_ViewFetch( view_select, &rec_select );
392 if (rc != ERROR_SUCCESS)
393 break;
395 rc = MSI_DatabaseOpenViewW( package->db, query_insert, &view_insert );
396 if (rc != ERROR_SUCCESS)
398 msiobj_release( &rec_select->hdr );
399 continue;
402 rc = MSI_ViewExecute( view_insert, rec_select );
403 MSI_ViewClose( view_insert );
404 msiobj_release( &view_insert->hdr );
405 if (rc != ERROR_SUCCESS)
407 MSIRECORD *rec_update;
409 TRACE("insert failed, trying update\n");
411 rc = MSI_DatabaseOpenViewW( package->db, query_update, &view_update );
412 if (rc != ERROR_SUCCESS)
414 WARN("open view failed %u\n", rc);
415 msiobj_release( &rec_select->hdr );
416 continue;
419 rec_update = MSI_CreateRecord( 2 );
420 MSI_RecordCopyField( rec_select, 1, rec_update, 2 );
421 MSI_RecordCopyField( rec_select, 2, rec_update, 1 );
422 rc = MSI_ViewExecute( view_update, rec_update );
423 if (rc != ERROR_SUCCESS)
424 WARN("update failed %u\n", rc);
426 MSI_ViewClose( view_update );
427 msiobj_release( &view_update->hdr );
428 msiobj_release( &rec_update->hdr );
431 msiobj_release( &rec_select->hdr );
434 MSI_ViewClose( view_select );
435 msiobj_release( &view_select->hdr );
436 return rc;
440 * set_installed_prop
442 * Sets the "Installed" property to indicate that
443 * the product is installed for the current user.
445 static UINT set_installed_prop( MSIPACKAGE *package )
447 HKEY hkey = 0;
448 UINT r;
450 r = MSIREG_OpenUninstallKey( package, &hkey, FALSE );
451 if (r == ERROR_SUCCESS)
453 RegCloseKey( hkey );
454 msi_set_property( package->db, szInstalled, szOne );
457 return r;
460 static UINT set_user_sid_prop( MSIPACKAGE *package )
462 SID_NAME_USE use;
463 LPWSTR user_name;
464 LPWSTR sid_str = NULL, dom = NULL;
465 DWORD size, dom_size;
466 PSID psid = NULL;
467 UINT r = ERROR_FUNCTION_FAILED;
469 size = 0;
470 GetUserNameW( NULL, &size );
472 user_name = msi_alloc( (size + 1) * sizeof(WCHAR) );
473 if (!user_name)
474 return ERROR_OUTOFMEMORY;
476 if (!GetUserNameW( user_name, &size ))
477 goto done;
479 size = 0;
480 dom_size = 0;
481 LookupAccountNameW( NULL, user_name, NULL, &size, NULL, &dom_size, &use );
483 psid = msi_alloc( size );
484 dom = msi_alloc( dom_size*sizeof (WCHAR) );
485 if (!psid || !dom)
487 r = ERROR_OUTOFMEMORY;
488 goto done;
491 if (!LookupAccountNameW( NULL, user_name, psid, &size, dom, &dom_size, &use ))
492 goto done;
494 if (!ConvertSidToStringSidW( psid, &sid_str ))
495 goto done;
497 r = msi_set_property( package->db, szUserSID, sid_str );
499 done:
500 LocalFree( sid_str );
501 msi_free( dom );
502 msi_free( psid );
503 msi_free( user_name );
505 return r;
508 static LPWSTR get_fusion_filename(MSIPACKAGE *package)
510 HKEY netsetup;
511 LONG res;
512 LPWSTR file = NULL;
513 DWORD index = 0, size;
514 WCHAR ver[MAX_PATH];
515 WCHAR name[MAX_PATH];
516 WCHAR windir[MAX_PATH];
518 static const WCHAR fusion[] = {'f','u','s','i','o','n','.','d','l','l',0};
519 static const WCHAR sub[] = {
520 'S','o','f','t','w','a','r','e','\\',
521 'M','i','c','r','o','s','o','f','t','\\',
522 'N','E','T',' ','F','r','a','m','e','w','o','r','k',' ','S','e','t','u','p','\\',
523 'N','D','P',0
525 static const WCHAR subdir[] = {
526 'M','i','c','r','o','s','o','f','t','.','N','E','T','\\',
527 'F','r','a','m','e','w','o','r','k','\\',0
530 res = RegOpenKeyExW(HKEY_LOCAL_MACHINE, sub, 0, KEY_ENUMERATE_SUB_KEYS, &netsetup);
531 if (res != ERROR_SUCCESS)
532 return NULL;
534 GetWindowsDirectoryW(windir, MAX_PATH);
536 ver[0] = '\0';
537 size = MAX_PATH;
538 while (RegEnumKeyExW(netsetup, index, name, &size, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
540 index++;
542 /* verify existence of fusion.dll .Net 3.0 does not install a new one */
543 if (strcmpW( ver, name ) < 0)
545 LPWSTR check;
546 size = lstrlenW(windir) + lstrlenW(subdir) + lstrlenW(name) +lstrlenW(fusion) + 3;
547 check = msi_alloc(size * sizeof(WCHAR));
549 if (!check)
551 msi_free(file);
552 return NULL;
555 lstrcpyW(check, windir);
556 lstrcatW(check, szBackSlash);
557 lstrcatW(check, subdir);
558 lstrcatW(check, name);
559 lstrcatW(check, szBackSlash);
560 lstrcatW(check, fusion);
562 if(GetFileAttributesW(check) != INVALID_FILE_ATTRIBUTES)
564 msi_free(file);
565 file = check;
566 lstrcpyW(ver, name);
568 else
569 msi_free(check);
573 RegCloseKey(netsetup);
574 return file;
577 typedef struct tagLANGANDCODEPAGE
579 WORD wLanguage;
580 WORD wCodePage;
581 } LANGANDCODEPAGE;
583 static void set_msi_assembly_prop(MSIPACKAGE *package)
585 UINT val_len;
586 DWORD size, handle;
587 LPVOID version = NULL;
588 WCHAR buf[MAX_PATH];
589 LPWSTR fusion, verstr;
590 LANGANDCODEPAGE *translate;
592 static const WCHAR netasm[] = {
593 'M','s','i','N','e','t','A','s','s','e','m','b','l','y','S','u','p','p','o','r','t',0
595 static const WCHAR translation[] = {
596 '\\','V','a','r','F','i','l','e','I','n','f','o',
597 '\\','T','r','a','n','s','l','a','t','i','o','n',0
599 static const WCHAR verfmt[] = {
600 '\\','S','t','r','i','n','g','F','i','l','e','I','n','f','o',
601 '\\','%','0','4','x','%','0','4','x',
602 '\\','P','r','o','d','u','c','t','V','e','r','s','i','o','n',0
605 fusion = get_fusion_filename(package);
606 if (!fusion)
607 return;
609 size = GetFileVersionInfoSizeW(fusion, &handle);
610 if (!size) return;
612 version = msi_alloc(size);
613 if (!version) return;
615 if (!GetFileVersionInfoW(fusion, handle, size, version))
616 goto done;
618 if (!VerQueryValueW(version, translation, (LPVOID *)&translate, &val_len))
619 goto done;
621 sprintfW(buf, verfmt, translate[0].wLanguage, translate[0].wCodePage);
623 if (!VerQueryValueW(version, buf, (LPVOID *)&verstr, &val_len))
624 goto done;
626 if (!val_len || !verstr)
627 goto done;
629 msi_set_property(package->db, netasm, verstr);
631 done:
632 msi_free(fusion);
633 msi_free(version);
636 static VOID set_installer_properties(MSIPACKAGE *package)
638 WCHAR pth[MAX_PATH];
639 WCHAR *ptr;
640 OSVERSIONINFOEXW OSVersion;
641 MEMORYSTATUSEX msex;
642 DWORD verval, len;
643 WCHAR verstr[10], bufstr[20];
644 HDC dc;
645 HKEY hkey;
646 LPWSTR username, companyname;
647 SYSTEM_INFO sys_info;
648 SYSTEMTIME systemtime;
649 LANGID langid;
651 static const WCHAR szCommonFilesFolder[] = {'C','o','m','m','o','n','F','i','l','e','s','F','o','l','d','e','r',0};
652 static const WCHAR szProgramFilesFolder[] = {'P','r','o','g','r','a','m','F','i','l','e','s','F','o','l','d','e','r',0};
653 static const WCHAR szCommonAppDataFolder[] = {'C','o','m','m','o','n','A','p','p','D','a','t','a','F','o','l','d','e','r',0};
654 static const WCHAR szFavoritesFolder[] = {'F','a','v','o','r','i','t','e','s','F','o','l','d','e','r',0};
655 static const WCHAR szFontsFolder[] = {'F','o','n','t','s','F','o','l','d','e','r',0};
656 static const WCHAR szSendToFolder[] = {'S','e','n','d','T','o','F','o','l','d','e','r',0};
657 static const WCHAR szStartMenuFolder[] = {'S','t','a','r','t','M','e','n','u','F','o','l','d','e','r',0};
658 static const WCHAR szStartupFolder[] = {'S','t','a','r','t','u','p','F','o','l','d','e','r',0};
659 static const WCHAR szTemplateFolder[] = {'T','e','m','p','l','a','t','e','F','o','l','d','e','r',0};
660 static const WCHAR szDesktopFolder[] = {'D','e','s','k','t','o','p','F','o','l','d','e','r',0};
661 static const WCHAR szProgramMenuFolder[] = {'P','r','o','g','r','a','m','M','e','n','u','F','o','l','d','e','r',0};
662 static const WCHAR szAdminToolsFolder[] = {'A','d','m','i','n','T','o','o','l','s','F','o','l','d','e','r',0};
663 static const WCHAR szAppDataFolder[] = {'A','p','p','D','a','t','a','F','o','l','d','e','r',0};
664 static const WCHAR szSystemFolder[] = {'S','y','s','t','e','m','F','o','l','d','e','r',0};
665 static const WCHAR szSystem16Folder[] = {'S','y','s','t','e','m','1','6','F','o','l','d','e','r',0};
666 static const WCHAR szLocalAppDataFolder[] = {'L','o','c','a','l','A','p','p','D','a','t','a','F','o','l','d','e','r',0};
667 static const WCHAR szMyPicturesFolder[] = {'M','y','P','i','c','t','u','r','e','s','F','o','l','d','e','r',0};
668 static const WCHAR szPersonalFolder[] = {'P','e','r','s','o','n','a','l','F','o','l','d','e','r',0};
669 static const WCHAR szWindowsFolder[] = {'W','i','n','d','o','w','s','F','o','l','d','e','r',0};
670 static const WCHAR szWindowsVolume[] = {'W','i','n','d','o','w','s','V','o','l','u','m','e',0};
671 static const WCHAR szTempFolder[]= {'T','e','m','p','F','o','l','d','e','r',0};
672 static const WCHAR szPrivileged[] = {'P','r','i','v','i','l','e','g','e','d',0};
673 static const WCHAR szVersion9x[] = {'V','e','r','s','i','o','n','9','X',0};
674 static const WCHAR szVersionNT[] = {'V','e','r','s','i','o','n','N','T',0};
675 static const WCHAR szMsiNTProductType[] = {'M','s','i','N','T','P','r','o','d','u','c','t','T','y','p','e',0};
676 static const WCHAR szFormat[] = {'%','l','i',0};
677 static const WCHAR szWindowsBuild[] = {'W','i','n','d','o','w','s','B','u','i','l','d',0};
678 static const WCHAR szServicePackLevel[] = {'S','e','r','v','i','c','e','P','a','c','k','L','e','v','e','l',0};
679 static const WCHAR szSix[] = {'6',0 };
680 static const WCHAR szVersionMsi[] = { 'V','e','r','s','i','o','n','M','s','i',0 };
681 static const WCHAR szVersionDatabase[] = { 'V','e','r','s','i','o','n','D','a','t','a','b','a','s','e',0 };
682 static const WCHAR szPhysicalMemory[] = { 'P','h','y','s','i','c','a','l','M','e','m','o','r','y',0 };
683 static const WCHAR szFormat2[] = {'%','l','i','.','%','l','i',0};
684 static const WCHAR szScreenX[] = {'S','c','r','e','e','n','X',0};
685 static const WCHAR szScreenY[] = {'S','c','r','e','e','n','Y',0};
686 static const WCHAR szColorBits[] = {'C','o','l','o','r','B','i','t','s',0};
687 static const WCHAR szIntFormat[] = {'%','d',0};
688 static const WCHAR szMsiAMD64[] = { 'M','s','i','A','M','D','6','4',0 };
689 static const WCHAR szMsix64[] = { 'M','s','i','x','6','4',0 };
690 static const WCHAR szSystem64Folder[] = { 'S','y','s','t','e','m','6','4','F','o','l','d','e','r',0 };
691 static const WCHAR szCommonFiles64Folder[] = { 'C','o','m','m','o','n','F','i','l','e','s','6','4','F','o','l','d','e','r',0 };
692 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 };
693 static const WCHAR szVersionNT64[] = { 'V','e','r','s','i','o','n','N','T','6','4',0 };
694 static const WCHAR szUserInfo[] = {
695 'S','O','F','T','W','A','R','E','\\',
696 'M','i','c','r','o','s','o','f','t','\\',
697 'M','S',' ','S','e','t','u','p',' ','(','A','C','M','E',')','\\',
698 'U','s','e','r',' ','I','n','f','o',0
700 static const WCHAR szDefName[] = { 'D','e','f','N','a','m','e',0 };
701 static const WCHAR szDefCompany[] = { 'D','e','f','C','o','m','p','a','n','y',0 };
702 static const WCHAR szCurrentVersion[] = {
703 'S','O','F','T','W','A','R','E','\\',
704 'M','i','c','r','o','s','o','f','t','\\',
705 'W','i','n','d','o','w','s',' ','N','T','\\',
706 'C','u','r','r','e','n','t','V','e','r','s','i','o','n',0
708 static const WCHAR szRegisteredUser[] = {'R','e','g','i','s','t','e','r','e','d','O','w','n','e','r',0};
709 static const WCHAR szRegisteredOrganization[] = {
710 'R','e','g','i','s','t','e','r','e','d','O','r','g','a','n','i','z','a','t','i','o','n',0
712 static const WCHAR szUSERNAME[] = {'U','S','E','R','N','A','M','E',0};
713 static const WCHAR szCOMPANYNAME[] = {'C','O','M','P','A','N','Y','N','A','M','E',0};
714 static const WCHAR szDate[] = {'D','a','t','e',0};
715 static const WCHAR szTime[] = {'T','i','m','e',0};
716 static const WCHAR szUserLanguageID[] = {'U','s','e','r','L','a','n','g','u','a','g','e','I','D',0};
717 static const WCHAR szSystemLangID[] = {'S','y','s','t','e','m','L','a','n','g','u','a','g','e','I','D',0};
718 static const WCHAR szProductState[] = {'P','r','o','d','u','c','t','S','t','a','t','e',0};
719 static const WCHAR szLogonUser[] = {'L','o','g','o','n','U','s','e','r',0};
720 static const WCHAR szNetHoodFolder[] = {'N','e','t','H','o','o','d','F','o','l','d','e','r',0};
721 static const WCHAR szPrintHoodFolder[] = {'P','r','i','n','t','H','o','o','d','F','o','l','d','e','r',0};
722 static const WCHAR szRecentFolder[] = {'R','e','c','e','n','t','F','o','l','d','e','r',0};
725 * Other things that probably should be set:
727 * ComputerName VirtualMemory
728 * ShellAdvSupport DefaultUIFont PackagecodeChanging
729 * CaptionHeight BorderTop BorderSide TextHeight
730 * RedirectedDllSupport
733 SHGetFolderPathW(NULL, CSIDL_COMMON_APPDATA, NULL, 0, pth);
734 strcatW(pth, szBackSlash);
735 msi_set_property(package->db, szCommonAppDataFolder, pth);
737 SHGetFolderPathW(NULL, CSIDL_FAVORITES, NULL, 0, pth);
738 strcatW(pth, szBackSlash);
739 msi_set_property(package->db, szFavoritesFolder, pth);
741 SHGetFolderPathW(NULL, CSIDL_FONTS, NULL, 0, pth);
742 strcatW(pth, szBackSlash);
743 msi_set_property(package->db, szFontsFolder, pth);
745 SHGetFolderPathW(NULL, CSIDL_SENDTO, NULL, 0, pth);
746 strcatW(pth, szBackSlash);
747 msi_set_property(package->db, szSendToFolder, pth);
749 SHGetFolderPathW(NULL, CSIDL_STARTMENU, NULL, 0, pth);
750 strcatW(pth, szBackSlash);
751 msi_set_property(package->db, szStartMenuFolder, pth);
753 SHGetFolderPathW(NULL, CSIDL_STARTUP, NULL, 0, pth);
754 strcatW(pth, szBackSlash);
755 msi_set_property(package->db, szStartupFolder, pth);
757 SHGetFolderPathW(NULL, CSIDL_TEMPLATES, NULL, 0, pth);
758 strcatW(pth, szBackSlash);
759 msi_set_property(package->db, szTemplateFolder, pth);
761 SHGetFolderPathW(NULL, CSIDL_DESKTOP, NULL, 0, pth);
762 strcatW(pth, szBackSlash);
763 msi_set_property(package->db, szDesktopFolder, pth);
765 /* FIXME: set to AllUsers profile path if ALLUSERS is set */
766 SHGetFolderPathW(NULL, CSIDL_PROGRAMS, NULL, 0, pth);
767 strcatW(pth, szBackSlash);
768 msi_set_property(package->db, szProgramMenuFolder, pth);
770 SHGetFolderPathW(NULL, CSIDL_ADMINTOOLS, NULL, 0, pth);
771 strcatW(pth, szBackSlash);
772 msi_set_property(package->db, szAdminToolsFolder, pth);
774 SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, 0, pth);
775 strcatW(pth, szBackSlash);
776 msi_set_property(package->db, szAppDataFolder, pth);
778 SHGetFolderPathW(NULL, CSIDL_SYSTEM, NULL, 0, pth);
779 strcatW(pth, szBackSlash);
780 msi_set_property(package->db, szSystemFolder, pth);
781 msi_set_property(package->db, szSystem16Folder, pth);
783 SHGetFolderPathW(NULL, CSIDL_LOCAL_APPDATA, NULL, 0, pth);
784 strcatW(pth, szBackSlash);
785 msi_set_property(package->db, szLocalAppDataFolder, pth);
787 SHGetFolderPathW(NULL, CSIDL_MYPICTURES, NULL, 0, pth);
788 strcatW(pth, szBackSlash);
789 msi_set_property(package->db, szMyPicturesFolder, pth);
791 SHGetFolderPathW(NULL, CSIDL_PERSONAL, NULL, 0, pth);
792 strcatW(pth, szBackSlash);
793 msi_set_property(package->db, szPersonalFolder, pth);
795 SHGetFolderPathW(NULL, CSIDL_WINDOWS, NULL, 0, pth);
796 strcatW(pth, szBackSlash);
797 msi_set_property(package->db, szWindowsFolder, pth);
799 SHGetFolderPathW(NULL, CSIDL_PRINTHOOD, NULL, 0, pth);
800 strcatW(pth, szBackSlash);
801 msi_set_property(package->db, szPrintHoodFolder, pth);
803 SHGetFolderPathW(NULL, CSIDL_NETHOOD, NULL, 0, pth);
804 strcatW(pth, szBackSlash);
805 msi_set_property(package->db, szNetHoodFolder, pth);
807 SHGetFolderPathW(NULL, CSIDL_RECENT, NULL, 0, pth);
808 strcatW(pth, szBackSlash);
809 msi_set_property(package->db, szRecentFolder, pth);
811 /* Physical Memory is specified in MB. Using total amount. */
812 msex.dwLength = sizeof(msex);
813 GlobalMemoryStatusEx( &msex );
814 sprintfW( bufstr, szIntFormat, (int)(msex.ullTotalPhys / 1024 / 1024) );
815 msi_set_property(package->db, szPhysicalMemory, bufstr);
817 SHGetFolderPathW(NULL, CSIDL_WINDOWS, NULL, 0, pth);
818 ptr = strchrW(pth,'\\');
819 if (ptr) *(ptr + 1) = 0;
820 msi_set_property(package->db, szWindowsVolume, pth);
822 GetTempPathW(MAX_PATH,pth);
823 msi_set_property(package->db, szTempFolder, pth);
825 /* in a wine environment the user is always admin and privileged */
826 msi_set_property(package->db, szAdminUser, szOne);
827 msi_set_property(package->db, szPrivileged, szOne);
829 /* set the os things */
830 OSVersion.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEXW);
831 GetVersionExW((OSVERSIONINFOW *)&OSVersion);
832 verval = OSVersion.dwMinorVersion + OSVersion.dwMajorVersion * 100;
833 sprintfW(verstr, szFormat, verval);
834 switch (OSVersion.dwPlatformId)
836 case VER_PLATFORM_WIN32_WINDOWS:
837 msi_set_property(package->db, szVersion9x, verstr);
838 break;
839 case VER_PLATFORM_WIN32_NT:
840 msi_set_property(package->db, szVersionNT, verstr);
841 sprintfW(verstr, szFormat,OSVersion.wProductType);
842 msi_set_property(package->db, szMsiNTProductType, verstr);
843 break;
845 sprintfW(verstr, szFormat, OSVersion.dwBuildNumber);
846 msi_set_property(package->db, szWindowsBuild, verstr);
847 /* just fudge this */
848 msi_set_property(package->db, szServicePackLevel, szSix);
850 sprintfW( bufstr, szFormat2, MSI_MAJORVERSION, MSI_MINORVERSION);
851 msi_set_property( package->db, szVersionMsi, bufstr );
852 sprintfW( bufstr, szFormat, MSI_MAJORVERSION * 100);
853 msi_set_property( package->db, szVersionDatabase, bufstr );
855 GetNativeSystemInfo( &sys_info );
856 sprintfW( bufstr, szIntFormat, sys_info.wProcessorLevel );
857 if (sys_info.u.s.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL)
859 msi_set_property( package->db, szIntel, bufstr );
861 GetSystemDirectoryW( pth, MAX_PATH );
862 PathAddBackslashW( pth );
863 msi_set_property( package->db, szSystemFolder, pth );
865 SHGetFolderPathW( NULL, CSIDL_PROGRAM_FILES, NULL, 0, pth );
866 PathAddBackslashW( pth );
867 msi_set_property( package->db, szProgramFilesFolder, pth );
869 SHGetFolderPathW( NULL, CSIDL_PROGRAM_FILES_COMMON, NULL, 0, pth );
870 PathAddBackslashW( pth );
871 msi_set_property( package->db, szCommonFilesFolder, pth );
873 else if (sys_info.u.s.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
875 msi_set_property( package->db, szMsiAMD64, bufstr );
876 msi_set_property( package->db, szMsix64, bufstr );
877 msi_set_property( package->db, szVersionNT64, verstr );
879 GetSystemDirectoryW( pth, MAX_PATH );
880 PathAddBackslashW( pth );
881 msi_set_property( package->db, szSystem64Folder, pth );
883 GetSystemWow64DirectoryW( pth, MAX_PATH );
884 PathAddBackslashW( pth );
885 msi_set_property( package->db, szSystemFolder, pth );
887 SHGetFolderPathW( NULL, CSIDL_PROGRAM_FILES, NULL, 0, pth );
888 PathAddBackslashW( pth );
889 msi_set_property( package->db, szProgramFiles64Folder, pth );
891 SHGetFolderPathW( NULL, CSIDL_PROGRAM_FILESX86, NULL, 0, pth );
892 PathAddBackslashW( pth );
893 msi_set_property( package->db, szProgramFilesFolder, pth );
895 SHGetFolderPathW( NULL, CSIDL_PROGRAM_FILES_COMMON, NULL, 0, pth );
896 PathAddBackslashW( pth );
897 msi_set_property( package->db, szCommonFiles64Folder, pth );
899 SHGetFolderPathW( NULL, CSIDL_PROGRAM_FILES_COMMONX86, NULL, 0, pth );
900 PathAddBackslashW( pth );
901 msi_set_property( package->db, szCommonFilesFolder, pth );
904 /* Screen properties. */
905 dc = GetDC(0);
906 sprintfW( bufstr, szIntFormat, GetDeviceCaps( dc, HORZRES ) );
907 msi_set_property( package->db, szScreenX, bufstr );
908 sprintfW( bufstr, szIntFormat, GetDeviceCaps( dc, VERTRES ));
909 msi_set_property( package->db, szScreenY, bufstr );
910 sprintfW( bufstr, szIntFormat, GetDeviceCaps( dc, BITSPIXEL ));
911 msi_set_property( package->db, szColorBits, bufstr );
912 ReleaseDC(0, dc);
914 /* USERNAME and COMPANYNAME */
915 username = msi_dup_property( package->db, szUSERNAME );
916 companyname = msi_dup_property( package->db, szCOMPANYNAME );
918 if ((!username || !companyname) &&
919 RegOpenKeyW( HKEY_CURRENT_USER, szUserInfo, &hkey ) == ERROR_SUCCESS)
921 if (!username &&
922 (username = msi_reg_get_val_str( hkey, szDefName )))
923 msi_set_property( package->db, szUSERNAME, username );
924 if (!companyname &&
925 (companyname = msi_reg_get_val_str( hkey, szDefCompany )))
926 msi_set_property( package->db, szCOMPANYNAME, companyname );
927 CloseHandle( hkey );
929 if ((!username || !companyname) &&
930 RegOpenKeyW( HKEY_LOCAL_MACHINE, szCurrentVersion, &hkey ) == ERROR_SUCCESS)
932 if (!username &&
933 (username = msi_reg_get_val_str( hkey, szRegisteredUser )))
934 msi_set_property( package->db, szUSERNAME, username );
935 if (!companyname &&
936 (companyname = msi_reg_get_val_str( hkey, szRegisteredOrganization )))
937 msi_set_property( package->db, szCOMPANYNAME, companyname );
938 CloseHandle( hkey );
940 msi_free( username );
941 msi_free( companyname );
943 if ( set_user_sid_prop( package ) != ERROR_SUCCESS)
944 ERR("Failed to set the UserSID property\n");
946 /* Date and time properties */
947 GetSystemTime( &systemtime );
948 if (GetDateFormatW( LOCALE_USER_DEFAULT, DATE_SHORTDATE, &systemtime,
949 NULL, bufstr, sizeof(bufstr)/sizeof(bufstr[0]) ))
950 msi_set_property( package->db, szDate, bufstr );
951 else
952 ERR("Couldn't set Date property: GetDateFormat failed with error %d\n", GetLastError());
954 if (GetTimeFormatW( LOCALE_USER_DEFAULT,
955 TIME_FORCE24HOURFORMAT | TIME_NOTIMEMARKER,
956 &systemtime, NULL, bufstr,
957 sizeof(bufstr)/sizeof(bufstr[0]) ))
958 msi_set_property( package->db, szTime, bufstr );
959 else
960 ERR("Couldn't set Time property: GetTimeFormat failed with error %d\n", GetLastError());
962 set_msi_assembly_prop( package );
964 langid = GetUserDefaultLangID();
965 sprintfW(bufstr, szIntFormat, langid);
966 msi_set_property( package->db, szUserLanguageID, bufstr );
968 langid = GetSystemDefaultLangID();
969 sprintfW(bufstr, szIntFormat, langid);
970 msi_set_property( package->db, szSystemLangID, bufstr );
972 sprintfW(bufstr, szIntFormat, MsiQueryProductStateW(package->ProductCode));
973 msi_set_property( package->db, szProductState, bufstr );
975 len = 0;
976 if (!GetUserNameW( NULL, &len ) && GetLastError() == ERROR_MORE_DATA)
978 WCHAR *username;
979 if ((username = msi_alloc( len * sizeof(WCHAR) )))
981 if (GetUserNameW( username, &len ))
982 msi_set_property( package->db, szLogonUser, username );
983 msi_free( username );
988 static UINT msi_load_summary_properties( MSIPACKAGE *package )
990 UINT rc;
991 MSIHANDLE suminfo;
992 MSIHANDLE hdb = alloc_msihandle( &package->db->hdr );
993 INT count;
994 DWORD len;
995 LPWSTR package_code;
996 static const WCHAR szPackageCode[] = {
997 'P','a','c','k','a','g','e','C','o','d','e',0};
999 if (!hdb) {
1000 ERR("Unable to allocate handle\n");
1001 return ERROR_OUTOFMEMORY;
1004 rc = MsiGetSummaryInformationW( hdb, NULL, 0, &suminfo );
1005 MsiCloseHandle(hdb);
1006 if (rc != ERROR_SUCCESS)
1008 ERR("Unable to open Summary Information\n");
1009 return rc;
1012 rc = MsiSummaryInfoGetPropertyW( suminfo, PID_PAGECOUNT, NULL,
1013 &count, NULL, NULL, NULL );
1014 if (rc != ERROR_SUCCESS)
1016 WARN("Unable to query page count: %d\n", rc);
1017 goto done;
1020 /* load package code property */
1021 len = 0;
1022 rc = MsiSummaryInfoGetPropertyW( suminfo, PID_REVNUMBER, NULL,
1023 NULL, NULL, NULL, &len );
1024 if (rc != ERROR_MORE_DATA)
1026 WARN("Unable to query revision number: %d\n", rc);
1027 rc = ERROR_FUNCTION_FAILED;
1028 goto done;
1031 len++;
1032 package_code = msi_alloc( len * sizeof(WCHAR) );
1033 rc = MsiSummaryInfoGetPropertyW( suminfo, PID_REVNUMBER, NULL,
1034 NULL, NULL, package_code, &len );
1035 if (rc != ERROR_SUCCESS)
1037 WARN("Unable to query rev number: %d\n", rc);
1038 goto done;
1041 msi_set_property( package->db, szPackageCode, package_code );
1042 msi_free( package_code );
1044 /* load package attributes */
1045 count = 0;
1046 MsiSummaryInfoGetPropertyW( suminfo, PID_WORDCOUNT, NULL,
1047 &count, NULL, NULL, NULL );
1048 package->WordCount = count;
1050 done:
1051 MsiCloseHandle(suminfo);
1052 return rc;
1055 static MSIPACKAGE *msi_alloc_package( void )
1057 MSIPACKAGE *package;
1059 package = alloc_msiobject( MSIHANDLETYPE_PACKAGE, sizeof (MSIPACKAGE),
1060 MSI_FreePackage );
1061 if( package )
1063 list_init( &package->components );
1064 list_init( &package->features );
1065 list_init( &package->files );
1066 list_init( &package->tempfiles );
1067 list_init( &package->folders );
1068 list_init( &package->subscriptions );
1069 list_init( &package->appids );
1070 list_init( &package->classes );
1071 list_init( &package->mimes );
1072 list_init( &package->extensions );
1073 list_init( &package->progids );
1074 list_init( &package->RunningActions );
1075 list_init( &package->sourcelist_info );
1076 list_init( &package->sourcelist_media );
1077 list_init( &package->patches );
1078 list_init( &package->binaries );
1081 return package;
1084 static UINT msi_load_admin_properties(MSIPACKAGE *package)
1086 BYTE *data;
1087 UINT r, sz;
1089 static const WCHAR stmname[] = {'A','d','m','i','n','P','r','o','p','e','r','t','i','e','s',0};
1091 r = read_stream_data(package->db->storage, stmname, FALSE, &data, &sz);
1092 if (r != ERROR_SUCCESS)
1093 return r;
1095 r = msi_parse_command_line(package, (WCHAR *)data, TRUE);
1097 msi_free(data);
1098 return r;
1101 void msi_adjust_privilege_properties( MSIPACKAGE *package )
1103 /* FIXME: this should depend on the user's privileges */
1104 if (msi_get_property_int( package->db, szAllUsers, 0 ) == 2)
1106 TRACE("resetting ALLUSERS property from 2 to 1\n");
1107 msi_set_property( package->db, szAllUsers, szOne );
1109 msi_set_property( package->db, szAdminUser, szOne );
1112 MSIPACKAGE *MSI_CreatePackage( MSIDATABASE *db, LPCWSTR base_url )
1114 static const WCHAR szLevel[] = { 'U','I','L','e','v','e','l',0 };
1115 static const WCHAR szpi[] = {'%','i',0};
1116 MSIPACKAGE *package;
1117 WCHAR uilevel[10];
1118 UINT r;
1120 TRACE("%p\n", db);
1122 package = msi_alloc_package();
1123 if (package)
1125 msiobj_addref( &db->hdr );
1126 package->db = db;
1128 package->WordCount = 0;
1129 package->PackagePath = strdupW( db->path );
1130 package->BaseURL = strdupW( base_url );
1132 create_temp_property_table( package );
1133 msi_clone_properties( package );
1134 msi_adjust_privilege_properties( package );
1136 package->ProductCode = msi_dup_property( package->db, szProductCode );
1137 package->script = msi_alloc_zero( sizeof(MSISCRIPT) );
1139 set_installed_prop( package );
1140 set_installer_properties( package );
1142 sprintfW(uilevel,szpi,gUILevel);
1143 msi_set_property(package->db, szLevel, uilevel);
1145 r = msi_load_summary_properties( package );
1146 if (r != ERROR_SUCCESS)
1148 msiobj_release( &package->hdr );
1149 return NULL;
1152 if (package->WordCount & msidbSumInfoSourceTypeAdminImage)
1153 msi_load_admin_properties( package );
1155 package->log_file = INVALID_HANDLE_VALUE;
1158 return package;
1162 * copy_package_to_temp [internal]
1164 * copy the msi file to a temp file to prevent locking a CD
1165 * with a multi disc install
1167 * FIXME: I think this is wrong, and instead of copying the package,
1168 * we should read all the tables to memory, then open the
1169 * database to read binary streams on demand.
1171 static UINT copy_package_to_temp( LPCWSTR szPackage, LPWSTR filename )
1173 WCHAR path[MAX_PATH];
1175 GetTempPathW( MAX_PATH, path );
1176 GetTempFileNameW( path, szMsi, 0, filename );
1178 if( !CopyFileW( szPackage, filename, FALSE ) )
1180 UINT error = GetLastError();
1181 if ( error == ERROR_FILE_NOT_FOUND )
1182 ERR("can't find %s\n", debugstr_w(szPackage));
1183 else
1184 ERR("failed to copy package %s to %s (%u)\n", debugstr_w(szPackage), debugstr_w(filename), error);
1185 DeleteFileW( filename );
1186 return error;
1189 return ERROR_SUCCESS;
1192 UINT msi_download_file( LPCWSTR szUrl, LPWSTR filename )
1194 LPINTERNET_CACHE_ENTRY_INFOW cache_entry;
1195 DWORD size = 0;
1196 HRESULT hr;
1198 /* call will always fail, becase size is 0,
1199 * but will return ERROR_FILE_NOT_FOUND first
1200 * if the file doesn't exist
1202 GetUrlCacheEntryInfoW( szUrl, NULL, &size );
1203 if ( GetLastError() != ERROR_FILE_NOT_FOUND )
1205 cache_entry = msi_alloc( size );
1206 if ( !GetUrlCacheEntryInfoW( szUrl, cache_entry, &size ) )
1208 UINT error = GetLastError();
1209 msi_free( cache_entry );
1210 return error;
1213 lstrcpyW( filename, cache_entry->lpszLocalFileName );
1214 msi_free( cache_entry );
1215 return ERROR_SUCCESS;
1218 hr = URLDownloadToCacheFileW( NULL, szUrl, filename, MAX_PATH, 0, NULL );
1219 if ( FAILED(hr) )
1221 WARN("failed to download %s to cache file\n", debugstr_w(szUrl));
1222 return ERROR_FUNCTION_FAILED;
1225 return ERROR_SUCCESS;
1228 UINT msi_get_local_package_name( LPWSTR path, LPCWSTR suffix )
1230 static const WCHAR szInstaller[] = {
1231 '\\','I','n','s','t','a','l','l','e','r','\\',0};
1232 static const WCHAR fmt[] = {'%','x',0};
1233 DWORD time, len, i, offset;
1234 HANDLE handle;
1236 time = GetTickCount();
1237 GetWindowsDirectoryW( path, MAX_PATH );
1238 strcatW( path, szInstaller );
1239 CreateDirectoryW( path, NULL );
1241 len = strlenW(path);
1242 for (i = 0; i < 0x10000; i++)
1244 offset = snprintfW( path + len, MAX_PATH - len, fmt, (time + i) & 0xffff );
1245 memcpy( path + len + offset, suffix, (strlenW( suffix ) + 1) * sizeof(WCHAR) );
1246 handle = CreateFileW( path, GENERIC_WRITE, 0, NULL,
1247 CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0 );
1248 if (handle != INVALID_HANDLE_VALUE)
1250 CloseHandle(handle);
1251 break;
1253 if (GetLastError() != ERROR_FILE_EXISTS &&
1254 GetLastError() != ERROR_SHARING_VIOLATION)
1255 return ERROR_FUNCTION_FAILED;
1258 return ERROR_SUCCESS;
1261 static UINT apply_registered_patch( MSIPACKAGE *package, LPCWSTR patch_code )
1263 UINT r;
1264 DWORD len;
1265 WCHAR patch_file[MAX_PATH];
1266 MSIDATABASE *patch_db;
1267 MSIPATCHINFO *patch_info;
1268 MSISUMMARYINFO *si;
1270 len = sizeof(patch_file) / sizeof(WCHAR);
1271 r = MsiGetPatchInfoExW( patch_code, package->ProductCode, NULL, package->Context,
1272 INSTALLPROPERTY_LOCALPACKAGEW, patch_file, &len );
1273 if (r != ERROR_SUCCESS)
1275 ERR("failed to get patch filename %u\n", r);
1276 return r;
1279 r = MSI_OpenDatabaseW( patch_file, MSIDBOPEN_READONLY + MSIDBOPEN_PATCHFILE, &patch_db );
1280 if (r != ERROR_SUCCESS)
1282 ERR("failed to open patch database %s\n", debugstr_w( patch_file ));
1283 return r;
1286 si = MSI_GetSummaryInformationW( patch_db->storage, 0 );
1287 if (!si)
1289 msiobj_release( &patch_db->hdr );
1290 return ERROR_FUNCTION_FAILED;
1293 r = msi_parse_patch_summary( si, &patch_info );
1294 msiobj_release( &si->hdr );
1295 if (r != ERROR_SUCCESS)
1297 ERR("failed to parse patch summary %u\n", r);
1298 msiobj_release( &patch_db->hdr );
1299 return r;
1302 patch_info->localfile = strdupW( patch_file );
1303 if (!patch_info->localfile)
1305 msiobj_release( &patch_db->hdr );
1306 return ERROR_OUTOFMEMORY;
1309 r = msi_apply_patch_db( package, patch_db, patch_info );
1310 msiobj_release( &patch_db->hdr );
1311 if (r != ERROR_SUCCESS)
1313 ERR("failed to apply patch %u\n", r);
1314 msi_free( patch_info->patchcode );
1315 msi_free( patch_info->transforms );
1316 msi_free( patch_info->localfile );
1317 msi_free( patch_info );
1319 return r;
1322 static UINT msi_parse_summary( MSISUMMARYINFO *si, MSIPACKAGE *package )
1324 WCHAR *template, *p, *q;
1325 DWORD i, count;
1327 package->version = msi_suminfo_get_int32( si, PID_PAGECOUNT );
1328 TRACE("version: %d\n", package->version);
1330 template = msi_suminfo_dup_string( si, PID_TEMPLATE );
1331 if (!template)
1332 return ERROR_SUCCESS; /* native accepts missing template property */
1334 TRACE("template: %s\n", debugstr_w(template));
1336 p = strchrW( template, ';' );
1337 if (!p)
1339 WARN("invalid template string %s\n", debugstr_w(template));
1340 msi_free( template );
1341 return ERROR_PATCH_PACKAGE_INVALID;
1343 *p = 0;
1344 if (!template[0] || !strcmpW( template, szIntel ))
1345 package->platform = PLATFORM_INTEL;
1346 else if (!strcmpW( template, szIntel64 ))
1347 package->platform = PLATFORM_INTEL64;
1348 else if (!strcmpW( template, szX64 ) || !strcmpW( template, szAMD64 ))
1349 package->platform = PLATFORM_X64;
1350 else
1352 WARN("unknown platform %s\n", debugstr_w(template));
1353 msi_free( template );
1354 return ERROR_INSTALL_PLATFORM_UNSUPPORTED;
1356 p++;
1357 if (!*p)
1359 msi_free( template );
1360 return ERROR_SUCCESS;
1362 count = 1;
1363 for (q = p; (q = strchrW( q, ',' )); q++) count++;
1365 package->langids = msi_alloc( count * sizeof(LANGID) );
1366 if (!package->langids)
1368 msi_free( template );
1369 return ERROR_OUTOFMEMORY;
1372 i = 0;
1373 while (*p)
1375 q = strchrW( p, ',' );
1376 if (q) *q = 0;
1377 package->langids[i] = atoiW( p );
1378 if (!q) break;
1379 p = q + 1;
1380 i++;
1382 package->num_langids = i + 1;
1384 msi_free( template );
1385 return ERROR_SUCCESS;
1388 static UINT validate_package( MSIPACKAGE *package )
1390 BOOL is_wow64;
1391 UINT i;
1393 IsWow64Process( GetCurrentProcess(), &is_wow64 );
1394 if (package->platform == PLATFORM_X64)
1396 if (!is_64bit && !is_wow64)
1397 return ERROR_INSTALL_PLATFORM_UNSUPPORTED;
1398 if (package->version < 200)
1399 return ERROR_INSTALL_PACKAGE_INVALID;
1401 if (!package->num_langids)
1403 return ERROR_SUCCESS;
1405 for (i = 0; i < package->num_langids; i++)
1407 LANGID langid = package->langids[i];
1409 if (PRIMARYLANGID( langid ) == LANG_NEUTRAL)
1411 langid = MAKELANGID( PRIMARYLANGID( GetSystemDefaultLangID() ), SUBLANGID( langid ) );
1413 if (SUBLANGID( langid ) == SUBLANG_NEUTRAL)
1415 langid = MAKELANGID( PRIMARYLANGID( langid ), SUBLANGID( GetSystemDefaultLangID() ) );
1417 if (IsValidLocale( langid, LCID_INSTALLED ))
1418 return ERROR_SUCCESS;
1420 return ERROR_INSTALL_LANGUAGE_UNSUPPORTED;
1423 UINT MSI_OpenPackageW(LPCWSTR szPackage, MSIPACKAGE **pPackage)
1425 static const WCHAR Database[] = {'D','A','T','A','B','A','S','E',0};
1426 static const WCHAR dotmsi[] = {'.','m','s','i',0};
1427 MSIDATABASE *db = NULL;
1428 MSIPACKAGE *package;
1429 MSIHANDLE handle;
1430 LPWSTR ptr, base_url = NULL;
1431 UINT r;
1432 WCHAR temppath[MAX_PATH], localfile[MAX_PATH], cachefile[MAX_PATH];
1433 LPCWSTR file = szPackage;
1434 DWORD index = 0;
1435 MSISUMMARYINFO *si;
1437 TRACE("%s %p\n", debugstr_w(szPackage), pPackage);
1439 if( szPackage[0] == '#' )
1441 handle = atoiW(&szPackage[1]);
1442 db = msihandle2msiinfo( handle, MSIHANDLETYPE_DATABASE );
1443 if( !db )
1445 IWineMsiRemoteDatabase *remote_database;
1447 remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( handle );
1448 if ( !remote_database )
1449 return ERROR_INVALID_HANDLE;
1451 IWineMsiRemoteDatabase_Release( remote_database );
1452 WARN("MsiOpenPackage not allowed during a custom action!\n");
1454 return ERROR_FUNCTION_FAILED;
1457 else
1459 if ( UrlIsW( szPackage, URLIS_URL ) )
1461 r = msi_download_file( szPackage, cachefile );
1462 if ( r != ERROR_SUCCESS )
1463 return r;
1465 r = copy_package_to_temp( cachefile, temppath );
1466 if ( r != ERROR_SUCCESS )
1467 return r;
1469 file = temppath;
1471 base_url = strdupW( szPackage );
1472 if ( !base_url )
1473 return ERROR_OUTOFMEMORY;
1475 ptr = strrchrW( base_url, '/' );
1476 if (ptr) *(ptr + 1) = '\0';
1478 else
1480 r = copy_package_to_temp( szPackage, temppath );
1481 if ( r != ERROR_SUCCESS )
1482 return r;
1484 file = temppath;
1487 r = msi_get_local_package_name( localfile, dotmsi );
1488 if (r != ERROR_SUCCESS)
1489 return r;
1491 TRACE("Copying to local package %s\n", debugstr_w(localfile));
1493 if (!CopyFileW( file, localfile, FALSE ))
1495 ERR("Unable to copy package (%s -> %s) (error %u)\n",
1496 debugstr_w(file), debugstr_w(localfile), GetLastError());
1497 return GetLastError();
1500 TRACE("Opening relocated package %s\n", debugstr_w( file ));
1502 /* transforms that add binary streams require that we open the database
1503 * read/write, which is safe because we always create a copy that is thrown
1504 * away when we're done.
1506 r = MSI_OpenDatabaseW( file, MSIDBOPEN_TRANSACT, &db );
1507 if( r != ERROR_SUCCESS )
1509 if (file != szPackage)
1510 DeleteFileW( file );
1512 if (GetFileAttributesW(szPackage) == INVALID_FILE_ATTRIBUTES)
1513 return ERROR_FILE_NOT_FOUND;
1515 return r;
1518 db->localfile = strdupW( localfile );
1521 package = MSI_CreatePackage( db, base_url );
1522 msi_free( base_url );
1523 msiobj_release( &db->hdr );
1524 if( !package )
1526 if (file != szPackage)
1527 DeleteFileW( file );
1529 return ERROR_INSTALL_PACKAGE_INVALID;
1532 if( file != szPackage )
1533 track_tempfile( package, file );
1535 si = MSI_GetSummaryInformationW( db->storage, 0 );
1536 if (!si)
1538 WARN("failed to load summary info %u\n", r);
1539 msiobj_release( &package->hdr );
1540 return ERROR_INSTALL_PACKAGE_INVALID;
1543 r = msi_parse_summary( si, package );
1544 msiobj_release( &si->hdr );
1545 if (r != ERROR_SUCCESS)
1547 WARN("failed to parse summary info %u\n", r);
1548 msiobj_release( &package->hdr );
1549 return r;
1552 r = validate_package( package );
1553 if (r != ERROR_SUCCESS)
1555 msiobj_release( &package->hdr );
1556 return r;
1558 msi_set_property( package->db, Database, db->path );
1560 if( UrlIsW( szPackage, URLIS_URL ) )
1561 msi_set_property( package->db, szOriginalDatabase, szPackage );
1562 else if( szPackage[0] == '#' )
1563 msi_set_property( package->db, szOriginalDatabase, db->path );
1564 else
1566 WCHAR fullpath[MAX_PATH];
1568 GetFullPathNameW( szPackage, MAX_PATH, fullpath, NULL );
1569 msi_set_property( package->db, szOriginalDatabase, fullpath );
1572 msi_set_context( package );
1574 while (1)
1576 WCHAR patch_code[GUID_SIZE];
1577 r = MsiEnumPatchesExW( package->ProductCode, NULL, package->Context,
1578 MSIPATCHSTATE_APPLIED, index, patch_code, NULL, NULL, NULL, NULL );
1579 if (r != ERROR_SUCCESS)
1580 break;
1582 TRACE("found registered patch %s\n", debugstr_w(patch_code));
1584 r = apply_registered_patch( package, patch_code );
1585 if (r != ERROR_SUCCESS)
1587 ERR("registered patch failed to apply %u\n", r);
1588 msiobj_release( &package->hdr );
1589 return r;
1592 index++;
1595 if (index)
1597 msi_clone_properties( package );
1598 msi_adjust_privilege_properties( package );
1601 if (gszLogFile)
1602 package->log_file = CreateFileW( gszLogFile, GENERIC_WRITE, FILE_SHARE_WRITE, NULL,
1603 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
1605 *pPackage = package;
1606 return ERROR_SUCCESS;
1609 UINT WINAPI MsiOpenPackageExW(LPCWSTR szPackage, DWORD dwOptions, MSIHANDLE *phPackage)
1611 MSIPACKAGE *package = NULL;
1612 UINT ret;
1614 TRACE("%s %08x %p\n", debugstr_w(szPackage), dwOptions, phPackage );
1616 if( !szPackage || !phPackage )
1617 return ERROR_INVALID_PARAMETER;
1619 if ( !*szPackage )
1621 FIXME("Should create an empty database and package\n");
1622 return ERROR_FUNCTION_FAILED;
1625 if( dwOptions )
1626 FIXME("dwOptions %08x not supported\n", dwOptions);
1628 ret = MSI_OpenPackageW( szPackage, &package );
1629 if( ret == ERROR_SUCCESS )
1631 *phPackage = alloc_msihandle( &package->hdr );
1632 if (! *phPackage)
1633 ret = ERROR_NOT_ENOUGH_MEMORY;
1634 msiobj_release( &package->hdr );
1637 return ret;
1640 UINT WINAPI MsiOpenPackageW(LPCWSTR szPackage, MSIHANDLE *phPackage)
1642 return MsiOpenPackageExW( szPackage, 0, phPackage );
1645 UINT WINAPI MsiOpenPackageExA(LPCSTR szPackage, DWORD dwOptions, MSIHANDLE *phPackage)
1647 LPWSTR szwPack = NULL;
1648 UINT ret;
1650 if( szPackage )
1652 szwPack = strdupAtoW( szPackage );
1653 if( !szwPack )
1654 return ERROR_OUTOFMEMORY;
1657 ret = MsiOpenPackageExW( szwPack, dwOptions, phPackage );
1659 msi_free( szwPack );
1661 return ret;
1664 UINT WINAPI MsiOpenPackageA(LPCSTR szPackage, MSIHANDLE *phPackage)
1666 return MsiOpenPackageExA( szPackage, 0, phPackage );
1669 MSIHANDLE WINAPI MsiGetActiveDatabase(MSIHANDLE hInstall)
1671 MSIPACKAGE *package;
1672 MSIHANDLE handle = 0;
1673 IUnknown *remote_unk;
1674 IWineMsiRemotePackage *remote_package;
1676 TRACE("(%d)\n",hInstall);
1678 package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE);
1679 if( package)
1681 handle = alloc_msihandle( &package->db->hdr );
1682 msiobj_release( &package->hdr );
1684 else if ((remote_unk = msi_get_remote(hInstall)))
1686 if (IUnknown_QueryInterface(remote_unk, &IID_IWineMsiRemotePackage,
1687 (LPVOID *)&remote_package) == S_OK)
1689 IWineMsiRemotePackage_GetActiveDatabase(remote_package, &handle);
1690 IWineMsiRemotePackage_Release(remote_package);
1692 else
1694 WARN("remote handle %d is not a package\n", hInstall);
1696 IUnknown_Release(remote_unk);
1699 return handle;
1702 INT MSI_ProcessMessage( MSIPACKAGE *package, INSTALLMESSAGE eMessageType,
1703 MSIRECORD *record)
1705 static const WCHAR szActionData[] =
1706 {'A','c','t','i','o','n','D','a','t','a',0};
1707 static const WCHAR szSetProgress[] =
1708 {'S','e','t','P','r','o','g','r','e','s','s',0};
1709 static const WCHAR szActionText[] =
1710 {'A','c','t','i','o','n','T','e','x','t',0};
1711 LPWSTR message;
1712 DWORD sz, total_size = 0, log_type = 0;
1713 INT i, rc = 0;
1714 char *msg;
1715 int len;
1717 TRACE("%x\n", eMessageType);
1719 if ((eMessageType & 0xff000000) == INSTALLMESSAGE_ERROR)
1720 log_type |= INSTALLLOGMODE_ERROR;
1721 if ((eMessageType & 0xff000000) == INSTALLMESSAGE_WARNING)
1722 log_type |= INSTALLLOGMODE_WARNING;
1723 if ((eMessageType & 0xff000000) == INSTALLMESSAGE_USER)
1724 log_type |= INSTALLLOGMODE_USER;
1725 if ((eMessageType & 0xff000000) == INSTALLMESSAGE_INFO)
1726 log_type |= INSTALLLOGMODE_INFO;
1727 if ((eMessageType & 0xff000000) == INSTALLMESSAGE_COMMONDATA)
1728 log_type |= INSTALLLOGMODE_COMMONDATA;
1729 if ((eMessageType & 0xff000000) == INSTALLMESSAGE_ACTIONSTART)
1730 log_type |= INSTALLLOGMODE_ACTIONSTART;
1731 if ((eMessageType & 0xff000000) == INSTALLMESSAGE_ACTIONDATA)
1732 log_type |= INSTALLLOGMODE_ACTIONDATA;
1733 /* just a guess */
1734 if ((eMessageType & 0xff000000) == INSTALLMESSAGE_PROGRESS)
1735 log_type |= 0x800;
1737 if ((eMessageType & 0xff000000) == INSTALLMESSAGE_ACTIONSTART)
1739 static const WCHAR template_s[]=
1740 {'A','c','t','i','o','n',' ','%','s',':',' ','%','s','.',' ',0};
1741 static const WCHAR format[] =
1742 {'H','H','\'',':','\'','m','m','\'',':','\'','s','s',0};
1743 WCHAR timet[0x100];
1744 LPCWSTR action_text, action;
1745 LPWSTR deformatted = NULL;
1747 GetTimeFormatW(LOCALE_USER_DEFAULT, 0, NULL, format, timet, 0x100);
1749 action = MSI_RecordGetString(record, 1);
1750 action_text = MSI_RecordGetString(record, 2);
1752 if (!action || !action_text)
1753 return IDOK;
1755 deformat_string(package, action_text, &deformatted);
1757 len = strlenW(timet) + strlenW(action) + strlenW(template_s);
1758 if (deformatted)
1759 len += strlenW(deformatted);
1760 message = msi_alloc(len*sizeof(WCHAR));
1761 sprintfW(message, template_s, timet, action);
1762 if (deformatted)
1763 strcatW(message, deformatted);
1764 msi_free(deformatted);
1766 else
1768 INT msg_field=1;
1769 message = msi_alloc(1*sizeof (WCHAR));
1770 message[0]=0;
1771 msg_field = MSI_RecordGetFieldCount(record);
1772 for (i = 1; i <= msg_field; i++)
1774 LPWSTR tmp;
1775 WCHAR number[3];
1776 static const WCHAR format[] = { '%','i',':',' ',0};
1777 sz = 0;
1778 MSI_RecordGetStringW(record,i,NULL,&sz);
1779 sz+=4;
1780 total_size+=sz*sizeof(WCHAR);
1781 tmp = msi_alloc(sz*sizeof(WCHAR));
1782 message = msi_realloc(message,total_size*sizeof (WCHAR));
1784 MSI_RecordGetStringW(record,i,tmp,&sz);
1786 if (msg_field > 1)
1788 sprintfW(number,format,i);
1789 strcatW(message,number);
1791 strcatW(message,tmp);
1792 if (msg_field > 1)
1793 strcatW(message, szSpace);
1795 msi_free(tmp);
1799 TRACE("%p %p %p %x %x %s\n", gUIHandlerA, gUIHandlerW, gUIHandlerRecord,
1800 gUIFilter, log_type, debugstr_w(message));
1802 /* convert it to ASCII */
1803 len = WideCharToMultiByte( CP_ACP, 0, message, -1, NULL, 0, NULL, NULL );
1804 msg = msi_alloc( len );
1805 WideCharToMultiByte( CP_ACP, 0, message, -1, msg, len, NULL, NULL );
1807 if (gUIHandlerW && (gUIFilter & log_type))
1809 rc = gUIHandlerW( gUIContext, eMessageType, message );
1811 else if (gUIHandlerA && (gUIFilter & log_type))
1813 rc = gUIHandlerA( gUIContext, eMessageType, msg );
1815 else if (gUIHandlerRecord && (gUIFilter & log_type))
1817 MSIHANDLE rec = MsiCreateRecord( 1 );
1818 MsiRecordSetStringW( rec, 0, message );
1819 rc = gUIHandlerRecord( gUIContext, eMessageType, rec );
1820 MsiCloseHandle( rec );
1823 if (!rc && package->log_file != INVALID_HANDLE_VALUE &&
1824 (eMessageType & 0xff000000) != INSTALLMESSAGE_PROGRESS)
1826 DWORD written;
1827 WriteFile( package->log_file, msg, len - 1, &written, NULL );
1828 WriteFile( package->log_file, "\n", 1, &written, NULL );
1830 msi_free( msg );
1831 msi_free( message );
1833 switch (eMessageType & 0xff000000)
1835 case INSTALLMESSAGE_ACTIONDATA:
1836 /* FIXME: format record here instead of in ui_actiondata to get the
1837 * correct action data for external scripts */
1838 ControlEvent_FireSubscribedEvent(package, szActionData, record);
1839 break;
1840 case INSTALLMESSAGE_ACTIONSTART:
1842 MSIRECORD *uirow;
1843 LPWSTR deformated;
1844 LPCWSTR action_text = MSI_RecordGetString(record, 2);
1846 deformat_string(package, action_text, &deformated);
1847 uirow = MSI_CreateRecord(1);
1848 MSI_RecordSetStringW(uirow, 1, deformated);
1849 TRACE("INSTALLMESSAGE_ACTIONSTART: %s\n", debugstr_w(deformated));
1850 msi_free(deformated);
1852 ControlEvent_FireSubscribedEvent(package, szActionText, uirow);
1854 msiobj_release(&uirow->hdr);
1855 break;
1857 case INSTALLMESSAGE_PROGRESS:
1858 ControlEvent_FireSubscribedEvent(package, szSetProgress, record);
1859 break;
1862 return ERROR_SUCCESS;
1865 INT WINAPI MsiProcessMessage( MSIHANDLE hInstall, INSTALLMESSAGE eMessageType,
1866 MSIHANDLE hRecord)
1868 UINT ret = ERROR_INVALID_HANDLE;
1869 MSIPACKAGE *package = NULL;
1870 MSIRECORD *record = NULL;
1872 package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE );
1873 if( !package )
1875 HRESULT hr;
1876 IWineMsiRemotePackage *remote_package;
1878 remote_package = (IWineMsiRemotePackage *)msi_get_remote( hInstall );
1879 if (!remote_package)
1880 return ERROR_INVALID_HANDLE;
1882 hr = IWineMsiRemotePackage_ProcessMessage( remote_package, eMessageType, hRecord );
1884 IWineMsiRemotePackage_Release( remote_package );
1886 if (FAILED(hr))
1888 if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
1889 return HRESULT_CODE(hr);
1891 return ERROR_FUNCTION_FAILED;
1894 return ERROR_SUCCESS;
1897 record = msihandle2msiinfo( hRecord, MSIHANDLETYPE_RECORD );
1898 if( !record )
1899 goto out;
1901 ret = MSI_ProcessMessage( package, eMessageType, record );
1903 out:
1904 msiobj_release( &package->hdr );
1905 if( record )
1906 msiobj_release( &record->hdr );
1908 return ret;
1911 /* property code */
1913 UINT WINAPI MsiSetPropertyA( MSIHANDLE hInstall, LPCSTR szName, LPCSTR szValue )
1915 LPWSTR szwName = NULL, szwValue = NULL;
1916 UINT r = ERROR_OUTOFMEMORY;
1918 szwName = strdupAtoW( szName );
1919 if( szName && !szwName )
1920 goto end;
1922 szwValue = strdupAtoW( szValue );
1923 if( szValue && !szwValue )
1924 goto end;
1926 r = MsiSetPropertyW( hInstall, szwName, szwValue);
1928 end:
1929 msi_free( szwName );
1930 msi_free( szwValue );
1932 return r;
1935 void msi_reset_folders( MSIPACKAGE *package, BOOL source )
1937 MSIFOLDER *folder;
1939 LIST_FOR_EACH_ENTRY( folder, &package->folders, MSIFOLDER, entry )
1941 if ( source )
1943 msi_free( folder->ResolvedSource );
1944 folder->ResolvedSource = NULL;
1946 else
1948 msi_free( folder->ResolvedTarget );
1949 folder->ResolvedTarget = NULL;
1954 UINT msi_set_property( MSIDATABASE *db, LPCWSTR szName, LPCWSTR szValue )
1956 MSIQUERY *view;
1957 MSIRECORD *row = NULL;
1958 UINT rc;
1959 DWORD sz = 0;
1960 WCHAR Query[1024];
1962 static const WCHAR Insert[] = {
1963 'I','N','S','E','R','T',' ','i','n','t','o',' ',
1964 '`','_','P','r','o','p','e','r','t','y','`',' ','(',
1965 '`','_','P','r','o','p','e','r','t','y','`',',',
1966 '`','V','a','l','u','e','`',')',' ','V','A','L','U','E','S'
1967 ,' ','(','?',',','?',')',0};
1968 static const WCHAR Update[] = {
1969 'U','P','D','A','T','E',' ','`','_','P','r','o','p','e','r','t','y','`',
1970 ' ','s','e','t',' ','`','V','a','l','u','e','`',' ','=',' ','?',' ',
1971 'w','h','e','r','e',' ','`','_','P','r','o','p','e','r','t','y','`',
1972 ' ','=',' ','\'','%','s','\'',0};
1973 static const WCHAR Delete[] = {
1974 'D','E','L','E','T','E',' ','F','R','O','M',' ',
1975 '`','_','P','r','o','p','e','r','t','y','`',' ','W','H','E','R','E',' ',
1976 '`','_','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',0};
1978 TRACE("%p %s %s\n", db, debugstr_w(szName), debugstr_w(szValue));
1980 if (!szName)
1981 return ERROR_INVALID_PARAMETER;
1983 /* this one is weird... */
1984 if (!szName[0])
1985 return szValue ? ERROR_FUNCTION_FAILED : ERROR_SUCCESS;
1987 rc = msi_get_property(db, szName, 0, &sz);
1988 if (!szValue || !*szValue)
1990 sprintfW(Query, Delete, szName);
1992 else if (rc == ERROR_MORE_DATA || rc == ERROR_SUCCESS)
1994 sprintfW(Query, Update, szName);
1996 row = MSI_CreateRecord(1);
1997 MSI_RecordSetStringW(row, 1, szValue);
1999 else
2001 strcpyW(Query, Insert);
2003 row = MSI_CreateRecord(2);
2004 MSI_RecordSetStringW(row, 1, szName);
2005 MSI_RecordSetStringW(row, 2, szValue);
2008 rc = MSI_DatabaseOpenViewW(db, Query, &view);
2009 if (rc == ERROR_SUCCESS)
2011 rc = MSI_ViewExecute(view, row);
2012 MSI_ViewClose(view);
2013 msiobj_release(&view->hdr);
2016 if (row)
2017 msiobj_release(&row->hdr);
2019 return rc;
2022 UINT WINAPI MsiSetPropertyW( MSIHANDLE hInstall, LPCWSTR szName, LPCWSTR szValue)
2024 MSIPACKAGE *package;
2025 UINT ret;
2027 package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE);
2028 if( !package )
2030 HRESULT hr;
2031 BSTR name = NULL, value = NULL;
2032 IWineMsiRemotePackage *remote_package;
2034 remote_package = (IWineMsiRemotePackage *)msi_get_remote( hInstall );
2035 if (!remote_package)
2036 return ERROR_INVALID_HANDLE;
2038 name = SysAllocString( szName );
2039 value = SysAllocString( szValue );
2040 if ((!name && szName) || (!value && szValue))
2042 SysFreeString( name );
2043 SysFreeString( value );
2044 IWineMsiRemotePackage_Release( remote_package );
2045 return ERROR_OUTOFMEMORY;
2048 hr = IWineMsiRemotePackage_SetProperty( remote_package, name, value );
2050 SysFreeString( name );
2051 SysFreeString( value );
2052 IWineMsiRemotePackage_Release( remote_package );
2054 if (FAILED(hr))
2056 if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
2057 return HRESULT_CODE(hr);
2059 return ERROR_FUNCTION_FAILED;
2062 return ERROR_SUCCESS;
2065 ret = msi_set_property( package->db, szName, szValue );
2066 if (ret == ERROR_SUCCESS && !strcmpW( szName, cszSourceDir ))
2067 msi_reset_folders( package, TRUE );
2069 msiobj_release( &package->hdr );
2070 return ret;
2073 static MSIRECORD *msi_get_property_row( MSIDATABASE *db, LPCWSTR name )
2075 MSIQUERY *view;
2076 MSIRECORD *rec, *row = NULL;
2077 UINT r;
2079 static const WCHAR query[]= {
2080 'S','E','L','E','C','T',' ','`','V','a','l','u','e','`',' ',
2081 'F','R','O','M',' ' ,'`','_','P','r','o','p','e','r','t','y','`',
2082 ' ','W','H','E','R','E',' ' ,'`','_','P','r','o','p','e','r','t','y','`',
2083 '=','?',0};
2085 if (!name || !*name)
2086 return NULL;
2088 rec = MSI_CreateRecord(1);
2089 if (!rec)
2090 return NULL;
2092 MSI_RecordSetStringW(rec, 1, name);
2094 r = MSI_DatabaseOpenViewW(db, query, &view);
2095 if (r == ERROR_SUCCESS)
2097 MSI_ViewExecute(view, rec);
2098 MSI_ViewFetch(view, &row);
2099 MSI_ViewClose(view);
2100 msiobj_release(&view->hdr);
2103 msiobj_release(&rec->hdr);
2104 return row;
2107 /* internal function, not compatible with MsiGetPropertyW */
2108 UINT msi_get_property( MSIDATABASE *db, LPCWSTR szName,
2109 LPWSTR szValueBuf, LPDWORD pchValueBuf )
2111 MSIRECORD *row;
2112 UINT rc = ERROR_FUNCTION_FAILED;
2114 row = msi_get_property_row( db, szName );
2116 if (*pchValueBuf > 0)
2117 szValueBuf[0] = 0;
2119 if (row)
2121 rc = MSI_RecordGetStringW(row, 1, szValueBuf, pchValueBuf);
2122 msiobj_release(&row->hdr);
2125 if (rc == ERROR_SUCCESS)
2126 TRACE("returning %s for property %s\n", debugstr_w(szValueBuf),
2127 debugstr_w(szName));
2128 else if (rc == ERROR_MORE_DATA)
2129 TRACE("need %d sized buffer for %s\n", *pchValueBuf,
2130 debugstr_w(szName));
2131 else
2133 *pchValueBuf = 0;
2134 TRACE("property %s not found\n", debugstr_w(szName));
2137 return rc;
2140 LPWSTR msi_dup_property(MSIDATABASE *db, LPCWSTR prop)
2142 DWORD sz = 0;
2143 LPWSTR str;
2144 UINT r;
2146 r = msi_get_property(db, prop, NULL, &sz);
2147 if (r != ERROR_SUCCESS && r != ERROR_MORE_DATA)
2148 return NULL;
2150 sz++;
2151 str = msi_alloc(sz * sizeof(WCHAR));
2152 r = msi_get_property(db, prop, str, &sz);
2153 if (r != ERROR_SUCCESS)
2155 msi_free(str);
2156 str = NULL;
2159 return str;
2162 int msi_get_property_int( MSIDATABASE *db, LPCWSTR prop, int def )
2164 LPWSTR str = msi_dup_property( db, prop );
2165 int val = str ? atoiW(str) : def;
2166 msi_free(str);
2167 return val;
2170 static UINT MSI_GetProperty( MSIHANDLE handle, LPCWSTR name,
2171 awstring *szValueBuf, LPDWORD pchValueBuf )
2173 MSIPACKAGE *package;
2174 MSIRECORD *row = NULL;
2175 UINT r = ERROR_FUNCTION_FAILED;
2176 LPCWSTR val = NULL;
2178 TRACE("%u %s %p %p\n", handle, debugstr_w(name),
2179 szValueBuf->str.w, pchValueBuf );
2181 if (!name)
2182 return ERROR_INVALID_PARAMETER;
2184 package = msihandle2msiinfo( handle, MSIHANDLETYPE_PACKAGE );
2185 if (!package)
2187 HRESULT hr;
2188 IWineMsiRemotePackage *remote_package;
2189 LPWSTR value = NULL;
2190 BSTR bname;
2191 DWORD len;
2193 remote_package = (IWineMsiRemotePackage *)msi_get_remote( handle );
2194 if (!remote_package)
2195 return ERROR_INVALID_HANDLE;
2197 bname = SysAllocString( name );
2198 if (!bname)
2200 IWineMsiRemotePackage_Release( remote_package );
2201 return ERROR_OUTOFMEMORY;
2204 len = 0;
2205 hr = IWineMsiRemotePackage_GetProperty( remote_package, bname, NULL, &len );
2206 if (FAILED(hr))
2207 goto done;
2209 len++;
2210 value = msi_alloc(len * sizeof(WCHAR));
2211 if (!value)
2213 r = ERROR_OUTOFMEMORY;
2214 goto done;
2217 hr = IWineMsiRemotePackage_GetProperty( remote_package, bname, (BSTR *)value, &len );
2218 if (FAILED(hr))
2219 goto done;
2221 r = msi_strcpy_to_awstring( value, szValueBuf, pchValueBuf );
2223 /* Bug required by Adobe installers */
2224 if (!szValueBuf->unicode && !szValueBuf->str.a)
2225 *pchValueBuf *= sizeof(WCHAR);
2227 done:
2228 IWineMsiRemotePackage_Release(remote_package);
2229 SysFreeString(bname);
2230 msi_free(value);
2232 if (FAILED(hr))
2234 if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
2235 return HRESULT_CODE(hr);
2237 return ERROR_FUNCTION_FAILED;
2240 return r;
2243 row = msi_get_property_row( package->db, name );
2244 if (row)
2245 val = MSI_RecordGetString( row, 1 );
2247 if (!val)
2248 val = szEmpty;
2250 r = msi_strcpy_to_awstring( val, szValueBuf, pchValueBuf );
2252 if (row)
2253 msiobj_release( &row->hdr );
2254 msiobj_release( &package->hdr );
2256 return r;
2259 UINT WINAPI MsiGetPropertyA( MSIHANDLE hInstall, LPCSTR szName,
2260 LPSTR szValueBuf, LPDWORD pchValueBuf )
2262 awstring val;
2263 LPWSTR name;
2264 UINT r;
2266 val.unicode = FALSE;
2267 val.str.a = szValueBuf;
2269 name = strdupAtoW( szName );
2270 if (szName && !name)
2271 return ERROR_OUTOFMEMORY;
2273 r = MSI_GetProperty( hInstall, name, &val, pchValueBuf );
2274 msi_free( name );
2275 return r;
2278 UINT WINAPI MsiGetPropertyW( MSIHANDLE hInstall, LPCWSTR szName,
2279 LPWSTR szValueBuf, LPDWORD pchValueBuf )
2281 awstring val;
2283 val.unicode = TRUE;
2284 val.str.w = szValueBuf;
2286 return MSI_GetProperty( hInstall, szName, &val, pchValueBuf );
2289 typedef struct _msi_remote_package_impl {
2290 IWineMsiRemotePackage IWineMsiRemotePackage_iface;
2291 MSIHANDLE package;
2292 LONG refs;
2293 } msi_remote_package_impl;
2295 static inline msi_remote_package_impl *impl_from_IWineMsiRemotePackage( IWineMsiRemotePackage *iface )
2297 return CONTAINING_RECORD(iface, msi_remote_package_impl, IWineMsiRemotePackage_iface);
2300 static HRESULT WINAPI mrp_QueryInterface( IWineMsiRemotePackage *iface,
2301 REFIID riid,LPVOID *ppobj)
2303 if( IsEqualCLSID( riid, &IID_IUnknown ) ||
2304 IsEqualCLSID( riid, &IID_IWineMsiRemotePackage ) )
2306 IUnknown_AddRef( iface );
2307 *ppobj = iface;
2308 return S_OK;
2311 return E_NOINTERFACE;
2314 static ULONG WINAPI mrp_AddRef( IWineMsiRemotePackage *iface )
2316 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2318 return InterlockedIncrement( &This->refs );
2321 static ULONG WINAPI mrp_Release( IWineMsiRemotePackage *iface )
2323 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2324 ULONG r;
2326 r = InterlockedDecrement( &This->refs );
2327 if (r == 0)
2329 MsiCloseHandle( This->package );
2330 msi_free( This );
2332 return r;
2335 static HRESULT WINAPI mrp_SetMsiHandle( IWineMsiRemotePackage *iface, MSIHANDLE handle )
2337 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2338 This->package = handle;
2339 return S_OK;
2342 static HRESULT WINAPI mrp_GetActiveDatabase( IWineMsiRemotePackage *iface, MSIHANDLE *handle )
2344 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2345 IWineMsiRemoteDatabase *rdb = NULL;
2346 HRESULT hr;
2347 MSIHANDLE hdb;
2349 hr = create_msi_remote_database( NULL, (LPVOID *)&rdb );
2350 if (FAILED(hr) || !rdb)
2352 ERR("Failed to create remote database\n");
2353 return hr;
2356 hdb = MsiGetActiveDatabase(This->package);
2358 hr = IWineMsiRemoteDatabase_SetMsiHandle( rdb, hdb );
2359 if (FAILED(hr))
2361 ERR("Failed to set the database handle\n");
2362 return hr;
2365 *handle = alloc_msi_remote_handle( (IUnknown *)rdb );
2366 return S_OK;
2369 static HRESULT WINAPI mrp_GetProperty( IWineMsiRemotePackage *iface, BSTR property, BSTR *value, DWORD *size )
2371 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2372 UINT r;
2374 r = MsiGetPropertyW(This->package, (LPWSTR)property, (LPWSTR)value, size);
2375 if (r != ERROR_SUCCESS)
2376 return HRESULT_FROM_WIN32(r);
2378 return S_OK;
2381 static HRESULT WINAPI mrp_SetProperty( IWineMsiRemotePackage *iface, BSTR property, BSTR value )
2383 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2384 UINT r = MsiSetPropertyW(This->package, property, value);
2385 return HRESULT_FROM_WIN32(r);
2388 static HRESULT WINAPI mrp_ProcessMessage( IWineMsiRemotePackage *iface, INSTALLMESSAGE message, MSIHANDLE record )
2390 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2391 UINT r = MsiProcessMessage(This->package, message, record);
2392 return HRESULT_FROM_WIN32(r);
2395 static HRESULT WINAPI mrp_DoAction( IWineMsiRemotePackage *iface, BSTR action )
2397 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2398 UINT r = MsiDoActionW(This->package, action);
2399 return HRESULT_FROM_WIN32(r);
2402 static HRESULT WINAPI mrp_Sequence( IWineMsiRemotePackage *iface, BSTR table, int sequence )
2404 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2405 UINT r = MsiSequenceW(This->package, table, sequence);
2406 return HRESULT_FROM_WIN32(r);
2409 static HRESULT WINAPI mrp_GetTargetPath( IWineMsiRemotePackage *iface, BSTR folder, BSTR *value, DWORD *size )
2411 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2412 UINT r = MsiGetTargetPathW(This->package, (LPWSTR)folder, (LPWSTR)value, size);
2413 return HRESULT_FROM_WIN32(r);
2416 static HRESULT WINAPI mrp_SetTargetPath( IWineMsiRemotePackage *iface, BSTR folder, BSTR value)
2418 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2419 UINT r = MsiSetTargetPathW(This->package, folder, value);
2420 return HRESULT_FROM_WIN32(r);
2423 static HRESULT WINAPI mrp_GetSourcePath( IWineMsiRemotePackage *iface, BSTR folder, BSTR *value, DWORD *size )
2425 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2426 UINT r = MsiGetSourcePathW(This->package, (LPWSTR)folder, (LPWSTR)value, size);
2427 return HRESULT_FROM_WIN32(r);
2430 static HRESULT WINAPI mrp_GetMode( IWineMsiRemotePackage *iface, MSIRUNMODE mode, BOOL *ret )
2432 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2433 *ret = MsiGetMode(This->package, mode);
2434 return S_OK;
2437 static HRESULT WINAPI mrp_SetMode( IWineMsiRemotePackage *iface, MSIRUNMODE mode, BOOL state )
2439 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2440 UINT r = MsiSetMode(This->package, mode, state);
2441 return HRESULT_FROM_WIN32(r);
2444 static HRESULT WINAPI mrp_GetFeatureState( IWineMsiRemotePackage *iface, BSTR feature,
2445 INSTALLSTATE *installed, INSTALLSTATE *action )
2447 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2448 UINT r = MsiGetFeatureStateW(This->package, feature, installed, action);
2449 return HRESULT_FROM_WIN32(r);
2452 static HRESULT WINAPI mrp_SetFeatureState( IWineMsiRemotePackage *iface, BSTR feature, INSTALLSTATE state )
2454 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2455 UINT r = MsiSetFeatureStateW(This->package, feature, state);
2456 return HRESULT_FROM_WIN32(r);
2459 static HRESULT WINAPI mrp_GetComponentState( IWineMsiRemotePackage *iface, BSTR component,
2460 INSTALLSTATE *installed, INSTALLSTATE *action )
2462 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2463 UINT r = MsiGetComponentStateW(This->package, component, installed, action);
2464 return HRESULT_FROM_WIN32(r);
2467 static HRESULT WINAPI mrp_SetComponentState( IWineMsiRemotePackage *iface, BSTR component, INSTALLSTATE state )
2469 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2470 UINT r = MsiSetComponentStateW(This->package, component, state);
2471 return HRESULT_FROM_WIN32(r);
2474 static HRESULT WINAPI mrp_GetLanguage( IWineMsiRemotePackage *iface, LANGID *language )
2476 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2477 *language = MsiGetLanguage(This->package);
2478 return S_OK;
2481 static HRESULT WINAPI mrp_SetInstallLevel( IWineMsiRemotePackage *iface, int level )
2483 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2484 UINT r = MsiSetInstallLevel(This->package, level);
2485 return HRESULT_FROM_WIN32(r);
2488 static HRESULT WINAPI mrp_FormatRecord( IWineMsiRemotePackage *iface, MSIHANDLE record,
2489 BSTR *value)
2491 DWORD size = 0;
2492 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2493 UINT r = MsiFormatRecordW(This->package, record, NULL, &size);
2494 if (r == ERROR_SUCCESS)
2496 *value = SysAllocStringLen(NULL, size);
2497 if (!*value)
2498 return E_OUTOFMEMORY;
2499 size++;
2500 r = MsiFormatRecordW(This->package, record, *value, &size);
2502 return HRESULT_FROM_WIN32(r);
2505 static HRESULT WINAPI mrp_EvaluateCondition( IWineMsiRemotePackage *iface, BSTR condition )
2507 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2508 UINT r = MsiEvaluateConditionW(This->package, condition);
2509 return HRESULT_FROM_WIN32(r);
2512 static HRESULT WINAPI mrp_GetFeatureCost( IWineMsiRemotePackage *iface, BSTR feature,
2513 INT cost_tree, INSTALLSTATE state, INT *cost )
2515 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2516 UINT r = MsiGetFeatureCostW(This->package, feature, cost_tree, state, cost);
2517 return HRESULT_FROM_WIN32(r);
2520 static const IWineMsiRemotePackageVtbl msi_remote_package_vtbl =
2522 mrp_QueryInterface,
2523 mrp_AddRef,
2524 mrp_Release,
2525 mrp_SetMsiHandle,
2526 mrp_GetActiveDatabase,
2527 mrp_GetProperty,
2528 mrp_SetProperty,
2529 mrp_ProcessMessage,
2530 mrp_DoAction,
2531 mrp_Sequence,
2532 mrp_GetTargetPath,
2533 mrp_SetTargetPath,
2534 mrp_GetSourcePath,
2535 mrp_GetMode,
2536 mrp_SetMode,
2537 mrp_GetFeatureState,
2538 mrp_SetFeatureState,
2539 mrp_GetComponentState,
2540 mrp_SetComponentState,
2541 mrp_GetLanguage,
2542 mrp_SetInstallLevel,
2543 mrp_FormatRecord,
2544 mrp_EvaluateCondition,
2545 mrp_GetFeatureCost,
2548 HRESULT create_msi_remote_package( IUnknown *pOuter, LPVOID *ppObj )
2550 msi_remote_package_impl* This;
2552 This = msi_alloc( sizeof *This );
2553 if (!This)
2554 return E_OUTOFMEMORY;
2556 This->IWineMsiRemotePackage_iface.lpVtbl = &msi_remote_package_vtbl;
2557 This->package = 0;
2558 This->refs = 1;
2560 *ppObj = This;
2562 return S_OK;
2565 UINT msi_package_add_info(MSIPACKAGE *package, DWORD context, DWORD options,
2566 LPCWSTR property, LPWSTR value)
2568 MSISOURCELISTINFO *info;
2570 info = msi_alloc(sizeof(MSISOURCELISTINFO));
2571 if (!info)
2572 return ERROR_OUTOFMEMORY;
2574 info->context = context;
2575 info->options = options;
2576 info->property = property;
2577 info->value = strdupW(value);
2578 list_add_head(&package->sourcelist_info, &info->entry);
2580 return ERROR_SUCCESS;
2583 UINT msi_package_add_media_disk(MSIPACKAGE *package, DWORD context, DWORD options,
2584 DWORD disk_id, LPWSTR volume_label, LPWSTR disk_prompt)
2586 MSIMEDIADISK *disk;
2588 disk = msi_alloc(sizeof(MSIMEDIADISK));
2589 if (!disk)
2590 return ERROR_OUTOFMEMORY;
2592 disk->context = context;
2593 disk->options = options;
2594 disk->disk_id = disk_id;
2595 disk->volume_label = strdupW(volume_label);
2596 disk->disk_prompt = strdupW(disk_prompt);
2597 list_add_head(&package->sourcelist_media, &disk->entry);
2599 return ERROR_SUCCESS;