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
23 #include <wine/exception.h>
26 #include "msvchelper.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(msi
);
31 #define CUSTOM_ACTION_TYPE_MASK 0x3F
33 typedef struct tagMSIRUNNINGACTION
41 typedef UINT (WINAPI
*MsiCustomActionEntryPoint
)( MSIHANDLE
);
43 static CRITICAL_SECTION msi_custom_action_cs
;
44 static CRITICAL_SECTION_DEBUG msi_custom_action_cs_debug
=
46 0, 0, &msi_custom_action_cs
,
47 { &msi_custom_action_cs_debug
.ProcessLocksList
,
48 &msi_custom_action_cs_debug
.ProcessLocksList
},
49 0, 0, { (DWORD_PTR
)(__FILE__
": msi_custom_action_cs") }
51 static CRITICAL_SECTION msi_custom_action_cs
= { &msi_custom_action_cs_debug
, -1, 0, 0, 0, 0 };
53 static struct list msi_pending_custom_actions
= LIST_INIT( msi_pending_custom_actions
);
55 UINT
msi_schedule_action( MSIPACKAGE
*package
, UINT script
, const WCHAR
*action
)
58 WCHAR
**newbuf
= NULL
;
60 if (script
>= SCRIPT_MAX
)
62 FIXME("Unknown script requested %u\n", script
);
63 return ERROR_FUNCTION_FAILED
;
65 TRACE("Scheduling action %s in script %u\n", debugstr_w(action
), script
);
67 count
= package
->script_actions_count
[script
];
68 package
->script_actions_count
[script
]++;
69 if (count
!= 0) newbuf
= msi_realloc( package
->script_actions
[script
],
70 package
->script_actions_count
[script
] * sizeof(WCHAR
*) );
71 else newbuf
= msi_alloc( sizeof(WCHAR
*) );
73 newbuf
[count
] = strdupW( action
);
74 package
->script_actions
[script
] = newbuf
;
78 UINT
msi_register_unique_action( MSIPACKAGE
*package
, const WCHAR
*action
)
81 WCHAR
**newbuf
= NULL
;
83 TRACE("Registering %s as unique action\n", debugstr_w(action
));
85 count
= package
->unique_actions_count
;
86 package
->unique_actions_count
++;
87 if (count
!= 0) newbuf
= msi_realloc( package
->unique_actions
,
88 package
->unique_actions_count
* sizeof(WCHAR
*) );
89 else newbuf
= msi_alloc( sizeof(WCHAR
*) );
91 newbuf
[count
] = strdupW( action
);
92 package
->unique_actions
= newbuf
;
96 BOOL
msi_action_is_unique( const MSIPACKAGE
*package
, const WCHAR
*action
)
100 for (i
= 0; i
< package
->unique_actions_count
; i
++)
102 if (!strcmpW( package
->unique_actions
[i
], action
)) return TRUE
;
107 static BOOL
check_execution_scheduling_options(MSIPACKAGE
*package
, LPCWSTR action
, UINT options
)
109 if ((options
& msidbCustomActionTypeClientRepeat
) ==
110 msidbCustomActionTypeClientRepeat
)
112 if (!(package
->InWhatSequence
& SEQUENCE_UI
&&
113 package
->InWhatSequence
& SEQUENCE_EXEC
))
115 TRACE("Skipping action due to dbCustomActionTypeClientRepeat option.\n");
119 else if (options
& msidbCustomActionTypeFirstSequence
)
121 if (package
->InWhatSequence
& SEQUENCE_UI
&&
122 package
->InWhatSequence
& SEQUENCE_EXEC
)
124 TRACE("Skipping action due to msidbCustomActionTypeFirstSequence option.\n");
128 else if (options
& msidbCustomActionTypeOncePerProcess
)
130 if (msi_action_is_unique(package
, action
))
132 TRACE("Skipping action due to msidbCustomActionTypeOncePerProcess option.\n");
136 msi_register_unique_action(package
, action
);
142 /* stores the following properties before the action:
144 * [CustomActionData<=>UserSID<=>ProductCode]Action
146 static LPWSTR
msi_get_deferred_action(LPCWSTR action
, LPCWSTR actiondata
,
147 LPCWSTR usersid
, LPCWSTR prodcode
)
152 static const WCHAR format
[] = {
153 '[','%','s','<','=','>','%','s','<','=','>','%','s',']','%','s',0
157 return strdupW(action
);
159 len
= lstrlenW(action
) + lstrlenW(actiondata
) +
160 lstrlenW(usersid
) + lstrlenW(prodcode
) +
161 lstrlenW(format
) - 7;
162 deferred
= msi_alloc(len
* sizeof(WCHAR
));
164 sprintfW(deferred
, format
, actiondata
, usersid
, prodcode
, action
);
168 static void set_deferred_action_props( MSIPACKAGE
*package
, const WCHAR
*deferred_data
)
170 static const WCHAR sep
[] = {'<','=','>',0};
171 const WCHAR
*end
, *beg
= deferred_data
+ 1;
173 end
= strstrW(beg
, sep
);
174 msi_set_property( package
->db
, szCustomActionData
, beg
, end
- beg
);
177 end
= strstrW(beg
, sep
);
178 msi_set_property( package
->db
, szUserSID
, beg
, end
- beg
);
181 end
= strchrW(beg
, ']');
182 msi_set_property( package
->db
, szProductCode
, beg
, end
- beg
);
185 WCHAR
*msi_create_temp_file( MSIDATABASE
*db
)
192 UINT len
= sizeof(tmp
)/sizeof(tmp
[0]);
194 if (msi_get_property( db
, szTempFolder
, tmp
, &len
) ||
195 GetFileAttributesW( tmp
) != FILE_ATTRIBUTE_DIRECTORY
)
197 GetTempPathW( MAX_PATH
, tmp
);
199 if (!(db
->tempfolder
= strdupW( tmp
))) return NULL
;
202 if ((ret
= msi_alloc( (strlenW( db
->tempfolder
) + 20) * sizeof(WCHAR
) )))
204 if (!GetTempFileNameW( db
->tempfolder
, szMsi
, 0, ret
))
214 static MSIBINARY
*create_temp_binary( MSIPACKAGE
*package
, LPCWSTR source
, BOOL dll
)
216 static const WCHAR query
[] = {
217 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
218 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',' ',
219 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
221 MSIBINARY
*binary
= NULL
;
228 if (!(tmpfile
= msi_create_temp_file( package
->db
))) return NULL
;
230 if (!(row
= MSI_QueryGetRecord( package
->db
, query
, source
))) goto error
;
231 if (!(binary
= msi_alloc_zero( sizeof(MSIBINARY
) ))) goto error
;
233 file
= CreateFileW( tmpfile
, GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
234 if (file
== INVALID_HANDLE_VALUE
) goto error
;
239 r
= MSI_RecordReadStream( row
, 2, buffer
, &sz
);
240 if (r
!= ERROR_SUCCESS
)
242 ERR("Failed to get stream\n");
245 WriteFile( file
, buffer
, sz
, &write
, NULL
);
246 } while (sz
== sizeof buffer
);
249 if (r
!= ERROR_SUCCESS
) goto error
;
251 /* keep a reference to prevent the dll from being unloaded */
252 if (dll
&& !(binary
->module
= LoadLibraryW( tmpfile
)))
254 ERR( "failed to load dll %s (%u)\n", debugstr_w( tmpfile
), GetLastError() );
256 binary
->source
= strdupW( source
);
257 binary
->tmpfile
= tmpfile
;
258 list_add_tail( &package
->binaries
, &binary
->entry
);
260 msiobj_release( &row
->hdr
);
264 if (row
) msiobj_release( &row
->hdr
);
265 DeleteFileW( tmpfile
);
271 static MSIBINARY
*get_temp_binary( MSIPACKAGE
*package
, LPCWSTR source
, BOOL dll
)
275 LIST_FOR_EACH_ENTRY( binary
, &package
->binaries
, MSIBINARY
, entry
)
277 if (!strcmpW( binary
->source
, source
))
281 return create_temp_binary( package
, source
, dll
);
284 static void file_running_action(MSIPACKAGE
* package
, HANDLE Handle
,
285 BOOL process
, LPCWSTR name
)
287 MSIRUNNINGACTION
*action
;
289 action
= msi_alloc( sizeof(MSIRUNNINGACTION
) );
291 action
->handle
= Handle
;
292 action
->process
= process
;
293 action
->name
= strdupW(name
);
295 list_add_tail( &package
->RunningActions
, &action
->entry
);
298 static UINT
custom_get_process_return( HANDLE process
)
302 GetExitCodeProcess( process
, &rc
);
303 TRACE("exit code is %u\n", rc
);
305 return ERROR_FUNCTION_FAILED
;
306 return ERROR_SUCCESS
;
309 static UINT
custom_get_thread_return( MSIPACKAGE
*package
, HANDLE thread
)
313 GetExitCodeThread( thread
, &rc
);
317 case ERROR_FUNCTION_NOT_CALLED
:
319 case ERROR_INSTALL_USEREXIT
:
320 case ERROR_INSTALL_FAILURE
:
322 case ERROR_NO_MORE_ITEMS
:
323 return ERROR_SUCCESS
;
324 case ERROR_INSTALL_SUSPEND
:
325 ACTION_ForceReboot( package
);
326 return ERROR_SUCCESS
;
328 ERR("Invalid Return Code %d\n",rc
);
329 return ERROR_INSTALL_FAILURE
;
333 static UINT
wait_process_handle(MSIPACKAGE
* package
, UINT type
,
334 HANDLE ProcessHandle
, LPCWSTR name
)
336 UINT rc
= ERROR_SUCCESS
;
338 if (!(type
& msidbCustomActionTypeAsync
))
340 TRACE("waiting for %s\n", debugstr_w(name
));
342 msi_dialog_check_messages(ProcessHandle
);
344 if (!(type
& msidbCustomActionTypeContinue
))
345 rc
= custom_get_process_return(ProcessHandle
);
347 CloseHandle(ProcessHandle
);
351 TRACE("%s running in background\n", debugstr_w(name
));
353 if (!(type
& msidbCustomActionTypeContinue
))
354 file_running_action(package
, ProcessHandle
, TRUE
, name
);
356 CloseHandle(ProcessHandle
);
362 typedef struct _msi_custom_action_info
{
372 } msi_custom_action_info
;
374 static void release_custom_action_data( msi_custom_action_info
*info
)
376 EnterCriticalSection( &msi_custom_action_cs
);
380 list_remove( &info
->entry
);
382 CloseHandle( info
->handle
);
383 msi_free( info
->action
);
384 msi_free( info
->source
);
385 msi_free( info
->target
);
386 msiobj_release( &info
->package
->hdr
);
390 LeaveCriticalSection( &msi_custom_action_cs
);
393 /* must be called inside msi_custom_action_cs if info is in the pending custom actions list */
394 static void addref_custom_action_data( msi_custom_action_info
*info
)
399 static UINT
wait_thread_handle( msi_custom_action_info
*info
)
401 UINT rc
= ERROR_SUCCESS
;
403 if (!(info
->type
& msidbCustomActionTypeAsync
))
405 TRACE("waiting for %s\n", debugstr_w( info
->action
));
407 msi_dialog_check_messages( info
->handle
);
409 if (!(info
->type
& msidbCustomActionTypeContinue
))
410 rc
= custom_get_thread_return( info
->package
, info
->handle
);
412 release_custom_action_data( info
);
416 TRACE("%s running in background\n", debugstr_w( info
->action
));
422 static msi_custom_action_info
*find_action_by_guid( const GUID
*guid
)
424 msi_custom_action_info
*info
;
427 EnterCriticalSection( &msi_custom_action_cs
);
429 LIST_FOR_EACH_ENTRY( info
, &msi_pending_custom_actions
, msi_custom_action_info
, entry
)
431 if (IsEqualGUID( &info
->guid
, guid
))
433 addref_custom_action_data( info
);
439 LeaveCriticalSection( &msi_custom_action_cs
);
447 static void handle_msi_break( LPCWSTR target
)
452 static const WCHAR MsiBreak
[] = { 'M','s','i','B','r','e','a','k',0 };
453 static const WCHAR WindowsInstaller
[] = {
454 'W','i','n','d','o','w','s',' ','I','n','s','t','a','l','l','e','r',0
457 static const WCHAR format
[] = {
458 'T','o',' ','d','e','b','u','g',' ','y','o','u','r',' ',
459 'c','u','s','t','o','m',' ','a','c','t','i','o','n',',',' ',
460 'a','t','t','a','c','h',' ','y','o','u','r',' ','d','e','b','u','g','g','e','r',' ',
461 't','o',' ','p','r','o','c','e','s','s',' ','%','i',' ','(','0','x','%','X',')',' ',
462 'a','n','d',' ','p','r','e','s','s',' ','O','K',0
465 if( !GetEnvironmentVariableW( MsiBreak
, val
, MAX_PATH
))
468 if( strcmpiW( val
, target
))
471 msg
= msi_alloc( (lstrlenW(format
) + 10) * sizeof(WCHAR
) );
475 wsprintfW( msg
, format
, GetCurrentProcessId(), GetCurrentProcessId());
476 MessageBoxW( NULL
, msg
, WindowsInstaller
, MB_OK
);
481 static UINT
get_action_info( const GUID
*guid
, INT
*type
, MSIHANDLE
*handle
,
482 BSTR
*dll
, BSTR
*funcname
,
483 IWineMsiRemotePackage
**package
)
485 IClassFactory
*cf
= NULL
;
486 IWineMsiRemoteCustomAction
*rca
= NULL
;
489 r
= DllGetClassObject( &CLSID_WineMsiRemoteCustomAction
,
490 &IID_IClassFactory
, (LPVOID
*)&cf
);
493 ERR("failed to get IClassFactory interface\n");
494 return ERROR_FUNCTION_FAILED
;
497 r
= IClassFactory_CreateInstance( cf
, NULL
, &IID_IWineMsiRemoteCustomAction
, (LPVOID
*)&rca
);
500 ERR("failed to get IWineMsiRemoteCustomAction interface\n");
501 return ERROR_FUNCTION_FAILED
;
504 r
= IWineMsiRemoteCustomAction_GetActionInfo( rca
, guid
, type
, handle
, dll
, funcname
, package
);
505 IWineMsiRemoteCustomAction_Release( rca
);
508 ERR("GetActionInfo failed\n");
509 return ERROR_FUNCTION_FAILED
;
512 return ERROR_SUCCESS
;
516 extern UINT
CUSTOMPROC_wrapper( MsiCustomActionEntryPoint proc
, MSIHANDLE handle
);
517 __ASM_GLOBAL_FUNC( CUSTOMPROC_wrapper
,
519 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
520 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
522 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
525 "movl 8(%ebp),%eax\n\t"
528 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
529 __ASM_CFI(".cfi_same_value %ebp\n\t")
532 static inline UINT
CUSTOMPROC_wrapper( MsiCustomActionEntryPoint proc
, MSIHANDLE handle
)
538 static DWORD
ACTION_CallDllFunction( const GUID
*guid
)
540 MsiCustomActionEntryPoint fn
;
541 MSIHANDLE hPackage
, handle
;
544 UINT r
= ERROR_FUNCTION_FAILED
;
545 BSTR dll
= NULL
, function
= NULL
;
547 IWineMsiRemotePackage
*remote_package
= NULL
;
549 TRACE("%s\n", debugstr_guid( guid
));
551 r
= get_action_info( guid
, &type
, &handle
, &dll
, &function
, &remote_package
);
552 if (r
!= ERROR_SUCCESS
)
555 hModule
= LoadLibraryW( dll
);
558 ERR( "failed to load dll %s (%u)\n", debugstr_w( dll
), GetLastError() );
559 return ERROR_SUCCESS
;
562 proc
= strdupWtoA( function
);
563 fn
= (MsiCustomActionEntryPoint
) GetProcAddress( hModule
, proc
);
567 hPackage
= alloc_msi_remote_handle( (IUnknown
*)remote_package
);
570 IWineMsiRemotePackage_SetMsiHandle( remote_package
, handle
);
571 TRACE("calling %s\n", debugstr_w( function
) );
572 handle_msi_break( function
);
576 r
= CUSTOMPROC_wrapper( fn
, hPackage
);
580 ERR("Custom action (%s:%s) caused a page fault: %08x\n",
581 debugstr_w(dll
), debugstr_w(function
), GetExceptionCode());
586 MsiCloseHandle( hPackage
);
589 ERR("failed to create handle for %p\n", remote_package
);
592 ERR("GetProcAddress(%s) failed\n", debugstr_w( function
) );
594 FreeLibrary(hModule
);
596 IWineMsiRemotePackage_Release( remote_package
);
597 SysFreeString( dll
);
598 SysFreeString( function
);
599 MsiCloseHandle( handle
);
604 static DWORD WINAPI
DllThread( LPVOID arg
)
609 TRACE("custom action (%x) started\n", GetCurrentThreadId() );
611 rc
= ACTION_CallDllFunction( guid
);
613 TRACE("custom action (%x) returned %i\n", GetCurrentThreadId(), rc
);
615 MsiCloseAllHandles();
619 static msi_custom_action_info
*do_msidbCustomActionTypeDll(
620 MSIPACKAGE
*package
, INT type
, LPCWSTR source
, LPCWSTR target
, LPCWSTR action
)
622 msi_custom_action_info
*info
;
624 info
= msi_alloc( sizeof *info
);
628 msiobj_addref( &package
->hdr
);
629 info
->refs
= 2; /* 1 for our caller and 1 for thread we created */
630 info
->package
= package
;
632 info
->target
= strdupW( target
);
633 info
->source
= strdupW( source
);
634 info
->action
= strdupW( action
);
635 CoCreateGuid( &info
->guid
);
637 EnterCriticalSection( &msi_custom_action_cs
);
638 list_add_tail( &msi_pending_custom_actions
, &info
->entry
);
639 LeaveCriticalSection( &msi_custom_action_cs
);
641 info
->handle
= CreateThread( NULL
, 0, DllThread
, &info
->guid
, 0, NULL
);
644 /* release both references */
645 release_custom_action_data( info
);
646 release_custom_action_data( info
);
653 static UINT
HANDLE_CustomType1( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
654 INT type
, const WCHAR
*action
)
656 msi_custom_action_info
*info
;
659 if (!(binary
= get_temp_binary( package
, source
, TRUE
)))
660 return ERROR_FUNCTION_FAILED
;
662 TRACE("Calling function %s from %s\n", debugstr_w(target
), debugstr_w(binary
->tmpfile
));
664 info
= do_msidbCustomActionTypeDll( package
, type
, binary
->tmpfile
, target
, action
);
665 return wait_thread_handle( info
);
668 static HANDLE
execute_command( const WCHAR
*app
, WCHAR
*arg
, const WCHAR
*dir
)
670 static const WCHAR dotexeW
[] = {'.','e','x','e',0};
672 PROCESS_INFORMATION info
;
673 WCHAR
*exe
= NULL
, *cmd
= NULL
, *p
;
681 if (!(exe
= msi_alloc( MAX_PATH
* sizeof(WCHAR
) ))) return INVALID_HANDLE_VALUE
;
682 len_exe
= SearchPathW( NULL
, app
, dotexeW
, MAX_PATH
, exe
, NULL
);
683 if (len_exe
>= MAX_PATH
)
686 if (!(exe
= msi_alloc( len_exe
* sizeof(WCHAR
) ))) return INVALID_HANDLE_VALUE
;
687 len_exe
= SearchPathW( NULL
, app
, dotexeW
, len_exe
, exe
, NULL
);
691 ERR("can't find executable %u\n", GetLastError());
693 return INVALID_HANDLE_VALUE
;
696 if (arg
) len_arg
= strlenW( arg
);
697 if (!(cmd
= msi_alloc( (len_exe
+ len_arg
+ 4) * sizeof(WCHAR
) )))
700 return INVALID_HANDLE_VALUE
;
703 if (strchrW( exe
, ' ' ))
706 memcpy( p
, exe
, len_exe
* sizeof(WCHAR
) );
719 memcpy( p
, arg
, len_arg
* sizeof(WCHAR
) );
723 memset( &si
, 0, sizeof(STARTUPINFOW
) );
724 ret
= CreateProcessW( exe
, exe
? cmd
: arg
, NULL
, NULL
, FALSE
, 0, NULL
, dir
, &si
, &info
);
729 ERR("unable to execute command %u\n", GetLastError());
730 return INVALID_HANDLE_VALUE
;
732 CloseHandle( info
.hThread
);
733 return info
.hProcess
;
736 static UINT
HANDLE_CustomType2( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
737 INT type
, const WCHAR
*action
)
743 if (!(binary
= get_temp_binary( package
, source
, FALSE
))) return ERROR_FUNCTION_FAILED
;
745 deformat_string( package
, target
, &arg
);
746 TRACE("exe %s arg %s\n", debugstr_w(binary
->tmpfile
), debugstr_w(arg
));
748 handle
= execute_command( binary
->tmpfile
, arg
, szCRoot
);
750 if (handle
== INVALID_HANDLE_VALUE
) return ERROR_SUCCESS
;
751 return wait_process_handle( package
, type
, handle
, action
);
754 static UINT
HANDLE_CustomType17( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
755 INT type
, const WCHAR
*action
)
757 msi_custom_action_info
*info
;
760 TRACE("%s %s\n", debugstr_w(source
), debugstr_w(target
));
762 file
= msi_get_loaded_file( package
, source
);
765 ERR("invalid file key %s\n", debugstr_w( source
));
766 return ERROR_FUNCTION_FAILED
;
769 info
= do_msidbCustomActionTypeDll( package
, type
, file
->TargetPath
, target
, action
);
770 return wait_thread_handle( info
);
773 static UINT
HANDLE_CustomType18( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
774 INT type
, const WCHAR
*action
)
780 if (!(file
= msi_get_loaded_file( package
, source
))) return ERROR_FUNCTION_FAILED
;
782 deformat_string( package
, target
, &arg
);
783 TRACE("exe %s arg %s\n", debugstr_w(file
->TargetPath
), debugstr_w(arg
));
785 handle
= execute_command( file
->TargetPath
, arg
, szCRoot
);
787 if (handle
== INVALID_HANDLE_VALUE
) return ERROR_SUCCESS
;
788 return wait_process_handle( package
, type
, handle
, action
);
791 static UINT
HANDLE_CustomType19( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
792 INT type
, const WCHAR
*action
)
794 static const WCHAR query
[] = {
795 'S','E','L','E','C','T',' ','`','M','e','s','s','a','g','e','`',' ',
796 'F','R','O','M',' ','`','E','r','r','o','r','`',' ',
797 'W','H','E','R','E',' ','`','E','r','r','o','r','`',' ','=',' ',
801 LPWSTR deformated
= NULL
;
803 deformat_string( package
, target
, &deformated
);
805 /* first try treat the error as a number */
806 row
= MSI_QueryGetRecord( package
->db
, query
, deformated
);
809 LPCWSTR error
= MSI_RecordGetString( row
, 1 );
810 if ((package
->ui_level
& INSTALLUILEVEL_MASK
) != INSTALLUILEVEL_NONE
)
811 MessageBoxW( NULL
, error
, NULL
, MB_OK
);
812 msiobj_release( &row
->hdr
);
814 else if ((package
->ui_level
& INSTALLUILEVEL_MASK
) != INSTALLUILEVEL_NONE
)
815 MessageBoxW( NULL
, deformated
, NULL
, MB_OK
);
817 msi_free( deformated
);
819 return ERROR_INSTALL_FAILURE
;
822 static UINT
HANDLE_CustomType23( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
823 INT type
, const WCHAR
*action
)
825 static const WCHAR msiexecW
[] = {'m','s','i','e','x','e','c',0};
826 static const WCHAR paramsW
[] = {'/','q','b',' ','/','i',' '};
827 WCHAR
*dir
, *arg
, *p
;
828 UINT len_src
, len_dir
, len_tgt
, len
= sizeof(paramsW
)/sizeof(paramsW
[0]);
831 if (!(dir
= msi_dup_property( package
->db
, szOriginalDatabase
))) return ERROR_OUTOFMEMORY
;
832 if (!(p
= strrchrW( dir
, '\\' )) && !(p
= strrchrW( dir
, '/' )))
835 return ERROR_FUNCTION_FAILED
;
839 len_src
= strlenW( source
);
840 len_tgt
= strlenW( target
);
841 if (!(arg
= msi_alloc( (len
+ len_dir
+ len_src
+ len_tgt
+ 5) * sizeof(WCHAR
) )))
844 return ERROR_OUTOFMEMORY
;
846 memcpy( arg
, paramsW
, sizeof(paramsW
) );
848 memcpy( arg
+ len
, dir
, len_dir
* sizeof(WCHAR
) );
851 memcpy( arg
+ len
, source
, len_src
* sizeof(WCHAR
) );
855 strcpyW( arg
+ len
, target
);
857 TRACE("installing %s concurrently\n", debugstr_w(source
));
859 handle
= execute_command( msiexecW
, arg
, dir
);
862 if (handle
== INVALID_HANDLE_VALUE
) return ERROR_SUCCESS
;
863 return wait_process_handle( package
, type
, handle
, action
);
866 static UINT
HANDLE_CustomType50( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
867 INT type
, const WCHAR
*action
)
872 if (!(exe
= msi_dup_property( package
->db
, source
))) return ERROR_SUCCESS
;
874 deformat_string( package
, target
, &arg
);
875 TRACE("exe %s arg %s\n", debugstr_w(exe
), debugstr_w(arg
));
877 handle
= execute_command( exe
, arg
, szCRoot
);
880 if (handle
== INVALID_HANDLE_VALUE
) return ERROR_SUCCESS
;
881 return wait_process_handle( package
, type
, handle
, action
);
884 static UINT
HANDLE_CustomType34( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
885 INT type
, const WCHAR
*action
)
887 const WCHAR
*workingdir
= NULL
;
893 workingdir
= msi_get_target_folder( package
, source
);
894 if (!workingdir
) return ERROR_FUNCTION_FAILED
;
896 deformat_string( package
, target
, &cmd
);
897 if (!cmd
) return ERROR_FUNCTION_FAILED
;
899 TRACE("cmd %s dir %s\n", debugstr_w(cmd
), debugstr_w(workingdir
));
901 handle
= execute_command( NULL
, cmd
, workingdir
);
903 if (handle
== INVALID_HANDLE_VALUE
) return ERROR_SUCCESS
;
904 return wait_process_handle( package
, type
, handle
, action
);
907 static DWORD
ACTION_CallScript( const GUID
*guid
)
909 msi_custom_action_info
*info
;
911 UINT r
= ERROR_FUNCTION_FAILED
;
913 info
= find_action_by_guid( guid
);
916 ERR("failed to find action %s\n", debugstr_guid( guid
) );
917 return ERROR_FUNCTION_FAILED
;
920 TRACE("function %s, script %s\n", debugstr_w( info
->target
), debugstr_w( info
->source
) );
922 hPackage
= alloc_msihandle( &info
->package
->hdr
);
925 r
= call_script( hPackage
, info
->type
, info
->source
, info
->target
, info
->action
);
926 TRACE("script returned %u\n", r
);
927 MsiCloseHandle( hPackage
);
930 ERR("failed to create handle for %p\n", info
->package
);
932 release_custom_action_data( info
);
936 static DWORD WINAPI
ScriptThread( LPVOID arg
)
941 TRACE("custom action (%x) started\n", GetCurrentThreadId() );
943 rc
= ACTION_CallScript( guid
);
945 TRACE("custom action (%x) returned %i\n", GetCurrentThreadId(), rc
);
947 MsiCloseAllHandles();
951 static msi_custom_action_info
*do_msidbCustomActionTypeScript(
952 MSIPACKAGE
*package
, INT type
, LPCWSTR script
, LPCWSTR function
, LPCWSTR action
)
954 msi_custom_action_info
*info
;
956 info
= msi_alloc( sizeof *info
);
960 msiobj_addref( &package
->hdr
);
961 info
->refs
= 2; /* 1 for our caller and 1 for thread we created */
962 info
->package
= package
;
964 info
->target
= strdupW( function
);
965 info
->source
= strdupW( script
);
966 info
->action
= strdupW( action
);
967 CoCreateGuid( &info
->guid
);
969 EnterCriticalSection( &msi_custom_action_cs
);
970 list_add_tail( &msi_pending_custom_actions
, &info
->entry
);
971 LeaveCriticalSection( &msi_custom_action_cs
);
973 info
->handle
= CreateThread( NULL
, 0, ScriptThread
, &info
->guid
, 0, NULL
);
976 /* release both references */
977 release_custom_action_data( info
);
978 release_custom_action_data( info
);
985 static UINT
HANDLE_CustomType37_38( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
986 INT type
, const WCHAR
*action
)
988 msi_custom_action_info
*info
;
990 TRACE("%s %s\n", debugstr_w(source
), debugstr_w(target
));
992 info
= do_msidbCustomActionTypeScript( package
, type
, target
, NULL
, action
);
993 return wait_thread_handle( info
);
996 static UINT
HANDLE_CustomType5_6( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
997 INT type
, const WCHAR
*action
)
999 static const WCHAR query
[] = {
1000 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1001 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',' ',
1002 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
1003 MSIRECORD
*row
= NULL
;
1004 msi_custom_action_info
*info
;
1005 CHAR
*buffer
= NULL
;
1006 WCHAR
*bufferw
= NULL
;
1010 TRACE("%s %s\n", debugstr_w(source
), debugstr_w(target
));
1012 row
= MSI_QueryGetRecord(package
->db
, query
, source
);
1014 return ERROR_FUNCTION_FAILED
;
1016 r
= MSI_RecordReadStream(row
, 2, NULL
, &sz
);
1017 if (r
!= ERROR_SUCCESS
) goto done
;
1019 buffer
= msi_alloc( sz
+ 1 );
1022 r
= ERROR_FUNCTION_FAILED
;
1026 r
= MSI_RecordReadStream(row
, 2, buffer
, &sz
);
1027 if (r
!= ERROR_SUCCESS
)
1031 bufferw
= strdupAtoW(buffer
);
1034 r
= ERROR_FUNCTION_FAILED
;
1038 info
= do_msidbCustomActionTypeScript( package
, type
, bufferw
, target
, action
);
1039 r
= wait_thread_handle( info
);
1044 msiobj_release(&row
->hdr
);
1048 static UINT
HANDLE_CustomType21_22( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
1049 INT type
, const WCHAR
*action
)
1051 msi_custom_action_info
*info
;
1054 DWORD sz
, szHighWord
= 0, read
;
1056 WCHAR
*bufferw
=NULL
;
1060 TRACE("%s %s\n", debugstr_w(source
), debugstr_w(target
));
1062 file
= msi_get_loaded_file(package
, source
);
1065 ERR("invalid file key %s\n", debugstr_w(source
));
1066 return ERROR_FUNCTION_FAILED
;
1069 hFile
= CreateFileW(file
->TargetPath
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, NULL
);
1070 if (hFile
== INVALID_HANDLE_VALUE
) return ERROR_FUNCTION_FAILED
;
1072 sz
= GetFileSize(hFile
, &szHighWord
);
1073 if (sz
== INVALID_FILE_SIZE
|| szHighWord
!= 0)
1076 return ERROR_FUNCTION_FAILED
;
1078 buffer
= msi_alloc( sz
+ 1 );
1082 return ERROR_FUNCTION_FAILED
;
1084 bRet
= ReadFile(hFile
, buffer
, sz
, &read
, NULL
);
1088 r
= ERROR_FUNCTION_FAILED
;
1092 bufferw
= strdupAtoW(buffer
);
1095 r
= ERROR_FUNCTION_FAILED
;
1098 info
= do_msidbCustomActionTypeScript( package
, type
, bufferw
, target
, action
);
1099 r
= wait_thread_handle( info
);
1107 static UINT
HANDLE_CustomType53_54( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
1108 INT type
, const WCHAR
*action
)
1110 msi_custom_action_info
*info
;
1113 TRACE("%s %s\n", debugstr_w(source
), debugstr_w(target
));
1115 prop
= msi_dup_property( package
->db
, source
);
1116 if (!prop
) return ERROR_SUCCESS
;
1118 info
= do_msidbCustomActionTypeScript( package
, type
, prop
, NULL
, action
);
1120 return wait_thread_handle( info
);
1123 static BOOL
action_type_matches_script( UINT type
, UINT script
)
1128 case SCRIPT_INSTALL
:
1129 return !(type
& msidbCustomActionTypeCommit
) && !(type
& msidbCustomActionTypeRollback
);
1131 return (type
& msidbCustomActionTypeCommit
);
1132 case SCRIPT_ROLLBACK
:
1133 return (type
& msidbCustomActionTypeRollback
);
1135 ERR("unhandled script %u\n", script
);
1140 static UINT
defer_custom_action( MSIPACKAGE
*package
, const WCHAR
*action
, UINT type
)
1142 WCHAR
*actiondata
= msi_dup_property( package
->db
, action
);
1143 WCHAR
*usersid
= msi_dup_property( package
->db
, szUserSID
);
1144 WCHAR
*prodcode
= msi_dup_property( package
->db
, szProductCode
);
1145 WCHAR
*deferred
= msi_get_deferred_action( action
, actiondata
, usersid
, prodcode
);
1149 msi_free( actiondata
);
1150 msi_free( usersid
);
1151 msi_free( prodcode
);
1152 return ERROR_OUTOFMEMORY
;
1154 if (type
& msidbCustomActionTypeCommit
)
1156 TRACE("deferring commit action\n");
1157 msi_schedule_action( package
, SCRIPT_COMMIT
, deferred
);
1159 else if (type
& msidbCustomActionTypeRollback
)
1161 TRACE("deferring rollback action\n");
1162 msi_schedule_action( package
, SCRIPT_ROLLBACK
, deferred
);
1166 TRACE("deferring install action\n");
1167 msi_schedule_action( package
, SCRIPT_INSTALL
, deferred
);
1170 msi_free( actiondata
);
1171 msi_free( usersid
);
1172 msi_free( prodcode
);
1173 msi_free( deferred
);
1174 return ERROR_SUCCESS
;
1177 UINT
ACTION_CustomAction( MSIPACKAGE
*package
, LPCWSTR action
, UINT script
)
1179 static const WCHAR query
[] = {
1180 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1181 '`','C','u','s','t','o','m','A','c','t','i','o','n','`',' ','W','H','E','R','E',' ',
1182 '`','A','c','t','i' ,'o','n','`',' ','=',' ','\'','%','s','\'',0};
1183 UINT rc
= ERROR_SUCCESS
;
1186 const WCHAR
*source
, *target
, *ptr
, *deferred_data
= NULL
;
1187 WCHAR
*deformated
= NULL
;
1190 /* deferred action: [properties]Action */
1191 if ((ptr
= strrchrW(action
, ']')))
1193 deferred_data
= action
;
1197 row
= MSI_QueryGetRecord( package
->db
, query
, action
);
1199 return ERROR_FUNCTION_NOT_CALLED
;
1201 type
= MSI_RecordGetInteger(row
,2);
1202 source
= MSI_RecordGetString(row
,3);
1203 target
= MSI_RecordGetString(row
,4);
1205 TRACE("Handling custom action %s (%x %s %s)\n",debugstr_w(action
),type
,
1206 debugstr_w(source
), debugstr_w(target
));
1208 /* handle some of the deferred actions */
1209 if (type
& msidbCustomActionTypeTSAware
)
1210 FIXME("msidbCustomActionTypeTSAware not handled\n");
1212 if (type
& msidbCustomActionTypeInScript
)
1214 if (type
& msidbCustomActionTypeNoImpersonate
)
1215 WARN("msidbCustomActionTypeNoImpersonate not handled\n");
1217 if (!action_type_matches_script( type
, script
))
1219 rc
= defer_custom_action( package
, action
, type
);
1224 LPWSTR actiondata
= msi_dup_property( package
->db
, action
);
1226 if (type
& msidbCustomActionTypeInScript
)
1227 package
->scheduled_action_running
= TRUE
;
1229 if (type
& msidbCustomActionTypeCommit
)
1230 package
->commit_action_running
= TRUE
;
1232 if (type
& msidbCustomActionTypeRollback
)
1233 package
->rollback_action_running
= TRUE
;
1236 set_deferred_action_props(package
, deferred_data
);
1237 else if (actiondata
)
1238 msi_set_property( package
->db
, szCustomActionData
, actiondata
, -1 );
1240 msi_set_property( package
->db
, szCustomActionData
, szEmpty
, -1 );
1242 msi_free(actiondata
);
1245 else if (!check_execution_scheduling_options(package
,action
,type
))
1251 switch (type
& CUSTOM_ACTION_TYPE_MASK
)
1253 case 1: /* DLL file stored in a Binary table stream */
1254 rc
= HANDLE_CustomType1(package
,source
,target
,type
,action
);
1256 case 2: /* EXE file stored in a Binary table stream */
1257 rc
= HANDLE_CustomType2(package
,source
,target
,type
,action
);
1259 case 18: /*EXE file installed with package */
1260 rc
= HANDLE_CustomType18(package
,source
,target
,type
,action
);
1262 case 19: /* Error that halts install */
1263 rc
= HANDLE_CustomType19(package
,source
,target
,type
,action
);
1266 rc
= HANDLE_CustomType17(package
,source
,target
,type
,action
);
1268 case 23: /* installs another package in the source tree */
1269 deformat_string(package
,target
,&deformated
);
1270 rc
= HANDLE_CustomType23(package
,source
,deformated
,type
,action
);
1271 msi_free(deformated
);
1273 case 50: /*EXE file specified by a property value */
1274 rc
= HANDLE_CustomType50(package
,source
,target
,type
,action
);
1276 case 34: /*EXE to be run in specified directory */
1277 rc
= HANDLE_CustomType34(package
,source
,target
,type
,action
);
1279 case 35: /* Directory set with formatted text. */
1280 deformat_string(package
,target
,&deformated
);
1281 MSI_SetTargetPathW(package
, source
, deformated
);
1282 msi_free(deformated
);
1284 case 51: /* Property set with formatted text. */
1288 len
= deformat_string( package
, target
, &deformated
);
1289 rc
= msi_set_property( package
->db
, source
, deformated
, len
);
1290 if (rc
== ERROR_SUCCESS
&& !strcmpW( source
, szSourceDir
))
1291 msi_reset_folders( package
, TRUE
);
1292 msi_free(deformated
);
1294 case 37: /* JScript/VBScript text stored in target column. */
1296 rc
= HANDLE_CustomType37_38(package
,source
,target
,type
,action
);
1299 case 6: /* JScript/VBScript file stored in a Binary table stream. */
1300 rc
= HANDLE_CustomType5_6(package
,source
,target
,type
,action
);
1302 case 21: /* JScript/VBScript file installed with the product. */
1304 rc
= HANDLE_CustomType21_22(package
,source
,target
,type
,action
);
1306 case 53: /* JScript/VBScript text specified by a property value. */
1308 rc
= HANDLE_CustomType53_54(package
,source
,target
,type
,action
);
1311 FIXME("unhandled action type %u (%s %s)\n", type
& CUSTOM_ACTION_TYPE_MASK
,
1312 debugstr_w(source
), debugstr_w(target
));
1316 package
->scheduled_action_running
= FALSE
;
1317 package
->commit_action_running
= FALSE
;
1318 package
->rollback_action_running
= FALSE
;
1319 msiobj_release(&row
->hdr
);
1323 void ACTION_FinishCustomActions(const MSIPACKAGE
* package
)
1326 HANDLE
*wait_handles
;
1327 unsigned int handle_count
, i
;
1328 msi_custom_action_info
*info
, *cursor
;
1330 while ((item
= list_head( &package
->RunningActions
)))
1332 MSIRUNNINGACTION
*action
= LIST_ENTRY( item
, MSIRUNNINGACTION
, entry
);
1334 list_remove( &action
->entry
);
1336 TRACE("waiting for %s\n", debugstr_w( action
->name
) );
1337 msi_dialog_check_messages( action
->handle
);
1339 CloseHandle( action
->handle
);
1340 msi_free( action
->name
);
1344 EnterCriticalSection( &msi_custom_action_cs
);
1346 handle_count
= list_count( &msi_pending_custom_actions
);
1347 wait_handles
= msi_alloc( handle_count
* sizeof(HANDLE
) );
1350 LIST_FOR_EACH_ENTRY_SAFE( info
, cursor
, &msi_pending_custom_actions
, msi_custom_action_info
, entry
)
1352 if (info
->package
== package
)
1354 if (DuplicateHandle(GetCurrentProcess(), info
->handle
, GetCurrentProcess(), &wait_handles
[handle_count
], SYNCHRONIZE
, FALSE
, 0))
1359 LeaveCriticalSection( &msi_custom_action_cs
);
1361 for (i
= 0; i
< handle_count
; i
++)
1363 msi_dialog_check_messages( wait_handles
[i
] );
1364 CloseHandle( wait_handles
[i
] );
1366 msi_free( wait_handles
);
1368 EnterCriticalSection( &msi_custom_action_cs
);
1369 LIST_FOR_EACH_ENTRY_SAFE( info
, cursor
, &msi_pending_custom_actions
, msi_custom_action_info
, entry
)
1371 if (info
->package
== package
) release_custom_action_data( info
);
1373 LeaveCriticalSection( &msi_custom_action_cs
);
1376 typedef struct _msi_custom_remote_impl
{
1377 IWineMsiRemoteCustomAction IWineMsiRemoteCustomAction_iface
;
1379 } msi_custom_remote_impl
;
1381 static inline msi_custom_remote_impl
*impl_from_IWineMsiRemoteCustomAction( IWineMsiRemoteCustomAction
*iface
)
1383 return CONTAINING_RECORD(iface
, msi_custom_remote_impl
, IWineMsiRemoteCustomAction_iface
);
1386 static HRESULT WINAPI
mcr_QueryInterface( IWineMsiRemoteCustomAction
*iface
,
1387 REFIID riid
,LPVOID
*ppobj
)
1389 if( IsEqualCLSID( riid
, &IID_IUnknown
) ||
1390 IsEqualCLSID( riid
, &IID_IWineMsiRemoteCustomAction
) )
1392 IWineMsiRemoteCustomAction_AddRef( iface
);
1397 return E_NOINTERFACE
;
1400 static ULONG WINAPI
mcr_AddRef( IWineMsiRemoteCustomAction
*iface
)
1402 msi_custom_remote_impl
* This
= impl_from_IWineMsiRemoteCustomAction( iface
);
1404 return InterlockedIncrement( &This
->refs
);
1407 static ULONG WINAPI
mcr_Release( IWineMsiRemoteCustomAction
*iface
)
1409 msi_custom_remote_impl
* This
= impl_from_IWineMsiRemoteCustomAction( iface
);
1412 r
= InterlockedDecrement( &This
->refs
);
1418 static HRESULT WINAPI
mcr_GetActionInfo( IWineMsiRemoteCustomAction
*iface
, LPCGUID custom_action_guid
,
1419 INT
*type
, MSIHANDLE
*handle
, BSTR
*dll
, BSTR
*func
, IWineMsiRemotePackage
**remote_package
)
1421 msi_custom_action_info
*info
;
1423 info
= find_action_by_guid( custom_action_guid
);
1428 *handle
= alloc_msihandle( &info
->package
->hdr
);
1429 *dll
= SysAllocString( info
->source
);
1430 *func
= SysAllocString( info
->target
);
1432 release_custom_action_data( info
);
1433 return create_msi_remote_package( NULL
, (LPVOID
*)remote_package
);
1436 static const IWineMsiRemoteCustomActionVtbl msi_custom_remote_vtbl
=
1444 HRESULT
create_msi_custom_remote( IUnknown
*pOuter
, LPVOID
*ppObj
)
1446 msi_custom_remote_impl
* This
;
1448 This
= msi_alloc( sizeof *This
);
1450 return E_OUTOFMEMORY
;
1452 This
->IWineMsiRemoteCustomAction_iface
.lpVtbl
= &msi_custom_remote_vtbl
;
1455 *ppObj
= &This
->IWineMsiRemoteCustomAction_iface
;