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
22 #include "wine/port.h"
36 #include "msiserver.h"
37 #include "wine/debug.h"
38 #include "wine/unicode.h"
39 #include "wine/exception.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(msi);
43 #define CUSTOM_ACTION_TYPE_MASK 0x3F
44 static const WCHAR c_collen[] = {'C',':','\\',0};
45 static const WCHAR cszTempFolder[]= {'T','e','m','p','F','o','l','d','e','r',0};
47 typedef struct tagMSIRUNNINGACTION
55 static UINT HANDLE_CustomType1(MSIPACKAGE *package, LPCWSTR source,
56 LPCWSTR target, const INT type, LPCWSTR action);
57 static UINT HANDLE_CustomType2(MSIPACKAGE *package, LPCWSTR source,
58 LPCWSTR target, const INT type, LPCWSTR action);
59 static UINT HANDLE_CustomType17(MSIPACKAGE *package, LPCWSTR source,
60 LPCWSTR target, const INT type, LPCWSTR action);
61 static UINT HANDLE_CustomType18(MSIPACKAGE *package, LPCWSTR source,
62 LPCWSTR target, const INT type, LPCWSTR action);
63 static UINT HANDLE_CustomType19(MSIPACKAGE *package, LPCWSTR source,
64 LPCWSTR target, const INT type, LPCWSTR action);
65 static UINT HANDLE_CustomType23(MSIPACKAGE *package, LPCWSTR source,
66 LPCWSTR target, const INT type, LPCWSTR action);
67 static UINT HANDLE_CustomType50(MSIPACKAGE *package, LPCWSTR source,
68 LPCWSTR target, const INT type, LPCWSTR action);
69 static UINT HANDLE_CustomType34(MSIPACKAGE *package, LPCWSTR source,
70 LPCWSTR target, const INT type, LPCWSTR action);
71 static UINT HANDLE_CustomType37_38(MSIPACKAGE *package, LPCWSTR source,
72 LPCWSTR target, const INT type, LPCWSTR action);
73 static UINT HANDLE_CustomType5_6(MSIPACKAGE *package, LPCWSTR source,
74 LPCWSTR target, const INT type, LPCWSTR action);
75 static UINT HANDLE_CustomType21_22(MSIPACKAGE *package, LPCWSTR source,
76 LPCWSTR target, const INT type, LPCWSTR action);
77 static UINT HANDLE_CustomType53_54(MSIPACKAGE *package, LPCWSTR source,
78 LPCWSTR target, const INT type, LPCWSTR action);
80 typedef UINT (WINAPI *MsiCustomActionEntryPoint)( MSIHANDLE );
82 static CRITICAL_SECTION msi_custom_action_cs;
83 static CRITICAL_SECTION_DEBUG msi_custom_action_cs_debug =
85 0, 0, &msi_custom_action_cs,
86 { &msi_custom_action_cs_debug.ProcessLocksList,
87 &msi_custom_action_cs_debug.ProcessLocksList },
88 0, 0, { (DWORD_PTR)(__FILE__ ": msi_custom_action_cs") }
90 static CRITICAL_SECTION msi_custom_action_cs = { &msi_custom_action_cs_debug, -1, 0, 0, 0, 0 };
92 static struct list msi_pending_custom_actions = LIST_INIT( msi_pending_custom_actions );
94 static BOOL check_execution_scheduling_options(MSIPACKAGE *package, LPCWSTR action, UINT options)
99 if ((options & msidbCustomActionTypeClientRepeat) ==
100 msidbCustomActionTypeClientRepeat)
102 if (!(package->script->InWhatSequence & SEQUENCE_UI &&
103 package->script->InWhatSequence & SEQUENCE_EXEC))
105 TRACE("Skipping action due to dbCustomActionTypeClientRepeat option.\n");
109 else if (options & msidbCustomActionTypeFirstSequence)
111 if (package->script->InWhatSequence & SEQUENCE_UI &&
112 package->script->InWhatSequence & SEQUENCE_EXEC )
114 TRACE("Skipping action due to msidbCustomActionTypeFirstSequence option.\n");
118 else if (options & msidbCustomActionTypeOncePerProcess)
120 if (check_unique_action(package,action))
122 TRACE("Skipping action due to msidbCustomActionTypeOncePerProcess option.\n");
126 register_unique_action(package,action);
132 /* stores the following properties before the action:
134 * [CustomActionData<=>UserSID<=>ProductCode]Action
136 static LPWSTR msi_get_deferred_action(LPCWSTR action, LPCWSTR actiondata,
137 LPCWSTR usersid, LPCWSTR prodcode)
142 static const WCHAR format[] = {
143 '[','%','s','<','=','>','%','s','<','=','>','%','s',']','%','s',0
147 return strdupW(action);
149 len = lstrlenW(action) + lstrlenW(actiondata) +
150 lstrlenW(usersid) + lstrlenW(prodcode) +
151 lstrlenW(format) - 7;
152 deferred = msi_alloc(len * sizeof(WCHAR));
154 sprintfW(deferred, format, actiondata, usersid, prodcode, action);
158 static void set_deferred_action_props(MSIPACKAGE *package, LPWSTR deferred_data)
160 LPWSTR end, beg = deferred_data + 1;
162 static const WCHAR sep[] = {'<','=','>',0};
164 end = strstrW(beg, sep);
166 msi_set_property(package->db, szCustomActionData, beg);
169 end = strstrW(beg, sep);
171 msi_set_property(package->db, szUserSID, beg);
174 end = strchrW(beg, ']');
176 msi_set_property(package->db, szProductCode, beg);
179 UINT ACTION_CustomAction(MSIPACKAGE *package, LPCWSTR action, UINT script, BOOL execute)
181 UINT rc = ERROR_SUCCESS;
183 static const WCHAR ExecSeqQuery[] =
184 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
185 '`','C','u','s','t','o' ,'m','A','c','t','i','o','n','`',
186 ' ','W','H','E','R','E',' ','`','A','c','t','i' ,'o','n','`',' ',
187 '=',' ','\'','%','s','\'',0};
189 LPCWSTR source, target;
190 LPWSTR ptr, deferred_data = NULL;
191 LPWSTR action_copy = strdupW(action);
192 WCHAR *deformated=NULL;
194 /* deferred action: [properties]Action */
195 if ((ptr = strrchrW(action_copy, ']')))
197 deferred_data = action_copy;
201 row = MSI_QueryGetRecord( package->db, ExecSeqQuery, action );
204 msi_free(action_copy);
205 return ERROR_CALL_NOT_IMPLEMENTED;
208 type = MSI_RecordGetInteger(row,2);
210 source = MSI_RecordGetString(row,3);
211 target = MSI_RecordGetString(row,4);
213 TRACE("Handling custom action %s (%x %s %s)\n",debugstr_w(action),type,
214 debugstr_w(source), debugstr_w(target));
216 /* handle some of the deferred actions */
217 if (type & msidbCustomActionTypeTSAware)
218 FIXME("msidbCustomActionTypeTSAware not handled\n");
220 if (type & msidbCustomActionTypeInScript)
222 if (type & msidbCustomActionTypeNoImpersonate)
223 WARN("msidbCustomActionTypeNoImpersonate not handled\n");
225 if (type & msidbCustomActionTypeRollback)
227 FIXME("Rollback only action... rollbacks not supported yet\n");
228 schedule_action(package, ROLLBACK_SCRIPT, action);
234 LPWSTR actiondata = msi_dup_property(package->db, action);
235 LPWSTR usersid = msi_dup_property(package->db, szUserSID);
236 LPWSTR prodcode = msi_dup_property(package->db, szProductCode);
237 LPWSTR deferred = msi_get_deferred_action(action, actiondata, usersid, prodcode);
239 if (type & msidbCustomActionTypeCommit)
241 TRACE("Deferring Commit Action!\n");
242 schedule_action(package, COMMIT_SCRIPT, deferred);
246 TRACE("Deferring Action!\n");
247 schedule_action(package, INSTALL_SCRIPT, deferred);
251 msi_free(actiondata);
259 LPWSTR actiondata = msi_dup_property( package->db, action );
264 package->scheduled_action_running = TRUE;
267 package->commit_action_running = TRUE;
269 case ROLLBACK_SCRIPT:
270 package->rollback_action_running = TRUE;
277 set_deferred_action_props(package, deferred_data);
279 msi_set_property(package->db, szCustomActionData, actiondata);
281 msi_set_property(package->db, szCustomActionData, szEmpty);
283 msi_free(actiondata);
286 else if (!check_execution_scheduling_options(package,action,type))
292 switch (type & CUSTOM_ACTION_TYPE_MASK)
294 case 1: /* DLL file stored in a Binary table stream */
295 rc = HANDLE_CustomType1(package,source,target,type,action);
297 case 2: /* EXE file stored in a Binary table stream */
298 rc = HANDLE_CustomType2(package,source,target,type,action);
300 case 18: /*EXE file installed with package */
301 rc = HANDLE_CustomType18(package,source,target,type,action);
303 case 19: /* Error that halts install */
304 rc = HANDLE_CustomType19(package,source,target,type,action);
307 rc = HANDLE_CustomType17(package,source,target,type,action);
309 case 23: /* installs another package in the source tree */
310 deformat_string(package,target,&deformated);
311 rc = HANDLE_CustomType23(package,source,deformated,type,action);
312 msi_free(deformated);
314 case 50: /*EXE file specified by a property value */
315 rc = HANDLE_CustomType50(package,source,target,type,action);
317 case 34: /*EXE to be run in specified directory */
318 rc = HANDLE_CustomType34(package,source,target,type,action);
320 case 35: /* Directory set with formatted text. */
321 deformat_string(package,target,&deformated);
322 MSI_SetTargetPathW(package, source, deformated);
323 msi_free(deformated);
325 case 51: /* Property set with formatted text. */
329 deformat_string(package,target,&deformated);
330 rc = msi_set_property( package->db, source, deformated );
331 if (rc == ERROR_SUCCESS && !strcmpW( source, cszSourceDir ))
332 msi_reset_folders( package, TRUE );
333 msi_free(deformated);
335 case 37: /* JScript/VBScript text stored in target column. */
337 rc = HANDLE_CustomType37_38(package,source,target,type,action);
340 case 6: /* JScript/VBScript file stored in a Binary table stream. */
341 rc = HANDLE_CustomType5_6(package,source,target,type,action);
343 case 21: /* JScript/VBScript file installed with the product. */
345 rc = HANDLE_CustomType21_22(package,source,target,type,action);
347 case 53: /* JScript/VBScript text specified by a property value. */
349 rc = HANDLE_CustomType53_54(package,source,target,type,action);
352 FIXME("UNHANDLED ACTION TYPE %i (%s %s)\n",
353 type & CUSTOM_ACTION_TYPE_MASK, debugstr_w(source),
358 package->scheduled_action_running = FALSE;
359 package->commit_action_running = FALSE;
360 package->rollback_action_running = FALSE;
361 msi_free(action_copy);
362 msiobj_release(&row->hdr);
367 static UINT store_binary_to_temp(MSIPACKAGE *package, LPCWSTR source,
370 static const WCHAR query[] = {
371 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
372 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',' ',
373 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
381 if (msi_get_property(package->db, cszTempFolder, fmt, &sz) != ERROR_SUCCESS)
382 GetTempPathW(MAX_PATH, fmt);
384 if (GetTempFileNameW(fmt, szMsi, 0, tmp_file) == 0)
386 TRACE("Unable to create file\n");
387 return ERROR_FUNCTION_FAILED;
389 track_tempfile(package, tmp_file);
391 row = MSI_QueryGetRecord(package->db, query, source);
393 return ERROR_FUNCTION_FAILED;
395 /* write out the file */
396 file = CreateFileW(tmp_file, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
397 FILE_ATTRIBUTE_NORMAL, NULL);
398 if (file == INVALID_HANDLE_VALUE)
399 r = ERROR_FUNCTION_FAILED;
406 r = MSI_RecordReadStream(row, 2, buffer, &sz);
407 if (r != ERROR_SUCCESS)
409 ERR("Failed to get stream\n");
412 WriteFile(file, buffer, sz, &write, NULL);
413 } while (sz == sizeof buffer);
417 msiobj_release(&row->hdr);
422 static void file_running_action(MSIPACKAGE* package, HANDLE Handle,
423 BOOL process, LPCWSTR name)
425 MSIRUNNINGACTION *action;
427 action = msi_alloc( sizeof(MSIRUNNINGACTION) );
429 action->handle = Handle;
430 action->process = process;
431 action->name = strdupW(name);
433 list_add_tail( &package->RunningActions, &action->entry );
436 static UINT custom_get_process_return( HANDLE process )
440 GetExitCodeProcess( process, &rc );
442 return ERROR_FUNCTION_FAILED;
443 return ERROR_SUCCESS;
446 static UINT custom_get_thread_return( MSIPACKAGE *package, HANDLE thread )
450 GetExitCodeThread( thread, &rc );
454 case ERROR_FUNCTION_NOT_CALLED:
456 case ERROR_INSTALL_USEREXIT:
457 case ERROR_INSTALL_FAILURE:
459 case ERROR_NO_MORE_ITEMS:
460 return ERROR_SUCCESS;
461 case ERROR_INSTALL_SUSPEND:
462 ACTION_ForceReboot( package );
463 return ERROR_SUCCESS;
465 ERR("Invalid Return Code %d\n",rc);
466 return ERROR_INSTALL_FAILURE;
470 static UINT wait_process_handle(MSIPACKAGE* package, UINT type,
471 HANDLE ProcessHandle, LPCWSTR name)
473 UINT rc = ERROR_SUCCESS;
475 if (!(type & msidbCustomActionTypeAsync))
477 TRACE("waiting for %s\n", debugstr_w(name));
479 msi_dialog_check_messages(ProcessHandle);
481 if (!(type & msidbCustomActionTypeContinue))
482 rc = custom_get_process_return(ProcessHandle);
484 CloseHandle(ProcessHandle);
488 TRACE("%s running in background\n", debugstr_w(name));
490 if (!(type & msidbCustomActionTypeContinue))
491 file_running_action(package, ProcessHandle, TRUE, name);
493 CloseHandle(ProcessHandle);
499 typedef struct _msi_custom_action_info {
509 } msi_custom_action_info;
511 static void release_custom_action_data( msi_custom_action_info *info )
513 EnterCriticalSection( &msi_custom_action_cs );
517 list_remove( &info->entry );
519 CloseHandle( info->handle );
520 msi_free( info->action );
521 msi_free( info->source );
522 msi_free( info->target );
523 msiobj_release( &info->package->hdr );
527 LeaveCriticalSection( &msi_custom_action_cs );
530 /* must be called inside msi_custom_action_cs if info is in the pending custom actions list */
531 static void addref_custom_action_data( msi_custom_action_info *info )
536 static UINT wait_thread_handle( msi_custom_action_info *info )
538 UINT rc = ERROR_SUCCESS;
540 if (!(info->type & msidbCustomActionTypeAsync))
542 TRACE("waiting for %s\n", debugstr_w( info->action ));
544 msi_dialog_check_messages( info->handle );
546 if (!(info->type & msidbCustomActionTypeContinue))
547 rc = custom_get_thread_return( info->package, info->handle );
549 release_custom_action_data( info );
553 TRACE("%s running in background\n", debugstr_w( info->action ));
559 static msi_custom_action_info *find_action_by_guid( const GUID *guid )
561 msi_custom_action_info *info;
564 EnterCriticalSection( &msi_custom_action_cs );
566 LIST_FOR_EACH_ENTRY( info, &msi_pending_custom_actions, msi_custom_action_info, entry )
568 if (IsEqualGUID( &info->guid, guid ))
570 addref_custom_action_data( info );
576 LeaveCriticalSection( &msi_custom_action_cs );
584 static void handle_msi_break( LPCWSTR target )
589 static const WCHAR MsiBreak[] = { 'M','s','i','B','r','e','a','k',0 };
590 static const WCHAR WindowsInstaller[] = {
591 'W','i','n','d','o','w','s',' ','I','n','s','t','a','l','l','e','r',0
594 static const WCHAR format[] = {
595 'T','o',' ','d','e','b','u','g',' ','y','o','u','r',' ',
596 'c','u','s','t','o','m',' ','a','c','t','i','o','n',',',' ',
597 'a','t','t','a','c','h',' ','y','o','u','r',' ','d','e','b','u','g','g','e','r',' ',
598 't','o',' ','p','r','o','c','e','s','s',' ','%','i',' ','(','0','x','%','X',')',' ',
599 'a','n','d',' ','p','r','e','s','s',' ','O','K',0
602 if( !GetEnvironmentVariableW( MsiBreak, val, MAX_PATH ))
605 if( lstrcmpiW( val, target ))
608 msg = msi_alloc( (lstrlenW(format) + 10) * sizeof(WCHAR) );
612 wsprintfW( msg, format, GetCurrentProcessId(), GetCurrentProcessId());
613 MessageBoxW( NULL, msg, WindowsInstaller, MB_OK);
618 static UINT get_action_info( const GUID *guid, INT *type, MSIHANDLE *handle,
619 BSTR *dll, BSTR *funcname,
620 IWineMsiRemotePackage **package )
622 IClassFactory *cf = NULL;
623 IWineMsiRemoteCustomAction *rca = NULL;
626 r = DllGetClassObject( &CLSID_IWineMsiRemoteCustomAction,
627 &IID_IClassFactory, (LPVOID *)&cf );
630 ERR("failed to get IClassFactory interface\n");
631 return ERROR_FUNCTION_FAILED;
634 r = IClassFactory_CreateInstance( cf, NULL, &IID_IWineMsiRemoteCustomAction, (LPVOID *)&rca );
637 ERR("failed to get IWineMsiRemoteCustomAction interface\n");
638 return ERROR_FUNCTION_FAILED;
641 r = IWineMsiRemoteCustomAction_GetActionInfo( rca, guid, type, handle, dll, funcname, package );
642 IWineMsiRemoteCustomAction_Release( rca );
645 ERR("GetActionInfo failed\n");
646 return ERROR_FUNCTION_FAILED;
649 return ERROR_SUCCESS;
653 extern UINT CUSTOMPROC_wrapper( MsiCustomActionEntryPoint proc, MSIHANDLE handle );
654 __ASM_GLOBAL_FUNC( CUSTOMPROC_wrapper,
656 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
657 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
659 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
661 "movl 8(%ebp),%eax\n\t"
664 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
665 __ASM_CFI(".cfi_same_value %ebp\n\t")
668 static inline UINT CUSTOMPROC_wrapper( MsiCustomActionEntryPoint proc, MSIHANDLE handle )
674 static DWORD ACTION_CallDllFunction( const GUID *guid )
676 MsiCustomActionEntryPoint fn;
677 MSIHANDLE hPackage, handle;
680 UINT r = ERROR_FUNCTION_FAILED;
681 BSTR dll = NULL, function = NULL;
683 IWineMsiRemotePackage *remote_package = NULL;
685 TRACE("%s\n", debugstr_guid( guid ));
687 r = get_action_info( guid, &type, &handle, &dll, &function, &remote_package );
688 if (r != ERROR_SUCCESS)
691 hModule = LoadLibraryW( dll );
694 ERR("failed to load dll %s (%u)\n", debugstr_w( dll ), GetLastError() );
698 proc = strdupWtoA( function );
699 fn = (MsiCustomActionEntryPoint) GetProcAddress( hModule, proc );
703 hPackage = alloc_msi_remote_handle( (IUnknown *)remote_package );
706 IWineMsiRemotePackage_SetMsiHandle( remote_package, handle );
707 TRACE("calling %s\n", debugstr_w( function ) );
708 handle_msi_break( function );
712 r = CUSTOMPROC_wrapper( fn, hPackage );
716 ERR("Custom action (%s:%s) caused a page fault: %08x\n",
717 debugstr_w(dll), debugstr_w(function), GetExceptionCode());
722 MsiCloseHandle( hPackage );
725 ERR("failed to create handle for %p\n", remote_package );
728 ERR("GetProcAddress(%s) failed\n", debugstr_w( function ) );
730 FreeLibrary(hModule);
732 IWineMsiRemotePackage_Release( remote_package );
733 SysFreeString( dll );
734 SysFreeString( function );
735 MsiCloseHandle( handle );
740 static DWORD WINAPI DllThread( LPVOID arg )
745 TRACE("custom action (%x) started\n", GetCurrentThreadId() );
747 rc = ACTION_CallDllFunction( guid );
749 TRACE("custom action (%x) returned %i\n", GetCurrentThreadId(), rc );
751 MsiCloseAllHandles();
755 static DWORD ACTION_CAInstallPackage(const GUID *guid)
757 msi_custom_action_info *info;
758 UINT r = ERROR_FUNCTION_FAILED;
759 INSTALLUILEVEL old_level;
761 info = find_action_by_guid(guid);
764 ERR("failed to find action %s\n", debugstr_guid(guid));
768 old_level = MsiSetInternalUI(INSTALLUILEVEL_BASIC, NULL);
769 r = MsiInstallProductW(info->source, info->target);
770 MsiSetInternalUI(old_level, NULL);
772 release_custom_action_data(info);
777 static DWORD WINAPI ConcurrentInstallThread(LPVOID arg)
782 TRACE("concurrent installation (%x) started\n", GetCurrentThreadId());
784 rc = ACTION_CAInstallPackage(guid);
786 TRACE("concurrent installation (%x) returned %i\n", GetCurrentThreadId(), rc);
788 MsiCloseAllHandles();
792 static msi_custom_action_info *do_msidbCustomActionTypeDll(
793 MSIPACKAGE *package, INT type, LPCWSTR source, LPCWSTR target, LPCWSTR action )
795 msi_custom_action_info *info;
797 info = msi_alloc( sizeof *info );
801 msiobj_addref( &package->hdr );
802 info->refs = 2; /* 1 for our caller and 1 for thread we created */
803 info->package = package;
805 info->target = strdupW( target );
806 info->source = strdupW( source );
807 info->action = strdupW( action );
808 CoCreateGuid( &info->guid );
810 EnterCriticalSection( &msi_custom_action_cs );
811 list_add_tail( &msi_pending_custom_actions, &info->entry );
812 LeaveCriticalSection( &msi_custom_action_cs );
814 info->handle = CreateThread( NULL, 0, DllThread, &info->guid, 0, NULL );
817 /* release both references */
818 release_custom_action_data( info );
819 release_custom_action_data( info );
826 static msi_custom_action_info *do_msidbCAConcurrentInstall(
827 MSIPACKAGE *package, INT type, LPCWSTR source, LPCWSTR target, LPCWSTR action)
829 msi_custom_action_info *info;
831 info = msi_alloc( sizeof *info );
835 msiobj_addref( &package->hdr );
836 info->refs = 2; /* 1 for our caller and 1 for thread we created */
837 info->package = package;
839 info->target = strdupW( target );
840 info->source = strdupW( source );
841 info->action = strdupW( action );
842 CoCreateGuid( &info->guid );
844 EnterCriticalSection( &msi_custom_action_cs );
845 list_add_tail( &msi_pending_custom_actions, &info->entry );
846 LeaveCriticalSection( &msi_custom_action_cs );
848 info->handle = CreateThread( NULL, 0, ConcurrentInstallThread, &info->guid, 0, NULL );
851 /* release both references */
852 release_custom_action_data( info );
853 release_custom_action_data( info );
860 static UINT HANDLE_CustomType23(MSIPACKAGE *package, LPCWSTR source,
861 LPCWSTR target, const INT type, LPCWSTR action)
863 msi_custom_action_info *info;
864 WCHAR package_path[MAX_PATH];
869 msi_get_property(package->db, cszSourceDir, package_path, &size);
870 lstrcatW(package_path, szBackSlash);
871 lstrcatW(package_path, source);
873 TRACE("Installing package %s concurrently\n", debugstr_w(package_path));
875 info = do_msidbCAConcurrentInstall(package, type, package_path, target, action);
877 r = wait_thread_handle(info);
878 release_custom_action_data( info );
882 static UINT HANDLE_CustomType1(MSIPACKAGE *package, LPCWSTR source,
883 LPCWSTR target, const INT type, LPCWSTR action)
885 msi_custom_action_info *info;
886 WCHAR tmp_file[MAX_PATH];
889 r = store_binary_to_temp(package, source, tmp_file);
890 if (r != ERROR_SUCCESS)
893 TRACE("Calling function %s from %s\n",debugstr_w(target),
894 debugstr_w(tmp_file));
896 if (!strchrW(tmp_file,'.'))
897 strcatW(tmp_file, szDot);
899 info = do_msidbCustomActionTypeDll( package, type, tmp_file, target, action );
901 r = wait_thread_handle( info );
902 release_custom_action_data( info );
906 static UINT HANDLE_CustomType2(MSIPACKAGE *package, LPCWSTR source,
907 LPCWSTR target, const INT type, LPCWSTR action)
909 WCHAR tmp_file[MAX_PATH];
911 PROCESS_INFORMATION info;
914 WCHAR *deformated = NULL;
916 static const WCHAR spc[] = {' ',0};
919 memset(&si,0,sizeof(STARTUPINFOW));
921 r = store_binary_to_temp(package, source, tmp_file);
922 if (r != ERROR_SUCCESS)
925 deformat_string(package,target,&deformated);
927 len = strlenW(tmp_file)+2;
930 len += strlenW(deformated);
932 cmd = msi_alloc(sizeof(WCHAR)*len);
934 strcpyW(cmd,tmp_file);
938 strcatW(cmd,deformated);
940 msi_free(deformated);
943 TRACE("executing exe %s\n", debugstr_w(cmd));
945 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
946 c_collen, &si, &info);
951 ERR("Unable to execute command %s\n", debugstr_w(cmd));
952 return ERROR_SUCCESS;
954 CloseHandle( info.hThread );
956 r = wait_process_handle(package, type, info.hProcess, action);
961 static UINT HANDLE_CustomType17(MSIPACKAGE *package, LPCWSTR source,
962 LPCWSTR target, const INT type, LPCWSTR action)
964 msi_custom_action_info *info;
968 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
970 file = get_loaded_file( package, source );
973 ERR("invalid file key %s\n", debugstr_w( source ));
974 return ERROR_FUNCTION_FAILED;
977 info = do_msidbCustomActionTypeDll( package, type, file->TargetPath, target, action );
979 r = wait_thread_handle( info );
980 release_custom_action_data( info );
984 static UINT HANDLE_CustomType18(MSIPACKAGE *package, LPCWSTR source,
985 LPCWSTR target, const INT type, LPCWSTR action)
988 PROCESS_INFORMATION info;
993 static const WCHAR spc[] = {' ',0};
996 memset(&si,0,sizeof(STARTUPINFOW));
998 file = get_loaded_file(package,source);
1000 return ERROR_FUNCTION_FAILED;
1002 len = lstrlenW( file->TargetPath );
1004 deformat_string(package,target,&deformated);
1006 len += strlenW(deformated);
1009 cmd = msi_alloc(len * sizeof(WCHAR));
1011 lstrcpyW( cmd, file->TargetPath);
1015 strcatW(cmd, deformated);
1017 msi_free(deformated);
1020 TRACE("executing exe %s\n", debugstr_w(cmd));
1022 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
1023 c_collen, &si, &info);
1027 ERR("Unable to execute command %s\n", debugstr_w(cmd));
1029 return ERROR_SUCCESS;
1032 CloseHandle( info.hThread );
1034 return wait_process_handle(package, type, info.hProcess, action);
1037 static UINT HANDLE_CustomType19(MSIPACKAGE *package, LPCWSTR source,
1038 LPCWSTR target, const INT type, LPCWSTR action)
1040 static const WCHAR query[] = {
1041 'S','E','L','E','C','T',' ','`','M','e','s','s','a','g','e','`',' ',
1042 'F','R','O','M',' ','`','E','r','r','o','r','`',' ',
1043 'W','H','E','R','E',' ','`','E','r','r','o','r','`',' ','=',' ',
1047 LPWSTR deformated = NULL;
1049 deformat_string( package, target, &deformated );
1051 /* first try treat the error as a number */
1052 row = MSI_QueryGetRecord( package->db, query, deformated );
1055 LPCWSTR error = MSI_RecordGetString( row, 1 );
1056 if ((gUILevel & INSTALLUILEVEL_MASK) != INSTALLUILEVEL_NONE)
1057 MessageBoxW( NULL, error, NULL, MB_OK );
1058 msiobj_release( &row->hdr );
1060 else if ((gUILevel & INSTALLUILEVEL_MASK) != INSTALLUILEVEL_NONE)
1061 MessageBoxW( NULL, deformated, NULL, MB_OK );
1063 msi_free( deformated );
1065 return ERROR_INSTALL_FAILURE;
1068 static UINT HANDLE_CustomType50(MSIPACKAGE *package, LPCWSTR source,
1069 LPCWSTR target, const INT type, LPCWSTR action)
1072 PROCESS_INFORMATION info;
1078 static const WCHAR spc[] = {' ',0};
1080 memset(&si,0,sizeof(STARTUPINFOW));
1081 memset(&info,0,sizeof(PROCESS_INFORMATION));
1083 prop = msi_dup_property( package->db, source );
1085 return ERROR_SUCCESS;
1087 deformat_string(package,target,&deformated);
1088 len = strlenW(prop) + 2;
1090 len += strlenW(deformated);
1092 cmd = msi_alloc(sizeof(WCHAR)*len);
1098 strcatW(cmd,deformated);
1100 msi_free(deformated);
1104 TRACE("executing exe %s\n", debugstr_w(cmd));
1106 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
1107 c_collen, &si, &info);
1111 ERR("Unable to execute command %s\n", debugstr_w(cmd));
1113 return ERROR_SUCCESS;
1117 CloseHandle( info.hThread );
1119 return wait_process_handle(package, type, info.hProcess, action);
1122 static UINT HANDLE_CustomType34(MSIPACKAGE *package, LPCWSTR source,
1123 LPCWSTR target, const INT type, LPCWSTR action)
1125 LPWSTR workingdir, filename;
1127 PROCESS_INFORMATION info;
1130 memset(&si, 0, sizeof(STARTUPINFOW));
1132 workingdir = resolve_folder(package, source, FALSE, FALSE, TRUE, NULL);
1135 return ERROR_FUNCTION_FAILED;
1137 deformat_string(package, target, &filename);
1141 msi_free(workingdir);
1142 return ERROR_FUNCTION_FAILED;
1145 TRACE("executing exe %s with working directory %s\n",
1146 debugstr_w(filename), debugstr_w(workingdir));
1148 rc = CreateProcessW(NULL, filename, NULL, NULL, FALSE, 0, NULL,
1149 workingdir, &si, &info);
1153 ERR("Unable to execute command %s with working directory %s\n",
1154 debugstr_w(filename), debugstr_w(workingdir));
1156 msi_free(workingdir);
1157 return ERROR_SUCCESS;
1161 msi_free(workingdir);
1163 CloseHandle( info.hThread );
1165 return wait_process_handle(package, type, info.hProcess, action);
1168 static DWORD ACTION_CallScript( const GUID *guid )
1170 msi_custom_action_info *info;
1172 UINT r = ERROR_FUNCTION_FAILED;
1174 info = find_action_by_guid( guid );
1177 ERR("failed to find action %s\n", debugstr_guid( guid) );
1181 TRACE("function %s, script %s\n", debugstr_w( info->target ), debugstr_w( info->source ) );
1183 hPackage = alloc_msihandle( &info->package->hdr );
1186 r = call_script( hPackage, info->type, info->source, info->target, info->action );
1187 MsiCloseHandle( hPackage );
1190 ERR("failed to create handle for %p\n", info->package );
1192 release_custom_action_data( info );
1197 static DWORD WINAPI ScriptThread( LPVOID arg )
1202 TRACE("custom action (%x) started\n", GetCurrentThreadId() );
1204 rc = ACTION_CallScript( guid );
1206 TRACE("custom action (%x) returned %i\n", GetCurrentThreadId(), rc );
1208 MsiCloseAllHandles();
1212 static msi_custom_action_info *do_msidbCustomActionTypeScript(
1213 MSIPACKAGE *package, INT type, LPCWSTR script, LPCWSTR function, LPCWSTR action )
1215 msi_custom_action_info *info;
1217 info = msi_alloc( sizeof *info );
1221 msiobj_addref( &package->hdr );
1222 info->refs = 2; /* 1 for our caller and 1 for thread we created */
1223 info->package = package;
1225 info->target = strdupW( function );
1226 info->source = strdupW( script );
1227 info->action = strdupW( action );
1228 CoCreateGuid( &info->guid );
1230 EnterCriticalSection( &msi_custom_action_cs );
1231 list_add_tail( &msi_pending_custom_actions, &info->entry );
1232 LeaveCriticalSection( &msi_custom_action_cs );
1234 info->handle = CreateThread( NULL, 0, ScriptThread, &info->guid, 0, NULL );
1237 /* release both references */
1238 release_custom_action_data( info );
1239 release_custom_action_data( info );
1246 static UINT HANDLE_CustomType37_38(MSIPACKAGE *package, LPCWSTR source,
1247 LPCWSTR target, const INT type, LPCWSTR action)
1250 msi_custom_action_info *info;
1252 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1254 info = do_msidbCustomActionTypeScript( package, type, target, NULL, action );
1256 r = wait_thread_handle( info );
1257 release_custom_action_data( info );
1261 static UINT HANDLE_CustomType5_6(MSIPACKAGE *package, LPCWSTR source,
1262 LPCWSTR target, const INT type, LPCWSTR action)
1264 static const WCHAR query[] = {
1265 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1266 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',' ',
1267 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
1269 msi_custom_action_info *info;
1270 CHAR *buffer = NULL;
1271 WCHAR *bufferw = NULL;
1275 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1277 row = MSI_QueryGetRecord(package->db, query, source);
1279 return ERROR_FUNCTION_FAILED;
1281 r = MSI_RecordReadStream(row, 2, NULL, &sz);
1282 if (r != ERROR_SUCCESS)
1285 buffer = msi_alloc(sizeof(CHAR)*(sz+1));
1287 return ERROR_FUNCTION_FAILED;
1289 r = MSI_RecordReadStream(row, 2, buffer, &sz);
1290 if (r != ERROR_SUCCESS)
1294 bufferw = strdupAtoW(buffer);
1297 r = ERROR_FUNCTION_FAILED;
1301 info = do_msidbCustomActionTypeScript( package, type, bufferw, target, action );
1302 r = wait_thread_handle( info );
1303 release_custom_action_data( info );
1311 static UINT HANDLE_CustomType21_22(MSIPACKAGE *package, LPCWSTR source,
1312 LPCWSTR target, const INT type, LPCWSTR action)
1314 msi_custom_action_info *info;
1317 DWORD sz, szHighWord = 0, read;
1319 WCHAR *bufferw=NULL;
1323 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1325 file = get_loaded_file(package,source);
1328 ERR("invalid file key %s\n", debugstr_w(source));
1329 return ERROR_FUNCTION_FAILED;
1332 hFile = CreateFileW(file->TargetPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
1333 if (hFile == INVALID_HANDLE_VALUE)
1334 return ERROR_FUNCTION_FAILED;
1336 sz = GetFileSize(hFile, &szHighWord);
1337 if (sz == INVALID_FILE_SIZE || szHighWord != 0)
1340 return ERROR_FUNCTION_FAILED;
1343 buffer = msi_alloc(sizeof(CHAR)*(sz+1));
1347 return ERROR_FUNCTION_FAILED;
1350 bRet = ReadFile(hFile, buffer, sz, &read, NULL);
1354 r = ERROR_FUNCTION_FAILED;
1359 bufferw = strdupAtoW(buffer);
1362 r = ERROR_FUNCTION_FAILED;
1366 info = do_msidbCustomActionTypeScript( package, type, bufferw, target, action );
1367 r = wait_thread_handle( info );
1368 release_custom_action_data( info );
1376 static UINT HANDLE_CustomType53_54(MSIPACKAGE *package, LPCWSTR source,
1377 LPCWSTR target, const INT type, LPCWSTR action)
1379 msi_custom_action_info *info;
1383 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1385 prop = msi_dup_property( package->db, source );
1387 return ERROR_SUCCESS;
1389 info = do_msidbCustomActionTypeScript( package, type, prop, NULL, action );
1391 r = wait_thread_handle( info );
1392 release_custom_action_data( info );
1396 void ACTION_FinishCustomActions(const MSIPACKAGE* package)
1399 HANDLE *wait_handles;
1400 unsigned int handle_count, i;
1401 msi_custom_action_info *info, *cursor;
1403 while ((item = list_head( &package->RunningActions )))
1405 MSIRUNNINGACTION *action = LIST_ENTRY( item, MSIRUNNINGACTION, entry );
1407 list_remove( &action->entry );
1409 TRACE("waiting for %s\n", debugstr_w( action->name ) );
1410 msi_dialog_check_messages( action->handle );
1412 CloseHandle( action->handle );
1413 msi_free( action->name );
1417 EnterCriticalSection( &msi_custom_action_cs );
1419 handle_count = list_count( &msi_pending_custom_actions );
1420 wait_handles = HeapAlloc( GetProcessHeap(), 0, handle_count * sizeof(HANDLE) );
1423 LIST_FOR_EACH_ENTRY_SAFE( info, cursor, &msi_pending_custom_actions, msi_custom_action_info, entry )
1425 if (info->package == package )
1427 if (DuplicateHandle(GetCurrentProcess(), info->handle, GetCurrentProcess(), &wait_handles[handle_count], SYNCHRONIZE, FALSE, 0))
1432 LeaveCriticalSection( &msi_custom_action_cs );
1434 for (i = 0; i < handle_count; i++)
1436 msi_dialog_check_messages( wait_handles[i] );
1437 CloseHandle( wait_handles[i] );
1440 HeapFree( GetProcessHeap(), 0, wait_handles );
1443 typedef struct _msi_custom_remote_impl {
1444 const IWineMsiRemoteCustomActionVtbl *lpVtbl;
1446 } msi_custom_remote_impl;
1448 static inline msi_custom_remote_impl* mcr_from_IWineMsiRemoteCustomAction( IWineMsiRemoteCustomAction* iface )
1450 return (msi_custom_remote_impl*) iface;
1453 static HRESULT WINAPI mcr_QueryInterface( IWineMsiRemoteCustomAction *iface,
1454 REFIID riid,LPVOID *ppobj)
1456 if( IsEqualCLSID( riid, &IID_IUnknown ) ||
1457 IsEqualCLSID( riid, &IID_IWineMsiRemoteCustomAction ) )
1459 IUnknown_AddRef( iface );
1464 return E_NOINTERFACE;
1467 static ULONG WINAPI mcr_AddRef( IWineMsiRemoteCustomAction *iface )
1469 msi_custom_remote_impl* This = mcr_from_IWineMsiRemoteCustomAction( iface );
1471 return InterlockedIncrement( &This->refs );
1474 static ULONG WINAPI mcr_Release( IWineMsiRemoteCustomAction *iface )
1476 msi_custom_remote_impl* This = mcr_from_IWineMsiRemoteCustomAction( iface );
1479 r = InterlockedDecrement( &This->refs );
1485 static HRESULT WINAPI mcr_GetActionInfo( IWineMsiRemoteCustomAction *iface, LPCGUID custom_action_guid,
1486 INT *type, MSIHANDLE *handle, BSTR *dll, BSTR *func, IWineMsiRemotePackage **remote_package )
1488 msi_custom_action_info *info;
1490 info = find_action_by_guid( custom_action_guid );
1495 *handle = alloc_msihandle( &info->package->hdr );
1496 *dll = SysAllocString( info->source );
1497 *func = SysAllocString( info->target );
1499 release_custom_action_data( info );
1500 return create_msi_remote_package( NULL, (LPVOID *)remote_package );
1503 static const IWineMsiRemoteCustomActionVtbl msi_custom_remote_vtbl =
1511 HRESULT create_msi_custom_remote( IUnknown *pOuter, LPVOID *ppObj )
1513 msi_custom_remote_impl* This;
1515 This = msi_alloc( sizeof *This );
1517 return E_OUTOFMEMORY;
1519 This->lpVtbl = &msi_custom_remote_vtbl;