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 free_custom_action_data( msi_custom_action_info
*info
)
395 EnterCriticalSection( &msi_custom_action_cs
);
397 list_remove( &info
->entry
);
399 CloseHandle( info
->handle
);
400 msi_free( info
->action
);
401 msi_free( info
->source
);
402 msi_free( info
->target
);
403 msiobj_release( &info
->package
->hdr
);
406 LeaveCriticalSection( &msi_custom_action_cs
);
409 static UINT
wait_thread_handle( msi_custom_action_info
*info
)
411 UINT rc
= ERROR_SUCCESS
;
413 if (!(info
->type
& msidbCustomActionTypeAsync
))
415 TRACE("waiting for %s\n", debugstr_w( info
->action
));
417 msi_dialog_check_messages( info
->handle
);
419 if (!(info
->type
& msidbCustomActionTypeContinue
))
420 rc
= custom_get_thread_return( info
->package
, info
->handle
);
422 free_custom_action_data( info
);
426 TRACE("%s running in background\n", debugstr_w( info
->action
));
432 static msi_custom_action_info
*find_action_by_guid( const GUID
*guid
)
434 msi_custom_action_info
*info
;
437 EnterCriticalSection( &msi_custom_action_cs
);
439 LIST_FOR_EACH_ENTRY( info
, &msi_pending_custom_actions
, msi_custom_action_info
, entry
)
441 if (IsEqualGUID( &info
->guid
, guid
))
448 LeaveCriticalSection( &msi_custom_action_cs
);
456 static void handle_msi_break(LPCSTR target
)
458 char format
[] = "To debug your custom action, attach your debugger to "
459 "process %i (0x%X) and press OK";
463 if (!GetEnvironmentVariableA("MsiBreak", val
, MAX_PATH
))
466 if (strcasecmp(val
, target
))
469 sprintf(msg
, format
, GetCurrentProcessId(), GetCurrentProcessId());
470 MessageBoxA(NULL
, msg
, "Windows Installer", MB_OK
);
474 static WCHAR ncalrpcW
[] = {'n','c','a','l','r','p','c',0};
475 static WCHAR endpoint_lrpcW
[] = {'m','s','i',0};
478 /* wrapper for apps that don't declare the thread function correctly */
479 extern UINT
custom_proc_wrapper( MsiCustomActionEntryPoint entry
, MSIHANDLE hinst
);
480 __ASM_GLOBAL_FUNC(custom_proc_wrapper
,
482 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
483 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
485 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
490 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
491 __ASM_CFI(".cfi_same_value %ebp\n\t")
494 static UINT
custom_proc_wrapper( MsiCustomActionEntryPoint entry
, MSIHANDLE hinst
)
500 UINT CDECL
__wine_msi_call_dll_function(const GUID
*guid
)
502 MsiCustomActionEntryPoint fn
;
503 MSIHANDLE remote_package
= 0;
504 RPC_WSTR binding_str
;
513 TRACE("%s\n", debugstr_guid( guid
));
517 status
= RpcStringBindingComposeW(NULL
, ncalrpcW
, NULL
, endpoint_lrpcW
, NULL
, &binding_str
);
518 if (status
!= RPC_S_OK
)
520 ERR("RpcStringBindingCompose failed: %#x\n", status
);
523 status
= RpcBindingFromStringBindingW(binding_str
, &rpc_handle
);
524 if (status
!= RPC_S_OK
)
526 ERR("RpcBindingFromStringBinding failed: %#x\n", status
);
529 RpcStringFreeW(&binding_str
);
532 r
= remote_GetActionInfo(guid
, &type
, &dll
, &proc
, &remote_package
);
533 if (r
!= ERROR_SUCCESS
)
536 hModule
= LoadLibraryW( dll
);
539 ERR( "failed to load dll %s (%u)\n", debugstr_w( dll
), GetLastError() );
540 return ERROR_SUCCESS
;
543 fn
= (MsiCustomActionEntryPoint
) GetProcAddress( hModule
, proc
);
546 hPackage
= alloc_msi_remote_handle( remote_package
);
549 TRACE("calling %s\n", debugstr_a(proc
));
550 handle_msi_break(proc
);
554 r
= custom_proc_wrapper(fn
, hPackage
);
558 ERR("Custom action (%s:%s) caused a page fault: %08x\n",
559 debugstr_w(dll
), debugstr_a(proc
), GetExceptionCode());
564 MsiCloseHandle( hPackage
);
567 ERR("failed to create handle for %x\n", remote_package
);
570 ERR("GetProcAddress(%s) failed\n", debugstr_a(proc
));
572 FreeLibrary(hModule
);
575 midl_user_free(proc
);
580 static DWORD
custom_start_server(MSIPACKAGE
*package
, DWORD arch
)
582 static const WCHAR pipe_name
[] = {'\\','\\','.','\\','p','i','p','e','\\','m','s','i','c','a','_','%','x','_','%','d',0};
583 static const WCHAR msiexecW
[] = {'\\','m','s','i','e','x','e','c','.','e','x','e',0};
584 static const WCHAR argsW
[] = {'%','s',' ','-','E','m','b','e','d','d','i','n','g',' ','%','d',0};
586 WCHAR path
[MAX_PATH
], cmdline
[MAX_PATH
+ 23];
587 PROCESS_INFORMATION pi
= {0};
588 STARTUPINFOW si
= {0};
594 if ((arch
== SCS_32BIT_BINARY
&& package
->custom_server_32_process
) ||
595 (arch
== SCS_64BIT_BINARY
&& package
->custom_server_64_process
))
596 return ERROR_SUCCESS
;
598 sprintfW(buffer
, pipe_name
, GetCurrentProcessId(), arch
== SCS_32BIT_BINARY
? 32 : 64);
599 pipe
= CreateNamedPipeW(buffer
, PIPE_ACCESS_DUPLEX
, 0, 1, sizeof(DWORD64
),
600 sizeof(GUID
), 0, NULL
);
601 if (pipe
== INVALID_HANDLE_VALUE
)
602 ERR("Failed to create custom action client pipe: %u\n", GetLastError());
604 if (sizeof(void *) == 8 && arch
== SCS_32BIT_BINARY
)
605 GetSystemWow64DirectoryW(path
, MAX_PATH
- sizeof(msiexecW
)/sizeof(WCHAR
));
607 GetSystemDirectoryW(path
, MAX_PATH
- sizeof(msiexecW
)/sizeof(WCHAR
));
608 strcatW(path
, msiexecW
);
609 sprintfW(cmdline
, argsW
, path
, GetCurrentProcessId());
611 if (IsWow64Process(GetCurrentProcess(), &wow64
) && wow64
&& arch
== SCS_64BIT_BINARY
)
613 Wow64DisableWow64FsRedirection(&cookie
);
614 CreateProcessW(path
, cmdline
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &si
, &pi
);
615 Wow64RevertWow64FsRedirection(cookie
);
618 CreateProcessW(path
, cmdline
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &si
, &pi
);
620 CloseHandle(pi
.hThread
);
622 if (arch
== SCS_32BIT_BINARY
)
624 package
->custom_server_32_process
= pi
.hProcess
;
625 package
->custom_server_32_pipe
= pipe
;
629 package
->custom_server_64_process
= pi
.hProcess
;
630 package
->custom_server_64_pipe
= pipe
;
633 if (!ConnectNamedPipe(pipe
, NULL
))
635 ERR("Failed to connect to custom action server: %u\n", GetLastError());
636 return GetLastError();
639 return ERROR_SUCCESS
;
642 void custom_stop_server(HANDLE process
, HANDLE pipe
)
646 WriteFile(pipe
, &GUID_NULL
, sizeof(GUID_NULL
), &size
, NULL
);
647 WaitForSingleObject(process
, INFINITE
);
648 CloseHandle(process
);
652 static DWORD WINAPI
custom_client_thread(void *arg
)
654 msi_custom_action_info
*info
= arg
;
662 CoInitializeEx(NULL
, COINIT_MULTITHREADED
); /* needed to marshal streams */
664 if (info
->arch
== SCS_32BIT_BINARY
)
666 process
= info
->package
->custom_server_32_process
;
667 pipe
= info
->package
->custom_server_32_pipe
;
671 process
= info
->package
->custom_server_64_process
;
672 pipe
= info
->package
->custom_server_64_pipe
;
675 EnterCriticalSection(&msi_custom_action_cs
);
677 if (!WriteFile(pipe
, &info
->guid
, sizeof(info
->guid
), &size
, NULL
) ||
678 size
!= sizeof(info
->guid
))
680 ERR("Failed to write to custom action client pipe: %u\n", GetLastError());
681 LeaveCriticalSection(&msi_custom_action_cs
);
682 return GetLastError();
684 if (!ReadFile(pipe
, &thread64
, sizeof(thread64
), &size
, NULL
) || size
!= sizeof(thread64
))
686 ERR("Failed to read from custom action client pipe: %u\n", GetLastError());
687 LeaveCriticalSection(&msi_custom_action_cs
);
688 return GetLastError();
691 LeaveCriticalSection(&msi_custom_action_cs
);
693 if (DuplicateHandle(process
, (HANDLE
)(DWORD_PTR
)thread64
, GetCurrentProcess(),
694 &thread
, 0, FALSE
, DUPLICATE_SAME_ACCESS
| DUPLICATE_CLOSE_SOURCE
))
696 WaitForSingleObject(thread
, INFINITE
);
697 GetExitCodeThread(thread
, &rc
);
707 static msi_custom_action_info
*do_msidbCustomActionTypeDll(
708 MSIPACKAGE
*package
, INT type
, LPCWSTR source
, LPCWSTR target
, LPCWSTR action
)
710 msi_custom_action_info
*info
;
714 info
= msi_alloc( sizeof *info
);
718 msiobj_addref( &package
->hdr
);
719 info
->package
= package
;
721 info
->target
= strdupW( target
);
722 info
->source
= strdupW( source
);
723 info
->action
= strdupW( action
);
724 CoCreateGuid( &info
->guid
);
726 EnterCriticalSection( &msi_custom_action_cs
);
727 list_add_tail( &msi_pending_custom_actions
, &info
->entry
);
728 LeaveCriticalSection( &msi_custom_action_cs
);
730 if (!package
->rpc_server_started
)
732 status
= RpcServerUseProtseqEpW(ncalrpcW
, RPC_C_PROTSEQ_MAX_REQS_DEFAULT
,
733 endpoint_lrpcW
, NULL
);
734 if (status
!= RPC_S_OK
)
736 ERR("RpcServerUseProtseqEp failed: %#x\n", status
);
740 status
= RpcServerRegisterIfEx(s_IWineMsiRemote_v0_0_s_ifspec
, NULL
, NULL
,
741 RPC_IF_AUTOLISTEN
, RPC_C_LISTEN_MAX_CALLS_DEFAULT
, NULL
);
742 if (status
!= RPC_S_OK
)
744 ERR("RpcServerRegisterIfEx failed: %#x\n", status
);
748 info
->package
->rpc_server_started
= 1;
751 ret
= GetBinaryTypeW(source
, &info
->arch
);
753 info
->arch
= (sizeof(void *) == 8 ? SCS_64BIT_BINARY
: SCS_32BIT_BINARY
);
755 custom_start_server(package
, info
->arch
);
757 info
->handle
= CreateThread(NULL
, 0, custom_client_thread
, info
, 0, NULL
);
760 free_custom_action_data( info
);
767 static UINT
HANDLE_CustomType1( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
768 INT type
, const WCHAR
*action
)
770 msi_custom_action_info
*info
;
773 if (!(binary
= get_temp_binary(package
, source
)))
774 return ERROR_FUNCTION_FAILED
;
776 TRACE("Calling function %s from %s\n", debugstr_w(target
), debugstr_w(binary
->tmpfile
));
778 info
= do_msidbCustomActionTypeDll( package
, type
, binary
->tmpfile
, target
, action
);
779 return wait_thread_handle( info
);
782 static HANDLE
execute_command( const WCHAR
*app
, WCHAR
*arg
, const WCHAR
*dir
)
784 static const WCHAR dotexeW
[] = {'.','e','x','e',0};
786 PROCESS_INFORMATION info
;
787 WCHAR
*exe
= NULL
, *cmd
= NULL
, *p
;
795 if (!(exe
= msi_alloc( MAX_PATH
* sizeof(WCHAR
) ))) return INVALID_HANDLE_VALUE
;
796 len_exe
= SearchPathW( NULL
, app
, dotexeW
, MAX_PATH
, exe
, NULL
);
797 if (len_exe
>= MAX_PATH
)
800 if (!(exe
= msi_alloc( len_exe
* sizeof(WCHAR
) ))) return INVALID_HANDLE_VALUE
;
801 len_exe
= SearchPathW( NULL
, app
, dotexeW
, len_exe
, exe
, NULL
);
805 ERR("can't find executable %u\n", GetLastError());
807 return INVALID_HANDLE_VALUE
;
810 if (arg
) len_arg
= strlenW( arg
);
811 if (!(cmd
= msi_alloc( (len_exe
+ len_arg
+ 4) * sizeof(WCHAR
) )))
814 return INVALID_HANDLE_VALUE
;
817 if (strchrW( exe
, ' ' ))
820 memcpy( p
, exe
, len_exe
* sizeof(WCHAR
) );
833 memcpy( p
, arg
, len_arg
* sizeof(WCHAR
) );
837 memset( &si
, 0, sizeof(STARTUPINFOW
) );
838 ret
= CreateProcessW( exe
, exe
? cmd
: arg
, NULL
, NULL
, FALSE
, 0, NULL
, dir
, &si
, &info
);
843 ERR("unable to execute command %u\n", GetLastError());
844 return INVALID_HANDLE_VALUE
;
846 CloseHandle( info
.hThread
);
847 return info
.hProcess
;
850 static UINT
HANDLE_CustomType2( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
851 INT type
, const WCHAR
*action
)
857 if (!(binary
= get_temp_binary(package
, source
)))
858 return ERROR_FUNCTION_FAILED
;
860 deformat_string( package
, target
, &arg
);
861 TRACE("exe %s arg %s\n", debugstr_w(binary
->tmpfile
), debugstr_w(arg
));
863 handle
= execute_command( binary
->tmpfile
, arg
, szCRoot
);
865 if (handle
== INVALID_HANDLE_VALUE
) return ERROR_SUCCESS
;
866 return wait_process_handle( package
, type
, handle
, action
);
869 static UINT
HANDLE_CustomType17( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
870 INT type
, const WCHAR
*action
)
872 msi_custom_action_info
*info
;
875 TRACE("%s %s\n", debugstr_w(source
), debugstr_w(target
));
877 file
= msi_get_loaded_file( package
, source
);
880 ERR("invalid file key %s\n", debugstr_w( source
));
881 return ERROR_FUNCTION_FAILED
;
884 info
= do_msidbCustomActionTypeDll( package
, type
, file
->TargetPath
, target
, action
);
885 return wait_thread_handle( info
);
888 static UINT
HANDLE_CustomType18( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
889 INT type
, const WCHAR
*action
)
895 if (!(file
= msi_get_loaded_file( package
, source
))) return ERROR_FUNCTION_FAILED
;
897 deformat_string( package
, target
, &arg
);
898 TRACE("exe %s arg %s\n", debugstr_w(file
->TargetPath
), debugstr_w(arg
));
900 handle
= execute_command( file
->TargetPath
, arg
, szCRoot
);
902 if (handle
== INVALID_HANDLE_VALUE
) return ERROR_SUCCESS
;
903 return wait_process_handle( package
, type
, handle
, action
);
906 static UINT
HANDLE_CustomType19( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
907 INT type
, const WCHAR
*action
)
909 static const WCHAR query
[] = {
910 'S','E','L','E','C','T',' ','`','M','e','s','s','a','g','e','`',' ',
911 'F','R','O','M',' ','`','E','r','r','o','r','`',' ',
912 'W','H','E','R','E',' ','`','E','r','r','o','r','`',' ','=',' ',
916 LPWSTR deformated
= NULL
;
918 deformat_string( package
, target
, &deformated
);
920 /* first try treat the error as a number */
921 row
= MSI_QueryGetRecord( package
->db
, query
, deformated
);
924 LPCWSTR error
= MSI_RecordGetString( row
, 1 );
925 if ((package
->ui_level
& INSTALLUILEVEL_MASK
) != INSTALLUILEVEL_NONE
)
926 MessageBoxW( NULL
, error
, NULL
, MB_OK
);
927 msiobj_release( &row
->hdr
);
929 else if ((package
->ui_level
& INSTALLUILEVEL_MASK
) != INSTALLUILEVEL_NONE
)
930 MessageBoxW( NULL
, deformated
, NULL
, MB_OK
);
932 msi_free( deformated
);
934 return ERROR_INSTALL_FAILURE
;
937 static UINT
HANDLE_CustomType23( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
938 INT type
, const WCHAR
*action
)
940 static const WCHAR msiexecW
[] = {'m','s','i','e','x','e','c',0};
941 static const WCHAR paramsW
[] = {'/','q','b',' ','/','i',' '};
942 WCHAR
*dir
, *arg
, *p
;
943 UINT len_src
, len_dir
, len_tgt
, len
= sizeof(paramsW
)/sizeof(paramsW
[0]);
946 if (!(dir
= msi_dup_property( package
->db
, szOriginalDatabase
))) return ERROR_OUTOFMEMORY
;
947 if (!(p
= strrchrW( dir
, '\\' )) && !(p
= strrchrW( dir
, '/' )))
950 return ERROR_FUNCTION_FAILED
;
954 len_src
= strlenW( source
);
955 len_tgt
= strlenW( target
);
956 if (!(arg
= msi_alloc( (len
+ len_dir
+ len_src
+ len_tgt
+ 5) * sizeof(WCHAR
) )))
959 return ERROR_OUTOFMEMORY
;
961 memcpy( arg
, paramsW
, sizeof(paramsW
) );
963 memcpy( arg
+ len
, dir
, len_dir
* sizeof(WCHAR
) );
966 memcpy( arg
+ len
, source
, len_src
* sizeof(WCHAR
) );
970 strcpyW( arg
+ len
, target
);
972 TRACE("installing %s concurrently\n", debugstr_w(source
));
974 handle
= execute_command( msiexecW
, arg
, dir
);
977 if (handle
== INVALID_HANDLE_VALUE
) return ERROR_SUCCESS
;
978 return wait_process_handle( package
, type
, handle
, action
);
981 static UINT
HANDLE_CustomType50( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
982 INT type
, const WCHAR
*action
)
987 if (!(exe
= msi_dup_property( package
->db
, source
))) return ERROR_SUCCESS
;
989 deformat_string( package
, target
, &arg
);
990 TRACE("exe %s arg %s\n", debugstr_w(exe
), debugstr_w(arg
));
992 handle
= execute_command( exe
, arg
, szCRoot
);
995 if (handle
== INVALID_HANDLE_VALUE
) return ERROR_SUCCESS
;
996 return wait_process_handle( package
, type
, handle
, action
);
999 static UINT
HANDLE_CustomType34( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
1000 INT type
, const WCHAR
*action
)
1002 const WCHAR
*workingdir
= NULL
;
1008 workingdir
= msi_get_target_folder( package
, source
);
1009 if (!workingdir
) return ERROR_FUNCTION_FAILED
;
1011 deformat_string( package
, target
, &cmd
);
1012 if (!cmd
) return ERROR_FUNCTION_FAILED
;
1014 TRACE("cmd %s dir %s\n", debugstr_w(cmd
), debugstr_w(workingdir
));
1016 handle
= execute_command( NULL
, cmd
, workingdir
);
1018 if (handle
== INVALID_HANDLE_VALUE
) return ERROR_SUCCESS
;
1019 return wait_process_handle( package
, type
, handle
, action
);
1022 static DWORD
ACTION_CallScript( const GUID
*guid
)
1024 msi_custom_action_info
*info
;
1026 UINT r
= ERROR_FUNCTION_FAILED
;
1028 info
= find_action_by_guid( guid
);
1031 ERR("failed to find action %s\n", debugstr_guid( guid
) );
1032 return ERROR_FUNCTION_FAILED
;
1035 TRACE("function %s, script %s\n", debugstr_w( info
->target
), debugstr_w( info
->source
) );
1037 hPackage
= alloc_msihandle( &info
->package
->hdr
);
1040 r
= call_script( hPackage
, info
->type
, info
->source
, info
->target
, info
->action
);
1041 TRACE("script returned %u\n", r
);
1042 MsiCloseHandle( hPackage
);
1045 ERR("failed to create handle for %p\n", info
->package
);
1050 static DWORD WINAPI
ScriptThread( LPVOID arg
)
1055 TRACE("custom action (%x) started\n", GetCurrentThreadId() );
1057 rc
= ACTION_CallScript( guid
);
1059 TRACE("custom action (%x) returned %i\n", GetCurrentThreadId(), rc
);
1061 MsiCloseAllHandles();
1065 static msi_custom_action_info
*do_msidbCustomActionTypeScript(
1066 MSIPACKAGE
*package
, INT type
, LPCWSTR script
, LPCWSTR function
, LPCWSTR action
)
1068 msi_custom_action_info
*info
;
1070 info
= msi_alloc( sizeof *info
);
1074 msiobj_addref( &package
->hdr
);
1075 info
->package
= package
;
1077 info
->target
= strdupW( function
);
1078 info
->source
= strdupW( script
);
1079 info
->action
= strdupW( action
);
1080 CoCreateGuid( &info
->guid
);
1082 EnterCriticalSection( &msi_custom_action_cs
);
1083 list_add_tail( &msi_pending_custom_actions
, &info
->entry
);
1084 LeaveCriticalSection( &msi_custom_action_cs
);
1086 info
->handle
= CreateThread( NULL
, 0, ScriptThread
, &info
->guid
, 0, NULL
);
1089 free_custom_action_data( info
);
1096 static UINT
HANDLE_CustomType37_38( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
1097 INT type
, const WCHAR
*action
)
1099 msi_custom_action_info
*info
;
1101 TRACE("%s %s\n", debugstr_w(source
), debugstr_w(target
));
1103 info
= do_msidbCustomActionTypeScript( package
, type
, target
, NULL
, action
);
1104 return wait_thread_handle( info
);
1107 static UINT
HANDLE_CustomType5_6( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
1108 INT type
, const WCHAR
*action
)
1110 static const WCHAR query
[] = {
1111 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1112 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',' ',
1113 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
1114 MSIRECORD
*row
= NULL
;
1115 msi_custom_action_info
*info
;
1116 CHAR
*buffer
= NULL
;
1117 WCHAR
*bufferw
= NULL
;
1121 TRACE("%s %s\n", debugstr_w(source
), debugstr_w(target
));
1123 row
= MSI_QueryGetRecord(package
->db
, query
, source
);
1125 return ERROR_FUNCTION_FAILED
;
1127 r
= MSI_RecordReadStream(row
, 2, NULL
, &sz
);
1128 if (r
!= ERROR_SUCCESS
) goto done
;
1130 buffer
= msi_alloc( sz
+ 1 );
1133 r
= ERROR_FUNCTION_FAILED
;
1137 r
= MSI_RecordReadStream(row
, 2, buffer
, &sz
);
1138 if (r
!= ERROR_SUCCESS
)
1142 bufferw
= strdupAtoW(buffer
);
1145 r
= ERROR_FUNCTION_FAILED
;
1149 info
= do_msidbCustomActionTypeScript( package
, type
, bufferw
, target
, action
);
1150 r
= wait_thread_handle( info
);
1155 msiobj_release(&row
->hdr
);
1159 static UINT
HANDLE_CustomType21_22( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
1160 INT type
, const WCHAR
*action
)
1162 msi_custom_action_info
*info
;
1165 DWORD sz
, szHighWord
= 0, read
;
1167 WCHAR
*bufferw
=NULL
;
1171 TRACE("%s %s\n", debugstr_w(source
), debugstr_w(target
));
1173 file
= msi_get_loaded_file(package
, source
);
1176 ERR("invalid file key %s\n", debugstr_w(source
));
1177 return ERROR_FUNCTION_FAILED
;
1180 hFile
= CreateFileW(file
->TargetPath
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, NULL
);
1181 if (hFile
== INVALID_HANDLE_VALUE
) return ERROR_FUNCTION_FAILED
;
1183 sz
= GetFileSize(hFile
, &szHighWord
);
1184 if (sz
== INVALID_FILE_SIZE
|| szHighWord
!= 0)
1187 return ERROR_FUNCTION_FAILED
;
1189 buffer
= msi_alloc( sz
+ 1 );
1193 return ERROR_FUNCTION_FAILED
;
1195 bRet
= ReadFile(hFile
, buffer
, sz
, &read
, NULL
);
1199 r
= ERROR_FUNCTION_FAILED
;
1203 bufferw
= strdupAtoW(buffer
);
1206 r
= ERROR_FUNCTION_FAILED
;
1209 info
= do_msidbCustomActionTypeScript( package
, type
, bufferw
, target
, action
);
1210 r
= wait_thread_handle( info
);
1218 static UINT
HANDLE_CustomType53_54( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
1219 INT type
, const WCHAR
*action
)
1221 msi_custom_action_info
*info
;
1224 TRACE("%s %s\n", debugstr_w(source
), debugstr_w(target
));
1226 prop
= msi_dup_property( package
->db
, source
);
1227 if (!prop
) return ERROR_SUCCESS
;
1229 info
= do_msidbCustomActionTypeScript( package
, type
, prop
, NULL
, action
);
1231 return wait_thread_handle( info
);
1234 static BOOL
action_type_matches_script( UINT type
, UINT script
)
1240 case SCRIPT_INSTALL
:
1241 return !(type
& msidbCustomActionTypeCommit
) && !(type
& msidbCustomActionTypeRollback
);
1243 return (type
& msidbCustomActionTypeCommit
);
1244 case SCRIPT_ROLLBACK
:
1245 return (type
& msidbCustomActionTypeRollback
);
1247 ERR("unhandled script %u\n", script
);
1252 static UINT
defer_custom_action( MSIPACKAGE
*package
, const WCHAR
*action
, UINT type
)
1254 WCHAR
*actiondata
= msi_dup_property( package
->db
, action
);
1255 WCHAR
*usersid
= msi_dup_property( package
->db
, szUserSID
);
1256 WCHAR
*prodcode
= msi_dup_property( package
->db
, szProductCode
);
1257 WCHAR
*deferred
= msi_get_deferred_action( action
, actiondata
, usersid
, prodcode
);
1261 msi_free( actiondata
);
1262 msi_free( usersid
);
1263 msi_free( prodcode
);
1264 return ERROR_OUTOFMEMORY
;
1266 if (type
& msidbCustomActionTypeCommit
)
1268 TRACE("deferring commit action\n");
1269 msi_schedule_action( package
, SCRIPT_COMMIT
, deferred
);
1271 else if (type
& msidbCustomActionTypeRollback
)
1273 TRACE("deferring rollback action\n");
1274 msi_schedule_action( package
, SCRIPT_ROLLBACK
, deferred
);
1278 TRACE("deferring install action\n");
1279 msi_schedule_action( package
, SCRIPT_INSTALL
, deferred
);
1282 msi_free( actiondata
);
1283 msi_free( usersid
);
1284 msi_free( prodcode
);
1285 msi_free( deferred
);
1286 return ERROR_SUCCESS
;
1289 UINT
ACTION_CustomAction(MSIPACKAGE
*package
, const WCHAR
*action
)
1291 static const WCHAR query
[] = {
1292 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1293 '`','C','u','s','t','o','m','A','c','t','i','o','n','`',' ','W','H','E','R','E',' ',
1294 '`','A','c','t','i' ,'o','n','`',' ','=',' ','\'','%','s','\'',0};
1295 UINT rc
= ERROR_SUCCESS
;
1298 const WCHAR
*source
, *target
, *ptr
, *deferred_data
= NULL
;
1299 WCHAR
*deformated
= NULL
;
1302 /* deferred action: [properties]Action */
1303 if ((ptr
= strrchrW(action
, ']')))
1305 deferred_data
= action
;
1309 row
= MSI_QueryGetRecord( package
->db
, query
, action
);
1311 return ERROR_FUNCTION_NOT_CALLED
;
1313 type
= MSI_RecordGetInteger(row
,2);
1314 source
= MSI_RecordGetString(row
,3);
1315 target
= MSI_RecordGetString(row
,4);
1317 TRACE("Handling custom action %s (%x %s %s)\n",debugstr_w(action
),type
,
1318 debugstr_w(source
), debugstr_w(target
));
1320 /* handle some of the deferred actions */
1321 if (type
& msidbCustomActionTypeTSAware
)
1322 FIXME("msidbCustomActionTypeTSAware not handled\n");
1324 if (type
& msidbCustomActionTypeInScript
)
1326 if (type
& msidbCustomActionTypeNoImpersonate
)
1327 WARN("msidbCustomActionTypeNoImpersonate not handled\n");
1329 if (!action_type_matches_script(type
, package
->script
))
1331 rc
= defer_custom_action( package
, action
, type
);
1336 LPWSTR actiondata
= msi_dup_property( package
->db
, action
);
1338 if (type
& msidbCustomActionTypeInScript
)
1339 package
->scheduled_action_running
= TRUE
;
1341 if (type
& msidbCustomActionTypeCommit
)
1342 package
->commit_action_running
= TRUE
;
1344 if (type
& msidbCustomActionTypeRollback
)
1345 package
->rollback_action_running
= TRUE
;
1348 set_deferred_action_props(package
, deferred_data
);
1349 else if (actiondata
)
1350 msi_set_property( package
->db
, szCustomActionData
, actiondata
, -1 );
1352 msi_set_property( package
->db
, szCustomActionData
, szEmpty
, -1 );
1354 msi_free(actiondata
);
1357 else if (!check_execution_scheduling_options(package
,action
,type
))
1363 switch (type
& CUSTOM_ACTION_TYPE_MASK
)
1365 case 1: /* DLL file stored in a Binary table stream */
1366 rc
= HANDLE_CustomType1(package
,source
,target
,type
,action
);
1368 case 2: /* EXE file stored in a Binary table stream */
1369 rc
= HANDLE_CustomType2(package
,source
,target
,type
,action
);
1371 case 18: /*EXE file installed with package */
1372 rc
= HANDLE_CustomType18(package
,source
,target
,type
,action
);
1374 case 19: /* Error that halts install */
1375 rc
= HANDLE_CustomType19(package
,source
,target
,type
,action
);
1378 rc
= HANDLE_CustomType17(package
,source
,target
,type
,action
);
1380 case 23: /* installs another package in the source tree */
1381 deformat_string(package
,target
,&deformated
);
1382 rc
= HANDLE_CustomType23(package
,source
,deformated
,type
,action
);
1383 msi_free(deformated
);
1385 case 50: /*EXE file specified by a property value */
1386 rc
= HANDLE_CustomType50(package
,source
,target
,type
,action
);
1388 case 34: /*EXE to be run in specified directory */
1389 rc
= HANDLE_CustomType34(package
,source
,target
,type
,action
);
1391 case 35: /* Directory set with formatted text. */
1392 deformat_string(package
,target
,&deformated
);
1393 MSI_SetTargetPathW(package
, source
, deformated
);
1394 msi_free(deformated
);
1396 case 51: /* Property set with formatted text. */
1400 len
= deformat_string( package
, target
, &deformated
);
1401 rc
= msi_set_property( package
->db
, source
, deformated
, len
);
1402 if (rc
== ERROR_SUCCESS
&& !strcmpW( source
, szSourceDir
))
1403 msi_reset_folders( package
, TRUE
);
1404 msi_free(deformated
);
1406 case 37: /* JScript/VBScript text stored in target column. */
1408 rc
= HANDLE_CustomType37_38(package
,source
,target
,type
,action
);
1411 case 6: /* JScript/VBScript file stored in a Binary table stream. */
1412 rc
= HANDLE_CustomType5_6(package
,source
,target
,type
,action
);
1414 case 21: /* JScript/VBScript file installed with the product. */
1416 rc
= HANDLE_CustomType21_22(package
,source
,target
,type
,action
);
1418 case 53: /* JScript/VBScript text specified by a property value. */
1420 rc
= HANDLE_CustomType53_54(package
,source
,target
,type
,action
);
1423 FIXME("unhandled action type %u (%s %s)\n", type
& CUSTOM_ACTION_TYPE_MASK
,
1424 debugstr_w(source
), debugstr_w(target
));
1428 package
->scheduled_action_running
= FALSE
;
1429 package
->commit_action_running
= FALSE
;
1430 package
->rollback_action_running
= FALSE
;
1431 msiobj_release(&row
->hdr
);
1435 void ACTION_FinishCustomActions(const MSIPACKAGE
* package
)
1438 HANDLE
*wait_handles
;
1439 unsigned int handle_count
, i
;
1440 msi_custom_action_info
*info
, *cursor
;
1442 while ((item
= list_head( &package
->RunningActions
)))
1444 MSIRUNNINGACTION
*action
= LIST_ENTRY( item
, MSIRUNNINGACTION
, entry
);
1446 list_remove( &action
->entry
);
1448 TRACE("waiting for %s\n", debugstr_w( action
->name
) );
1449 msi_dialog_check_messages( action
->handle
);
1451 CloseHandle( action
->handle
);
1452 msi_free( action
->name
);
1456 EnterCriticalSection( &msi_custom_action_cs
);
1458 handle_count
= list_count( &msi_pending_custom_actions
);
1459 wait_handles
= msi_alloc( handle_count
* sizeof(HANDLE
) );
1462 LIST_FOR_EACH_ENTRY_SAFE( info
, cursor
, &msi_pending_custom_actions
, msi_custom_action_info
, entry
)
1464 if (info
->package
== package
)
1466 if (DuplicateHandle(GetCurrentProcess(), info
->handle
, GetCurrentProcess(), &wait_handles
[handle_count
], SYNCHRONIZE
, FALSE
, 0))
1471 LeaveCriticalSection( &msi_custom_action_cs
);
1473 for (i
= 0; i
< handle_count
; i
++)
1475 msi_dialog_check_messages( wait_handles
[i
] );
1476 CloseHandle( wait_handles
[i
] );
1478 msi_free( wait_handles
);
1480 EnterCriticalSection( &msi_custom_action_cs
);
1481 LIST_FOR_EACH_ENTRY_SAFE( info
, cursor
, &msi_pending_custom_actions
, msi_custom_action_info
, entry
)
1483 if (info
->package
== package
)
1484 free_custom_action_data( info
);
1486 LeaveCriticalSection( &msi_custom_action_cs
);
1489 UINT __cdecl
s_remote_GetActionInfo(const GUID
*guid
, int *type
, LPWSTR
*dll
, LPSTR
*func
, MSIHANDLE
*hinst
)
1491 msi_custom_action_info
*info
;
1493 info
= find_action_by_guid(guid
);
1495 return ERROR_INVALID_DATA
;
1498 *hinst
= alloc_msihandle(&info
->package
->hdr
);
1499 *dll
= strdupW(info
->source
);
1500 *func
= strdupWtoA(info
->target
);
1502 return ERROR_SUCCESS
;