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");
227 LPWSTR actiondata = msi_dup_property(package->db, action);
228 LPWSTR usersid = msi_dup_property(package->db, szUserSID);
229 LPWSTR prodcode = msi_dup_property(package->db, szProductCode);
230 LPWSTR deferred = msi_get_deferred_action(action, actiondata, usersid, prodcode);
232 if (type & msidbCustomActionTypeCommit)
234 TRACE("Deferring commit action\n");
235 schedule_action(package, COMMIT_SCRIPT, deferred);
237 else if (type & msidbCustomActionTypeRollback)
239 FIXME("Deferring rollback only action... rollbacks not supported yet\n");
240 schedule_action(package, ROLLBACK_SCRIPT, deferred);
244 TRACE("Deferring action\n");
245 schedule_action(package, INSTALL_SCRIPT, deferred);
249 msi_free(actiondata);
257 LPWSTR actiondata = msi_dup_property( package->db, action );
259 if (type & msidbCustomActionTypeInScript)
260 package->scheduled_action_running = TRUE;
262 if (type & msidbCustomActionTypeCommit)
263 package->commit_action_running = TRUE;
265 if (type & msidbCustomActionTypeRollback)
266 package->rollback_action_running = TRUE;
269 set_deferred_action_props(package, deferred_data);
271 msi_set_property(package->db, szCustomActionData, actiondata);
273 msi_set_property(package->db, szCustomActionData, szEmpty);
275 msi_free(actiondata);
278 else if (!check_execution_scheduling_options(package,action,type))
284 switch (type & CUSTOM_ACTION_TYPE_MASK)
286 case 1: /* DLL file stored in a Binary table stream */
287 rc = HANDLE_CustomType1(package,source,target,type,action);
289 case 2: /* EXE file stored in a Binary table stream */
290 rc = HANDLE_CustomType2(package,source,target,type,action);
292 case 18: /*EXE file installed with package */
293 rc = HANDLE_CustomType18(package,source,target,type,action);
295 case 19: /* Error that halts install */
296 rc = HANDLE_CustomType19(package,source,target,type,action);
299 rc = HANDLE_CustomType17(package,source,target,type,action);
301 case 23: /* installs another package in the source tree */
302 deformat_string(package,target,&deformated);
303 rc = HANDLE_CustomType23(package,source,deformated,type,action);
304 msi_free(deformated);
306 case 50: /*EXE file specified by a property value */
307 rc = HANDLE_CustomType50(package,source,target,type,action);
309 case 34: /*EXE to be run in specified directory */
310 rc = HANDLE_CustomType34(package,source,target,type,action);
312 case 35: /* Directory set with formatted text. */
313 deformat_string(package,target,&deformated);
314 MSI_SetTargetPathW(package, source, deformated);
315 msi_free(deformated);
317 case 51: /* Property set with formatted text. */
321 deformat_string(package,target,&deformated);
322 rc = msi_set_property( package->db, source, deformated );
323 if (rc == ERROR_SUCCESS && !strcmpW( source, cszSourceDir ))
324 msi_reset_folders( package, TRUE );
325 msi_free(deformated);
327 case 37: /* JScript/VBScript text stored in target column. */
329 rc = HANDLE_CustomType37_38(package,source,target,type,action);
332 case 6: /* JScript/VBScript file stored in a Binary table stream. */
333 rc = HANDLE_CustomType5_6(package,source,target,type,action);
335 case 21: /* JScript/VBScript file installed with the product. */
337 rc = HANDLE_CustomType21_22(package,source,target,type,action);
339 case 53: /* JScript/VBScript text specified by a property value. */
341 rc = HANDLE_CustomType53_54(package,source,target,type,action);
344 FIXME("UNHANDLED ACTION TYPE %i (%s %s)\n",
345 type & CUSTOM_ACTION_TYPE_MASK, debugstr_w(source),
350 package->scheduled_action_running = FALSE;
351 package->commit_action_running = FALSE;
352 package->rollback_action_running = FALSE;
353 msi_free(action_copy);
354 msiobj_release(&row->hdr);
358 static MSIBINARY *create_temp_binary( MSIPACKAGE *package, LPCWSTR source, BOOL dll )
360 static const WCHAR query[] = {
361 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
362 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',' ',
363 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
368 WCHAR fmt[MAX_PATH], tmpfile[MAX_PATH];
369 DWORD sz = MAX_PATH, write;
372 if (msi_get_property(package->db, cszTempFolder, fmt, &sz) != ERROR_SUCCESS)
373 GetTempPathW(MAX_PATH, fmt);
375 if (!GetTempFileNameW( fmt, szMsi, 0, tmpfile ))
377 TRACE("unable to create temp file %s (%u)\n", debugstr_w(tmpfile), GetLastError());
381 row = MSI_QueryGetRecord(package->db, query, source);
385 if (!(binary = msi_alloc_zero( sizeof(MSIBINARY) )))
387 msiobj_release( &row->hdr );
390 file = CreateFileW( tmpfile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
391 if (file == INVALID_HANDLE_VALUE)
393 msiobj_release( &row->hdr );
400 r = MSI_RecordReadStream( row, 2, buffer, &sz );
401 if (r != ERROR_SUCCESS)
403 ERR("Failed to get stream\n");
406 WriteFile( file, buffer, sz, &write, NULL );
407 } while (sz == sizeof buffer);
410 msiobj_release( &row->hdr );
411 if (r != ERROR_SUCCESS)
413 DeleteFileW( tmpfile );
418 /* keep a reference to prevent the dll from being unloaded */
419 if (dll && !(binary->module = LoadLibraryW( tmpfile )))
421 ERR("failed to load dll %s (%u)\n", debugstr_w( binary->tmpfile ), GetLastError() );
422 DeleteFileW( tmpfile );
426 binary->source = strdupW( source );
427 binary->tmpfile = strdupW( tmpfile );
428 list_add_tail( &package->binaries, &binary->entry );
432 static MSIBINARY *get_temp_binary( MSIPACKAGE *package, LPCWSTR source, BOOL dll )
436 LIST_FOR_EACH_ENTRY( binary, &package->binaries, MSIBINARY, entry )
438 if (!strcmpW( binary->source, source ))
442 return create_temp_binary( package, source, dll );
445 static void file_running_action(MSIPACKAGE* package, HANDLE Handle,
446 BOOL process, LPCWSTR name)
448 MSIRUNNINGACTION *action;
450 action = msi_alloc( sizeof(MSIRUNNINGACTION) );
452 action->handle = Handle;
453 action->process = process;
454 action->name = strdupW(name);
456 list_add_tail( &package->RunningActions, &action->entry );
459 static UINT custom_get_process_return( HANDLE process )
463 GetExitCodeProcess( process, &rc );
465 return ERROR_FUNCTION_FAILED;
466 return ERROR_SUCCESS;
469 static UINT custom_get_thread_return( MSIPACKAGE *package, HANDLE thread )
473 GetExitCodeThread( thread, &rc );
477 case ERROR_FUNCTION_NOT_CALLED:
479 case ERROR_INSTALL_USEREXIT:
480 case ERROR_INSTALL_FAILURE:
482 case ERROR_NO_MORE_ITEMS:
483 return ERROR_SUCCESS;
484 case ERROR_INSTALL_SUSPEND:
485 ACTION_ForceReboot( package );
486 return ERROR_SUCCESS;
488 ERR("Invalid Return Code %d\n",rc);
489 return ERROR_INSTALL_FAILURE;
493 static UINT wait_process_handle(MSIPACKAGE* package, UINT type,
494 HANDLE ProcessHandle, LPCWSTR name)
496 UINT rc = ERROR_SUCCESS;
498 if (!(type & msidbCustomActionTypeAsync))
500 TRACE("waiting for %s\n", debugstr_w(name));
502 msi_dialog_check_messages(ProcessHandle);
504 if (!(type & msidbCustomActionTypeContinue))
505 rc = custom_get_process_return(ProcessHandle);
507 CloseHandle(ProcessHandle);
511 TRACE("%s running in background\n", debugstr_w(name));
513 if (!(type & msidbCustomActionTypeContinue))
514 file_running_action(package, ProcessHandle, TRUE, name);
516 CloseHandle(ProcessHandle);
522 typedef struct _msi_custom_action_info {
532 } msi_custom_action_info;
534 static void release_custom_action_data( msi_custom_action_info *info )
536 EnterCriticalSection( &msi_custom_action_cs );
540 list_remove( &info->entry );
542 CloseHandle( info->handle );
543 msi_free( info->action );
544 msi_free( info->source );
545 msi_free( info->target );
546 msiobj_release( &info->package->hdr );
550 LeaveCriticalSection( &msi_custom_action_cs );
553 /* must be called inside msi_custom_action_cs if info is in the pending custom actions list */
554 static void addref_custom_action_data( msi_custom_action_info *info )
559 static UINT wait_thread_handle( msi_custom_action_info *info )
561 UINT rc = ERROR_SUCCESS;
563 if (!(info->type & msidbCustomActionTypeAsync))
565 TRACE("waiting for %s\n", debugstr_w( info->action ));
567 msi_dialog_check_messages( info->handle );
569 if (!(info->type & msidbCustomActionTypeContinue))
570 rc = custom_get_thread_return( info->package, info->handle );
572 release_custom_action_data( info );
576 TRACE("%s running in background\n", debugstr_w( info->action ));
582 static msi_custom_action_info *find_action_by_guid( const GUID *guid )
584 msi_custom_action_info *info;
587 EnterCriticalSection( &msi_custom_action_cs );
589 LIST_FOR_EACH_ENTRY( info, &msi_pending_custom_actions, msi_custom_action_info, entry )
591 if (IsEqualGUID( &info->guid, guid ))
593 addref_custom_action_data( info );
599 LeaveCriticalSection( &msi_custom_action_cs );
607 static void handle_msi_break( LPCWSTR target )
612 static const WCHAR MsiBreak[] = { 'M','s','i','B','r','e','a','k',0 };
613 static const WCHAR WindowsInstaller[] = {
614 'W','i','n','d','o','w','s',' ','I','n','s','t','a','l','l','e','r',0
617 static const WCHAR format[] = {
618 'T','o',' ','d','e','b','u','g',' ','y','o','u','r',' ',
619 'c','u','s','t','o','m',' ','a','c','t','i','o','n',',',' ',
620 'a','t','t','a','c','h',' ','y','o','u','r',' ','d','e','b','u','g','g','e','r',' ',
621 't','o',' ','p','r','o','c','e','s','s',' ','%','i',' ','(','0','x','%','X',')',' ',
622 'a','n','d',' ','p','r','e','s','s',' ','O','K',0
625 if( !GetEnvironmentVariableW( MsiBreak, val, MAX_PATH ))
628 if( strcmpiW( val, target ))
631 msg = msi_alloc( (lstrlenW(format) + 10) * sizeof(WCHAR) );
635 wsprintfW( msg, format, GetCurrentProcessId(), GetCurrentProcessId());
636 MessageBoxW( NULL, msg, WindowsInstaller, MB_OK);
641 static UINT get_action_info( const GUID *guid, INT *type, MSIHANDLE *handle,
642 BSTR *dll, BSTR *funcname,
643 IWineMsiRemotePackage **package )
645 IClassFactory *cf = NULL;
646 IWineMsiRemoteCustomAction *rca = NULL;
649 r = DllGetClassObject( &CLSID_WineMsiRemoteCustomAction,
650 &IID_IClassFactory, (LPVOID *)&cf );
653 ERR("failed to get IClassFactory interface\n");
654 return ERROR_FUNCTION_FAILED;
657 r = IClassFactory_CreateInstance( cf, NULL, &IID_IWineMsiRemoteCustomAction, (LPVOID *)&rca );
660 ERR("failed to get IWineMsiRemoteCustomAction interface\n");
661 return ERROR_FUNCTION_FAILED;
664 r = IWineMsiRemoteCustomAction_GetActionInfo( rca, guid, type, handle, dll, funcname, package );
665 IWineMsiRemoteCustomAction_Release( rca );
668 ERR("GetActionInfo failed\n");
669 return ERROR_FUNCTION_FAILED;
672 return ERROR_SUCCESS;
676 extern UINT CUSTOMPROC_wrapper( MsiCustomActionEntryPoint proc, MSIHANDLE handle );
677 __ASM_GLOBAL_FUNC( CUSTOMPROC_wrapper,
679 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
680 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
682 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
684 "movl 8(%ebp),%eax\n\t"
687 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
688 __ASM_CFI(".cfi_same_value %ebp\n\t")
691 static inline UINT CUSTOMPROC_wrapper( MsiCustomActionEntryPoint proc, MSIHANDLE handle )
697 static DWORD ACTION_CallDllFunction( const GUID *guid )
699 MsiCustomActionEntryPoint fn;
700 MSIHANDLE hPackage, handle;
703 UINT r = ERROR_FUNCTION_FAILED;
704 BSTR dll = NULL, function = NULL;
706 IWineMsiRemotePackage *remote_package = NULL;
708 TRACE("%s\n", debugstr_guid( guid ));
710 r = get_action_info( guid, &type, &handle, &dll, &function, &remote_package );
711 if (r != ERROR_SUCCESS)
714 hModule = LoadLibraryW( dll );
717 ERR("failed to load dll %s (%u)\n", debugstr_w( dll ), GetLastError() );
721 proc = strdupWtoA( function );
722 fn = (MsiCustomActionEntryPoint) GetProcAddress( hModule, proc );
726 hPackage = alloc_msi_remote_handle( (IUnknown *)remote_package );
729 IWineMsiRemotePackage_SetMsiHandle( remote_package, handle );
730 TRACE("calling %s\n", debugstr_w( function ) );
731 handle_msi_break( function );
735 r = CUSTOMPROC_wrapper( fn, hPackage );
739 ERR("Custom action (%s:%s) caused a page fault: %08x\n",
740 debugstr_w(dll), debugstr_w(function), GetExceptionCode());
745 MsiCloseHandle( hPackage );
748 ERR("failed to create handle for %p\n", remote_package );
751 ERR("GetProcAddress(%s) failed\n", debugstr_w( function ) );
753 FreeLibrary(hModule);
755 IWineMsiRemotePackage_Release( remote_package );
756 SysFreeString( dll );
757 SysFreeString( function );
758 MsiCloseHandle( handle );
763 static DWORD WINAPI DllThread( LPVOID arg )
768 TRACE("custom action (%x) started\n", GetCurrentThreadId() );
770 rc = ACTION_CallDllFunction( guid );
772 TRACE("custom action (%x) returned %i\n", GetCurrentThreadId(), rc );
774 MsiCloseAllHandles();
778 static DWORD ACTION_CAInstallPackage(const GUID *guid)
780 msi_custom_action_info *info;
781 UINT r = ERROR_FUNCTION_FAILED;
782 INSTALLUILEVEL old_level;
784 info = find_action_by_guid(guid);
787 ERR("failed to find action %s\n", debugstr_guid(guid));
791 old_level = MsiSetInternalUI(INSTALLUILEVEL_BASIC, NULL);
792 r = MsiInstallProductW(info->source, info->target);
793 MsiSetInternalUI(old_level, NULL);
795 release_custom_action_data(info);
800 static DWORD WINAPI ConcurrentInstallThread(LPVOID arg)
805 TRACE("concurrent installation (%x) started\n", GetCurrentThreadId());
807 rc = ACTION_CAInstallPackage(guid);
809 TRACE("concurrent installation (%x) returned %i\n", GetCurrentThreadId(), rc);
811 MsiCloseAllHandles();
815 static msi_custom_action_info *do_msidbCustomActionTypeDll(
816 MSIPACKAGE *package, INT type, LPCWSTR source, LPCWSTR target, LPCWSTR action )
818 msi_custom_action_info *info;
820 info = msi_alloc( sizeof *info );
824 msiobj_addref( &package->hdr );
825 info->refs = 2; /* 1 for our caller and 1 for thread we created */
826 info->package = package;
828 info->target = strdupW( target );
829 info->source = strdupW( source );
830 info->action = strdupW( action );
831 CoCreateGuid( &info->guid );
833 EnterCriticalSection( &msi_custom_action_cs );
834 list_add_tail( &msi_pending_custom_actions, &info->entry );
835 LeaveCriticalSection( &msi_custom_action_cs );
837 info->handle = CreateThread( NULL, 0, DllThread, &info->guid, 0, NULL );
840 /* release both references */
841 release_custom_action_data( info );
842 release_custom_action_data( info );
849 static msi_custom_action_info *do_msidbCAConcurrentInstall(
850 MSIPACKAGE *package, INT type, LPCWSTR source, LPCWSTR target, LPCWSTR action)
852 msi_custom_action_info *info;
854 info = msi_alloc( sizeof *info );
858 msiobj_addref( &package->hdr );
859 info->refs = 2; /* 1 for our caller and 1 for thread we created */
860 info->package = package;
862 info->target = strdupW( target );
863 info->source = strdupW( source );
864 info->action = strdupW( action );
865 CoCreateGuid( &info->guid );
867 EnterCriticalSection( &msi_custom_action_cs );
868 list_add_tail( &msi_pending_custom_actions, &info->entry );
869 LeaveCriticalSection( &msi_custom_action_cs );
871 info->handle = CreateThread( NULL, 0, ConcurrentInstallThread, &info->guid, 0, NULL );
874 /* release both references */
875 release_custom_action_data( info );
876 release_custom_action_data( info );
883 static UINT HANDLE_CustomType23(MSIPACKAGE *package, LPCWSTR source,
884 LPCWSTR target, const INT type, LPCWSTR action)
886 msi_custom_action_info *info;
887 WCHAR package_path[MAX_PATH];
892 msi_get_property(package->db, cszSourceDir, package_path, &size);
893 lstrcatW(package_path, szBackSlash);
894 lstrcatW(package_path, source);
896 TRACE("Installing package %s concurrently\n", debugstr_w(package_path));
898 info = do_msidbCAConcurrentInstall(package, type, package_path, target, action);
900 r = wait_thread_handle(info);
901 release_custom_action_data( info );
905 static UINT HANDLE_CustomType1(MSIPACKAGE *package, LPCWSTR source,
906 LPCWSTR target, const INT type, LPCWSTR action)
908 msi_custom_action_info *info;
912 if (!(binary = get_temp_binary( package, source, TRUE )))
913 return ERROR_FUNCTION_FAILED;
915 TRACE("Calling function %s from %s\n", debugstr_w(target), debugstr_w(binary->tmpfile));
917 info = do_msidbCustomActionTypeDll( package, type, binary->tmpfile, target, action );
919 r = wait_thread_handle( info );
920 release_custom_action_data( info );
924 static UINT HANDLE_CustomType2(MSIPACKAGE *package, LPCWSTR source,
925 LPCWSTR target, const INT type, LPCWSTR action)
928 PROCESS_INFORMATION info;
931 WCHAR *deformated = NULL;
933 static const WCHAR spc[] = {' ',0};
937 memset(&si,0,sizeof(STARTUPINFOW));
939 if (!(binary = get_temp_binary( package, source, FALSE )))
940 return ERROR_FUNCTION_FAILED;
942 deformat_string(package,target,&deformated);
944 len = strlenW( binary->tmpfile ) + 2;
946 len += strlenW(deformated);
948 cmd = msi_alloc(sizeof(WCHAR)*len);
950 strcpyW( cmd, binary->tmpfile );
954 strcatW(cmd,deformated);
955 msi_free(deformated);
958 TRACE("executing exe %s\n", debugstr_w(cmd));
960 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
961 c_collen, &si, &info);
966 ERR("Unable to execute command %s\n", debugstr_w(cmd));
967 return ERROR_SUCCESS;
969 CloseHandle( info.hThread );
971 r = wait_process_handle(package, type, info.hProcess, action);
976 static UINT HANDLE_CustomType17(MSIPACKAGE *package, LPCWSTR source,
977 LPCWSTR target, const INT type, LPCWSTR action)
979 msi_custom_action_info *info;
983 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
985 file = get_loaded_file( package, source );
988 ERR("invalid file key %s\n", debugstr_w( source ));
989 return ERROR_FUNCTION_FAILED;
992 info = do_msidbCustomActionTypeDll( package, type, file->TargetPath, target, action );
994 r = wait_thread_handle( info );
995 release_custom_action_data( info );
999 static UINT HANDLE_CustomType18(MSIPACKAGE *package, LPCWSTR source,
1000 LPCWSTR target, const INT type, LPCWSTR action)
1003 PROCESS_INFORMATION info;
1008 static const WCHAR spc[] = {' ',0};
1011 memset(&si,0,sizeof(STARTUPINFOW));
1013 file = get_loaded_file(package,source);
1015 return ERROR_FUNCTION_FAILED;
1017 len = lstrlenW( file->TargetPath );
1019 deformat_string(package,target,&deformated);
1021 len += strlenW(deformated);
1024 cmd = msi_alloc(len * sizeof(WCHAR));
1026 lstrcpyW( cmd, file->TargetPath);
1030 strcatW(cmd, deformated);
1032 msi_free(deformated);
1035 TRACE("executing exe %s\n", debugstr_w(cmd));
1037 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
1038 c_collen, &si, &info);
1042 ERR("Unable to execute command %s\n", debugstr_w(cmd));
1044 return ERROR_SUCCESS;
1047 CloseHandle( info.hThread );
1049 return wait_process_handle(package, type, info.hProcess, action);
1052 static UINT HANDLE_CustomType19(MSIPACKAGE *package, LPCWSTR source,
1053 LPCWSTR target, const INT type, LPCWSTR action)
1055 static const WCHAR query[] = {
1056 'S','E','L','E','C','T',' ','`','M','e','s','s','a','g','e','`',' ',
1057 'F','R','O','M',' ','`','E','r','r','o','r','`',' ',
1058 'W','H','E','R','E',' ','`','E','r','r','o','r','`',' ','=',' ',
1062 LPWSTR deformated = NULL;
1064 deformat_string( package, target, &deformated );
1066 /* first try treat the error as a number */
1067 row = MSI_QueryGetRecord( package->db, query, deformated );
1070 LPCWSTR error = MSI_RecordGetString( row, 1 );
1071 if ((gUILevel & INSTALLUILEVEL_MASK) != INSTALLUILEVEL_NONE)
1072 MessageBoxW( NULL, error, NULL, MB_OK );
1073 msiobj_release( &row->hdr );
1075 else if ((gUILevel & INSTALLUILEVEL_MASK) != INSTALLUILEVEL_NONE)
1076 MessageBoxW( NULL, deformated, NULL, MB_OK );
1078 msi_free( deformated );
1080 return ERROR_INSTALL_FAILURE;
1083 static UINT HANDLE_CustomType50(MSIPACKAGE *package, LPCWSTR source,
1084 LPCWSTR target, const INT type, LPCWSTR action)
1087 PROCESS_INFORMATION info;
1093 static const WCHAR spc[] = {' ',0};
1095 memset(&si,0,sizeof(STARTUPINFOW));
1096 memset(&info,0,sizeof(PROCESS_INFORMATION));
1098 prop = msi_dup_property( package->db, source );
1100 return ERROR_SUCCESS;
1102 deformat_string(package,target,&deformated);
1103 len = strlenW(prop) + 2;
1105 len += strlenW(deformated);
1107 cmd = msi_alloc(sizeof(WCHAR)*len);
1113 strcatW(cmd,deformated);
1115 msi_free(deformated);
1119 TRACE("executing exe %s\n", debugstr_w(cmd));
1121 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
1122 c_collen, &si, &info);
1126 ERR("Unable to execute command %s\n", debugstr_w(cmd));
1128 return ERROR_SUCCESS;
1132 CloseHandle( info.hThread );
1134 return wait_process_handle(package, type, info.hProcess, action);
1137 static UINT HANDLE_CustomType34(MSIPACKAGE *package, LPCWSTR source,
1138 LPCWSTR target, const INT type, LPCWSTR action)
1140 LPWSTR workingdir, filename;
1142 PROCESS_INFORMATION info;
1145 memset(&si, 0, sizeof(STARTUPINFOW));
1147 workingdir = resolve_folder(package, source, FALSE, FALSE, TRUE, NULL);
1150 return ERROR_FUNCTION_FAILED;
1152 deformat_string(package, target, &filename);
1156 msi_free(workingdir);
1157 return ERROR_FUNCTION_FAILED;
1160 TRACE("executing exe %s with working directory %s\n",
1161 debugstr_w(filename), debugstr_w(workingdir));
1163 rc = CreateProcessW(NULL, filename, NULL, NULL, FALSE, 0, NULL,
1164 workingdir, &si, &info);
1168 ERR("Unable to execute command %s with working directory %s\n",
1169 debugstr_w(filename), debugstr_w(workingdir));
1171 msi_free(workingdir);
1172 return ERROR_SUCCESS;
1176 msi_free(workingdir);
1178 CloseHandle( info.hThread );
1180 return wait_process_handle(package, type, info.hProcess, action);
1183 static DWORD ACTION_CallScript( const GUID *guid )
1185 msi_custom_action_info *info;
1187 UINT r = ERROR_FUNCTION_FAILED;
1189 info = find_action_by_guid( guid );
1192 ERR("failed to find action %s\n", debugstr_guid( guid) );
1196 TRACE("function %s, script %s\n", debugstr_w( info->target ), debugstr_w( info->source ) );
1198 hPackage = alloc_msihandle( &info->package->hdr );
1201 r = call_script( hPackage, info->type, info->source, info->target, info->action );
1202 MsiCloseHandle( hPackage );
1205 ERR("failed to create handle for %p\n", info->package );
1207 release_custom_action_data( info );
1212 static DWORD WINAPI ScriptThread( LPVOID arg )
1217 TRACE("custom action (%x) started\n", GetCurrentThreadId() );
1219 rc = ACTION_CallScript( guid );
1221 TRACE("custom action (%x) returned %i\n", GetCurrentThreadId(), rc );
1223 MsiCloseAllHandles();
1227 static msi_custom_action_info *do_msidbCustomActionTypeScript(
1228 MSIPACKAGE *package, INT type, LPCWSTR script, LPCWSTR function, LPCWSTR action )
1230 msi_custom_action_info *info;
1232 info = msi_alloc( sizeof *info );
1236 msiobj_addref( &package->hdr );
1237 info->refs = 2; /* 1 for our caller and 1 for thread we created */
1238 info->package = package;
1240 info->target = strdupW( function );
1241 info->source = strdupW( script );
1242 info->action = strdupW( action );
1243 CoCreateGuid( &info->guid );
1245 EnterCriticalSection( &msi_custom_action_cs );
1246 list_add_tail( &msi_pending_custom_actions, &info->entry );
1247 LeaveCriticalSection( &msi_custom_action_cs );
1249 info->handle = CreateThread( NULL, 0, ScriptThread, &info->guid, 0, NULL );
1252 /* release both references */
1253 release_custom_action_data( info );
1254 release_custom_action_data( info );
1261 static UINT HANDLE_CustomType37_38(MSIPACKAGE *package, LPCWSTR source,
1262 LPCWSTR target, const INT type, LPCWSTR action)
1265 msi_custom_action_info *info;
1267 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1269 info = do_msidbCustomActionTypeScript( package, type, target, NULL, action );
1271 r = wait_thread_handle( info );
1272 release_custom_action_data( info );
1276 static UINT HANDLE_CustomType5_6(MSIPACKAGE *package, LPCWSTR source,
1277 LPCWSTR target, const INT type, LPCWSTR action)
1279 static const WCHAR query[] = {
1280 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1281 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',' ',
1282 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
1284 msi_custom_action_info *info;
1285 CHAR *buffer = NULL;
1286 WCHAR *bufferw = NULL;
1290 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1292 row = MSI_QueryGetRecord(package->db, query, source);
1294 return ERROR_FUNCTION_FAILED;
1296 r = MSI_RecordReadStream(row, 2, NULL, &sz);
1297 if (r != ERROR_SUCCESS)
1300 buffer = msi_alloc(sizeof(CHAR)*(sz+1));
1302 return ERROR_FUNCTION_FAILED;
1304 r = MSI_RecordReadStream(row, 2, buffer, &sz);
1305 if (r != ERROR_SUCCESS)
1309 bufferw = strdupAtoW(buffer);
1312 r = ERROR_FUNCTION_FAILED;
1316 info = do_msidbCustomActionTypeScript( package, type, bufferw, target, action );
1317 r = wait_thread_handle( info );
1318 release_custom_action_data( info );
1326 static UINT HANDLE_CustomType21_22(MSIPACKAGE *package, LPCWSTR source,
1327 LPCWSTR target, const INT type, LPCWSTR action)
1329 msi_custom_action_info *info;
1332 DWORD sz, szHighWord = 0, read;
1334 WCHAR *bufferw=NULL;
1338 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1340 file = get_loaded_file(package,source);
1343 ERR("invalid file key %s\n", debugstr_w(source));
1344 return ERROR_FUNCTION_FAILED;
1347 hFile = CreateFileW(file->TargetPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
1348 if (hFile == INVALID_HANDLE_VALUE)
1349 return ERROR_FUNCTION_FAILED;
1351 sz = GetFileSize(hFile, &szHighWord);
1352 if (sz == INVALID_FILE_SIZE || szHighWord != 0)
1355 return ERROR_FUNCTION_FAILED;
1358 buffer = msi_alloc(sizeof(CHAR)*(sz+1));
1362 return ERROR_FUNCTION_FAILED;
1365 bRet = ReadFile(hFile, buffer, sz, &read, NULL);
1369 r = ERROR_FUNCTION_FAILED;
1374 bufferw = strdupAtoW(buffer);
1377 r = ERROR_FUNCTION_FAILED;
1381 info = do_msidbCustomActionTypeScript( package, type, bufferw, target, action );
1382 r = wait_thread_handle( info );
1383 release_custom_action_data( info );
1391 static UINT HANDLE_CustomType53_54(MSIPACKAGE *package, LPCWSTR source,
1392 LPCWSTR target, const INT type, LPCWSTR action)
1394 msi_custom_action_info *info;
1398 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1400 prop = msi_dup_property( package->db, source );
1402 return ERROR_SUCCESS;
1404 info = do_msidbCustomActionTypeScript( package, type, prop, NULL, action );
1406 r = wait_thread_handle( info );
1407 release_custom_action_data( info );
1411 void ACTION_FinishCustomActions(const MSIPACKAGE* package)
1414 HANDLE *wait_handles;
1415 unsigned int handle_count, i;
1416 msi_custom_action_info *info, *cursor;
1418 while ((item = list_head( &package->RunningActions )))
1420 MSIRUNNINGACTION *action = LIST_ENTRY( item, MSIRUNNINGACTION, entry );
1422 list_remove( &action->entry );
1424 TRACE("waiting for %s\n", debugstr_w( action->name ) );
1425 msi_dialog_check_messages( action->handle );
1427 CloseHandle( action->handle );
1428 msi_free( action->name );
1432 EnterCriticalSection( &msi_custom_action_cs );
1434 handle_count = list_count( &msi_pending_custom_actions );
1435 wait_handles = msi_alloc( handle_count * sizeof(HANDLE) );
1438 LIST_FOR_EACH_ENTRY_SAFE( info, cursor, &msi_pending_custom_actions, msi_custom_action_info, entry )
1440 if (info->package == package )
1442 if (DuplicateHandle(GetCurrentProcess(), info->handle, GetCurrentProcess(), &wait_handles[handle_count], SYNCHRONIZE, FALSE, 0))
1447 LeaveCriticalSection( &msi_custom_action_cs );
1449 for (i = 0; i < handle_count; i++)
1451 msi_dialog_check_messages( wait_handles[i] );
1452 CloseHandle( wait_handles[i] );
1455 msi_free( wait_handles );
1458 typedef struct _msi_custom_remote_impl {
1459 IWineMsiRemoteCustomAction IWineMsiRemoteCustomAction_iface;
1461 } msi_custom_remote_impl;
1463 static inline msi_custom_remote_impl *impl_from_IWineMsiRemoteCustomAction( IWineMsiRemoteCustomAction *iface )
1465 return CONTAINING_RECORD(iface, msi_custom_remote_impl, IWineMsiRemoteCustomAction_iface);
1468 static HRESULT WINAPI mcr_QueryInterface( IWineMsiRemoteCustomAction *iface,
1469 REFIID riid,LPVOID *ppobj)
1471 if( IsEqualCLSID( riid, &IID_IUnknown ) ||
1472 IsEqualCLSID( riid, &IID_IWineMsiRemoteCustomAction ) )
1474 IUnknown_AddRef( iface );
1479 return E_NOINTERFACE;
1482 static ULONG WINAPI mcr_AddRef( IWineMsiRemoteCustomAction *iface )
1484 msi_custom_remote_impl* This = impl_from_IWineMsiRemoteCustomAction( iface );
1486 return InterlockedIncrement( &This->refs );
1489 static ULONG WINAPI mcr_Release( IWineMsiRemoteCustomAction *iface )
1491 msi_custom_remote_impl* This = impl_from_IWineMsiRemoteCustomAction( iface );
1494 r = InterlockedDecrement( &This->refs );
1500 static HRESULT WINAPI mcr_GetActionInfo( IWineMsiRemoteCustomAction *iface, LPCGUID custom_action_guid,
1501 INT *type, MSIHANDLE *handle, BSTR *dll, BSTR *func, IWineMsiRemotePackage **remote_package )
1503 msi_custom_action_info *info;
1505 info = find_action_by_guid( custom_action_guid );
1510 *handle = alloc_msihandle( &info->package->hdr );
1511 *dll = SysAllocString( info->source );
1512 *func = SysAllocString( info->target );
1514 release_custom_action_data( info );
1515 return create_msi_remote_package( NULL, (LPVOID *)remote_package );
1518 static const IWineMsiRemoteCustomActionVtbl msi_custom_remote_vtbl =
1526 HRESULT create_msi_custom_remote( IUnknown *pOuter, LPVOID *ppObj )
1528 msi_custom_remote_impl* This;
1530 This = msi_alloc( sizeof *This );
1532 return E_OUTOFMEMORY;
1534 This->IWineMsiRemoteCustomAction_iface.lpVtbl = &msi_custom_remote_vtbl;