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 WARN( "failed to load dll %s (%u)\n", debugstr_w( tmpfile
), GetLastError() );
429 binary
->source
= strdupW( source
);
430 binary
->tmpfile
= strdupW( tmpfile
);
431 list_add_tail( &package
->binaries
, &binary
->entry
);
435 static MSIBINARY
*get_temp_binary( MSIPACKAGE
*package
, LPCWSTR source
, BOOL dll
)
439 LIST_FOR_EACH_ENTRY( binary
, &package
->binaries
, MSIBINARY
, entry
)
441 if (!strcmpW( binary
->source
, source
))
445 return create_temp_binary( package
, source
, dll
);
448 static void file_running_action(MSIPACKAGE
* package
, HANDLE Handle
,
449 BOOL process
, LPCWSTR name
)
451 MSIRUNNINGACTION
*action
;
453 action
= msi_alloc( sizeof(MSIRUNNINGACTION
) );
455 action
->handle
= Handle
;
456 action
->process
= process
;
457 action
->name
= strdupW(name
);
459 list_add_tail( &package
->RunningActions
, &action
->entry
);
462 static UINT
custom_get_process_return( HANDLE process
)
466 GetExitCodeProcess( process
, &rc
);
468 return ERROR_FUNCTION_FAILED
;
469 return ERROR_SUCCESS
;
472 static UINT
custom_get_thread_return( MSIPACKAGE
*package
, HANDLE thread
)
476 GetExitCodeThread( thread
, &rc
);
480 case ERROR_FUNCTION_NOT_CALLED
:
482 case ERROR_INSTALL_USEREXIT
:
483 case ERROR_INSTALL_FAILURE
:
485 case ERROR_NO_MORE_ITEMS
:
486 return ERROR_SUCCESS
;
487 case ERROR_INSTALL_SUSPEND
:
488 ACTION_ForceReboot( package
);
489 return ERROR_SUCCESS
;
491 ERR("Invalid Return Code %d\n",rc
);
492 return ERROR_INSTALL_FAILURE
;
496 static UINT
wait_process_handle(MSIPACKAGE
* package
, UINT type
,
497 HANDLE ProcessHandle
, LPCWSTR name
)
499 UINT rc
= ERROR_SUCCESS
;
501 if (!(type
& msidbCustomActionTypeAsync
))
503 TRACE("waiting for %s\n", debugstr_w(name
));
505 msi_dialog_check_messages(ProcessHandle
);
507 if (!(type
& msidbCustomActionTypeContinue
))
508 rc
= custom_get_process_return(ProcessHandle
);
510 CloseHandle(ProcessHandle
);
514 TRACE("%s running in background\n", debugstr_w(name
));
516 if (!(type
& msidbCustomActionTypeContinue
))
517 file_running_action(package
, ProcessHandle
, TRUE
, name
);
519 CloseHandle(ProcessHandle
);
525 typedef struct _msi_custom_action_info
{
535 } msi_custom_action_info
;
537 static void release_custom_action_data( msi_custom_action_info
*info
)
539 EnterCriticalSection( &msi_custom_action_cs
);
543 list_remove( &info
->entry
);
545 CloseHandle( info
->handle
);
546 msi_free( info
->action
);
547 msi_free( info
->source
);
548 msi_free( info
->target
);
549 msiobj_release( &info
->package
->hdr
);
553 LeaveCriticalSection( &msi_custom_action_cs
);
556 /* must be called inside msi_custom_action_cs if info is in the pending custom actions list */
557 static void addref_custom_action_data( msi_custom_action_info
*info
)
562 static UINT
wait_thread_handle( msi_custom_action_info
*info
)
564 UINT rc
= ERROR_SUCCESS
;
566 if (!(info
->type
& msidbCustomActionTypeAsync
))
568 TRACE("waiting for %s\n", debugstr_w( info
->action
));
570 msi_dialog_check_messages( info
->handle
);
572 if (!(info
->type
& msidbCustomActionTypeContinue
))
573 rc
= custom_get_thread_return( info
->package
, info
->handle
);
575 release_custom_action_data( info
);
579 TRACE("%s running in background\n", debugstr_w( info
->action
));
585 static msi_custom_action_info
*find_action_by_guid( const GUID
*guid
)
587 msi_custom_action_info
*info
;
590 EnterCriticalSection( &msi_custom_action_cs
);
592 LIST_FOR_EACH_ENTRY( info
, &msi_pending_custom_actions
, msi_custom_action_info
, entry
)
594 if (IsEqualGUID( &info
->guid
, guid
))
596 addref_custom_action_data( info
);
602 LeaveCriticalSection( &msi_custom_action_cs
);
610 static void handle_msi_break( LPCWSTR target
)
615 static const WCHAR MsiBreak
[] = { 'M','s','i','B','r','e','a','k',0 };
616 static const WCHAR WindowsInstaller
[] = {
617 'W','i','n','d','o','w','s',' ','I','n','s','t','a','l','l','e','r',0
620 static const WCHAR format
[] = {
621 'T','o',' ','d','e','b','u','g',' ','y','o','u','r',' ',
622 'c','u','s','t','o','m',' ','a','c','t','i','o','n',',',' ',
623 'a','t','t','a','c','h',' ','y','o','u','r',' ','d','e','b','u','g','g','e','r',' ',
624 't','o',' ','p','r','o','c','e','s','s',' ','%','i',' ','(','0','x','%','X',')',' ',
625 'a','n','d',' ','p','r','e','s','s',' ','O','K',0
628 if( !GetEnvironmentVariableW( MsiBreak
, val
, MAX_PATH
))
631 if( strcmpiW( val
, target
))
634 msg
= msi_alloc( (lstrlenW(format
) + 10) * sizeof(WCHAR
) );
638 wsprintfW( msg
, format
, GetCurrentProcessId(), GetCurrentProcessId());
639 MessageBoxW( NULL
, msg
, WindowsInstaller
, MB_OK
);
644 static UINT
get_action_info( const GUID
*guid
, INT
*type
, MSIHANDLE
*handle
,
645 BSTR
*dll
, BSTR
*funcname
,
646 IWineMsiRemotePackage
**package
)
648 IClassFactory
*cf
= NULL
;
649 IWineMsiRemoteCustomAction
*rca
= NULL
;
652 r
= DllGetClassObject( &CLSID_WineMsiRemoteCustomAction
,
653 &IID_IClassFactory
, (LPVOID
*)&cf
);
656 ERR("failed to get IClassFactory interface\n");
657 return ERROR_FUNCTION_FAILED
;
660 r
= IClassFactory_CreateInstance( cf
, NULL
, &IID_IWineMsiRemoteCustomAction
, (LPVOID
*)&rca
);
663 ERR("failed to get IWineMsiRemoteCustomAction interface\n");
664 return ERROR_FUNCTION_FAILED
;
667 r
= IWineMsiRemoteCustomAction_GetActionInfo( rca
, guid
, type
, handle
, dll
, funcname
, package
);
668 IWineMsiRemoteCustomAction_Release( rca
);
671 ERR("GetActionInfo failed\n");
672 return ERROR_FUNCTION_FAILED
;
675 return ERROR_SUCCESS
;
679 extern UINT
CUSTOMPROC_wrapper( MsiCustomActionEntryPoint proc
, MSIHANDLE handle
);
680 __ASM_GLOBAL_FUNC( CUSTOMPROC_wrapper
,
682 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
683 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
685 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
687 "movl 8(%ebp),%eax\n\t"
690 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
691 __ASM_CFI(".cfi_same_value %ebp\n\t")
694 static inline UINT
CUSTOMPROC_wrapper( MsiCustomActionEntryPoint proc
, MSIHANDLE handle
)
700 static DWORD
ACTION_CallDllFunction( const GUID
*guid
)
702 MsiCustomActionEntryPoint fn
;
703 MSIHANDLE hPackage
, handle
;
706 UINT r
= ERROR_FUNCTION_FAILED
;
707 BSTR dll
= NULL
, function
= NULL
;
709 IWineMsiRemotePackage
*remote_package
= NULL
;
711 TRACE("%s\n", debugstr_guid( guid
));
713 r
= get_action_info( guid
, &type
, &handle
, &dll
, &function
, &remote_package
);
714 if (r
!= ERROR_SUCCESS
)
717 hModule
= LoadLibraryW( dll
);
720 WARN( "failed to load dll %s (%u)\n", debugstr_w( dll
), GetLastError() );
721 return ERROR_SUCCESS
;
724 proc
= strdupWtoA( function
);
725 fn
= (MsiCustomActionEntryPoint
) GetProcAddress( hModule
, proc
);
729 hPackage
= alloc_msi_remote_handle( (IUnknown
*)remote_package
);
732 IWineMsiRemotePackage_SetMsiHandle( remote_package
, handle
);
733 TRACE("calling %s\n", debugstr_w( function
) );
734 handle_msi_break( function
);
738 r
= CUSTOMPROC_wrapper( fn
, hPackage
);
742 ERR("Custom action (%s:%s) caused a page fault: %08x\n",
743 debugstr_w(dll
), debugstr_w(function
), GetExceptionCode());
748 MsiCloseHandle( hPackage
);
751 ERR("failed to create handle for %p\n", remote_package
);
754 ERR("GetProcAddress(%s) failed\n", debugstr_w( function
) );
756 FreeLibrary(hModule
);
758 IWineMsiRemotePackage_Release( remote_package
);
759 SysFreeString( dll
);
760 SysFreeString( function
);
761 MsiCloseHandle( handle
);
766 static DWORD WINAPI
DllThread( LPVOID arg
)
771 TRACE("custom action (%x) started\n", GetCurrentThreadId() );
773 rc
= ACTION_CallDllFunction( guid
);
775 TRACE("custom action (%x) returned %i\n", GetCurrentThreadId(), rc
);
777 MsiCloseAllHandles();
781 static DWORD
ACTION_CAInstallPackage(const GUID
*guid
)
783 msi_custom_action_info
*info
;
784 UINT r
= ERROR_FUNCTION_FAILED
;
785 INSTALLUILEVEL old_level
;
787 info
= find_action_by_guid(guid
);
790 ERR("failed to find action %s\n", debugstr_guid(guid
));
794 old_level
= MsiSetInternalUI(INSTALLUILEVEL_BASIC
, NULL
);
795 r
= MsiInstallProductW(info
->source
, info
->target
);
796 MsiSetInternalUI(old_level
, NULL
);
798 release_custom_action_data(info
);
803 static DWORD WINAPI
ConcurrentInstallThread(LPVOID arg
)
808 TRACE("concurrent installation (%x) started\n", GetCurrentThreadId());
810 rc
= ACTION_CAInstallPackage(guid
);
812 TRACE("concurrent installation (%x) returned %i\n", GetCurrentThreadId(), rc
);
814 MsiCloseAllHandles();
818 static msi_custom_action_info
*do_msidbCustomActionTypeDll(
819 MSIPACKAGE
*package
, INT type
, LPCWSTR source
, LPCWSTR target
, LPCWSTR action
)
821 msi_custom_action_info
*info
;
823 info
= msi_alloc( sizeof *info
);
827 msiobj_addref( &package
->hdr
);
828 info
->refs
= 2; /* 1 for our caller and 1 for thread we created */
829 info
->package
= package
;
831 info
->target
= strdupW( target
);
832 info
->source
= strdupW( source
);
833 info
->action
= strdupW( action
);
834 CoCreateGuid( &info
->guid
);
836 EnterCriticalSection( &msi_custom_action_cs
);
837 list_add_tail( &msi_pending_custom_actions
, &info
->entry
);
838 LeaveCriticalSection( &msi_custom_action_cs
);
840 info
->handle
= CreateThread( NULL
, 0, DllThread
, &info
->guid
, 0, NULL
);
843 /* release both references */
844 release_custom_action_data( info
);
845 release_custom_action_data( info
);
852 static msi_custom_action_info
*do_msidbCAConcurrentInstall(
853 MSIPACKAGE
*package
, INT type
, LPCWSTR source
, LPCWSTR target
, LPCWSTR action
)
855 msi_custom_action_info
*info
;
857 info
= msi_alloc( sizeof *info
);
861 msiobj_addref( &package
->hdr
);
862 info
->refs
= 2; /* 1 for our caller and 1 for thread we created */
863 info
->package
= package
;
865 info
->target
= strdupW( target
);
866 info
->source
= strdupW( source
);
867 info
->action
= strdupW( action
);
868 CoCreateGuid( &info
->guid
);
870 EnterCriticalSection( &msi_custom_action_cs
);
871 list_add_tail( &msi_pending_custom_actions
, &info
->entry
);
872 LeaveCriticalSection( &msi_custom_action_cs
);
874 info
->handle
= CreateThread( NULL
, 0, ConcurrentInstallThread
, &info
->guid
, 0, NULL
);
877 /* release both references */
878 release_custom_action_data( info
);
879 release_custom_action_data( info
);
886 static UINT
HANDLE_CustomType23(MSIPACKAGE
*package
, LPCWSTR source
,
887 LPCWSTR target
, const INT type
, LPCWSTR action
)
889 msi_custom_action_info
*info
;
890 WCHAR package_path
[MAX_PATH
];
895 msi_get_property(package
->db
, cszSourceDir
, package_path
, &size
);
896 lstrcatW(package_path
, szBackSlash
);
897 lstrcatW(package_path
, source
);
899 TRACE("Installing package %s concurrently\n", debugstr_w(package_path
));
901 info
= do_msidbCAConcurrentInstall(package
, type
, package_path
, target
, action
);
903 r
= wait_thread_handle(info
);
904 release_custom_action_data( info
);
908 static UINT
HANDLE_CustomType1(MSIPACKAGE
*package
, LPCWSTR source
,
909 LPCWSTR target
, const INT type
, LPCWSTR action
)
911 msi_custom_action_info
*info
;
915 if (!(binary
= get_temp_binary( package
, source
, TRUE
)))
916 return ERROR_FUNCTION_FAILED
;
918 TRACE("Calling function %s from %s\n", debugstr_w(target
), debugstr_w(binary
->tmpfile
));
920 info
= do_msidbCustomActionTypeDll( package
, type
, binary
->tmpfile
, target
, action
);
922 r
= wait_thread_handle( info
);
923 release_custom_action_data( info
);
927 static UINT
HANDLE_CustomType2(MSIPACKAGE
*package
, LPCWSTR source
,
928 LPCWSTR target
, const INT type
, LPCWSTR action
)
931 PROCESS_INFORMATION info
;
934 WCHAR
*deformated
= NULL
;
936 static const WCHAR spc
[] = {' ',0};
940 memset(&si
,0,sizeof(STARTUPINFOW
));
942 if (!(binary
= get_temp_binary( package
, source
, FALSE
)))
943 return ERROR_FUNCTION_FAILED
;
945 deformat_string(package
,target
,&deformated
);
947 len
= strlenW( binary
->tmpfile
) + 2;
949 len
+= strlenW(deformated
);
951 cmd
= msi_alloc(sizeof(WCHAR
)*len
);
953 strcpyW( cmd
, binary
->tmpfile
);
957 strcatW(cmd
,deformated
);
958 msi_free(deformated
);
961 TRACE("executing exe %s\n", debugstr_w(cmd
));
963 rc
= CreateProcessW(NULL
, cmd
, NULL
, NULL
, FALSE
, 0, NULL
,
964 c_collen
, &si
, &info
);
969 ERR("Unable to execute command %s\n", debugstr_w(cmd
));
970 return ERROR_SUCCESS
;
972 CloseHandle( info
.hThread
);
974 r
= wait_process_handle(package
, type
, info
.hProcess
, action
);
979 static UINT
HANDLE_CustomType17(MSIPACKAGE
*package
, LPCWSTR source
,
980 LPCWSTR target
, const INT type
, LPCWSTR action
)
982 msi_custom_action_info
*info
;
986 TRACE("%s %s\n", debugstr_w(source
), debugstr_w(target
));
988 file
= get_loaded_file( package
, source
);
991 ERR("invalid file key %s\n", debugstr_w( source
));
992 return ERROR_FUNCTION_FAILED
;
995 info
= do_msidbCustomActionTypeDll( package
, type
, file
->TargetPath
, target
, action
);
997 r
= wait_thread_handle( info
);
998 release_custom_action_data( info
);
1002 static UINT
HANDLE_CustomType18(MSIPACKAGE
*package
, LPCWSTR source
,
1003 LPCWSTR target
, const INT type
, LPCWSTR action
)
1006 PROCESS_INFORMATION info
;
1011 static const WCHAR spc
[] = {' ',0};
1014 memset(&si
,0,sizeof(STARTUPINFOW
));
1016 file
= get_loaded_file(package
,source
);
1018 return ERROR_FUNCTION_FAILED
;
1020 len
= lstrlenW( file
->TargetPath
);
1022 deformat_string(package
,target
,&deformated
);
1024 len
+= strlenW(deformated
);
1027 cmd
= msi_alloc(len
* sizeof(WCHAR
));
1029 lstrcpyW( cmd
, file
->TargetPath
);
1033 strcatW(cmd
, deformated
);
1035 msi_free(deformated
);
1038 TRACE("executing exe %s\n", debugstr_w(cmd
));
1040 rc
= CreateProcessW(NULL
, cmd
, NULL
, NULL
, FALSE
, 0, NULL
,
1041 c_collen
, &si
, &info
);
1045 ERR("Unable to execute command %s\n", debugstr_w(cmd
));
1047 return ERROR_SUCCESS
;
1050 CloseHandle( info
.hThread
);
1052 return wait_process_handle(package
, type
, info
.hProcess
, action
);
1055 static UINT
HANDLE_CustomType19(MSIPACKAGE
*package
, LPCWSTR source
,
1056 LPCWSTR target
, const INT type
, LPCWSTR action
)
1058 static const WCHAR query
[] = {
1059 'S','E','L','E','C','T',' ','`','M','e','s','s','a','g','e','`',' ',
1060 'F','R','O','M',' ','`','E','r','r','o','r','`',' ',
1061 'W','H','E','R','E',' ','`','E','r','r','o','r','`',' ','=',' ',
1065 LPWSTR deformated
= NULL
;
1067 deformat_string( package
, target
, &deformated
);
1069 /* first try treat the error as a number */
1070 row
= MSI_QueryGetRecord( package
->db
, query
, deformated
);
1073 LPCWSTR error
= MSI_RecordGetString( row
, 1 );
1074 if ((gUILevel
& INSTALLUILEVEL_MASK
) != INSTALLUILEVEL_NONE
)
1075 MessageBoxW( NULL
, error
, NULL
, MB_OK
);
1076 msiobj_release( &row
->hdr
);
1078 else if ((gUILevel
& INSTALLUILEVEL_MASK
) != INSTALLUILEVEL_NONE
)
1079 MessageBoxW( NULL
, deformated
, NULL
, MB_OK
);
1081 msi_free( deformated
);
1083 return ERROR_INSTALL_FAILURE
;
1086 static UINT
HANDLE_CustomType50(MSIPACKAGE
*package
, LPCWSTR source
,
1087 LPCWSTR target
, const INT type
, LPCWSTR action
)
1090 PROCESS_INFORMATION info
;
1096 static const WCHAR spc
[] = {' ',0};
1098 memset(&si
,0,sizeof(STARTUPINFOW
));
1099 memset(&info
,0,sizeof(PROCESS_INFORMATION
));
1101 prop
= msi_dup_property( package
->db
, source
);
1103 return ERROR_SUCCESS
;
1105 deformat_string(package
,target
,&deformated
);
1106 len
= strlenW(prop
) + 2;
1108 len
+= strlenW(deformated
);
1110 cmd
= msi_alloc(sizeof(WCHAR
)*len
);
1116 strcatW(cmd
,deformated
);
1118 msi_free(deformated
);
1122 TRACE("executing exe %s\n", debugstr_w(cmd
));
1124 rc
= CreateProcessW(NULL
, cmd
, NULL
, NULL
, FALSE
, 0, NULL
,
1125 c_collen
, &si
, &info
);
1129 ERR("Unable to execute command %s\n", debugstr_w(cmd
));
1131 return ERROR_SUCCESS
;
1135 CloseHandle( info
.hThread
);
1137 return wait_process_handle(package
, type
, info
.hProcess
, action
);
1140 static UINT
HANDLE_CustomType34(MSIPACKAGE
*package
, LPCWSTR source
,
1141 LPCWSTR target
, const INT type
, LPCWSTR action
)
1143 LPWSTR workingdir
, filename
;
1145 PROCESS_INFORMATION info
;
1148 memset(&si
, 0, sizeof(STARTUPINFOW
));
1150 workingdir
= resolve_target_folder( package
, source
, FALSE
, TRUE
, NULL
);
1152 return ERROR_FUNCTION_FAILED
;
1154 deformat_string(package
, target
, &filename
);
1158 msi_free(workingdir
);
1159 return ERROR_FUNCTION_FAILED
;
1162 TRACE("executing exe %s with working directory %s\n",
1163 debugstr_w(filename
), debugstr_w(workingdir
));
1165 rc
= CreateProcessW(NULL
, filename
, NULL
, NULL
, FALSE
, 0, NULL
,
1166 workingdir
, &si
, &info
);
1170 ERR("Unable to execute command %s with working directory %s\n",
1171 debugstr_w(filename
), debugstr_w(workingdir
));
1173 msi_free(workingdir
);
1174 return ERROR_SUCCESS
;
1178 msi_free(workingdir
);
1180 CloseHandle( info
.hThread
);
1182 return wait_process_handle(package
, type
, info
.hProcess
, action
);
1185 static DWORD
ACTION_CallScript( const GUID
*guid
)
1187 msi_custom_action_info
*info
;
1191 info
= find_action_by_guid( guid
);
1194 ERR("failed to find action %s\n", debugstr_guid( guid
) );
1195 return ERROR_FUNCTION_FAILED
;
1198 TRACE("function %s, script %s\n", debugstr_w( info
->target
), debugstr_w( info
->source
) );
1200 hPackage
= alloc_msihandle( &info
->package
->hdr
);
1203 r
= call_script( hPackage
, info
->type
, info
->source
, info
->target
, info
->action
);
1204 TRACE("script returned %u\n", r
);
1205 MsiCloseHandle( hPackage
);
1208 ERR("failed to create handle for %p\n", info
->package
);
1210 release_custom_action_data( info
);
1214 static DWORD WINAPI
ScriptThread( LPVOID arg
)
1219 TRACE("custom action (%x) started\n", GetCurrentThreadId() );
1221 rc
= ACTION_CallScript( guid
);
1223 TRACE("custom action (%x) returned %i\n", GetCurrentThreadId(), rc
);
1225 MsiCloseAllHandles();
1229 static msi_custom_action_info
*do_msidbCustomActionTypeScript(
1230 MSIPACKAGE
*package
, INT type
, LPCWSTR script
, LPCWSTR function
, LPCWSTR action
)
1232 msi_custom_action_info
*info
;
1234 info
= msi_alloc( sizeof *info
);
1238 msiobj_addref( &package
->hdr
);
1239 info
->refs
= 2; /* 1 for our caller and 1 for thread we created */
1240 info
->package
= package
;
1242 info
->target
= strdupW( function
);
1243 info
->source
= strdupW( script
);
1244 info
->action
= strdupW( action
);
1245 CoCreateGuid( &info
->guid
);
1247 EnterCriticalSection( &msi_custom_action_cs
);
1248 list_add_tail( &msi_pending_custom_actions
, &info
->entry
);
1249 LeaveCriticalSection( &msi_custom_action_cs
);
1251 info
->handle
= CreateThread( NULL
, 0, ScriptThread
, &info
->guid
, 0, NULL
);
1254 /* release both references */
1255 release_custom_action_data( info
);
1256 release_custom_action_data( info
);
1263 static UINT
HANDLE_CustomType37_38(MSIPACKAGE
*package
, LPCWSTR source
,
1264 LPCWSTR target
, const INT type
, LPCWSTR action
)
1267 msi_custom_action_info
*info
;
1269 TRACE("%s %s\n", debugstr_w(source
), debugstr_w(target
));
1271 info
= do_msidbCustomActionTypeScript( package
, type
, target
, NULL
, action
);
1273 r
= wait_thread_handle( info
);
1274 release_custom_action_data( info
);
1278 static UINT
HANDLE_CustomType5_6(MSIPACKAGE
*package
, LPCWSTR source
,
1279 LPCWSTR target
, const INT type
, LPCWSTR action
)
1281 static const WCHAR query
[] = {
1282 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1283 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',' ',
1284 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
1286 msi_custom_action_info
*info
;
1287 CHAR
*buffer
= NULL
;
1288 WCHAR
*bufferw
= NULL
;
1292 TRACE("%s %s\n", debugstr_w(source
), debugstr_w(target
));
1294 row
= MSI_QueryGetRecord(package
->db
, query
, source
);
1296 return ERROR_FUNCTION_FAILED
;
1298 r
= MSI_RecordReadStream(row
, 2, NULL
, &sz
);
1299 if (r
!= ERROR_SUCCESS
)
1302 buffer
= msi_alloc(sizeof(CHAR
)*(sz
+1));
1304 return ERROR_FUNCTION_FAILED
;
1306 r
= MSI_RecordReadStream(row
, 2, buffer
, &sz
);
1307 if (r
!= ERROR_SUCCESS
)
1311 bufferw
= strdupAtoW(buffer
);
1314 r
= ERROR_FUNCTION_FAILED
;
1318 info
= do_msidbCustomActionTypeScript( package
, type
, bufferw
, target
, action
);
1319 r
= wait_thread_handle( info
);
1320 release_custom_action_data( info
);
1328 static UINT
HANDLE_CustomType21_22(MSIPACKAGE
*package
, LPCWSTR source
,
1329 LPCWSTR target
, const INT type
, LPCWSTR action
)
1331 msi_custom_action_info
*info
;
1334 DWORD sz
, szHighWord
= 0, read
;
1336 WCHAR
*bufferw
=NULL
;
1340 TRACE("%s %s\n", debugstr_w(source
), debugstr_w(target
));
1342 file
= get_loaded_file(package
,source
);
1345 ERR("invalid file key %s\n", debugstr_w(source
));
1346 return ERROR_FUNCTION_FAILED
;
1349 hFile
= CreateFileW(file
->TargetPath
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, NULL
);
1350 if (hFile
== INVALID_HANDLE_VALUE
)
1351 return ERROR_FUNCTION_FAILED
;
1353 sz
= GetFileSize(hFile
, &szHighWord
);
1354 if (sz
== INVALID_FILE_SIZE
|| szHighWord
!= 0)
1357 return ERROR_FUNCTION_FAILED
;
1360 buffer
= msi_alloc(sizeof(CHAR
)*(sz
+1));
1364 return ERROR_FUNCTION_FAILED
;
1367 bRet
= ReadFile(hFile
, buffer
, sz
, &read
, NULL
);
1371 r
= ERROR_FUNCTION_FAILED
;
1376 bufferw
= strdupAtoW(buffer
);
1379 r
= ERROR_FUNCTION_FAILED
;
1383 info
= do_msidbCustomActionTypeScript( package
, type
, bufferw
, target
, action
);
1384 r
= wait_thread_handle( info
);
1385 release_custom_action_data( info
);
1393 static UINT
HANDLE_CustomType53_54(MSIPACKAGE
*package
, LPCWSTR source
,
1394 LPCWSTR target
, const INT type
, LPCWSTR action
)
1396 msi_custom_action_info
*info
;
1400 TRACE("%s %s\n", debugstr_w(source
), debugstr_w(target
));
1402 prop
= msi_dup_property( package
->db
, source
);
1404 return ERROR_SUCCESS
;
1406 info
= do_msidbCustomActionTypeScript( package
, type
, prop
, NULL
, action
);
1408 r
= wait_thread_handle( info
);
1409 release_custom_action_data( info
);
1413 void ACTION_FinishCustomActions(const MSIPACKAGE
* package
)
1416 HANDLE
*wait_handles
;
1417 unsigned int handle_count
, i
;
1418 msi_custom_action_info
*info
, *cursor
;
1420 while ((item
= list_head( &package
->RunningActions
)))
1422 MSIRUNNINGACTION
*action
= LIST_ENTRY( item
, MSIRUNNINGACTION
, entry
);
1424 list_remove( &action
->entry
);
1426 TRACE("waiting for %s\n", debugstr_w( action
->name
) );
1427 msi_dialog_check_messages( action
->handle
);
1429 CloseHandle( action
->handle
);
1430 msi_free( action
->name
);
1434 EnterCriticalSection( &msi_custom_action_cs
);
1436 handle_count
= list_count( &msi_pending_custom_actions
);
1437 wait_handles
= msi_alloc( handle_count
* sizeof(HANDLE
) );
1440 LIST_FOR_EACH_ENTRY_SAFE( info
, cursor
, &msi_pending_custom_actions
, msi_custom_action_info
, entry
)
1442 if (info
->package
== package
)
1444 if (DuplicateHandle(GetCurrentProcess(), info
->handle
, GetCurrentProcess(), &wait_handles
[handle_count
], SYNCHRONIZE
, FALSE
, 0))
1449 LeaveCriticalSection( &msi_custom_action_cs
);
1451 for (i
= 0; i
< handle_count
; i
++)
1453 msi_dialog_check_messages( wait_handles
[i
] );
1454 CloseHandle( wait_handles
[i
] );
1457 msi_free( wait_handles
);
1460 typedef struct _msi_custom_remote_impl
{
1461 IWineMsiRemoteCustomAction IWineMsiRemoteCustomAction_iface
;
1463 } msi_custom_remote_impl
;
1465 static inline msi_custom_remote_impl
*impl_from_IWineMsiRemoteCustomAction( IWineMsiRemoteCustomAction
*iface
)
1467 return CONTAINING_RECORD(iface
, msi_custom_remote_impl
, IWineMsiRemoteCustomAction_iface
);
1470 static HRESULT WINAPI
mcr_QueryInterface( IWineMsiRemoteCustomAction
*iface
,
1471 REFIID riid
,LPVOID
*ppobj
)
1473 if( IsEqualCLSID( riid
, &IID_IUnknown
) ||
1474 IsEqualCLSID( riid
, &IID_IWineMsiRemoteCustomAction
) )
1476 IUnknown_AddRef( iface
);
1481 return E_NOINTERFACE
;
1484 static ULONG WINAPI
mcr_AddRef( IWineMsiRemoteCustomAction
*iface
)
1486 msi_custom_remote_impl
* This
= impl_from_IWineMsiRemoteCustomAction( iface
);
1488 return InterlockedIncrement( &This
->refs
);
1491 static ULONG WINAPI
mcr_Release( IWineMsiRemoteCustomAction
*iface
)
1493 msi_custom_remote_impl
* This
= impl_from_IWineMsiRemoteCustomAction( iface
);
1496 r
= InterlockedDecrement( &This
->refs
);
1502 static HRESULT WINAPI
mcr_GetActionInfo( IWineMsiRemoteCustomAction
*iface
, LPCGUID custom_action_guid
,
1503 INT
*type
, MSIHANDLE
*handle
, BSTR
*dll
, BSTR
*func
, IWineMsiRemotePackage
**remote_package
)
1505 msi_custom_action_info
*info
;
1507 info
= find_action_by_guid( custom_action_guid
);
1512 *handle
= alloc_msihandle( &info
->package
->hdr
);
1513 *dll
= SysAllocString( info
->source
);
1514 *func
= SysAllocString( info
->target
);
1516 release_custom_action_data( info
);
1517 return create_msi_remote_package( NULL
, (LPVOID
*)remote_package
);
1520 static const IWineMsiRemoteCustomActionVtbl msi_custom_remote_vtbl
=
1528 HRESULT
create_msi_custom_remote( IUnknown
*pOuter
, LPVOID
*ppObj
)
1530 msi_custom_remote_impl
* This
;
1532 This
= msi_alloc( sizeof *This
);
1534 return E_OUTOFMEMORY
;
1536 This
->IWineMsiRemoteCustomAction_iface
.lpVtbl
= &msi_custom_remote_vtbl
;