windows.networking.hostname/tests: Check if passed HSTRING is duplicated.
[wine.git] / dlls / msi / package.c
blob8c2168d9226b928db09911bff4a791634155e1a6
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 COBJMACROS
23 #include <stdarg.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winreg.h"
27 #include "winnls.h"
28 #include "winternl.h"
29 #include "shlwapi.h"
30 #include "wingdi.h"
31 #include "msi.h"
32 #include "msiquery.h"
33 #include "objidl.h"
34 #include "wincrypt.h"
35 #include "winuser.h"
36 #include "wininet.h"
37 #include "winver.h"
38 #include "urlmon.h"
39 #include "shlobj.h"
40 #include "objbase.h"
41 #include "msidefs.h"
42 #include "sddl.h"
44 #include "wine/debug.h"
45 #include "wine/exception.h"
47 #include "msipriv.h"
48 #include "winemsi.h"
49 #include "resource.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(msi);
53 static void free_feature( MSIFEATURE *feature )
55 struct list *item, *cursor;
57 LIST_FOR_EACH_SAFE( item, cursor, &feature->Children )
59 FeatureList *fl = LIST_ENTRY( item, FeatureList, entry );
60 list_remove( &fl->entry );
61 free( fl );
64 LIST_FOR_EACH_SAFE( item, cursor, &feature->Components )
66 ComponentList *cl = LIST_ENTRY( item, ComponentList, entry );
67 list_remove( &cl->entry );
68 free( cl );
70 free( feature->Feature );
71 free( feature->Feature_Parent );
72 free( feature->Directory );
73 free( feature->Description );
74 free( feature->Title );
75 free( feature );
78 static void free_folder( MSIFOLDER *folder )
80 struct list *item, *cursor;
82 LIST_FOR_EACH_SAFE( item, cursor, &folder->children )
84 FolderList *fl = LIST_ENTRY( item, FolderList, entry );
85 list_remove( &fl->entry );
86 free( fl );
88 free( folder->Parent );
89 free( folder->Directory );
90 free( folder->TargetDefault );
91 free( folder->SourceLongPath );
92 free( folder->SourceShortPath );
93 free( folder->ResolvedTarget );
94 free( folder->ResolvedSource );
95 free( folder );
98 static void free_extension( MSIEXTENSION *ext )
100 struct list *item, *cursor;
102 LIST_FOR_EACH_SAFE( item, cursor, &ext->verbs )
104 MSIVERB *verb = LIST_ENTRY( item, MSIVERB, entry );
106 list_remove( &verb->entry );
107 free( verb->Verb );
108 free( verb->Command );
109 free( verb->Argument );
110 free( verb );
113 free( ext->Extension );
114 free( ext->ProgIDText );
115 free( ext );
118 static void free_assembly( MSIASSEMBLY *assembly )
120 free( assembly->feature );
121 free( assembly->manifest );
122 free( assembly->application );
123 free( assembly->display_name );
124 if (assembly->tempdir) RemoveDirectoryW( assembly->tempdir );
125 free( assembly->tempdir );
126 free( assembly );
129 void msi_free_action_script( MSIPACKAGE *package, UINT script )
131 UINT i;
132 for (i = 0; i < package->script_actions_count[script]; i++)
133 free( package->script_actions[script][i] );
135 free( package->script_actions[script] );
136 package->script_actions[script] = NULL;
137 package->script_actions_count[script] = 0;
140 static void free_package_structures( MSIPACKAGE *package )
142 struct list *item, *cursor;
143 int i;
145 LIST_FOR_EACH_SAFE( item, cursor, &package->features )
147 MSIFEATURE *feature = LIST_ENTRY( item, MSIFEATURE, entry );
148 list_remove( &feature->entry );
149 free_feature( feature );
152 LIST_FOR_EACH_SAFE( item, cursor, &package->folders )
154 MSIFOLDER *folder = LIST_ENTRY( item, MSIFOLDER, entry );
155 list_remove( &folder->entry );
156 free_folder( folder );
159 LIST_FOR_EACH_SAFE( item, cursor, &package->files )
161 MSIFILE *file = LIST_ENTRY( item, MSIFILE, entry );
163 list_remove( &file->entry );
164 free( file->File );
165 free( file->FileName );
166 free( file->ShortName );
167 free( file->LongName );
168 free( file->Version );
169 free( file->Language );
170 if (msi_is_global_assembly( file->Component )) DeleteFileW( file->TargetPath );
171 free( file->TargetPath );
172 free( file );
175 LIST_FOR_EACH_SAFE( item, cursor, &package->components )
177 MSICOMPONENT *comp = LIST_ENTRY( item, MSICOMPONENT, entry );
179 list_remove( &comp->entry );
180 free( comp->Component );
181 free( comp->ComponentId );
182 free( comp->Directory );
183 free( comp->Condition );
184 free( comp->KeyPath );
185 free( comp->FullKeypath );
186 if (comp->assembly) free_assembly( comp->assembly );
187 free( comp );
190 LIST_FOR_EACH_SAFE( item, cursor, &package->filepatches )
192 MSIFILEPATCH *patch = LIST_ENTRY( item, MSIFILEPATCH, entry );
194 list_remove( &patch->entry );
195 free( patch->path );
196 free( patch );
199 /* clean up extension, progid, class and verb structures */
200 LIST_FOR_EACH_SAFE( item, cursor, &package->classes )
202 MSICLASS *cls = LIST_ENTRY( item, MSICLASS, entry );
204 list_remove( &cls->entry );
205 free( cls->clsid );
206 free( cls->Context );
207 free( cls->Description );
208 free( cls->FileTypeMask );
209 free( cls->IconPath );
210 free( cls->DefInprocHandler );
211 free( cls->DefInprocHandler32 );
212 free( cls->Argument );
213 free( cls->ProgIDText );
214 free( cls );
217 LIST_FOR_EACH_SAFE( item, cursor, &package->extensions )
219 MSIEXTENSION *ext = LIST_ENTRY( item, MSIEXTENSION, entry );
221 list_remove( &ext->entry );
222 free_extension( ext );
225 LIST_FOR_EACH_SAFE( item, cursor, &package->progids )
227 MSIPROGID *progid = LIST_ENTRY( item, MSIPROGID, entry );
229 list_remove( &progid->entry );
230 free( progid->ProgID );
231 free( progid->Description );
232 free( progid->IconPath );
233 free( progid );
236 LIST_FOR_EACH_SAFE( item, cursor, &package->mimes )
238 MSIMIME *mt = LIST_ENTRY( item, MSIMIME, entry );
240 list_remove( &mt->entry );
241 free( mt->suffix );
242 free( mt->clsid );
243 free( mt->ContentType );
244 free( mt );
247 LIST_FOR_EACH_SAFE( item, cursor, &package->appids )
249 MSIAPPID *appid = LIST_ENTRY( item, MSIAPPID, entry );
251 list_remove( &appid->entry );
252 free( appid->AppID );
253 free( appid->RemoteServerName );
254 free( appid->LocalServer );
255 free( appid->ServiceParameters );
256 free( appid->DllSurrogate );
257 free( appid );
260 LIST_FOR_EACH_SAFE( item, cursor, &package->sourcelist_info )
262 MSISOURCELISTINFO *info = LIST_ENTRY( item, MSISOURCELISTINFO, entry );
264 list_remove( &info->entry );
265 free( info->value );
266 free( info );
269 LIST_FOR_EACH_SAFE( item, cursor, &package->sourcelist_media )
271 MSIMEDIADISK *info = LIST_ENTRY( item, MSIMEDIADISK, entry );
273 list_remove( &info->entry );
274 free( info->volume_label );
275 free( info->disk_prompt );
276 free( info );
279 for (i = 0; i < SCRIPT_MAX; i++)
280 msi_free_action_script( package, i );
282 for (i = 0; i < package->unique_actions_count; i++)
283 free( package->unique_actions[i] );
284 free( package->unique_actions );
286 LIST_FOR_EACH_SAFE( item, cursor, &package->binaries )
288 MSIBINARY *binary = LIST_ENTRY( item, MSIBINARY, entry );
290 list_remove( &binary->entry );
291 if (!DeleteFileW( binary->tmpfile ))
292 ERR( "failed to delete %s (%lu)\n", debugstr_w(binary->tmpfile), GetLastError() );
293 free( binary->source );
294 free( binary->tmpfile );
295 free( binary );
298 LIST_FOR_EACH_SAFE( item, cursor, &package->cabinet_streams )
300 MSICABINETSTREAM *cab = LIST_ENTRY( item, MSICABINETSTREAM, entry );
302 list_remove( &cab->entry );
303 IStorage_Release( cab->storage );
304 free( cab->stream );
305 free( cab );
308 LIST_FOR_EACH_SAFE( item, cursor, &package->patches )
310 MSIPATCHINFO *patch = LIST_ENTRY( item, MSIPATCHINFO, entry );
312 list_remove( &patch->entry );
313 if (patch->delete_on_close && !DeleteFileW( patch->localfile ))
315 ERR( "failed to delete %s (%lu)\n", debugstr_w(patch->localfile), GetLastError() );
317 msi_free_patchinfo( patch );
320 free( package->PackagePath );
321 free( package->ProductCode );
322 free( package->ActionFormat );
323 free( package->LastAction );
324 free( package->LastActionTemplate );
325 free( package->langids );
327 /* cleanup control event subscriptions */
328 msi_event_cleanup_all_subscriptions( package );
331 static void MSI_FreePackage( MSIOBJECTHDR *arg)
333 MSIPACKAGE *package = (MSIPACKAGE *)arg;
335 msi_destroy_assembly_caches( package );
337 if( package->dialog )
338 msi_dialog_destroy( package->dialog );
340 msiobj_release( &package->db->hdr );
341 free_package_structures(package);
342 CloseHandle( package->log_file );
343 if (package->rpc_server_started)
344 RpcServerUnregisterIf(s_IWineMsiRemote_v0_0_s_ifspec, NULL, FALSE);
345 if (rpc_handle)
346 RpcBindingFree(&rpc_handle);
347 if (package->custom_server_32_process)
348 custom_stop_server(package->custom_server_32_process, package->custom_server_32_pipe);
349 if (package->custom_server_64_process)
350 custom_stop_server(package->custom_server_64_process, package->custom_server_64_pipe);
352 if (package->delete_on_close) DeleteFileW( package->localfile );
353 free( package->localfile );
354 MSI_ProcessMessage(NULL, INSTALLMESSAGE_TERMINATE, 0);
357 static UINT create_temp_property_table(MSIPACKAGE *package)
359 MSIQUERY *view;
360 UINT rc;
362 rc = MSI_DatabaseOpenViewW(package->db, L"CREATE TABLE `_Property` ( `_Property` CHAR(56) NOT NULL TEMPORARY, "
363 L"`Value` CHAR(98) NOT NULL TEMPORARY PRIMARY KEY `_Property`) HOLD", &view);
364 if (rc != ERROR_SUCCESS)
365 return rc;
367 rc = MSI_ViewExecute(view, 0);
368 MSI_ViewClose(view);
369 msiobj_release(&view->hdr);
370 return rc;
373 UINT msi_clone_properties( MSIDATABASE *db )
375 MSIQUERY *view_select;
376 UINT rc;
378 rc = MSI_DatabaseOpenViewW( db, L"SELECT * FROM `Property`", &view_select );
379 if (rc != ERROR_SUCCESS)
380 return rc;
382 rc = MSI_ViewExecute( view_select, 0 );
383 if (rc != ERROR_SUCCESS)
385 MSI_ViewClose( view_select );
386 msiobj_release( &view_select->hdr );
387 return rc;
390 while (1)
392 MSIQUERY *view_insert, *view_update;
393 MSIRECORD *rec_select;
395 rc = MSI_ViewFetch( view_select, &rec_select );
396 if (rc != ERROR_SUCCESS)
397 break;
399 rc = MSI_DatabaseOpenViewW( db, L"INSERT INTO `_Property` (`_Property`,`Value`) VALUES (?,?)", &view_insert );
400 if (rc != ERROR_SUCCESS)
402 msiobj_release( &rec_select->hdr );
403 continue;
406 rc = MSI_ViewExecute( view_insert, rec_select );
407 MSI_ViewClose( view_insert );
408 msiobj_release( &view_insert->hdr );
409 if (rc != ERROR_SUCCESS)
411 MSIRECORD *rec_update;
413 TRACE("insert failed, trying update\n");
415 rc = MSI_DatabaseOpenViewW( db, L"UPDATE `_Property` SET `Value` = ? WHERE `_Property` = ?", &view_update );
416 if (rc != ERROR_SUCCESS)
418 WARN("open view failed %u\n", rc);
419 msiobj_release( &rec_select->hdr );
420 continue;
423 rec_update = MSI_CreateRecord( 2 );
424 MSI_RecordCopyField( rec_select, 1, rec_update, 2 );
425 MSI_RecordCopyField( rec_select, 2, rec_update, 1 );
426 rc = MSI_ViewExecute( view_update, rec_update );
427 if (rc != ERROR_SUCCESS)
428 WARN("update failed %u\n", rc);
430 MSI_ViewClose( view_update );
431 msiobj_release( &view_update->hdr );
432 msiobj_release( &rec_update->hdr );
435 msiobj_release( &rec_select->hdr );
438 MSI_ViewClose( view_select );
439 msiobj_release( &view_select->hdr );
440 return rc;
444 * set_installed_prop
446 * Sets the "Installed" property to indicate that
447 * the product is installed for the current user.
449 static UINT set_installed_prop( MSIPACKAGE *package )
451 HKEY hkey;
452 UINT r;
454 if (!package->ProductCode) return ERROR_FUNCTION_FAILED;
456 r = MSIREG_OpenUninstallKey( package->ProductCode, package->platform, &hkey, FALSE );
457 if (r == ERROR_SUCCESS)
459 RegCloseKey( hkey );
460 msi_set_property( package->db, L"Installed", L"1", -1 );
462 return r;
465 static UINT set_user_sid_prop( MSIPACKAGE *package )
467 SID_NAME_USE use;
468 LPWSTR user_name;
469 LPWSTR sid_str = NULL, dom = NULL;
470 DWORD size, dom_size;
471 PSID psid = NULL;
472 UINT r = ERROR_FUNCTION_FAILED;
474 size = 0;
475 GetUserNameW( NULL, &size );
477 user_name = malloc( (size + 1) * sizeof(WCHAR) );
478 if (!user_name)
479 return ERROR_OUTOFMEMORY;
481 if (!GetUserNameW( user_name, &size ))
482 goto done;
484 size = 0;
485 dom_size = 0;
486 LookupAccountNameW( NULL, user_name, NULL, &size, NULL, &dom_size, &use );
488 psid = malloc( size );
489 dom = malloc( dom_size * sizeof (WCHAR) );
490 if (!psid || !dom)
492 r = ERROR_OUTOFMEMORY;
493 goto done;
496 if (!LookupAccountNameW( NULL, user_name, psid, &size, dom, &dom_size, &use ))
497 goto done;
499 if (!ConvertSidToStringSidW( psid, &sid_str ))
500 goto done;
502 r = msi_set_property( package->db, L"UserSID", sid_str, -1 );
504 done:
505 LocalFree( sid_str );
506 free( dom );
507 free( psid );
508 free( user_name );
510 return r;
513 static LPWSTR get_fusion_filename(MSIPACKAGE *package)
515 HKEY netsetup, hkey;
516 LONG res;
517 DWORD size, len, type;
518 WCHAR windir[MAX_PATH], path[MAX_PATH], *filename = NULL;
520 res = RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\NET Framework Setup\\NDP", 0, KEY_CREATE_SUB_KEY,
521 &netsetup);
522 if (res != ERROR_SUCCESS)
523 return NULL;
525 if (!RegCreateKeyExW(netsetup, L"v4\\Client", 0, NULL, 0, KEY_QUERY_VALUE, NULL, &hkey, NULL))
527 size = sizeof(path);
528 if (!RegQueryValueExW(hkey, L"InstallPath", NULL, &type, (BYTE *)path, &size))
530 len = lstrlenW(path) + lstrlenW(L"fusion.dll") + 2;
531 if (!(filename = malloc(len * sizeof(WCHAR)))) return NULL;
533 lstrcpyW(filename, path);
534 lstrcatW(filename, L"\\");
535 lstrcatW(filename, L"fusion.dll");
536 if (GetFileAttributesW(filename) != INVALID_FILE_ATTRIBUTES)
538 TRACE( "found %s\n", debugstr_w(filename) );
539 RegCloseKey(hkey);
540 RegCloseKey(netsetup);
541 return filename;
544 RegCloseKey(hkey);
547 if (!RegCreateKeyExW(netsetup, L"v2.0.50727", 0, NULL, 0, KEY_QUERY_VALUE, NULL, &hkey, NULL))
549 RegCloseKey(hkey);
550 GetWindowsDirectoryW(windir, MAX_PATH);
551 len = lstrlenW(windir) + lstrlenW(L"Microsoft.NET\\Framework\\") + lstrlenW(L"v2.0.50727") +
552 lstrlenW(L"fusion.dll") + 3;
553 free(filename);
554 if (!(filename = malloc(len * sizeof(WCHAR)))) return NULL;
556 lstrcpyW(filename, windir);
557 lstrcatW(filename, L"\\");
558 lstrcatW(filename, L"Microsoft.NET\\Framework\\");
559 lstrcatW(filename, L"v2.0.50727");
560 lstrcatW(filename, L"\\");
561 lstrcatW(filename, L"fusion.dll");
562 if (GetFileAttributesW(filename) != INVALID_FILE_ATTRIBUTES)
564 TRACE( "found %s\n", debugstr_w(filename) );
565 RegCloseKey(netsetup);
566 return filename;
570 RegCloseKey(netsetup);
571 return filename;
574 struct lang_codepage
576 WORD wLanguage;
577 WORD wCodePage;
580 static void set_msi_assembly_prop(MSIPACKAGE *package)
582 UINT val_len;
583 DWORD size, handle;
584 LPVOID version = NULL;
585 WCHAR buf[MAX_PATH];
586 LPWSTR fusion, verstr;
587 struct lang_codepage *translate;
589 fusion = get_fusion_filename(package);
590 if (!fusion)
591 return;
593 size = GetFileVersionInfoSizeW(fusion, &handle);
594 if (!size)
595 goto done;
597 version = malloc(size);
598 if (!version)
599 goto done;
601 if (!GetFileVersionInfoW(fusion, handle, size, version))
602 goto done;
604 if (!VerQueryValueW(version, L"\\VarFileInfo\\Translation", (LPVOID *)&translate, &val_len))
605 goto done;
607 swprintf(buf, ARRAY_SIZE(buf), L"\\StringFileInfo\\%04x%04x\\ProductVersion", translate[0].wLanguage,
608 translate[0].wCodePage);
610 if (!VerQueryValueW(version, buf, (LPVOID *)&verstr, &val_len))
611 goto done;
613 if (!val_len || !verstr)
614 goto done;
616 msi_set_property( package->db, L"MsiNetAssemblySupport", verstr, -1 );
618 done:
619 free(fusion);
620 free(version);
623 static VOID set_installer_properties(MSIPACKAGE *package)
625 WCHAR *ptr;
626 RTL_OSVERSIONINFOEXW OSVersion;
627 MEMORYSTATUSEX msex;
628 DWORD verval, len, type;
629 WCHAR pth[MAX_PATH], verstr[11], bufstr[22];
630 HDC dc;
631 HKEY hkey;
632 LPWSTR username, companyname;
633 SYSTEM_INFO sys_info;
634 LANGID langid;
637 * Other things that probably should be set:
639 * VirtualMemory ShellAdvSupport DefaultUIFont PackagecodeChanging
640 * CaptionHeight BorderTop BorderSide TextHeight RedirectedDllSupport
643 SHGetFolderPathW(NULL, CSIDL_COMMON_APPDATA, NULL, 0, pth);
644 lstrcatW(pth, L"\\");
645 msi_set_property( package->db, L"CommonAppDataFolder", pth, -1 );
647 SHGetFolderPathW(NULL, CSIDL_FAVORITES, NULL, 0, pth);
648 lstrcatW(pth, L"\\");
649 msi_set_property( package->db, L"FavoritesFolder", pth, -1 );
651 SHGetFolderPathW(NULL, CSIDL_FONTS, NULL, 0, pth);
652 lstrcatW(pth, L"\\");
653 msi_set_property( package->db, L"FontsFolder", pth, -1 );
655 SHGetFolderPathW(NULL, CSIDL_SENDTO, NULL, 0, pth);
656 lstrcatW(pth, L"\\");
657 msi_set_property( package->db, L"SendToFolder", pth, -1 );
659 SHGetFolderPathW(NULL, CSIDL_STARTMENU, NULL, 0, pth);
660 lstrcatW(pth, L"\\");
661 msi_set_property( package->db, L"StartMenuFolder", pth, -1 );
663 SHGetFolderPathW(NULL, CSIDL_STARTUP, NULL, 0, pth);
664 lstrcatW(pth, L"\\");
665 msi_set_property( package->db, L"StartupFolder", pth, -1 );
667 SHGetFolderPathW(NULL, CSIDL_TEMPLATES, NULL, 0, pth);
668 lstrcatW(pth, L"\\");
669 msi_set_property( package->db, L"TemplateFolder", pth, -1 );
671 SHGetFolderPathW(NULL, CSIDL_DESKTOP, NULL, 0, pth);
672 lstrcatW(pth, L"\\");
673 msi_set_property( package->db, L"DesktopFolder", pth, -1 );
675 /* FIXME: set to AllUsers profile path if ALLUSERS is set */
676 SHGetFolderPathW(NULL, CSIDL_PROGRAMS, NULL, 0, pth);
677 lstrcatW(pth, L"\\");
678 msi_set_property( package->db, L"ProgramMenuFolder", pth, -1 );
680 SHGetFolderPathW(NULL, CSIDL_ADMINTOOLS, NULL, 0, pth);
681 lstrcatW(pth, L"\\");
682 msi_set_property( package->db, L"AdminToolsFolder", pth, -1 );
684 SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, 0, pth);
685 lstrcatW(pth, L"\\");
686 msi_set_property( package->db, L"AppDataFolder", pth, -1 );
688 SHGetFolderPathW(NULL, CSIDL_SYSTEM, NULL, 0, pth);
689 lstrcatW(pth, L"\\");
690 msi_set_property( package->db, L"SystemFolder", pth, -1 );
691 msi_set_property( package->db, L"System16Folder", pth, -1 );
693 SHGetFolderPathW(NULL, CSIDL_LOCAL_APPDATA, NULL, 0, pth);
694 lstrcatW(pth, L"\\");
695 msi_set_property( package->db, L"LocalAppDataFolder", pth, -1 );
697 SHGetFolderPathW(NULL, CSIDL_MYPICTURES, NULL, 0, pth);
698 lstrcatW(pth, L"\\");
699 msi_set_property( package->db, L"MyPicturesFolder", pth, -1 );
701 SHGetFolderPathW(NULL, CSIDL_PERSONAL, NULL, 0, pth);
702 lstrcatW(pth, L"\\");
703 msi_set_property( package->db, L"PersonalFolder", pth, -1 );
705 SHGetFolderPathW(NULL, CSIDL_WINDOWS, NULL, 0, pth);
706 lstrcatW(pth, L"\\");
707 msi_set_property( package->db, L"WindowsFolder", pth, -1 );
709 SHGetFolderPathW(NULL, CSIDL_PRINTHOOD, NULL, 0, pth);
710 lstrcatW(pth, L"\\");
711 msi_set_property( package->db, L"PrintHoodFolder", pth, -1 );
713 SHGetFolderPathW(NULL, CSIDL_NETHOOD, NULL, 0, pth);
714 lstrcatW(pth, L"\\");
715 msi_set_property( package->db, L"NetHoodFolder", pth, -1 );
717 SHGetFolderPathW(NULL, CSIDL_RECENT, NULL, 0, pth);
718 lstrcatW(pth, L"\\");
719 msi_set_property( package->db, L"RecentFolder", pth, -1 );
721 /* Physical Memory is specified in MB. Using total amount. */
722 msex.dwLength = sizeof(msex);
723 GlobalMemoryStatusEx( &msex );
724 len = swprintf( bufstr, ARRAY_SIZE(bufstr), L"%d", (int)(msex.ullTotalPhys / 1024 / 1024) );
725 msi_set_property( package->db, L"PhysicalMemory", bufstr, len );
727 SHGetFolderPathW(NULL, CSIDL_WINDOWS, NULL, 0, pth);
728 ptr = wcschr(pth,'\\');
729 if (ptr) *(ptr + 1) = 0;
730 msi_set_property( package->db, L"WindowsVolume", pth, -1 );
732 len = GetTempPathW(MAX_PATH, pth);
733 msi_set_property( package->db, L"TempFolder", pth, len );
735 /* in a wine environment the user is always admin and privileged */
736 msi_set_property( package->db, L"AdminUser", L"1", -1 );
737 msi_set_property( package->db, L"Privileged", L"1", -1 );
738 msi_set_property( package->db, L"MsiRunningElevated", L"1", -1 );
740 /* set the os things */
741 OSVersion.dwOSVersionInfoSize = sizeof(OSVersion);
742 RtlGetVersion(&OSVersion);
743 verval = OSVersion.dwMinorVersion + OSVersion.dwMajorVersion * 100;
744 if (verval > 603)
746 verval = 603;
747 OSVersion.dwBuildNumber = 9600;
749 len = swprintf( verstr, ARRAY_SIZE(verstr), L"%u", verval );
750 switch (OSVersion.dwPlatformId)
752 case VER_PLATFORM_WIN32_WINDOWS:
753 msi_set_property( package->db, L"Version9X", verstr, len );
754 break;
755 case VER_PLATFORM_WIN32_NT:
756 msi_set_property( package->db, L"VersionNT", verstr, len );
757 len = swprintf( bufstr, ARRAY_SIZE(bufstr), L"%u", OSVersion.wProductType );
758 msi_set_property( package->db, L"MsiNTProductType", bufstr, len );
759 break;
761 len = swprintf( bufstr, ARRAY_SIZE(bufstr), L"%u", OSVersion.dwBuildNumber );
762 msi_set_property( package->db, L"WindowsBuild", bufstr, len );
763 len = swprintf( bufstr, ARRAY_SIZE(bufstr), L"%u", OSVersion.wServicePackMajor );
764 msi_set_property( package->db, L"ServicePackLevel", bufstr, len );
766 len = swprintf( bufstr, ARRAY_SIZE(bufstr), L"%u.%u", MSI_MAJORVERSION, MSI_MINORVERSION );
767 msi_set_property( package->db, L"VersionMsi", bufstr, len );
768 len = swprintf( bufstr, ARRAY_SIZE(bufstr), L"%u", MSI_MAJORVERSION * 100 );
769 msi_set_property( package->db, L"VersionDatabase", bufstr, len );
771 RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion", 0,
772 KEY_QUERY_VALUE | KEY_WOW64_64KEY, &hkey);
774 GetNativeSystemInfo( &sys_info );
775 len = swprintf( bufstr, ARRAY_SIZE(bufstr), L"%d", sys_info.wProcessorLevel );
776 msi_set_property( package->db, L"Intel", bufstr, len );
777 if (sys_info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL)
779 GetSystemDirectoryW( pth, MAX_PATH );
780 PathAddBackslashW( pth );
781 msi_set_property( package->db, L"SystemFolder", pth, -1 );
783 len = sizeof(pth);
784 RegQueryValueExW(hkey, L"ProgramFilesDir", 0, &type, (BYTE *)pth, &len);
785 PathAddBackslashW( pth );
786 msi_set_property( package->db, L"ProgramFilesFolder", pth, -1 );
788 len = sizeof(pth);
789 RegQueryValueExW(hkey, L"CommonFilesDir", 0, &type, (BYTE *)pth, &len);
790 PathAddBackslashW( pth );
791 msi_set_property( package->db, L"CommonFilesFolder", pth, -1 );
793 else if (sys_info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
795 msi_set_property( package->db, L"MsiAMD64", bufstr, -1 );
796 msi_set_property( package->db, L"Msix64", bufstr, -1 );
797 msi_set_property( package->db, L"VersionNT64", verstr, -1 );
799 GetSystemDirectoryW( pth, MAX_PATH );
800 PathAddBackslashW( pth );
801 msi_set_property( package->db, L"System64Folder", pth, -1 );
803 GetSystemWow64DirectoryW( pth, MAX_PATH );
804 PathAddBackslashW( pth );
805 msi_set_property( package->db, L"SystemFolder", pth, -1 );
807 len = sizeof(pth);
808 RegQueryValueExW(hkey, L"ProgramFilesDir", 0, &type, (BYTE *)pth, &len);
809 PathAddBackslashW( pth );
810 msi_set_property( package->db, L"ProgramFiles64Folder", pth, -1 );
812 len = sizeof(pth);
813 RegQueryValueExW(hkey, L"ProgramFilesDir (x86)", 0, &type, (BYTE *)pth, &len);
814 PathAddBackslashW( pth );
815 msi_set_property( package->db, L"ProgramFilesFolder", pth, -1 );
817 len = sizeof(pth);
818 RegQueryValueExW(hkey, L"CommonFilesDir", 0, &type, (BYTE *)pth, &len);
819 PathAddBackslashW( pth );
820 msi_set_property( package->db, L"CommonFiles64Folder", pth, -1 );
822 len = sizeof(pth);
823 RegQueryValueExW(hkey, L"CommonFilesDir (x86)", 0, &type, (BYTE *)pth, &len);
824 PathAddBackslashW( pth );
825 msi_set_property( package->db, L"CommonFilesFolder", pth, -1 );
828 RegCloseKey(hkey);
830 /* Screen properties. */
831 dc = GetDC(0);
832 len = swprintf( bufstr, ARRAY_SIZE(bufstr), L"%d", GetDeviceCaps(dc, HORZRES) );
833 msi_set_property( package->db, L"ScreenX", bufstr, len );
834 len = swprintf( bufstr, ARRAY_SIZE(bufstr), L"%d", GetDeviceCaps(dc, VERTRES) );
835 msi_set_property( package->db, L"ScreenY", bufstr, len );
836 len = swprintf( bufstr, ARRAY_SIZE(bufstr), L"%d", GetDeviceCaps(dc, BITSPIXEL) );
837 msi_set_property( package->db, L"ColorBits", bufstr, len );
838 ReleaseDC(0, dc);
840 /* USERNAME and COMPANYNAME */
841 username = msi_dup_property( package->db, L"USERNAME" );
842 companyname = msi_dup_property( package->db, L"COMPANYNAME" );
844 if ((!username || !companyname) &&
845 RegOpenKeyW( HKEY_CURRENT_USER, L"SOFTWARE\\Microsoft\\MS Setup (ACME)\\User Info", &hkey ) == ERROR_SUCCESS)
847 if (!username &&
848 (username = msi_reg_get_val_str( hkey, L"DefName" )))
849 msi_set_property( package->db, L"USERNAME", username, -1 );
850 if (!companyname &&
851 (companyname = msi_reg_get_val_str( hkey, L"DefCompany" )))
852 msi_set_property( package->db, L"COMPANYNAME", companyname, -1 );
853 CloseHandle( hkey );
855 if ((!username || !companyname) &&
856 RegOpenKeyExW( HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", 0,
857 KEY_QUERY_VALUE|KEY_WOW64_64KEY, &hkey ) == ERROR_SUCCESS)
859 if (!username &&
860 (username = msi_reg_get_val_str( hkey, L"RegisteredOwner" )))
861 msi_set_property( package->db, L"USERNAME", username, -1 );
862 if (!companyname &&
863 (companyname = msi_reg_get_val_str( hkey, L"RegisteredOrganization" )))
864 msi_set_property( package->db, L"COMPANYNAME", companyname, -1 );
865 CloseHandle( hkey );
867 free( username );
868 free( companyname );
870 if ( set_user_sid_prop( package ) != ERROR_SUCCESS)
871 ERR("Failed to set the UserSID property\n");
873 set_msi_assembly_prop( package );
875 langid = GetUserDefaultLangID();
876 len = swprintf( bufstr, ARRAY_SIZE(bufstr), L"%d", langid );
877 msi_set_property( package->db, L"UserLanguageID", bufstr, len );
879 langid = GetSystemDefaultLangID();
880 len = swprintf( bufstr, ARRAY_SIZE(bufstr), L"%d", langid );
881 msi_set_property( package->db, L"SystemLanguageID", bufstr, len );
883 len = swprintf( bufstr, ARRAY_SIZE(bufstr), L"%d", MsiQueryProductStateW(package->ProductCode) );
884 msi_set_property( package->db, L"ProductState", bufstr, len );
886 len = 0;
887 if (!GetUserNameW( NULL, &len ) && GetLastError() == ERROR_INSUFFICIENT_BUFFER)
889 WCHAR *username;
890 if ((username = malloc( len * sizeof(WCHAR) )))
892 if (GetUserNameW( username, &len ))
893 msi_set_property( package->db, L"LogonUser", username, len - 1 );
894 free( username );
897 len = 0;
898 if (!GetComputerNameW( NULL, &len ) && GetLastError() == ERROR_BUFFER_OVERFLOW)
900 WCHAR *computername;
901 if ((computername = malloc( len * sizeof(WCHAR) )))
903 if (GetComputerNameW( computername, &len ))
904 msi_set_property( package->db, L"ComputerName", computername, len );
905 free( computername );
910 static MSIPACKAGE *alloc_package( void )
912 MSIPACKAGE *package;
914 package = alloc_msiobject( MSIHANDLETYPE_PACKAGE, sizeof (MSIPACKAGE),
915 MSI_FreePackage );
916 if( package )
918 list_init( &package->components );
919 list_init( &package->features );
920 list_init( &package->files );
921 list_init( &package->filepatches );
922 list_init( &package->tempfiles );
923 list_init( &package->folders );
924 list_init( &package->subscriptions );
925 list_init( &package->appids );
926 list_init( &package->classes );
927 list_init( &package->mimes );
928 list_init( &package->extensions );
929 list_init( &package->progids );
930 list_init( &package->RunningActions );
931 list_init( &package->sourcelist_info );
932 list_init( &package->sourcelist_media );
933 list_init( &package->patches );
934 list_init( &package->binaries );
935 list_init( &package->cabinet_streams );
938 return package;
941 static UINT load_admin_properties(MSIPACKAGE *package)
943 BYTE *data;
944 UINT r, sz;
946 r = read_stream_data(package->db->storage, L"AdminProperties", FALSE, &data, &sz);
947 if (r != ERROR_SUCCESS)
948 return r;
950 r = msi_parse_command_line(package, (WCHAR *)data, TRUE);
952 free(data);
953 return r;
956 void msi_adjust_privilege_properties( MSIPACKAGE *package )
958 /* FIXME: this should depend on the user's privileges */
959 if (msi_get_property_int( package->db, L"ALLUSERS", 0 ) == 2)
961 TRACE("resetting ALLUSERS property from 2 to 1\n");
962 msi_set_property( package->db, L"ALLUSERS", L"1", -1 );
964 msi_set_property( package->db, L"AdminUser", L"1", -1 );
965 msi_set_property( package->db, L"Privileged", L"1", -1 );
966 msi_set_property( package->db, L"MsiRunningElevated", L"1", -1 );
969 MSIPACKAGE *MSI_CreatePackage( MSIDATABASE *db )
971 MSIPACKAGE *package;
972 WCHAR uilevel[11];
973 int len;
974 UINT r;
976 TRACE("%p\n", db);
978 package = alloc_package();
979 if (package)
981 msiobj_addref( &db->hdr );
982 package->db = db;
984 package->LastAction = NULL;
985 package->LastActionTemplate = NULL;
986 package->LastActionResult = MSI_NULL_INTEGER;
987 package->WordCount = 0;
988 package->PackagePath = wcsdup( db->path );
990 create_temp_property_table( package );
991 msi_clone_properties( package->db );
992 msi_adjust_privilege_properties( package );
994 package->ProductCode = msi_dup_property( package->db, L"ProductCode" );
996 set_installer_properties( package );
998 package->ui_level = gUILevel;
999 len = swprintf( uilevel, ARRAY_SIZE(uilevel), L"%u", gUILevel & INSTALLUILEVEL_MASK );
1000 msi_set_property( package->db, L"UILevel", uilevel, len );
1002 r = msi_load_suminfo_properties( package );
1003 if (r != ERROR_SUCCESS)
1005 msiobj_release( &package->hdr );
1006 return NULL;
1009 if (package->WordCount & msidbSumInfoSourceTypeAdminImage)
1010 load_admin_properties( package );
1012 package->log_file = INVALID_HANDLE_VALUE;
1013 package->script = SCRIPT_NONE;
1015 return package;
1018 UINT msi_download_file( LPCWSTR szUrl, LPWSTR filename )
1020 LPINTERNET_CACHE_ENTRY_INFOW cache_entry;
1021 DWORD size = 0;
1022 HRESULT hr;
1024 /* call will always fail, because size is 0,
1025 * but will return ERROR_FILE_NOT_FOUND first
1026 * if the file doesn't exist
1028 GetUrlCacheEntryInfoW( szUrl, NULL, &size );
1029 if ( GetLastError() != ERROR_FILE_NOT_FOUND )
1031 cache_entry = malloc( size );
1032 if ( !GetUrlCacheEntryInfoW( szUrl, cache_entry, &size ) )
1034 UINT error = GetLastError();
1035 free( cache_entry );
1036 return error;
1039 lstrcpyW( filename, cache_entry->lpszLocalFileName );
1040 free( cache_entry );
1041 return ERROR_SUCCESS;
1044 hr = URLDownloadToCacheFileW( NULL, szUrl, filename, MAX_PATH, 0, NULL );
1045 if ( FAILED(hr) )
1047 WARN("failed to download %s to cache file\n", debugstr_w(szUrl));
1048 return ERROR_FUNCTION_FAILED;
1051 return ERROR_SUCCESS;
1054 UINT msi_create_empty_local_file( LPWSTR path, LPCWSTR suffix )
1056 DWORD time, len, i, offset;
1057 HANDLE handle;
1059 time = GetTickCount();
1060 GetWindowsDirectoryW( path, MAX_PATH );
1061 lstrcatW( path, L"\\Installer\\" );
1062 CreateDirectoryW( path, NULL );
1064 len = lstrlenW(path);
1065 for (i = 0; i < 0x10000; i++)
1067 offset = swprintf( path + len, MAX_PATH - len, L"%x", (time + i) & 0xffff );
1068 memcpy( path + len + offset, suffix, (lstrlenW( suffix ) + 1) * sizeof(WCHAR) );
1069 handle = CreateFileW( path, GENERIC_WRITE, 0, NULL,
1070 CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0 );
1071 if (handle != INVALID_HANDLE_VALUE)
1073 CloseHandle(handle);
1074 break;
1076 if (GetLastError() != ERROR_FILE_EXISTS &&
1077 GetLastError() != ERROR_SHARING_VIOLATION)
1078 return ERROR_FUNCTION_FAILED;
1081 return ERROR_SUCCESS;
1084 static enum platform parse_platform( const WCHAR *str )
1086 if (!str[0] || !wcscmp( str, L"Intel" )) return PLATFORM_INTEL;
1087 else if (!wcscmp( str, L"Intel64" )) return PLATFORM_INTEL64;
1088 else if (!wcscmp( str, L"x64" ) || !wcscmp( str, L"AMD64" )) return PLATFORM_X64;
1089 else if (!wcscmp( str, L"Arm" )) return PLATFORM_ARM;
1090 else if (!wcscmp( str, L"Arm64" )) return PLATFORM_ARM64;
1091 return PLATFORM_UNRECOGNIZED;
1094 static UINT parse_suminfo( MSISUMMARYINFO *si, MSIPACKAGE *package )
1096 WCHAR *template, *p, *q, *platform;
1097 DWORD i, count;
1099 package->version = msi_suminfo_get_int32( si, PID_PAGECOUNT );
1100 TRACE("version: %d\n", package->version);
1102 template = msi_suminfo_dup_string( si, PID_TEMPLATE );
1103 if (!template)
1104 return ERROR_SUCCESS; /* native accepts missing template property */
1106 TRACE("template: %s\n", debugstr_w(template));
1108 p = wcschr( template, ';' );
1109 if (!p)
1111 WARN("invalid template string %s\n", debugstr_w(template));
1112 free( template );
1113 return ERROR_PATCH_PACKAGE_INVALID;
1115 *p = 0;
1116 platform = template;
1117 if ((q = wcschr( platform, ',' ))) *q = 0;
1118 package->platform = parse_platform( platform );
1119 while (package->platform == PLATFORM_UNRECOGNIZED && q)
1121 platform = q + 1;
1122 if ((q = wcschr( platform, ',' ))) *q = 0;
1123 package->platform = parse_platform( platform );
1125 if (package->platform == PLATFORM_UNRECOGNIZED)
1127 WARN("unknown platform %s\n", debugstr_w(template));
1128 free( template );
1129 return ERROR_INSTALL_PLATFORM_UNSUPPORTED;
1131 p++;
1132 if (!*p)
1134 free( template );
1135 return ERROR_SUCCESS;
1137 count = 1;
1138 for (q = p; (q = wcschr( q, ',' )); q++) count++;
1140 package->langids = malloc( count * sizeof(LANGID) );
1141 if (!package->langids)
1143 free( template );
1144 return ERROR_OUTOFMEMORY;
1147 i = 0;
1148 while (*p)
1150 q = wcschr( p, ',' );
1151 if (q) *q = 0;
1152 package->langids[i] = wcstol( p, NULL, 10 );
1153 if (!q) break;
1154 p = q + 1;
1155 i++;
1157 package->num_langids = i + 1;
1159 free( template );
1160 return ERROR_SUCCESS;
1163 static UINT validate_package( MSIPACKAGE *package )
1165 UINT i;
1167 if (package->platform == PLATFORM_INTEL64)
1168 return ERROR_INSTALL_PLATFORM_UNSUPPORTED;
1169 #ifndef __arm__
1170 if (package->platform == PLATFORM_ARM)
1171 return ERROR_INSTALL_PLATFORM_UNSUPPORTED;
1172 #endif
1173 #ifndef __aarch64__
1174 if (package->platform == PLATFORM_ARM64)
1175 return ERROR_INSTALL_PLATFORM_UNSUPPORTED;
1176 #endif
1177 if (package->platform == PLATFORM_X64)
1179 if (!is_64bit && !is_wow64)
1180 return ERROR_INSTALL_PLATFORM_UNSUPPORTED;
1181 if (package->version < 200)
1182 return ERROR_INSTALL_PACKAGE_INVALID;
1184 if (!package->num_langids)
1186 return ERROR_SUCCESS;
1188 for (i = 0; i < package->num_langids; i++)
1190 LANGID langid = package->langids[i];
1192 if (PRIMARYLANGID( langid ) == LANG_NEUTRAL)
1194 langid = MAKELANGID( PRIMARYLANGID( GetSystemDefaultLangID() ), SUBLANGID( langid ) );
1196 if (SUBLANGID( langid ) == SUBLANG_NEUTRAL)
1198 langid = MAKELANGID( PRIMARYLANGID( langid ), SUBLANGID( GetSystemDefaultLangID() ) );
1200 if (IsValidLocale( langid, LCID_INSTALLED ))
1201 return ERROR_SUCCESS;
1203 return ERROR_INSTALL_LANGUAGE_UNSUPPORTED;
1206 static WCHAR *get_property( MSIDATABASE *db, const WCHAR *prop )
1208 WCHAR query[MAX_PATH];
1209 MSIQUERY *view;
1210 MSIRECORD *rec;
1211 WCHAR *ret = NULL;
1213 swprintf(query, ARRAY_SIZE(query), L"SELECT `Value` FROM `Property` WHERE `Property`='%s'", prop);
1214 if (MSI_DatabaseOpenViewW( db, query, &view ) != ERROR_SUCCESS)
1216 return NULL;
1218 if (MSI_ViewExecute( view, 0 ) != ERROR_SUCCESS)
1220 MSI_ViewClose( view );
1221 msiobj_release( &view->hdr );
1222 return NULL;
1224 if (MSI_ViewFetch( view, &rec ) == ERROR_SUCCESS)
1226 ret = wcsdup( MSI_RecordGetString( rec, 1 ) );
1227 msiobj_release( &rec->hdr );
1229 MSI_ViewClose( view );
1230 msiobj_release( &view->hdr );
1231 return ret;
1234 static WCHAR *get_product_code( MSIDATABASE *db )
1236 return get_property( db, L"ProductCode" );
1239 static WCHAR *get_product_version( MSIDATABASE *db )
1241 return get_property( db, L"ProductVersion" );
1244 static UINT get_registered_local_package( const WCHAR *product, WCHAR *localfile )
1246 MSIINSTALLCONTEXT context;
1247 WCHAR *filename;
1248 HKEY props_key;
1249 UINT r;
1251 r = msi_locate_product( product, &context );
1252 if (r != ERROR_SUCCESS)
1253 return r;
1255 r = MSIREG_OpenInstallProps( product, context, NULL, &props_key, FALSE );
1256 if (r != ERROR_SUCCESS)
1257 return r;
1259 filename = msi_reg_get_val_str( props_key, INSTALLPROPERTY_LOCALPACKAGEW );
1260 RegCloseKey( props_key );
1261 if (!filename)
1262 return ERROR_FUNCTION_FAILED;
1264 lstrcpyW( localfile, filename );
1265 free( filename );
1266 return ERROR_SUCCESS;
1269 WCHAR *msi_get_package_code( MSIDATABASE *db )
1271 WCHAR *ret;
1272 MSISUMMARYINFO *si;
1273 UINT r;
1275 r = msi_get_suminfo( db->storage, 0, &si );
1276 if (r != ERROR_SUCCESS)
1278 r = msi_get_db_suminfo( db, 0, &si );
1279 if (r != ERROR_SUCCESS)
1281 WARN("failed to load summary info %u\n", r);
1282 return NULL;
1285 ret = msi_suminfo_dup_string( si, PID_REVNUMBER );
1286 msiobj_release( &si->hdr );
1287 return ret;
1290 static UINT get_local_package( MSIDATABASE *db, WCHAR *localfile )
1292 WCHAR *product_code;
1293 UINT r;
1295 if (!(product_code = get_product_code( db )))
1296 return ERROR_INSTALL_PACKAGE_INVALID;
1297 r = get_registered_local_package( product_code, localfile );
1298 free( product_code );
1299 return r;
1302 UINT msi_set_original_database_property( MSIDATABASE *db, const WCHAR *package )
1304 UINT r;
1306 if (UrlIsW( package, URLIS_URL ))
1307 r = msi_set_property( db, L"OriginalDatabase", package, -1 );
1308 else if (package[0] == '#')
1309 r = msi_set_property( db, L"OriginalDatabase", db->path, -1 );
1310 else
1312 DWORD len;
1313 WCHAR *path;
1315 if (!(len = GetFullPathNameW( package, 0, NULL, NULL ))) return GetLastError();
1316 if (!(path = malloc( len * sizeof(WCHAR) ))) return ERROR_OUTOFMEMORY;
1317 len = GetFullPathNameW( package, len, path, NULL );
1318 r = msi_set_property( db, L"OriginalDatabase", path, len );
1319 free( path );
1321 return r;
1324 UINT MSI_OpenPackageW(LPCWSTR szPackage, DWORD dwOptions, MSIPACKAGE **pPackage)
1326 MSIDATABASE *db;
1327 MSIPACKAGE *package;
1328 MSIHANDLE handle;
1329 MSIRECORD *data_row, *info_row;
1330 UINT r;
1331 WCHAR localfile[MAX_PATH], cachefile[MAX_PATH];
1332 LPCWSTR file = szPackage;
1333 DWORD index = 0;
1334 MSISUMMARYINFO *si;
1335 BOOL delete_on_close = FALSE;
1336 WCHAR *info_template, *productname, *product_code;
1337 MSIINSTALLCONTEXT context;
1339 TRACE("%s %p\n", debugstr_w(szPackage), pPackage);
1341 MSI_ProcessMessage(NULL, INSTALLMESSAGE_INITIALIZE, 0);
1343 localfile[0] = 0;
1344 if( szPackage[0] == '#' )
1346 handle = wcstol(&szPackage[1], NULL, 10);
1347 if (!(db = msihandle2msiinfo(handle, MSIHANDLETYPE_DATABASE)))
1348 return ERROR_INVALID_HANDLE;
1350 else
1352 WCHAR *product_version = NULL;
1354 if ( UrlIsW( szPackage, URLIS_URL ) )
1356 r = msi_download_file( szPackage, cachefile );
1357 if (r != ERROR_SUCCESS)
1358 return r;
1360 file = cachefile;
1362 r = MSI_OpenDatabaseW( file, MSIDBOPEN_READONLY, &db );
1363 if (r != ERROR_SUCCESS)
1365 if (GetFileAttributesW( file ) == INVALID_FILE_ATTRIBUTES)
1366 return ERROR_FILE_NOT_FOUND;
1367 return r;
1369 r = get_local_package( db, localfile );
1370 if (r != ERROR_SUCCESS || GetFileAttributesW( localfile ) == INVALID_FILE_ATTRIBUTES)
1372 DWORD localfile_attr;
1374 r = msi_create_empty_local_file( localfile, L".msi" );
1375 if (r != ERROR_SUCCESS)
1377 msiobj_release( &db->hdr );
1378 return r;
1381 if (!CopyFileW( file, localfile, FALSE ))
1383 r = GetLastError();
1384 WARN("unable to copy package %s to %s (%u)\n", debugstr_w(file), debugstr_w(localfile), r);
1385 DeleteFileW( localfile );
1386 msiobj_release( &db->hdr );
1387 return r;
1389 delete_on_close = TRUE;
1391 /* Remove read-only bit, we are opening it with write access in MSI_OpenDatabaseW below. */
1392 localfile_attr = GetFileAttributesW( localfile );
1393 if (localfile_attr & FILE_ATTRIBUTE_READONLY)
1394 SetFileAttributesW( localfile, localfile_attr & ~FILE_ATTRIBUTE_READONLY);
1396 else if (dwOptions & WINE_OPENPACKAGEFLAGS_RECACHE)
1398 if (!CopyFileW( file, localfile, FALSE ))
1400 r = GetLastError();
1401 WARN("unable to update cached package (%u)\n", r);
1402 msiobj_release( &db->hdr );
1403 return r;
1406 else
1407 product_version = get_product_version( db );
1408 msiobj_release( &db->hdr );
1409 TRACE("opening package %s\n", debugstr_w( localfile ));
1410 r = MSI_OpenDatabaseW( localfile, MSIDBOPEN_TRANSACT, &db );
1411 if (r != ERROR_SUCCESS)
1413 free( product_version );
1414 return r;
1417 if (product_version)
1419 WCHAR *cache_version = get_product_version( db );
1420 if (!product_version != !cache_version ||
1421 (product_version && wcscmp(product_version, cache_version)))
1423 msiobj_release( &db->hdr );
1424 free( product_version );
1425 free( cache_version );
1426 return ERROR_PRODUCT_VERSION;
1428 free( product_version );
1429 free( cache_version );
1432 package = MSI_CreatePackage( db );
1433 msiobj_release( &db->hdr );
1434 if (!package) return ERROR_INSTALL_PACKAGE_INVALID;
1435 package->localfile = wcsdup( localfile );
1436 package->delete_on_close = delete_on_close;
1438 r = msi_get_suminfo( db->storage, 0, &si );
1439 if (r != ERROR_SUCCESS)
1441 r = msi_get_db_suminfo( db, 0, &si );
1442 if (r != ERROR_SUCCESS)
1444 WARN("failed to load summary info\n");
1445 msiobj_release( &package->hdr );
1446 return ERROR_INSTALL_PACKAGE_INVALID;
1449 r = parse_suminfo( si, package );
1450 msiobj_release( &si->hdr );
1451 if (r != ERROR_SUCCESS)
1453 WARN("failed to parse summary info %u\n", r);
1454 msiobj_release( &package->hdr );
1455 return r;
1457 r = validate_package( package );
1458 if (r != ERROR_SUCCESS)
1460 msiobj_release( &package->hdr );
1461 return r;
1463 msi_set_property( package->db, L"DATABASE", db->path, -1 );
1464 set_installed_prop( package );
1465 msi_set_context( package );
1467 product_code = get_product_code( db );
1468 if (msi_locate_product( product_code, &context ) == ERROR_SUCCESS)
1470 TRACE("product already registered\n");
1471 msi_set_property( package->db, L"ProductToBeRegistered", L"1", -1 );
1473 free( product_code );
1475 while (1)
1477 WCHAR patch_code[GUID_SIZE];
1478 r = MsiEnumPatchesExW( package->ProductCode, NULL, package->Context,
1479 MSIPATCHSTATE_APPLIED, index, patch_code, NULL, NULL, NULL, NULL );
1480 if (r != ERROR_SUCCESS)
1481 break;
1483 TRACE("found registered patch %s\n", debugstr_w(patch_code));
1485 r = msi_apply_registered_patch( package, patch_code );
1486 if (r != ERROR_SUCCESS)
1488 ERR("registered patch failed to apply %u\n", r);
1489 msiobj_release( &package->hdr );
1490 return r;
1492 index++;
1494 if (index) msi_adjust_privilege_properties( package );
1496 r = msi_set_original_database_property( package->db, szPackage );
1497 if (r != ERROR_SUCCESS)
1499 msiobj_release( &package->hdr );
1500 return r;
1502 if (gszLogFile)
1503 package->log_file = CreateFileW( gszLogFile, GENERIC_WRITE, FILE_SHARE_WRITE, NULL,
1504 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
1506 /* FIXME: when should these messages be sent? */
1507 data_row = MSI_CreateRecord(3);
1508 if (!data_row)
1509 return ERROR_OUTOFMEMORY;
1510 MSI_RecordSetStringW(data_row, 0, NULL);
1511 MSI_RecordSetInteger(data_row, 1, 0);
1512 MSI_RecordSetInteger(data_row, 2, package->num_langids ? package->langids[0] : 0);
1513 MSI_RecordSetInteger(data_row, 3, msi_get_string_table_codepage(package->db->strings));
1514 MSI_ProcessMessageVerbatim(package, INSTALLMESSAGE_COMMONDATA, data_row);
1516 info_row = MSI_CreateRecord(0);
1517 if (!info_row)
1519 msiobj_release(&data_row->hdr);
1520 return ERROR_OUTOFMEMORY;
1522 info_template = msi_get_error_message(package->db, MSIERR_INFO_LOGGINGSTART);
1523 MSI_RecordSetStringW(info_row, 0, info_template);
1524 free(info_template);
1525 MSI_ProcessMessage(package, INSTALLMESSAGE_INFO|MB_ICONHAND, info_row);
1527 MSI_ProcessMessage(package, INSTALLMESSAGE_COMMONDATA, data_row);
1529 productname = msi_dup_property(package->db, INSTALLPROPERTY_PRODUCTNAMEW);
1530 MSI_RecordSetInteger(data_row, 1, 1);
1531 MSI_RecordSetStringW(data_row, 2, productname);
1532 MSI_RecordSetStringW(data_row, 3, NULL);
1533 MSI_ProcessMessage(package, INSTALLMESSAGE_COMMONDATA, data_row);
1535 free(productname);
1536 msiobj_release(&info_row->hdr);
1537 msiobj_release(&data_row->hdr);
1539 *pPackage = package;
1540 return ERROR_SUCCESS;
1543 UINT WINAPI MsiOpenPackageExW( const WCHAR *szPackage, DWORD dwOptions, MSIHANDLE *phPackage )
1545 MSIPACKAGE *package = NULL;
1546 UINT ret;
1548 TRACE( "%s, %#lx, %p\n", debugstr_w(szPackage), dwOptions, phPackage );
1550 if( !szPackage || !phPackage )
1551 return ERROR_INVALID_PARAMETER;
1553 if ( !*szPackage )
1555 FIXME("Should create an empty database and package\n");
1556 return ERROR_FUNCTION_FAILED;
1559 if( dwOptions )
1560 FIXME( "dwOptions %#lx not supported\n", dwOptions );
1562 ret = MSI_OpenPackageW( szPackage, 0, &package );
1563 if( ret == ERROR_SUCCESS )
1565 *phPackage = alloc_msihandle( &package->hdr );
1566 if (! *phPackage)
1567 ret = ERROR_NOT_ENOUGH_MEMORY;
1568 msiobj_release( &package->hdr );
1570 else
1571 MSI_ProcessMessage(NULL, INSTALLMESSAGE_TERMINATE, 0);
1573 return ret;
1576 UINT WINAPI MsiOpenPackageW(LPCWSTR szPackage, MSIHANDLE *phPackage)
1578 return MsiOpenPackageExW( szPackage, 0, phPackage );
1581 UINT WINAPI MsiOpenPackageExA(LPCSTR szPackage, DWORD dwOptions, MSIHANDLE *phPackage)
1583 LPWSTR szwPack = NULL;
1584 UINT ret;
1586 if( szPackage )
1588 szwPack = strdupAtoW( szPackage );
1589 if( !szwPack )
1590 return ERROR_OUTOFMEMORY;
1593 ret = MsiOpenPackageExW( szwPack, dwOptions, phPackage );
1595 free( szwPack );
1597 return ret;
1600 UINT WINAPI MsiOpenPackageA(LPCSTR szPackage, MSIHANDLE *phPackage)
1602 return MsiOpenPackageExA( szPackage, 0, phPackage );
1605 MSIHANDLE WINAPI MsiGetActiveDatabase( MSIHANDLE hInstall )
1607 MSIPACKAGE *package;
1608 MSIHANDLE handle = 0;
1609 MSIHANDLE remote;
1611 TRACE( "%lu\n", hInstall );
1613 package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE);
1614 if( package)
1616 handle = alloc_msihandle( &package->db->hdr );
1617 msiobj_release( &package->hdr );
1619 else if ((remote = msi_get_remote(hInstall)))
1621 __TRY
1623 handle = remote_GetActiveDatabase(remote);
1624 handle = alloc_msi_remote_handle(handle);
1626 __EXCEPT(rpc_filter)
1628 handle = 0;
1630 __ENDTRY
1633 return handle;
1636 static INT internal_ui_handler(MSIPACKAGE *package, INSTALLMESSAGE eMessageType, MSIRECORD *record, LPCWSTR message)
1638 if (!package || (package->ui_level & INSTALLUILEVEL_MASK) == INSTALLUILEVEL_NONE)
1639 return 0;
1641 /* todo: check if message needs additional styles (topmost/foreground/modality?) */
1643 switch (eMessageType & 0xff000000)
1645 case INSTALLMESSAGE_FATALEXIT:
1646 case INSTALLMESSAGE_ERROR:
1647 case INSTALLMESSAGE_OUTOFDISKSPACE:
1648 if (package->ui_level & INSTALLUILEVEL_PROGRESSONLY) return 0;
1649 if (!(eMessageType & MB_ICONMASK))
1650 eMessageType |= MB_ICONEXCLAMATION;
1651 return MessageBoxW(gUIhwnd, message, L"Windows Installer", eMessageType & 0x00ffffff);
1652 case INSTALLMESSAGE_WARNING:
1653 if (package->ui_level & INSTALLUILEVEL_PROGRESSONLY) return 0;
1654 if (!(eMessageType & MB_ICONMASK))
1655 eMessageType |= MB_ICONASTERISK;
1656 return MessageBoxW(gUIhwnd, message, L"Windows Installer", eMessageType & 0x00ffffff);
1657 case INSTALLMESSAGE_USER:
1658 if (package->ui_level & INSTALLUILEVEL_PROGRESSONLY) return 0;
1659 if (!(eMessageType & MB_ICONMASK))
1660 eMessageType |= MB_USERICON;
1661 return MessageBoxW(gUIhwnd, message, L"Windows Installer", eMessageType & 0x00ffffff);
1662 case INSTALLMESSAGE_INFO:
1663 case INSTALLMESSAGE_INITIALIZE:
1664 case INSTALLMESSAGE_TERMINATE:
1665 case INSTALLMESSAGE_INSTALLSTART:
1666 case INSTALLMESSAGE_INSTALLEND:
1667 return 0;
1668 case INSTALLMESSAGE_SHOWDIALOG:
1670 LPWSTR dialog = msi_dup_record_field(record, 0);
1671 INT rc = ACTION_DialogBox(package, dialog);
1672 free(dialog);
1673 return rc;
1675 case INSTALLMESSAGE_ACTIONSTART:
1677 LPWSTR deformatted;
1678 MSIRECORD *uirow = MSI_CreateRecord(1);
1679 if (!uirow) return -1;
1680 deformat_string(package, MSI_RecordGetString(record, 2), &deformatted);
1681 MSI_RecordSetStringW(uirow, 1, deformatted);
1682 msi_event_fire(package, L"ActionText", uirow);
1684 free(deformatted);
1685 msiobj_release(&uirow->hdr);
1686 return 1;
1688 case INSTALLMESSAGE_ACTIONDATA:
1690 MSIRECORD *uirow = MSI_CreateRecord(1);
1691 if (!uirow) return -1;
1692 MSI_RecordSetStringW(uirow, 1, message);
1693 msi_event_fire(package, L"ActionData", uirow);
1694 msiobj_release(&uirow->hdr);
1696 if (package->action_progress_increment)
1698 uirow = MSI_CreateRecord(2);
1699 if (!uirow) return -1;
1700 MSI_RecordSetInteger(uirow, 1, 2);
1701 MSI_RecordSetInteger(uirow, 2, package->action_progress_increment);
1702 msi_event_fire(package, L"SetProgress", uirow);
1703 msiobj_release(&uirow->hdr);
1705 return 1;
1707 case INSTALLMESSAGE_PROGRESS:
1708 msi_event_fire(package, L"SetProgress", record);
1709 return 1;
1710 case INSTALLMESSAGE_COMMONDATA:
1711 switch (MSI_RecordGetInteger(record, 1))
1713 case 0:
1714 case 1:
1715 /* do nothing */
1716 return 0;
1717 default:
1718 /* fall through */
1721 default:
1722 FIXME("internal UI not implemented for message 0x%08x (UI level = %x)\n", eMessageType, package->ui_level);
1723 return 0;
1727 static const struct
1729 int id;
1730 const WCHAR *text;
1732 internal_errors[] =
1734 {2726, L"DEBUG: Error [1]: Action not found: [2]"},
1738 static LPCWSTR get_internal_error_message(int error)
1740 int i = 0;
1742 while (internal_errors[i].id != 0)
1744 if (internal_errors[i].id == error)
1745 return internal_errors[i].text;
1746 i++;
1749 FIXME("missing error message %d\n", error);
1750 return NULL;
1753 /* Returned string must be freed */
1754 LPWSTR msi_get_error_message(MSIDATABASE *db, int error)
1756 MSIRECORD *record;
1757 LPWSTR ret = NULL;
1759 if ((record = MSI_QueryGetRecord(db, L"SELECT `Message` FROM `Error` WHERE `Error` = %d", error)))
1761 ret = msi_dup_record_field(record, 1);
1762 msiobj_release(&record->hdr);
1764 else if (error < 2000)
1766 int len = LoadStringW(msi_hInstance, IDS_ERROR_BASE + error, (LPWSTR) &ret, 0);
1767 if (len)
1769 ret = malloc((len + 1) * sizeof(WCHAR));
1770 LoadStringW(msi_hInstance, IDS_ERROR_BASE + error, ret, len + 1);
1772 else
1773 ret = NULL;
1776 return ret;
1779 INT MSI_ProcessMessageVerbatim(MSIPACKAGE *package, INSTALLMESSAGE eMessageType, MSIRECORD *record)
1781 LPWSTR message = {0};
1782 DWORD len;
1783 DWORD log_type = 1 << (eMessageType >> 24);
1784 UINT res;
1785 INT rc = 0;
1786 char *msg;
1788 TRACE("%x\n", eMessageType);
1789 if (TRACE_ON(msi)) dump_record(record);
1791 if (!package || !record)
1792 message = NULL;
1793 else {
1794 res = MSI_FormatRecordW(package, record, message, &len);
1795 if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA)
1796 return res;
1797 len++;
1798 message = malloc(len * sizeof(WCHAR));
1799 if (!message) return ERROR_OUTOFMEMORY;
1800 MSI_FormatRecordW(package, record, message, &len);
1803 /* convert it to ANSI */
1804 len = WideCharToMultiByte( CP_ACP, 0, message, -1, NULL, 0, NULL, NULL );
1805 msg = malloc( len );
1806 WideCharToMultiByte( CP_ACP, 0, message, -1, msg, len, NULL, NULL );
1808 if (gUIHandlerRecord && (gUIFilterRecord & log_type))
1810 MSIHANDLE rec = alloc_msihandle(&record->hdr);
1811 TRACE( "calling UI handler %p(pvContext = %p, iMessageType = %#x, hRecord = %lu)\n",
1812 gUIHandlerRecord, gUIContextRecord, eMessageType, rec );
1813 rc = gUIHandlerRecord( gUIContextRecord, eMessageType, rec );
1814 MsiCloseHandle( rec );
1816 if (!rc && gUIHandlerW && (gUIFilter & log_type))
1818 TRACE( "calling UI handler %p(pvContext = %p, iMessageType = %#x, szMessage = %s)\n",
1819 gUIHandlerW, gUIContext, eMessageType, debugstr_w(message) );
1820 rc = gUIHandlerW( gUIContext, eMessageType, message );
1822 else if (!rc && gUIHandlerA && (gUIFilter & log_type))
1824 TRACE( "calling UI handler %p(pvContext = %p, iMessageType = %#x, szMessage = %s)\n",
1825 gUIHandlerA, gUIContext, eMessageType, debugstr_a(msg) );
1826 rc = gUIHandlerA( gUIContext, eMessageType, msg );
1829 if (!rc)
1830 rc = internal_ui_handler(package, eMessageType, record, message);
1832 if (!rc && package && package->log_file != INVALID_HANDLE_VALUE &&
1833 (eMessageType & 0xff000000) != INSTALLMESSAGE_PROGRESS)
1835 DWORD written;
1836 WriteFile( package->log_file, msg, len - 1, &written, NULL );
1837 WriteFile( package->log_file, "\n", 1, &written, NULL );
1839 free( msg );
1840 free( message );
1842 return rc;
1845 INT MSI_ProcessMessage( MSIPACKAGE *package, INSTALLMESSAGE eMessageType, MSIRECORD *record )
1847 switch (eMessageType & 0xff000000)
1849 case INSTALLMESSAGE_FATALEXIT:
1850 case INSTALLMESSAGE_ERROR:
1851 case INSTALLMESSAGE_WARNING:
1852 case INSTALLMESSAGE_USER:
1853 case INSTALLMESSAGE_INFO:
1854 case INSTALLMESSAGE_OUTOFDISKSPACE:
1855 if (MSI_RecordGetInteger(record, 1) != MSI_NULL_INTEGER)
1857 /* error message */
1859 LPWSTR template;
1860 LPWSTR template_rec = NULL, template_prefix = NULL;
1861 int error = MSI_RecordGetInteger(record, 1);
1863 if (MSI_RecordIsNull(record, 0))
1865 if (error >= 32)
1867 template_rec = msi_get_error_message(package->db, error);
1869 if (!template_rec && error >= 2000)
1871 /* internal error, not localized */
1872 if ((template_rec = (LPWSTR) get_internal_error_message(error)))
1874 MSI_RecordSetStringW(record, 0, template_rec);
1875 MSI_ProcessMessageVerbatim(package, INSTALLMESSAGE_INFO, record);
1877 template_rec = msi_get_error_message(package->db, MSIERR_INSTALLERROR);
1878 MSI_RecordSetStringW(record, 0, template_rec);
1879 MSI_ProcessMessageVerbatim(package, eMessageType, record);
1880 free(template_rec);
1881 return 0;
1885 else
1886 template_rec = msi_dup_record_field(record, 0);
1888 template_prefix = msi_get_error_message(package->db, eMessageType >> 24);
1889 if (!template_prefix) template_prefix = wcsdup(L"");
1891 if (!template_rec)
1893 /* always returns 0 */
1894 MSI_RecordSetStringW(record, 0, template_prefix);
1895 MSI_ProcessMessageVerbatim(package, eMessageType, record);
1896 free(template_prefix);
1897 return 0;
1900 template = malloc((wcslen(template_rec) + wcslen(template_prefix) + 1) * sizeof(WCHAR));
1901 if (!template)
1903 free(template_prefix);
1904 free(template_rec);
1905 return ERROR_OUTOFMEMORY;
1908 lstrcpyW(template, template_prefix);
1909 lstrcatW(template, template_rec);
1910 MSI_RecordSetStringW(record, 0, template);
1912 free(template_prefix);
1913 free(template_rec);
1914 free(template);
1916 break;
1917 case INSTALLMESSAGE_ACTIONSTART:
1919 WCHAR *template = msi_get_error_message(package->db, MSIERR_ACTIONSTART);
1920 MSI_RecordSetStringW(record, 0, template);
1921 free(template);
1923 free(package->LastAction);
1924 free(package->LastActionTemplate);
1925 package->LastAction = msi_dup_record_field(record, 1);
1926 if (!package->LastAction) package->LastAction = wcsdup(L"");
1927 package->LastActionTemplate = msi_dup_record_field(record, 3);
1928 break;
1930 case INSTALLMESSAGE_ACTIONDATA:
1931 if (package->LastAction && package->LastActionTemplate)
1933 size_t len = lstrlenW(package->LastAction) + lstrlenW(package->LastActionTemplate) + 7;
1934 WCHAR *template = malloc(len * sizeof(WCHAR));
1935 if (!template) return ERROR_OUTOFMEMORY;
1936 swprintf(template, len, L"{{%s: }}%s", package->LastAction, package->LastActionTemplate);
1937 MSI_RecordSetStringW(record, 0, template);
1938 free(template);
1940 break;
1941 case INSTALLMESSAGE_COMMONDATA:
1943 WCHAR *template = msi_get_error_message(package->db, MSIERR_COMMONDATA);
1944 MSI_RecordSetStringW(record, 0, template);
1945 free(template);
1947 break;
1950 return MSI_ProcessMessageVerbatim(package, eMessageType, record);
1953 INT WINAPI MsiProcessMessage( MSIHANDLE hInstall, INSTALLMESSAGE eMessageType,
1954 MSIHANDLE hRecord)
1956 UINT ret = ERROR_INVALID_HANDLE;
1957 MSIPACKAGE *package = NULL;
1958 MSIRECORD *record = NULL;
1960 if ((eMessageType & 0xff000000) == INSTALLMESSAGE_INITIALIZE ||
1961 (eMessageType & 0xff000000) == INSTALLMESSAGE_TERMINATE)
1962 return -1;
1964 if ((eMessageType & 0xff000000) == INSTALLMESSAGE_COMMONDATA &&
1965 MsiRecordGetInteger(hRecord, 1) != 2)
1966 return -1;
1968 record = msihandle2msiinfo(hRecord, MSIHANDLETYPE_RECORD);
1969 if (!record)
1970 return ERROR_INVALID_HANDLE;
1972 package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE );
1973 if( !package )
1975 MSIHANDLE remote;
1977 if (!(remote = msi_get_remote(hInstall)))
1978 return ERROR_INVALID_HANDLE;
1980 __TRY
1982 ret = remote_ProcessMessage(remote, eMessageType, (struct wire_record *)&record->count);
1984 __EXCEPT(rpc_filter)
1986 ret = GetExceptionCode();
1988 __ENDTRY
1990 msiobj_release(&record->hdr);
1991 return ret;
1994 ret = MSI_ProcessMessage( package, eMessageType, record );
1996 msiobj_release( &record->hdr );
1997 msiobj_release( &package->hdr );
1998 return ret;
2001 /* property code */
2003 UINT WINAPI MsiSetPropertyA( MSIHANDLE hInstall, LPCSTR szName, LPCSTR szValue )
2005 LPWSTR szwName = NULL, szwValue = NULL;
2006 UINT r = ERROR_OUTOFMEMORY;
2008 szwName = strdupAtoW( szName );
2009 if( szName && !szwName )
2010 goto end;
2012 szwValue = strdupAtoW( szValue );
2013 if( szValue && !szwValue )
2014 goto end;
2016 r = MsiSetPropertyW( hInstall, szwName, szwValue);
2018 end:
2019 free( szwName );
2020 free( szwValue );
2022 return r;
2025 void msi_reset_source_folders( MSIPACKAGE *package )
2027 MSIFOLDER *folder;
2029 LIST_FOR_EACH_ENTRY( folder, &package->folders, MSIFOLDER, entry )
2031 free( folder->ResolvedSource );
2032 folder->ResolvedSource = NULL;
2036 UINT msi_set_property( MSIDATABASE *db, const WCHAR *name, const WCHAR *value, int len )
2038 MSIQUERY *view;
2039 MSIRECORD *row = NULL;
2040 DWORD sz = 0;
2041 WCHAR query[1024];
2042 UINT rc;
2044 TRACE("%p %s %s %d\n", db, debugstr_w(name), debugstr_wn(value, len), len);
2046 if (!name)
2047 return ERROR_INVALID_PARAMETER;
2049 /* this one is weird... */
2050 if (!name[0])
2051 return value ? ERROR_FUNCTION_FAILED : ERROR_SUCCESS;
2053 if (value && len < 0) len = lstrlenW( value );
2055 rc = msi_get_property( db, name, 0, &sz );
2056 if (!value || (!*value && !len))
2058 swprintf( query, ARRAY_SIZE(query), L"DELETE FROM `_Property` WHERE `_Property` = '%s'", name );
2060 else if (rc == ERROR_MORE_DATA || rc == ERROR_SUCCESS)
2062 swprintf( query, ARRAY_SIZE(query), L"UPDATE `_Property` SET `Value` = ? WHERE `_Property` = '%s'", name );
2063 row = MSI_CreateRecord(1);
2064 msi_record_set_string( row, 1, value, len );
2066 else
2068 lstrcpyW( query, L"INSERT INTO `_Property` (`_Property`,`Value`) VALUES (?,?)" );
2069 row = MSI_CreateRecord(2);
2070 msi_record_set_string( row, 1, name, -1 );
2071 msi_record_set_string( row, 2, value, len );
2074 rc = MSI_DatabaseOpenViewW(db, query, &view);
2075 if (rc == ERROR_SUCCESS)
2077 rc = MSI_ViewExecute(view, row);
2078 MSI_ViewClose(view);
2079 msiobj_release(&view->hdr);
2081 if (row) msiobj_release(&row->hdr);
2082 return rc;
2085 UINT WINAPI MsiSetPropertyW( MSIHANDLE hInstall, LPCWSTR szName, LPCWSTR szValue)
2087 MSIPACKAGE *package;
2088 UINT ret;
2090 package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE);
2091 if( !package )
2093 MSIHANDLE remote;
2095 if (!(remote = msi_get_remote(hInstall)))
2096 return ERROR_INVALID_HANDLE;
2098 __TRY
2100 ret = remote_SetProperty(remote, szName, szValue);
2102 __EXCEPT(rpc_filter)
2104 ret = GetExceptionCode();
2106 __ENDTRY
2108 return ret;
2111 ret = msi_set_property( package->db, szName, szValue, -1 );
2112 if (ret == ERROR_SUCCESS && !wcscmp( szName, L"SourceDir" ))
2113 msi_reset_source_folders( package );
2115 msiobj_release( &package->hdr );
2116 return ret;
2119 static MSIRECORD *get_property_row( MSIDATABASE *db, const WCHAR *name )
2121 MSIRECORD *rec, *row = NULL;
2122 MSIQUERY *view;
2123 UINT r;
2124 WCHAR *buffer;
2125 int length;
2127 if (!name || !*name)
2128 return NULL;
2130 if (!wcscmp(name, L"Date"))
2132 length = GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, NULL, NULL, NULL, 0);
2133 if (!length)
2134 return NULL;
2135 buffer = malloc(length * sizeof(WCHAR));
2136 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, NULL, NULL, buffer, length);
2138 row = MSI_CreateRecord(1);
2139 if (!row)
2141 free(buffer);
2142 return NULL;
2144 MSI_RecordSetStringW(row, 1, buffer);
2145 free(buffer);
2146 return row;
2148 else if (!wcscmp(name, L"Time"))
2150 length = GetTimeFormatW(LOCALE_USER_DEFAULT, TIME_NOTIMEMARKER, NULL, NULL, NULL, 0);
2151 if (!length)
2152 return NULL;
2153 buffer = malloc(length * sizeof(WCHAR));
2154 GetTimeFormatW(LOCALE_USER_DEFAULT, TIME_NOTIMEMARKER, NULL, NULL, buffer, length);
2156 row = MSI_CreateRecord(1);
2157 if (!row)
2159 free(buffer);
2160 return NULL;
2162 MSI_RecordSetStringW(row, 1, buffer);
2163 free(buffer);
2164 return row;
2167 rec = MSI_CreateRecord(1);
2168 if (!rec)
2169 return NULL;
2171 MSI_RecordSetStringW(rec, 1, name);
2173 r = MSI_DatabaseOpenViewW(db, L"SELECT `Value` FROM `_Property` WHERE `_Property`=?", &view);
2174 if (r == ERROR_SUCCESS)
2176 MSI_ViewExecute(view, rec);
2177 MSI_ViewFetch(view, &row);
2178 MSI_ViewClose(view);
2179 msiobj_release(&view->hdr);
2181 msiobj_release(&rec->hdr);
2182 return row;
2185 /* internal function, not compatible with MsiGetPropertyW */
2186 UINT msi_get_property( MSIDATABASE *db, LPCWSTR szName,
2187 LPWSTR szValueBuf, LPDWORD pchValueBuf )
2189 MSIRECORD *row;
2190 UINT rc = ERROR_FUNCTION_FAILED;
2192 TRACE("%p %s %p %p\n", db, debugstr_w(szName), szValueBuf, pchValueBuf);
2194 row = get_property_row( db, szName );
2196 if (*pchValueBuf > 0)
2197 szValueBuf[0] = 0;
2199 if (row)
2201 rc = MSI_RecordGetStringW(row, 1, szValueBuf, pchValueBuf);
2202 msiobj_release(&row->hdr);
2205 if (rc == ERROR_SUCCESS)
2206 TRACE("returning %s for property %s\n", debugstr_wn(szValueBuf, *pchValueBuf),
2207 debugstr_w(szName));
2208 else if (rc == ERROR_MORE_DATA)
2209 TRACE( "need %lu sized buffer for %s\n", *pchValueBuf, debugstr_w(szName) );
2210 else
2212 *pchValueBuf = 0;
2213 TRACE("property %s not found\n", debugstr_w(szName));
2216 return rc;
2219 LPWSTR msi_dup_property(MSIDATABASE *db, LPCWSTR prop)
2221 DWORD sz = 0;
2222 LPWSTR str;
2223 UINT r;
2225 r = msi_get_property(db, prop, NULL, &sz);
2226 if (r != ERROR_SUCCESS && r != ERROR_MORE_DATA)
2227 return NULL;
2229 sz++;
2230 str = malloc(sz * sizeof(WCHAR));
2231 r = msi_get_property(db, prop, str, &sz);
2232 if (r != ERROR_SUCCESS)
2234 free(str);
2235 str = NULL;
2238 return str;
2241 int msi_get_property_int( MSIDATABASE *db, LPCWSTR prop, int def )
2243 LPWSTR str = msi_dup_property( db, prop );
2244 int val = str ? wcstol(str, NULL, 10) : def;
2245 free(str);
2246 return val;
2249 UINT WINAPI MsiGetPropertyA(MSIHANDLE hinst, const char *name, char *buf, DWORD *sz)
2251 const WCHAR *value = L"";
2252 MSIPACKAGE *package;
2253 MSIRECORD *row;
2254 WCHAR *nameW;
2255 int len = 0;
2256 UINT r;
2258 if (!name)
2259 return ERROR_INVALID_PARAMETER;
2261 if (!(nameW = strdupAtoW(name)))
2262 return ERROR_OUTOFMEMORY;
2264 package = msihandle2msiinfo(hinst, MSIHANDLETYPE_PACKAGE);
2265 if (!package)
2267 WCHAR *value = NULL, *tmp;
2268 MSIHANDLE remote;
2269 DWORD len;
2271 if (!(remote = msi_get_remote(hinst)))
2273 free(nameW);
2274 return ERROR_INVALID_HANDLE;
2277 __TRY
2279 r = remote_GetProperty(remote, nameW, &value, &len);
2281 __EXCEPT(rpc_filter)
2283 r = GetExceptionCode();
2285 __ENDTRY
2287 free(nameW);
2289 if (!r)
2291 /* String might contain embedded nulls.
2292 * Native returns the correct size but truncates the string. */
2293 tmp = calloc(1, (len + 1) * sizeof(WCHAR));
2294 if (!tmp)
2296 midl_user_free(value);
2297 return ERROR_OUTOFMEMORY;
2299 lstrcpyW(tmp, value);
2301 r = msi_strncpyWtoA(tmp, len, buf, sz, TRUE);
2303 free(tmp);
2305 midl_user_free(value);
2306 return r;
2309 row = get_property_row(package->db, nameW);
2310 if (row)
2311 value = msi_record_get_string(row, 1, &len);
2313 r = msi_strncpyWtoA(value, len, buf, sz, FALSE);
2315 free(nameW);
2316 if (row) msiobj_release(&row->hdr);
2317 msiobj_release(&package->hdr);
2318 return r;
2321 UINT WINAPI MsiGetPropertyW(MSIHANDLE hinst, const WCHAR *name, WCHAR *buf, DWORD *sz)
2323 const WCHAR *value = L"";
2324 MSIPACKAGE *package;
2325 MSIRECORD *row;
2326 int len = 0;
2327 UINT r;
2329 if (!name)
2330 return ERROR_INVALID_PARAMETER;
2332 package = msihandle2msiinfo(hinst, MSIHANDLETYPE_PACKAGE);
2333 if (!package)
2335 WCHAR *value = NULL, *tmp;
2336 MSIHANDLE remote;
2337 DWORD len;
2339 if (!(remote = msi_get_remote(hinst)))
2340 return ERROR_INVALID_HANDLE;
2342 __TRY
2344 r = remote_GetProperty(remote, name, &value, &len);
2346 __EXCEPT(rpc_filter)
2348 r = GetExceptionCode();
2350 __ENDTRY
2352 if (!r)
2354 /* String might contain embedded nulls.
2355 * Native returns the correct size but truncates the string. */
2356 tmp = calloc(1, (len + 1) * sizeof(WCHAR));
2357 if (!tmp)
2359 midl_user_free(value);
2360 return ERROR_OUTOFMEMORY;
2362 lstrcpyW(tmp, value);
2364 r = msi_strncpyW(tmp, len, buf, sz);
2366 free(tmp);
2368 midl_user_free(value);
2369 return r;
2372 row = get_property_row(package->db, name);
2373 if (row)
2374 value = msi_record_get_string(row, 1, &len);
2376 r = msi_strncpyW(value, len, buf, sz);
2378 if (row) msiobj_release(&row->hdr);
2379 msiobj_release(&package->hdr);
2380 return r;
2383 MSIHANDLE __cdecl s_remote_GetActiveDatabase(MSIHANDLE hinst)
2385 return MsiGetActiveDatabase(hinst);
2388 UINT __cdecl s_remote_GetProperty(MSIHANDLE hinst, LPCWSTR property, LPWSTR *value, DWORD *size)
2390 WCHAR empty[1];
2391 UINT r;
2393 *size = 0;
2394 r = MsiGetPropertyW(hinst, property, empty, size);
2395 if (r == ERROR_MORE_DATA)
2397 ++*size;
2398 *value = midl_user_allocate(*size * sizeof(WCHAR));
2399 if (!*value)
2400 return ERROR_OUTOFMEMORY;
2401 r = MsiGetPropertyW(hinst, property, *value, size);
2403 return r;
2406 UINT __cdecl s_remote_SetProperty(MSIHANDLE hinst, LPCWSTR property, LPCWSTR value)
2408 return MsiSetPropertyW(hinst, property, value);
2411 int __cdecl s_remote_ProcessMessage(MSIHANDLE hinst, INSTALLMESSAGE message, struct wire_record *remote_rec)
2413 MSIHANDLE rec;
2414 int ret;
2415 UINT r;
2417 if ((r = unmarshal_record(remote_rec, &rec)))
2418 return r;
2420 ret = MsiProcessMessage(hinst, message, rec);
2422 MsiCloseHandle(rec);
2423 return ret;
2426 UINT __cdecl s_remote_DoAction(MSIHANDLE hinst, LPCWSTR action)
2428 return MsiDoActionW(hinst, action);
2431 UINT __cdecl s_remote_Sequence(MSIHANDLE hinst, LPCWSTR table, int sequence)
2433 return MsiSequenceW(hinst, table, sequence);
2436 UINT __cdecl s_remote_GetTargetPath(MSIHANDLE hinst, LPCWSTR folder, LPWSTR *value)
2438 WCHAR empty[1];
2439 DWORD size = 0;
2440 UINT r;
2442 r = MsiGetTargetPathW(hinst, folder, empty, &size);
2443 if (r == ERROR_MORE_DATA)
2445 *value = midl_user_allocate(++size * sizeof(WCHAR));
2446 if (!*value)
2447 return ERROR_OUTOFMEMORY;
2448 r = MsiGetTargetPathW(hinst, folder, *value, &size);
2450 return r;
2453 UINT __cdecl s_remote_SetTargetPath(MSIHANDLE hinst, LPCWSTR folder, LPCWSTR value)
2455 return MsiSetTargetPathW(hinst, folder, value);
2458 UINT __cdecl s_remote_GetSourcePath(MSIHANDLE hinst, LPCWSTR folder, LPWSTR *value)
2460 WCHAR empty[1];
2461 DWORD size = 1;
2462 UINT r;
2464 r = MsiGetSourcePathW(hinst, folder, empty, &size);
2465 if (r == ERROR_MORE_DATA)
2467 *value = midl_user_allocate(++size * sizeof(WCHAR));
2468 if (!*value)
2469 return ERROR_OUTOFMEMORY;
2470 r = MsiGetSourcePathW(hinst, folder, *value, &size);
2472 return r;
2475 BOOL __cdecl s_remote_GetMode(MSIHANDLE hinst, MSIRUNMODE mode)
2477 return MsiGetMode(hinst, mode);
2480 UINT __cdecl s_remote_SetMode(MSIHANDLE hinst, MSIRUNMODE mode, BOOL state)
2482 return MsiSetMode(hinst, mode, state);
2485 UINT __cdecl s_remote_GetFeatureState(MSIHANDLE hinst, LPCWSTR feature,
2486 INSTALLSTATE *installed, INSTALLSTATE *action)
2488 return MsiGetFeatureStateW(hinst, feature, installed, action);
2491 UINT __cdecl s_remote_SetFeatureState(MSIHANDLE hinst, LPCWSTR feature, INSTALLSTATE state)
2493 return MsiSetFeatureStateW(hinst, feature, state);
2496 UINT __cdecl s_remote_GetComponentState(MSIHANDLE hinst, LPCWSTR component,
2497 INSTALLSTATE *installed, INSTALLSTATE *action)
2499 return MsiGetComponentStateW(hinst, component, installed, action);
2502 UINT __cdecl s_remote_SetComponentState(MSIHANDLE hinst, LPCWSTR component, INSTALLSTATE state)
2504 return MsiSetComponentStateW(hinst, component, state);
2507 LANGID __cdecl s_remote_GetLanguage(MSIHANDLE hinst)
2509 return MsiGetLanguage(hinst);
2512 UINT __cdecl s_remote_SetInstallLevel(MSIHANDLE hinst, int level)
2514 return MsiSetInstallLevel(hinst, level);
2517 UINT __cdecl s_remote_FormatRecord(MSIHANDLE hinst, struct wire_record *remote_rec, LPWSTR *value)
2519 WCHAR empty[1];
2520 DWORD size = 0;
2521 MSIHANDLE rec;
2522 UINT r;
2524 if ((r = unmarshal_record(remote_rec, &rec)))
2525 return r;
2527 r = MsiFormatRecordW(hinst, rec, empty, &size);
2528 if (r == ERROR_MORE_DATA)
2530 *value = midl_user_allocate(++size * sizeof(WCHAR));
2531 if (!*value)
2533 MsiCloseHandle(rec);
2534 return ERROR_OUTOFMEMORY;
2536 r = MsiFormatRecordW(hinst, rec, *value, &size);
2539 MsiCloseHandle(rec);
2540 return r;
2543 MSICONDITION __cdecl s_remote_EvaluateCondition(MSIHANDLE hinst, LPCWSTR condition)
2545 return MsiEvaluateConditionW(hinst, condition);
2548 UINT __cdecl s_remote_GetFeatureCost(MSIHANDLE hinst, LPCWSTR feature,
2549 MSICOSTTREE cost_tree, INSTALLSTATE state, INT *cost)
2551 return MsiGetFeatureCostW(hinst, feature, cost_tree, state, cost);
2554 UINT __cdecl s_remote_EnumComponentCosts(MSIHANDLE hinst, LPCWSTR component,
2555 DWORD index, INSTALLSTATE state, LPWSTR drive, INT *cost, INT *temp)
2557 DWORD size = 3;
2558 return MsiEnumComponentCostsW(hinst, component, index, state, drive, &size, cost, temp);
2561 UINT msi_package_add_info(MSIPACKAGE *package, DWORD context, DWORD options,
2562 LPCWSTR property, LPWSTR value)
2564 MSISOURCELISTINFO *info;
2566 LIST_FOR_EACH_ENTRY( info, &package->sourcelist_info, MSISOURCELISTINFO, entry )
2568 if (!wcscmp( info->value, value )) return ERROR_SUCCESS;
2571 info = malloc(sizeof(MSISOURCELISTINFO));
2572 if (!info)
2573 return ERROR_OUTOFMEMORY;
2575 info->context = context;
2576 info->options = options;
2577 info->property = property;
2578 info->value = wcsdup(value);
2579 list_add_head(&package->sourcelist_info, &info->entry);
2581 return ERROR_SUCCESS;
2584 UINT msi_package_add_media_disk(MSIPACKAGE *package, DWORD context, DWORD options,
2585 DWORD disk_id, LPWSTR volume_label, LPWSTR disk_prompt)
2587 MSIMEDIADISK *disk;
2589 LIST_FOR_EACH_ENTRY( disk, &package->sourcelist_media, MSIMEDIADISK, entry )
2591 if (disk->disk_id == disk_id) return ERROR_SUCCESS;
2594 disk = malloc(sizeof(MSIMEDIADISK));
2595 if (!disk)
2596 return ERROR_OUTOFMEMORY;
2598 disk->context = context;
2599 disk->options = options;
2600 disk->disk_id = disk_id;
2601 disk->volume_label = wcsdup(volume_label);
2602 disk->disk_prompt = wcsdup(disk_prompt);
2603 list_add_head(&package->sourcelist_media, &disk->entry);
2605 return ERROR_SUCCESS;