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 typedef UINT (WINAPI
*MsiCustomActionEntryPoint
)( MSIHANDLE
);
55 static CRITICAL_SECTION msi_custom_action_cs
;
56 static CRITICAL_SECTION_DEBUG msi_custom_action_cs_debug
=
58 0, 0, &msi_custom_action_cs
,
59 { &msi_custom_action_cs_debug
.ProcessLocksList
,
60 &msi_custom_action_cs_debug
.ProcessLocksList
},
61 0, 0, { (DWORD_PTR
)(__FILE__
": msi_custom_action_cs") }
63 static CRITICAL_SECTION msi_custom_action_cs
= { &msi_custom_action_cs_debug
, -1, 0, 0, 0, 0 };
65 static struct list msi_pending_custom_actions
= LIST_INIT( msi_pending_custom_actions
);
67 UINT
msi_schedule_action( MSIPACKAGE
*package
, UINT script
, const WCHAR
*action
)
70 WCHAR
**newbuf
= NULL
;
72 if (script
>= SCRIPT_MAX
)
74 FIXME("Unknown script requested %u\n", script
);
75 return ERROR_FUNCTION_FAILED
;
77 TRACE("Scheduling action %s in script %u\n", debugstr_w(action
), script
);
79 count
= package
->script_actions_count
[script
];
80 package
->script_actions_count
[script
]++;
81 if (count
!= 0) newbuf
= msi_realloc( package
->script_actions
[script
],
82 package
->script_actions_count
[script
] * sizeof(WCHAR
*) );
83 else newbuf
= msi_alloc( sizeof(WCHAR
*) );
85 newbuf
[count
] = strdupW( action
);
86 package
->script_actions
[script
] = newbuf
;
90 UINT
msi_register_unique_action( MSIPACKAGE
*package
, const WCHAR
*action
)
93 WCHAR
**newbuf
= NULL
;
95 TRACE("Registering %s as unique action\n", debugstr_w(action
));
97 count
= package
->unique_actions_count
;
98 package
->unique_actions_count
++;
99 if (count
!= 0) newbuf
= msi_realloc( package
->unique_actions
,
100 package
->unique_actions_count
* sizeof(WCHAR
*) );
101 else newbuf
= msi_alloc( sizeof(WCHAR
*) );
103 newbuf
[count
] = strdupW( action
);
104 package
->unique_actions
= newbuf
;
105 return ERROR_SUCCESS
;
108 BOOL
msi_action_is_unique( const MSIPACKAGE
*package
, const WCHAR
*action
)
112 for (i
= 0; i
< package
->unique_actions_count
; i
++)
114 if (!strcmpW( package
->unique_actions
[i
], action
)) return TRUE
;
119 static BOOL
check_execution_scheduling_options(MSIPACKAGE
*package
, LPCWSTR action
, UINT options
)
121 if ((options
& msidbCustomActionTypeClientRepeat
) ==
122 msidbCustomActionTypeClientRepeat
)
124 if (!(package
->InWhatSequence
& SEQUENCE_UI
&&
125 package
->InWhatSequence
& SEQUENCE_EXEC
))
127 TRACE("Skipping action due to dbCustomActionTypeClientRepeat option.\n");
131 else if (options
& msidbCustomActionTypeFirstSequence
)
133 if (package
->InWhatSequence
& SEQUENCE_UI
&&
134 package
->InWhatSequence
& SEQUENCE_EXEC
)
136 TRACE("Skipping action due to msidbCustomActionTypeFirstSequence option.\n");
140 else if (options
& msidbCustomActionTypeOncePerProcess
)
142 if (msi_action_is_unique(package
, action
))
144 TRACE("Skipping action due to msidbCustomActionTypeOncePerProcess option.\n");
148 msi_register_unique_action(package
, action
);
154 /* stores the following properties before the action:
156 * [CustomActionData<=>UserSID<=>ProductCode]Action
158 static LPWSTR
msi_get_deferred_action(LPCWSTR action
, LPCWSTR actiondata
,
159 LPCWSTR usersid
, LPCWSTR prodcode
)
164 static const WCHAR format
[] = {
165 '[','%','s','<','=','>','%','s','<','=','>','%','s',']','%','s',0
169 return strdupW(action
);
171 len
= lstrlenW(action
) + lstrlenW(actiondata
) +
172 lstrlenW(usersid
) + lstrlenW(prodcode
) +
173 lstrlenW(format
) - 7;
174 deferred
= msi_alloc(len
* sizeof(WCHAR
));
176 sprintfW(deferred
, format
, actiondata
, usersid
, prodcode
, action
);
180 static void set_deferred_action_props( MSIPACKAGE
*package
, const WCHAR
*deferred_data
)
182 static const WCHAR sep
[] = {'<','=','>',0};
183 const WCHAR
*end
, *beg
= deferred_data
+ 1;
185 end
= strstrW(beg
, sep
);
186 msi_set_property( package
->db
, szCustomActionData
, beg
, end
- beg
);
189 end
= strstrW(beg
, sep
);
190 msi_set_property( package
->db
, szUserSID
, beg
, end
- beg
);
193 end
= strchrW(beg
, ']');
194 msi_set_property( package
->db
, szProductCode
, beg
, end
- beg
);
197 WCHAR
*msi_create_temp_file( MSIDATABASE
*db
)
204 UINT len
= sizeof(tmp
)/sizeof(tmp
[0]);
206 if (msi_get_property( db
, szTempFolder
, tmp
, &len
) ||
207 GetFileAttributesW( tmp
) != FILE_ATTRIBUTE_DIRECTORY
)
209 GetTempPathW( MAX_PATH
, tmp
);
211 if (!(db
->tempfolder
= strdupW( tmp
))) return NULL
;
214 if ((ret
= msi_alloc( (strlenW( db
->tempfolder
) + 20) * sizeof(WCHAR
) )))
216 if (!GetTempFileNameW( db
->tempfolder
, szMsi
, 0, ret
))
226 static MSIBINARY
*create_temp_binary( MSIPACKAGE
*package
, LPCWSTR source
, BOOL dll
)
228 static const WCHAR query
[] = {
229 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
230 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',' ',
231 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
233 MSIBINARY
*binary
= NULL
;
240 if (!(tmpfile
= msi_create_temp_file( package
->db
))) return NULL
;
242 if (!(row
= MSI_QueryGetRecord( package
->db
, query
, source
))) goto error
;
243 if (!(binary
= msi_alloc_zero( sizeof(MSIBINARY
) ))) goto error
;
245 file
= CreateFileW( tmpfile
, GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
246 if (file
== INVALID_HANDLE_VALUE
) goto error
;
251 r
= MSI_RecordReadStream( row
, 2, buffer
, &sz
);
252 if (r
!= ERROR_SUCCESS
)
254 ERR("Failed to get stream\n");
257 WriteFile( file
, buffer
, sz
, &write
, NULL
);
258 } while (sz
== sizeof buffer
);
261 if (r
!= ERROR_SUCCESS
) goto error
;
263 /* keep a reference to prevent the dll from being unloaded */
264 if (dll
&& !(binary
->module
= LoadLibraryW( tmpfile
)))
266 ERR( "failed to load dll %s (%u)\n", debugstr_w( tmpfile
), GetLastError() );
268 binary
->source
= strdupW( source
);
269 binary
->tmpfile
= tmpfile
;
270 list_add_tail( &package
->binaries
, &binary
->entry
);
272 msiobj_release( &row
->hdr
);
276 if (row
) msiobj_release( &row
->hdr
);
277 DeleteFileW( tmpfile
);
283 static MSIBINARY
*get_temp_binary( MSIPACKAGE
*package
, LPCWSTR source
, BOOL dll
)
287 LIST_FOR_EACH_ENTRY( binary
, &package
->binaries
, MSIBINARY
, entry
)
289 if (!strcmpW( binary
->source
, source
))
293 return create_temp_binary( package
, source
, dll
);
296 static void file_running_action(MSIPACKAGE
* package
, HANDLE Handle
,
297 BOOL process
, LPCWSTR name
)
299 MSIRUNNINGACTION
*action
;
301 action
= msi_alloc( sizeof(MSIRUNNINGACTION
) );
303 action
->handle
= Handle
;
304 action
->process
= process
;
305 action
->name
= strdupW(name
);
307 list_add_tail( &package
->RunningActions
, &action
->entry
);
310 static UINT
custom_get_process_return( HANDLE process
)
314 GetExitCodeProcess( process
, &rc
);
315 TRACE("exit code is %u\n", rc
);
317 return ERROR_FUNCTION_FAILED
;
318 return ERROR_SUCCESS
;
321 static UINT
custom_get_thread_return( MSIPACKAGE
*package
, HANDLE thread
)
325 GetExitCodeThread( thread
, &rc
);
329 case ERROR_FUNCTION_NOT_CALLED
:
331 case ERROR_INSTALL_USEREXIT
:
332 case ERROR_INSTALL_FAILURE
:
334 case ERROR_NO_MORE_ITEMS
:
335 return ERROR_SUCCESS
;
336 case ERROR_INSTALL_SUSPEND
:
337 ACTION_ForceReboot( package
);
338 return ERROR_SUCCESS
;
340 ERR("Invalid Return Code %d\n",rc
);
341 return ERROR_INSTALL_FAILURE
;
345 static UINT
wait_process_handle(MSIPACKAGE
* package
, UINT type
,
346 HANDLE ProcessHandle
, LPCWSTR name
)
348 UINT rc
= ERROR_SUCCESS
;
350 if (!(type
& msidbCustomActionTypeAsync
))
352 TRACE("waiting for %s\n", debugstr_w(name
));
354 msi_dialog_check_messages(ProcessHandle
);
356 if (!(type
& msidbCustomActionTypeContinue
))
357 rc
= custom_get_process_return(ProcessHandle
);
359 CloseHandle(ProcessHandle
);
363 TRACE("%s running in background\n", debugstr_w(name
));
365 if (!(type
& msidbCustomActionTypeContinue
))
366 file_running_action(package
, ProcessHandle
, TRUE
, name
);
368 CloseHandle(ProcessHandle
);
374 typedef struct _msi_custom_action_info
{
384 } msi_custom_action_info
;
386 static void release_custom_action_data( msi_custom_action_info
*info
)
388 EnterCriticalSection( &msi_custom_action_cs
);
392 list_remove( &info
->entry
);
394 CloseHandle( info
->handle
);
395 msi_free( info
->action
);
396 msi_free( info
->source
);
397 msi_free( info
->target
);
398 msiobj_release( &info
->package
->hdr
);
402 LeaveCriticalSection( &msi_custom_action_cs
);
405 /* must be called inside msi_custom_action_cs if info is in the pending custom actions list */
406 static void addref_custom_action_data( msi_custom_action_info
*info
)
411 static UINT
wait_thread_handle( msi_custom_action_info
*info
)
413 UINT rc
= ERROR_SUCCESS
;
415 if (!(info
->type
& msidbCustomActionTypeAsync
))
417 TRACE("waiting for %s\n", debugstr_w( info
->action
));
419 msi_dialog_check_messages( info
->handle
);
421 if (!(info
->type
& msidbCustomActionTypeContinue
))
422 rc
= custom_get_thread_return( info
->package
, info
->handle
);
424 release_custom_action_data( info
);
428 TRACE("%s running in background\n", debugstr_w( info
->action
));
434 static msi_custom_action_info
*find_action_by_guid( const GUID
*guid
)
436 msi_custom_action_info
*info
;
439 EnterCriticalSection( &msi_custom_action_cs
);
441 LIST_FOR_EACH_ENTRY( info
, &msi_pending_custom_actions
, msi_custom_action_info
, entry
)
443 if (IsEqualGUID( &info
->guid
, guid
))
445 addref_custom_action_data( info
);
451 LeaveCriticalSection( &msi_custom_action_cs
);
459 static void handle_msi_break( LPCWSTR target
)
464 static const WCHAR MsiBreak
[] = { 'M','s','i','B','r','e','a','k',0 };
465 static const WCHAR WindowsInstaller
[] = {
466 'W','i','n','d','o','w','s',' ','I','n','s','t','a','l','l','e','r',0
469 static const WCHAR format
[] = {
470 'T','o',' ','d','e','b','u','g',' ','y','o','u','r',' ',
471 'c','u','s','t','o','m',' ','a','c','t','i','o','n',',',' ',
472 'a','t','t','a','c','h',' ','y','o','u','r',' ','d','e','b','u','g','g','e','r',' ',
473 't','o',' ','p','r','o','c','e','s','s',' ','%','i',' ','(','0','x','%','X',')',' ',
474 'a','n','d',' ','p','r','e','s','s',' ','O','K',0
477 if( !GetEnvironmentVariableW( MsiBreak
, val
, MAX_PATH
))
480 if( strcmpiW( val
, target
))
483 msg
= msi_alloc( (lstrlenW(format
) + 10) * sizeof(WCHAR
) );
487 wsprintfW( msg
, format
, GetCurrentProcessId(), GetCurrentProcessId());
488 MessageBoxW( NULL
, msg
, WindowsInstaller
, MB_OK
);
493 static UINT
get_action_info( const GUID
*guid
, INT
*type
, MSIHANDLE
*handle
,
494 BSTR
*dll
, BSTR
*funcname
,
495 IWineMsiRemotePackage
**package
)
497 IClassFactory
*cf
= NULL
;
498 IWineMsiRemoteCustomAction
*rca
= NULL
;
501 r
= DllGetClassObject( &CLSID_WineMsiRemoteCustomAction
,
502 &IID_IClassFactory
, (LPVOID
*)&cf
);
505 ERR("failed to get IClassFactory interface\n");
506 return ERROR_FUNCTION_FAILED
;
509 r
= IClassFactory_CreateInstance( cf
, NULL
, &IID_IWineMsiRemoteCustomAction
, (LPVOID
*)&rca
);
512 ERR("failed to get IWineMsiRemoteCustomAction interface\n");
513 return ERROR_FUNCTION_FAILED
;
516 r
= IWineMsiRemoteCustomAction_GetActionInfo( rca
, guid
, type
, handle
, dll
, funcname
, package
);
517 IWineMsiRemoteCustomAction_Release( rca
);
520 ERR("GetActionInfo failed\n");
521 return ERROR_FUNCTION_FAILED
;
524 return ERROR_SUCCESS
;
528 extern UINT
CUSTOMPROC_wrapper( MsiCustomActionEntryPoint proc
, MSIHANDLE handle
);
529 __ASM_GLOBAL_FUNC( CUSTOMPROC_wrapper
,
531 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
532 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
534 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
537 "movl 8(%ebp),%eax\n\t"
540 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
541 __ASM_CFI(".cfi_same_value %ebp\n\t")
544 static inline UINT
CUSTOMPROC_wrapper( MsiCustomActionEntryPoint proc
, MSIHANDLE handle
)
550 static DWORD
ACTION_CallDllFunction( const GUID
*guid
)
552 MsiCustomActionEntryPoint fn
;
553 MSIHANDLE hPackage
, handle
;
556 UINT r
= ERROR_FUNCTION_FAILED
;
557 BSTR dll
= NULL
, function
= NULL
;
559 IWineMsiRemotePackage
*remote_package
= NULL
;
561 TRACE("%s\n", debugstr_guid( guid
));
563 r
= get_action_info( guid
, &type
, &handle
, &dll
, &function
, &remote_package
);
564 if (r
!= ERROR_SUCCESS
)
567 hModule
= LoadLibraryW( dll
);
570 ERR( "failed to load dll %s (%u)\n", debugstr_w( dll
), GetLastError() );
571 return ERROR_SUCCESS
;
574 proc
= strdupWtoA( function
);
575 fn
= (MsiCustomActionEntryPoint
) GetProcAddress( hModule
, proc
);
579 hPackage
= alloc_msi_remote_handle( (IUnknown
*)remote_package
);
582 IWineMsiRemotePackage_SetMsiHandle( remote_package
, handle
);
583 TRACE("calling %s\n", debugstr_w( function
) );
584 handle_msi_break( function
);
588 r
= CUSTOMPROC_wrapper( fn
, hPackage
);
592 ERR("Custom action (%s:%s) caused a page fault: %08x\n",
593 debugstr_w(dll
), debugstr_w(function
), GetExceptionCode());
598 MsiCloseHandle( hPackage
);
601 ERR("failed to create handle for %p\n", remote_package
);
604 ERR("GetProcAddress(%s) failed\n", debugstr_w( function
) );
606 FreeLibrary(hModule
);
608 IWineMsiRemotePackage_Release( remote_package
);
609 SysFreeString( dll
);
610 SysFreeString( function
);
611 MsiCloseHandle( handle
);
616 static DWORD WINAPI
DllThread( LPVOID arg
)
621 TRACE("custom action (%x) started\n", GetCurrentThreadId() );
623 rc
= ACTION_CallDllFunction( guid
);
625 TRACE("custom action (%x) returned %i\n", GetCurrentThreadId(), rc
);
627 MsiCloseAllHandles();
631 static msi_custom_action_info
*do_msidbCustomActionTypeDll(
632 MSIPACKAGE
*package
, INT type
, LPCWSTR source
, LPCWSTR target
, LPCWSTR action
)
634 msi_custom_action_info
*info
;
636 info
= msi_alloc( sizeof *info
);
640 msiobj_addref( &package
->hdr
);
641 info
->refs
= 2; /* 1 for our caller and 1 for thread we created */
642 info
->package
= package
;
644 info
->target
= strdupW( target
);
645 info
->source
= strdupW( source
);
646 info
->action
= strdupW( action
);
647 CoCreateGuid( &info
->guid
);
649 EnterCriticalSection( &msi_custom_action_cs
);
650 list_add_tail( &msi_pending_custom_actions
, &info
->entry
);
651 LeaveCriticalSection( &msi_custom_action_cs
);
653 info
->handle
= CreateThread( NULL
, 0, DllThread
, &info
->guid
, 0, NULL
);
656 /* release both references */
657 release_custom_action_data( info
);
658 release_custom_action_data( info
);
665 static UINT
HANDLE_CustomType1( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
666 INT type
, const WCHAR
*action
)
668 msi_custom_action_info
*info
;
671 if (!(binary
= get_temp_binary( package
, source
, TRUE
)))
672 return ERROR_FUNCTION_FAILED
;
674 TRACE("Calling function %s from %s\n", debugstr_w(target
), debugstr_w(binary
->tmpfile
));
676 info
= do_msidbCustomActionTypeDll( package
, type
, binary
->tmpfile
, target
, action
);
677 return wait_thread_handle( info
);
680 static HANDLE
execute_command( const WCHAR
*app
, WCHAR
*arg
, const WCHAR
*dir
)
682 static const WCHAR dotexeW
[] = {'.','e','x','e',0};
684 PROCESS_INFORMATION info
;
685 WCHAR
*exe
= NULL
, *cmd
= NULL
, *p
;
693 if (!(exe
= msi_alloc( MAX_PATH
* sizeof(WCHAR
) ))) return INVALID_HANDLE_VALUE
;
694 len_exe
= SearchPathW( NULL
, app
, dotexeW
, MAX_PATH
, exe
, NULL
);
695 if (len_exe
>= MAX_PATH
)
698 if (!(exe
= msi_alloc( len_exe
* sizeof(WCHAR
) ))) return INVALID_HANDLE_VALUE
;
699 len_exe
= SearchPathW( NULL
, app
, dotexeW
, len_exe
, exe
, NULL
);
703 ERR("can't find executable %u\n", GetLastError());
705 return INVALID_HANDLE_VALUE
;
708 if (arg
) len_arg
= strlenW( arg
);
709 if (!(cmd
= msi_alloc( (len_exe
+ len_arg
+ 4) * sizeof(WCHAR
) )))
712 return INVALID_HANDLE_VALUE
;
715 if (strchrW( exe
, ' ' ))
718 memcpy( p
, exe
, len_exe
* sizeof(WCHAR
) );
731 memcpy( p
, arg
, len_arg
* sizeof(WCHAR
) );
735 memset( &si
, 0, sizeof(STARTUPINFOW
) );
736 ret
= CreateProcessW( exe
, exe
? cmd
: arg
, NULL
, NULL
, FALSE
, 0, NULL
, dir
, &si
, &info
);
741 ERR("unable to execute command %u\n", GetLastError());
742 return INVALID_HANDLE_VALUE
;
744 CloseHandle( info
.hThread
);
745 return info
.hProcess
;
748 static UINT
HANDLE_CustomType2( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
749 INT type
, const WCHAR
*action
)
755 if (!(binary
= get_temp_binary( package
, source
, FALSE
))) return ERROR_FUNCTION_FAILED
;
757 deformat_string( package
, target
, &arg
);
758 TRACE("exe %s arg %s\n", debugstr_w(binary
->tmpfile
), debugstr_w(arg
));
760 handle
= execute_command( binary
->tmpfile
, arg
, szCRoot
);
762 if (handle
== INVALID_HANDLE_VALUE
) return ERROR_SUCCESS
;
763 return wait_process_handle( package
, type
, handle
, action
);
766 static UINT
HANDLE_CustomType17( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
767 INT type
, const WCHAR
*action
)
769 msi_custom_action_info
*info
;
772 TRACE("%s %s\n", debugstr_w(source
), debugstr_w(target
));
774 file
= msi_get_loaded_file( package
, source
);
777 ERR("invalid file key %s\n", debugstr_w( source
));
778 return ERROR_FUNCTION_FAILED
;
781 info
= do_msidbCustomActionTypeDll( package
, type
, file
->TargetPath
, target
, action
);
782 return wait_thread_handle( info
);
785 static UINT
HANDLE_CustomType18( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
786 INT type
, const WCHAR
*action
)
792 if (!(file
= msi_get_loaded_file( package
, source
))) return ERROR_FUNCTION_FAILED
;
794 deformat_string( package
, target
, &arg
);
795 TRACE("exe %s arg %s\n", debugstr_w(file
->TargetPath
), debugstr_w(arg
));
797 handle
= execute_command( file
->TargetPath
, arg
, szCRoot
);
799 if (handle
== INVALID_HANDLE_VALUE
) return ERROR_SUCCESS
;
800 return wait_process_handle( package
, type
, handle
, action
);
803 static UINT
HANDLE_CustomType19( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
804 INT type
, const WCHAR
*action
)
806 static const WCHAR query
[] = {
807 'S','E','L','E','C','T',' ','`','M','e','s','s','a','g','e','`',' ',
808 'F','R','O','M',' ','`','E','r','r','o','r','`',' ',
809 'W','H','E','R','E',' ','`','E','r','r','o','r','`',' ','=',' ',
813 LPWSTR deformated
= NULL
;
815 deformat_string( package
, target
, &deformated
);
817 /* first try treat the error as a number */
818 row
= MSI_QueryGetRecord( package
->db
, query
, deformated
);
821 LPCWSTR error
= MSI_RecordGetString( row
, 1 );
822 if ((package
->ui_level
& INSTALLUILEVEL_MASK
) != INSTALLUILEVEL_NONE
)
823 MessageBoxW( NULL
, error
, NULL
, MB_OK
);
824 msiobj_release( &row
->hdr
);
826 else if ((package
->ui_level
& INSTALLUILEVEL_MASK
) != INSTALLUILEVEL_NONE
)
827 MessageBoxW( NULL
, deformated
, NULL
, MB_OK
);
829 msi_free( deformated
);
831 return ERROR_INSTALL_FAILURE
;
834 static UINT
HANDLE_CustomType23( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
835 INT type
, const WCHAR
*action
)
837 static const WCHAR msiexecW
[] = {'m','s','i','e','x','e','c',0};
838 static const WCHAR paramsW
[] = {'/','q','b',' ','/','i',' '};
839 WCHAR
*dir
, *arg
, *p
;
840 UINT len_src
, len_dir
, len_tgt
, len
= sizeof(paramsW
)/sizeof(paramsW
[0]);
843 if (!(dir
= msi_dup_property( package
->db
, szOriginalDatabase
))) return ERROR_OUTOFMEMORY
;
844 if (!(p
= strrchrW( dir
, '\\' )) && !(p
= strrchrW( dir
, '/' )))
847 return ERROR_FUNCTION_FAILED
;
851 len_src
= strlenW( source
);
852 len_tgt
= strlenW( target
);
853 if (!(arg
= msi_alloc( (len
+ len_dir
+ len_src
+ len_tgt
+ 5) * sizeof(WCHAR
) )))
856 return ERROR_OUTOFMEMORY
;
858 memcpy( arg
, paramsW
, sizeof(paramsW
) );
860 memcpy( arg
+ len
, dir
, len_dir
* sizeof(WCHAR
) );
863 memcpy( arg
+ len
, source
, len_src
* sizeof(WCHAR
) );
867 strcpyW( arg
+ len
, target
);
869 TRACE("installing %s concurrently\n", debugstr_w(source
));
871 handle
= execute_command( msiexecW
, arg
, dir
);
874 if (handle
== INVALID_HANDLE_VALUE
) return ERROR_SUCCESS
;
875 return wait_process_handle( package
, type
, handle
, action
);
878 static UINT
HANDLE_CustomType50( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
879 INT type
, const WCHAR
*action
)
884 if (!(exe
= msi_dup_property( package
->db
, source
))) return ERROR_SUCCESS
;
886 deformat_string( package
, target
, &arg
);
887 TRACE("exe %s arg %s\n", debugstr_w(exe
), debugstr_w(arg
));
889 handle
= execute_command( exe
, arg
, szCRoot
);
892 if (handle
== INVALID_HANDLE_VALUE
) return ERROR_SUCCESS
;
893 return wait_process_handle( package
, type
, handle
, action
);
896 static UINT
HANDLE_CustomType34( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
897 INT type
, const WCHAR
*action
)
899 const WCHAR
*workingdir
= NULL
;
905 workingdir
= msi_get_target_folder( package
, source
);
906 if (!workingdir
) return ERROR_FUNCTION_FAILED
;
908 deformat_string( package
, target
, &cmd
);
909 if (!cmd
) return ERROR_FUNCTION_FAILED
;
911 TRACE("cmd %s dir %s\n", debugstr_w(cmd
), debugstr_w(workingdir
));
913 handle
= execute_command( NULL
, cmd
, workingdir
);
915 if (handle
== INVALID_HANDLE_VALUE
) return ERROR_SUCCESS
;
916 return wait_process_handle( package
, type
, handle
, action
);
919 static DWORD
ACTION_CallScript( const GUID
*guid
)
921 msi_custom_action_info
*info
;
923 UINT r
= ERROR_FUNCTION_FAILED
;
925 info
= find_action_by_guid( guid
);
928 ERR("failed to find action %s\n", debugstr_guid( guid
) );
929 return ERROR_FUNCTION_FAILED
;
932 TRACE("function %s, script %s\n", debugstr_w( info
->target
), debugstr_w( info
->source
) );
934 hPackage
= alloc_msihandle( &info
->package
->hdr
);
937 r
= call_script( hPackage
, info
->type
, info
->source
, info
->target
, info
->action
);
938 TRACE("script returned %u\n", r
);
939 MsiCloseHandle( hPackage
);
942 ERR("failed to create handle for %p\n", info
->package
);
944 release_custom_action_data( info
);
948 static DWORD WINAPI
ScriptThread( LPVOID arg
)
953 TRACE("custom action (%x) started\n", GetCurrentThreadId() );
955 rc
= ACTION_CallScript( guid
);
957 TRACE("custom action (%x) returned %i\n", GetCurrentThreadId(), rc
);
959 MsiCloseAllHandles();
963 static msi_custom_action_info
*do_msidbCustomActionTypeScript(
964 MSIPACKAGE
*package
, INT type
, LPCWSTR script
, LPCWSTR function
, LPCWSTR action
)
966 msi_custom_action_info
*info
;
968 info
= msi_alloc( sizeof *info
);
972 msiobj_addref( &package
->hdr
);
973 info
->refs
= 2; /* 1 for our caller and 1 for thread we created */
974 info
->package
= package
;
976 info
->target
= strdupW( function
);
977 info
->source
= strdupW( script
);
978 info
->action
= strdupW( action
);
979 CoCreateGuid( &info
->guid
);
981 EnterCriticalSection( &msi_custom_action_cs
);
982 list_add_tail( &msi_pending_custom_actions
, &info
->entry
);
983 LeaveCriticalSection( &msi_custom_action_cs
);
985 info
->handle
= CreateThread( NULL
, 0, ScriptThread
, &info
->guid
, 0, NULL
);
988 /* release both references */
989 release_custom_action_data( info
);
990 release_custom_action_data( info
);
997 static UINT
HANDLE_CustomType37_38( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
998 INT type
, const WCHAR
*action
)
1000 msi_custom_action_info
*info
;
1002 TRACE("%s %s\n", debugstr_w(source
), debugstr_w(target
));
1004 info
= do_msidbCustomActionTypeScript( package
, type
, target
, NULL
, action
);
1005 return wait_thread_handle( info
);
1008 static UINT
HANDLE_CustomType5_6( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
1009 INT type
, const WCHAR
*action
)
1011 static const WCHAR query
[] = {
1012 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1013 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',' ',
1014 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
1015 MSIRECORD
*row
= NULL
;
1016 msi_custom_action_info
*info
;
1017 CHAR
*buffer
= NULL
;
1018 WCHAR
*bufferw
= NULL
;
1022 TRACE("%s %s\n", debugstr_w(source
), debugstr_w(target
));
1024 row
= MSI_QueryGetRecord(package
->db
, query
, source
);
1026 return ERROR_FUNCTION_FAILED
;
1028 r
= MSI_RecordReadStream(row
, 2, NULL
, &sz
);
1029 if (r
!= ERROR_SUCCESS
) goto done
;
1031 buffer
= msi_alloc( sz
+ 1 );
1034 r
= ERROR_FUNCTION_FAILED
;
1038 r
= MSI_RecordReadStream(row
, 2, buffer
, &sz
);
1039 if (r
!= ERROR_SUCCESS
)
1043 bufferw
= strdupAtoW(buffer
);
1046 r
= ERROR_FUNCTION_FAILED
;
1050 info
= do_msidbCustomActionTypeScript( package
, type
, bufferw
, target
, action
);
1051 r
= wait_thread_handle( info
);
1056 msiobj_release(&row
->hdr
);
1060 static UINT
HANDLE_CustomType21_22( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
1061 INT type
, const WCHAR
*action
)
1063 msi_custom_action_info
*info
;
1066 DWORD sz
, szHighWord
= 0, read
;
1068 WCHAR
*bufferw
=NULL
;
1072 TRACE("%s %s\n", debugstr_w(source
), debugstr_w(target
));
1074 file
= msi_get_loaded_file(package
, source
);
1077 ERR("invalid file key %s\n", debugstr_w(source
));
1078 return ERROR_FUNCTION_FAILED
;
1081 hFile
= CreateFileW(file
->TargetPath
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, NULL
);
1082 if (hFile
== INVALID_HANDLE_VALUE
) return ERROR_FUNCTION_FAILED
;
1084 sz
= GetFileSize(hFile
, &szHighWord
);
1085 if (sz
== INVALID_FILE_SIZE
|| szHighWord
!= 0)
1088 return ERROR_FUNCTION_FAILED
;
1090 buffer
= msi_alloc( sz
+ 1 );
1094 return ERROR_FUNCTION_FAILED
;
1096 bRet
= ReadFile(hFile
, buffer
, sz
, &read
, NULL
);
1100 r
= ERROR_FUNCTION_FAILED
;
1104 bufferw
= strdupAtoW(buffer
);
1107 r
= ERROR_FUNCTION_FAILED
;
1110 info
= do_msidbCustomActionTypeScript( package
, type
, bufferw
, target
, action
);
1111 r
= wait_thread_handle( info
);
1119 static UINT
HANDLE_CustomType53_54( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
1120 INT type
, const WCHAR
*action
)
1122 msi_custom_action_info
*info
;
1125 TRACE("%s %s\n", debugstr_w(source
), debugstr_w(target
));
1127 prop
= msi_dup_property( package
->db
, source
);
1128 if (!prop
) return ERROR_SUCCESS
;
1130 info
= do_msidbCustomActionTypeScript( package
, type
, prop
, NULL
, action
);
1132 return wait_thread_handle( info
);
1135 static BOOL
action_type_matches_script( UINT type
, UINT script
)
1140 case SCRIPT_INSTALL
:
1141 return !(type
& msidbCustomActionTypeCommit
) && !(type
& msidbCustomActionTypeRollback
);
1143 return (type
& msidbCustomActionTypeCommit
);
1144 case SCRIPT_ROLLBACK
:
1145 return (type
& msidbCustomActionTypeRollback
);
1147 ERR("unhandled script %u\n", script
);
1152 static UINT
defer_custom_action( MSIPACKAGE
*package
, const WCHAR
*action
, UINT type
)
1154 WCHAR
*actiondata
= msi_dup_property( package
->db
, action
);
1155 WCHAR
*usersid
= msi_dup_property( package
->db
, szUserSID
);
1156 WCHAR
*prodcode
= msi_dup_property( package
->db
, szProductCode
);
1157 WCHAR
*deferred
= msi_get_deferred_action( action
, actiondata
, usersid
, prodcode
);
1161 msi_free( actiondata
);
1162 msi_free( usersid
);
1163 msi_free( prodcode
);
1164 return ERROR_OUTOFMEMORY
;
1166 if (type
& msidbCustomActionTypeCommit
)
1168 TRACE("deferring commit action\n");
1169 msi_schedule_action( package
, SCRIPT_COMMIT
, deferred
);
1171 else if (type
& msidbCustomActionTypeRollback
)
1173 TRACE("deferring rollback action\n");
1174 msi_schedule_action( package
, SCRIPT_ROLLBACK
, deferred
);
1178 TRACE("deferring install action\n");
1179 msi_schedule_action( package
, SCRIPT_INSTALL
, deferred
);
1182 msi_free( actiondata
);
1183 msi_free( usersid
);
1184 msi_free( prodcode
);
1185 msi_free( deferred
);
1186 return ERROR_SUCCESS
;
1189 UINT
ACTION_CustomAction( MSIPACKAGE
*package
, LPCWSTR action
, UINT script
)
1191 static const WCHAR query
[] = {
1192 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1193 '`','C','u','s','t','o','m','A','c','t','i','o','n','`',' ','W','H','E','R','E',' ',
1194 '`','A','c','t','i' ,'o','n','`',' ','=',' ','\'','%','s','\'',0};
1195 UINT rc
= ERROR_SUCCESS
;
1198 const WCHAR
*source
, *target
, *ptr
, *deferred_data
= NULL
;
1199 WCHAR
*deformated
= NULL
;
1202 /* deferred action: [properties]Action */
1203 if ((ptr
= strrchrW(action
, ']')))
1205 deferred_data
= action
;
1209 row
= MSI_QueryGetRecord( package
->db
, query
, action
);
1211 return ERROR_FUNCTION_NOT_CALLED
;
1213 type
= MSI_RecordGetInteger(row
,2);
1214 source
= MSI_RecordGetString(row
,3);
1215 target
= MSI_RecordGetString(row
,4);
1217 TRACE("Handling custom action %s (%x %s %s)\n",debugstr_w(action
),type
,
1218 debugstr_w(source
), debugstr_w(target
));
1220 /* handle some of the deferred actions */
1221 if (type
& msidbCustomActionTypeTSAware
)
1222 FIXME("msidbCustomActionTypeTSAware not handled\n");
1224 if (type
& msidbCustomActionTypeInScript
)
1226 if (type
& msidbCustomActionTypeNoImpersonate
)
1227 WARN("msidbCustomActionTypeNoImpersonate not handled\n");
1229 if (!action_type_matches_script( type
, script
))
1231 rc
= defer_custom_action( package
, action
, type
);
1236 LPWSTR actiondata
= msi_dup_property( package
->db
, action
);
1238 if (type
& msidbCustomActionTypeInScript
)
1239 package
->scheduled_action_running
= TRUE
;
1241 if (type
& msidbCustomActionTypeCommit
)
1242 package
->commit_action_running
= TRUE
;
1244 if (type
& msidbCustomActionTypeRollback
)
1245 package
->rollback_action_running
= TRUE
;
1248 set_deferred_action_props(package
, deferred_data
);
1249 else if (actiondata
)
1250 msi_set_property( package
->db
, szCustomActionData
, actiondata
, -1 );
1252 msi_set_property( package
->db
, szCustomActionData
, szEmpty
, -1 );
1254 msi_free(actiondata
);
1257 else if (!check_execution_scheduling_options(package
,action
,type
))
1263 switch (type
& CUSTOM_ACTION_TYPE_MASK
)
1265 case 1: /* DLL file stored in a Binary table stream */
1266 rc
= HANDLE_CustomType1(package
,source
,target
,type
,action
);
1268 case 2: /* EXE file stored in a Binary table stream */
1269 rc
= HANDLE_CustomType2(package
,source
,target
,type
,action
);
1271 case 18: /*EXE file installed with package */
1272 rc
= HANDLE_CustomType18(package
,source
,target
,type
,action
);
1274 case 19: /* Error that halts install */
1275 rc
= HANDLE_CustomType19(package
,source
,target
,type
,action
);
1278 rc
= HANDLE_CustomType17(package
,source
,target
,type
,action
);
1280 case 23: /* installs another package in the source tree */
1281 deformat_string(package
,target
,&deformated
);
1282 rc
= HANDLE_CustomType23(package
,source
,deformated
,type
,action
);
1283 msi_free(deformated
);
1285 case 50: /*EXE file specified by a property value */
1286 rc
= HANDLE_CustomType50(package
,source
,target
,type
,action
);
1288 case 34: /*EXE to be run in specified directory */
1289 rc
= HANDLE_CustomType34(package
,source
,target
,type
,action
);
1291 case 35: /* Directory set with formatted text. */
1292 deformat_string(package
,target
,&deformated
);
1293 MSI_SetTargetPathW(package
, source
, deformated
);
1294 msi_free(deformated
);
1296 case 51: /* Property set with formatted text. */
1300 len
= deformat_string( package
, target
, &deformated
);
1301 rc
= msi_set_property( package
->db
, source
, deformated
, len
);
1302 if (rc
== ERROR_SUCCESS
&& !strcmpW( source
, szSourceDir
))
1303 msi_reset_folders( package
, TRUE
);
1304 msi_free(deformated
);
1306 case 37: /* JScript/VBScript text stored in target column. */
1308 rc
= HANDLE_CustomType37_38(package
,source
,target
,type
,action
);
1311 case 6: /* JScript/VBScript file stored in a Binary table stream. */
1312 rc
= HANDLE_CustomType5_6(package
,source
,target
,type
,action
);
1314 case 21: /* JScript/VBScript file installed with the product. */
1316 rc
= HANDLE_CustomType21_22(package
,source
,target
,type
,action
);
1318 case 53: /* JScript/VBScript text specified by a property value. */
1320 rc
= HANDLE_CustomType53_54(package
,source
,target
,type
,action
);
1323 FIXME("unhandled action type %u (%s %s)\n", type
& CUSTOM_ACTION_TYPE_MASK
,
1324 debugstr_w(source
), debugstr_w(target
));
1328 package
->scheduled_action_running
= FALSE
;
1329 package
->commit_action_running
= FALSE
;
1330 package
->rollback_action_running
= FALSE
;
1331 msiobj_release(&row
->hdr
);
1335 void ACTION_FinishCustomActions(const MSIPACKAGE
* package
)
1338 HANDLE
*wait_handles
;
1339 unsigned int handle_count
, i
;
1340 msi_custom_action_info
*info
, *cursor
;
1342 while ((item
= list_head( &package
->RunningActions
)))
1344 MSIRUNNINGACTION
*action
= LIST_ENTRY( item
, MSIRUNNINGACTION
, entry
);
1346 list_remove( &action
->entry
);
1348 TRACE("waiting for %s\n", debugstr_w( action
->name
) );
1349 msi_dialog_check_messages( action
->handle
);
1351 CloseHandle( action
->handle
);
1352 msi_free( action
->name
);
1356 EnterCriticalSection( &msi_custom_action_cs
);
1358 handle_count
= list_count( &msi_pending_custom_actions
);
1359 wait_handles
= msi_alloc( handle_count
* sizeof(HANDLE
) );
1362 LIST_FOR_EACH_ENTRY_SAFE( info
, cursor
, &msi_pending_custom_actions
, msi_custom_action_info
, entry
)
1364 if (info
->package
== package
)
1366 if (DuplicateHandle(GetCurrentProcess(), info
->handle
, GetCurrentProcess(), &wait_handles
[handle_count
], SYNCHRONIZE
, FALSE
, 0))
1371 LeaveCriticalSection( &msi_custom_action_cs
);
1373 for (i
= 0; i
< handle_count
; i
++)
1375 msi_dialog_check_messages( wait_handles
[i
] );
1376 CloseHandle( wait_handles
[i
] );
1378 msi_free( wait_handles
);
1380 EnterCriticalSection( &msi_custom_action_cs
);
1381 LIST_FOR_EACH_ENTRY_SAFE( info
, cursor
, &msi_pending_custom_actions
, msi_custom_action_info
, entry
)
1383 if (info
->package
== package
) release_custom_action_data( info
);
1385 LeaveCriticalSection( &msi_custom_action_cs
);
1388 typedef struct _msi_custom_remote_impl
{
1389 IWineMsiRemoteCustomAction IWineMsiRemoteCustomAction_iface
;
1391 } msi_custom_remote_impl
;
1393 static inline msi_custom_remote_impl
*impl_from_IWineMsiRemoteCustomAction( IWineMsiRemoteCustomAction
*iface
)
1395 return CONTAINING_RECORD(iface
, msi_custom_remote_impl
, IWineMsiRemoteCustomAction_iface
);
1398 static HRESULT WINAPI
mcr_QueryInterface( IWineMsiRemoteCustomAction
*iface
,
1399 REFIID riid
,LPVOID
*ppobj
)
1401 if( IsEqualCLSID( riid
, &IID_IUnknown
) ||
1402 IsEqualCLSID( riid
, &IID_IWineMsiRemoteCustomAction
) )
1404 IWineMsiRemoteCustomAction_AddRef( iface
);
1409 return E_NOINTERFACE
;
1412 static ULONG WINAPI
mcr_AddRef( IWineMsiRemoteCustomAction
*iface
)
1414 msi_custom_remote_impl
* This
= impl_from_IWineMsiRemoteCustomAction( iface
);
1416 return InterlockedIncrement( &This
->refs
);
1419 static ULONG WINAPI
mcr_Release( IWineMsiRemoteCustomAction
*iface
)
1421 msi_custom_remote_impl
* This
= impl_from_IWineMsiRemoteCustomAction( iface
);
1424 r
= InterlockedDecrement( &This
->refs
);
1430 static HRESULT WINAPI
mcr_GetActionInfo( IWineMsiRemoteCustomAction
*iface
, LPCGUID custom_action_guid
,
1431 INT
*type
, MSIHANDLE
*handle
, BSTR
*dll
, BSTR
*func
, IWineMsiRemotePackage
**remote_package
)
1433 msi_custom_action_info
*info
;
1435 info
= find_action_by_guid( custom_action_guid
);
1440 *handle
= alloc_msihandle( &info
->package
->hdr
);
1441 *dll
= SysAllocString( info
->source
);
1442 *func
= SysAllocString( info
->target
);
1444 release_custom_action_data( info
);
1445 return create_msi_remote_package( NULL
, (LPVOID
*)remote_package
);
1448 static const IWineMsiRemoteCustomActionVtbl msi_custom_remote_vtbl
=
1456 HRESULT
create_msi_custom_remote( IUnknown
*pOuter
, LPVOID
*ppObj
)
1458 msi_custom_remote_impl
* This
;
1460 This
= msi_alloc( sizeof *This
);
1462 return E_OUTOFMEMORY
;
1464 This
->IWineMsiRemoteCustomAction_iface
.lpVtbl
= &msi_custom_remote_vtbl
;
1467 *ppObj
= &This
->IWineMsiRemoteCustomAction_iface
;