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\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);
277 if (type & msidbCustomActionTypeRollback)
279 FIXME("Rollbacks not supported yet\n");
284 else if (!check_execution_scheduling_options(package,action,type))
290 switch (type & CUSTOM_ACTION_TYPE_MASK)
292 case 1: /* DLL file stored in a Binary table stream */
293 rc = HANDLE_CustomType1(package,source,target,type,action);
295 case 2: /* EXE file stored in a Binary table stream */
296 rc = HANDLE_CustomType2(package,source,target,type,action);
298 case 18: /*EXE file installed with package */
299 rc = HANDLE_CustomType18(package,source,target,type,action);
301 case 19: /* Error that halts install */
302 rc = HANDLE_CustomType19(package,source,target,type,action);
305 rc = HANDLE_CustomType17(package,source,target,type,action);
307 case 23: /* installs another package in the source tree */
308 deformat_string(package,target,&deformated);
309 rc = HANDLE_CustomType23(package,source,deformated,type,action);
310 msi_free(deformated);
312 case 50: /*EXE file specified by a property value */
313 rc = HANDLE_CustomType50(package,source,target,type,action);
315 case 34: /*EXE to be run in specified directory */
316 rc = HANDLE_CustomType34(package,source,target,type,action);
318 case 35: /* Directory set with formatted text. */
319 deformat_string(package,target,&deformated);
320 MSI_SetTargetPathW(package, source, deformated);
321 msi_free(deformated);
323 case 51: /* Property set with formatted text. */
327 deformat_string(package,target,&deformated);
328 rc = msi_set_property( package->db, source, deformated );
329 if (rc == ERROR_SUCCESS && !strcmpW( source, cszSourceDir ))
330 msi_reset_folders( package, TRUE );
331 msi_free(deformated);
333 case 37: /* JScript/VBScript text stored in target column. */
335 rc = HANDLE_CustomType37_38(package,source,target,type,action);
338 case 6: /* JScript/VBScript file stored in a Binary table stream. */
339 rc = HANDLE_CustomType5_6(package,source,target,type,action);
341 case 21: /* JScript/VBScript file installed with the product. */
343 rc = HANDLE_CustomType21_22(package,source,target,type,action);
345 case 53: /* JScript/VBScript text specified by a property value. */
347 rc = HANDLE_CustomType53_54(package,source,target,type,action);
350 FIXME("UNHANDLED ACTION TYPE %i (%s %s)\n",
351 type & CUSTOM_ACTION_TYPE_MASK, debugstr_w(source),
356 package->scheduled_action_running = FALSE;
357 package->commit_action_running = FALSE;
358 package->rollback_action_running = FALSE;
359 msi_free(action_copy);
360 msiobj_release(&row->hdr);
364 static MSIBINARY *create_temp_binary( MSIPACKAGE *package, LPCWSTR source, BOOL dll )
366 static const WCHAR query[] = {
367 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
368 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',' ',
369 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
374 WCHAR fmt[MAX_PATH], tmpfile[MAX_PATH];
375 DWORD sz = MAX_PATH, write;
378 if (msi_get_property(package->db, cszTempFolder, fmt, &sz) != ERROR_SUCCESS)
379 GetTempPathW(MAX_PATH, fmt);
381 if (!GetTempFileNameW( fmt, szMsi, 0, tmpfile ))
383 TRACE("unable to create temp file %s (%u)\n", debugstr_w(tmpfile), GetLastError());
387 row = MSI_QueryGetRecord(package->db, query, source);
391 if (!(binary = msi_alloc_zero( sizeof(MSIBINARY) )))
393 msiobj_release( &row->hdr );
396 file = CreateFileW( tmpfile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
397 if (file == INVALID_HANDLE_VALUE)
399 msiobj_release( &row->hdr );
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);
416 msiobj_release( &row->hdr );
417 if (r != ERROR_SUCCESS)
419 DeleteFileW( tmpfile );
424 /* keep a reference to prevent the dll from being unloaded */
425 if (dll && !(binary->module = LoadLibraryW( tmpfile )))
427 ERR("failed to load dll %s (%u)\n", debugstr_w( binary->tmpfile ), GetLastError() );
428 DeleteFileW( tmpfile );
432 binary->source = strdupW( source );
433 binary->tmpfile = strdupW( tmpfile );
434 list_add_tail( &package->binaries, &binary->entry );
438 static MSIBINARY *get_temp_binary( MSIPACKAGE *package, LPCWSTR source, BOOL dll )
442 LIST_FOR_EACH_ENTRY( binary, &package->binaries, MSIBINARY, entry )
444 if (!strcmpW( binary->source, source ))
448 return create_temp_binary( package, source, dll );
451 static void file_running_action(MSIPACKAGE* package, HANDLE Handle,
452 BOOL process, LPCWSTR name)
454 MSIRUNNINGACTION *action;
456 action = msi_alloc( sizeof(MSIRUNNINGACTION) );
458 action->handle = Handle;
459 action->process = process;
460 action->name = strdupW(name);
462 list_add_tail( &package->RunningActions, &action->entry );
465 static UINT custom_get_process_return( HANDLE process )
469 GetExitCodeProcess( process, &rc );
471 return ERROR_FUNCTION_FAILED;
472 return ERROR_SUCCESS;
475 static UINT custom_get_thread_return( MSIPACKAGE *package, HANDLE thread )
479 GetExitCodeThread( thread, &rc );
483 case ERROR_FUNCTION_NOT_CALLED:
485 case ERROR_INSTALL_USEREXIT:
486 case ERROR_INSTALL_FAILURE:
488 case ERROR_NO_MORE_ITEMS:
489 return ERROR_SUCCESS;
490 case ERROR_INSTALL_SUSPEND:
491 ACTION_ForceReboot( package );
492 return ERROR_SUCCESS;
494 ERR("Invalid Return Code %d\n",rc);
495 return ERROR_INSTALL_FAILURE;
499 static UINT wait_process_handle(MSIPACKAGE* package, UINT type,
500 HANDLE ProcessHandle, LPCWSTR name)
502 UINT rc = ERROR_SUCCESS;
504 if (!(type & msidbCustomActionTypeAsync))
506 TRACE("waiting for %s\n", debugstr_w(name));
508 msi_dialog_check_messages(ProcessHandle);
510 if (!(type & msidbCustomActionTypeContinue))
511 rc = custom_get_process_return(ProcessHandle);
513 CloseHandle(ProcessHandle);
517 TRACE("%s running in background\n", debugstr_w(name));
519 if (!(type & msidbCustomActionTypeContinue))
520 file_running_action(package, ProcessHandle, TRUE, name);
522 CloseHandle(ProcessHandle);
528 typedef struct _msi_custom_action_info {
538 } msi_custom_action_info;
540 static void release_custom_action_data( msi_custom_action_info *info )
542 EnterCriticalSection( &msi_custom_action_cs );
546 list_remove( &info->entry );
548 CloseHandle( info->handle );
549 msi_free( info->action );
550 msi_free( info->source );
551 msi_free( info->target );
552 msiobj_release( &info->package->hdr );
556 LeaveCriticalSection( &msi_custom_action_cs );
559 /* must be called inside msi_custom_action_cs if info is in the pending custom actions list */
560 static void addref_custom_action_data( msi_custom_action_info *info )
565 static UINT wait_thread_handle( msi_custom_action_info *info )
567 UINT rc = ERROR_SUCCESS;
569 if (!(info->type & msidbCustomActionTypeAsync))
571 TRACE("waiting for %s\n", debugstr_w( info->action ));
573 msi_dialog_check_messages( info->handle );
575 if (!(info->type & msidbCustomActionTypeContinue))
576 rc = custom_get_thread_return( info->package, info->handle );
578 release_custom_action_data( info );
582 TRACE("%s running in background\n", debugstr_w( info->action ));
588 static msi_custom_action_info *find_action_by_guid( const GUID *guid )
590 msi_custom_action_info *info;
593 EnterCriticalSection( &msi_custom_action_cs );
595 LIST_FOR_EACH_ENTRY( info, &msi_pending_custom_actions, msi_custom_action_info, entry )
597 if (IsEqualGUID( &info->guid, guid ))
599 addref_custom_action_data( info );
605 LeaveCriticalSection( &msi_custom_action_cs );
613 static void handle_msi_break( LPCWSTR target )
618 static const WCHAR MsiBreak[] = { 'M','s','i','B','r','e','a','k',0 };
619 static const WCHAR WindowsInstaller[] = {
620 'W','i','n','d','o','w','s',' ','I','n','s','t','a','l','l','e','r',0
623 static const WCHAR format[] = {
624 'T','o',' ','d','e','b','u','g',' ','y','o','u','r',' ',
625 'c','u','s','t','o','m',' ','a','c','t','i','o','n',',',' ',
626 'a','t','t','a','c','h',' ','y','o','u','r',' ','d','e','b','u','g','g','e','r',' ',
627 't','o',' ','p','r','o','c','e','s','s',' ','%','i',' ','(','0','x','%','X',')',' ',
628 'a','n','d',' ','p','r','e','s','s',' ','O','K',0
631 if( !GetEnvironmentVariableW( MsiBreak, val, MAX_PATH ))
634 if( strcmpiW( val, target ))
637 msg = msi_alloc( (lstrlenW(format) + 10) * sizeof(WCHAR) );
641 wsprintfW( msg, format, GetCurrentProcessId(), GetCurrentProcessId());
642 MessageBoxW( NULL, msg, WindowsInstaller, MB_OK);
647 static UINT get_action_info( const GUID *guid, INT *type, MSIHANDLE *handle,
648 BSTR *dll, BSTR *funcname,
649 IWineMsiRemotePackage **package )
651 IClassFactory *cf = NULL;
652 IWineMsiRemoteCustomAction *rca = NULL;
655 r = DllGetClassObject( &CLSID_WineMsiRemoteCustomAction,
656 &IID_IClassFactory, (LPVOID *)&cf );
659 ERR("failed to get IClassFactory interface\n");
660 return ERROR_FUNCTION_FAILED;
663 r = IClassFactory_CreateInstance( cf, NULL, &IID_IWineMsiRemoteCustomAction, (LPVOID *)&rca );
666 ERR("failed to get IWineMsiRemoteCustomAction interface\n");
667 return ERROR_FUNCTION_FAILED;
670 r = IWineMsiRemoteCustomAction_GetActionInfo( rca, guid, type, handle, dll, funcname, package );
671 IWineMsiRemoteCustomAction_Release( rca );
674 ERR("GetActionInfo failed\n");
675 return ERROR_FUNCTION_FAILED;
678 return ERROR_SUCCESS;
682 extern UINT CUSTOMPROC_wrapper( MsiCustomActionEntryPoint proc, MSIHANDLE handle );
683 __ASM_GLOBAL_FUNC( CUSTOMPROC_wrapper,
685 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
686 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
688 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
690 "movl 8(%ebp),%eax\n\t"
693 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
694 __ASM_CFI(".cfi_same_value %ebp\n\t")
697 static inline UINT CUSTOMPROC_wrapper( MsiCustomActionEntryPoint proc, MSIHANDLE handle )
703 static DWORD ACTION_CallDllFunction( const GUID *guid )
705 MsiCustomActionEntryPoint fn;
706 MSIHANDLE hPackage, handle;
709 UINT r = ERROR_FUNCTION_FAILED;
710 BSTR dll = NULL, function = NULL;
712 IWineMsiRemotePackage *remote_package = NULL;
714 TRACE("%s\n", debugstr_guid( guid ));
716 r = get_action_info( guid, &type, &handle, &dll, &function, &remote_package );
717 if (r != ERROR_SUCCESS)
720 hModule = LoadLibraryW( dll );
723 ERR("failed to load dll %s (%u)\n", debugstr_w( dll ), GetLastError() );
727 proc = strdupWtoA( function );
728 fn = (MsiCustomActionEntryPoint) GetProcAddress( hModule, proc );
732 hPackage = alloc_msi_remote_handle( (IUnknown *)remote_package );
735 IWineMsiRemotePackage_SetMsiHandle( remote_package, handle );
736 TRACE("calling %s\n", debugstr_w( function ) );
737 handle_msi_break( function );
741 r = CUSTOMPROC_wrapper( fn, hPackage );
745 ERR("Custom action (%s:%s) caused a page fault: %08x\n",
746 debugstr_w(dll), debugstr_w(function), GetExceptionCode());
751 MsiCloseHandle( hPackage );
754 ERR("failed to create handle for %p\n", remote_package );
757 ERR("GetProcAddress(%s) failed\n", debugstr_w( function ) );
759 FreeLibrary(hModule);
761 IWineMsiRemotePackage_Release( remote_package );
762 SysFreeString( dll );
763 SysFreeString( function );
764 MsiCloseHandle( handle );
769 static DWORD WINAPI DllThread( LPVOID arg )
774 TRACE("custom action (%x) started\n", GetCurrentThreadId() );
776 rc = ACTION_CallDllFunction( guid );
778 TRACE("custom action (%x) returned %i\n", GetCurrentThreadId(), rc );
780 MsiCloseAllHandles();
784 static DWORD ACTION_CAInstallPackage(const GUID *guid)
786 msi_custom_action_info *info;
787 UINT r = ERROR_FUNCTION_FAILED;
788 INSTALLUILEVEL old_level;
790 info = find_action_by_guid(guid);
793 ERR("failed to find action %s\n", debugstr_guid(guid));
797 old_level = MsiSetInternalUI(INSTALLUILEVEL_BASIC, NULL);
798 r = MsiInstallProductW(info->source, info->target);
799 MsiSetInternalUI(old_level, NULL);
801 release_custom_action_data(info);
806 static DWORD WINAPI ConcurrentInstallThread(LPVOID arg)
811 TRACE("concurrent installation (%x) started\n", GetCurrentThreadId());
813 rc = ACTION_CAInstallPackage(guid);
815 TRACE("concurrent installation (%x) returned %i\n", GetCurrentThreadId(), rc);
817 MsiCloseAllHandles();
821 static msi_custom_action_info *do_msidbCustomActionTypeDll(
822 MSIPACKAGE *package, INT type, LPCWSTR source, LPCWSTR target, LPCWSTR action )
824 msi_custom_action_info *info;
826 info = msi_alloc( sizeof *info );
830 msiobj_addref( &package->hdr );
831 info->refs = 2; /* 1 for our caller and 1 for thread we created */
832 info->package = package;
834 info->target = strdupW( target );
835 info->source = strdupW( source );
836 info->action = strdupW( action );
837 CoCreateGuid( &info->guid );
839 EnterCriticalSection( &msi_custom_action_cs );
840 list_add_tail( &msi_pending_custom_actions, &info->entry );
841 LeaveCriticalSection( &msi_custom_action_cs );
843 info->handle = CreateThread( NULL, 0, DllThread, &info->guid, 0, NULL );
846 /* release both references */
847 release_custom_action_data( info );
848 release_custom_action_data( info );
855 static msi_custom_action_info *do_msidbCAConcurrentInstall(
856 MSIPACKAGE *package, INT type, LPCWSTR source, LPCWSTR target, LPCWSTR action)
858 msi_custom_action_info *info;
860 info = msi_alloc( sizeof *info );
864 msiobj_addref( &package->hdr );
865 info->refs = 2; /* 1 for our caller and 1 for thread we created */
866 info->package = package;
868 info->target = strdupW( target );
869 info->source = strdupW( source );
870 info->action = strdupW( action );
871 CoCreateGuid( &info->guid );
873 EnterCriticalSection( &msi_custom_action_cs );
874 list_add_tail( &msi_pending_custom_actions, &info->entry );
875 LeaveCriticalSection( &msi_custom_action_cs );
877 info->handle = CreateThread( NULL, 0, ConcurrentInstallThread, &info->guid, 0, NULL );
880 /* release both references */
881 release_custom_action_data( info );
882 release_custom_action_data( info );
889 static UINT HANDLE_CustomType23(MSIPACKAGE *package, LPCWSTR source,
890 LPCWSTR target, const INT type, LPCWSTR action)
892 msi_custom_action_info *info;
893 WCHAR package_path[MAX_PATH];
898 msi_get_property(package->db, cszSourceDir, package_path, &size);
899 lstrcatW(package_path, szBackSlash);
900 lstrcatW(package_path, source);
902 TRACE("Installing package %s concurrently\n", debugstr_w(package_path));
904 info = do_msidbCAConcurrentInstall(package, type, package_path, target, action);
906 r = wait_thread_handle(info);
907 release_custom_action_data( info );
911 static UINT HANDLE_CustomType1(MSIPACKAGE *package, LPCWSTR source,
912 LPCWSTR target, const INT type, LPCWSTR action)
914 msi_custom_action_info *info;
918 if (!(binary = get_temp_binary( package, source, TRUE )))
919 return ERROR_FUNCTION_FAILED;
921 TRACE("Calling function %s from %s\n", debugstr_w(target), debugstr_w(binary->tmpfile));
923 info = do_msidbCustomActionTypeDll( package, type, binary->tmpfile, target, action );
925 r = wait_thread_handle( info );
926 release_custom_action_data( info );
930 static UINT HANDLE_CustomType2(MSIPACKAGE *package, LPCWSTR source,
931 LPCWSTR target, const INT type, LPCWSTR action)
934 PROCESS_INFORMATION info;
937 WCHAR *deformated = NULL;
939 static const WCHAR spc[] = {' ',0};
943 memset(&si,0,sizeof(STARTUPINFOW));
945 if (!(binary = get_temp_binary( package, source, FALSE )))
946 return ERROR_FUNCTION_FAILED;
948 deformat_string(package,target,&deformated);
950 len = strlenW( binary->tmpfile ) + 2;
952 len += strlenW(deformated);
954 cmd = msi_alloc(sizeof(WCHAR)*len);
956 strcpyW( cmd, binary->tmpfile );
960 strcatW(cmd,deformated);
961 msi_free(deformated);
964 TRACE("executing exe %s\n", debugstr_w(cmd));
966 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
967 c_collen, &si, &info);
972 ERR("Unable to execute command %s\n", debugstr_w(cmd));
973 return ERROR_SUCCESS;
975 CloseHandle( info.hThread );
977 r = wait_process_handle(package, type, info.hProcess, action);
982 static UINT HANDLE_CustomType17(MSIPACKAGE *package, LPCWSTR source,
983 LPCWSTR target, const INT type, LPCWSTR action)
985 msi_custom_action_info *info;
989 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
991 file = get_loaded_file( package, source );
994 ERR("invalid file key %s\n", debugstr_w( source ));
995 return ERROR_FUNCTION_FAILED;
998 info = do_msidbCustomActionTypeDll( package, type, file->TargetPath, target, action );
1000 r = wait_thread_handle( info );
1001 release_custom_action_data( info );
1005 static UINT HANDLE_CustomType18(MSIPACKAGE *package, LPCWSTR source,
1006 LPCWSTR target, const INT type, LPCWSTR action)
1009 PROCESS_INFORMATION info;
1014 static const WCHAR spc[] = {' ',0};
1017 memset(&si,0,sizeof(STARTUPINFOW));
1019 file = get_loaded_file(package,source);
1021 return ERROR_FUNCTION_FAILED;
1023 len = lstrlenW( file->TargetPath );
1025 deformat_string(package,target,&deformated);
1027 len += strlenW(deformated);
1030 cmd = msi_alloc(len * sizeof(WCHAR));
1032 lstrcpyW( cmd, file->TargetPath);
1036 strcatW(cmd, deformated);
1038 msi_free(deformated);
1041 TRACE("executing exe %s\n", debugstr_w(cmd));
1043 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
1044 c_collen, &si, &info);
1048 ERR("Unable to execute command %s\n", debugstr_w(cmd));
1050 return ERROR_SUCCESS;
1053 CloseHandle( info.hThread );
1055 return wait_process_handle(package, type, info.hProcess, action);
1058 static UINT HANDLE_CustomType19(MSIPACKAGE *package, LPCWSTR source,
1059 LPCWSTR target, const INT type, LPCWSTR action)
1061 static const WCHAR query[] = {
1062 'S','E','L','E','C','T',' ','`','M','e','s','s','a','g','e','`',' ',
1063 'F','R','O','M',' ','`','E','r','r','o','r','`',' ',
1064 'W','H','E','R','E',' ','`','E','r','r','o','r','`',' ','=',' ',
1068 LPWSTR deformated = NULL;
1070 deformat_string( package, target, &deformated );
1072 /* first try treat the error as a number */
1073 row = MSI_QueryGetRecord( package->db, query, deformated );
1076 LPCWSTR error = MSI_RecordGetString( row, 1 );
1077 if ((gUILevel & INSTALLUILEVEL_MASK) != INSTALLUILEVEL_NONE)
1078 MessageBoxW( NULL, error, NULL, MB_OK );
1079 msiobj_release( &row->hdr );
1081 else if ((gUILevel & INSTALLUILEVEL_MASK) != INSTALLUILEVEL_NONE)
1082 MessageBoxW( NULL, deformated, NULL, MB_OK );
1084 msi_free( deformated );
1086 return ERROR_INSTALL_FAILURE;
1089 static UINT HANDLE_CustomType50(MSIPACKAGE *package, LPCWSTR source,
1090 LPCWSTR target, const INT type, LPCWSTR action)
1093 PROCESS_INFORMATION info;
1099 static const WCHAR spc[] = {' ',0};
1101 memset(&si,0,sizeof(STARTUPINFOW));
1102 memset(&info,0,sizeof(PROCESS_INFORMATION));
1104 prop = msi_dup_property( package->db, source );
1106 return ERROR_SUCCESS;
1108 deformat_string(package,target,&deformated);
1109 len = strlenW(prop) + 2;
1111 len += strlenW(deformated);
1113 cmd = msi_alloc(sizeof(WCHAR)*len);
1119 strcatW(cmd,deformated);
1121 msi_free(deformated);
1125 TRACE("executing exe %s\n", debugstr_w(cmd));
1127 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
1128 c_collen, &si, &info);
1132 ERR("Unable to execute command %s\n", debugstr_w(cmd));
1134 return ERROR_SUCCESS;
1138 CloseHandle( info.hThread );
1140 return wait_process_handle(package, type, info.hProcess, action);
1143 static UINT HANDLE_CustomType34(MSIPACKAGE *package, LPCWSTR source,
1144 LPCWSTR target, const INT type, LPCWSTR action)
1146 LPWSTR workingdir, filename;
1148 PROCESS_INFORMATION info;
1151 memset(&si, 0, sizeof(STARTUPINFOW));
1153 workingdir = resolve_folder(package, source, FALSE, FALSE, TRUE, NULL);
1156 return ERROR_FUNCTION_FAILED;
1158 deformat_string(package, target, &filename);
1162 msi_free(workingdir);
1163 return ERROR_FUNCTION_FAILED;
1166 TRACE("executing exe %s with working directory %s\n",
1167 debugstr_w(filename), debugstr_w(workingdir));
1169 rc = CreateProcessW(NULL, filename, NULL, NULL, FALSE, 0, NULL,
1170 workingdir, &si, &info);
1174 ERR("Unable to execute command %s with working directory %s\n",
1175 debugstr_w(filename), debugstr_w(workingdir));
1177 msi_free(workingdir);
1178 return ERROR_SUCCESS;
1182 msi_free(workingdir);
1184 CloseHandle( info.hThread );
1186 return wait_process_handle(package, type, info.hProcess, action);
1189 static DWORD ACTION_CallScript( const GUID *guid )
1191 msi_custom_action_info *info;
1195 info = find_action_by_guid( guid );
1198 ERR("failed to find action %s\n", debugstr_guid( guid) );
1199 return ERROR_FUNCTION_FAILED;
1202 TRACE("function %s, script %s\n", debugstr_w( info->target ), debugstr_w( info->source ) );
1204 hPackage = alloc_msihandle( &info->package->hdr );
1207 r = call_script( hPackage, info->type, info->source, info->target, info->action );
1208 TRACE("script returned %u\n", r);
1209 MsiCloseHandle( hPackage );
1212 ERR("failed to create handle for %p\n", info->package );
1214 release_custom_action_data( info );
1218 static DWORD WINAPI ScriptThread( LPVOID arg )
1223 TRACE("custom action (%x) started\n", GetCurrentThreadId() );
1225 rc = ACTION_CallScript( guid );
1227 TRACE("custom action (%x) returned %i\n", GetCurrentThreadId(), rc );
1229 MsiCloseAllHandles();
1233 static msi_custom_action_info *do_msidbCustomActionTypeScript(
1234 MSIPACKAGE *package, INT type, LPCWSTR script, LPCWSTR function, LPCWSTR action )
1236 msi_custom_action_info *info;
1238 info = msi_alloc( sizeof *info );
1242 msiobj_addref( &package->hdr );
1243 info->refs = 2; /* 1 for our caller and 1 for thread we created */
1244 info->package = package;
1246 info->target = strdupW( function );
1247 info->source = strdupW( script );
1248 info->action = strdupW( action );
1249 CoCreateGuid( &info->guid );
1251 EnterCriticalSection( &msi_custom_action_cs );
1252 list_add_tail( &msi_pending_custom_actions, &info->entry );
1253 LeaveCriticalSection( &msi_custom_action_cs );
1255 info->handle = CreateThread( NULL, 0, ScriptThread, &info->guid, 0, NULL );
1258 /* release both references */
1259 release_custom_action_data( info );
1260 release_custom_action_data( info );
1267 static UINT HANDLE_CustomType37_38(MSIPACKAGE *package, LPCWSTR source,
1268 LPCWSTR target, const INT type, LPCWSTR action)
1271 msi_custom_action_info *info;
1273 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1275 info = do_msidbCustomActionTypeScript( package, type, target, NULL, action );
1277 r = wait_thread_handle( info );
1278 release_custom_action_data( info );
1282 static UINT HANDLE_CustomType5_6(MSIPACKAGE *package, LPCWSTR source,
1283 LPCWSTR target, const INT type, LPCWSTR action)
1285 static const WCHAR query[] = {
1286 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1287 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',' ',
1288 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
1290 msi_custom_action_info *info;
1291 CHAR *buffer = NULL;
1292 WCHAR *bufferw = NULL;
1296 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1298 row = MSI_QueryGetRecord(package->db, query, source);
1300 return ERROR_FUNCTION_FAILED;
1302 r = MSI_RecordReadStream(row, 2, NULL, &sz);
1303 if (r != ERROR_SUCCESS)
1306 buffer = msi_alloc(sizeof(CHAR)*(sz+1));
1308 return ERROR_FUNCTION_FAILED;
1310 r = MSI_RecordReadStream(row, 2, buffer, &sz);
1311 if (r != ERROR_SUCCESS)
1315 bufferw = strdupAtoW(buffer);
1318 r = ERROR_FUNCTION_FAILED;
1322 info = do_msidbCustomActionTypeScript( package, type, bufferw, target, action );
1323 r = wait_thread_handle( info );
1324 release_custom_action_data( info );
1332 static UINT HANDLE_CustomType21_22(MSIPACKAGE *package, LPCWSTR source,
1333 LPCWSTR target, const INT type, LPCWSTR action)
1335 msi_custom_action_info *info;
1338 DWORD sz, szHighWord = 0, read;
1340 WCHAR *bufferw=NULL;
1344 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1346 file = get_loaded_file(package,source);
1349 ERR("invalid file key %s\n", debugstr_w(source));
1350 return ERROR_FUNCTION_FAILED;
1353 hFile = CreateFileW(file->TargetPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
1354 if (hFile == INVALID_HANDLE_VALUE)
1355 return ERROR_FUNCTION_FAILED;
1357 sz = GetFileSize(hFile, &szHighWord);
1358 if (sz == INVALID_FILE_SIZE || szHighWord != 0)
1361 return ERROR_FUNCTION_FAILED;
1364 buffer = msi_alloc(sizeof(CHAR)*(sz+1));
1368 return ERROR_FUNCTION_FAILED;
1371 bRet = ReadFile(hFile, buffer, sz, &read, NULL);
1375 r = ERROR_FUNCTION_FAILED;
1380 bufferw = strdupAtoW(buffer);
1383 r = ERROR_FUNCTION_FAILED;
1387 info = do_msidbCustomActionTypeScript( package, type, bufferw, target, action );
1388 r = wait_thread_handle( info );
1389 release_custom_action_data( info );
1397 static UINT HANDLE_CustomType53_54(MSIPACKAGE *package, LPCWSTR source,
1398 LPCWSTR target, const INT type, LPCWSTR action)
1400 msi_custom_action_info *info;
1404 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1406 prop = msi_dup_property( package->db, source );
1408 return ERROR_SUCCESS;
1410 info = do_msidbCustomActionTypeScript( package, type, prop, NULL, action );
1412 r = wait_thread_handle( info );
1413 release_custom_action_data( info );
1417 void ACTION_FinishCustomActions(const MSIPACKAGE* package)
1420 HANDLE *wait_handles;
1421 unsigned int handle_count, i;
1422 msi_custom_action_info *info, *cursor;
1424 while ((item = list_head( &package->RunningActions )))
1426 MSIRUNNINGACTION *action = LIST_ENTRY( item, MSIRUNNINGACTION, entry );
1428 list_remove( &action->entry );
1430 TRACE("waiting for %s\n", debugstr_w( action->name ) );
1431 msi_dialog_check_messages( action->handle );
1433 CloseHandle( action->handle );
1434 msi_free( action->name );
1438 EnterCriticalSection( &msi_custom_action_cs );
1440 handle_count = list_count( &msi_pending_custom_actions );
1441 wait_handles = msi_alloc( handle_count * sizeof(HANDLE) );
1444 LIST_FOR_EACH_ENTRY_SAFE( info, cursor, &msi_pending_custom_actions, msi_custom_action_info, entry )
1446 if (info->package == package )
1448 if (DuplicateHandle(GetCurrentProcess(), info->handle, GetCurrentProcess(), &wait_handles[handle_count], SYNCHRONIZE, FALSE, 0))
1453 LeaveCriticalSection( &msi_custom_action_cs );
1455 for (i = 0; i < handle_count; i++)
1457 msi_dialog_check_messages( wait_handles[i] );
1458 CloseHandle( wait_handles[i] );
1461 msi_free( wait_handles );
1464 typedef struct _msi_custom_remote_impl {
1465 IWineMsiRemoteCustomAction IWineMsiRemoteCustomAction_iface;
1467 } msi_custom_remote_impl;
1469 static inline msi_custom_remote_impl *impl_from_IWineMsiRemoteCustomAction( IWineMsiRemoteCustomAction *iface )
1471 return CONTAINING_RECORD(iface, msi_custom_remote_impl, IWineMsiRemoteCustomAction_iface);
1474 static HRESULT WINAPI mcr_QueryInterface( IWineMsiRemoteCustomAction *iface,
1475 REFIID riid,LPVOID *ppobj)
1477 if( IsEqualCLSID( riid, &IID_IUnknown ) ||
1478 IsEqualCLSID( riid, &IID_IWineMsiRemoteCustomAction ) )
1480 IUnknown_AddRef( iface );
1485 return E_NOINTERFACE;
1488 static ULONG WINAPI mcr_AddRef( IWineMsiRemoteCustomAction *iface )
1490 msi_custom_remote_impl* This = impl_from_IWineMsiRemoteCustomAction( iface );
1492 return InterlockedIncrement( &This->refs );
1495 static ULONG WINAPI mcr_Release( IWineMsiRemoteCustomAction *iface )
1497 msi_custom_remote_impl* This = impl_from_IWineMsiRemoteCustomAction( iface );
1500 r = InterlockedDecrement( &This->refs );
1506 static HRESULT WINAPI mcr_GetActionInfo( IWineMsiRemoteCustomAction *iface, LPCGUID custom_action_guid,
1507 INT *type, MSIHANDLE *handle, BSTR *dll, BSTR *func, IWineMsiRemotePackage **remote_package )
1509 msi_custom_action_info *info;
1511 info = find_action_by_guid( custom_action_guid );
1516 *handle = alloc_msihandle( &info->package->hdr );
1517 *dll = SysAllocString( info->source );
1518 *func = SysAllocString( info->target );
1520 release_custom_action_data( info );
1521 return create_msi_remote_package( NULL, (LPVOID *)remote_package );
1524 static const IWineMsiRemoteCustomActionVtbl msi_custom_remote_vtbl =
1532 HRESULT create_msi_custom_remote( IUnknown *pOuter, LPVOID *ppObj )
1534 msi_custom_remote_impl* This;
1536 This = msi_alloc( sizeof *This );
1538 return E_OUTOFMEMORY;
1540 This->IWineMsiRemoteCustomAction_iface.lpVtbl = &msi_custom_remote_vtbl;