2 * Custom Action processing for the Microsoft Installer (msi.dll)
4 * Copyright 2005 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
33 #include "msiserver.h"
34 #include "wine/debug.h"
35 #include "wine/unicode.h"
36 #include "wine/exception.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(msi);
40 #define CUSTOM_ACTION_TYPE_MASK 0x3F
41 static const WCHAR c_collen[] = {'C',':','\\',0};
42 static const WCHAR cszTempFolder[]= {'T','e','m','p','F','o','l','d','e','r',0};
45 static const WCHAR szActionData[] = {
46 'C','u','s','t','o','m','A','c','t','i','o','n','D','a','t','a',0
48 static const WCHAR ProdCode[] = {
49 'P','r','o','d','u','c','t','C','o','d','e',0
51 static const WCHAR UserSID[] = {'U','s','e','r','S','I','D',0};
53 typedef struct tagMSIRUNNINGACTION
61 static UINT HANDLE_CustomType1(MSIPACKAGE *package, LPCWSTR source,
62 LPCWSTR target, const INT type, LPCWSTR action);
63 static UINT HANDLE_CustomType2(MSIPACKAGE *package, LPCWSTR source,
64 LPCWSTR target, const INT type, LPCWSTR action);
65 static UINT HANDLE_CustomType17(MSIPACKAGE *package, LPCWSTR source,
66 LPCWSTR target, const INT type, LPCWSTR action);
67 static UINT HANDLE_CustomType18(MSIPACKAGE *package, LPCWSTR source,
68 LPCWSTR target, const INT type, LPCWSTR action);
69 static UINT HANDLE_CustomType19(MSIPACKAGE *package, LPCWSTR source,
70 LPCWSTR target, const INT type, LPCWSTR action);
71 static UINT HANDLE_CustomType23(MSIPACKAGE *package, LPCWSTR source,
72 LPCWSTR target, const INT type, LPCWSTR action);
73 static UINT HANDLE_CustomType50(MSIPACKAGE *package, LPCWSTR source,
74 LPCWSTR target, const INT type, LPCWSTR action);
75 static UINT HANDLE_CustomType34(MSIPACKAGE *package, LPCWSTR source,
76 LPCWSTR target, const INT type, LPCWSTR action);
77 static UINT HANDLE_CustomType37_38(MSIPACKAGE *package, LPCWSTR source,
78 LPCWSTR target, const INT type, LPCWSTR action);
79 static UINT HANDLE_CustomType5_6(MSIPACKAGE *package, LPCWSTR source,
80 LPCWSTR target, const INT type, LPCWSTR action);
81 static UINT HANDLE_CustomType21_22(MSIPACKAGE *package, LPCWSTR source,
82 LPCWSTR target, const INT type, LPCWSTR action);
83 static UINT HANDLE_CustomType53_54(MSIPACKAGE *package, LPCWSTR source,
84 LPCWSTR target, const INT type, LPCWSTR action);
86 typedef UINT (WINAPI *MsiCustomActionEntryPoint)( MSIHANDLE );
88 static CRITICAL_SECTION msi_custom_action_cs;
89 static CRITICAL_SECTION_DEBUG msi_custom_action_cs_debug =
91 0, 0, &msi_custom_action_cs,
92 { &msi_custom_action_cs_debug.ProcessLocksList,
93 &msi_custom_action_cs_debug.ProcessLocksList },
94 0, 0, { (DWORD_PTR)(__FILE__ ": msi_custom_action_cs") }
96 static CRITICAL_SECTION msi_custom_action_cs = { &msi_custom_action_cs_debug, -1, 0, 0, 0, 0 };
98 static struct list msi_pending_custom_actions = LIST_INIT( msi_pending_custom_actions );
100 static BOOL check_execution_scheduling_options(MSIPACKAGE *package, LPCWSTR action, UINT options)
102 if (!package->script)
105 if ((options & msidbCustomActionTypeClientRepeat) ==
106 msidbCustomActionTypeClientRepeat)
108 if (!(package->script->InWhatSequence & SEQUENCE_UI &&
109 package->script->InWhatSequence & SEQUENCE_EXEC))
111 TRACE("Skipping action due to dbCustomActionTypeClientRepeat option.\n");
115 else if (options & msidbCustomActionTypeFirstSequence)
117 if (package->script->InWhatSequence & SEQUENCE_UI &&
118 package->script->InWhatSequence & SEQUENCE_EXEC )
120 TRACE("Skipping action due to msidbCustomActionTypeFirstSequence option.\n");
124 else if (options & msidbCustomActionTypeOncePerProcess)
126 if (check_unique_action(package,action))
128 TRACE("Skipping action due to msidbCustomActionTypeOncePerProcess option.\n");
132 register_unique_action(package,action);
138 /* stores the following properties before the action:
140 * [CustomActionData<=>UserSID<=>ProductCode]Action
142 static LPWSTR msi_get_deferred_action(LPCWSTR action, LPCWSTR actiondata,
143 LPCWSTR usersid, LPCWSTR prodcode)
148 static const WCHAR format[] = {
149 '[','%','s','<','=','>','%','s','<','=','>','%','s',']','%','s',0
153 return strdupW(action);
155 len = lstrlenW(action) + lstrlenW(actiondata) +
156 lstrlenW(usersid) + lstrlenW(prodcode) + 5;
157 deferred = msi_alloc(len * sizeof(WCHAR));
159 sprintfW(deferred, format, actiondata, usersid, prodcode, action);
163 static void set_deferred_action_props(MSIPACKAGE *package, LPWSTR deferred_data)
165 LPWSTR end, beg = deferred_data + 1;
167 static const WCHAR sep[] = {'<','=','>',0};
169 end = strstrW(beg, sep);
171 MSI_SetPropertyW(package, szActionData, beg);
174 end = strstrW(beg, sep);
176 MSI_SetPropertyW(package, UserSID, beg);
179 end = strchrW(beg, ']');
181 MSI_SetPropertyW(package, ProdCode, beg);
184 UINT ACTION_CustomAction(MSIPACKAGE *package, LPCWSTR action, UINT script, BOOL execute)
186 UINT rc = ERROR_SUCCESS;
188 static const WCHAR ExecSeqQuery[] =
189 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
190 '`','C','u','s','t','o' ,'m','A','c','t','i','o','n','`',
191 ' ','W','H','E','R','E',' ','`','A','c','t','i' ,'o','n','`',' ',
192 '=',' ','\'','%','s','\'',0};
194 LPCWSTR source, target;
195 LPWSTR ptr, deferred_data = NULL;
196 LPWSTR action_copy = strdupW(action);
197 WCHAR *deformated=NULL;
199 /* deferred action: [properties]Action */
200 if ((ptr = strrchrW(action_copy, ']')))
202 deferred_data = action_copy;
206 row = MSI_QueryGetRecord( package->db, ExecSeqQuery, action );
209 msi_free(action_copy);
210 return ERROR_CALL_NOT_IMPLEMENTED;
213 type = MSI_RecordGetInteger(row,2);
215 source = MSI_RecordGetString(row,3);
216 target = MSI_RecordGetString(row,4);
218 TRACE("Handling custom action %s (%x %s %s)\n",debugstr_w(action),type,
219 debugstr_w(source), debugstr_w(target));
221 /* handle some of the deferred actions */
222 if (type & msidbCustomActionTypeTSAware)
223 FIXME("msidbCustomActionTypeTSAware not handled\n");
225 if (type & msidbCustomActionTypeInScript)
227 if (type & msidbCustomActionTypeNoImpersonate)
228 FIXME("msidbCustomActionTypeNoImpersonate not handled\n");
230 if (type & msidbCustomActionTypeRollback)
232 FIXME("Rollback only action... rollbacks not supported yet\n");
233 schedule_action(package, ROLLBACK_SCRIPT, action);
239 LPWSTR actiondata = msi_dup_property(package, action);
240 LPWSTR usersid = msi_dup_property(package, UserSID);
241 LPWSTR prodcode = msi_dup_property(package, ProdCode);
242 LPWSTR deferred = msi_get_deferred_action(action, actiondata, usersid, prodcode);
244 if (type & msidbCustomActionTypeCommit)
246 TRACE("Deferring Commit Action!\n");
247 schedule_action(package, COMMIT_SCRIPT, deferred);
251 TRACE("Deferring Action!\n");
252 schedule_action(package, INSTALL_SCRIPT, deferred);
256 msi_free(actiondata);
264 static const WCHAR szBlank[] = {0};
266 LPWSTR actiondata = msi_dup_property( package, action );
271 package->scheduled_action_running = TRUE;
274 package->commit_action_running = TRUE;
276 case ROLLBACK_SCRIPT:
277 package->rollback_action_running = TRUE;
284 set_deferred_action_props(package, deferred_data);
286 MSI_SetPropertyW(package,szActionData,actiondata);
288 MSI_SetPropertyW(package,szActionData,szBlank);
290 msi_free(actiondata);
293 else if (!check_execution_scheduling_options(package,action,type))
299 switch (type & CUSTOM_ACTION_TYPE_MASK)
301 case 1: /* DLL file stored in a Binary table stream */
302 rc = HANDLE_CustomType1(package,source,target,type,action);
304 case 2: /* EXE file stored in a Binary table stream */
305 rc = HANDLE_CustomType2(package,source,target,type,action);
307 case 18: /*EXE file installed with package */
308 rc = HANDLE_CustomType18(package,source,target,type,action);
310 case 19: /* Error that halts install */
311 rc = HANDLE_CustomType19(package,source,target,type,action);
314 rc = HANDLE_CustomType17(package,source,target,type,action);
316 case 23: /* installs another package in the source tree */
317 deformat_string(package,target,&deformated);
318 rc = HANDLE_CustomType23(package,source,deformated,type,action);
320 case 50: /*EXE file specified by a property value */
321 rc = HANDLE_CustomType50(package,source,target,type,action);
323 case 34: /*EXE to be run in specified directory */
324 rc = HANDLE_CustomType34(package,source,target,type,action);
326 case 35: /* Directory set with formatted text. */
327 deformat_string(package,target,&deformated);
328 MSI_SetTargetPathW(package, source, deformated);
329 msi_free(deformated);
331 case 51: /* Property set with formatted text. */
332 deformat_string(package,target,&deformated);
333 rc = MSI_SetPropertyW(package,source,deformated);
334 msi_free(deformated);
336 case 37: /* JScript/VBScript text stored in target column. */
338 rc = HANDLE_CustomType37_38(package,source,target,type,action);
341 case 6: /* JScript/VBScript file stored in a Binary table stream. */
342 rc = HANDLE_CustomType5_6(package,source,target,type,action);
344 case 21: /* JScript/VBScript file installed with the product. */
346 rc = HANDLE_CustomType21_22(package,source,target,type,action);
348 case 53: /* JScript/VBScript text specified by a property value. */
350 rc = HANDLE_CustomType53_54(package,source,target,type,action);
353 FIXME("UNHANDLED ACTION TYPE %i (%s %s)\n",
354 type & CUSTOM_ACTION_TYPE_MASK, debugstr_w(source),
359 package->scheduled_action_running = FALSE;
360 package->commit_action_running = FALSE;
361 package->rollback_action_running = FALSE;
362 msi_free(action_copy);
363 msiobj_release(&row->hdr);
368 static UINT store_binary_to_temp(MSIPACKAGE *package, LPCWSTR source,
371 static const WCHAR query[] = {
372 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
373 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',' ',
374 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
378 static const WCHAR f1[] = {'m','s','i',0};
383 if (MSI_GetPropertyW(package, cszTempFolder, fmt, &sz) != ERROR_SUCCESS)
384 GetTempPathW(MAX_PATH, fmt);
386 if (GetTempFileNameW(fmt, f1, 0, tmp_file) == 0)
388 TRACE("Unable to create file\n");
389 return ERROR_FUNCTION_FAILED;
391 track_tempfile(package, tmp_file);
393 row = MSI_QueryGetRecord(package->db, query, source);
395 return ERROR_FUNCTION_FAILED;
397 /* write out the file */
398 file = CreateFileW(tmp_file, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
399 FILE_ATTRIBUTE_NORMAL, NULL);
400 if (file == INVALID_HANDLE_VALUE)
401 r = ERROR_FUNCTION_FAILED;
408 r = MSI_RecordReadStream(row, 2, buffer, &sz);
409 if (r != ERROR_SUCCESS)
411 ERR("Failed to get stream\n");
414 WriteFile(file, buffer, sz, &write, NULL);
415 } while (sz == sizeof buffer);
419 msiobj_release(&row->hdr);
424 static void file_running_action(MSIPACKAGE* package, HANDLE Handle,
425 BOOL process, LPCWSTR name)
427 MSIRUNNINGACTION *action;
429 action = msi_alloc( sizeof(MSIRUNNINGACTION) );
431 action->handle = Handle;
432 action->process = process;
433 action->name = strdupW(name);
435 list_add_tail( &package->RunningActions, &action->entry );
438 static UINT custom_get_process_return( HANDLE process )
442 GetExitCodeProcess( process, &rc );
444 return ERROR_FUNCTION_FAILED;
445 return ERROR_SUCCESS;
448 static UINT custom_get_thread_return( MSIPACKAGE *package, HANDLE thread )
452 GetExitCodeThread( thread, &rc );
456 case ERROR_FUNCTION_NOT_CALLED:
458 case ERROR_INSTALL_USEREXIT:
459 case ERROR_INSTALL_FAILURE:
461 case ERROR_NO_MORE_ITEMS:
462 return ERROR_SUCCESS;
463 case ERROR_INSTALL_SUSPEND:
464 ACTION_ForceReboot( package );
465 return ERROR_SUCCESS;
467 ERR("Invalid Return Code %d\n",rc);
468 return ERROR_INSTALL_FAILURE;
472 static UINT wait_process_handle(MSIPACKAGE* package, UINT type,
473 HANDLE ProcessHandle, LPCWSTR name)
475 UINT rc = ERROR_SUCCESS;
477 if (!(type & msidbCustomActionTypeAsync))
479 TRACE("waiting for %s\n", debugstr_w(name));
481 msi_dialog_check_messages(ProcessHandle);
483 if (!(type & msidbCustomActionTypeContinue))
484 rc = custom_get_process_return(ProcessHandle);
486 CloseHandle(ProcessHandle);
490 TRACE("%s running in background\n", debugstr_w(name));
492 if (!(type & msidbCustomActionTypeContinue))
493 file_running_action(package, ProcessHandle, TRUE, name);
495 CloseHandle(ProcessHandle);
501 typedef struct _msi_custom_action_info {
511 } msi_custom_action_info;
513 static void release_custom_action_data( msi_custom_action_info *info )
515 EnterCriticalSection( &msi_custom_action_cs );
519 list_remove( &info->entry );
521 CloseHandle( info->handle );
522 msi_free( info->action );
523 msi_free( info->source );
524 msi_free( info->target );
525 msiobj_release( &info->package->hdr );
529 LeaveCriticalSection( &msi_custom_action_cs );
532 /* must be called inside msi_custom_action_cs if info is in the pending custom actions list */
533 static void addref_custom_action_data( msi_custom_action_info *info )
538 static UINT wait_thread_handle( msi_custom_action_info *info )
540 UINT rc = ERROR_SUCCESS;
542 if (!(info->type & msidbCustomActionTypeAsync))
544 TRACE("waiting for %s\n", debugstr_w( info->action ));
546 msi_dialog_check_messages( info->handle );
548 if (!(info->type & msidbCustomActionTypeContinue))
549 rc = custom_get_thread_return( info->package, info->handle );
551 release_custom_action_data( info );
555 TRACE("%s running in background\n", debugstr_w( info->action ));
561 static msi_custom_action_info *find_action_by_guid( const GUID *guid )
563 msi_custom_action_info *info;
566 EnterCriticalSection( &msi_custom_action_cs );
568 LIST_FOR_EACH_ENTRY( info, &msi_pending_custom_actions, msi_custom_action_info, entry )
570 if (IsEqualGUID( &info->guid, guid ))
572 addref_custom_action_data( info );
578 LeaveCriticalSection( &msi_custom_action_cs );
586 static void handle_msi_break( LPCWSTR target )
591 static const WCHAR MsiBreak[] = { 'M','s','i','B','r','e','a','k',0 };
592 static const WCHAR WindowsInstaller[] = {
593 'W','i','n','d','o','w','s',' ','I','n','s','t','a','l','l','e','r',0
596 static const WCHAR format[] = {
597 'T','o',' ','d','e','b','u','g',' ','y','o','u','r',' ',
598 'c','u','s','t','o','m',' ','a','c','t','i','o','n',',',' ',
599 'a','t','t','a','c','h',' ','y','o','u','r',' ','d','e','b','u','g','g','e','r',' ',
600 't','o',' ','p','r','o','c','e','s','s',' ','%','i',' ','(','0','x','%','X',')',' ',
601 'a','n','d',' ','p','r','e','s','s',' ','O','K',0
604 if( !GetEnvironmentVariableW( MsiBreak, val, MAX_PATH ))
607 if( lstrcmpiW( val, target ))
610 msg = msi_alloc( (lstrlenW(format) + 10) * sizeof(WCHAR) );
614 wsprintfW( msg, format, GetCurrentProcessId(), GetCurrentProcessId());
615 MessageBoxW( NULL, msg, WindowsInstaller, MB_OK);
620 static UINT get_action_info( const GUID *guid, INT *type, MSIHANDLE *handle,
621 BSTR *dll, BSTR *funcname,
622 IWineMsiRemotePackage **package )
624 IClassFactory *cf = NULL;
625 IWineMsiRemoteCustomAction *rca = NULL;
628 r = DllGetClassObject( &CLSID_IWineMsiRemoteCustomAction,
629 &IID_IClassFactory, (LPVOID *)&cf );
632 ERR("failed to get IClassFactory interface\n");
633 return ERROR_FUNCTION_FAILED;
636 r = IClassFactory_CreateInstance( cf, NULL, &IID_IWineMsiRemoteCustomAction, (LPVOID *)&rca );
639 ERR("failed to get IWineMsiRemoteCustomAction interface\n");
640 return ERROR_FUNCTION_FAILED;
643 r = IWineMsiRemoteCustomAction_GetActionInfo( rca, guid, type, handle, dll, funcname, package );
644 IWineMsiRemoteCustomAction_Release( rca );
647 ERR("GetActionInfo failed\n");
648 return ERROR_FUNCTION_FAILED;
651 return ERROR_SUCCESS;
654 static DWORD WINAPI ACTION_CallDllFunction( const GUID *guid )
656 MsiCustomActionEntryPoint fn;
657 MSIHANDLE hPackage, handle;
660 UINT r = ERROR_FUNCTION_FAILED;
661 BSTR dll = NULL, function = NULL;
663 IWineMsiRemotePackage *remote_package = NULL;
665 TRACE("%s\n", debugstr_guid( guid ));
667 r = get_action_info( guid, &type, &handle, &dll, &function, &remote_package );
668 if (r != ERROR_SUCCESS)
671 hModule = LoadLibraryW( dll );
674 ERR("failed to load dll %s\n", debugstr_w( dll ) );
678 proc = strdupWtoA( function );
679 fn = (MsiCustomActionEntryPoint) GetProcAddress( hModule, proc );
683 hPackage = alloc_msi_remote_handle( (IUnknown *)remote_package );
686 IWineMsiRemotePackage_SetMsiHandle( remote_package, handle );
687 TRACE("calling %s\n", debugstr_w( function ) );
688 handle_msi_break( function );
696 ERR("Custom action (%s:%s) caused a page fault: %08x\n",
697 debugstr_w(dll), debugstr_w(function), GetExceptionCode());
702 MsiCloseHandle( hPackage );
705 ERR("failed to create handle for %p\n", remote_package );
708 ERR("GetProcAddress(%s) failed\n", debugstr_w( function ) );
710 FreeLibrary(hModule);
712 IWineMsiRemotePackage_Release( remote_package );
713 SysFreeString( dll );
714 SysFreeString( function );
715 MsiCloseHandle( handle );
720 static DWORD WINAPI DllThread( LPVOID arg )
725 TRACE("custom action (%x) started\n", GetCurrentThreadId() );
727 rc = ACTION_CallDllFunction( guid );
729 TRACE("custom action (%x) returned %i\n", GetCurrentThreadId(), rc );
731 MsiCloseAllHandles();
735 static DWORD WINAPI ACTION_CAInstallPackage(const GUID *guid)
737 msi_custom_action_info *info;
738 UINT r = ERROR_FUNCTION_FAILED;
739 INSTALLUILEVEL old_level;
741 info = find_action_by_guid(guid);
744 ERR("failed to find action %s\n", debugstr_guid(guid));
748 old_level = MsiSetInternalUI(INSTALLUILEVEL_BASIC, NULL);
749 r = MsiInstallProductW(info->source, info->target);
750 MsiSetInternalUI(old_level, NULL);
752 release_custom_action_data(info);
757 static DWORD WINAPI ConcurrentInstallThread(LPVOID arg)
762 TRACE("concurrent installation (%x) started\n", GetCurrentThreadId());
764 rc = ACTION_CAInstallPackage(guid);
766 TRACE("concurrent installation (%x) returned %i\n", GetCurrentThreadId(), rc);
768 MsiCloseAllHandles();
772 static msi_custom_action_info *do_msidbCustomActionTypeDll(
773 MSIPACKAGE *package, INT type, LPCWSTR source, LPCWSTR target, LPCWSTR action )
775 msi_custom_action_info *info;
777 info = msi_alloc( sizeof *info );
781 msiobj_addref( &package->hdr );
782 info->refs = 2; /* 1 for our caller and 1 for thread we created */
783 info->package = package;
785 info->target = strdupW( target );
786 info->source = strdupW( source );
787 info->action = strdupW( action );
788 CoCreateGuid( &info->guid );
790 EnterCriticalSection( &msi_custom_action_cs );
791 list_add_tail( &msi_pending_custom_actions, &info->entry );
792 LeaveCriticalSection( &msi_custom_action_cs );
794 info->handle = CreateThread( NULL, 0, DllThread, &info->guid, 0, NULL );
797 /* release both references */
798 release_custom_action_data( info );
799 release_custom_action_data( info );
806 static msi_custom_action_info *do_msidbCAConcurrentInstall(
807 MSIPACKAGE *package, INT type, LPCWSTR source, LPCWSTR target, LPCWSTR action)
809 msi_custom_action_info *info;
811 info = msi_alloc( sizeof *info );
815 msiobj_addref( &package->hdr );
816 info->refs = 2; /* 1 for our caller and 1 for thread we created */
817 info->package = package;
819 info->target = strdupW( target );
820 info->source = strdupW( source );
821 info->action = strdupW( action );
822 CoCreateGuid( &info->guid );
824 EnterCriticalSection( &msi_custom_action_cs );
825 list_add_tail( &msi_pending_custom_actions, &info->entry );
826 LeaveCriticalSection( &msi_custom_action_cs );
828 info->handle = CreateThread( NULL, 0, ConcurrentInstallThread, &info->guid, 0, NULL );
831 /* release both references */
832 release_custom_action_data( info );
833 release_custom_action_data( info );
840 static UINT HANDLE_CustomType23(MSIPACKAGE *package, LPCWSTR source,
841 LPCWSTR target, const INT type, LPCWSTR action)
843 msi_custom_action_info *info;
844 WCHAR package_path[MAX_PATH];
847 static const WCHAR backslash[] = {'\\',0};
850 MSI_GetPropertyW(package, cszSourceDir, package_path, &size);
851 lstrcatW(package_path, backslash);
852 lstrcatW(package_path, source);
854 if (GetFileAttributesW(package_path) == INVALID_FILE_ATTRIBUTES)
856 ERR("Source package does not exist: %s\n", debugstr_w(package_path));
857 return ERROR_FUNCTION_FAILED;
860 TRACE("Installing package %s concurrently\n", debugstr_w(package_path));
862 info = do_msidbCAConcurrentInstall(package, type, package_path, target, action);
864 return wait_thread_handle(info);
867 static UINT HANDLE_CustomType1(MSIPACKAGE *package, LPCWSTR source,
868 LPCWSTR target, const INT type, LPCWSTR action)
870 msi_custom_action_info *info;
871 WCHAR tmp_file[MAX_PATH];
874 r = store_binary_to_temp(package, source, tmp_file);
875 if (r != ERROR_SUCCESS)
878 TRACE("Calling function %s from %s\n",debugstr_w(target),
879 debugstr_w(tmp_file));
881 if (!strchrW(tmp_file,'.'))
883 static const WCHAR dot[]={'.',0};
884 strcatW(tmp_file,dot);
887 info = do_msidbCustomActionTypeDll( package, type, tmp_file, target, action );
889 return wait_thread_handle( info );
892 static UINT HANDLE_CustomType2(MSIPACKAGE *package, LPCWSTR source,
893 LPCWSTR target, const INT type, LPCWSTR action)
895 WCHAR tmp_file[MAX_PATH];
897 PROCESS_INFORMATION info;
900 WCHAR *deformated = NULL;
902 static const WCHAR spc[] = {' ',0};
905 memset(&si,0,sizeof(STARTUPINFOW));
907 r = store_binary_to_temp(package, source, tmp_file);
908 if (r != ERROR_SUCCESS)
911 deformat_string(package,target,&deformated);
913 len = strlenW(tmp_file)+2;
916 len += strlenW(deformated);
918 cmd = msi_alloc(sizeof(WCHAR)*len);
920 strcpyW(cmd,tmp_file);
924 strcatW(cmd,deformated);
926 msi_free(deformated);
929 TRACE("executing exe %s\n", debugstr_w(cmd));
931 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
932 c_collen, &si, &info);
937 ERR("Unable to execute command %s\n", debugstr_w(cmd));
938 return ERROR_SUCCESS;
940 CloseHandle( info.hThread );
942 r = wait_process_handle(package, type, info.hProcess, action);
947 static UINT HANDLE_CustomType17(MSIPACKAGE *package, LPCWSTR source,
948 LPCWSTR target, const INT type, LPCWSTR action)
950 msi_custom_action_info *info;
953 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
955 file = get_loaded_file( package, source );
958 ERR("invalid file key %s\n", debugstr_w( source ));
959 return ERROR_FUNCTION_FAILED;
962 info = do_msidbCustomActionTypeDll( package, type, file->TargetPath, target, action );
964 return wait_thread_handle( info );
967 static UINT HANDLE_CustomType18(MSIPACKAGE *package, LPCWSTR source,
968 LPCWSTR target, const INT type, LPCWSTR action)
971 PROCESS_INFORMATION info;
976 static const WCHAR spc[] = {' ',0};
979 memset(&si,0,sizeof(STARTUPINFOW));
981 file = get_loaded_file(package,source);
983 return ERROR_FUNCTION_FAILED;
985 len = lstrlenW( file->TargetPath );
987 deformat_string(package,target,&deformated);
989 len += strlenW(deformated);
992 cmd = msi_alloc(len * sizeof(WCHAR));
994 lstrcpyW( cmd, file->TargetPath);
998 strcatW(cmd, deformated);
1000 msi_free(deformated);
1003 TRACE("executing exe %s\n", debugstr_w(cmd));
1005 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
1006 c_collen, &si, &info);
1010 ERR("Unable to execute command %s\n", debugstr_w(cmd));
1012 return ERROR_SUCCESS;
1015 CloseHandle( info.hThread );
1017 return wait_process_handle(package, type, info.hProcess, action);
1020 static UINT HANDLE_CustomType19(MSIPACKAGE *package, LPCWSTR source,
1021 LPCWSTR target, const INT type, LPCWSTR action)
1023 static const WCHAR query[] = {
1024 'S','E','L','E','C','T',' ','`','M','e','s','s','a','g','e','`',' ',
1025 'F','R','O','M',' ','`','E','r','r','o','r','`',' ',
1026 'W','H','E','R','E',' ','`','E','r','r','o','r','`',' ','=',' ',
1030 LPWSTR deformated = NULL;
1032 deformat_string( package, target, &deformated );
1034 /* first try treat the error as a number */
1035 row = MSI_QueryGetRecord( package->db, query, deformated );
1038 LPCWSTR error = MSI_RecordGetString( row, 1 );
1039 MessageBoxW( NULL, error, NULL, MB_OK );
1040 msiobj_release( &row->hdr );
1043 MessageBoxW( NULL, deformated, NULL, MB_OK );
1045 msi_free( deformated );
1047 return ERROR_FUNCTION_FAILED;
1050 static UINT HANDLE_CustomType50(MSIPACKAGE *package, LPCWSTR source,
1051 LPCWSTR target, const INT type, LPCWSTR action)
1054 PROCESS_INFORMATION info;
1060 static const WCHAR spc[] = {' ',0};
1062 memset(&si,0,sizeof(STARTUPINFOW));
1063 memset(&info,0,sizeof(PROCESS_INFORMATION));
1065 prop = msi_dup_property( package, source );
1067 return ERROR_SUCCESS;
1069 deformat_string(package,target,&deformated);
1070 len = strlenW(prop) + 2;
1072 len += strlenW(deformated);
1074 cmd = msi_alloc(sizeof(WCHAR)*len);
1080 strcatW(cmd,deformated);
1082 msi_free(deformated);
1086 TRACE("executing exe %s\n", debugstr_w(cmd));
1088 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
1089 c_collen, &si, &info);
1093 ERR("Unable to execute command %s\n", debugstr_w(cmd));
1095 return ERROR_SUCCESS;
1099 CloseHandle( info.hThread );
1101 return wait_process_handle(package, type, info.hProcess, action);
1104 static UINT HANDLE_CustomType34(MSIPACKAGE *package, LPCWSTR source,
1105 LPCWSTR target, const INT type, LPCWSTR action)
1107 LPWSTR filename, deformated;
1109 PROCESS_INFORMATION info;
1112 memset(&si,0,sizeof(STARTUPINFOW));
1114 filename = resolve_folder(package, source, FALSE, FALSE, TRUE, NULL);
1117 return ERROR_FUNCTION_FAILED;
1119 SetCurrentDirectoryW(filename);
1122 deformat_string(package,target,&deformated);
1125 return ERROR_FUNCTION_FAILED;
1127 TRACE("executing exe %s\n", debugstr_w(deformated));
1129 rc = CreateProcessW(NULL, deformated, NULL, NULL, FALSE, 0, NULL,
1130 c_collen, &si, &info);
1134 ERR("Unable to execute command %s\n", debugstr_w(deformated));
1135 msi_free(deformated);
1136 return ERROR_SUCCESS;
1138 msi_free(deformated);
1139 CloseHandle( info.hThread );
1141 return wait_process_handle(package, type, info.hProcess, action);
1144 static DWORD WINAPI ACTION_CallScript( const GUID *guid )
1146 msi_custom_action_info *info;
1148 UINT r = ERROR_FUNCTION_FAILED;
1150 info = find_action_by_guid( guid );
1153 ERR("failed to find action %s\n", debugstr_guid( guid) );
1157 TRACE("function %s, script %s\n", debugstr_w( info->target ), debugstr_w( info->source ) );
1159 hPackage = alloc_msihandle( &info->package->hdr );
1162 r = call_script( hPackage, info->type, info->source, info->target, info->action );
1163 MsiCloseHandle( hPackage );
1166 ERR("failed to create handle for %p\n", info->package );
1168 if (info->type & msidbCustomActionTypeAsync &&
1169 info->type & msidbCustomActionTypeContinue)
1170 release_custom_action_data( info );
1175 static DWORD WINAPI ScriptThread( LPVOID arg )
1180 TRACE("custom action (%x) started\n", GetCurrentThreadId() );
1182 rc = ACTION_CallScript( guid );
1184 TRACE("custom action (%x) returned %i\n", GetCurrentThreadId(), rc );
1186 MsiCloseAllHandles();
1190 static msi_custom_action_info *do_msidbCustomActionTypeScript(
1191 MSIPACKAGE *package, INT type, LPCWSTR script, LPCWSTR function, LPCWSTR action )
1193 msi_custom_action_info *info;
1195 info = msi_alloc( sizeof *info );
1199 msiobj_addref( &package->hdr );
1200 info->refs = 2; /* 1 for our caller and 1 for thread we created */
1201 info->package = package;
1203 info->target = strdupW( function );
1204 info->source = strdupW( script );
1205 info->action = strdupW( action );
1206 CoCreateGuid( &info->guid );
1208 EnterCriticalSection( &msi_custom_action_cs );
1209 list_add_tail( &msi_pending_custom_actions, &info->entry );
1210 LeaveCriticalSection( &msi_custom_action_cs );
1212 info->handle = CreateThread( NULL, 0, ScriptThread, &info->guid, 0, NULL );
1215 /* release both references */
1216 release_custom_action_data( info );
1217 release_custom_action_data( info );
1224 static UINT HANDLE_CustomType37_38(MSIPACKAGE *package, LPCWSTR source,
1225 LPCWSTR target, const INT type, LPCWSTR action)
1227 msi_custom_action_info *info;
1229 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1231 info = do_msidbCustomActionTypeScript( package, type, target, NULL, action );
1233 return wait_thread_handle( info );
1236 static UINT HANDLE_CustomType5_6(MSIPACKAGE *package, LPCWSTR source,
1237 LPCWSTR target, const INT type, LPCWSTR action)
1239 static const WCHAR query[] = {
1240 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1241 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',' ',
1242 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
1244 msi_custom_action_info *info;
1245 CHAR *buffer = NULL;
1246 WCHAR *bufferw = NULL;
1250 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1252 row = MSI_QueryGetRecord(package->db, query, source);
1254 return ERROR_FUNCTION_FAILED;
1256 r = MSI_RecordReadStream(row, 2, NULL, &sz);
1257 if (r != ERROR_SUCCESS)
1260 buffer = msi_alloc(sizeof(CHAR)*(sz+1));
1262 return ERROR_FUNCTION_FAILED;
1264 r = MSI_RecordReadStream(row, 2, buffer, &sz);
1265 if (r != ERROR_SUCCESS)
1269 bufferw = strdupAtoW(buffer);
1272 r = ERROR_FUNCTION_FAILED;
1276 info = do_msidbCustomActionTypeScript( package, type, bufferw, target, action );
1277 r = wait_thread_handle( info );
1285 static UINT HANDLE_CustomType21_22(MSIPACKAGE *package, LPCWSTR source,
1286 LPCWSTR target, const INT type, LPCWSTR action)
1288 msi_custom_action_info *info;
1291 DWORD sz, szHighWord = 0, read;
1293 WCHAR *bufferw=NULL;
1297 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1299 file = get_loaded_file(package,source);
1302 ERR("invalid file key %s\n", debugstr_w(source));
1303 return ERROR_FUNCTION_FAILED;
1306 hFile = CreateFileW(file->TargetPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
1307 if (hFile == INVALID_HANDLE_VALUE)
1308 return ERROR_FUNCTION_FAILED;
1310 sz = GetFileSize(hFile, &szHighWord);
1311 if (sz == INVALID_FILE_SIZE || szHighWord != 0)
1314 return ERROR_FUNCTION_FAILED;
1317 buffer = msi_alloc(sizeof(CHAR)*(sz+1));
1321 return ERROR_FUNCTION_FAILED;
1324 bRet = ReadFile(hFile, buffer, sz, &read, NULL);
1328 r = ERROR_FUNCTION_FAILED;
1333 bufferw = strdupAtoW(buffer);
1336 r = ERROR_FUNCTION_FAILED;
1340 info = do_msidbCustomActionTypeScript( package, type, bufferw, target, action );
1341 r = wait_thread_handle( info );
1349 static UINT HANDLE_CustomType53_54(MSIPACKAGE *package, LPCWSTR source,
1350 LPCWSTR target, const INT type, LPCWSTR action)
1352 msi_custom_action_info *info;
1355 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1357 prop = msi_dup_property(package,source);
1359 return ERROR_SUCCESS;
1361 info = do_msidbCustomActionTypeScript( package, type, prop, NULL, action );
1363 return wait_thread_handle( info );
1366 void ACTION_FinishCustomActions(const MSIPACKAGE* package)
1369 HANDLE *wait_handles;
1370 unsigned int handle_count, i;
1371 msi_custom_action_info *info, *cursor;
1373 while ((item = list_head( &package->RunningActions )))
1375 MSIRUNNINGACTION *action = LIST_ENTRY( item, MSIRUNNINGACTION, entry );
1377 list_remove( &action->entry );
1379 TRACE("waiting for %s\n", debugstr_w( action->name ) );
1380 msi_dialog_check_messages( action->handle );
1382 CloseHandle( action->handle );
1383 msi_free( action->name );
1387 EnterCriticalSection( &msi_custom_action_cs );
1389 handle_count = list_count( &msi_pending_custom_actions );
1390 wait_handles = HeapAlloc( GetProcessHeap(), 0, handle_count * sizeof(HANDLE) );
1393 LIST_FOR_EACH_ENTRY_SAFE( info, cursor, &msi_pending_custom_actions, msi_custom_action_info, entry )
1395 if (info->package == package )
1397 if (DuplicateHandle(GetCurrentProcess(), info->handle, GetCurrentProcess(), &wait_handles[handle_count], SYNCHRONIZE, FALSE, 0))
1402 LeaveCriticalSection( &msi_custom_action_cs );
1404 for (i = 0; i < handle_count; i++)
1406 msi_dialog_check_messages( wait_handles[i] );
1407 CloseHandle( wait_handles[i] );
1410 HeapFree( GetProcessHeap(), 0, wait_handles );
1413 typedef struct _msi_custom_remote_impl {
1414 const IWineMsiRemoteCustomActionVtbl *lpVtbl;
1416 } msi_custom_remote_impl;
1418 static inline msi_custom_remote_impl* mcr_from_IWineMsiRemoteCustomAction( IWineMsiRemoteCustomAction* iface )
1420 return (msi_custom_remote_impl*) iface;
1423 static HRESULT WINAPI mcr_QueryInterface( IWineMsiRemoteCustomAction *iface,
1424 REFIID riid,LPVOID *ppobj)
1426 if( IsEqualCLSID( riid, &IID_IUnknown ) ||
1427 IsEqualCLSID( riid, &IID_IWineMsiRemoteCustomAction ) )
1429 IUnknown_AddRef( iface );
1434 return E_NOINTERFACE;
1437 static ULONG WINAPI mcr_AddRef( IWineMsiRemoteCustomAction *iface )
1439 msi_custom_remote_impl* This = mcr_from_IWineMsiRemoteCustomAction( iface );
1441 return InterlockedIncrement( &This->refs );
1444 static ULONG WINAPI mcr_Release( IWineMsiRemoteCustomAction *iface )
1446 msi_custom_remote_impl* This = mcr_from_IWineMsiRemoteCustomAction( iface );
1449 r = InterlockedDecrement( &This->refs );
1455 static HRESULT WINAPI mcr_GetActionInfo( IWineMsiRemoteCustomAction *iface, LPCGUID custom_action_guid,
1456 INT *type, MSIHANDLE *handle, BSTR *dll, BSTR *func, IWineMsiRemotePackage **remote_package )
1458 msi_custom_action_info *info;
1460 info = find_action_by_guid( custom_action_guid );
1465 *handle = alloc_msihandle( &info->package->hdr );
1466 *dll = SysAllocString( info->source );
1467 *func = SysAllocString( info->target );
1469 release_custom_action_data( info );
1470 return create_msi_remote_package( NULL, (LPVOID *)remote_package );
1473 static const IWineMsiRemoteCustomActionVtbl msi_custom_remote_vtbl =
1481 HRESULT create_msi_custom_remote( IUnknown *pOuter, LPVOID *ppObj )
1483 msi_custom_remote_impl* This;
1485 This = msi_alloc( sizeof *This );
1487 return E_OUTOFMEMORY;
1489 This->lpVtbl = &msi_custom_remote_vtbl;