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[] = {'[','%','s',';','%','s',';','%','s',']','%','s',0};
151 return strdupW(action);
153 len = lstrlenW(action) + lstrlenW(actiondata) +
154 lstrlenW(usersid) + lstrlenW(prodcode) + 5;
155 deferred = msi_alloc(len * sizeof(WCHAR));
157 sprintfW(deferred, format, actiondata, usersid, prodcode, action);
161 static void set_deferred_action_props(MSIPACKAGE *package, LPWSTR deferred_data)
163 LPWSTR end, beg = deferred_data + 1;
165 end = strchrW(beg, ';');
167 MSI_SetPropertyW(package, szActionData, beg);
170 end = strchrW(beg, ';');
172 MSI_SetPropertyW(package, UserSID, beg);
175 end = strchrW(beg, ']');
177 MSI_SetPropertyW(package, ProdCode, beg);
180 UINT ACTION_CustomAction(MSIPACKAGE *package, LPCWSTR action, UINT script, BOOL execute)
182 UINT rc = ERROR_SUCCESS;
184 static const WCHAR ExecSeqQuery[] =
185 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
186 '`','C','u','s','t','o' ,'m','A','c','t','i','o','n','`',
187 ' ','W','H','E','R','E',' ','`','A','c','t','i' ,'o','n','`',' ',
188 '=',' ','\'','%','s','\'',0};
190 LPCWSTR source, target;
191 LPWSTR ptr, deferred_data = NULL;
192 LPWSTR action_copy = strdupW(action);
193 WCHAR *deformated=NULL;
195 /* deferred action: [properties]Action */
196 if ((ptr = strchrW(action_copy, ']')))
198 deferred_data = action_copy;
202 row = MSI_QueryGetRecord( package->db, ExecSeqQuery, action );
205 msi_free(action_copy);
206 return ERROR_CALL_NOT_IMPLEMENTED;
209 type = MSI_RecordGetInteger(row,2);
211 source = MSI_RecordGetString(row,3);
212 target = MSI_RecordGetString(row,4);
214 TRACE("Handling custom action %s (%x %s %s)\n",debugstr_w(action),type,
215 debugstr_w(source), debugstr_w(target));
217 /* handle some of the deferred actions */
218 if (type & msidbCustomActionTypeTSAware)
219 FIXME("msidbCustomActionTypeTSAware not handled\n");
221 if (type & msidbCustomActionTypeInScript)
223 if (type & msidbCustomActionTypeNoImpersonate)
224 FIXME("msidbCustomActionTypeNoImpersonate not handled\n");
226 if (type & msidbCustomActionTypeRollback)
228 FIXME("Rollback only action... rollbacks not supported yet\n");
229 schedule_action(package, ROLLBACK_SCRIPT, action);
235 LPWSTR actiondata = msi_dup_property(package, action);
236 LPWSTR usersid = msi_dup_property(package, UserSID);
237 LPWSTR prodcode = msi_dup_property(package, ProdCode);
238 LPWSTR deferred = msi_get_deferred_action(action, actiondata, usersid, prodcode);
240 if (type & msidbCustomActionTypeCommit)
242 TRACE("Deferring Commit Action!\n");
243 schedule_action(package, COMMIT_SCRIPT, deferred);
247 TRACE("Deferring Action!\n");
248 schedule_action(package, INSTALL_SCRIPT, deferred);
252 msi_free(actiondata);
260 static const WCHAR szBlank[] = {0};
262 LPWSTR actiondata = msi_dup_property( package, action );
267 package->scheduled_action_running = TRUE;
270 package->commit_action_running = TRUE;
272 case ROLLBACK_SCRIPT:
273 package->rollback_action_running = TRUE;
280 set_deferred_action_props(package, deferred_data);
282 MSI_SetPropertyW(package,szActionData,actiondata);
284 MSI_SetPropertyW(package,szActionData,szBlank);
286 msi_free(actiondata);
289 else if (!check_execution_scheduling_options(package,action,type))
295 switch (type & CUSTOM_ACTION_TYPE_MASK)
297 case 1: /* DLL file stored in a Binary table stream */
298 rc = HANDLE_CustomType1(package,source,target,type,action);
300 case 2: /* EXE file stored in a Binary table stream */
301 rc = HANDLE_CustomType2(package,source,target,type,action);
303 case 18: /*EXE file installed with package */
304 rc = HANDLE_CustomType18(package,source,target,type,action);
306 case 19: /* Error that halts install */
307 rc = HANDLE_CustomType19(package,source,target,type,action);
310 rc = HANDLE_CustomType17(package,source,target,type,action);
312 case 23: /* installs another package in the source tree */
313 deformat_string(package,target,&deformated);
314 rc = HANDLE_CustomType23(package,source,deformated,type,action);
316 case 50: /*EXE file specified by a property value */
317 rc = HANDLE_CustomType50(package,source,target,type,action);
319 case 34: /*EXE to be run in specified directory */
320 rc = HANDLE_CustomType34(package,source,target,type,action);
322 case 35: /* Directory set with formatted text. */
323 deformat_string(package,target,&deformated);
324 MSI_SetTargetPathW(package, source, deformated);
325 msi_free(deformated);
327 case 51: /* Property set with formatted text. */
328 deformat_string(package,target,&deformated);
329 rc = MSI_SetPropertyW(package,source,deformated);
330 msi_free(deformated);
332 case 37: /* JScript/VBScript text stored in target column. */
334 rc = HANDLE_CustomType37_38(package,source,target,type,action);
337 case 6: /* JScript/VBScript file stored in a Binary table stream. */
338 rc = HANDLE_CustomType5_6(package,source,target,type,action);
340 case 21: /* JScript/VBScript file installed with the product. */
342 rc = HANDLE_CustomType21_22(package,source,target,type,action);
344 case 53: /* JScript/VBScript text specified by a property value. */
346 rc = HANDLE_CustomType53_54(package,source,target,type,action);
349 FIXME("UNHANDLED ACTION TYPE %i (%s %s)\n",
350 type & CUSTOM_ACTION_TYPE_MASK, debugstr_w(source),
355 package->scheduled_action_running = FALSE;
356 package->commit_action_running = FALSE;
357 package->rollback_action_running = FALSE;
358 msi_free(action_copy);
359 msiobj_release(&row->hdr);
364 static UINT store_binary_to_temp(MSIPACKAGE *package, LPCWSTR source,
367 static const WCHAR query[] = {
368 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
369 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',' ',
370 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
374 static const WCHAR f1[] = {'m','s','i',0};
379 if (MSI_GetPropertyW(package, cszTempFolder, fmt, &sz) != ERROR_SUCCESS)
380 GetTempPathW(MAX_PATH, fmt);
382 if (GetTempFileNameW(fmt, f1, 0, tmp_file) == 0)
384 TRACE("Unable to create file\n");
385 return ERROR_FUNCTION_FAILED;
387 track_tempfile(package, tmp_file);
389 row = MSI_QueryGetRecord(package->db, query, source);
391 return ERROR_FUNCTION_FAILED;
393 /* write out the file */
394 file = CreateFileW(tmp_file, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
395 FILE_ATTRIBUTE_NORMAL, NULL);
396 if (file == INVALID_HANDLE_VALUE)
397 r = ERROR_FUNCTION_FAILED;
404 r = MSI_RecordReadStream(row, 2, buffer, &sz);
405 if (r != ERROR_SUCCESS)
407 ERR("Failed to get stream\n");
410 WriteFile(file, buffer, sz, &write, NULL);
411 } while (sz == sizeof buffer);
415 msiobj_release(&row->hdr);
420 static void file_running_action(MSIPACKAGE* package, HANDLE Handle,
421 BOOL process, LPCWSTR name)
423 MSIRUNNINGACTION *action;
425 action = msi_alloc( sizeof(MSIRUNNINGACTION) );
427 action->handle = Handle;
428 action->process = process;
429 action->name = strdupW(name);
431 list_add_tail( &package->RunningActions, &action->entry );
434 static UINT custom_get_process_return( HANDLE process )
438 GetExitCodeProcess( process, &rc );
440 return ERROR_FUNCTION_FAILED;
441 return ERROR_SUCCESS;
444 static UINT custom_get_thread_return( MSIPACKAGE *package, HANDLE thread )
448 GetExitCodeThread( thread, &rc );
452 case ERROR_FUNCTION_NOT_CALLED:
454 case ERROR_INSTALL_USEREXIT:
455 case ERROR_INSTALL_FAILURE:
457 case ERROR_NO_MORE_ITEMS:
458 return ERROR_SUCCESS;
459 case ERROR_INSTALL_SUSPEND:
460 ACTION_ForceReboot( package );
461 return ERROR_SUCCESS;
463 ERR("Invalid Return Code %d\n",rc);
464 return ERROR_INSTALL_FAILURE;
468 static UINT wait_process_handle(MSIPACKAGE* package, UINT type,
469 HANDLE ProcessHandle, LPCWSTR name)
471 UINT rc = ERROR_SUCCESS;
473 if (!(type & msidbCustomActionTypeAsync))
475 TRACE("waiting for %s\n", debugstr_w(name));
477 msi_dialog_check_messages(ProcessHandle);
479 if (!(type & msidbCustomActionTypeContinue))
480 rc = custom_get_process_return(ProcessHandle);
482 CloseHandle(ProcessHandle);
486 TRACE("%s running in background\n", debugstr_w(name));
488 if (!(type & msidbCustomActionTypeContinue))
489 file_running_action(package, ProcessHandle, TRUE, name);
491 CloseHandle(ProcessHandle);
497 typedef struct _msi_custom_action_info {
507 } msi_custom_action_info;
509 static void release_custom_action_data( msi_custom_action_info *info )
511 EnterCriticalSection( &msi_custom_action_cs );
515 list_remove( &info->entry );
517 CloseHandle( info->handle );
518 msi_free( info->action );
519 msi_free( info->source );
520 msi_free( info->target );
521 msiobj_release( &info->package->hdr );
525 LeaveCriticalSection( &msi_custom_action_cs );
528 /* must be called inside msi_custom_action_cs if info is in the pending custom actions list */
529 static void addref_custom_action_data( msi_custom_action_info *info )
534 static UINT wait_thread_handle( msi_custom_action_info *info )
536 UINT rc = ERROR_SUCCESS;
538 if (!(info->type & msidbCustomActionTypeAsync))
540 TRACE("waiting for %s\n", debugstr_w( info->action ));
542 msi_dialog_check_messages( info->handle );
544 if (!(info->type & msidbCustomActionTypeContinue))
545 rc = custom_get_thread_return( info->package, info->handle );
547 release_custom_action_data( info );
551 TRACE("%s running in background\n", debugstr_w( info->action ));
557 static msi_custom_action_info *find_action_by_guid( const GUID *guid )
559 msi_custom_action_info *info;
562 EnterCriticalSection( &msi_custom_action_cs );
564 LIST_FOR_EACH_ENTRY( info, &msi_pending_custom_actions, msi_custom_action_info, entry )
566 if (IsEqualGUID( &info->guid, guid ))
568 addref_custom_action_data( info );
574 LeaveCriticalSection( &msi_custom_action_cs );
582 static void handle_msi_break( LPCWSTR target )
587 static const WCHAR MsiBreak[] = { 'M','s','i','B','r','e','a','k',0 };
588 static const WCHAR WindowsInstaller[] = {
589 'W','i','n','d','o','w','s',' ','I','n','s','t','a','l','l','e','r',0
592 static const WCHAR format[] = {
593 'T','o',' ','d','e','b','u','g',' ','y','o','u','r',' ',
594 'c','u','s','t','o','m',' ','a','c','t','i','o','n',',',' ',
595 'a','t','t','a','c','h',' ','y','o','u','r',' ','d','e','b','u','g','g','e','r',' ',
596 't','o',' ','p','r','o','c','e','s','s',' ','%','i',' ','(','0','x','%','X',')',' ',
597 'a','n','d',' ','p','r','e','s','s',' ','O','K',0
600 if( !GetEnvironmentVariableW( MsiBreak, val, MAX_PATH ))
603 if( lstrcmpiW( val, target ))
606 msg = msi_alloc( (lstrlenW(format) + 10) * sizeof(WCHAR) );
610 wsprintfW( msg, format, GetCurrentProcessId(), GetCurrentProcessId());
611 MessageBoxW( NULL, msg, WindowsInstaller, MB_OK);
616 static UINT get_action_info( const GUID *guid, INT *type, MSIHANDLE *handle,
617 BSTR *dll, BSTR *funcname,
618 IWineMsiRemotePackage **package )
620 IClassFactory *cf = NULL;
621 IWineMsiRemoteCustomAction *rca = NULL;
624 r = DllGetClassObject( &CLSID_IWineMsiRemoteCustomAction,
625 &IID_IClassFactory, (LPVOID *)&cf );
628 ERR("failed to get IClassFactory interface\n");
629 return ERROR_FUNCTION_FAILED;
632 r = IClassFactory_CreateInstance( cf, NULL, &IID_IWineMsiRemoteCustomAction, (LPVOID *)&rca );
635 ERR("failed to get IWineMsiRemoteCustomAction interface\n");
636 return ERROR_FUNCTION_FAILED;
639 r = IWineMsiRemoteCustomAction_GetActionInfo( rca, guid, type, handle, dll, funcname, package );
640 IWineMsiRemoteCustomAction_Release( rca );
643 ERR("GetActionInfo failed\n");
644 return ERROR_FUNCTION_FAILED;
647 return ERROR_SUCCESS;
650 static DWORD WINAPI ACTION_CallDllFunction( const GUID *guid )
652 MsiCustomActionEntryPoint fn;
653 MSIHANDLE hPackage, handle;
656 UINT r = ERROR_FUNCTION_FAILED;
657 BSTR dll = NULL, function = NULL;
659 IWineMsiRemotePackage *remote_package = NULL;
661 TRACE("%s\n", debugstr_guid( guid ));
663 r = get_action_info( guid, &type, &handle, &dll, &function, &remote_package );
664 if (r != ERROR_SUCCESS)
667 hModule = LoadLibraryW( dll );
670 ERR("failed to load dll %s\n", debugstr_w( dll ) );
674 proc = strdupWtoA( function );
675 fn = (MsiCustomActionEntryPoint) GetProcAddress( hModule, proc );
679 hPackage = alloc_msi_remote_handle( (IUnknown *)remote_package );
682 IWineMsiRemotePackage_SetMsiHandle( remote_package, handle );
683 TRACE("calling %s\n", debugstr_w( function ) );
684 handle_msi_break( function );
692 ERR("Custom action (%s:%s) caused a page fault: %08x\n",
693 debugstr_w(dll), debugstr_w(function), GetExceptionCode());
698 MsiCloseHandle( hPackage );
701 ERR("failed to create handle for %p\n", remote_package );
704 ERR("GetProcAddress(%s) failed\n", debugstr_w( function ) );
706 FreeLibrary(hModule);
708 IWineMsiRemotePackage_Release( remote_package );
709 SysFreeString( dll );
710 SysFreeString( function );
711 MsiCloseHandle( handle );
716 static DWORD WINAPI DllThread( LPVOID arg )
721 TRACE("custom action (%x) started\n", GetCurrentThreadId() );
723 rc = ACTION_CallDllFunction( guid );
725 TRACE("custom action (%x) returned %i\n", GetCurrentThreadId(), rc );
727 MsiCloseAllHandles();
731 static DWORD WINAPI ACTION_CAInstallPackage(const GUID *guid)
733 msi_custom_action_info *info;
734 UINT r = ERROR_FUNCTION_FAILED;
735 INSTALLUILEVEL old_level;
737 info = find_action_by_guid(guid);
740 ERR("failed to find action %s\n", debugstr_guid(guid));
744 old_level = MsiSetInternalUI(INSTALLUILEVEL_BASIC, NULL);
745 r = MsiInstallProductW(info->source, info->target);
746 MsiSetInternalUI(old_level, NULL);
748 release_custom_action_data(info);
753 static DWORD WINAPI ConcurrentInstallThread(LPVOID arg)
758 TRACE("concurrent installation (%x) started\n", GetCurrentThreadId());
760 rc = ACTION_CAInstallPackage(guid);
762 TRACE("concurrent installation (%x) returned %i\n", GetCurrentThreadId(), rc);
764 MsiCloseAllHandles();
768 static msi_custom_action_info *do_msidbCustomActionTypeDll(
769 MSIPACKAGE *package, INT type, LPCWSTR source, LPCWSTR target, LPCWSTR action )
771 msi_custom_action_info *info;
773 info = msi_alloc( sizeof *info );
777 msiobj_addref( &package->hdr );
778 info->refs = 2; /* 1 for our caller and 1 for thread we created */
779 info->package = package;
781 info->target = strdupW( target );
782 info->source = strdupW( source );
783 info->action = strdupW( action );
784 CoCreateGuid( &info->guid );
786 EnterCriticalSection( &msi_custom_action_cs );
787 list_add_tail( &msi_pending_custom_actions, &info->entry );
788 LeaveCriticalSection( &msi_custom_action_cs );
790 info->handle = CreateThread( NULL, 0, DllThread, &info->guid, 0, NULL );
793 /* release both references */
794 release_custom_action_data( info );
795 release_custom_action_data( info );
802 static msi_custom_action_info *do_msidbCAConcurrentInstall(
803 MSIPACKAGE *package, INT type, LPCWSTR source, LPCWSTR target, LPCWSTR action)
805 msi_custom_action_info *info;
807 info = msi_alloc( sizeof *info );
811 msiobj_addref( &package->hdr );
812 info->refs = 2; /* 1 for our caller and 1 for thread we created */
813 info->package = package;
815 info->target = strdupW( target );
816 info->source = strdupW( source );
817 info->action = strdupW( action );
818 CoCreateGuid( &info->guid );
820 EnterCriticalSection( &msi_custom_action_cs );
821 list_add_tail( &msi_pending_custom_actions, &info->entry );
822 LeaveCriticalSection( &msi_custom_action_cs );
824 info->handle = CreateThread( NULL, 0, ConcurrentInstallThread, &info->guid, 0, NULL );
827 /* release both references */
828 release_custom_action_data( info );
829 release_custom_action_data( info );
836 static UINT HANDLE_CustomType23(MSIPACKAGE *package, LPCWSTR source,
837 LPCWSTR target, const INT type, LPCWSTR action)
839 msi_custom_action_info *info;
840 WCHAR package_path[MAX_PATH];
843 static const WCHAR backslash[] = {'\\',0};
845 MSI_GetPropertyW(package, cszSourceDir, package_path, &size);
846 lstrcatW(package_path, backslash);
847 lstrcatW(package_path, source);
849 if (GetFileAttributesW(package_path) == INVALID_FILE_ATTRIBUTES)
851 ERR("Source package does not exist: %s\n", debugstr_w(package_path));
852 return ERROR_FUNCTION_FAILED;
855 TRACE("Installing package %s concurrently\n", debugstr_w(package_path));
857 info = do_msidbCAConcurrentInstall(package, type, package_path, target, action);
859 return wait_thread_handle(info);
862 static UINT HANDLE_CustomType1(MSIPACKAGE *package, LPCWSTR source,
863 LPCWSTR target, const INT type, LPCWSTR action)
865 msi_custom_action_info *info;
866 WCHAR tmp_file[MAX_PATH];
869 r = store_binary_to_temp(package, source, tmp_file);
870 if (r != ERROR_SUCCESS)
873 TRACE("Calling function %s from %s\n",debugstr_w(target),
874 debugstr_w(tmp_file));
876 if (!strchrW(tmp_file,'.'))
878 static const WCHAR dot[]={'.',0};
879 strcatW(tmp_file,dot);
882 info = do_msidbCustomActionTypeDll( package, type, tmp_file, target, action );
884 return wait_thread_handle( info );
887 static UINT HANDLE_CustomType2(MSIPACKAGE *package, LPCWSTR source,
888 LPCWSTR target, const INT type, LPCWSTR action)
890 WCHAR tmp_file[MAX_PATH];
892 PROCESS_INFORMATION info;
895 WCHAR *deformated = NULL;
897 static const WCHAR spc[] = {' ',0};
900 memset(&si,0,sizeof(STARTUPINFOW));
902 r = store_binary_to_temp(package, source, tmp_file);
903 if (r != ERROR_SUCCESS)
906 deformat_string(package,target,&deformated);
908 len = strlenW(tmp_file)+2;
911 len += strlenW(deformated);
913 cmd = msi_alloc(sizeof(WCHAR)*len);
915 strcpyW(cmd,tmp_file);
919 strcatW(cmd,deformated);
921 msi_free(deformated);
924 TRACE("executing exe %s\n", debugstr_w(cmd));
926 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
927 c_collen, &si, &info);
932 ERR("Unable to execute command %s\n", debugstr_w(cmd));
933 return ERROR_SUCCESS;
935 CloseHandle( info.hThread );
937 r = wait_process_handle(package, type, info.hProcess, action);
942 static UINT HANDLE_CustomType17(MSIPACKAGE *package, LPCWSTR source,
943 LPCWSTR target, const INT type, LPCWSTR action)
945 msi_custom_action_info *info;
948 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
950 file = get_loaded_file( package, source );
953 ERR("invalid file key %s\n", debugstr_w( source ));
954 return ERROR_FUNCTION_FAILED;
957 info = do_msidbCustomActionTypeDll( package, type, file->TargetPath, target, action );
959 return wait_thread_handle( info );
962 static UINT HANDLE_CustomType18(MSIPACKAGE *package, LPCWSTR source,
963 LPCWSTR target, const INT type, LPCWSTR action)
966 PROCESS_INFORMATION info;
971 static const WCHAR spc[] = {' ',0};
974 memset(&si,0,sizeof(STARTUPINFOW));
976 file = get_loaded_file(package,source);
978 return ERROR_FUNCTION_FAILED;
980 len = lstrlenW( file->TargetPath );
982 deformat_string(package,target,&deformated);
984 len += strlenW(deformated);
987 cmd = msi_alloc(len * sizeof(WCHAR));
989 lstrcpyW( cmd, file->TargetPath);
993 strcatW(cmd, deformated);
995 msi_free(deformated);
998 TRACE("executing exe %s\n", debugstr_w(cmd));
1000 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
1001 c_collen, &si, &info);
1005 ERR("Unable to execute command %s\n", debugstr_w(cmd));
1007 return ERROR_SUCCESS;
1010 CloseHandle( info.hThread );
1012 return wait_process_handle(package, type, info.hProcess, action);
1015 static UINT HANDLE_CustomType19(MSIPACKAGE *package, LPCWSTR source,
1016 LPCWSTR target, const INT type, LPCWSTR action)
1018 static const WCHAR query[] = {
1019 'S','E','L','E','C','T',' ','`','M','e','s','s','a','g','e','`',' ',
1020 'F','R','O','M',' ','`','E','r','r','o','r','`',' ',
1021 'W','H','E','R','E',' ','`','E','r','r','o','r','`',' ','=',' ',
1025 LPWSTR deformated = NULL;
1027 deformat_string( package, target, &deformated );
1029 /* first try treat the error as a number */
1030 row = MSI_QueryGetRecord( package->db, query, deformated );
1033 LPCWSTR error = MSI_RecordGetString( row, 1 );
1034 MessageBoxW( NULL, error, NULL, MB_OK );
1035 msiobj_release( &row->hdr );
1038 MessageBoxW( NULL, deformated, NULL, MB_OK );
1040 msi_free( deformated );
1042 return ERROR_FUNCTION_FAILED;
1045 static UINT HANDLE_CustomType50(MSIPACKAGE *package, LPCWSTR source,
1046 LPCWSTR target, const INT type, LPCWSTR action)
1049 PROCESS_INFORMATION info;
1055 static const WCHAR spc[] = {' ',0};
1057 memset(&si,0,sizeof(STARTUPINFOW));
1058 memset(&info,0,sizeof(PROCESS_INFORMATION));
1060 prop = msi_dup_property( package, source );
1062 return ERROR_SUCCESS;
1064 deformat_string(package,target,&deformated);
1065 len = strlenW(prop) + 2;
1067 len += strlenW(deformated);
1069 cmd = msi_alloc(sizeof(WCHAR)*len);
1075 strcatW(cmd,deformated);
1077 msi_free(deformated);
1081 TRACE("executing exe %s\n", debugstr_w(cmd));
1083 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
1084 c_collen, &si, &info);
1088 ERR("Unable to execute command %s\n", debugstr_w(cmd));
1090 return ERROR_SUCCESS;
1094 CloseHandle( info.hThread );
1096 return wait_process_handle(package, type, info.hProcess, action);
1099 static UINT HANDLE_CustomType34(MSIPACKAGE *package, LPCWSTR source,
1100 LPCWSTR target, const INT type, LPCWSTR action)
1102 LPWSTR filename, deformated;
1104 PROCESS_INFORMATION info;
1107 memset(&si,0,sizeof(STARTUPINFOW));
1109 filename = resolve_folder(package, source, FALSE, FALSE, TRUE, NULL);
1112 return ERROR_FUNCTION_FAILED;
1114 SetCurrentDirectoryW(filename);
1117 deformat_string(package,target,&deformated);
1120 return ERROR_FUNCTION_FAILED;
1122 TRACE("executing exe %s\n", debugstr_w(deformated));
1124 rc = CreateProcessW(NULL, deformated, NULL, NULL, FALSE, 0, NULL,
1125 c_collen, &si, &info);
1129 ERR("Unable to execute command %s\n", debugstr_w(deformated));
1130 msi_free(deformated);
1131 return ERROR_SUCCESS;
1133 msi_free(deformated);
1134 CloseHandle( info.hThread );
1136 return wait_process_handle(package, type, info.hProcess, action);
1139 static DWORD WINAPI ACTION_CallScript( const GUID *guid )
1141 msi_custom_action_info *info;
1143 UINT r = ERROR_FUNCTION_FAILED;
1145 info = find_action_by_guid( guid );
1148 ERR("failed to find action %s\n", debugstr_guid( guid) );
1152 TRACE("function %s, script %s\n", debugstr_w( info->target ), debugstr_w( info->source ) );
1154 hPackage = alloc_msihandle( &info->package->hdr );
1157 r = call_script( hPackage, info->type, info->source, info->target, info->action );
1158 MsiCloseHandle( hPackage );
1161 ERR("failed to create handle for %p\n", info->package );
1163 if (info->type & msidbCustomActionTypeAsync &&
1164 info->type & msidbCustomActionTypeContinue)
1165 release_custom_action_data( info );
1170 static DWORD WINAPI ScriptThread( LPVOID arg )
1175 TRACE("custom action (%x) started\n", GetCurrentThreadId() );
1177 rc = ACTION_CallScript( guid );
1179 TRACE("custom action (%x) returned %i\n", GetCurrentThreadId(), rc );
1181 MsiCloseAllHandles();
1185 static msi_custom_action_info *do_msidbCustomActionTypeScript(
1186 MSIPACKAGE *package, INT type, LPCWSTR script, LPCWSTR function, LPCWSTR action )
1188 msi_custom_action_info *info;
1190 info = msi_alloc( sizeof *info );
1194 msiobj_addref( &package->hdr );
1195 info->refs = 2; /* 1 for our caller and 1 for thread we created */
1196 info->package = package;
1198 info->target = strdupW( function );
1199 info->source = strdupW( script );
1200 info->action = strdupW( action );
1201 CoCreateGuid( &info->guid );
1203 EnterCriticalSection( &msi_custom_action_cs );
1204 list_add_tail( &msi_pending_custom_actions, &info->entry );
1205 LeaveCriticalSection( &msi_custom_action_cs );
1207 info->handle = CreateThread( NULL, 0, ScriptThread, &info->guid, 0, NULL );
1210 /* release both references */
1211 release_custom_action_data( info );
1212 release_custom_action_data( info );
1219 static UINT HANDLE_CustomType37_38(MSIPACKAGE *package, LPCWSTR source,
1220 LPCWSTR target, const INT type, LPCWSTR action)
1222 msi_custom_action_info *info;
1224 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1226 info = do_msidbCustomActionTypeScript( package, type, target, NULL, action );
1228 return wait_thread_handle( info );
1231 static UINT HANDLE_CustomType5_6(MSIPACKAGE *package, LPCWSTR source,
1232 LPCWSTR target, const INT type, LPCWSTR action)
1234 static const WCHAR query[] = {
1235 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1236 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',' ',
1237 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
1239 msi_custom_action_info *info;
1240 CHAR *buffer = NULL;
1241 WCHAR *bufferw = NULL;
1245 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1247 row = MSI_QueryGetRecord(package->db, query, source);
1249 return ERROR_FUNCTION_FAILED;
1251 r = MSI_RecordReadStream(row, 2, NULL, &sz);
1252 if (r != ERROR_SUCCESS)
1255 buffer = msi_alloc(sizeof(CHAR)*(sz+1));
1257 return ERROR_FUNCTION_FAILED;
1259 r = MSI_RecordReadStream(row, 2, buffer, &sz);
1260 if (r != ERROR_SUCCESS)
1264 bufferw = strdupAtoW(buffer);
1267 r = ERROR_FUNCTION_FAILED;
1271 info = do_msidbCustomActionTypeScript( package, type, bufferw, target, action );
1272 r = wait_thread_handle( info );
1280 static UINT HANDLE_CustomType21_22(MSIPACKAGE *package, LPCWSTR source,
1281 LPCWSTR target, const INT type, LPCWSTR action)
1283 msi_custom_action_info *info;
1286 DWORD sz, szHighWord = 0, read;
1288 WCHAR *bufferw=NULL;
1292 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1294 file = get_loaded_file(package,source);
1297 ERR("invalid file key %s\n", debugstr_w(source));
1298 return ERROR_FUNCTION_FAILED;
1301 hFile = CreateFileW(file->TargetPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
1302 if (hFile == INVALID_HANDLE_VALUE)
1303 return ERROR_FUNCTION_FAILED;
1305 sz = GetFileSize(hFile, &szHighWord);
1306 if (sz == INVALID_FILE_SIZE || szHighWord != 0)
1309 return ERROR_FUNCTION_FAILED;
1312 buffer = msi_alloc(sizeof(CHAR)*(sz+1));
1316 return ERROR_FUNCTION_FAILED;
1319 bRet = ReadFile(hFile, buffer, sz, &read, NULL);
1323 r = ERROR_FUNCTION_FAILED;
1328 bufferw = strdupAtoW(buffer);
1331 r = ERROR_FUNCTION_FAILED;
1335 info = do_msidbCustomActionTypeScript( package, type, bufferw, target, action );
1336 r = wait_thread_handle( info );
1344 static UINT HANDLE_CustomType53_54(MSIPACKAGE *package, LPCWSTR source,
1345 LPCWSTR target, const INT type, LPCWSTR action)
1347 msi_custom_action_info *info;
1350 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1352 prop = msi_dup_property(package,source);
1354 return ERROR_SUCCESS;
1356 info = do_msidbCustomActionTypeScript( package, type, prop, NULL, action );
1358 return wait_thread_handle( info );
1361 void ACTION_FinishCustomActions(const MSIPACKAGE* package)
1364 HANDLE *wait_handles;
1365 unsigned int handle_count, i;
1366 msi_custom_action_info *info, *cursor;
1368 while ((item = list_head( &package->RunningActions )))
1370 MSIRUNNINGACTION *action = LIST_ENTRY( item, MSIRUNNINGACTION, entry );
1372 list_remove( &action->entry );
1374 TRACE("waiting for %s\n", debugstr_w( action->name ) );
1375 msi_dialog_check_messages( action->handle );
1377 CloseHandle( action->handle );
1378 msi_free( action->name );
1382 EnterCriticalSection( &msi_custom_action_cs );
1384 handle_count = list_count( &msi_pending_custom_actions );
1385 wait_handles = HeapAlloc( GetProcessHeap(), 0, handle_count * sizeof(HANDLE) );
1388 LIST_FOR_EACH_ENTRY_SAFE( info, cursor, &msi_pending_custom_actions, msi_custom_action_info, entry )
1390 if (info->package == package )
1392 if (DuplicateHandle(GetCurrentProcess(), info->handle, GetCurrentProcess(), &wait_handles[handle_count], SYNCHRONIZE, FALSE, 0))
1397 LeaveCriticalSection( &msi_custom_action_cs );
1399 for (i = 0; i < handle_count; i++)
1401 msi_dialog_check_messages( wait_handles[i] );
1402 CloseHandle( wait_handles[i] );
1405 HeapFree( GetProcessHeap(), 0, wait_handles );
1408 typedef struct _msi_custom_remote_impl {
1409 const IWineMsiRemoteCustomActionVtbl *lpVtbl;
1411 } msi_custom_remote_impl;
1413 static inline msi_custom_remote_impl* mcr_from_IWineMsiRemoteCustomAction( IWineMsiRemoteCustomAction* iface )
1415 return (msi_custom_remote_impl*) iface;
1418 static HRESULT WINAPI mcr_QueryInterface( IWineMsiRemoteCustomAction *iface,
1419 REFIID riid,LPVOID *ppobj)
1421 if( IsEqualCLSID( riid, &IID_IUnknown ) ||
1422 IsEqualCLSID( riid, &IID_IWineMsiRemoteCustomAction ) )
1424 IUnknown_AddRef( iface );
1429 return E_NOINTERFACE;
1432 static ULONG WINAPI mcr_AddRef( IWineMsiRemoteCustomAction *iface )
1434 msi_custom_remote_impl* This = mcr_from_IWineMsiRemoteCustomAction( iface );
1436 return InterlockedIncrement( &This->refs );
1439 static ULONG WINAPI mcr_Release( IWineMsiRemoteCustomAction *iface )
1441 msi_custom_remote_impl* This = mcr_from_IWineMsiRemoteCustomAction( iface );
1444 r = InterlockedDecrement( &This->refs );
1450 static HRESULT WINAPI mcr_GetActionInfo( IWineMsiRemoteCustomAction *iface, LPCGUID custom_action_guid,
1451 INT *type, MSIHANDLE *handle, BSTR *dll, BSTR *func, IWineMsiRemotePackage **remote_package )
1453 msi_custom_action_info *info;
1455 info = find_action_by_guid( custom_action_guid );
1460 *handle = alloc_msihandle( &info->package->hdr );
1461 *dll = SysAllocString( info->source );
1462 *func = SysAllocString( info->target );
1464 release_custom_action_data( info );
1465 return create_msi_remote_package( NULL, (LPVOID *)remote_package );
1468 static const IWineMsiRemoteCustomActionVtbl msi_custom_remote_vtbl =
1476 HRESULT create_msi_custom_remote( IUnknown *pOuter, LPVOID *ppObj )
1478 msi_custom_remote_impl* This;
1480 This = msi_alloc( sizeof *This );
1482 return E_OUTOFMEMORY;
1484 This->lpVtbl = &msi_custom_remote_vtbl;