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
45 typedef struct tagMSIRUNNINGACTION
53 static UINT HANDLE_CustomType1(MSIPACKAGE *package, LPCWSTR source,
54 LPCWSTR target, const INT type, LPCWSTR action);
55 static UINT HANDLE_CustomType2(MSIPACKAGE *package, LPCWSTR source,
56 LPCWSTR target, const INT type, LPCWSTR action);
57 static UINT HANDLE_CustomType17(MSIPACKAGE *package, LPCWSTR source,
58 LPCWSTR target, const INT type, LPCWSTR action);
59 static UINT HANDLE_CustomType18(MSIPACKAGE *package, LPCWSTR source,
60 LPCWSTR target, const INT type, LPCWSTR action);
61 static UINT HANDLE_CustomType19(MSIPACKAGE *package, LPCWSTR source,
62 LPCWSTR target, const INT type, LPCWSTR action);
63 static UINT HANDLE_CustomType23(MSIPACKAGE *package, LPCWSTR source,
64 LPCWSTR target, const INT type, LPCWSTR action);
65 static UINT HANDLE_CustomType50(MSIPACKAGE *package, LPCWSTR source,
66 LPCWSTR target, const INT type, LPCWSTR action);
67 static UINT HANDLE_CustomType34(MSIPACKAGE *package, LPCWSTR source,
68 LPCWSTR target, const INT type, LPCWSTR action);
69 static UINT HANDLE_CustomType37_38(MSIPACKAGE *package, LPCWSTR source,
70 LPCWSTR target, const INT type, LPCWSTR action);
71 static UINT HANDLE_CustomType5_6(MSIPACKAGE *package, LPCWSTR source,
72 LPCWSTR target, const INT type, LPCWSTR action);
73 static UINT HANDLE_CustomType21_22(MSIPACKAGE *package, LPCWSTR source,
74 LPCWSTR target, const INT type, LPCWSTR action);
75 static UINT HANDLE_CustomType53_54(MSIPACKAGE *package, LPCWSTR source,
76 LPCWSTR target, const INT type, LPCWSTR action);
78 typedef UINT (WINAPI *MsiCustomActionEntryPoint)( MSIHANDLE );
80 static CRITICAL_SECTION msi_custom_action_cs;
81 static CRITICAL_SECTION_DEBUG msi_custom_action_cs_debug =
83 0, 0, &msi_custom_action_cs,
84 { &msi_custom_action_cs_debug.ProcessLocksList,
85 &msi_custom_action_cs_debug.ProcessLocksList },
86 0, 0, { (DWORD_PTR)(__FILE__ ": msi_custom_action_cs") }
88 static CRITICAL_SECTION msi_custom_action_cs = { &msi_custom_action_cs_debug, -1, 0, 0, 0, 0 };
90 static struct list msi_pending_custom_actions = LIST_INIT( msi_pending_custom_actions );
92 UINT msi_schedule_action( MSIPACKAGE *package, UINT script, const WCHAR *action )
95 WCHAR **newbuf = NULL;
97 if (script >= TOTAL_SCRIPTS)
99 FIXME("Unknown script requested %u\n", script);
100 return ERROR_FUNCTION_FAILED;
102 TRACE("Scheduling action %s in script %u\n", debugstr_w(action), script);
104 count = package->script->ActionCount[script];
105 package->script->ActionCount[script]++;
106 if (count != 0) newbuf = msi_realloc( package->script->Actions[script],
107 package->script->ActionCount[script] * sizeof(WCHAR *) );
108 else newbuf = msi_alloc( sizeof(WCHAR *) );
110 newbuf[count] = strdupW( action );
111 package->script->Actions[script] = newbuf;
112 return ERROR_SUCCESS;
115 UINT msi_register_unique_action( MSIPACKAGE *package, const WCHAR *action )
118 WCHAR **newbuf = NULL;
120 if (!package->script) return FALSE;
122 TRACE("Registering %s as unique action\n", debugstr_w(action));
124 count = package->script->UniqueActionsCount;
125 package->script->UniqueActionsCount++;
126 if (count != 0) newbuf = msi_realloc( package->script->UniqueActions,
127 package->script->UniqueActionsCount * sizeof(WCHAR *) );
128 else newbuf = msi_alloc( sizeof(WCHAR *) );
130 newbuf[count] = strdupW( action );
131 package->script->UniqueActions = newbuf;
132 return ERROR_SUCCESS;
135 BOOL msi_action_is_unique( const MSIPACKAGE *package, const WCHAR *action )
139 if (!package->script) return FALSE;
141 for (i = 0; i < package->script->UniqueActionsCount; i++)
143 if (!strcmpW( package->script->UniqueActions[i], action )) return TRUE;
148 static BOOL check_execution_scheduling_options(MSIPACKAGE *package, LPCWSTR action, UINT options)
150 if (!package->script)
153 if ((options & msidbCustomActionTypeClientRepeat) ==
154 msidbCustomActionTypeClientRepeat)
156 if (!(package->script->InWhatSequence & SEQUENCE_UI &&
157 package->script->InWhatSequence & SEQUENCE_EXEC))
159 TRACE("Skipping action due to dbCustomActionTypeClientRepeat option.\n");
163 else if (options & msidbCustomActionTypeFirstSequence)
165 if (package->script->InWhatSequence & SEQUENCE_UI &&
166 package->script->InWhatSequence & SEQUENCE_EXEC )
168 TRACE("Skipping action due to msidbCustomActionTypeFirstSequence option.\n");
172 else if (options & msidbCustomActionTypeOncePerProcess)
174 if (msi_action_is_unique(package, action))
176 TRACE("Skipping action due to msidbCustomActionTypeOncePerProcess option.\n");
180 msi_register_unique_action(package, action);
186 /* stores the following properties before the action:
188 * [CustomActionData<=>UserSID<=>ProductCode]Action
190 static LPWSTR msi_get_deferred_action(LPCWSTR action, LPCWSTR actiondata,
191 LPCWSTR usersid, LPCWSTR prodcode)
196 static const WCHAR format[] = {
197 '[','%','s','<','=','>','%','s','<','=','>','%','s',']','%','s',0
201 return strdupW(action);
203 len = lstrlenW(action) + lstrlenW(actiondata) +
204 lstrlenW(usersid) + lstrlenW(prodcode) +
205 lstrlenW(format) - 7;
206 deferred = msi_alloc(len * sizeof(WCHAR));
208 sprintfW(deferred, format, actiondata, usersid, prodcode, action);
212 static void set_deferred_action_props(MSIPACKAGE *package, LPWSTR deferred_data)
214 LPWSTR end, beg = deferred_data + 1;
216 static const WCHAR sep[] = {'<','=','>',0};
218 end = strstrW(beg, sep);
220 msi_set_property(package->db, szCustomActionData, beg);
223 end = strstrW(beg, sep);
225 msi_set_property(package->db, szUserSID, beg);
228 end = strchrW(beg, ']');
230 msi_set_property(package->db, szProductCode, beg);
233 UINT ACTION_CustomAction(MSIPACKAGE *package, LPCWSTR action, UINT script, BOOL execute)
235 UINT rc = ERROR_SUCCESS;
237 static const WCHAR ExecSeqQuery[] =
238 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
239 '`','C','u','s','t','o' ,'m','A','c','t','i','o','n','`',
240 ' ','W','H','E','R','E',' ','`','A','c','t','i' ,'o','n','`',' ',
241 '=',' ','\'','%','s','\'',0};
243 LPCWSTR source, target;
244 LPWSTR ptr, deferred_data = NULL;
245 LPWSTR action_copy = strdupW(action);
246 WCHAR *deformated=NULL;
248 /* deferred action: [properties]Action */
249 if ((ptr = strrchrW(action_copy, ']')))
251 deferred_data = action_copy;
255 row = MSI_QueryGetRecord( package->db, ExecSeqQuery, action );
258 msi_free(action_copy);
259 return ERROR_CALL_NOT_IMPLEMENTED;
262 type = MSI_RecordGetInteger(row,2);
264 source = MSI_RecordGetString(row,3);
265 target = MSI_RecordGetString(row,4);
267 TRACE("Handling custom action %s (%x %s %s)\n",debugstr_w(action),type,
268 debugstr_w(source), debugstr_w(target));
270 /* handle some of the deferred actions */
271 if (type & msidbCustomActionTypeTSAware)
272 FIXME("msidbCustomActionTypeTSAware not handled\n");
274 if (type & msidbCustomActionTypeInScript)
276 if (type & msidbCustomActionTypeNoImpersonate)
277 WARN("msidbCustomActionTypeNoImpersonate not handled\n");
281 LPWSTR actiondata = msi_dup_property(package->db, action);
282 LPWSTR usersid = msi_dup_property(package->db, szUserSID);
283 LPWSTR prodcode = msi_dup_property(package->db, szProductCode);
284 LPWSTR deferred = msi_get_deferred_action(action, actiondata, usersid, prodcode);
286 if (type & msidbCustomActionTypeCommit)
288 TRACE("Deferring commit action\n");
289 msi_schedule_action(package, COMMIT_SCRIPT, deferred);
291 else if (type & msidbCustomActionTypeRollback)
293 TRACE("Deferring rollback action\n");
294 msi_schedule_action(package, ROLLBACK_SCRIPT, deferred);
298 TRACE("Deferring action\n");
299 msi_schedule_action(package, INSTALL_SCRIPT, deferred);
303 msi_free(actiondata);
311 LPWSTR actiondata = msi_dup_property( package->db, action );
313 if (type & msidbCustomActionTypeInScript)
314 package->scheduled_action_running = TRUE;
316 if (type & msidbCustomActionTypeCommit)
317 package->commit_action_running = TRUE;
319 if (type & msidbCustomActionTypeRollback)
320 package->rollback_action_running = TRUE;
323 set_deferred_action_props(package, deferred_data);
325 msi_set_property(package->db, szCustomActionData, actiondata);
327 msi_set_property(package->db, szCustomActionData, szEmpty);
329 msi_free(actiondata);
332 else if (!check_execution_scheduling_options(package,action,type))
338 switch (type & CUSTOM_ACTION_TYPE_MASK)
340 case 1: /* DLL file stored in a Binary table stream */
341 rc = HANDLE_CustomType1(package,source,target,type,action);
343 case 2: /* EXE file stored in a Binary table stream */
344 rc = HANDLE_CustomType2(package,source,target,type,action);
346 case 18: /*EXE file installed with package */
347 rc = HANDLE_CustomType18(package,source,target,type,action);
349 case 19: /* Error that halts install */
350 rc = HANDLE_CustomType19(package,source,target,type,action);
353 rc = HANDLE_CustomType17(package,source,target,type,action);
355 case 23: /* installs another package in the source tree */
356 deformat_string(package,target,&deformated);
357 rc = HANDLE_CustomType23(package,source,deformated,type,action);
358 msi_free(deformated);
360 case 50: /*EXE file specified by a property value */
361 rc = HANDLE_CustomType50(package,source,target,type,action);
363 case 34: /*EXE to be run in specified directory */
364 rc = HANDLE_CustomType34(package,source,target,type,action);
366 case 35: /* Directory set with formatted text. */
367 deformat_string(package,target,&deformated);
368 MSI_SetTargetPathW(package, source, deformated);
369 msi_free(deformated);
371 case 51: /* Property set with formatted text. */
375 deformat_string(package,target,&deformated);
376 rc = msi_set_property( package->db, source, deformated );
377 if (rc == ERROR_SUCCESS && !strcmpW( source, szSourceDir ))
378 msi_reset_folders( package, TRUE );
379 msi_free(deformated);
381 case 37: /* JScript/VBScript text stored in target column. */
383 rc = HANDLE_CustomType37_38(package,source,target,type,action);
386 case 6: /* JScript/VBScript file stored in a Binary table stream. */
387 rc = HANDLE_CustomType5_6(package,source,target,type,action);
389 case 21: /* JScript/VBScript file installed with the product. */
391 rc = HANDLE_CustomType21_22(package,source,target,type,action);
393 case 53: /* JScript/VBScript text specified by a property value. */
395 rc = HANDLE_CustomType53_54(package,source,target,type,action);
398 FIXME("UNHANDLED ACTION TYPE %i (%s %s)\n",
399 type & CUSTOM_ACTION_TYPE_MASK, debugstr_w(source),
404 package->scheduled_action_running = FALSE;
405 package->commit_action_running = FALSE;
406 package->rollback_action_running = FALSE;
407 msi_free(action_copy);
408 msiobj_release(&row->hdr);
412 static MSIBINARY *create_temp_binary( MSIPACKAGE *package, LPCWSTR source, BOOL dll )
414 static const WCHAR query[] = {
415 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
416 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',' ',
417 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
422 WCHAR fmt[MAX_PATH], tmpfile[MAX_PATH];
423 DWORD sz = MAX_PATH, write;
426 if (msi_get_property(package->db, szTempFolder, fmt, &sz) != ERROR_SUCCESS)
427 GetTempPathW(MAX_PATH, fmt);
429 if (!GetTempFileNameW( fmt, szMsi, 0, tmpfile ))
431 TRACE("unable to create temp file %s (%u)\n", debugstr_w(tmpfile), GetLastError());
435 row = MSI_QueryGetRecord(package->db, query, source);
439 if (!(binary = msi_alloc_zero( sizeof(MSIBINARY) )))
441 msiobj_release( &row->hdr );
444 file = CreateFileW( tmpfile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
445 if (file == INVALID_HANDLE_VALUE)
447 msiobj_release( &row->hdr );
454 r = MSI_RecordReadStream( row, 2, buffer, &sz );
455 if (r != ERROR_SUCCESS)
457 ERR("Failed to get stream\n");
460 WriteFile( file, buffer, sz, &write, NULL );
461 } while (sz == sizeof buffer);
464 msiobj_release( &row->hdr );
465 if (r != ERROR_SUCCESS)
467 DeleteFileW( tmpfile );
472 /* keep a reference to prevent the dll from being unloaded */
473 if (dll && !(binary->module = LoadLibraryW( tmpfile )))
475 WARN( "failed to load dll %s (%u)\n", debugstr_w( tmpfile ), GetLastError() );
477 binary->source = strdupW( source );
478 binary->tmpfile = strdupW( tmpfile );
479 list_add_tail( &package->binaries, &binary->entry );
483 static MSIBINARY *get_temp_binary( MSIPACKAGE *package, LPCWSTR source, BOOL dll )
487 LIST_FOR_EACH_ENTRY( binary, &package->binaries, MSIBINARY, entry )
489 if (!strcmpW( binary->source, source ))
493 return create_temp_binary( package, source, dll );
496 static void file_running_action(MSIPACKAGE* package, HANDLE Handle,
497 BOOL process, LPCWSTR name)
499 MSIRUNNINGACTION *action;
501 action = msi_alloc( sizeof(MSIRUNNINGACTION) );
503 action->handle = Handle;
504 action->process = process;
505 action->name = strdupW(name);
507 list_add_tail( &package->RunningActions, &action->entry );
510 static UINT custom_get_process_return( HANDLE process )
514 GetExitCodeProcess( process, &rc );
516 return ERROR_FUNCTION_FAILED;
517 return ERROR_SUCCESS;
520 static UINT custom_get_thread_return( MSIPACKAGE *package, HANDLE thread )
524 GetExitCodeThread( thread, &rc );
528 case ERROR_FUNCTION_NOT_CALLED:
530 case ERROR_INSTALL_USEREXIT:
531 case ERROR_INSTALL_FAILURE:
533 case ERROR_NO_MORE_ITEMS:
534 return ERROR_SUCCESS;
535 case ERROR_INSTALL_SUSPEND:
536 ACTION_ForceReboot( package );
537 return ERROR_SUCCESS;
539 ERR("Invalid Return Code %d\n",rc);
540 return ERROR_INSTALL_FAILURE;
544 static UINT wait_process_handle(MSIPACKAGE* package, UINT type,
545 HANDLE ProcessHandle, LPCWSTR name)
547 UINT rc = ERROR_SUCCESS;
549 if (!(type & msidbCustomActionTypeAsync))
551 TRACE("waiting for %s\n", debugstr_w(name));
553 msi_dialog_check_messages(ProcessHandle);
555 if (!(type & msidbCustomActionTypeContinue))
556 rc = custom_get_process_return(ProcessHandle);
558 CloseHandle(ProcessHandle);
562 TRACE("%s running in background\n", debugstr_w(name));
564 if (!(type & msidbCustomActionTypeContinue))
565 file_running_action(package, ProcessHandle, TRUE, name);
567 CloseHandle(ProcessHandle);
573 typedef struct _msi_custom_action_info {
583 } msi_custom_action_info;
585 static void release_custom_action_data( msi_custom_action_info *info )
587 EnterCriticalSection( &msi_custom_action_cs );
591 list_remove( &info->entry );
593 CloseHandle( info->handle );
594 msi_free( info->action );
595 msi_free( info->source );
596 msi_free( info->target );
597 msiobj_release( &info->package->hdr );
601 LeaveCriticalSection( &msi_custom_action_cs );
604 /* must be called inside msi_custom_action_cs if info is in the pending custom actions list */
605 static void addref_custom_action_data( msi_custom_action_info *info )
610 static UINT wait_thread_handle( msi_custom_action_info *info )
612 UINT rc = ERROR_SUCCESS;
614 if (!(info->type & msidbCustomActionTypeAsync))
616 TRACE("waiting for %s\n", debugstr_w( info->action ));
618 msi_dialog_check_messages( info->handle );
620 if (!(info->type & msidbCustomActionTypeContinue))
621 rc = custom_get_thread_return( info->package, info->handle );
623 release_custom_action_data( info );
627 TRACE("%s running in background\n", debugstr_w( info->action ));
633 static msi_custom_action_info *find_action_by_guid( const GUID *guid )
635 msi_custom_action_info *info;
638 EnterCriticalSection( &msi_custom_action_cs );
640 LIST_FOR_EACH_ENTRY( info, &msi_pending_custom_actions, msi_custom_action_info, entry )
642 if (IsEqualGUID( &info->guid, guid ))
644 addref_custom_action_data( info );
650 LeaveCriticalSection( &msi_custom_action_cs );
658 static void handle_msi_break( LPCWSTR target )
663 static const WCHAR MsiBreak[] = { 'M','s','i','B','r','e','a','k',0 };
664 static const WCHAR WindowsInstaller[] = {
665 'W','i','n','d','o','w','s',' ','I','n','s','t','a','l','l','e','r',0
668 static const WCHAR format[] = {
669 'T','o',' ','d','e','b','u','g',' ','y','o','u','r',' ',
670 'c','u','s','t','o','m',' ','a','c','t','i','o','n',',',' ',
671 'a','t','t','a','c','h',' ','y','o','u','r',' ','d','e','b','u','g','g','e','r',' ',
672 't','o',' ','p','r','o','c','e','s','s',' ','%','i',' ','(','0','x','%','X',')',' ',
673 'a','n','d',' ','p','r','e','s','s',' ','O','K',0
676 if( !GetEnvironmentVariableW( MsiBreak, val, MAX_PATH ))
679 if( strcmpiW( val, target ))
682 msg = msi_alloc( (lstrlenW(format) + 10) * sizeof(WCHAR) );
686 wsprintfW( msg, format, GetCurrentProcessId(), GetCurrentProcessId());
687 MessageBoxW( NULL, msg, WindowsInstaller, MB_OK);
692 static UINT get_action_info( const GUID *guid, INT *type, MSIHANDLE *handle,
693 BSTR *dll, BSTR *funcname,
694 IWineMsiRemotePackage **package )
696 IClassFactory *cf = NULL;
697 IWineMsiRemoteCustomAction *rca = NULL;
700 r = DllGetClassObject( &CLSID_WineMsiRemoteCustomAction,
701 &IID_IClassFactory, (LPVOID *)&cf );
704 ERR("failed to get IClassFactory interface\n");
705 return ERROR_FUNCTION_FAILED;
708 r = IClassFactory_CreateInstance( cf, NULL, &IID_IWineMsiRemoteCustomAction, (LPVOID *)&rca );
711 ERR("failed to get IWineMsiRemoteCustomAction interface\n");
712 return ERROR_FUNCTION_FAILED;
715 r = IWineMsiRemoteCustomAction_GetActionInfo( rca, guid, type, handle, dll, funcname, package );
716 IWineMsiRemoteCustomAction_Release( rca );
719 ERR("GetActionInfo failed\n");
720 return ERROR_FUNCTION_FAILED;
723 return ERROR_SUCCESS;
727 extern UINT CUSTOMPROC_wrapper( MsiCustomActionEntryPoint proc, MSIHANDLE handle );
728 __ASM_GLOBAL_FUNC( CUSTOMPROC_wrapper,
730 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
731 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
733 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
735 "movl 8(%ebp),%eax\n\t"
738 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
739 __ASM_CFI(".cfi_same_value %ebp\n\t")
742 static inline UINT CUSTOMPROC_wrapper( MsiCustomActionEntryPoint proc, MSIHANDLE handle )
748 static DWORD ACTION_CallDllFunction( const GUID *guid )
750 MsiCustomActionEntryPoint fn;
751 MSIHANDLE hPackage, handle;
754 UINT r = ERROR_FUNCTION_FAILED;
755 BSTR dll = NULL, function = NULL;
757 IWineMsiRemotePackage *remote_package = NULL;
759 TRACE("%s\n", debugstr_guid( guid ));
761 r = get_action_info( guid, &type, &handle, &dll, &function, &remote_package );
762 if (r != ERROR_SUCCESS)
765 hModule = LoadLibraryW( dll );
768 WARN( "failed to load dll %s (%u)\n", debugstr_w( dll ), GetLastError() );
769 return ERROR_SUCCESS;
772 proc = strdupWtoA( function );
773 fn = (MsiCustomActionEntryPoint) GetProcAddress( hModule, proc );
777 hPackage = alloc_msi_remote_handle( (IUnknown *)remote_package );
780 IWineMsiRemotePackage_SetMsiHandle( remote_package, handle );
781 TRACE("calling %s\n", debugstr_w( function ) );
782 handle_msi_break( function );
786 r = CUSTOMPROC_wrapper( fn, hPackage );
790 ERR("Custom action (%s:%s) caused a page fault: %08x\n",
791 debugstr_w(dll), debugstr_w(function), GetExceptionCode());
796 MsiCloseHandle( hPackage );
799 ERR("failed to create handle for %p\n", remote_package );
802 ERR("GetProcAddress(%s) failed\n", debugstr_w( function ) );
804 FreeLibrary(hModule);
806 IWineMsiRemotePackage_Release( remote_package );
807 SysFreeString( dll );
808 SysFreeString( function );
809 MsiCloseHandle( handle );
814 static DWORD WINAPI DllThread( LPVOID arg )
819 TRACE("custom action (%x) started\n", GetCurrentThreadId() );
821 rc = ACTION_CallDllFunction( guid );
823 TRACE("custom action (%x) returned %i\n", GetCurrentThreadId(), rc );
825 MsiCloseAllHandles();
829 static DWORD ACTION_CAInstallPackage(const GUID *guid)
831 msi_custom_action_info *info;
832 UINT r = ERROR_FUNCTION_FAILED;
833 INSTALLUILEVEL old_level;
835 info = find_action_by_guid(guid);
838 ERR("failed to find action %s\n", debugstr_guid(guid));
842 old_level = MsiSetInternalUI(INSTALLUILEVEL_BASIC, NULL);
843 r = MsiInstallProductW(info->source, info->target);
844 MsiSetInternalUI(old_level, NULL);
846 release_custom_action_data(info);
851 static DWORD WINAPI ConcurrentInstallThread(LPVOID arg)
856 TRACE("concurrent installation (%x) started\n", GetCurrentThreadId());
858 rc = ACTION_CAInstallPackage(guid);
860 TRACE("concurrent installation (%x) returned %i\n", GetCurrentThreadId(), rc);
862 MsiCloseAllHandles();
866 static msi_custom_action_info *do_msidbCustomActionTypeDll(
867 MSIPACKAGE *package, INT type, LPCWSTR source, LPCWSTR target, LPCWSTR action )
869 msi_custom_action_info *info;
871 info = msi_alloc( sizeof *info );
875 msiobj_addref( &package->hdr );
876 info->refs = 2; /* 1 for our caller and 1 for thread we created */
877 info->package = package;
879 info->target = strdupW( target );
880 info->source = strdupW( source );
881 info->action = strdupW( action );
882 CoCreateGuid( &info->guid );
884 EnterCriticalSection( &msi_custom_action_cs );
885 list_add_tail( &msi_pending_custom_actions, &info->entry );
886 LeaveCriticalSection( &msi_custom_action_cs );
888 info->handle = CreateThread( NULL, 0, DllThread, &info->guid, 0, NULL );
891 /* release both references */
892 release_custom_action_data( info );
893 release_custom_action_data( info );
900 static msi_custom_action_info *do_msidbCAConcurrentInstall(
901 MSIPACKAGE *package, INT type, LPCWSTR source, LPCWSTR target, LPCWSTR action)
903 msi_custom_action_info *info;
905 info = msi_alloc( sizeof *info );
909 msiobj_addref( &package->hdr );
910 info->refs = 2; /* 1 for our caller and 1 for thread we created */
911 info->package = package;
913 info->target = strdupW( target );
914 info->source = strdupW( source );
915 info->action = strdupW( action );
916 CoCreateGuid( &info->guid );
918 EnterCriticalSection( &msi_custom_action_cs );
919 list_add_tail( &msi_pending_custom_actions, &info->entry );
920 LeaveCriticalSection( &msi_custom_action_cs );
922 info->handle = CreateThread( NULL, 0, ConcurrentInstallThread, &info->guid, 0, NULL );
925 /* release both references */
926 release_custom_action_data( info );
927 release_custom_action_data( info );
934 static UINT HANDLE_CustomType23(MSIPACKAGE *package, LPCWSTR source,
935 LPCWSTR target, const INT type, LPCWSTR action)
937 msi_custom_action_info *info;
938 WCHAR package_path[MAX_PATH];
943 msi_get_property(package->db, szSourceDir, package_path, &size);
944 lstrcatW(package_path, szBackSlash);
945 lstrcatW(package_path, source);
947 TRACE("Installing package %s concurrently\n", debugstr_w(package_path));
949 info = do_msidbCAConcurrentInstall(package, type, package_path, target, action);
951 r = wait_thread_handle(info);
952 release_custom_action_data( info );
956 static UINT HANDLE_CustomType1(MSIPACKAGE *package, LPCWSTR source,
957 LPCWSTR target, const INT type, LPCWSTR action)
959 msi_custom_action_info *info;
963 if (!(binary = get_temp_binary( package, source, TRUE )))
964 return ERROR_FUNCTION_FAILED;
966 TRACE("Calling function %s from %s\n", debugstr_w(target), debugstr_w(binary->tmpfile));
968 info = do_msidbCustomActionTypeDll( package, type, binary->tmpfile, target, action );
970 r = wait_thread_handle( info );
971 release_custom_action_data( info );
975 static UINT HANDLE_CustomType2(MSIPACKAGE *package, LPCWSTR source,
976 LPCWSTR target, const INT type, LPCWSTR action)
979 PROCESS_INFORMATION info;
982 WCHAR *deformated = NULL;
984 static const WCHAR spc[] = {' ',0};
988 memset(&si,0,sizeof(STARTUPINFOW));
990 if (!(binary = get_temp_binary( package, source, FALSE )))
991 return ERROR_FUNCTION_FAILED;
993 deformat_string(package,target,&deformated);
995 len = strlenW( binary->tmpfile ) + 2;
997 len += strlenW(deformated);
999 cmd = msi_alloc(sizeof(WCHAR)*len);
1001 strcpyW( cmd, binary->tmpfile );
1005 strcatW(cmd,deformated);
1006 msi_free(deformated);
1009 TRACE("executing exe %s\n", debugstr_w(cmd));
1011 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL, szCRoot, &si, &info);
1016 ERR("Unable to execute command %s\n", debugstr_w(cmd));
1017 return ERROR_SUCCESS;
1019 CloseHandle( info.hThread );
1021 r = wait_process_handle(package, type, info.hProcess, action);
1026 static UINT HANDLE_CustomType17(MSIPACKAGE *package, LPCWSTR source,
1027 LPCWSTR target, const INT type, LPCWSTR action)
1029 msi_custom_action_info *info;
1033 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1035 file = msi_get_loaded_file( package, source );
1038 ERR("invalid file key %s\n", debugstr_w( source ));
1039 return ERROR_FUNCTION_FAILED;
1042 info = do_msidbCustomActionTypeDll( package, type, file->TargetPath, target, action );
1044 r = wait_thread_handle( info );
1045 release_custom_action_data( info );
1049 static UINT HANDLE_CustomType18(MSIPACKAGE *package, LPCWSTR source,
1050 LPCWSTR target, const INT type, LPCWSTR action)
1053 PROCESS_INFORMATION info;
1058 static const WCHAR spc[] = {' ',0};
1061 memset(&si,0,sizeof(STARTUPINFOW));
1063 file = msi_get_loaded_file(package, source);
1065 return ERROR_FUNCTION_FAILED;
1067 len = lstrlenW( file->TargetPath );
1069 deformat_string(package,target,&deformated);
1071 len += strlenW(deformated);
1074 cmd = msi_alloc(len * sizeof(WCHAR));
1076 lstrcpyW( cmd, file->TargetPath);
1080 strcatW(cmd, deformated);
1082 msi_free(deformated);
1085 TRACE("executing exe %s\n", debugstr_w(cmd));
1087 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL, szCRoot, &si, &info);
1091 ERR("Unable to execute command %s\n", debugstr_w(cmd));
1093 return ERROR_SUCCESS;
1096 CloseHandle( info.hThread );
1098 return wait_process_handle(package, type, info.hProcess, action);
1101 static UINT HANDLE_CustomType19(MSIPACKAGE *package, LPCWSTR source,
1102 LPCWSTR target, const INT type, LPCWSTR action)
1104 static const WCHAR query[] = {
1105 'S','E','L','E','C','T',' ','`','M','e','s','s','a','g','e','`',' ',
1106 'F','R','O','M',' ','`','E','r','r','o','r','`',' ',
1107 'W','H','E','R','E',' ','`','E','r','r','o','r','`',' ','=',' ',
1111 LPWSTR deformated = NULL;
1113 deformat_string( package, target, &deformated );
1115 /* first try treat the error as a number */
1116 row = MSI_QueryGetRecord( package->db, query, deformated );
1119 LPCWSTR error = MSI_RecordGetString( row, 1 );
1120 if ((gUILevel & INSTALLUILEVEL_MASK) != INSTALLUILEVEL_NONE)
1121 MessageBoxW( NULL, error, NULL, MB_OK );
1122 msiobj_release( &row->hdr );
1124 else if ((gUILevel & INSTALLUILEVEL_MASK) != INSTALLUILEVEL_NONE)
1125 MessageBoxW( NULL, deformated, NULL, MB_OK );
1127 msi_free( deformated );
1129 return ERROR_INSTALL_FAILURE;
1132 static UINT HANDLE_CustomType50(MSIPACKAGE *package, LPCWSTR source,
1133 LPCWSTR target, const INT type, LPCWSTR action)
1136 PROCESS_INFORMATION info;
1142 static const WCHAR spc[] = {' ',0};
1144 memset(&si,0,sizeof(STARTUPINFOW));
1145 memset(&info,0,sizeof(PROCESS_INFORMATION));
1147 prop = msi_dup_property( package->db, source );
1149 return ERROR_SUCCESS;
1151 deformat_string(package,target,&deformated);
1152 len = strlenW(prop) + 2;
1154 len += strlenW(deformated);
1156 cmd = msi_alloc(sizeof(WCHAR)*len);
1162 strcatW(cmd,deformated);
1164 msi_free(deformated);
1168 TRACE("executing exe %s\n", debugstr_w(cmd));
1170 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL, szCRoot, &si, &info);
1174 ERR("Unable to execute command %s\n", debugstr_w(cmd));
1176 return ERROR_SUCCESS;
1180 CloseHandle( info.hThread );
1182 return wait_process_handle(package, type, info.hProcess, action);
1185 static UINT HANDLE_CustomType34(MSIPACKAGE *package, LPCWSTR source,
1186 LPCWSTR target, const INT type, LPCWSTR action)
1189 const WCHAR *workingdir;
1191 PROCESS_INFORMATION info;
1194 memset(&si, 0, sizeof(STARTUPINFOW));
1196 workingdir = msi_get_target_folder( package, source );
1197 if (!workingdir) return ERROR_FUNCTION_FAILED;
1199 deformat_string(package, target, &filename);
1200 if (!filename) return ERROR_FUNCTION_FAILED;
1202 TRACE("executing exe %s with working directory %s\n",
1203 debugstr_w(filename), debugstr_w(workingdir));
1205 rc = CreateProcessW(NULL, filename, NULL, NULL, FALSE, 0, NULL,
1206 workingdir, &si, &info);
1210 ERR("Unable to execute command %s with working directory %s\n",
1211 debugstr_w(filename), debugstr_w(workingdir));
1213 return ERROR_SUCCESS;
1216 CloseHandle( info.hThread );
1218 return wait_process_handle(package, type, info.hProcess, action);
1221 static DWORD ACTION_CallScript( const GUID *guid )
1223 msi_custom_action_info *info;
1227 info = find_action_by_guid( guid );
1230 ERR("failed to find action %s\n", debugstr_guid( guid) );
1231 return ERROR_FUNCTION_FAILED;
1234 TRACE("function %s, script %s\n", debugstr_w( info->target ), debugstr_w( info->source ) );
1236 hPackage = alloc_msihandle( &info->package->hdr );
1239 r = call_script( hPackage, info->type, info->source, info->target, info->action );
1240 TRACE("script returned %u\n", r);
1241 MsiCloseHandle( hPackage );
1244 ERR("failed to create handle for %p\n", info->package );
1246 release_custom_action_data( info );
1250 static DWORD WINAPI ScriptThread( LPVOID arg )
1255 TRACE("custom action (%x) started\n", GetCurrentThreadId() );
1257 rc = ACTION_CallScript( guid );
1259 TRACE("custom action (%x) returned %i\n", GetCurrentThreadId(), rc );
1261 MsiCloseAllHandles();
1265 static msi_custom_action_info *do_msidbCustomActionTypeScript(
1266 MSIPACKAGE *package, INT type, LPCWSTR script, LPCWSTR function, LPCWSTR action )
1268 msi_custom_action_info *info;
1270 info = msi_alloc( sizeof *info );
1274 msiobj_addref( &package->hdr );
1275 info->refs = 2; /* 1 for our caller and 1 for thread we created */
1276 info->package = package;
1278 info->target = strdupW( function );
1279 info->source = strdupW( script );
1280 info->action = strdupW( action );
1281 CoCreateGuid( &info->guid );
1283 EnterCriticalSection( &msi_custom_action_cs );
1284 list_add_tail( &msi_pending_custom_actions, &info->entry );
1285 LeaveCriticalSection( &msi_custom_action_cs );
1287 info->handle = CreateThread( NULL, 0, ScriptThread, &info->guid, 0, NULL );
1290 /* release both references */
1291 release_custom_action_data( info );
1292 release_custom_action_data( info );
1299 static UINT HANDLE_CustomType37_38(MSIPACKAGE *package, LPCWSTR source,
1300 LPCWSTR target, const INT type, LPCWSTR action)
1303 msi_custom_action_info *info;
1305 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1307 info = do_msidbCustomActionTypeScript( package, type, target, NULL, action );
1309 r = wait_thread_handle( info );
1310 release_custom_action_data( info );
1314 static UINT HANDLE_CustomType5_6(MSIPACKAGE *package, LPCWSTR source,
1315 LPCWSTR target, const INT type, LPCWSTR action)
1317 static const WCHAR query[] = {
1318 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1319 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',' ',
1320 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
1322 msi_custom_action_info *info;
1323 CHAR *buffer = NULL;
1324 WCHAR *bufferw = NULL;
1328 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1330 row = MSI_QueryGetRecord(package->db, query, source);
1332 return ERROR_FUNCTION_FAILED;
1334 r = MSI_RecordReadStream(row, 2, NULL, &sz);
1335 if (r != ERROR_SUCCESS)
1338 buffer = msi_alloc(sizeof(CHAR)*(sz+1));
1340 return ERROR_FUNCTION_FAILED;
1342 r = MSI_RecordReadStream(row, 2, buffer, &sz);
1343 if (r != ERROR_SUCCESS)
1347 bufferw = strdupAtoW(buffer);
1350 r = ERROR_FUNCTION_FAILED;
1354 info = do_msidbCustomActionTypeScript( package, type, bufferw, target, action );
1355 r = wait_thread_handle( info );
1356 release_custom_action_data( info );
1364 static UINT HANDLE_CustomType21_22(MSIPACKAGE *package, LPCWSTR source,
1365 LPCWSTR target, const INT type, LPCWSTR action)
1367 msi_custom_action_info *info;
1370 DWORD sz, szHighWord = 0, read;
1372 WCHAR *bufferw=NULL;
1376 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1378 file = msi_get_loaded_file(package, source);
1381 ERR("invalid file key %s\n", debugstr_w(source));
1382 return ERROR_FUNCTION_FAILED;
1385 hFile = CreateFileW(file->TargetPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
1386 if (hFile == INVALID_HANDLE_VALUE)
1387 return ERROR_FUNCTION_FAILED;
1389 sz = GetFileSize(hFile, &szHighWord);
1390 if (sz == INVALID_FILE_SIZE || szHighWord != 0)
1393 return ERROR_FUNCTION_FAILED;
1396 buffer = msi_alloc(sizeof(CHAR)*(sz+1));
1400 return ERROR_FUNCTION_FAILED;
1403 bRet = ReadFile(hFile, buffer, sz, &read, NULL);
1407 r = ERROR_FUNCTION_FAILED;
1412 bufferw = strdupAtoW(buffer);
1415 r = ERROR_FUNCTION_FAILED;
1419 info = do_msidbCustomActionTypeScript( package, type, bufferw, target, action );
1420 r = wait_thread_handle( info );
1421 release_custom_action_data( info );
1429 static UINT HANDLE_CustomType53_54(MSIPACKAGE *package, LPCWSTR source,
1430 LPCWSTR target, const INT type, LPCWSTR action)
1432 msi_custom_action_info *info;
1436 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1438 prop = msi_dup_property( package->db, source );
1440 return ERROR_SUCCESS;
1442 info = do_msidbCustomActionTypeScript( package, type, prop, NULL, action );
1444 r = wait_thread_handle( info );
1445 release_custom_action_data( info );
1449 void ACTION_FinishCustomActions(const MSIPACKAGE* package)
1452 HANDLE *wait_handles;
1453 unsigned int handle_count, i;
1454 msi_custom_action_info *info, *cursor;
1456 while ((item = list_head( &package->RunningActions )))
1458 MSIRUNNINGACTION *action = LIST_ENTRY( item, MSIRUNNINGACTION, entry );
1460 list_remove( &action->entry );
1462 TRACE("waiting for %s\n", debugstr_w( action->name ) );
1463 msi_dialog_check_messages( action->handle );
1465 CloseHandle( action->handle );
1466 msi_free( action->name );
1470 EnterCriticalSection( &msi_custom_action_cs );
1472 handle_count = list_count( &msi_pending_custom_actions );
1473 wait_handles = msi_alloc( handle_count * sizeof(HANDLE) );
1476 LIST_FOR_EACH_ENTRY_SAFE( info, cursor, &msi_pending_custom_actions, msi_custom_action_info, entry )
1478 if (info->package == package )
1480 if (DuplicateHandle(GetCurrentProcess(), info->handle, GetCurrentProcess(), &wait_handles[handle_count], SYNCHRONIZE, FALSE, 0))
1485 LeaveCriticalSection( &msi_custom_action_cs );
1487 for (i = 0; i < handle_count; i++)
1489 msi_dialog_check_messages( wait_handles[i] );
1490 CloseHandle( wait_handles[i] );
1493 msi_free( wait_handles );
1496 typedef struct _msi_custom_remote_impl {
1497 IWineMsiRemoteCustomAction IWineMsiRemoteCustomAction_iface;
1499 } msi_custom_remote_impl;
1501 static inline msi_custom_remote_impl *impl_from_IWineMsiRemoteCustomAction( IWineMsiRemoteCustomAction *iface )
1503 return CONTAINING_RECORD(iface, msi_custom_remote_impl, IWineMsiRemoteCustomAction_iface);
1506 static HRESULT WINAPI mcr_QueryInterface( IWineMsiRemoteCustomAction *iface,
1507 REFIID riid,LPVOID *ppobj)
1509 if( IsEqualCLSID( riid, &IID_IUnknown ) ||
1510 IsEqualCLSID( riid, &IID_IWineMsiRemoteCustomAction ) )
1512 IUnknown_AddRef( iface );
1517 return E_NOINTERFACE;
1520 static ULONG WINAPI mcr_AddRef( IWineMsiRemoteCustomAction *iface )
1522 msi_custom_remote_impl* This = impl_from_IWineMsiRemoteCustomAction( iface );
1524 return InterlockedIncrement( &This->refs );
1527 static ULONG WINAPI mcr_Release( IWineMsiRemoteCustomAction *iface )
1529 msi_custom_remote_impl* This = impl_from_IWineMsiRemoteCustomAction( iface );
1532 r = InterlockedDecrement( &This->refs );
1538 static HRESULT WINAPI mcr_GetActionInfo( IWineMsiRemoteCustomAction *iface, LPCGUID custom_action_guid,
1539 INT *type, MSIHANDLE *handle, BSTR *dll, BSTR *func, IWineMsiRemotePackage **remote_package )
1541 msi_custom_action_info *info;
1543 info = find_action_by_guid( custom_action_guid );
1548 *handle = alloc_msihandle( &info->package->hdr );
1549 *dll = SysAllocString( info->source );
1550 *func = SysAllocString( info->target );
1552 release_custom_action_data( info );
1553 return create_msi_remote_package( NULL, (LPVOID *)remote_package );
1556 static const IWineMsiRemoteCustomActionVtbl msi_custom_remote_vtbl =
1564 HRESULT create_msi_custom_remote( IUnknown *pOuter, LPVOID *ppObj )
1566 msi_custom_remote_impl* This;
1568 This = msi_alloc( sizeof *This );
1570 return E_OUTOFMEMORY;
1572 This->IWineMsiRemoteCustomAction_iface.lpVtbl = &msi_custom_remote_vtbl;