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};
48 static const WCHAR szActionData
[] = {
49 'C','u','s','t','o','m','A','c','t','i','o','n','D','a','t','a',0
51 static const WCHAR ProdCode
[] = {
52 'P','r','o','d','u','c','t','C','o','d','e',0
54 static const WCHAR UserSID
[] = {'U','s','e','r','S','I','D',0};
56 typedef struct tagMSIRUNNINGACTION
64 static UINT
HANDLE_CustomType1(MSIPACKAGE
*package
, LPCWSTR source
,
65 LPCWSTR target
, const INT type
, LPCWSTR action
);
66 static UINT
HANDLE_CustomType2(MSIPACKAGE
*package
, LPCWSTR source
,
67 LPCWSTR target
, const INT type
, LPCWSTR action
);
68 static UINT
HANDLE_CustomType17(MSIPACKAGE
*package
, LPCWSTR source
,
69 LPCWSTR target
, const INT type
, LPCWSTR action
);
70 static UINT
HANDLE_CustomType18(MSIPACKAGE
*package
, LPCWSTR source
,
71 LPCWSTR target
, const INT type
, LPCWSTR action
);
72 static UINT
HANDLE_CustomType19(MSIPACKAGE
*package
, LPCWSTR source
,
73 LPCWSTR target
, const INT type
, LPCWSTR action
);
74 static UINT
HANDLE_CustomType23(MSIPACKAGE
*package
, LPCWSTR source
,
75 LPCWSTR target
, const INT type
, LPCWSTR action
);
76 static UINT
HANDLE_CustomType50(MSIPACKAGE
*package
, LPCWSTR source
,
77 LPCWSTR target
, const INT type
, LPCWSTR action
);
78 static UINT
HANDLE_CustomType34(MSIPACKAGE
*package
, LPCWSTR source
,
79 LPCWSTR target
, const INT type
, LPCWSTR action
);
80 static UINT
HANDLE_CustomType37_38(MSIPACKAGE
*package
, LPCWSTR source
,
81 LPCWSTR target
, const INT type
, LPCWSTR action
);
82 static UINT
HANDLE_CustomType5_6(MSIPACKAGE
*package
, LPCWSTR source
,
83 LPCWSTR target
, const INT type
, LPCWSTR action
);
84 static UINT
HANDLE_CustomType21_22(MSIPACKAGE
*package
, LPCWSTR source
,
85 LPCWSTR target
, const INT type
, LPCWSTR action
);
86 static UINT
HANDLE_CustomType53_54(MSIPACKAGE
*package
, LPCWSTR source
,
87 LPCWSTR target
, const INT type
, LPCWSTR action
);
89 typedef UINT (WINAPI
*MsiCustomActionEntryPoint
)( MSIHANDLE
);
91 static CRITICAL_SECTION msi_custom_action_cs
;
92 static CRITICAL_SECTION_DEBUG msi_custom_action_cs_debug
=
94 0, 0, &msi_custom_action_cs
,
95 { &msi_custom_action_cs_debug
.ProcessLocksList
,
96 &msi_custom_action_cs_debug
.ProcessLocksList
},
97 0, 0, { (DWORD_PTR
)(__FILE__
": msi_custom_action_cs") }
99 static CRITICAL_SECTION msi_custom_action_cs
= { &msi_custom_action_cs_debug
, -1, 0, 0, 0, 0 };
101 static struct list msi_pending_custom_actions
= LIST_INIT( msi_pending_custom_actions
);
103 static BOOL
check_execution_scheduling_options(MSIPACKAGE
*package
, LPCWSTR action
, UINT options
)
105 if (!package
->script
)
108 if ((options
& msidbCustomActionTypeClientRepeat
) ==
109 msidbCustomActionTypeClientRepeat
)
111 if (!(package
->script
->InWhatSequence
& SEQUENCE_UI
&&
112 package
->script
->InWhatSequence
& SEQUENCE_EXEC
))
114 TRACE("Skipping action due to dbCustomActionTypeClientRepeat option.\n");
118 else if (options
& msidbCustomActionTypeFirstSequence
)
120 if (package
->script
->InWhatSequence
& SEQUENCE_UI
&&
121 package
->script
->InWhatSequence
& SEQUENCE_EXEC
)
123 TRACE("Skipping action due to msidbCustomActionTypeFirstSequence option.\n");
127 else if (options
& msidbCustomActionTypeOncePerProcess
)
129 if (check_unique_action(package
,action
))
131 TRACE("Skipping action due to msidbCustomActionTypeOncePerProcess option.\n");
135 register_unique_action(package
,action
);
141 /* stores the following properties before the action:
143 * [CustomActionData<=>UserSID<=>ProductCode]Action
145 static LPWSTR
msi_get_deferred_action(LPCWSTR action
, LPCWSTR actiondata
,
146 LPCWSTR usersid
, LPCWSTR prodcode
)
151 static const WCHAR format
[] = {
152 '[','%','s','<','=','>','%','s','<','=','>','%','s',']','%','s',0
156 return strdupW(action
);
158 len
= lstrlenW(action
) + lstrlenW(actiondata
) +
159 lstrlenW(usersid
) + lstrlenW(prodcode
) +
160 lstrlenW(format
) - 7;
161 deferred
= msi_alloc(len
* sizeof(WCHAR
));
163 sprintfW(deferred
, format
, actiondata
, usersid
, prodcode
, action
);
167 static void set_deferred_action_props(MSIPACKAGE
*package
, LPWSTR deferred_data
)
169 LPWSTR end
, beg
= deferred_data
+ 1;
171 static const WCHAR sep
[] = {'<','=','>',0};
173 end
= strstrW(beg
, sep
);
175 MSI_SetPropertyW(package
, szActionData
, beg
);
178 end
= strstrW(beg
, sep
);
180 MSI_SetPropertyW(package
, UserSID
, beg
);
183 end
= strchrW(beg
, ']');
185 MSI_SetPropertyW(package
, ProdCode
, beg
);
188 UINT
ACTION_CustomAction(MSIPACKAGE
*package
, LPCWSTR action
, UINT script
, BOOL execute
)
190 UINT rc
= ERROR_SUCCESS
;
192 static const WCHAR ExecSeqQuery
[] =
193 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
194 '`','C','u','s','t','o' ,'m','A','c','t','i','o','n','`',
195 ' ','W','H','E','R','E',' ','`','A','c','t','i' ,'o','n','`',' ',
196 '=',' ','\'','%','s','\'',0};
198 LPCWSTR source
, target
;
199 LPWSTR ptr
, deferred_data
= NULL
;
200 LPWSTR action_copy
= strdupW(action
);
201 WCHAR
*deformated
=NULL
;
203 /* deferred action: [properties]Action */
204 if ((ptr
= strrchrW(action_copy
, ']')))
206 deferred_data
= action_copy
;
210 row
= MSI_QueryGetRecord( package
->db
, ExecSeqQuery
, action
);
213 msi_free(action_copy
);
214 return ERROR_CALL_NOT_IMPLEMENTED
;
217 type
= MSI_RecordGetInteger(row
,2);
219 source
= MSI_RecordGetString(row
,3);
220 target
= MSI_RecordGetString(row
,4);
222 TRACE("Handling custom action %s (%x %s %s)\n",debugstr_w(action
),type
,
223 debugstr_w(source
), debugstr_w(target
));
225 /* handle some of the deferred actions */
226 if (type
& msidbCustomActionTypeTSAware
)
227 FIXME("msidbCustomActionTypeTSAware not handled\n");
229 if (type
& msidbCustomActionTypeInScript
)
231 if (type
& msidbCustomActionTypeNoImpersonate
)
232 WARN("msidbCustomActionTypeNoImpersonate not handled\n");
234 if (type
& msidbCustomActionTypeRollback
)
236 FIXME("Rollback only action... rollbacks not supported yet\n");
237 schedule_action(package
, ROLLBACK_SCRIPT
, action
);
243 LPWSTR actiondata
= msi_dup_property(package
, action
);
244 LPWSTR usersid
= msi_dup_property(package
, UserSID
);
245 LPWSTR prodcode
= msi_dup_property(package
, ProdCode
);
246 LPWSTR deferred
= msi_get_deferred_action(action
, actiondata
, usersid
, prodcode
);
248 if (type
& msidbCustomActionTypeCommit
)
250 TRACE("Deferring Commit Action!\n");
251 schedule_action(package
, COMMIT_SCRIPT
, deferred
);
255 TRACE("Deferring Action!\n");
256 schedule_action(package
, INSTALL_SCRIPT
, deferred
);
260 msi_free(actiondata
);
268 static const WCHAR szBlank
[] = {0};
270 LPWSTR actiondata
= msi_dup_property( package
, action
);
275 package
->scheduled_action_running
= TRUE
;
278 package
->commit_action_running
= TRUE
;
280 case ROLLBACK_SCRIPT
:
281 package
->rollback_action_running
= TRUE
;
288 set_deferred_action_props(package
, deferred_data
);
290 MSI_SetPropertyW(package
,szActionData
,actiondata
);
292 MSI_SetPropertyW(package
,szActionData
,szBlank
);
294 msi_free(actiondata
);
297 else if (!check_execution_scheduling_options(package
,action
,type
))
303 switch (type
& CUSTOM_ACTION_TYPE_MASK
)
305 case 1: /* DLL file stored in a Binary table stream */
306 rc
= HANDLE_CustomType1(package
,source
,target
,type
,action
);
308 case 2: /* EXE file stored in a Binary table stream */
309 rc
= HANDLE_CustomType2(package
,source
,target
,type
,action
);
311 case 18: /*EXE file installed with package */
312 rc
= HANDLE_CustomType18(package
,source
,target
,type
,action
);
314 case 19: /* Error that halts install */
315 rc
= HANDLE_CustomType19(package
,source
,target
,type
,action
);
318 rc
= HANDLE_CustomType17(package
,source
,target
,type
,action
);
320 case 23: /* installs another package in the source tree */
321 deformat_string(package
,target
,&deformated
);
322 rc
= HANDLE_CustomType23(package
,source
,deformated
,type
,action
);
323 msi_free(deformated
);
325 case 50: /*EXE file specified by a property value */
326 rc
= HANDLE_CustomType50(package
,source
,target
,type
,action
);
328 case 34: /*EXE to be run in specified directory */
329 rc
= HANDLE_CustomType34(package
,source
,target
,type
,action
);
331 case 35: /* Directory set with formatted text. */
332 deformat_string(package
,target
,&deformated
);
333 MSI_SetTargetPathW(package
, source
, deformated
);
334 msi_free(deformated
);
336 case 51: /* Property set with formatted text. */
340 deformat_string(package
,target
,&deformated
);
341 rc
= MSI_SetPropertyW(package
,source
,deformated
);
342 msi_free(deformated
);
344 case 37: /* JScript/VBScript text stored in target column. */
346 rc
= HANDLE_CustomType37_38(package
,source
,target
,type
,action
);
349 case 6: /* JScript/VBScript file stored in a Binary table stream. */
350 rc
= HANDLE_CustomType5_6(package
,source
,target
,type
,action
);
352 case 21: /* JScript/VBScript file installed with the product. */
354 rc
= HANDLE_CustomType21_22(package
,source
,target
,type
,action
);
356 case 53: /* JScript/VBScript text specified by a property value. */
358 rc
= HANDLE_CustomType53_54(package
,source
,target
,type
,action
);
361 FIXME("UNHANDLED ACTION TYPE %i (%s %s)\n",
362 type
& CUSTOM_ACTION_TYPE_MASK
, debugstr_w(source
),
367 package
->scheduled_action_running
= FALSE
;
368 package
->commit_action_running
= FALSE
;
369 package
->rollback_action_running
= FALSE
;
370 msi_free(action_copy
);
371 msiobj_release(&row
->hdr
);
376 static UINT
store_binary_to_temp(MSIPACKAGE
*package
, LPCWSTR source
,
379 static const WCHAR query
[] = {
380 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
381 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',' ',
382 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
386 static const WCHAR f1
[] = {'m','s','i',0};
391 if (MSI_GetPropertyW(package
, cszTempFolder
, fmt
, &sz
) != ERROR_SUCCESS
)
392 GetTempPathW(MAX_PATH
, fmt
);
394 if (GetTempFileNameW(fmt
, f1
, 0, tmp_file
) == 0)
396 TRACE("Unable to create file\n");
397 return ERROR_FUNCTION_FAILED
;
399 track_tempfile(package
, tmp_file
);
401 row
= MSI_QueryGetRecord(package
->db
, query
, source
);
403 return ERROR_FUNCTION_FAILED
;
405 /* write out the file */
406 file
= CreateFileW(tmp_file
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
,
407 FILE_ATTRIBUTE_NORMAL
, NULL
);
408 if (file
== INVALID_HANDLE_VALUE
)
409 r
= ERROR_FUNCTION_FAILED
;
416 r
= MSI_RecordReadStream(row
, 2, buffer
, &sz
);
417 if (r
!= ERROR_SUCCESS
)
419 ERR("Failed to get stream\n");
422 WriteFile(file
, buffer
, sz
, &write
, NULL
);
423 } while (sz
== sizeof buffer
);
427 msiobj_release(&row
->hdr
);
432 static void file_running_action(MSIPACKAGE
* package
, HANDLE Handle
,
433 BOOL process
, LPCWSTR name
)
435 MSIRUNNINGACTION
*action
;
437 action
= msi_alloc( sizeof(MSIRUNNINGACTION
) );
439 action
->handle
= Handle
;
440 action
->process
= process
;
441 action
->name
= strdupW(name
);
443 list_add_tail( &package
->RunningActions
, &action
->entry
);
446 static UINT
custom_get_process_return( HANDLE process
)
450 GetExitCodeProcess( process
, &rc
);
452 return ERROR_FUNCTION_FAILED
;
453 return ERROR_SUCCESS
;
456 static UINT
custom_get_thread_return( MSIPACKAGE
*package
, HANDLE thread
)
460 GetExitCodeThread( thread
, &rc
);
464 case ERROR_FUNCTION_NOT_CALLED
:
466 case ERROR_INSTALL_USEREXIT
:
467 case ERROR_INSTALL_FAILURE
:
469 case ERROR_NO_MORE_ITEMS
:
470 return ERROR_SUCCESS
;
471 case ERROR_INSTALL_SUSPEND
:
472 ACTION_ForceReboot( package
);
473 return ERROR_SUCCESS
;
475 ERR("Invalid Return Code %d\n",rc
);
476 return ERROR_INSTALL_FAILURE
;
480 static UINT
wait_process_handle(MSIPACKAGE
* package
, UINT type
,
481 HANDLE ProcessHandle
, LPCWSTR name
)
483 UINT rc
= ERROR_SUCCESS
;
485 if (!(type
& msidbCustomActionTypeAsync
))
487 TRACE("waiting for %s\n", debugstr_w(name
));
489 msi_dialog_check_messages(ProcessHandle
);
491 if (!(type
& msidbCustomActionTypeContinue
))
492 rc
= custom_get_process_return(ProcessHandle
);
494 CloseHandle(ProcessHandle
);
498 TRACE("%s running in background\n", debugstr_w(name
));
500 if (!(type
& msidbCustomActionTypeContinue
))
501 file_running_action(package
, ProcessHandle
, TRUE
, name
);
503 CloseHandle(ProcessHandle
);
509 typedef struct _msi_custom_action_info
{
519 } msi_custom_action_info
;
521 static void release_custom_action_data( msi_custom_action_info
*info
)
523 EnterCriticalSection( &msi_custom_action_cs
);
527 list_remove( &info
->entry
);
529 CloseHandle( info
->handle
);
530 msi_free( info
->action
);
531 msi_free( info
->source
);
532 msi_free( info
->target
);
533 msiobj_release( &info
->package
->hdr
);
537 LeaveCriticalSection( &msi_custom_action_cs
);
540 /* must be called inside msi_custom_action_cs if info is in the pending custom actions list */
541 static void addref_custom_action_data( msi_custom_action_info
*info
)
546 static UINT
wait_thread_handle( msi_custom_action_info
*info
)
548 UINT rc
= ERROR_SUCCESS
;
550 if (!(info
->type
& msidbCustomActionTypeAsync
))
552 TRACE("waiting for %s\n", debugstr_w( info
->action
));
554 msi_dialog_check_messages( info
->handle
);
556 if (!(info
->type
& msidbCustomActionTypeContinue
))
557 rc
= custom_get_thread_return( info
->package
, info
->handle
);
559 release_custom_action_data( info
);
563 TRACE("%s running in background\n", debugstr_w( info
->action
));
569 static msi_custom_action_info
*find_action_by_guid( const GUID
*guid
)
571 msi_custom_action_info
*info
;
574 EnterCriticalSection( &msi_custom_action_cs
);
576 LIST_FOR_EACH_ENTRY( info
, &msi_pending_custom_actions
, msi_custom_action_info
, entry
)
578 if (IsEqualGUID( &info
->guid
, guid
))
580 addref_custom_action_data( info
);
586 LeaveCriticalSection( &msi_custom_action_cs
);
594 static void handle_msi_break( LPCWSTR target
)
599 static const WCHAR MsiBreak
[] = { 'M','s','i','B','r','e','a','k',0 };
600 static const WCHAR WindowsInstaller
[] = {
601 'W','i','n','d','o','w','s',' ','I','n','s','t','a','l','l','e','r',0
604 static const WCHAR format
[] = {
605 'T','o',' ','d','e','b','u','g',' ','y','o','u','r',' ',
606 'c','u','s','t','o','m',' ','a','c','t','i','o','n',',',' ',
607 'a','t','t','a','c','h',' ','y','o','u','r',' ','d','e','b','u','g','g','e','r',' ',
608 't','o',' ','p','r','o','c','e','s','s',' ','%','i',' ','(','0','x','%','X',')',' ',
609 'a','n','d',' ','p','r','e','s','s',' ','O','K',0
612 if( !GetEnvironmentVariableW( MsiBreak
, val
, MAX_PATH
))
615 if( lstrcmpiW( val
, target
))
618 msg
= msi_alloc( (lstrlenW(format
) + 10) * sizeof(WCHAR
) );
622 wsprintfW( msg
, format
, GetCurrentProcessId(), GetCurrentProcessId());
623 MessageBoxW( NULL
, msg
, WindowsInstaller
, MB_OK
);
628 static UINT
get_action_info( const GUID
*guid
, INT
*type
, MSIHANDLE
*handle
,
629 BSTR
*dll
, BSTR
*funcname
,
630 IWineMsiRemotePackage
**package
)
632 IClassFactory
*cf
= NULL
;
633 IWineMsiRemoteCustomAction
*rca
= NULL
;
636 r
= DllGetClassObject( &CLSID_IWineMsiRemoteCustomAction
,
637 &IID_IClassFactory
, (LPVOID
*)&cf
);
640 ERR("failed to get IClassFactory interface\n");
641 return ERROR_FUNCTION_FAILED
;
644 r
= IClassFactory_CreateInstance( cf
, NULL
, &IID_IWineMsiRemoteCustomAction
, (LPVOID
*)&rca
);
647 ERR("failed to get IWineMsiRemoteCustomAction interface\n");
648 return ERROR_FUNCTION_FAILED
;
651 r
= IWineMsiRemoteCustomAction_GetActionInfo( rca
, guid
, type
, handle
, dll
, funcname
, package
);
652 IWineMsiRemoteCustomAction_Release( rca
);
655 ERR("GetActionInfo failed\n");
656 return ERROR_FUNCTION_FAILED
;
659 return ERROR_SUCCESS
;
662 static DWORD
ACTION_CallDllFunction( const GUID
*guid
)
664 MsiCustomActionEntryPoint fn
;
665 MSIHANDLE hPackage
, handle
;
668 UINT r
= ERROR_FUNCTION_FAILED
;
669 BSTR dll
= NULL
, function
= NULL
;
671 IWineMsiRemotePackage
*remote_package
= NULL
;
673 TRACE("%s\n", debugstr_guid( guid
));
675 r
= get_action_info( guid
, &type
, &handle
, &dll
, &function
, &remote_package
);
676 if (r
!= ERROR_SUCCESS
)
679 hModule
= LoadLibraryW( dll
);
682 ERR("failed to load dll %s\n", debugstr_w( dll
) );
686 proc
= strdupWtoA( function
);
687 fn
= (MsiCustomActionEntryPoint
) GetProcAddress( hModule
, proc
);
691 hPackage
= alloc_msi_remote_handle( (IUnknown
*)remote_package
);
694 IWineMsiRemotePackage_SetMsiHandle( remote_package
, handle
);
695 TRACE("calling %s\n", debugstr_w( function
) );
696 handle_msi_break( function
);
704 ERR("Custom action (%s:%s) caused a page fault: %08x\n",
705 debugstr_w(dll
), debugstr_w(function
), GetExceptionCode());
710 MsiCloseHandle( hPackage
);
713 ERR("failed to create handle for %p\n", remote_package
);
716 ERR("GetProcAddress(%s) failed\n", debugstr_w( function
) );
718 FreeLibrary(hModule
);
720 IWineMsiRemotePackage_Release( remote_package
);
721 SysFreeString( dll
);
722 SysFreeString( function
);
723 MsiCloseHandle( handle
);
728 static DWORD WINAPI
DllThread( LPVOID arg
)
733 TRACE("custom action (%x) started\n", GetCurrentThreadId() );
735 rc
= ACTION_CallDllFunction( guid
);
737 TRACE("custom action (%x) returned %i\n", GetCurrentThreadId(), rc
);
739 MsiCloseAllHandles();
743 static DWORD
ACTION_CAInstallPackage(const GUID
*guid
)
745 msi_custom_action_info
*info
;
746 UINT r
= ERROR_FUNCTION_FAILED
;
747 INSTALLUILEVEL old_level
;
749 info
= find_action_by_guid(guid
);
752 ERR("failed to find action %s\n", debugstr_guid(guid
));
756 old_level
= MsiSetInternalUI(INSTALLUILEVEL_BASIC
, NULL
);
757 r
= MsiInstallProductW(info
->source
, info
->target
);
758 MsiSetInternalUI(old_level
, NULL
);
760 release_custom_action_data(info
);
765 static DWORD WINAPI
ConcurrentInstallThread(LPVOID arg
)
770 TRACE("concurrent installation (%x) started\n", GetCurrentThreadId());
772 rc
= ACTION_CAInstallPackage(guid
);
774 TRACE("concurrent installation (%x) returned %i\n", GetCurrentThreadId(), rc
);
776 MsiCloseAllHandles();
780 static msi_custom_action_info
*do_msidbCustomActionTypeDll(
781 MSIPACKAGE
*package
, INT type
, LPCWSTR source
, LPCWSTR target
, LPCWSTR action
)
783 msi_custom_action_info
*info
;
785 info
= msi_alloc( sizeof *info
);
789 msiobj_addref( &package
->hdr
);
790 info
->refs
= 2; /* 1 for our caller and 1 for thread we created */
791 info
->package
= package
;
793 info
->target
= strdupW( target
);
794 info
->source
= strdupW( source
);
795 info
->action
= strdupW( action
);
796 CoCreateGuid( &info
->guid
);
798 EnterCriticalSection( &msi_custom_action_cs
);
799 list_add_tail( &msi_pending_custom_actions
, &info
->entry
);
800 LeaveCriticalSection( &msi_custom_action_cs
);
802 info
->handle
= CreateThread( NULL
, 0, DllThread
, &info
->guid
, 0, NULL
);
805 /* release both references */
806 release_custom_action_data( info
);
807 release_custom_action_data( info
);
814 static msi_custom_action_info
*do_msidbCAConcurrentInstall(
815 MSIPACKAGE
*package
, INT type
, LPCWSTR source
, LPCWSTR target
, LPCWSTR action
)
817 msi_custom_action_info
*info
;
819 info
= msi_alloc( sizeof *info
);
823 msiobj_addref( &package
->hdr
);
824 info
->refs
= 2; /* 1 for our caller and 1 for thread we created */
825 info
->package
= package
;
827 info
->target
= strdupW( target
);
828 info
->source
= strdupW( source
);
829 info
->action
= strdupW( action
);
830 CoCreateGuid( &info
->guid
);
832 EnterCriticalSection( &msi_custom_action_cs
);
833 list_add_tail( &msi_pending_custom_actions
, &info
->entry
);
834 LeaveCriticalSection( &msi_custom_action_cs
);
836 info
->handle
= CreateThread( NULL
, 0, ConcurrentInstallThread
, &info
->guid
, 0, NULL
);
839 /* release both references */
840 release_custom_action_data( info
);
841 release_custom_action_data( info
);
848 static UINT
HANDLE_CustomType23(MSIPACKAGE
*package
, LPCWSTR source
,
849 LPCWSTR target
, const INT type
, LPCWSTR action
)
851 msi_custom_action_info
*info
;
852 WCHAR package_path
[MAX_PATH
];
855 static const WCHAR backslash
[] = {'\\',0};
858 MSI_GetPropertyW(package
, cszSourceDir
, package_path
, &size
);
859 lstrcatW(package_path
, backslash
);
860 lstrcatW(package_path
, source
);
862 TRACE("Installing package %s concurrently\n", debugstr_w(package_path
));
864 info
= do_msidbCAConcurrentInstall(package
, type
, package_path
, target
, action
);
866 return wait_thread_handle(info
);
869 static UINT
HANDLE_CustomType1(MSIPACKAGE
*package
, LPCWSTR source
,
870 LPCWSTR target
, const INT type
, LPCWSTR action
)
872 msi_custom_action_info
*info
;
873 WCHAR tmp_file
[MAX_PATH
];
876 r
= store_binary_to_temp(package
, source
, tmp_file
);
877 if (r
!= ERROR_SUCCESS
)
880 TRACE("Calling function %s from %s\n",debugstr_w(target
),
881 debugstr_w(tmp_file
));
883 if (!strchrW(tmp_file
,'.'))
885 static const WCHAR dot
[]={'.',0};
886 strcatW(tmp_file
,dot
);
889 info
= do_msidbCustomActionTypeDll( package
, type
, tmp_file
, target
, action
);
891 return wait_thread_handle( info
);
894 static UINT
HANDLE_CustomType2(MSIPACKAGE
*package
, LPCWSTR source
,
895 LPCWSTR target
, const INT type
, LPCWSTR action
)
897 WCHAR tmp_file
[MAX_PATH
];
899 PROCESS_INFORMATION info
;
902 WCHAR
*deformated
= NULL
;
904 static const WCHAR spc
[] = {' ',0};
907 memset(&si
,0,sizeof(STARTUPINFOW
));
909 r
= store_binary_to_temp(package
, source
, tmp_file
);
910 if (r
!= ERROR_SUCCESS
)
913 deformat_string(package
,target
,&deformated
);
915 len
= strlenW(tmp_file
)+2;
918 len
+= strlenW(deformated
);
920 cmd
= msi_alloc(sizeof(WCHAR
)*len
);
922 strcpyW(cmd
,tmp_file
);
926 strcatW(cmd
,deformated
);
928 msi_free(deformated
);
931 TRACE("executing exe %s\n", debugstr_w(cmd
));
933 rc
= CreateProcessW(NULL
, cmd
, NULL
, NULL
, FALSE
, 0, NULL
,
934 c_collen
, &si
, &info
);
939 ERR("Unable to execute command %s\n", debugstr_w(cmd
));
940 return ERROR_SUCCESS
;
942 CloseHandle( info
.hThread
);
944 r
= wait_process_handle(package
, type
, info
.hProcess
, action
);
949 static UINT
HANDLE_CustomType17(MSIPACKAGE
*package
, LPCWSTR source
,
950 LPCWSTR target
, const INT type
, LPCWSTR action
)
952 msi_custom_action_info
*info
;
955 TRACE("%s %s\n", debugstr_w(source
), debugstr_w(target
));
957 file
= get_loaded_file( package
, source
);
960 ERR("invalid file key %s\n", debugstr_w( source
));
961 return ERROR_FUNCTION_FAILED
;
964 info
= do_msidbCustomActionTypeDll( package
, type
, file
->TargetPath
, target
, action
);
966 return wait_thread_handle( info
);
969 static UINT
HANDLE_CustomType18(MSIPACKAGE
*package
, LPCWSTR source
,
970 LPCWSTR target
, const INT type
, LPCWSTR action
)
973 PROCESS_INFORMATION info
;
978 static const WCHAR spc
[] = {' ',0};
981 memset(&si
,0,sizeof(STARTUPINFOW
));
983 file
= get_loaded_file(package
,source
);
985 return ERROR_FUNCTION_FAILED
;
987 len
= lstrlenW( file
->TargetPath
);
989 deformat_string(package
,target
,&deformated
);
991 len
+= strlenW(deformated
);
994 cmd
= msi_alloc(len
* sizeof(WCHAR
));
996 lstrcpyW( cmd
, file
->TargetPath
);
1000 strcatW(cmd
, deformated
);
1002 msi_free(deformated
);
1005 TRACE("executing exe %s\n", debugstr_w(cmd
));
1007 rc
= CreateProcessW(NULL
, cmd
, NULL
, NULL
, FALSE
, 0, NULL
,
1008 c_collen
, &si
, &info
);
1012 ERR("Unable to execute command %s\n", debugstr_w(cmd
));
1014 return ERROR_SUCCESS
;
1017 CloseHandle( info
.hThread
);
1019 return wait_process_handle(package
, type
, info
.hProcess
, action
);
1022 static UINT
HANDLE_CustomType19(MSIPACKAGE
*package
, LPCWSTR source
,
1023 LPCWSTR target
, const INT type
, LPCWSTR action
)
1025 static const WCHAR query
[] = {
1026 'S','E','L','E','C','T',' ','`','M','e','s','s','a','g','e','`',' ',
1027 'F','R','O','M',' ','`','E','r','r','o','r','`',' ',
1028 'W','H','E','R','E',' ','`','E','r','r','o','r','`',' ','=',' ',
1032 LPWSTR deformated
= NULL
;
1034 deformat_string( package
, target
, &deformated
);
1036 /* first try treat the error as a number */
1037 row
= MSI_QueryGetRecord( package
->db
, query
, deformated
);
1040 LPCWSTR error
= MSI_RecordGetString( row
, 1 );
1041 MessageBoxW( NULL
, error
, NULL
, MB_OK
);
1042 msiobj_release( &row
->hdr
);
1045 MessageBoxW( NULL
, deformated
, NULL
, MB_OK
);
1047 msi_free( deformated
);
1049 return ERROR_FUNCTION_FAILED
;
1052 static UINT
HANDLE_CustomType50(MSIPACKAGE
*package
, LPCWSTR source
,
1053 LPCWSTR target
, const INT type
, LPCWSTR action
)
1056 PROCESS_INFORMATION info
;
1062 static const WCHAR spc
[] = {' ',0};
1064 memset(&si
,0,sizeof(STARTUPINFOW
));
1065 memset(&info
,0,sizeof(PROCESS_INFORMATION
));
1067 prop
= msi_dup_property( package
, source
);
1069 return ERROR_SUCCESS
;
1071 deformat_string(package
,target
,&deformated
);
1072 len
= strlenW(prop
) + 2;
1074 len
+= strlenW(deformated
);
1076 cmd
= msi_alloc(sizeof(WCHAR
)*len
);
1082 strcatW(cmd
,deformated
);
1084 msi_free(deformated
);
1088 TRACE("executing exe %s\n", debugstr_w(cmd
));
1090 rc
= CreateProcessW(NULL
, cmd
, NULL
, NULL
, FALSE
, 0, NULL
,
1091 c_collen
, &si
, &info
);
1095 ERR("Unable to execute command %s\n", debugstr_w(cmd
));
1097 return ERROR_SUCCESS
;
1101 CloseHandle( info
.hThread
);
1103 return wait_process_handle(package
, type
, info
.hProcess
, action
);
1106 static UINT
HANDLE_CustomType34(MSIPACKAGE
*package
, LPCWSTR source
,
1107 LPCWSTR target
, const INT type
, LPCWSTR action
)
1109 LPWSTR filename
, deformated
;
1111 PROCESS_INFORMATION info
;
1114 memset(&si
,0,sizeof(STARTUPINFOW
));
1116 filename
= resolve_folder(package
, source
, FALSE
, FALSE
, TRUE
, NULL
);
1119 return ERROR_FUNCTION_FAILED
;
1121 SetCurrentDirectoryW(filename
);
1124 deformat_string(package
,target
,&deformated
);
1127 return ERROR_FUNCTION_FAILED
;
1129 TRACE("executing exe %s\n", debugstr_w(deformated
));
1131 rc
= CreateProcessW(NULL
, deformated
, NULL
, NULL
, FALSE
, 0, NULL
,
1132 c_collen
, &si
, &info
);
1136 ERR("Unable to execute command %s\n", debugstr_w(deformated
));
1137 msi_free(deformated
);
1138 return ERROR_SUCCESS
;
1140 msi_free(deformated
);
1141 CloseHandle( info
.hThread
);
1143 return wait_process_handle(package
, type
, info
.hProcess
, action
);
1146 static DWORD
ACTION_CallScript( const GUID
*guid
)
1148 msi_custom_action_info
*info
;
1150 UINT r
= ERROR_FUNCTION_FAILED
;
1152 info
= find_action_by_guid( guid
);
1155 ERR("failed to find action %s\n", debugstr_guid( guid
) );
1159 TRACE("function %s, script %s\n", debugstr_w( info
->target
), debugstr_w( info
->source
) );
1161 hPackage
= alloc_msihandle( &info
->package
->hdr
);
1164 r
= call_script( hPackage
, info
->type
, info
->source
, info
->target
, info
->action
);
1165 MsiCloseHandle( hPackage
);
1168 ERR("failed to create handle for %p\n", info
->package
);
1170 if (info
->type
& msidbCustomActionTypeAsync
&&
1171 info
->type
& msidbCustomActionTypeContinue
)
1172 release_custom_action_data( info
);
1177 static DWORD WINAPI
ScriptThread( LPVOID arg
)
1182 TRACE("custom action (%x) started\n", GetCurrentThreadId() );
1184 rc
= ACTION_CallScript( guid
);
1186 TRACE("custom action (%x) returned %i\n", GetCurrentThreadId(), rc
);
1188 MsiCloseAllHandles();
1192 static msi_custom_action_info
*do_msidbCustomActionTypeScript(
1193 MSIPACKAGE
*package
, INT type
, LPCWSTR script
, LPCWSTR function
, LPCWSTR action
)
1195 msi_custom_action_info
*info
;
1197 info
= msi_alloc( sizeof *info
);
1201 msiobj_addref( &package
->hdr
);
1202 info
->refs
= 2; /* 1 for our caller and 1 for thread we created */
1203 info
->package
= package
;
1205 info
->target
= strdupW( function
);
1206 info
->source
= strdupW( script
);
1207 info
->action
= strdupW( action
);
1208 CoCreateGuid( &info
->guid
);
1210 EnterCriticalSection( &msi_custom_action_cs
);
1211 list_add_tail( &msi_pending_custom_actions
, &info
->entry
);
1212 LeaveCriticalSection( &msi_custom_action_cs
);
1214 info
->handle
= CreateThread( NULL
, 0, ScriptThread
, &info
->guid
, 0, NULL
);
1217 /* release both references */
1218 release_custom_action_data( info
);
1219 release_custom_action_data( info
);
1226 static UINT
HANDLE_CustomType37_38(MSIPACKAGE
*package
, LPCWSTR source
,
1227 LPCWSTR target
, const INT type
, LPCWSTR action
)
1229 msi_custom_action_info
*info
;
1231 TRACE("%s %s\n", debugstr_w(source
), debugstr_w(target
));
1233 info
= do_msidbCustomActionTypeScript( package
, type
, target
, NULL
, action
);
1235 return wait_thread_handle( info
);
1238 static UINT
HANDLE_CustomType5_6(MSIPACKAGE
*package
, LPCWSTR source
,
1239 LPCWSTR target
, const INT type
, LPCWSTR action
)
1241 static const WCHAR query
[] = {
1242 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1243 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',' ',
1244 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
1246 msi_custom_action_info
*info
;
1247 CHAR
*buffer
= NULL
;
1248 WCHAR
*bufferw
= NULL
;
1252 TRACE("%s %s\n", debugstr_w(source
), debugstr_w(target
));
1254 row
= MSI_QueryGetRecord(package
->db
, query
, source
);
1256 return ERROR_FUNCTION_FAILED
;
1258 r
= MSI_RecordReadStream(row
, 2, NULL
, &sz
);
1259 if (r
!= ERROR_SUCCESS
)
1262 buffer
= msi_alloc(sizeof(CHAR
)*(sz
+1));
1264 return ERROR_FUNCTION_FAILED
;
1266 r
= MSI_RecordReadStream(row
, 2, buffer
, &sz
);
1267 if (r
!= ERROR_SUCCESS
)
1271 bufferw
= strdupAtoW(buffer
);
1274 r
= ERROR_FUNCTION_FAILED
;
1278 info
= do_msidbCustomActionTypeScript( package
, type
, bufferw
, target
, action
);
1279 r
= wait_thread_handle( info
);
1287 static UINT
HANDLE_CustomType21_22(MSIPACKAGE
*package
, LPCWSTR source
,
1288 LPCWSTR target
, const INT type
, LPCWSTR action
)
1290 msi_custom_action_info
*info
;
1293 DWORD sz
, szHighWord
= 0, read
;
1295 WCHAR
*bufferw
=NULL
;
1299 TRACE("%s %s\n", debugstr_w(source
), debugstr_w(target
));
1301 file
= get_loaded_file(package
,source
);
1304 ERR("invalid file key %s\n", debugstr_w(source
));
1305 return ERROR_FUNCTION_FAILED
;
1308 hFile
= CreateFileW(file
->TargetPath
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, NULL
);
1309 if (hFile
== INVALID_HANDLE_VALUE
)
1310 return ERROR_FUNCTION_FAILED
;
1312 sz
= GetFileSize(hFile
, &szHighWord
);
1313 if (sz
== INVALID_FILE_SIZE
|| szHighWord
!= 0)
1316 return ERROR_FUNCTION_FAILED
;
1319 buffer
= msi_alloc(sizeof(CHAR
)*(sz
+1));
1323 return ERROR_FUNCTION_FAILED
;
1326 bRet
= ReadFile(hFile
, buffer
, sz
, &read
, NULL
);
1330 r
= ERROR_FUNCTION_FAILED
;
1335 bufferw
= strdupAtoW(buffer
);
1338 r
= ERROR_FUNCTION_FAILED
;
1342 info
= do_msidbCustomActionTypeScript( package
, type
, bufferw
, target
, action
);
1343 r
= wait_thread_handle( info
);
1351 static UINT
HANDLE_CustomType53_54(MSIPACKAGE
*package
, LPCWSTR source
,
1352 LPCWSTR target
, const INT type
, LPCWSTR action
)
1354 msi_custom_action_info
*info
;
1357 TRACE("%s %s\n", debugstr_w(source
), debugstr_w(target
));
1359 prop
= msi_dup_property(package
,source
);
1361 return ERROR_SUCCESS
;
1363 info
= do_msidbCustomActionTypeScript( package
, type
, prop
, NULL
, action
);
1365 return wait_thread_handle( info
);
1368 void ACTION_FinishCustomActions(const MSIPACKAGE
* package
)
1371 HANDLE
*wait_handles
;
1372 unsigned int handle_count
, i
;
1373 msi_custom_action_info
*info
, *cursor
;
1375 while ((item
= list_head( &package
->RunningActions
)))
1377 MSIRUNNINGACTION
*action
= LIST_ENTRY( item
, MSIRUNNINGACTION
, entry
);
1379 list_remove( &action
->entry
);
1381 TRACE("waiting for %s\n", debugstr_w( action
->name
) );
1382 msi_dialog_check_messages( action
->handle
);
1384 CloseHandle( action
->handle
);
1385 msi_free( action
->name
);
1389 EnterCriticalSection( &msi_custom_action_cs
);
1391 handle_count
= list_count( &msi_pending_custom_actions
);
1392 wait_handles
= HeapAlloc( GetProcessHeap(), 0, handle_count
* sizeof(HANDLE
) );
1395 LIST_FOR_EACH_ENTRY_SAFE( info
, cursor
, &msi_pending_custom_actions
, msi_custom_action_info
, entry
)
1397 if (info
->package
== package
)
1399 if (DuplicateHandle(GetCurrentProcess(), info
->handle
, GetCurrentProcess(), &wait_handles
[handle_count
], SYNCHRONIZE
, FALSE
, 0))
1404 LeaveCriticalSection( &msi_custom_action_cs
);
1406 for (i
= 0; i
< handle_count
; i
++)
1408 msi_dialog_check_messages( wait_handles
[i
] );
1409 CloseHandle( wait_handles
[i
] );
1412 HeapFree( GetProcessHeap(), 0, wait_handles
);
1415 typedef struct _msi_custom_remote_impl
{
1416 const IWineMsiRemoteCustomActionVtbl
*lpVtbl
;
1418 } msi_custom_remote_impl
;
1420 static inline msi_custom_remote_impl
* mcr_from_IWineMsiRemoteCustomAction( IWineMsiRemoteCustomAction
* iface
)
1422 return (msi_custom_remote_impl
*) iface
;
1425 static HRESULT WINAPI
mcr_QueryInterface( IWineMsiRemoteCustomAction
*iface
,
1426 REFIID riid
,LPVOID
*ppobj
)
1428 if( IsEqualCLSID( riid
, &IID_IUnknown
) ||
1429 IsEqualCLSID( riid
, &IID_IWineMsiRemoteCustomAction
) )
1431 IUnknown_AddRef( iface
);
1436 return E_NOINTERFACE
;
1439 static ULONG WINAPI
mcr_AddRef( IWineMsiRemoteCustomAction
*iface
)
1441 msi_custom_remote_impl
* This
= mcr_from_IWineMsiRemoteCustomAction( iface
);
1443 return InterlockedIncrement( &This
->refs
);
1446 static ULONG WINAPI
mcr_Release( IWineMsiRemoteCustomAction
*iface
)
1448 msi_custom_remote_impl
* This
= mcr_from_IWineMsiRemoteCustomAction( iface
);
1451 r
= InterlockedDecrement( &This
->refs
);
1457 static HRESULT WINAPI
mcr_GetActionInfo( IWineMsiRemoteCustomAction
*iface
, LPCGUID custom_action_guid
,
1458 INT
*type
, MSIHANDLE
*handle
, BSTR
*dll
, BSTR
*func
, IWineMsiRemotePackage
**remote_package
)
1460 msi_custom_action_info
*info
;
1462 info
= find_action_by_guid( custom_action_guid
);
1467 *handle
= alloc_msihandle( &info
->package
->hdr
);
1468 *dll
= SysAllocString( info
->source
);
1469 *func
= SysAllocString( info
->target
);
1471 release_custom_action_data( info
);
1472 return create_msi_remote_package( NULL
, (LPVOID
*)remote_package
);
1475 static const IWineMsiRemoteCustomActionVtbl msi_custom_remote_vtbl
=
1483 HRESULT
create_msi_custom_remote( IUnknown
*pOuter
, LPVOID
*ppObj
)
1485 msi_custom_remote_impl
* This
;
1487 This
= msi_alloc( sizeof *This
);
1489 return E_OUTOFMEMORY
;
1491 This
->lpVtbl
= &msi_custom_remote_vtbl
;