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 static UINT 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 schedule_action(package, COMMIT_SCRIPT, deferred);
291 else if (type & msidbCustomActionTypeRollback)
293 FIXME("Deferring rollback only action\n");
294 schedule_action(package, ROLLBACK_SCRIPT, deferred);
298 TRACE("Deferring action\n");
299 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);
331 if (type & msidbCustomActionTypeRollback)
333 FIXME("Rollbacks not supported yet\n");
338 else if (!check_execution_scheduling_options(package,action,type))
344 switch (type & CUSTOM_ACTION_TYPE_MASK)
346 case 1: /* DLL file stored in a Binary table stream */
347 rc = HANDLE_CustomType1(package,source,target,type,action);
349 case 2: /* EXE file stored in a Binary table stream */
350 rc = HANDLE_CustomType2(package,source,target,type,action);
352 case 18: /*EXE file installed with package */
353 rc = HANDLE_CustomType18(package,source,target,type,action);
355 case 19: /* Error that halts install */
356 rc = HANDLE_CustomType19(package,source,target,type,action);
359 rc = HANDLE_CustomType17(package,source,target,type,action);
361 case 23: /* installs another package in the source tree */
362 deformat_string(package,target,&deformated);
363 rc = HANDLE_CustomType23(package,source,deformated,type,action);
364 msi_free(deformated);
366 case 50: /*EXE file specified by a property value */
367 rc = HANDLE_CustomType50(package,source,target,type,action);
369 case 34: /*EXE to be run in specified directory */
370 rc = HANDLE_CustomType34(package,source,target,type,action);
372 case 35: /* Directory set with formatted text. */
373 deformat_string(package,target,&deformated);
374 MSI_SetTargetPathW(package, source, deformated);
375 msi_free(deformated);
377 case 51: /* Property set with formatted text. */
381 deformat_string(package,target,&deformated);
382 rc = msi_set_property( package->db, source, deformated );
383 if (rc == ERROR_SUCCESS && !strcmpW( source, szSourceDir ))
384 msi_reset_folders( package, TRUE );
385 msi_free(deformated);
387 case 37: /* JScript/VBScript text stored in target column. */
389 rc = HANDLE_CustomType37_38(package,source,target,type,action);
392 case 6: /* JScript/VBScript file stored in a Binary table stream. */
393 rc = HANDLE_CustomType5_6(package,source,target,type,action);
395 case 21: /* JScript/VBScript file installed with the product. */
397 rc = HANDLE_CustomType21_22(package,source,target,type,action);
399 case 53: /* JScript/VBScript text specified by a property value. */
401 rc = HANDLE_CustomType53_54(package,source,target,type,action);
404 FIXME("UNHANDLED ACTION TYPE %i (%s %s)\n",
405 type & CUSTOM_ACTION_TYPE_MASK, debugstr_w(source),
410 package->scheduled_action_running = FALSE;
411 package->commit_action_running = FALSE;
412 package->rollback_action_running = FALSE;
413 msi_free(action_copy);
414 msiobj_release(&row->hdr);
418 static MSIBINARY *create_temp_binary( MSIPACKAGE *package, LPCWSTR source, BOOL dll )
420 static const WCHAR query[] = {
421 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
422 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',' ',
423 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
428 WCHAR fmt[MAX_PATH], tmpfile[MAX_PATH];
429 DWORD sz = MAX_PATH, write;
432 if (msi_get_property(package->db, szTempFolder, fmt, &sz) != ERROR_SUCCESS)
433 GetTempPathW(MAX_PATH, fmt);
435 if (!GetTempFileNameW( fmt, szMsi, 0, tmpfile ))
437 TRACE("unable to create temp file %s (%u)\n", debugstr_w(tmpfile), GetLastError());
441 row = MSI_QueryGetRecord(package->db, query, source);
445 if (!(binary = msi_alloc_zero( sizeof(MSIBINARY) )))
447 msiobj_release( &row->hdr );
450 file = CreateFileW( tmpfile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
451 if (file == INVALID_HANDLE_VALUE)
453 msiobj_release( &row->hdr );
460 r = MSI_RecordReadStream( row, 2, buffer, &sz );
461 if (r != ERROR_SUCCESS)
463 ERR("Failed to get stream\n");
466 WriteFile( file, buffer, sz, &write, NULL );
467 } while (sz == sizeof buffer);
470 msiobj_release( &row->hdr );
471 if (r != ERROR_SUCCESS)
473 DeleteFileW( tmpfile );
478 /* keep a reference to prevent the dll from being unloaded */
479 if (dll && !(binary->module = LoadLibraryW( tmpfile )))
481 WARN( "failed to load dll %s (%u)\n", debugstr_w( tmpfile ), GetLastError() );
483 binary->source = strdupW( source );
484 binary->tmpfile = strdupW( tmpfile );
485 list_add_tail( &package->binaries, &binary->entry );
489 static MSIBINARY *get_temp_binary( MSIPACKAGE *package, LPCWSTR source, BOOL dll )
493 LIST_FOR_EACH_ENTRY( binary, &package->binaries, MSIBINARY, entry )
495 if (!strcmpW( binary->source, source ))
499 return create_temp_binary( package, source, dll );
502 static void file_running_action(MSIPACKAGE* package, HANDLE Handle,
503 BOOL process, LPCWSTR name)
505 MSIRUNNINGACTION *action;
507 action = msi_alloc( sizeof(MSIRUNNINGACTION) );
509 action->handle = Handle;
510 action->process = process;
511 action->name = strdupW(name);
513 list_add_tail( &package->RunningActions, &action->entry );
516 static UINT custom_get_process_return( HANDLE process )
520 GetExitCodeProcess( process, &rc );
522 return ERROR_FUNCTION_FAILED;
523 return ERROR_SUCCESS;
526 static UINT custom_get_thread_return( MSIPACKAGE *package, HANDLE thread )
530 GetExitCodeThread( thread, &rc );
534 case ERROR_FUNCTION_NOT_CALLED:
536 case ERROR_INSTALL_USEREXIT:
537 case ERROR_INSTALL_FAILURE:
539 case ERROR_NO_MORE_ITEMS:
540 return ERROR_SUCCESS;
541 case ERROR_INSTALL_SUSPEND:
542 ACTION_ForceReboot( package );
543 return ERROR_SUCCESS;
545 ERR("Invalid Return Code %d\n",rc);
546 return ERROR_INSTALL_FAILURE;
550 static UINT wait_process_handle(MSIPACKAGE* package, UINT type,
551 HANDLE ProcessHandle, LPCWSTR name)
553 UINT rc = ERROR_SUCCESS;
555 if (!(type & msidbCustomActionTypeAsync))
557 TRACE("waiting for %s\n", debugstr_w(name));
559 msi_dialog_check_messages(ProcessHandle);
561 if (!(type & msidbCustomActionTypeContinue))
562 rc = custom_get_process_return(ProcessHandle);
564 CloseHandle(ProcessHandle);
568 TRACE("%s running in background\n", debugstr_w(name));
570 if (!(type & msidbCustomActionTypeContinue))
571 file_running_action(package, ProcessHandle, TRUE, name);
573 CloseHandle(ProcessHandle);
579 typedef struct _msi_custom_action_info {
589 } msi_custom_action_info;
591 static void release_custom_action_data( msi_custom_action_info *info )
593 EnterCriticalSection( &msi_custom_action_cs );
597 list_remove( &info->entry );
599 CloseHandle( info->handle );
600 msi_free( info->action );
601 msi_free( info->source );
602 msi_free( info->target );
603 msiobj_release( &info->package->hdr );
607 LeaveCriticalSection( &msi_custom_action_cs );
610 /* must be called inside msi_custom_action_cs if info is in the pending custom actions list */
611 static void addref_custom_action_data( msi_custom_action_info *info )
616 static UINT wait_thread_handle( msi_custom_action_info *info )
618 UINT rc = ERROR_SUCCESS;
620 if (!(info->type & msidbCustomActionTypeAsync))
622 TRACE("waiting for %s\n", debugstr_w( info->action ));
624 msi_dialog_check_messages( info->handle );
626 if (!(info->type & msidbCustomActionTypeContinue))
627 rc = custom_get_thread_return( info->package, info->handle );
629 release_custom_action_data( info );
633 TRACE("%s running in background\n", debugstr_w( info->action ));
639 static msi_custom_action_info *find_action_by_guid( const GUID *guid )
641 msi_custom_action_info *info;
644 EnterCriticalSection( &msi_custom_action_cs );
646 LIST_FOR_EACH_ENTRY( info, &msi_pending_custom_actions, msi_custom_action_info, entry )
648 if (IsEqualGUID( &info->guid, guid ))
650 addref_custom_action_data( info );
656 LeaveCriticalSection( &msi_custom_action_cs );
664 static void handle_msi_break( LPCWSTR target )
669 static const WCHAR MsiBreak[] = { 'M','s','i','B','r','e','a','k',0 };
670 static const WCHAR WindowsInstaller[] = {
671 'W','i','n','d','o','w','s',' ','I','n','s','t','a','l','l','e','r',0
674 static const WCHAR format[] = {
675 'T','o',' ','d','e','b','u','g',' ','y','o','u','r',' ',
676 'c','u','s','t','o','m',' ','a','c','t','i','o','n',',',' ',
677 'a','t','t','a','c','h',' ','y','o','u','r',' ','d','e','b','u','g','g','e','r',' ',
678 't','o',' ','p','r','o','c','e','s','s',' ','%','i',' ','(','0','x','%','X',')',' ',
679 'a','n','d',' ','p','r','e','s','s',' ','O','K',0
682 if( !GetEnvironmentVariableW( MsiBreak, val, MAX_PATH ))
685 if( strcmpiW( val, target ))
688 msg = msi_alloc( (lstrlenW(format) + 10) * sizeof(WCHAR) );
692 wsprintfW( msg, format, GetCurrentProcessId(), GetCurrentProcessId());
693 MessageBoxW( NULL, msg, WindowsInstaller, MB_OK);
698 static UINT get_action_info( const GUID *guid, INT *type, MSIHANDLE *handle,
699 BSTR *dll, BSTR *funcname,
700 IWineMsiRemotePackage **package )
702 IClassFactory *cf = NULL;
703 IWineMsiRemoteCustomAction *rca = NULL;
706 r = DllGetClassObject( &CLSID_WineMsiRemoteCustomAction,
707 &IID_IClassFactory, (LPVOID *)&cf );
710 ERR("failed to get IClassFactory interface\n");
711 return ERROR_FUNCTION_FAILED;
714 r = IClassFactory_CreateInstance( cf, NULL, &IID_IWineMsiRemoteCustomAction, (LPVOID *)&rca );
717 ERR("failed to get IWineMsiRemoteCustomAction interface\n");
718 return ERROR_FUNCTION_FAILED;
721 r = IWineMsiRemoteCustomAction_GetActionInfo( rca, guid, type, handle, dll, funcname, package );
722 IWineMsiRemoteCustomAction_Release( rca );
725 ERR("GetActionInfo failed\n");
726 return ERROR_FUNCTION_FAILED;
729 return ERROR_SUCCESS;
733 extern UINT CUSTOMPROC_wrapper( MsiCustomActionEntryPoint proc, MSIHANDLE handle );
734 __ASM_GLOBAL_FUNC( CUSTOMPROC_wrapper,
736 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
737 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
739 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
741 "movl 8(%ebp),%eax\n\t"
744 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
745 __ASM_CFI(".cfi_same_value %ebp\n\t")
748 static inline UINT CUSTOMPROC_wrapper( MsiCustomActionEntryPoint proc, MSIHANDLE handle )
754 static DWORD ACTION_CallDllFunction( const GUID *guid )
756 MsiCustomActionEntryPoint fn;
757 MSIHANDLE hPackage, handle;
760 UINT r = ERROR_FUNCTION_FAILED;
761 BSTR dll = NULL, function = NULL;
763 IWineMsiRemotePackage *remote_package = NULL;
765 TRACE("%s\n", debugstr_guid( guid ));
767 r = get_action_info( guid, &type, &handle, &dll, &function, &remote_package );
768 if (r != ERROR_SUCCESS)
771 hModule = LoadLibraryW( dll );
774 WARN( "failed to load dll %s (%u)\n", debugstr_w( dll ), GetLastError() );
775 return ERROR_SUCCESS;
778 proc = strdupWtoA( function );
779 fn = (MsiCustomActionEntryPoint) GetProcAddress( hModule, proc );
783 hPackage = alloc_msi_remote_handle( (IUnknown *)remote_package );
786 IWineMsiRemotePackage_SetMsiHandle( remote_package, handle );
787 TRACE("calling %s\n", debugstr_w( function ) );
788 handle_msi_break( function );
792 r = CUSTOMPROC_wrapper( fn, hPackage );
796 ERR("Custom action (%s:%s) caused a page fault: %08x\n",
797 debugstr_w(dll), debugstr_w(function), GetExceptionCode());
802 MsiCloseHandle( hPackage );
805 ERR("failed to create handle for %p\n", remote_package );
808 ERR("GetProcAddress(%s) failed\n", debugstr_w( function ) );
810 FreeLibrary(hModule);
812 IWineMsiRemotePackage_Release( remote_package );
813 SysFreeString( dll );
814 SysFreeString( function );
815 MsiCloseHandle( handle );
820 static DWORD WINAPI DllThread( LPVOID arg )
825 TRACE("custom action (%x) started\n", GetCurrentThreadId() );
827 rc = ACTION_CallDllFunction( guid );
829 TRACE("custom action (%x) returned %i\n", GetCurrentThreadId(), rc );
831 MsiCloseAllHandles();
835 static DWORD ACTION_CAInstallPackage(const GUID *guid)
837 msi_custom_action_info *info;
838 UINT r = ERROR_FUNCTION_FAILED;
839 INSTALLUILEVEL old_level;
841 info = find_action_by_guid(guid);
844 ERR("failed to find action %s\n", debugstr_guid(guid));
848 old_level = MsiSetInternalUI(INSTALLUILEVEL_BASIC, NULL);
849 r = MsiInstallProductW(info->source, info->target);
850 MsiSetInternalUI(old_level, NULL);
852 release_custom_action_data(info);
857 static DWORD WINAPI ConcurrentInstallThread(LPVOID arg)
862 TRACE("concurrent installation (%x) started\n", GetCurrentThreadId());
864 rc = ACTION_CAInstallPackage(guid);
866 TRACE("concurrent installation (%x) returned %i\n", GetCurrentThreadId(), rc);
868 MsiCloseAllHandles();
872 static msi_custom_action_info *do_msidbCustomActionTypeDll(
873 MSIPACKAGE *package, INT type, LPCWSTR source, LPCWSTR target, LPCWSTR action )
875 msi_custom_action_info *info;
877 info = msi_alloc( sizeof *info );
881 msiobj_addref( &package->hdr );
882 info->refs = 2; /* 1 for our caller and 1 for thread we created */
883 info->package = package;
885 info->target = strdupW( target );
886 info->source = strdupW( source );
887 info->action = strdupW( action );
888 CoCreateGuid( &info->guid );
890 EnterCriticalSection( &msi_custom_action_cs );
891 list_add_tail( &msi_pending_custom_actions, &info->entry );
892 LeaveCriticalSection( &msi_custom_action_cs );
894 info->handle = CreateThread( NULL, 0, DllThread, &info->guid, 0, NULL );
897 /* release both references */
898 release_custom_action_data( info );
899 release_custom_action_data( info );
906 static msi_custom_action_info *do_msidbCAConcurrentInstall(
907 MSIPACKAGE *package, INT type, LPCWSTR source, LPCWSTR target, LPCWSTR action)
909 msi_custom_action_info *info;
911 info = msi_alloc( sizeof *info );
915 msiobj_addref( &package->hdr );
916 info->refs = 2; /* 1 for our caller and 1 for thread we created */
917 info->package = package;
919 info->target = strdupW( target );
920 info->source = strdupW( source );
921 info->action = strdupW( action );
922 CoCreateGuid( &info->guid );
924 EnterCriticalSection( &msi_custom_action_cs );
925 list_add_tail( &msi_pending_custom_actions, &info->entry );
926 LeaveCriticalSection( &msi_custom_action_cs );
928 info->handle = CreateThread( NULL, 0, ConcurrentInstallThread, &info->guid, 0, NULL );
931 /* release both references */
932 release_custom_action_data( info );
933 release_custom_action_data( info );
940 static UINT HANDLE_CustomType23(MSIPACKAGE *package, LPCWSTR source,
941 LPCWSTR target, const INT type, LPCWSTR action)
943 msi_custom_action_info *info;
944 WCHAR package_path[MAX_PATH];
949 msi_get_property(package->db, szSourceDir, package_path, &size);
950 lstrcatW(package_path, szBackSlash);
951 lstrcatW(package_path, source);
953 TRACE("Installing package %s concurrently\n", debugstr_w(package_path));
955 info = do_msidbCAConcurrentInstall(package, type, package_path, target, action);
957 r = wait_thread_handle(info);
958 release_custom_action_data( info );
962 static UINT HANDLE_CustomType1(MSIPACKAGE *package, LPCWSTR source,
963 LPCWSTR target, const INT type, LPCWSTR action)
965 msi_custom_action_info *info;
969 if (!(binary = get_temp_binary( package, source, TRUE )))
970 return ERROR_FUNCTION_FAILED;
972 TRACE("Calling function %s from %s\n", debugstr_w(target), debugstr_w(binary->tmpfile));
974 info = do_msidbCustomActionTypeDll( package, type, binary->tmpfile, target, action );
976 r = wait_thread_handle( info );
977 release_custom_action_data( info );
981 static UINT HANDLE_CustomType2(MSIPACKAGE *package, LPCWSTR source,
982 LPCWSTR target, const INT type, LPCWSTR action)
985 PROCESS_INFORMATION info;
988 WCHAR *deformated = NULL;
990 static const WCHAR spc[] = {' ',0};
994 memset(&si,0,sizeof(STARTUPINFOW));
996 if (!(binary = get_temp_binary( package, source, FALSE )))
997 return ERROR_FUNCTION_FAILED;
999 deformat_string(package,target,&deformated);
1001 len = strlenW( binary->tmpfile ) + 2;
1003 len += strlenW(deformated);
1005 cmd = msi_alloc(sizeof(WCHAR)*len);
1007 strcpyW( cmd, binary->tmpfile );
1011 strcatW(cmd,deformated);
1012 msi_free(deformated);
1015 TRACE("executing exe %s\n", debugstr_w(cmd));
1017 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL, szCRoot, &si, &info);
1022 ERR("Unable to execute command %s\n", debugstr_w(cmd));
1023 return ERROR_SUCCESS;
1025 CloseHandle( info.hThread );
1027 r = wait_process_handle(package, type, info.hProcess, action);
1032 static UINT HANDLE_CustomType17(MSIPACKAGE *package, LPCWSTR source,
1033 LPCWSTR target, const INT type, LPCWSTR action)
1035 msi_custom_action_info *info;
1039 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1041 file = msi_get_loaded_file( package, source );
1044 ERR("invalid file key %s\n", debugstr_w( source ));
1045 return ERROR_FUNCTION_FAILED;
1048 info = do_msidbCustomActionTypeDll( package, type, file->TargetPath, target, action );
1050 r = wait_thread_handle( info );
1051 release_custom_action_data( info );
1055 static UINT HANDLE_CustomType18(MSIPACKAGE *package, LPCWSTR source,
1056 LPCWSTR target, const INT type, LPCWSTR action)
1059 PROCESS_INFORMATION info;
1064 static const WCHAR spc[] = {' ',0};
1067 memset(&si,0,sizeof(STARTUPINFOW));
1069 file = msi_get_loaded_file(package, source);
1071 return ERROR_FUNCTION_FAILED;
1073 len = lstrlenW( file->TargetPath );
1075 deformat_string(package,target,&deformated);
1077 len += strlenW(deformated);
1080 cmd = msi_alloc(len * sizeof(WCHAR));
1082 lstrcpyW( cmd, file->TargetPath);
1086 strcatW(cmd, deformated);
1088 msi_free(deformated);
1091 TRACE("executing exe %s\n", debugstr_w(cmd));
1093 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL, szCRoot, &si, &info);
1097 ERR("Unable to execute command %s\n", debugstr_w(cmd));
1099 return ERROR_SUCCESS;
1102 CloseHandle( info.hThread );
1104 return wait_process_handle(package, type, info.hProcess, action);
1107 static UINT HANDLE_CustomType19(MSIPACKAGE *package, LPCWSTR source,
1108 LPCWSTR target, const INT type, LPCWSTR action)
1110 static const WCHAR query[] = {
1111 'S','E','L','E','C','T',' ','`','M','e','s','s','a','g','e','`',' ',
1112 'F','R','O','M',' ','`','E','r','r','o','r','`',' ',
1113 'W','H','E','R','E',' ','`','E','r','r','o','r','`',' ','=',' ',
1117 LPWSTR deformated = NULL;
1119 deformat_string( package, target, &deformated );
1121 /* first try treat the error as a number */
1122 row = MSI_QueryGetRecord( package->db, query, deformated );
1125 LPCWSTR error = MSI_RecordGetString( row, 1 );
1126 if ((gUILevel & INSTALLUILEVEL_MASK) != INSTALLUILEVEL_NONE)
1127 MessageBoxW( NULL, error, NULL, MB_OK );
1128 msiobj_release( &row->hdr );
1130 else if ((gUILevel & INSTALLUILEVEL_MASK) != INSTALLUILEVEL_NONE)
1131 MessageBoxW( NULL, deformated, NULL, MB_OK );
1133 msi_free( deformated );
1135 return ERROR_INSTALL_FAILURE;
1138 static UINT HANDLE_CustomType50(MSIPACKAGE *package, LPCWSTR source,
1139 LPCWSTR target, const INT type, LPCWSTR action)
1142 PROCESS_INFORMATION info;
1148 static const WCHAR spc[] = {' ',0};
1150 memset(&si,0,sizeof(STARTUPINFOW));
1151 memset(&info,0,sizeof(PROCESS_INFORMATION));
1153 prop = msi_dup_property( package->db, source );
1155 return ERROR_SUCCESS;
1157 deformat_string(package,target,&deformated);
1158 len = strlenW(prop) + 2;
1160 len += strlenW(deformated);
1162 cmd = msi_alloc(sizeof(WCHAR)*len);
1168 strcatW(cmd,deformated);
1170 msi_free(deformated);
1174 TRACE("executing exe %s\n", debugstr_w(cmd));
1176 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL, szCRoot, &si, &info);
1180 ERR("Unable to execute command %s\n", debugstr_w(cmd));
1182 return ERROR_SUCCESS;
1186 CloseHandle( info.hThread );
1188 return wait_process_handle(package, type, info.hProcess, action);
1191 static UINT HANDLE_CustomType34(MSIPACKAGE *package, LPCWSTR source,
1192 LPCWSTR target, const INT type, LPCWSTR action)
1195 const WCHAR *workingdir;
1197 PROCESS_INFORMATION info;
1200 memset(&si, 0, sizeof(STARTUPINFOW));
1202 workingdir = msi_get_target_folder( package, source );
1203 if (!workingdir) return ERROR_FUNCTION_FAILED;
1205 deformat_string(package, target, &filename);
1206 if (!filename) return ERROR_FUNCTION_FAILED;
1208 TRACE("executing exe %s with working directory %s\n",
1209 debugstr_w(filename), debugstr_w(workingdir));
1211 rc = CreateProcessW(NULL, filename, NULL, NULL, FALSE, 0, NULL,
1212 workingdir, &si, &info);
1216 ERR("Unable to execute command %s with working directory %s\n",
1217 debugstr_w(filename), debugstr_w(workingdir));
1219 return ERROR_SUCCESS;
1222 CloseHandle( info.hThread );
1224 return wait_process_handle(package, type, info.hProcess, action);
1227 static DWORD ACTION_CallScript( const GUID *guid )
1229 msi_custom_action_info *info;
1233 info = find_action_by_guid( guid );
1236 ERR("failed to find action %s\n", debugstr_guid( guid) );
1237 return ERROR_FUNCTION_FAILED;
1240 TRACE("function %s, script %s\n", debugstr_w( info->target ), debugstr_w( info->source ) );
1242 hPackage = alloc_msihandle( &info->package->hdr );
1245 r = call_script( hPackage, info->type, info->source, info->target, info->action );
1246 TRACE("script returned %u\n", r);
1247 MsiCloseHandle( hPackage );
1250 ERR("failed to create handle for %p\n", info->package );
1252 release_custom_action_data( info );
1256 static DWORD WINAPI ScriptThread( LPVOID arg )
1261 TRACE("custom action (%x) started\n", GetCurrentThreadId() );
1263 rc = ACTION_CallScript( guid );
1265 TRACE("custom action (%x) returned %i\n", GetCurrentThreadId(), rc );
1267 MsiCloseAllHandles();
1271 static msi_custom_action_info *do_msidbCustomActionTypeScript(
1272 MSIPACKAGE *package, INT type, LPCWSTR script, LPCWSTR function, LPCWSTR action )
1274 msi_custom_action_info *info;
1276 info = msi_alloc( sizeof *info );
1280 msiobj_addref( &package->hdr );
1281 info->refs = 2; /* 1 for our caller and 1 for thread we created */
1282 info->package = package;
1284 info->target = strdupW( function );
1285 info->source = strdupW( script );
1286 info->action = strdupW( action );
1287 CoCreateGuid( &info->guid );
1289 EnterCriticalSection( &msi_custom_action_cs );
1290 list_add_tail( &msi_pending_custom_actions, &info->entry );
1291 LeaveCriticalSection( &msi_custom_action_cs );
1293 info->handle = CreateThread( NULL, 0, ScriptThread, &info->guid, 0, NULL );
1296 /* release both references */
1297 release_custom_action_data( info );
1298 release_custom_action_data( info );
1305 static UINT HANDLE_CustomType37_38(MSIPACKAGE *package, LPCWSTR source,
1306 LPCWSTR target, const INT type, LPCWSTR action)
1309 msi_custom_action_info *info;
1311 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1313 info = do_msidbCustomActionTypeScript( package, type, target, NULL, action );
1315 r = wait_thread_handle( info );
1316 release_custom_action_data( info );
1320 static UINT HANDLE_CustomType5_6(MSIPACKAGE *package, LPCWSTR source,
1321 LPCWSTR target, const INT type, LPCWSTR action)
1323 static const WCHAR query[] = {
1324 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1325 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',' ',
1326 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
1328 msi_custom_action_info *info;
1329 CHAR *buffer = NULL;
1330 WCHAR *bufferw = NULL;
1334 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1336 row = MSI_QueryGetRecord(package->db, query, source);
1338 return ERROR_FUNCTION_FAILED;
1340 r = MSI_RecordReadStream(row, 2, NULL, &sz);
1341 if (r != ERROR_SUCCESS)
1344 buffer = msi_alloc(sizeof(CHAR)*(sz+1));
1346 return ERROR_FUNCTION_FAILED;
1348 r = MSI_RecordReadStream(row, 2, buffer, &sz);
1349 if (r != ERROR_SUCCESS)
1353 bufferw = strdupAtoW(buffer);
1356 r = ERROR_FUNCTION_FAILED;
1360 info = do_msidbCustomActionTypeScript( package, type, bufferw, target, action );
1361 r = wait_thread_handle( info );
1362 release_custom_action_data( info );
1370 static UINT HANDLE_CustomType21_22(MSIPACKAGE *package, LPCWSTR source,
1371 LPCWSTR target, const INT type, LPCWSTR action)
1373 msi_custom_action_info *info;
1376 DWORD sz, szHighWord = 0, read;
1378 WCHAR *bufferw=NULL;
1382 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1384 file = msi_get_loaded_file(package, source);
1387 ERR("invalid file key %s\n", debugstr_w(source));
1388 return ERROR_FUNCTION_FAILED;
1391 hFile = CreateFileW(file->TargetPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
1392 if (hFile == INVALID_HANDLE_VALUE)
1393 return ERROR_FUNCTION_FAILED;
1395 sz = GetFileSize(hFile, &szHighWord);
1396 if (sz == INVALID_FILE_SIZE || szHighWord != 0)
1399 return ERROR_FUNCTION_FAILED;
1402 buffer = msi_alloc(sizeof(CHAR)*(sz+1));
1406 return ERROR_FUNCTION_FAILED;
1409 bRet = ReadFile(hFile, buffer, sz, &read, NULL);
1413 r = ERROR_FUNCTION_FAILED;
1418 bufferw = strdupAtoW(buffer);
1421 r = ERROR_FUNCTION_FAILED;
1425 info = do_msidbCustomActionTypeScript( package, type, bufferw, target, action );
1426 r = wait_thread_handle( info );
1427 release_custom_action_data( info );
1435 static UINT HANDLE_CustomType53_54(MSIPACKAGE *package, LPCWSTR source,
1436 LPCWSTR target, const INT type, LPCWSTR action)
1438 msi_custom_action_info *info;
1442 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1444 prop = msi_dup_property( package->db, source );
1446 return ERROR_SUCCESS;
1448 info = do_msidbCustomActionTypeScript( package, type, prop, NULL, action );
1450 r = wait_thread_handle( info );
1451 release_custom_action_data( info );
1455 void ACTION_FinishCustomActions(const MSIPACKAGE* package)
1458 HANDLE *wait_handles;
1459 unsigned int handle_count, i;
1460 msi_custom_action_info *info, *cursor;
1462 while ((item = list_head( &package->RunningActions )))
1464 MSIRUNNINGACTION *action = LIST_ENTRY( item, MSIRUNNINGACTION, entry );
1466 list_remove( &action->entry );
1468 TRACE("waiting for %s\n", debugstr_w( action->name ) );
1469 msi_dialog_check_messages( action->handle );
1471 CloseHandle( action->handle );
1472 msi_free( action->name );
1476 EnterCriticalSection( &msi_custom_action_cs );
1478 handle_count = list_count( &msi_pending_custom_actions );
1479 wait_handles = msi_alloc( handle_count * sizeof(HANDLE) );
1482 LIST_FOR_EACH_ENTRY_SAFE( info, cursor, &msi_pending_custom_actions, msi_custom_action_info, entry )
1484 if (info->package == package )
1486 if (DuplicateHandle(GetCurrentProcess(), info->handle, GetCurrentProcess(), &wait_handles[handle_count], SYNCHRONIZE, FALSE, 0))
1491 LeaveCriticalSection( &msi_custom_action_cs );
1493 for (i = 0; i < handle_count; i++)
1495 msi_dialog_check_messages( wait_handles[i] );
1496 CloseHandle( wait_handles[i] );
1499 msi_free( wait_handles );
1502 typedef struct _msi_custom_remote_impl {
1503 IWineMsiRemoteCustomAction IWineMsiRemoteCustomAction_iface;
1505 } msi_custom_remote_impl;
1507 static inline msi_custom_remote_impl *impl_from_IWineMsiRemoteCustomAction( IWineMsiRemoteCustomAction *iface )
1509 return CONTAINING_RECORD(iface, msi_custom_remote_impl, IWineMsiRemoteCustomAction_iface);
1512 static HRESULT WINAPI mcr_QueryInterface( IWineMsiRemoteCustomAction *iface,
1513 REFIID riid,LPVOID *ppobj)
1515 if( IsEqualCLSID( riid, &IID_IUnknown ) ||
1516 IsEqualCLSID( riid, &IID_IWineMsiRemoteCustomAction ) )
1518 IUnknown_AddRef( iface );
1523 return E_NOINTERFACE;
1526 static ULONG WINAPI mcr_AddRef( IWineMsiRemoteCustomAction *iface )
1528 msi_custom_remote_impl* This = impl_from_IWineMsiRemoteCustomAction( iface );
1530 return InterlockedIncrement( &This->refs );
1533 static ULONG WINAPI mcr_Release( IWineMsiRemoteCustomAction *iface )
1535 msi_custom_remote_impl* This = impl_from_IWineMsiRemoteCustomAction( iface );
1538 r = InterlockedDecrement( &This->refs );
1544 static HRESULT WINAPI mcr_GetActionInfo( IWineMsiRemoteCustomAction *iface, LPCGUID custom_action_guid,
1545 INT *type, MSIHANDLE *handle, BSTR *dll, BSTR *func, IWineMsiRemotePackage **remote_package )
1547 msi_custom_action_info *info;
1549 info = find_action_by_guid( custom_action_guid );
1554 *handle = alloc_msihandle( &info->package->hdr );
1555 *dll = SysAllocString( info->source );
1556 *func = SysAllocString( info->target );
1558 release_custom_action_data( info );
1559 return create_msi_remote_package( NULL, (LPVOID *)remote_package );
1562 static const IWineMsiRemoteCustomActionVtbl msi_custom_remote_vtbl =
1570 HRESULT create_msi_custom_remote( IUnknown *pOuter, LPVOID *ppObj )
1572 msi_custom_remote_impl* This;
1574 This = msi_alloc( sizeof *This );
1576 return E_OUTOFMEMORY;
1578 This->IWineMsiRemoteCustomAction_iface.lpVtbl = &msi_custom_remote_vtbl;