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 #define COM_NO_WINDOWS_H
25 //#include "wine/port.h"
32 //#include "winerror.h"
33 //#include "msidefs.h"
39 #include <msiserver.h>
40 #include <wine/debug.h>
41 #include <wine/unicode.h>
42 #include <wine/exception.h>
44 WINE_DEFAULT_DEBUG_CHANNEL(msi
);
46 #define CUSTOM_ACTION_TYPE_MASK 0x3F
48 typedef struct tagMSIRUNNINGACTION
56 typedef UINT (WINAPI
*MsiCustomActionEntryPoint
)( MSIHANDLE
);
58 static CRITICAL_SECTION msi_custom_action_cs
;
59 static CRITICAL_SECTION_DEBUG msi_custom_action_cs_debug
=
61 0, 0, &msi_custom_action_cs
,
62 { &msi_custom_action_cs_debug
.ProcessLocksList
,
63 &msi_custom_action_cs_debug
.ProcessLocksList
},
64 0, 0, { (DWORD_PTR
)(__FILE__
": msi_custom_action_cs") }
66 static CRITICAL_SECTION msi_custom_action_cs
= { &msi_custom_action_cs_debug
, -1, 0, 0, 0, 0 };
68 static struct list msi_pending_custom_actions
= LIST_INIT( msi_pending_custom_actions
);
70 UINT
msi_schedule_action( MSIPACKAGE
*package
, UINT script
, const WCHAR
*action
)
73 WCHAR
**newbuf
= NULL
;
75 if (script
>= SCRIPT_MAX
)
77 FIXME("Unknown script requested %u\n", script
);
78 return ERROR_FUNCTION_FAILED
;
80 TRACE("Scheduling action %s in script %u\n", debugstr_w(action
), script
);
82 count
= package
->script
->ActionCount
[script
];
83 package
->script
->ActionCount
[script
]++;
84 if (count
!= 0) newbuf
= msi_realloc( package
->script
->Actions
[script
],
85 package
->script
->ActionCount
[script
] * sizeof(WCHAR
*) );
86 else newbuf
= msi_alloc( sizeof(WCHAR
*) );
88 newbuf
[count
] = strdupW( action
);
89 package
->script
->Actions
[script
] = newbuf
;
93 UINT
msi_register_unique_action( MSIPACKAGE
*package
, const WCHAR
*action
)
96 WCHAR
**newbuf
= NULL
;
98 if (!package
->script
) return FALSE
;
100 TRACE("Registering %s as unique action\n", debugstr_w(action
));
102 count
= package
->script
->UniqueActionsCount
;
103 package
->script
->UniqueActionsCount
++;
104 if (count
!= 0) newbuf
= msi_realloc( package
->script
->UniqueActions
,
105 package
->script
->UniqueActionsCount
* sizeof(WCHAR
*) );
106 else newbuf
= msi_alloc( sizeof(WCHAR
*) );
108 newbuf
[count
] = strdupW( action
);
109 package
->script
->UniqueActions
= newbuf
;
110 return ERROR_SUCCESS
;
113 BOOL
msi_action_is_unique( const MSIPACKAGE
*package
, const WCHAR
*action
)
117 if (!package
->script
) return FALSE
;
119 for (i
= 0; i
< package
->script
->UniqueActionsCount
; i
++)
121 if (!strcmpW( package
->script
->UniqueActions
[i
], action
)) return TRUE
;
126 static BOOL
check_execution_scheduling_options(MSIPACKAGE
*package
, LPCWSTR action
, UINT options
)
128 if (!package
->script
)
131 if ((options
& msidbCustomActionTypeClientRepeat
) ==
132 msidbCustomActionTypeClientRepeat
)
134 if (!(package
->script
->InWhatSequence
& SEQUENCE_UI
&&
135 package
->script
->InWhatSequence
& SEQUENCE_EXEC
))
137 TRACE("Skipping action due to dbCustomActionTypeClientRepeat option.\n");
141 else if (options
& msidbCustomActionTypeFirstSequence
)
143 if (package
->script
->InWhatSequence
& SEQUENCE_UI
&&
144 package
->script
->InWhatSequence
& SEQUENCE_EXEC
)
146 TRACE("Skipping action due to msidbCustomActionTypeFirstSequence option.\n");
150 else if (options
& msidbCustomActionTypeOncePerProcess
)
152 if (msi_action_is_unique(package
, action
))
154 TRACE("Skipping action due to msidbCustomActionTypeOncePerProcess option.\n");
158 msi_register_unique_action(package
, action
);
164 /* stores the following properties before the action:
166 * [CustomActionData<=>UserSID<=>ProductCode]Action
168 static LPWSTR
msi_get_deferred_action(LPCWSTR action
, LPCWSTR actiondata
,
169 LPCWSTR usersid
, LPCWSTR prodcode
)
174 static const WCHAR format
[] = {
175 '[','%','s','<','=','>','%','s','<','=','>','%','s',']','%','s',0
179 return strdupW(action
);
181 len
= lstrlenW(action
) + lstrlenW(actiondata
) +
182 lstrlenW(usersid
) + lstrlenW(prodcode
) +
183 lstrlenW(format
) - 7;
184 deferred
= msi_alloc(len
* sizeof(WCHAR
));
186 sprintfW(deferred
, format
, actiondata
, usersid
, prodcode
, action
);
190 static void set_deferred_action_props( MSIPACKAGE
*package
, const WCHAR
*deferred_data
)
192 static const WCHAR sep
[] = {'<','=','>',0};
193 const WCHAR
*end
, *beg
= deferred_data
+ 1;
195 end
= strstrW(beg
, sep
);
196 msi_set_property( package
->db
, szCustomActionData
, beg
, end
- beg
);
199 end
= strstrW(beg
, sep
);
200 msi_set_property( package
->db
, szUserSID
, beg
, end
- beg
);
203 end
= strchrW(beg
, ']');
204 msi_set_property( package
->db
, szProductCode
, beg
, end
- beg
);
207 static MSIBINARY
*create_temp_binary( MSIPACKAGE
*package
, LPCWSTR source
, BOOL dll
)
209 static const WCHAR query
[] = {
210 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
211 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',' ',
212 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
217 WCHAR fmt
[MAX_PATH
], tmpfile
[MAX_PATH
];
218 DWORD sz
= MAX_PATH
, write
;
221 if (msi_get_property(package
->db
, szTempFolder
, fmt
, &sz
) != ERROR_SUCCESS
)
222 GetTempPathW(MAX_PATH
, fmt
);
224 if (!GetTempFileNameW( fmt
, szMsi
, 0, tmpfile
))
226 TRACE("unable to create temp file %s (%u)\n", debugstr_w(tmpfile
), GetLastError());
230 row
= MSI_QueryGetRecord(package
->db
, query
, source
);
234 if (!(binary
= msi_alloc_zero( sizeof(MSIBINARY
) )))
236 msiobj_release( &row
->hdr
);
239 file
= CreateFileW( tmpfile
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
240 if (file
== INVALID_HANDLE_VALUE
)
242 msiobj_release( &row
->hdr
);
249 r
= MSI_RecordReadStream( row
, 2, buffer
, &sz
);
250 if (r
!= ERROR_SUCCESS
)
252 ERR("Failed to get stream\n");
255 WriteFile( file
, buffer
, sz
, &write
, NULL
);
256 } while (sz
== sizeof buffer
);
259 msiobj_release( &row
->hdr
);
260 if (r
!= ERROR_SUCCESS
)
262 DeleteFileW( tmpfile
);
267 /* keep a reference to prevent the dll from being unloaded */
268 if (dll
&& !(binary
->module
= LoadLibraryW( tmpfile
)))
270 WARN( "failed to load dll %s (%u)\n", debugstr_w( tmpfile
), GetLastError() );
272 binary
->source
= strdupW( source
);
273 binary
->tmpfile
= strdupW( tmpfile
);
274 list_add_tail( &package
->binaries
, &binary
->entry
);
278 static MSIBINARY
*get_temp_binary( MSIPACKAGE
*package
, LPCWSTR source
, BOOL dll
)
282 LIST_FOR_EACH_ENTRY( binary
, &package
->binaries
, MSIBINARY
, entry
)
284 if (!strcmpW( binary
->source
, source
))
288 return create_temp_binary( package
, source
, dll
);
291 static void file_running_action(MSIPACKAGE
* package
, HANDLE Handle
,
292 BOOL process
, LPCWSTR name
)
294 MSIRUNNINGACTION
*action
;
296 action
= msi_alloc( sizeof(MSIRUNNINGACTION
) );
298 action
->handle
= Handle
;
299 action
->process
= process
;
300 action
->name
= strdupW(name
);
302 list_add_tail( &package
->RunningActions
, &action
->entry
);
305 static UINT
custom_get_process_return( HANDLE process
)
309 GetExitCodeProcess( process
, &rc
);
310 TRACE("exit code is %u\n", rc
);
312 return ERROR_FUNCTION_FAILED
;
313 return ERROR_SUCCESS
;
316 static UINT
custom_get_thread_return( MSIPACKAGE
*package
, HANDLE thread
)
320 GetExitCodeThread( thread
, &rc
);
324 case ERROR_FUNCTION_NOT_CALLED
:
326 case ERROR_INSTALL_USEREXIT
:
327 case ERROR_INSTALL_FAILURE
:
329 case ERROR_NO_MORE_ITEMS
:
330 return ERROR_SUCCESS
;
331 case ERROR_INSTALL_SUSPEND
:
332 ACTION_ForceReboot( package
);
333 return ERROR_SUCCESS
;
335 ERR("Invalid Return Code %d\n",rc
);
336 return ERROR_INSTALL_FAILURE
;
340 static UINT
wait_process_handle(MSIPACKAGE
* package
, UINT type
,
341 HANDLE ProcessHandle
, LPCWSTR name
)
343 UINT rc
= ERROR_SUCCESS
;
345 if (!(type
& msidbCustomActionTypeAsync
))
347 TRACE("waiting for %s\n", debugstr_w(name
));
349 msi_dialog_check_messages(ProcessHandle
);
351 if (!(type
& msidbCustomActionTypeContinue
))
352 rc
= custom_get_process_return(ProcessHandle
);
354 CloseHandle(ProcessHandle
);
358 TRACE("%s running in background\n", debugstr_w(name
));
360 if (!(type
& msidbCustomActionTypeContinue
))
361 file_running_action(package
, ProcessHandle
, TRUE
, name
);
363 CloseHandle(ProcessHandle
);
369 typedef struct _msi_custom_action_info
{
379 } msi_custom_action_info
;
381 static void release_custom_action_data( msi_custom_action_info
*info
)
383 EnterCriticalSection( &msi_custom_action_cs
);
387 list_remove( &info
->entry
);
389 CloseHandle( info
->handle
);
390 msi_free( info
->action
);
391 msi_free( info
->source
);
392 msi_free( info
->target
);
393 msiobj_release( &info
->package
->hdr
);
397 LeaveCriticalSection( &msi_custom_action_cs
);
400 /* must be called inside msi_custom_action_cs if info is in the pending custom actions list */
401 static void addref_custom_action_data( msi_custom_action_info
*info
)
406 static UINT
wait_thread_handle( msi_custom_action_info
*info
)
408 UINT rc
= ERROR_SUCCESS
;
410 if (!(info
->type
& msidbCustomActionTypeAsync
))
412 TRACE("waiting for %s\n", debugstr_w( info
->action
));
414 msi_dialog_check_messages( info
->handle
);
416 if (!(info
->type
& msidbCustomActionTypeContinue
))
417 rc
= custom_get_thread_return( info
->package
, info
->handle
);
419 release_custom_action_data( info
);
423 TRACE("%s running in background\n", debugstr_w( info
->action
));
429 static msi_custom_action_info
*find_action_by_guid( const GUID
*guid
)
431 msi_custom_action_info
*info
;
434 EnterCriticalSection( &msi_custom_action_cs
);
436 LIST_FOR_EACH_ENTRY( info
, &msi_pending_custom_actions
, msi_custom_action_info
, entry
)
438 if (IsEqualGUID( &info
->guid
, guid
))
440 addref_custom_action_data( info
);
446 LeaveCriticalSection( &msi_custom_action_cs
);
454 static void handle_msi_break( LPCWSTR target
)
459 static const WCHAR MsiBreak
[] = { 'M','s','i','B','r','e','a','k',0 };
460 static const WCHAR WindowsInstaller
[] = {
461 'W','i','n','d','o','w','s',' ','I','n','s','t','a','l','l','e','r',0
464 static const WCHAR format
[] = {
465 'T','o',' ','d','e','b','u','g',' ','y','o','u','r',' ',
466 'c','u','s','t','o','m',' ','a','c','t','i','o','n',',',' ',
467 'a','t','t','a','c','h',' ','y','o','u','r',' ','d','e','b','u','g','g','e','r',' ',
468 't','o',' ','p','r','o','c','e','s','s',' ','%','i',' ','(','0','x','%','X',')',' ',
469 'a','n','d',' ','p','r','e','s','s',' ','O','K',0
472 if( !GetEnvironmentVariableW( MsiBreak
, val
, MAX_PATH
))
475 if( strcmpiW( val
, target
))
478 msg
= msi_alloc( (lstrlenW(format
) + 10) * sizeof(WCHAR
) );
482 wsprintfW( msg
, format
, GetCurrentProcessId(), GetCurrentProcessId());
483 MessageBoxW( NULL
, msg
, WindowsInstaller
, MB_OK
);
488 static UINT
get_action_info( const GUID
*guid
, INT
*type
, MSIHANDLE
*handle
,
489 BSTR
*dll
, BSTR
*funcname
,
490 IWineMsiRemotePackage
**package
)
492 IClassFactory
*cf
= NULL
;
493 IWineMsiRemoteCustomAction
*rca
= NULL
;
496 r
= DllGetClassObject( &CLSID_WineMsiRemoteCustomAction
,
497 &IID_IClassFactory
, (LPVOID
*)&cf
);
500 ERR("failed to get IClassFactory interface\n");
501 return ERROR_FUNCTION_FAILED
;
504 r
= IClassFactory_CreateInstance( cf
, NULL
, &IID_IWineMsiRemoteCustomAction
, (LPVOID
*)&rca
);
507 ERR("failed to get IWineMsiRemoteCustomAction interface\n");
508 return ERROR_FUNCTION_FAILED
;
511 r
= IWineMsiRemoteCustomAction_GetActionInfo( rca
, guid
, type
, handle
, dll
, funcname
, package
);
512 IWineMsiRemoteCustomAction_Release( rca
);
515 ERR("GetActionInfo failed\n");
516 return ERROR_FUNCTION_FAILED
;
519 return ERROR_SUCCESS
;
523 extern UINT
CUSTOMPROC_wrapper( MsiCustomActionEntryPoint proc
, MSIHANDLE handle
);
524 __ASM_GLOBAL_FUNC( CUSTOMPROC_wrapper
,
526 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
527 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
529 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
531 "movl 8(%ebp),%eax\n\t"
534 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
535 __ASM_CFI(".cfi_same_value %ebp\n\t")
538 static inline UINT
CUSTOMPROC_wrapper( MsiCustomActionEntryPoint proc
, MSIHANDLE handle
)
544 static DWORD
ACTION_CallDllFunction( const GUID
*guid
)
546 MsiCustomActionEntryPoint fn
;
547 MSIHANDLE hPackage
, handle
;
550 UINT r
= ERROR_FUNCTION_FAILED
;
551 BSTR dll
= NULL
, function
= NULL
;
553 IWineMsiRemotePackage
*remote_package
= NULL
;
555 TRACE("%s\n", debugstr_guid( guid
));
557 r
= get_action_info( guid
, &type
, &handle
, &dll
, &function
, &remote_package
);
558 if (r
!= ERROR_SUCCESS
)
561 hModule
= LoadLibraryW( dll
);
564 WARN( "failed to load dll %s (%u)\n", debugstr_w( dll
), GetLastError() );
565 return ERROR_SUCCESS
;
568 proc
= strdupWtoA( function
);
569 fn
= (MsiCustomActionEntryPoint
) GetProcAddress( hModule
, proc
);
573 hPackage
= alloc_msi_remote_handle( (IUnknown
*)remote_package
);
576 IWineMsiRemotePackage_SetMsiHandle( remote_package
, handle
);
577 TRACE("calling %s\n", debugstr_w( function
) );
578 handle_msi_break( function
);
582 r
= CUSTOMPROC_wrapper( fn
, hPackage
);
586 ERR("Custom action (%s:%s) caused a page fault: %08x\n",
587 debugstr_w(dll
), debugstr_w(function
), GetExceptionCode());
592 MsiCloseHandle( hPackage
);
595 ERR("failed to create handle for %p\n", remote_package
);
598 ERR("GetProcAddress(%s) failed\n", debugstr_w( function
) );
600 FreeLibrary(hModule
);
602 IWineMsiRemotePackage_Release( remote_package
);
603 SysFreeString( dll
);
604 SysFreeString( function
);
605 MsiCloseHandle( handle
);
610 static DWORD WINAPI
DllThread( LPVOID arg
)
615 TRACE("custom action (%x) started\n", GetCurrentThreadId() );
617 rc
= ACTION_CallDllFunction( guid
);
619 TRACE("custom action (%x) returned %i\n", GetCurrentThreadId(), rc
);
621 MsiCloseAllHandles();
625 static msi_custom_action_info
*do_msidbCustomActionTypeDll(
626 MSIPACKAGE
*package
, INT type
, LPCWSTR source
, LPCWSTR target
, LPCWSTR action
)
628 msi_custom_action_info
*info
;
630 info
= msi_alloc( sizeof *info
);
634 msiobj_addref( &package
->hdr
);
635 info
->refs
= 2; /* 1 for our caller and 1 for thread we created */
636 info
->package
= package
;
638 info
->target
= strdupW( target
);
639 info
->source
= strdupW( source
);
640 info
->action
= strdupW( action
);
641 CoCreateGuid( &info
->guid
);
643 EnterCriticalSection( &msi_custom_action_cs
);
644 list_add_tail( &msi_pending_custom_actions
, &info
->entry
);
645 LeaveCriticalSection( &msi_custom_action_cs
);
647 info
->handle
= CreateThread( NULL
, 0, DllThread
, &info
->guid
, 0, NULL
);
650 /* release both references */
651 release_custom_action_data( info
);
652 release_custom_action_data( info
);
659 static UINT
HANDLE_CustomType1( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
660 INT type
, const WCHAR
*action
)
662 msi_custom_action_info
*info
;
665 if (!(binary
= get_temp_binary( package
, source
, TRUE
)))
666 return ERROR_FUNCTION_FAILED
;
668 TRACE("Calling function %s from %s\n", debugstr_w(target
), debugstr_w(binary
->tmpfile
));
670 info
= do_msidbCustomActionTypeDll( package
, type
, binary
->tmpfile
, target
, action
);
671 return wait_thread_handle( info
);
674 static HANDLE
execute_command( const WCHAR
*app
, WCHAR
*arg
, const WCHAR
*dir
)
676 static const WCHAR dotexeW
[] = {'.','e','x','e',0};
678 PROCESS_INFORMATION info
;
679 WCHAR
*exe
= NULL
, *cmd
= NULL
, *p
;
687 if (!(exe
= msi_alloc( MAX_PATH
* sizeof(WCHAR
) ))) return INVALID_HANDLE_VALUE
;
688 len_exe
= SearchPathW( NULL
, app
, dotexeW
, MAX_PATH
, exe
, NULL
);
689 if (len_exe
>= MAX_PATH
)
692 if (!(exe
= msi_alloc( len_exe
* sizeof(WCHAR
) ))) return INVALID_HANDLE_VALUE
;
693 len_exe
= SearchPathW( NULL
, app
, dotexeW
, len_exe
, exe
, NULL
);
697 WARN("can't find executable %u\n", GetLastError());
699 return INVALID_HANDLE_VALUE
;
702 if (arg
) len_arg
= strlenW( arg
);
703 if (!(cmd
= msi_alloc( (len_exe
+ len_arg
+ 4) * sizeof(WCHAR
) )))
706 return INVALID_HANDLE_VALUE
;
709 if (strchrW( exe
, ' ' ))
712 memcpy( p
, exe
, len_exe
* sizeof(WCHAR
) );
725 memcpy( p
, arg
, len_arg
* sizeof(WCHAR
) );
729 memset( &si
, 0, sizeof(STARTUPINFOW
) );
730 ret
= CreateProcessW( exe
, exe
? cmd
: arg
, NULL
, NULL
, FALSE
, 0, NULL
, dir
, &si
, &info
);
735 WARN("unable to execute command %u\n", GetLastError());
736 return INVALID_HANDLE_VALUE
;
738 CloseHandle( info
.hThread
);
739 return info
.hProcess
;
742 static UINT
HANDLE_CustomType2( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
743 INT type
, const WCHAR
*action
)
749 if (!(binary
= get_temp_binary( package
, source
, FALSE
))) return ERROR_FUNCTION_FAILED
;
751 deformat_string( package
, target
, &arg
);
752 TRACE("exe %s arg %s\n", debugstr_w(binary
->tmpfile
), debugstr_w(arg
));
754 handle
= execute_command( binary
->tmpfile
, arg
, szCRoot
);
756 if (handle
== INVALID_HANDLE_VALUE
) return ERROR_SUCCESS
;
757 return wait_process_handle( package
, type
, handle
, action
);
760 static UINT
HANDLE_CustomType17( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
761 INT type
, const WCHAR
*action
)
763 msi_custom_action_info
*info
;
766 TRACE("%s %s\n", debugstr_w(source
), debugstr_w(target
));
768 file
= msi_get_loaded_file( package
, source
);
771 ERR("invalid file key %s\n", debugstr_w( source
));
772 return ERROR_FUNCTION_FAILED
;
775 info
= do_msidbCustomActionTypeDll( package
, type
, file
->TargetPath
, target
, action
);
776 return wait_thread_handle( info
);
779 static UINT
HANDLE_CustomType18( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
780 INT type
, const WCHAR
*action
)
786 if (!(file
= msi_get_loaded_file( package
, source
))) return ERROR_FUNCTION_FAILED
;
788 deformat_string( package
, target
, &arg
);
789 TRACE("exe %s arg %s\n", debugstr_w(file
->TargetPath
), debugstr_w(arg
));
791 handle
= execute_command( file
->TargetPath
, arg
, szCRoot
);
793 if (handle
== INVALID_HANDLE_VALUE
) return ERROR_SUCCESS
;
794 return wait_process_handle( package
, type
, handle
, action
);
797 static UINT
HANDLE_CustomType19( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
798 INT type
, const WCHAR
*action
)
800 static const WCHAR query
[] = {
801 'S','E','L','E','C','T',' ','`','M','e','s','s','a','g','e','`',' ',
802 'F','R','O','M',' ','`','E','r','r','o','r','`',' ',
803 'W','H','E','R','E',' ','`','E','r','r','o','r','`',' ','=',' ',
807 LPWSTR deformated
= NULL
;
809 deformat_string( package
, target
, &deformated
);
811 /* first try treat the error as a number */
812 row
= MSI_QueryGetRecord( package
->db
, query
, deformated
);
815 LPCWSTR error
= MSI_RecordGetString( row
, 1 );
816 if ((package
->ui_level
& INSTALLUILEVEL_MASK
) != INSTALLUILEVEL_NONE
)
817 MessageBoxW( NULL
, error
, NULL
, MB_OK
);
818 msiobj_release( &row
->hdr
);
820 else if ((package
->ui_level
& INSTALLUILEVEL_MASK
) != INSTALLUILEVEL_NONE
)
821 MessageBoxW( NULL
, deformated
, NULL
, MB_OK
);
823 msi_free( deformated
);
825 return ERROR_INSTALL_FAILURE
;
828 static UINT
HANDLE_CustomType23( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
829 INT type
, const WCHAR
*action
)
831 static const WCHAR msiexecW
[] = {'m','s','i','e','x','e','c',0};
832 static const WCHAR paramsW
[] = {'/','q','b',' ','/','i',' '};
833 WCHAR
*dir
, *arg
, *p
;
834 UINT len_src
, len_dir
, len_tgt
, len
= sizeof(paramsW
)/sizeof(paramsW
[0]);
837 if (!(dir
= msi_dup_property( package
->db
, szOriginalDatabase
))) return ERROR_OUTOFMEMORY
;
838 if (!(p
= strrchrW( dir
, '\\' )) && !(p
= strrchrW( dir
, '/' )))
841 return ERROR_FUNCTION_FAILED
;
845 len_src
= strlenW( source
);
846 len_tgt
= strlenW( target
);
847 if (!(arg
= msi_alloc( (len
+ len_dir
+ len_src
+ len_tgt
+ 5) * sizeof(WCHAR
) )))
850 return ERROR_OUTOFMEMORY
;
852 memcpy( arg
, paramsW
, sizeof(paramsW
) );
854 memcpy( arg
+ len
, dir
, len_dir
* sizeof(WCHAR
) );
857 memcpy( arg
+ len
, source
, len_src
* sizeof(WCHAR
) );
861 strcpyW( arg
+ len
, target
);
863 TRACE("installing %s concurrently\n", debugstr_w(source
));
865 handle
= execute_command( msiexecW
, arg
, dir
);
868 if (handle
== INVALID_HANDLE_VALUE
) return ERROR_SUCCESS
;
869 return wait_process_handle( package
, type
, handle
, action
);
872 static UINT
HANDLE_CustomType50( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
873 INT type
, const WCHAR
*action
)
878 if (!(exe
= msi_dup_property( package
->db
, source
))) return ERROR_SUCCESS
;
880 deformat_string( package
, target
, &arg
);
881 TRACE("exe %s arg %s\n", debugstr_w(exe
), debugstr_w(arg
));
883 handle
= execute_command( exe
, arg
, szCRoot
);
885 if (handle
== INVALID_HANDLE_VALUE
) return ERROR_SUCCESS
;
886 return wait_process_handle( package
, type
, handle
, action
);
889 static UINT
HANDLE_CustomType34( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
890 INT type
, const WCHAR
*action
)
892 const WCHAR
*workingdir
= NULL
;
898 workingdir
= msi_get_target_folder( package
, source
);
899 if (!workingdir
) return ERROR_FUNCTION_FAILED
;
901 deformat_string( package
, target
, &cmd
);
902 if (!cmd
) return ERROR_FUNCTION_FAILED
;
904 TRACE("cmd %s dir %s\n", debugstr_w(cmd
), debugstr_w(workingdir
));
906 handle
= execute_command( NULL
, cmd
, workingdir
);
908 if (handle
== INVALID_HANDLE_VALUE
) return ERROR_SUCCESS
;
909 return wait_process_handle( package
, type
, handle
, action
);
912 static DWORD
ACTION_CallScript( const GUID
*guid
)
914 msi_custom_action_info
*info
;
916 UINT r
= ERROR_FUNCTION_FAILED
;
918 info
= find_action_by_guid( guid
);
921 ERR("failed to find action %s\n", debugstr_guid( guid
) );
922 return ERROR_FUNCTION_FAILED
;
925 TRACE("function %s, script %s\n", debugstr_w( info
->target
), debugstr_w( info
->source
) );
927 hPackage
= alloc_msihandle( &info
->package
->hdr
);
930 r
= call_script( hPackage
, info
->type
, info
->source
, info
->target
, info
->action
);
931 TRACE("script returned %u\n", r
);
932 MsiCloseHandle( hPackage
);
935 ERR("failed to create handle for %p\n", info
->package
);
937 release_custom_action_data( info
);
941 static DWORD WINAPI
ScriptThread( LPVOID arg
)
946 TRACE("custom action (%x) started\n", GetCurrentThreadId() );
948 rc
= ACTION_CallScript( guid
);
950 TRACE("custom action (%x) returned %i\n", GetCurrentThreadId(), rc
);
952 MsiCloseAllHandles();
956 static msi_custom_action_info
*do_msidbCustomActionTypeScript(
957 MSIPACKAGE
*package
, INT type
, LPCWSTR script
, LPCWSTR function
, LPCWSTR action
)
959 msi_custom_action_info
*info
;
961 info
= msi_alloc( sizeof *info
);
965 msiobj_addref( &package
->hdr
);
966 info
->refs
= 2; /* 1 for our caller and 1 for thread we created */
967 info
->package
= package
;
969 info
->target
= strdupW( function
);
970 info
->source
= strdupW( script
);
971 info
->action
= strdupW( action
);
972 CoCreateGuid( &info
->guid
);
974 EnterCriticalSection( &msi_custom_action_cs
);
975 list_add_tail( &msi_pending_custom_actions
, &info
->entry
);
976 LeaveCriticalSection( &msi_custom_action_cs
);
978 info
->handle
= CreateThread( NULL
, 0, ScriptThread
, &info
->guid
, 0, NULL
);
981 /* release both references */
982 release_custom_action_data( info
);
983 release_custom_action_data( info
);
990 static UINT
HANDLE_CustomType37_38( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
991 INT type
, const WCHAR
*action
)
993 msi_custom_action_info
*info
;
995 TRACE("%s %s\n", debugstr_w(source
), debugstr_w(target
));
997 info
= do_msidbCustomActionTypeScript( package
, type
, target
, NULL
, action
);
998 return wait_thread_handle( info
);
1001 static UINT
HANDLE_CustomType5_6( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
1002 INT type
, const WCHAR
*action
)
1004 static const WCHAR query
[] = {
1005 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1006 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',' ',
1007 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
1009 msi_custom_action_info
*info
;
1010 CHAR
*buffer
= NULL
;
1011 WCHAR
*bufferw
= NULL
;
1015 TRACE("%s %s\n", debugstr_w(source
), debugstr_w(target
));
1017 row
= MSI_QueryGetRecord(package
->db
, query
, source
);
1019 return ERROR_FUNCTION_FAILED
;
1021 r
= MSI_RecordReadStream(row
, 2, NULL
, &sz
);
1022 if (r
!= ERROR_SUCCESS
) return r
;
1024 buffer
= msi_alloc( sz
+ 1 );
1025 if (!buffer
) return ERROR_FUNCTION_FAILED
;
1027 r
= MSI_RecordReadStream(row
, 2, buffer
, &sz
);
1028 if (r
!= ERROR_SUCCESS
)
1032 bufferw
= strdupAtoW(buffer
);
1035 r
= ERROR_FUNCTION_FAILED
;
1039 info
= do_msidbCustomActionTypeScript( package
, type
, bufferw
, target
, action
);
1040 r
= wait_thread_handle( info
);
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( MSIPACKAGE
*package
, 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
, BOOL execute
)
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_CALL_NOT_IMPLEMENTED
;
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 (!execute
|| !action_type_matches_script( package
, 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
;