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"
38 #include "wine/heap.h"
39 #include "wine/debug.h"
40 #include "wine/unicode.h"
41 #include "wine/exception.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(msi
);
45 #define CUSTOM_ACTION_TYPE_MASK 0x3F
47 typedef struct tagMSIRUNNINGACTION
55 typedef UINT (WINAPI
*MsiCustomActionEntryPoint
)( MSIHANDLE
);
57 static CRITICAL_SECTION msi_custom_action_cs
;
58 static CRITICAL_SECTION_DEBUG msi_custom_action_cs_debug
=
60 0, 0, &msi_custom_action_cs
,
61 { &msi_custom_action_cs_debug
.ProcessLocksList
,
62 &msi_custom_action_cs_debug
.ProcessLocksList
},
63 0, 0, { (DWORD_PTR
)(__FILE__
": msi_custom_action_cs") }
65 static CRITICAL_SECTION msi_custom_action_cs
= { &msi_custom_action_cs_debug
, -1, 0, 0, 0, 0 };
67 static struct list msi_pending_custom_actions
= LIST_INIT( msi_pending_custom_actions
);
69 void __RPC_FAR
* __RPC_USER
MIDL_user_allocate(SIZE_T len
)
71 return heap_alloc(len
);
74 void __RPC_USER
MIDL_user_free(void __RPC_FAR
* ptr
)
79 UINT
msi_schedule_action( MSIPACKAGE
*package
, UINT script
, const WCHAR
*action
)
82 WCHAR
**newbuf
= NULL
;
84 if (script
>= SCRIPT_MAX
)
86 FIXME("Unknown script requested %u\n", script
);
87 return ERROR_FUNCTION_FAILED
;
89 TRACE("Scheduling action %s in script %u\n", debugstr_w(action
), script
);
91 count
= package
->script_actions_count
[script
];
92 package
->script_actions_count
[script
]++;
93 if (count
!= 0) newbuf
= msi_realloc( package
->script_actions
[script
],
94 package
->script_actions_count
[script
] * sizeof(WCHAR
*) );
95 else newbuf
= msi_alloc( sizeof(WCHAR
*) );
97 newbuf
[count
] = strdupW( action
);
98 package
->script_actions
[script
] = newbuf
;
102 UINT
msi_register_unique_action( MSIPACKAGE
*package
, const WCHAR
*action
)
105 WCHAR
**newbuf
= NULL
;
107 TRACE("Registering %s as unique action\n", debugstr_w(action
));
109 count
= package
->unique_actions_count
;
110 package
->unique_actions_count
++;
111 if (count
!= 0) newbuf
= msi_realloc( package
->unique_actions
,
112 package
->unique_actions_count
* sizeof(WCHAR
*) );
113 else newbuf
= msi_alloc( sizeof(WCHAR
*) );
115 newbuf
[count
] = strdupW( action
);
116 package
->unique_actions
= newbuf
;
117 return ERROR_SUCCESS
;
120 BOOL
msi_action_is_unique( const MSIPACKAGE
*package
, const WCHAR
*action
)
124 for (i
= 0; i
< package
->unique_actions_count
; i
++)
126 if (!strcmpW( package
->unique_actions
[i
], action
)) return TRUE
;
131 static BOOL
check_execution_scheduling_options(MSIPACKAGE
*package
, LPCWSTR action
, UINT options
)
133 if ((options
& msidbCustomActionTypeClientRepeat
) ==
134 msidbCustomActionTypeClientRepeat
)
136 if (!(package
->InWhatSequence
& SEQUENCE_UI
&&
137 package
->InWhatSequence
& SEQUENCE_EXEC
))
139 TRACE("Skipping action due to dbCustomActionTypeClientRepeat option.\n");
143 else if (options
& msidbCustomActionTypeFirstSequence
)
145 if (package
->InWhatSequence
& SEQUENCE_UI
&&
146 package
->InWhatSequence
& SEQUENCE_EXEC
)
148 TRACE("Skipping action due to msidbCustomActionTypeFirstSequence option.\n");
152 else if (options
& msidbCustomActionTypeOncePerProcess
)
154 if (msi_action_is_unique(package
, action
))
156 TRACE("Skipping action due to msidbCustomActionTypeOncePerProcess option.\n");
160 msi_register_unique_action(package
, action
);
166 /* stores the following properties before the action:
168 * [CustomActionData<=>UserSID<=>ProductCode]Action
170 static LPWSTR
msi_get_deferred_action(LPCWSTR action
, LPCWSTR actiondata
,
171 LPCWSTR usersid
, LPCWSTR prodcode
)
176 static const WCHAR format
[] = {
177 '[','%','s','<','=','>','%','s','<','=','>','%','s',']','%','s',0
181 return strdupW(action
);
183 len
= lstrlenW(action
) + lstrlenW(actiondata
) +
184 lstrlenW(usersid
) + lstrlenW(prodcode
) +
185 lstrlenW(format
) - 7;
186 deferred
= msi_alloc(len
* sizeof(WCHAR
));
188 sprintfW(deferred
, format
, actiondata
, usersid
, prodcode
, action
);
192 static void set_deferred_action_props( MSIPACKAGE
*package
, const WCHAR
*deferred_data
)
194 static const WCHAR sep
[] = {'<','=','>',0};
195 const WCHAR
*end
, *beg
= deferred_data
+ 1;
197 end
= strstrW(beg
, sep
);
198 msi_set_property( package
->db
, szCustomActionData
, beg
, end
- beg
);
201 end
= strstrW(beg
, sep
);
202 msi_set_property( package
->db
, szUserSID
, beg
, end
- beg
);
205 end
= strchrW(beg
, ']');
206 msi_set_property( package
->db
, szProductCode
, beg
, end
- beg
);
209 WCHAR
*msi_create_temp_file( MSIDATABASE
*db
)
216 UINT len
= sizeof(tmp
)/sizeof(tmp
[0]);
218 if (msi_get_property( db
, szTempFolder
, tmp
, &len
) ||
219 GetFileAttributesW( tmp
) != FILE_ATTRIBUTE_DIRECTORY
)
221 GetTempPathW( MAX_PATH
, tmp
);
223 if (!(db
->tempfolder
= strdupW( tmp
))) return NULL
;
226 if ((ret
= msi_alloc( (strlenW( db
->tempfolder
) + 20) * sizeof(WCHAR
) )))
228 if (!GetTempFileNameW( db
->tempfolder
, szMsi
, 0, ret
))
238 static MSIBINARY
*create_temp_binary(MSIPACKAGE
*package
, LPCWSTR source
)
240 static const WCHAR query
[] = {
241 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
242 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',' ',
243 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
245 MSIBINARY
*binary
= NULL
;
252 if (!(tmpfile
= msi_create_temp_file( package
->db
))) return NULL
;
254 if (!(row
= MSI_QueryGetRecord( package
->db
, query
, source
))) goto error
;
255 if (!(binary
= msi_alloc_zero( sizeof(MSIBINARY
) ))) goto error
;
257 file
= CreateFileW( tmpfile
, GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
258 if (file
== INVALID_HANDLE_VALUE
) goto error
;
263 r
= MSI_RecordReadStream( row
, 2, buffer
, &sz
);
264 if (r
!= ERROR_SUCCESS
)
266 ERR("Failed to get stream\n");
269 WriteFile( file
, buffer
, sz
, &write
, NULL
);
270 } while (sz
== sizeof buffer
);
273 if (r
!= ERROR_SUCCESS
) goto error
;
275 binary
->source
= strdupW( source
);
276 binary
->tmpfile
= tmpfile
;
277 list_add_tail( &package
->binaries
, &binary
->entry
);
279 msiobj_release( &row
->hdr
);
283 if (row
) msiobj_release( &row
->hdr
);
284 DeleteFileW( tmpfile
);
290 static MSIBINARY
*get_temp_binary(MSIPACKAGE
*package
, LPCWSTR source
)
294 LIST_FOR_EACH_ENTRY( binary
, &package
->binaries
, MSIBINARY
, entry
)
296 if (!strcmpW( binary
->source
, source
))
300 return create_temp_binary(package
, source
);
303 static void file_running_action(MSIPACKAGE
* package
, HANDLE Handle
,
304 BOOL process
, LPCWSTR name
)
306 MSIRUNNINGACTION
*action
;
308 action
= msi_alloc( sizeof(MSIRUNNINGACTION
) );
310 action
->handle
= Handle
;
311 action
->process
= process
;
312 action
->name
= strdupW(name
);
314 list_add_tail( &package
->RunningActions
, &action
->entry
);
317 static UINT
custom_get_process_return( HANDLE process
)
321 GetExitCodeProcess( process
, &rc
);
322 TRACE("exit code is %u\n", rc
);
324 return ERROR_FUNCTION_FAILED
;
325 return ERROR_SUCCESS
;
328 static UINT
custom_get_thread_return( MSIPACKAGE
*package
, HANDLE thread
)
332 GetExitCodeThread( thread
, &rc
);
336 case ERROR_FUNCTION_NOT_CALLED
:
338 case ERROR_INSTALL_USEREXIT
:
339 case ERROR_INSTALL_FAILURE
:
341 case ERROR_NO_MORE_ITEMS
:
342 return ERROR_SUCCESS
;
343 case ERROR_INSTALL_SUSPEND
:
344 ACTION_ForceReboot( package
);
345 return ERROR_SUCCESS
;
347 ERR("Invalid Return Code %d\n",rc
);
348 return ERROR_INSTALL_FAILURE
;
352 static UINT
wait_process_handle(MSIPACKAGE
* package
, UINT type
,
353 HANDLE ProcessHandle
, LPCWSTR name
)
355 UINT rc
= ERROR_SUCCESS
;
357 if (!(type
& msidbCustomActionTypeAsync
))
359 TRACE("waiting for %s\n", debugstr_w(name
));
361 msi_dialog_check_messages(ProcessHandle
);
363 if (!(type
& msidbCustomActionTypeContinue
))
364 rc
= custom_get_process_return(ProcessHandle
);
366 CloseHandle(ProcessHandle
);
370 TRACE("%s running in background\n", debugstr_w(name
));
372 if (!(type
& msidbCustomActionTypeContinue
))
373 file_running_action(package
, ProcessHandle
, TRUE
, name
);
375 CloseHandle(ProcessHandle
);
381 typedef struct _msi_custom_action_info
{
391 } msi_custom_action_info
;
393 static void release_custom_action_data( msi_custom_action_info
*info
)
395 EnterCriticalSection( &msi_custom_action_cs
);
399 list_remove( &info
->entry
);
401 CloseHandle( info
->handle
);
402 msi_free( info
->action
);
403 msi_free( info
->source
);
404 msi_free( info
->target
);
405 msiobj_release( &info
->package
->hdr
);
409 LeaveCriticalSection( &msi_custom_action_cs
);
412 /* must be called inside msi_custom_action_cs if info is in the pending custom actions list */
413 static void addref_custom_action_data( msi_custom_action_info
*info
)
418 static UINT
wait_thread_handle( msi_custom_action_info
*info
)
420 UINT rc
= ERROR_SUCCESS
;
422 if (!(info
->type
& msidbCustomActionTypeAsync
))
424 TRACE("waiting for %s\n", debugstr_w( info
->action
));
426 msi_dialog_check_messages( info
->handle
);
428 if (!(info
->type
& msidbCustomActionTypeContinue
))
429 rc
= custom_get_thread_return( info
->package
, info
->handle
);
431 release_custom_action_data( info
);
435 TRACE("%s running in background\n", debugstr_w( info
->action
));
441 static msi_custom_action_info
*find_action_by_guid( const GUID
*guid
)
443 msi_custom_action_info
*info
;
446 EnterCriticalSection( &msi_custom_action_cs
);
448 LIST_FOR_EACH_ENTRY( info
, &msi_pending_custom_actions
, msi_custom_action_info
, entry
)
450 if (IsEqualGUID( &info
->guid
, guid
))
452 addref_custom_action_data( info
);
458 LeaveCriticalSection( &msi_custom_action_cs
);
466 static void handle_msi_break(LPCSTR target
)
468 char format
[] = "To debug your custom action, attach your debugger to "
469 "process %i (0x%X) and press OK";
473 if (!GetEnvironmentVariableA("MsiBreak", val
, MAX_PATH
))
476 if (strcasecmp(val
, target
))
479 sprintf(msg
, format
, GetCurrentProcessId(), GetCurrentProcessId());
480 MessageBoxA(NULL
, msg
, "Windows Installer", MB_OK
);
484 static WCHAR ncalrpcW
[] = {'n','c','a','l','r','p','c',0};
485 static WCHAR endpoint_lrpcW
[] = {'m','s','i',0};
487 UINT
__wine_msi_call_dll_function(const GUID
*guid
)
489 MsiCustomActionEntryPoint fn
;
490 MSIHANDLE remote_package
= 0;
491 RPC_WSTR binding_str
;
501 TRACE("%s\n", debugstr_guid( guid
));
503 status
= RpcStringBindingComposeW(NULL
, ncalrpcW
, NULL
, endpoint_lrpcW
, NULL
, &binding_str
);
504 if (status
!= RPC_S_OK
)
506 ERR("RpcStringBindingCompose failed: %#x\n", status
);
509 status
= RpcBindingFromStringBindingW(binding_str
, &rpc_handle
);
510 if (status
!= RPC_S_OK
)
512 ERR("RpcBindingFromStringBinding failed: %#x\n", status
);
515 RpcStringFreeW(&binding_str
);
517 /* We need this to unmarshal streams, and some apps expect it to be present. */
518 CoInitializeEx(NULL
, COINIT_MULTITHREADED
);
520 r
= remote_GetActionInfo(guid
, &type
, &dll
, &proc
, &remote_package
);
521 if (r
!= ERROR_SUCCESS
)
524 hModule
= LoadLibraryW( dll
);
527 ERR( "failed to load dll %s (%u)\n", debugstr_w( dll
), GetLastError() );
528 return ERROR_SUCCESS
;
531 fn
= (MsiCustomActionEntryPoint
) GetProcAddress( hModule
, proc
);
534 hPackage
= alloc_msi_remote_handle( remote_package
);
537 TRACE("calling %s\n", debugstr_a(proc
));
538 handle_msi_break(proc
);
542 thread
= CreateThread(NULL
, 0, (void *)fn
, (void *)(ULONG_PTR
) hPackage
, 0, NULL
);
543 WaitForSingleObject(thread
, INFINITE
);
544 GetExitCodeThread(thread
, &r
);
548 ERR("Custom action (%s:%s) caused a page fault: %08x\n",
549 debugstr_w(dll
), debugstr_a(proc
), GetExceptionCode());
554 MsiCloseHandle( hPackage
);
557 ERR("failed to create handle for %x\n", remote_package
);
560 ERR("GetProcAddress(%s) failed\n", debugstr_a(proc
));
562 FreeLibrary(hModule
);
564 MsiCloseHandle(hPackage
);
566 midl_user_free(proc
);
570 RpcBindingFree(&rpc_handle
);
575 static DWORD WINAPI
DllThread( LPVOID arg
)
577 static const WCHAR msiexecW
[] = {'\\','m','s','i','e','x','e','c','.','e','x','e',0};
578 static const WCHAR argsW
[] = {' ','-','E','m','b','e','d','d','i','n','g',' ',0};
579 msi_custom_action_info
*info
;
580 PROCESS_INFORMATION pi
= {0};
581 STARTUPINFOW si
= {0};
582 WCHAR buffer
[MAX_PATH
], cmdline
[MAX_PATH
+ 60];
591 TRACE("custom action (%x) started\n", GetCurrentThreadId() );
593 CoInitializeEx(NULL
, COINIT_MULTITHREADED
); /* needed to marshal streams */
595 status
= RpcServerUseProtseqEpW(ncalrpcW
, RPC_C_PROTSEQ_MAX_REQS_DEFAULT
, endpoint_lrpcW
, NULL
);
596 if (status
!= RPC_S_OK
)
598 ERR("RpcServerUseProtseqEp failed: %#x\n", status
);
602 status
= RpcServerRegisterIfEx(s_IWineMsiRemote_v0_0_s_ifspec
, NULL
, NULL
,
603 RPC_IF_AUTOLISTEN
, RPC_C_LISTEN_MAX_CALLS_DEFAULT
, NULL
);
604 if (status
!= RPC_S_OK
)
606 ERR("RpcServerRegisterIfEx failed: %#x\n", status
);
610 info
= find_action_by_guid(guid
);
611 ret
= GetBinaryTypeW(info
->source
, &arch
);
612 release_custom_action_data(info
);
614 if (sizeof(void *) == 8 && ret
&& arch
== SCS_32BIT_BINARY
)
615 GetSystemWow64DirectoryW(buffer
, MAX_PATH
- sizeof(msiexecW
)/sizeof(WCHAR
));
617 GetSystemDirectoryW(buffer
, MAX_PATH
- sizeof(msiexecW
)/sizeof(WCHAR
));
618 strcatW(buffer
, msiexecW
);
619 strcpyW(cmdline
, buffer
);
620 strcatW(cmdline
, argsW
);
621 StringFromGUID2(guid
, cmdline
+ strlenW(cmdline
), 39);
623 if (IsWow64Process(GetCurrentProcess(), &wow64
) && wow64
&& arch
== SCS_64BIT_BINARY
)
625 Wow64DisableWow64FsRedirection(&cookie
);
626 CreateProcessW(buffer
, cmdline
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &si
, &pi
);
627 Wow64RevertWow64FsRedirection(cookie
);
630 CreateProcessW(buffer
, cmdline
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &si
, &pi
);
632 WaitForSingleObject(pi
.hProcess
, INFINITE
);
633 GetExitCodeProcess(pi
.hProcess
, &rc
);
634 CloseHandle(pi
.hProcess
);
635 CloseHandle(pi
.hThread
);
637 status
= RpcServerUnregisterIf(s_IWineMsiRemote_v0_0_s_ifspec
, NULL
, FALSE
);
638 if (status
!= RPC_S_OK
)
640 ERR("RpcServerUnregisterIf failed: %#x\n", status
);
646 TRACE("custom action (%x) returned %i\n", GetCurrentThreadId(), rc
);
648 MsiCloseAllHandles();
652 static msi_custom_action_info
*do_msidbCustomActionTypeDll(
653 MSIPACKAGE
*package
, INT type
, LPCWSTR source
, LPCWSTR target
, LPCWSTR action
)
655 msi_custom_action_info
*info
;
657 info
= msi_alloc( sizeof *info
);
661 msiobj_addref( &package
->hdr
);
662 info
->refs
= 2; /* 1 for our caller and 1 for thread we created */
663 info
->package
= package
;
665 info
->target
= strdupW( target
);
666 info
->source
= strdupW( source
);
667 info
->action
= strdupW( action
);
668 CoCreateGuid( &info
->guid
);
670 EnterCriticalSection( &msi_custom_action_cs
);
671 list_add_tail( &msi_pending_custom_actions
, &info
->entry
);
672 LeaveCriticalSection( &msi_custom_action_cs
);
674 info
->handle
= CreateThread( NULL
, 0, DllThread
, &info
->guid
, 0, NULL
);
677 /* release both references */
678 release_custom_action_data( info
);
679 release_custom_action_data( info
);
686 static UINT
HANDLE_CustomType1( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
687 INT type
, const WCHAR
*action
)
689 msi_custom_action_info
*info
;
692 if (!(binary
= get_temp_binary(package
, source
)))
693 return ERROR_FUNCTION_FAILED
;
695 TRACE("Calling function %s from %s\n", debugstr_w(target
), debugstr_w(binary
->tmpfile
));
697 info
= do_msidbCustomActionTypeDll( package
, type
, binary
->tmpfile
, target
, action
);
698 return wait_thread_handle( info
);
701 static HANDLE
execute_command( const WCHAR
*app
, WCHAR
*arg
, const WCHAR
*dir
)
703 static const WCHAR dotexeW
[] = {'.','e','x','e',0};
705 PROCESS_INFORMATION info
;
706 WCHAR
*exe
= NULL
, *cmd
= NULL
, *p
;
714 if (!(exe
= msi_alloc( MAX_PATH
* sizeof(WCHAR
) ))) return INVALID_HANDLE_VALUE
;
715 len_exe
= SearchPathW( NULL
, app
, dotexeW
, MAX_PATH
, exe
, NULL
);
716 if (len_exe
>= MAX_PATH
)
719 if (!(exe
= msi_alloc( len_exe
* sizeof(WCHAR
) ))) return INVALID_HANDLE_VALUE
;
720 len_exe
= SearchPathW( NULL
, app
, dotexeW
, len_exe
, exe
, NULL
);
724 ERR("can't find executable %u\n", GetLastError());
726 return INVALID_HANDLE_VALUE
;
729 if (arg
) len_arg
= strlenW( arg
);
730 if (!(cmd
= msi_alloc( (len_exe
+ len_arg
+ 4) * sizeof(WCHAR
) )))
733 return INVALID_HANDLE_VALUE
;
736 if (strchrW( exe
, ' ' ))
739 memcpy( p
, exe
, len_exe
* sizeof(WCHAR
) );
752 memcpy( p
, arg
, len_arg
* sizeof(WCHAR
) );
756 memset( &si
, 0, sizeof(STARTUPINFOW
) );
757 ret
= CreateProcessW( exe
, exe
? cmd
: arg
, NULL
, NULL
, FALSE
, 0, NULL
, dir
, &si
, &info
);
762 ERR("unable to execute command %u\n", GetLastError());
763 return INVALID_HANDLE_VALUE
;
765 CloseHandle( info
.hThread
);
766 return info
.hProcess
;
769 static UINT
HANDLE_CustomType2( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
770 INT type
, const WCHAR
*action
)
776 if (!(binary
= get_temp_binary(package
, source
)))
777 return ERROR_FUNCTION_FAILED
;
779 deformat_string( package
, target
, &arg
);
780 TRACE("exe %s arg %s\n", debugstr_w(binary
->tmpfile
), debugstr_w(arg
));
782 handle
= execute_command( binary
->tmpfile
, arg
, szCRoot
);
784 if (handle
== INVALID_HANDLE_VALUE
) return ERROR_SUCCESS
;
785 return wait_process_handle( package
, type
, handle
, action
);
788 static UINT
HANDLE_CustomType17( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
789 INT type
, const WCHAR
*action
)
791 msi_custom_action_info
*info
;
794 TRACE("%s %s\n", debugstr_w(source
), debugstr_w(target
));
796 file
= msi_get_loaded_file( package
, source
);
799 ERR("invalid file key %s\n", debugstr_w( source
));
800 return ERROR_FUNCTION_FAILED
;
803 info
= do_msidbCustomActionTypeDll( package
, type
, file
->TargetPath
, target
, action
);
804 return wait_thread_handle( info
);
807 static UINT
HANDLE_CustomType18( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
808 INT type
, const WCHAR
*action
)
814 if (!(file
= msi_get_loaded_file( package
, source
))) return ERROR_FUNCTION_FAILED
;
816 deformat_string( package
, target
, &arg
);
817 TRACE("exe %s arg %s\n", debugstr_w(file
->TargetPath
), debugstr_w(arg
));
819 handle
= execute_command( file
->TargetPath
, arg
, szCRoot
);
821 if (handle
== INVALID_HANDLE_VALUE
) return ERROR_SUCCESS
;
822 return wait_process_handle( package
, type
, handle
, action
);
825 static UINT
HANDLE_CustomType19( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
826 INT type
, const WCHAR
*action
)
828 static const WCHAR query
[] = {
829 'S','E','L','E','C','T',' ','`','M','e','s','s','a','g','e','`',' ',
830 'F','R','O','M',' ','`','E','r','r','o','r','`',' ',
831 'W','H','E','R','E',' ','`','E','r','r','o','r','`',' ','=',' ',
835 LPWSTR deformated
= NULL
;
837 deformat_string( package
, target
, &deformated
);
839 /* first try treat the error as a number */
840 row
= MSI_QueryGetRecord( package
->db
, query
, deformated
);
843 LPCWSTR error
= MSI_RecordGetString( row
, 1 );
844 if ((package
->ui_level
& INSTALLUILEVEL_MASK
) != INSTALLUILEVEL_NONE
)
845 MessageBoxW( NULL
, error
, NULL
, MB_OK
);
846 msiobj_release( &row
->hdr
);
848 else if ((package
->ui_level
& INSTALLUILEVEL_MASK
) != INSTALLUILEVEL_NONE
)
849 MessageBoxW( NULL
, deformated
, NULL
, MB_OK
);
851 msi_free( deformated
);
853 return ERROR_INSTALL_FAILURE
;
856 static UINT
HANDLE_CustomType23( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
857 INT type
, const WCHAR
*action
)
859 static const WCHAR msiexecW
[] = {'m','s','i','e','x','e','c',0};
860 static const WCHAR paramsW
[] = {'/','q','b',' ','/','i',' '};
861 WCHAR
*dir
, *arg
, *p
;
862 UINT len_src
, len_dir
, len_tgt
, len
= sizeof(paramsW
)/sizeof(paramsW
[0]);
865 if (!(dir
= msi_dup_property( package
->db
, szOriginalDatabase
))) return ERROR_OUTOFMEMORY
;
866 if (!(p
= strrchrW( dir
, '\\' )) && !(p
= strrchrW( dir
, '/' )))
869 return ERROR_FUNCTION_FAILED
;
873 len_src
= strlenW( source
);
874 len_tgt
= strlenW( target
);
875 if (!(arg
= msi_alloc( (len
+ len_dir
+ len_src
+ len_tgt
+ 5) * sizeof(WCHAR
) )))
878 return ERROR_OUTOFMEMORY
;
880 memcpy( arg
, paramsW
, sizeof(paramsW
) );
882 memcpy( arg
+ len
, dir
, len_dir
* sizeof(WCHAR
) );
885 memcpy( arg
+ len
, source
, len_src
* sizeof(WCHAR
) );
889 strcpyW( arg
+ len
, target
);
891 TRACE("installing %s concurrently\n", debugstr_w(source
));
893 handle
= execute_command( msiexecW
, arg
, dir
);
896 if (handle
== INVALID_HANDLE_VALUE
) return ERROR_SUCCESS
;
897 return wait_process_handle( package
, type
, handle
, action
);
900 static UINT
HANDLE_CustomType50( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
901 INT type
, const WCHAR
*action
)
906 if (!(exe
= msi_dup_property( package
->db
, source
))) return ERROR_SUCCESS
;
908 deformat_string( package
, target
, &arg
);
909 TRACE("exe %s arg %s\n", debugstr_w(exe
), debugstr_w(arg
));
911 handle
= execute_command( exe
, arg
, szCRoot
);
914 if (handle
== INVALID_HANDLE_VALUE
) return ERROR_SUCCESS
;
915 return wait_process_handle( package
, type
, handle
, action
);
918 static UINT
HANDLE_CustomType34( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
919 INT type
, const WCHAR
*action
)
921 const WCHAR
*workingdir
= NULL
;
927 workingdir
= msi_get_target_folder( package
, source
);
928 if (!workingdir
) return ERROR_FUNCTION_FAILED
;
930 deformat_string( package
, target
, &cmd
);
931 if (!cmd
) return ERROR_FUNCTION_FAILED
;
933 TRACE("cmd %s dir %s\n", debugstr_w(cmd
), debugstr_w(workingdir
));
935 handle
= execute_command( NULL
, cmd
, workingdir
);
937 if (handle
== INVALID_HANDLE_VALUE
) return ERROR_SUCCESS
;
938 return wait_process_handle( package
, type
, handle
, action
);
941 static DWORD
ACTION_CallScript( const GUID
*guid
)
943 msi_custom_action_info
*info
;
945 UINT r
= ERROR_FUNCTION_FAILED
;
947 info
= find_action_by_guid( guid
);
950 ERR("failed to find action %s\n", debugstr_guid( guid
) );
951 return ERROR_FUNCTION_FAILED
;
954 TRACE("function %s, script %s\n", debugstr_w( info
->target
), debugstr_w( info
->source
) );
956 hPackage
= alloc_msihandle( &info
->package
->hdr
);
959 r
= call_script( hPackage
, info
->type
, info
->source
, info
->target
, info
->action
);
960 TRACE("script returned %u\n", r
);
961 MsiCloseHandle( hPackage
);
964 ERR("failed to create handle for %p\n", info
->package
);
966 release_custom_action_data( info
);
970 static DWORD WINAPI
ScriptThread( LPVOID arg
)
975 TRACE("custom action (%x) started\n", GetCurrentThreadId() );
977 rc
= ACTION_CallScript( guid
);
979 TRACE("custom action (%x) returned %i\n", GetCurrentThreadId(), rc
);
981 MsiCloseAllHandles();
985 static msi_custom_action_info
*do_msidbCustomActionTypeScript(
986 MSIPACKAGE
*package
, INT type
, LPCWSTR script
, LPCWSTR function
, LPCWSTR action
)
988 msi_custom_action_info
*info
;
990 info
= msi_alloc( sizeof *info
);
994 msiobj_addref( &package
->hdr
);
995 info
->refs
= 2; /* 1 for our caller and 1 for thread we created */
996 info
->package
= package
;
998 info
->target
= strdupW( function
);
999 info
->source
= strdupW( script
);
1000 info
->action
= strdupW( action
);
1001 CoCreateGuid( &info
->guid
);
1003 EnterCriticalSection( &msi_custom_action_cs
);
1004 list_add_tail( &msi_pending_custom_actions
, &info
->entry
);
1005 LeaveCriticalSection( &msi_custom_action_cs
);
1007 info
->handle
= CreateThread( NULL
, 0, ScriptThread
, &info
->guid
, 0, NULL
);
1010 /* release both references */
1011 release_custom_action_data( info
);
1012 release_custom_action_data( info
);
1019 static UINT
HANDLE_CustomType37_38( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
1020 INT type
, const WCHAR
*action
)
1022 msi_custom_action_info
*info
;
1024 TRACE("%s %s\n", debugstr_w(source
), debugstr_w(target
));
1026 info
= do_msidbCustomActionTypeScript( package
, type
, target
, NULL
, action
);
1027 return wait_thread_handle( info
);
1030 static UINT
HANDLE_CustomType5_6( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
1031 INT type
, const WCHAR
*action
)
1033 static const WCHAR query
[] = {
1034 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1035 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',' ',
1036 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
1037 MSIRECORD
*row
= NULL
;
1038 msi_custom_action_info
*info
;
1039 CHAR
*buffer
= NULL
;
1040 WCHAR
*bufferw
= NULL
;
1044 TRACE("%s %s\n", debugstr_w(source
), debugstr_w(target
));
1046 row
= MSI_QueryGetRecord(package
->db
, query
, source
);
1048 return ERROR_FUNCTION_FAILED
;
1050 r
= MSI_RecordReadStream(row
, 2, NULL
, &sz
);
1051 if (r
!= ERROR_SUCCESS
) goto done
;
1053 buffer
= msi_alloc( sz
+ 1 );
1056 r
= ERROR_FUNCTION_FAILED
;
1060 r
= MSI_RecordReadStream(row
, 2, buffer
, &sz
);
1061 if (r
!= ERROR_SUCCESS
)
1065 bufferw
= strdupAtoW(buffer
);
1068 r
= ERROR_FUNCTION_FAILED
;
1072 info
= do_msidbCustomActionTypeScript( package
, type
, bufferw
, target
, action
);
1073 r
= wait_thread_handle( info
);
1078 msiobj_release(&row
->hdr
);
1082 static UINT
HANDLE_CustomType21_22( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
1083 INT type
, const WCHAR
*action
)
1085 msi_custom_action_info
*info
;
1088 DWORD sz
, szHighWord
= 0, read
;
1090 WCHAR
*bufferw
=NULL
;
1094 TRACE("%s %s\n", debugstr_w(source
), debugstr_w(target
));
1096 file
= msi_get_loaded_file(package
, source
);
1099 ERR("invalid file key %s\n", debugstr_w(source
));
1100 return ERROR_FUNCTION_FAILED
;
1103 hFile
= CreateFileW(file
->TargetPath
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, NULL
);
1104 if (hFile
== INVALID_HANDLE_VALUE
) return ERROR_FUNCTION_FAILED
;
1106 sz
= GetFileSize(hFile
, &szHighWord
);
1107 if (sz
== INVALID_FILE_SIZE
|| szHighWord
!= 0)
1110 return ERROR_FUNCTION_FAILED
;
1112 buffer
= msi_alloc( sz
+ 1 );
1116 return ERROR_FUNCTION_FAILED
;
1118 bRet
= ReadFile(hFile
, buffer
, sz
, &read
, NULL
);
1122 r
= ERROR_FUNCTION_FAILED
;
1126 bufferw
= strdupAtoW(buffer
);
1129 r
= ERROR_FUNCTION_FAILED
;
1132 info
= do_msidbCustomActionTypeScript( package
, type
, bufferw
, target
, action
);
1133 r
= wait_thread_handle( info
);
1141 static UINT
HANDLE_CustomType53_54( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
1142 INT type
, const WCHAR
*action
)
1144 msi_custom_action_info
*info
;
1147 TRACE("%s %s\n", debugstr_w(source
), debugstr_w(target
));
1149 prop
= msi_dup_property( package
->db
, source
);
1150 if (!prop
) return ERROR_SUCCESS
;
1152 info
= do_msidbCustomActionTypeScript( package
, type
, prop
, NULL
, action
);
1154 return wait_thread_handle( info
);
1157 static BOOL
action_type_matches_script( UINT type
, UINT script
)
1162 case SCRIPT_INSTALL
:
1163 return !(type
& msidbCustomActionTypeCommit
) && !(type
& msidbCustomActionTypeRollback
);
1165 return (type
& msidbCustomActionTypeCommit
);
1166 case SCRIPT_ROLLBACK
:
1167 return (type
& msidbCustomActionTypeRollback
);
1169 ERR("unhandled script %u\n", script
);
1174 static UINT
defer_custom_action( MSIPACKAGE
*package
, const WCHAR
*action
, UINT type
)
1176 WCHAR
*actiondata
= msi_dup_property( package
->db
, action
);
1177 WCHAR
*usersid
= msi_dup_property( package
->db
, szUserSID
);
1178 WCHAR
*prodcode
= msi_dup_property( package
->db
, szProductCode
);
1179 WCHAR
*deferred
= msi_get_deferred_action( action
, actiondata
, usersid
, prodcode
);
1183 msi_free( actiondata
);
1184 msi_free( usersid
);
1185 msi_free( prodcode
);
1186 return ERROR_OUTOFMEMORY
;
1188 if (type
& msidbCustomActionTypeCommit
)
1190 TRACE("deferring commit action\n");
1191 msi_schedule_action( package
, SCRIPT_COMMIT
, deferred
);
1193 else if (type
& msidbCustomActionTypeRollback
)
1195 TRACE("deferring rollback action\n");
1196 msi_schedule_action( package
, SCRIPT_ROLLBACK
, deferred
);
1200 TRACE("deferring install action\n");
1201 msi_schedule_action( package
, SCRIPT_INSTALL
, deferred
);
1204 msi_free( actiondata
);
1205 msi_free( usersid
);
1206 msi_free( prodcode
);
1207 msi_free( deferred
);
1208 return ERROR_SUCCESS
;
1211 UINT
ACTION_CustomAction( MSIPACKAGE
*package
, LPCWSTR action
, UINT script
)
1213 static const WCHAR query
[] = {
1214 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1215 '`','C','u','s','t','o','m','A','c','t','i','o','n','`',' ','W','H','E','R','E',' ',
1216 '`','A','c','t','i' ,'o','n','`',' ','=',' ','\'','%','s','\'',0};
1217 UINT rc
= ERROR_SUCCESS
;
1220 const WCHAR
*source
, *target
, *ptr
, *deferred_data
= NULL
;
1221 WCHAR
*deformated
= NULL
;
1224 /* deferred action: [properties]Action */
1225 if ((ptr
= strrchrW(action
, ']')))
1227 deferred_data
= action
;
1231 row
= MSI_QueryGetRecord( package
->db
, query
, action
);
1233 return ERROR_FUNCTION_NOT_CALLED
;
1235 type
= MSI_RecordGetInteger(row
,2);
1236 source
= MSI_RecordGetString(row
,3);
1237 target
= MSI_RecordGetString(row
,4);
1239 TRACE("Handling custom action %s (%x %s %s)\n",debugstr_w(action
),type
,
1240 debugstr_w(source
), debugstr_w(target
));
1242 /* handle some of the deferred actions */
1243 if (type
& msidbCustomActionTypeTSAware
)
1244 FIXME("msidbCustomActionTypeTSAware not handled\n");
1246 if (type
& msidbCustomActionTypeInScript
)
1248 if (type
& msidbCustomActionTypeNoImpersonate
)
1249 WARN("msidbCustomActionTypeNoImpersonate not handled\n");
1251 if (!action_type_matches_script( type
, script
))
1253 rc
= defer_custom_action( package
, action
, type
);
1258 LPWSTR actiondata
= msi_dup_property( package
->db
, action
);
1260 if (type
& msidbCustomActionTypeInScript
)
1261 package
->scheduled_action_running
= TRUE
;
1263 if (type
& msidbCustomActionTypeCommit
)
1264 package
->commit_action_running
= TRUE
;
1266 if (type
& msidbCustomActionTypeRollback
)
1267 package
->rollback_action_running
= TRUE
;
1270 set_deferred_action_props(package
, deferred_data
);
1271 else if (actiondata
)
1272 msi_set_property( package
->db
, szCustomActionData
, actiondata
, -1 );
1274 msi_set_property( package
->db
, szCustomActionData
, szEmpty
, -1 );
1276 msi_free(actiondata
);
1279 else if (!check_execution_scheduling_options(package
,action
,type
))
1285 switch (type
& CUSTOM_ACTION_TYPE_MASK
)
1287 case 1: /* DLL file stored in a Binary table stream */
1288 rc
= HANDLE_CustomType1(package
,source
,target
,type
,action
);
1290 case 2: /* EXE file stored in a Binary table stream */
1291 rc
= HANDLE_CustomType2(package
,source
,target
,type
,action
);
1293 case 18: /*EXE file installed with package */
1294 rc
= HANDLE_CustomType18(package
,source
,target
,type
,action
);
1296 case 19: /* Error that halts install */
1297 rc
= HANDLE_CustomType19(package
,source
,target
,type
,action
);
1300 rc
= HANDLE_CustomType17(package
,source
,target
,type
,action
);
1302 case 23: /* installs another package in the source tree */
1303 deformat_string(package
,target
,&deformated
);
1304 rc
= HANDLE_CustomType23(package
,source
,deformated
,type
,action
);
1305 msi_free(deformated
);
1307 case 50: /*EXE file specified by a property value */
1308 rc
= HANDLE_CustomType50(package
,source
,target
,type
,action
);
1310 case 34: /*EXE to be run in specified directory */
1311 rc
= HANDLE_CustomType34(package
,source
,target
,type
,action
);
1313 case 35: /* Directory set with formatted text. */
1314 deformat_string(package
,target
,&deformated
);
1315 MSI_SetTargetPathW(package
, source
, deformated
);
1316 msi_free(deformated
);
1318 case 51: /* Property set with formatted text. */
1322 len
= deformat_string( package
, target
, &deformated
);
1323 rc
= msi_set_property( package
->db
, source
, deformated
, len
);
1324 if (rc
== ERROR_SUCCESS
&& !strcmpW( source
, szSourceDir
))
1325 msi_reset_folders( package
, TRUE
);
1326 msi_free(deformated
);
1328 case 37: /* JScript/VBScript text stored in target column. */
1330 rc
= HANDLE_CustomType37_38(package
,source
,target
,type
,action
);
1333 case 6: /* JScript/VBScript file stored in a Binary table stream. */
1334 rc
= HANDLE_CustomType5_6(package
,source
,target
,type
,action
);
1336 case 21: /* JScript/VBScript file installed with the product. */
1338 rc
= HANDLE_CustomType21_22(package
,source
,target
,type
,action
);
1340 case 53: /* JScript/VBScript text specified by a property value. */
1342 rc
= HANDLE_CustomType53_54(package
,source
,target
,type
,action
);
1345 FIXME("unhandled action type %u (%s %s)\n", type
& CUSTOM_ACTION_TYPE_MASK
,
1346 debugstr_w(source
), debugstr_w(target
));
1350 package
->scheduled_action_running
= FALSE
;
1351 package
->commit_action_running
= FALSE
;
1352 package
->rollback_action_running
= FALSE
;
1353 msiobj_release(&row
->hdr
);
1357 void ACTION_FinishCustomActions(const MSIPACKAGE
* package
)
1360 HANDLE
*wait_handles
;
1361 unsigned int handle_count
, i
;
1362 msi_custom_action_info
*info
, *cursor
;
1364 while ((item
= list_head( &package
->RunningActions
)))
1366 MSIRUNNINGACTION
*action
= LIST_ENTRY( item
, MSIRUNNINGACTION
, entry
);
1368 list_remove( &action
->entry
);
1370 TRACE("waiting for %s\n", debugstr_w( action
->name
) );
1371 msi_dialog_check_messages( action
->handle
);
1373 CloseHandle( action
->handle
);
1374 msi_free( action
->name
);
1378 EnterCriticalSection( &msi_custom_action_cs
);
1380 handle_count
= list_count( &msi_pending_custom_actions
);
1381 wait_handles
= msi_alloc( handle_count
* sizeof(HANDLE
) );
1384 LIST_FOR_EACH_ENTRY_SAFE( info
, cursor
, &msi_pending_custom_actions
, msi_custom_action_info
, entry
)
1386 if (info
->package
== package
)
1388 if (DuplicateHandle(GetCurrentProcess(), info
->handle
, GetCurrentProcess(), &wait_handles
[handle_count
], SYNCHRONIZE
, FALSE
, 0))
1393 LeaveCriticalSection( &msi_custom_action_cs
);
1395 for (i
= 0; i
< handle_count
; i
++)
1397 msi_dialog_check_messages( wait_handles
[i
] );
1398 CloseHandle( wait_handles
[i
] );
1400 msi_free( wait_handles
);
1402 EnterCriticalSection( &msi_custom_action_cs
);
1403 LIST_FOR_EACH_ENTRY_SAFE( info
, cursor
, &msi_pending_custom_actions
, msi_custom_action_info
, entry
)
1405 if (info
->package
== package
) release_custom_action_data( info
);
1407 LeaveCriticalSection( &msi_custom_action_cs
);
1410 UINT __cdecl
s_remote_GetActionInfo(const GUID
*guid
, int *type
, LPWSTR
*dll
, LPSTR
*func
, MSIHANDLE
*hinst
)
1412 msi_custom_action_info
*info
;
1414 info
= find_action_by_guid(guid
);
1416 return ERROR_INVALID_DATA
;
1419 *hinst
= alloc_msihandle(&info
->package
->hdr
);
1420 *dll
= strdupW(info
->source
);
1421 *func
= strdupWtoA(info
->target
);
1423 release_custom_action_data(info
);
1424 return ERROR_SUCCESS
;