ver: Use the 16-bit resource function in GetFileVersionInfo16().
[wine.git] / dlls / msi / custom.c
blobe2379424878b4c59ac125e3a8390c5c50b9fde02
1 /*
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
21 #define COBJMACROS
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winerror.h"
28 #include "msidefs.h"
29 #include "winuser.h"
30 #include "objbase.h"
31 #include "oleauto.h"
33 #include "msipriv.h"
34 #include "winemsi.h"
35 #include "wine/asm.h"
36 #include "wine/heap.h"
37 #include "wine/debug.h"
38 #include "wine/exception.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(msi);
42 #define CUSTOM_ACTION_TYPE_MASK 0x3F
44 typedef struct tagMSIRUNNINGACTION
46 struct list entry;
47 HANDLE handle;
48 BOOL process;
49 LPWSTR name;
50 } MSIRUNNINGACTION;
52 typedef UINT (WINAPI *MsiCustomActionEntryPoint)( MSIHANDLE );
54 static CRITICAL_SECTION msi_custom_action_cs;
55 static CRITICAL_SECTION_DEBUG msi_custom_action_cs_debug =
57 0, 0, &msi_custom_action_cs,
58 { &msi_custom_action_cs_debug.ProcessLocksList,
59 &msi_custom_action_cs_debug.ProcessLocksList },
60 0, 0, { (DWORD_PTR)(__FILE__ ": msi_custom_action_cs") }
62 static CRITICAL_SECTION msi_custom_action_cs = { &msi_custom_action_cs_debug, -1, 0, 0, 0, 0 };
64 static struct list msi_pending_custom_actions = LIST_INIT( msi_pending_custom_actions );
66 void __RPC_FAR * __RPC_USER MIDL_user_allocate(SIZE_T len)
68 return heap_alloc(len);
71 void __RPC_USER MIDL_user_free(void __RPC_FAR * ptr)
73 heap_free(ptr);
76 LONG WINAPI rpc_filter(EXCEPTION_POINTERS *eptr)
78 return I_RpcExceptionFilter(eptr->ExceptionRecord->ExceptionCode);
81 UINT msi_schedule_action( MSIPACKAGE *package, UINT script, const WCHAR *action )
83 UINT count;
84 WCHAR **newbuf = NULL;
86 if (script >= SCRIPT_MAX)
88 FIXME("Unknown script requested %u\n", script);
89 return ERROR_FUNCTION_FAILED;
91 TRACE("Scheduling action %s in script %u\n", debugstr_w(action), script);
93 count = package->script_actions_count[script];
94 package->script_actions_count[script]++;
95 if (count != 0) newbuf = msi_realloc( package->script_actions[script],
96 package->script_actions_count[script] * sizeof(WCHAR *) );
97 else newbuf = msi_alloc( sizeof(WCHAR *) );
99 newbuf[count] = strdupW( action );
100 package->script_actions[script] = newbuf;
101 return ERROR_SUCCESS;
104 UINT msi_register_unique_action( MSIPACKAGE *package, const WCHAR *action )
106 UINT count;
107 WCHAR **newbuf = NULL;
109 TRACE("Registering %s as unique action\n", debugstr_w(action));
111 count = package->unique_actions_count;
112 package->unique_actions_count++;
113 if (count != 0) newbuf = msi_realloc( package->unique_actions,
114 package->unique_actions_count * sizeof(WCHAR *) );
115 else newbuf = msi_alloc( sizeof(WCHAR *) );
117 newbuf[count] = strdupW( action );
118 package->unique_actions = newbuf;
119 return ERROR_SUCCESS;
122 BOOL msi_action_is_unique( const MSIPACKAGE *package, const WCHAR *action )
124 UINT i;
126 for (i = 0; i < package->unique_actions_count; i++)
128 if (!wcscmp( package->unique_actions[i], action )) return TRUE;
130 return FALSE;
133 static BOOL check_execution_scheduling_options(MSIPACKAGE *package, LPCWSTR action, UINT options)
135 if ((options & msidbCustomActionTypeClientRepeat) ==
136 msidbCustomActionTypeClientRepeat)
138 if (!(package->InWhatSequence & SEQUENCE_UI &&
139 package->InWhatSequence & SEQUENCE_EXEC))
141 TRACE("Skipping action due to dbCustomActionTypeClientRepeat option.\n");
142 return FALSE;
145 else if (options & msidbCustomActionTypeFirstSequence)
147 if (package->InWhatSequence & SEQUENCE_UI &&
148 package->InWhatSequence & SEQUENCE_EXEC )
150 TRACE("Skipping action due to msidbCustomActionTypeFirstSequence option.\n");
151 return FALSE;
154 else if (options & msidbCustomActionTypeOncePerProcess)
156 if (msi_action_is_unique(package, action))
158 TRACE("Skipping action due to msidbCustomActionTypeOncePerProcess option.\n");
159 return FALSE;
161 else
162 msi_register_unique_action(package, action);
165 return TRUE;
168 /* stores the following properties before the action:
170 * [CustomActionData<=>UserSID<=>ProductCode]Action
172 static LPWSTR msi_get_deferred_action(LPCWSTR action, LPCWSTR actiondata,
173 LPCWSTR usersid, LPCWSTR prodcode)
175 LPWSTR deferred;
176 DWORD len;
178 static const WCHAR format[] = {
179 '[','%','s','<','=','>','%','s','<','=','>','%','s',']','%','s',0
182 if (!actiondata)
183 return strdupW(action);
185 len = lstrlenW(action) + lstrlenW(actiondata) +
186 lstrlenW(usersid) + lstrlenW(prodcode) +
187 lstrlenW(format) - 7;
188 deferred = msi_alloc(len * sizeof(WCHAR));
190 swprintf(deferred, len, format, actiondata, usersid, prodcode, action);
191 return deferred;
194 static void set_deferred_action_props( MSIPACKAGE *package, const WCHAR *deferred_data )
196 static const WCHAR sep[] = {'<','=','>',0};
197 const WCHAR *end, *beg = deferred_data + 1;
199 end = wcsstr(beg, sep);
200 msi_set_property( package->db, szCustomActionData, beg, end - beg );
201 beg = end + 3;
203 end = wcsstr(beg, sep);
204 msi_set_property( package->db, szUserSID, beg, end - beg );
205 beg = end + 3;
207 end = wcschr(beg, ']');
208 msi_set_property( package->db, szProductCode, beg, end - beg );
211 WCHAR *msi_create_temp_file( MSIDATABASE *db )
213 WCHAR *ret;
215 if (!db->tempfolder)
217 WCHAR tmp[MAX_PATH];
218 UINT len = ARRAY_SIZE( tmp );
220 if (msi_get_property( db, szTempFolder, tmp, &len ) ||
221 GetFileAttributesW( tmp ) != FILE_ATTRIBUTE_DIRECTORY)
223 GetTempPathW( MAX_PATH, tmp );
225 if (!(db->tempfolder = strdupW( tmp ))) return NULL;
228 if ((ret = msi_alloc( (lstrlenW( db->tempfolder ) + 20) * sizeof(WCHAR) )))
230 if (!GetTempFileNameW( db->tempfolder, szMsi, 0, ret ))
232 msi_free( ret );
233 return NULL;
237 return ret;
240 static MSIBINARY *create_temp_binary(MSIPACKAGE *package, LPCWSTR source)
242 static const WCHAR query[] = {
243 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
244 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',' ',
245 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
246 MSIRECORD *row;
247 MSIBINARY *binary = NULL;
248 HANDLE file;
249 CHAR buffer[1024];
250 WCHAR *tmpfile;
251 DWORD sz, write;
252 UINT r;
254 if (!(tmpfile = msi_create_temp_file( package->db ))) return NULL;
256 if (!(row = MSI_QueryGetRecord( package->db, query, source ))) goto error;
257 if (!(binary = msi_alloc_zero( sizeof(MSIBINARY) ))) goto error;
259 file = CreateFileW( tmpfile, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
260 if (file == INVALID_HANDLE_VALUE) goto error;
264 sz = sizeof(buffer);
265 r = MSI_RecordReadStream( row, 2, buffer, &sz );
266 if (r != ERROR_SUCCESS)
268 ERR("Failed to get stream\n");
269 break;
271 WriteFile( file, buffer, sz, &write, NULL );
272 } while (sz == sizeof buffer);
274 CloseHandle( file );
275 if (r != ERROR_SUCCESS) goto error;
277 binary->source = strdupW( source );
278 binary->tmpfile = tmpfile;
279 list_add_tail( &package->binaries, &binary->entry );
281 msiobj_release( &row->hdr );
282 return binary;
284 error:
285 if (row) msiobj_release( &row->hdr );
286 DeleteFileW( tmpfile );
287 msi_free( tmpfile );
288 msi_free( binary );
289 return NULL;
292 static MSIBINARY *get_temp_binary(MSIPACKAGE *package, LPCWSTR source)
294 MSIBINARY *binary;
296 LIST_FOR_EACH_ENTRY( binary, &package->binaries, MSIBINARY, entry )
298 if (!wcscmp( binary->source, source ))
299 return binary;
302 return create_temp_binary(package, source);
305 static void file_running_action(MSIPACKAGE* package, HANDLE Handle,
306 BOOL process, LPCWSTR name)
308 MSIRUNNINGACTION *action;
310 action = msi_alloc( sizeof(MSIRUNNINGACTION) );
312 action->handle = Handle;
313 action->process = process;
314 action->name = strdupW(name);
316 list_add_tail( &package->RunningActions, &action->entry );
319 static UINT custom_get_process_return( HANDLE process )
321 DWORD rc = 0;
323 GetExitCodeProcess( process, &rc );
324 TRACE("exit code is %u\n", rc);
325 if (rc != 0)
326 return ERROR_FUNCTION_FAILED;
327 return ERROR_SUCCESS;
330 static UINT custom_get_thread_return( MSIPACKAGE *package, HANDLE thread )
332 DWORD rc = 0;
334 GetExitCodeThread( thread, &rc );
336 switch (rc)
338 case ERROR_FUNCTION_NOT_CALLED:
339 case ERROR_SUCCESS:
340 case ERROR_INSTALL_USEREXIT:
341 case ERROR_INSTALL_FAILURE:
342 return rc;
343 case ERROR_NO_MORE_ITEMS:
344 return ERROR_SUCCESS;
345 case ERROR_INSTALL_SUSPEND:
346 ACTION_ForceReboot( package );
347 return ERROR_SUCCESS;
348 default:
349 ERR("Invalid Return Code %d\n",rc);
350 return ERROR_INSTALL_FAILURE;
354 static UINT wait_process_handle(MSIPACKAGE* package, UINT type,
355 HANDLE ProcessHandle, LPCWSTR name)
357 UINT rc = ERROR_SUCCESS;
359 if (!(type & msidbCustomActionTypeAsync))
361 TRACE("waiting for %s\n", debugstr_w(name));
363 msi_dialog_check_messages(ProcessHandle);
365 if (!(type & msidbCustomActionTypeContinue))
366 rc = custom_get_process_return(ProcessHandle);
368 CloseHandle(ProcessHandle);
370 else
372 TRACE("%s running in background\n", debugstr_w(name));
374 if (!(type & msidbCustomActionTypeContinue))
375 file_running_action(package, ProcessHandle, TRUE, name);
376 else
377 CloseHandle(ProcessHandle);
380 return rc;
383 typedef struct _msi_custom_action_info {
384 struct list entry;
385 MSIPACKAGE *package;
386 LPWSTR source;
387 LPWSTR target;
388 HANDLE handle;
389 LPWSTR action;
390 INT type;
391 GUID guid;
392 DWORD arch;
393 } msi_custom_action_info;
395 static void free_custom_action_data( msi_custom_action_info *info )
397 EnterCriticalSection( &msi_custom_action_cs );
399 list_remove( &info->entry );
400 if (info->handle)
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 );
406 msi_free( info );
408 LeaveCriticalSection( &msi_custom_action_cs );
411 static UINT wait_thread_handle( msi_custom_action_info *info )
413 UINT rc = ERROR_SUCCESS;
415 if (!(info->type & msidbCustomActionTypeAsync))
417 TRACE("waiting for %s\n", debugstr_w( info->action ));
419 msi_dialog_check_messages( info->handle );
421 if (!(info->type & msidbCustomActionTypeContinue))
422 rc = custom_get_thread_return( info->package, info->handle );
424 free_custom_action_data( info );
426 else
428 TRACE("%s running in background\n", debugstr_w( info->action ));
431 return rc;
434 static msi_custom_action_info *find_action_by_guid( const GUID *guid )
436 msi_custom_action_info *info;
437 BOOL found = FALSE;
439 EnterCriticalSection( &msi_custom_action_cs );
441 LIST_FOR_EACH_ENTRY( info, &msi_pending_custom_actions, msi_custom_action_info, entry )
443 if (IsEqualGUID( &info->guid, guid ))
445 found = TRUE;
446 break;
450 LeaveCriticalSection( &msi_custom_action_cs );
452 if (!found)
453 return NULL;
455 return info;
458 static void handle_msi_break(LPCSTR target)
460 char format[] = "To debug your custom action, attach your debugger to "
461 "process %i (0x%X) and press OK";
462 char val[MAX_PATH];
463 char msg[100];
465 if (!GetEnvironmentVariableA("MsiBreak", val, MAX_PATH))
466 return;
468 if (strcmp(val, target))
469 return;
471 sprintf(msg, format, GetCurrentProcessId(), GetCurrentProcessId());
472 MessageBoxA(NULL, msg, "Windows Installer", MB_OK);
473 DebugBreak();
476 static WCHAR ncalrpcW[] = {'n','c','a','l','r','p','c',0};
477 static WCHAR endpoint_fmtW[] = {'m','s','i','%','x',0};
479 #ifdef __i386__
480 /* wrapper for apps that don't declare the thread function correctly */
481 extern UINT custom_proc_wrapper( MsiCustomActionEntryPoint entry, MSIHANDLE hinst );
482 __ASM_GLOBAL_FUNC(custom_proc_wrapper,
483 "pushl %ebp\n\t"
484 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
485 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
486 "movl %esp,%ebp\n\t"
487 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
488 "subl $4,%esp\n\t"
489 "pushl 12(%ebp)\n\t"
490 "call *8(%ebp)\n\t"
491 "leave\n\t"
492 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
493 __ASM_CFI(".cfi_same_value %ebp\n\t")
494 "ret" )
495 #else
496 static UINT custom_proc_wrapper( MsiCustomActionEntryPoint entry, MSIHANDLE hinst )
498 return entry(hinst);
500 #endif
502 UINT CDECL __wine_msi_call_dll_function(DWORD client_pid, const GUID *guid)
504 MsiCustomActionEntryPoint fn;
505 MSIHANDLE remote_package = 0;
506 RPC_WSTR binding_str;
507 MSIHANDLE hPackage;
508 RPC_STATUS status;
509 LPWSTR dll = NULL;
510 LPSTR proc = NULL;
511 HANDLE hModule;
512 INT type;
513 UINT r;
515 TRACE("%s\n", debugstr_guid( guid ));
517 if (!rpc_handle)
519 WCHAR endpoint[12];
521 swprintf(endpoint, ARRAY_SIZE(endpoint), endpoint_fmtW, client_pid);
522 status = RpcStringBindingComposeW(NULL, ncalrpcW, NULL, endpoint, NULL, &binding_str);
523 if (status != RPC_S_OK)
525 ERR("RpcStringBindingCompose failed: %#x\n", status);
526 return status;
528 status = RpcBindingFromStringBindingW(binding_str, &rpc_handle);
529 if (status != RPC_S_OK)
531 ERR("RpcBindingFromStringBinding failed: %#x\n", status);
532 return status;
534 RpcStringFreeW(&binding_str);
537 r = remote_GetActionInfo(guid, &type, &dll, &proc, &remote_package);
538 if (r != ERROR_SUCCESS)
539 return r;
541 hPackage = alloc_msi_remote_handle( remote_package );
542 if (!hPackage)
544 ERR( "failed to create handle for %x\n", remote_package );
545 midl_user_free( dll );
546 midl_user_free( proc );
547 return ERROR_INSTALL_FAILURE;
550 hModule = LoadLibraryW( dll );
551 if (!hModule)
553 ERR( "failed to load dll %s (%u)\n", debugstr_w( dll ), GetLastError() );
554 midl_user_free( dll );
555 midl_user_free( proc );
556 MsiCloseHandle( hPackage );
557 return ERROR_SUCCESS;
560 fn = (MsiCustomActionEntryPoint) GetProcAddress( hModule, proc );
561 if (!fn) WARN( "GetProcAddress(%s) failed\n", debugstr_a(proc) );
562 else
564 handle_msi_break(proc);
566 __TRY
568 r = custom_proc_wrapper( fn, hPackage );
570 __EXCEPT_PAGE_FAULT
572 ERR( "Custom action (%s:%s) caused a page fault: %08x\n",
573 debugstr_w(dll), debugstr_a(proc), GetExceptionCode() );
574 r = ERROR_SUCCESS;
576 __ENDTRY;
579 FreeLibrary(hModule);
581 midl_user_free(dll);
582 midl_user_free(proc);
584 MsiCloseAllHandles();
586 return r;
589 static DWORD custom_start_server(MSIPACKAGE *package, DWORD arch)
591 static const WCHAR pipe_name[] = {'\\','\\','.','\\','p','i','p','e','\\','m','s','i','c','a','_','%','x','_','%','d',0};
592 static const WCHAR msiexecW[] = {'\\','m','s','i','e','x','e','c','.','e','x','e',0};
593 static const WCHAR argsW[] = {'%','s',' ','-','E','m','b','e','d','d','i','n','g',' ','%','d',0};
595 WCHAR path[MAX_PATH], cmdline[MAX_PATH + 23];
596 PROCESS_INFORMATION pi = {0};
597 STARTUPINFOW si = {0};
598 WCHAR buffer[24];
599 void *cookie;
600 HANDLE pipe;
601 BOOL wow64;
603 if ((arch == SCS_32BIT_BINARY && package->custom_server_32_process) ||
604 (arch == SCS_64BIT_BINARY && package->custom_server_64_process))
605 return ERROR_SUCCESS;
607 swprintf(buffer, ARRAY_SIZE(buffer), pipe_name,
608 GetCurrentProcessId(), arch == SCS_32BIT_BINARY ? 32 : 64);
609 pipe = CreateNamedPipeW(buffer, PIPE_ACCESS_DUPLEX, 0, 1, sizeof(DWORD64),
610 sizeof(GUID), 0, NULL);
611 if (pipe == INVALID_HANDLE_VALUE)
612 ERR("Failed to create custom action client pipe: %u\n", GetLastError());
614 if (!IsWow64Process(GetCurrentProcess(), &wow64))
615 wow64 = FALSE;
617 if ((sizeof(void *) == 8 || wow64) && arch == SCS_32BIT_BINARY)
618 GetSystemWow64DirectoryW(path, MAX_PATH - ARRAY_SIZE(msiexecW));
619 else
620 GetSystemDirectoryW(path, MAX_PATH - ARRAY_SIZE(msiexecW));
621 lstrcatW(path, msiexecW);
622 swprintf(cmdline, ARRAY_SIZE(cmdline), argsW, path, GetCurrentProcessId());
624 if (wow64 && arch == SCS_64BIT_BINARY)
626 Wow64DisableWow64FsRedirection(&cookie);
627 CreateProcessW(path, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
628 Wow64RevertWow64FsRedirection(cookie);
630 else
631 CreateProcessW(path, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
633 CloseHandle(pi.hThread);
635 if (arch == SCS_32BIT_BINARY)
637 package->custom_server_32_process = pi.hProcess;
638 package->custom_server_32_pipe = pipe;
640 else
642 package->custom_server_64_process = pi.hProcess;
643 package->custom_server_64_pipe = pipe;
646 if (!ConnectNamedPipe(pipe, NULL))
648 ERR("Failed to connect to custom action server: %u\n", GetLastError());
649 return GetLastError();
652 return ERROR_SUCCESS;
655 void custom_stop_server(HANDLE process, HANDLE pipe)
657 DWORD size;
659 WriteFile(pipe, &GUID_NULL, sizeof(GUID_NULL), &size, NULL);
660 WaitForSingleObject(process, INFINITE);
661 CloseHandle(process);
662 CloseHandle(pipe);
665 static DWORD WINAPI custom_client_thread(void *arg)
667 msi_custom_action_info *info = arg;
668 DWORD64 thread64;
669 HANDLE process;
670 HANDLE thread;
671 HANDLE pipe;
672 DWORD size;
673 UINT rc;
675 CoInitializeEx(NULL, COINIT_MULTITHREADED); /* needed to marshal streams */
677 if (info->arch == SCS_32BIT_BINARY)
679 process = info->package->custom_server_32_process;
680 pipe = info->package->custom_server_32_pipe;
682 else
684 process = info->package->custom_server_64_process;
685 pipe = info->package->custom_server_64_pipe;
688 EnterCriticalSection(&msi_custom_action_cs);
690 if (!WriteFile(pipe, &info->guid, sizeof(info->guid), &size, NULL) ||
691 size != sizeof(info->guid))
693 ERR("Failed to write to custom action client pipe: %u\n", GetLastError());
694 LeaveCriticalSection(&msi_custom_action_cs);
695 return GetLastError();
697 if (!ReadFile(pipe, &thread64, sizeof(thread64), &size, NULL) || size != sizeof(thread64))
699 ERR("Failed to read from custom action client pipe: %u\n", GetLastError());
700 LeaveCriticalSection(&msi_custom_action_cs);
701 return GetLastError();
704 LeaveCriticalSection(&msi_custom_action_cs);
706 if (DuplicateHandle(process, (HANDLE)(DWORD_PTR)thread64, GetCurrentProcess(),
707 &thread, 0, FALSE, DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE))
709 WaitForSingleObject(thread, INFINITE);
710 GetExitCodeThread(thread, &rc);
711 CloseHandle(thread);
713 else
714 rc = GetLastError();
716 CoUninitialize();
717 return rc;
720 static msi_custom_action_info *do_msidbCustomActionTypeDll(
721 MSIPACKAGE *package, INT type, LPCWSTR source, LPCWSTR target, LPCWSTR action )
723 msi_custom_action_info *info;
724 RPC_STATUS status;
725 BOOL ret;
727 info = msi_alloc( sizeof *info );
728 if (!info)
729 return NULL;
731 msiobj_addref( &package->hdr );
732 info->package = package;
733 info->type = type;
734 info->target = strdupW( target );
735 info->source = strdupW( source );
736 info->action = strdupW( action );
737 CoCreateGuid( &info->guid );
739 EnterCriticalSection( &msi_custom_action_cs );
740 list_add_tail( &msi_pending_custom_actions, &info->entry );
741 LeaveCriticalSection( &msi_custom_action_cs );
743 if (!package->rpc_server_started)
745 WCHAR endpoint[12];
747 swprintf(endpoint, ARRAY_SIZE(endpoint), endpoint_fmtW, GetCurrentProcessId());
748 status = RpcServerUseProtseqEpW(ncalrpcW, RPC_C_PROTSEQ_MAX_REQS_DEFAULT,
749 endpoint, NULL);
750 if (status != RPC_S_OK)
752 ERR("RpcServerUseProtseqEp failed: %#x\n", status);
753 return NULL;
756 status = RpcServerRegisterIfEx(s_IWineMsiRemote_v0_0_s_ifspec, NULL, NULL,
757 RPC_IF_AUTOLISTEN, RPC_C_LISTEN_MAX_CALLS_DEFAULT, NULL);
758 if (status != RPC_S_OK)
760 ERR("RpcServerRegisterIfEx failed: %#x\n", status);
761 return NULL;
764 info->package->rpc_server_started = 1;
767 ret = GetBinaryTypeW(source, &info->arch);
768 if (!ret)
769 info->arch = (sizeof(void *) == 8 ? SCS_64BIT_BINARY : SCS_32BIT_BINARY);
771 custom_start_server(package, info->arch);
773 info->handle = CreateThread(NULL, 0, custom_client_thread, info, 0, NULL);
774 if (!info->handle)
776 free_custom_action_data( info );
777 return NULL;
780 return info;
783 static UINT HANDLE_CustomType1( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
784 INT type, const WCHAR *action )
786 msi_custom_action_info *info;
787 MSIBINARY *binary;
789 if (!(binary = get_temp_binary(package, source)))
790 return ERROR_FUNCTION_FAILED;
792 TRACE("Calling function %s from %s\n", debugstr_w(target), debugstr_w(binary->tmpfile));
794 info = do_msidbCustomActionTypeDll( package, type, binary->tmpfile, target, action );
795 return wait_thread_handle( info );
798 static HANDLE execute_command( const WCHAR *app, WCHAR *arg, const WCHAR *dir )
800 static const WCHAR dotexeW[] = {'.','e','x','e',0};
801 STARTUPINFOW si;
802 PROCESS_INFORMATION info;
803 WCHAR *exe = NULL, *cmd = NULL, *p;
804 BOOL ret;
806 if (app)
808 int len_arg = 0;
809 DWORD len_exe;
811 if (!(exe = msi_alloc( MAX_PATH * sizeof(WCHAR) ))) return INVALID_HANDLE_VALUE;
812 len_exe = SearchPathW( NULL, app, dotexeW, MAX_PATH, exe, NULL );
813 if (len_exe >= MAX_PATH)
815 msi_free( exe );
816 if (!(exe = msi_alloc( len_exe * sizeof(WCHAR) ))) return INVALID_HANDLE_VALUE;
817 len_exe = SearchPathW( NULL, app, dotexeW, len_exe, exe, NULL );
819 if (!len_exe)
821 ERR("can't find executable %u\n", GetLastError());
822 msi_free( exe );
823 return INVALID_HANDLE_VALUE;
826 if (arg) len_arg = lstrlenW( arg );
827 if (!(cmd = msi_alloc( (len_exe + len_arg + 4) * sizeof(WCHAR) )))
829 msi_free( exe );
830 return INVALID_HANDLE_VALUE;
832 p = cmd;
833 if (wcschr( exe, ' ' ))
835 *p++ = '\"';
836 memcpy( p, exe, len_exe * sizeof(WCHAR) );
837 p += len_exe;
838 *p++ = '\"';
839 *p = 0;
841 else
843 lstrcpyW( p, exe );
844 p += len_exe;
846 if (arg)
848 *p++ = ' ';
849 memcpy( p, arg, len_arg * sizeof(WCHAR) );
850 p[len_arg] = 0;
853 memset( &si, 0, sizeof(STARTUPINFOW) );
854 ret = CreateProcessW( exe, exe ? cmd : arg, NULL, NULL, FALSE, 0, NULL, dir, &si, &info );
855 msi_free( cmd );
856 msi_free( exe );
857 if (!ret)
859 ERR("unable to execute command %u\n", GetLastError());
860 return INVALID_HANDLE_VALUE;
862 CloseHandle( info.hThread );
863 return info.hProcess;
866 static UINT HANDLE_CustomType2( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
867 INT type, const WCHAR *action )
869 MSIBINARY *binary;
870 HANDLE handle;
871 WCHAR *arg;
873 if (!(binary = get_temp_binary(package, source)))
874 return ERROR_FUNCTION_FAILED;
876 deformat_string( package, target, &arg );
877 TRACE("exe %s arg %s\n", debugstr_w(binary->tmpfile), debugstr_w(arg));
879 handle = execute_command( binary->tmpfile, arg, szCRoot );
880 msi_free( arg );
881 if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
882 return wait_process_handle( package, type, handle, action );
885 static UINT HANDLE_CustomType17( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
886 INT type, const WCHAR *action )
888 msi_custom_action_info *info;
889 MSIFILE *file;
891 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
893 file = msi_get_loaded_file( package, source );
894 if (!file)
896 ERR("invalid file key %s\n", debugstr_w( source ));
897 return ERROR_FUNCTION_FAILED;
900 info = do_msidbCustomActionTypeDll( package, type, file->TargetPath, target, action );
901 return wait_thread_handle( info );
904 static UINT HANDLE_CustomType18( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
905 INT type, const WCHAR *action )
907 MSIFILE *file;
908 HANDLE handle;
909 WCHAR *arg;
911 if (!(file = msi_get_loaded_file( package, source ))) return ERROR_FUNCTION_FAILED;
913 deformat_string( package, target, &arg );
914 TRACE("exe %s arg %s\n", debugstr_w(file->TargetPath), debugstr_w(arg));
916 handle = execute_command( file->TargetPath, arg, szCRoot );
917 msi_free( arg );
918 if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
919 return wait_process_handle( package, type, handle, action );
922 static UINT HANDLE_CustomType19( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
923 INT type, const WCHAR *action )
925 static const WCHAR query[] = {
926 'S','E','L','E','C','T',' ','`','M','e','s','s','a','g','e','`',' ',
927 'F','R','O','M',' ','`','E','r','r','o','r','`',' ',
928 'W','H','E','R','E',' ','`','E','r','r','o','r','`',' ','=',' ',
929 '%','s',0
931 MSIRECORD *row = 0;
932 LPWSTR deformated = NULL;
934 deformat_string( package, target, &deformated );
936 /* first try treat the error as a number */
937 row = MSI_QueryGetRecord( package->db, query, deformated );
938 if( row )
940 LPCWSTR error = MSI_RecordGetString( row, 1 );
941 if ((package->ui_level & INSTALLUILEVEL_MASK) != INSTALLUILEVEL_NONE)
942 MessageBoxW( NULL, error, NULL, MB_OK );
943 msiobj_release( &row->hdr );
945 else if ((package->ui_level & INSTALLUILEVEL_MASK) != INSTALLUILEVEL_NONE)
946 MessageBoxW( NULL, deformated, NULL, MB_OK );
948 msi_free( deformated );
950 return ERROR_INSTALL_FAILURE;
953 static WCHAR *build_msiexec_args( const WCHAR *filename, const WCHAR *params )
955 UINT len_filename = lstrlenW( filename ), len_params = lstrlenW( params );
956 UINT len = ARRAY_SIZE(L"/qb /i ") - 1;
957 WCHAR *ret;
959 if (!(ret = msi_alloc( (len + len_filename + len_params + 4) * sizeof(WCHAR) ))) return NULL;
960 memcpy( ret, L"/qb /i ", sizeof(L"/qb /i ") );
961 ret[len++] = '"';
962 memcpy( ret + len, filename, len_filename * sizeof(WCHAR) );
963 len += len_filename;
964 ret[len++] = '"';
965 ret[len++] = ' ';
966 lstrcpyW( ret + len, params );
967 return ret;
970 static UINT HANDLE_CustomType23( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
971 INT type, const WCHAR *action )
973 WCHAR *dir, *filename, *args, *p;
974 UINT len_dir, len_source = lstrlenW( source );
975 HANDLE handle;
977 if (!(dir = msi_dup_property( package->db, szOriginalDatabase ))) return ERROR_OUTOFMEMORY;
978 if (!(p = wcsrchr( dir, '\\' )) && !(p = wcsrchr( dir, '/' )))
980 msi_free( dir );
981 return ERROR_FUNCTION_FAILED;
983 *p = 0;
984 len_dir = p - dir;
985 if (!(filename = msi_alloc( (len_dir + len_source + 2) * sizeof(WCHAR) )))
987 msi_free( dir );
988 return ERROR_OUTOFMEMORY;
990 memcpy( filename, dir, len_dir * sizeof(WCHAR) );
991 filename[len_dir++] = '\\';
992 memcpy( filename + len_dir, source, len_source * sizeof(WCHAR) );
993 filename[len_dir + len_source] = 0;
995 if (!(args = build_msiexec_args( filename, target )))
997 msi_free( dir );
998 return ERROR_OUTOFMEMORY;
1001 TRACE("installing %s concurrently\n", debugstr_w(source));
1003 handle = execute_command( L"msiexec", args, dir );
1004 msi_free( dir );
1005 msi_free( args );
1006 if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
1007 return wait_process_handle( package, type, handle, action );
1010 static UINT write_substorage_to_file( MSIPACKAGE *package, const WCHAR *source, const WCHAR *filename )
1012 IStorage *src = NULL, *dst = NULL;
1013 UINT r = ERROR_FUNCTION_FAILED;
1014 HRESULT hr;
1016 hr = StgCreateDocfile( filename, STGM_CREATE|STGM_TRANSACTED|STGM_WRITE|STGM_SHARE_EXCLUSIVE, 0, &dst );
1017 if (FAILED( hr ))
1019 WARN( "can't open destination storage %s (%08x)\n", debugstr_w(filename), hr );
1020 goto done;
1023 hr = IStorage_OpenStorage( package->db->storage, source, NULL, STGM_SHARE_EXCLUSIVE, NULL, 0, &src );
1024 if (FAILED( hr ))
1026 WARN( "can't open source storage %s (%08x)\n", debugstr_w(source), hr );
1027 goto done;
1030 hr = IStorage_CopyTo( src, 0, NULL, NULL, dst );
1031 if (FAILED( hr ))
1033 ERR( "failed to copy storage %s (%08x)\n", debugstr_w(source), hr );
1034 goto done;
1037 hr = IStorage_Commit( dst, 0 );
1038 if (FAILED( hr ))
1039 ERR( "failed to commit storage (%08x)\n", hr );
1040 else
1041 r = ERROR_SUCCESS;
1043 done:
1044 if (src) IStorage_Release( src );
1045 if (dst) IStorage_Release( dst );
1046 return r;
1049 static UINT HANDLE_CustomType7( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
1050 INT type, const WCHAR *action )
1052 WCHAR *tmpfile, *args;
1053 MSIBINARY *binary = NULL;
1054 HANDLE handle;
1055 UINT r;
1057 if (!(tmpfile = msi_create_temp_file( package->db ))) return ERROR_FUNCTION_FAILED;
1059 r = write_substorage_to_file( package, source, tmpfile );
1060 if (r != ERROR_SUCCESS)
1061 goto error;
1063 if (!(binary = msi_alloc( sizeof(*binary) ))) goto error;
1064 binary->source = NULL;
1065 binary->tmpfile = tmpfile;
1066 list_add_tail( &package->binaries, &binary->entry );
1068 if (!(args = build_msiexec_args( tmpfile, target ))) return ERROR_OUTOFMEMORY;
1070 TRACE("installing %s concurrently\n", debugstr_w(source));
1072 handle = execute_command( L"msiexec", args, L"C:\\" );
1073 msi_free( args );
1074 if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
1075 return wait_process_handle( package, type, handle, action );
1077 error:
1078 DeleteFileW( tmpfile );
1079 msi_free( tmpfile );
1080 return ERROR_FUNCTION_FAILED;
1083 static UINT HANDLE_CustomType50( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
1084 INT type, const WCHAR *action )
1086 WCHAR *exe, *arg;
1087 HANDLE handle;
1089 if (!(exe = msi_dup_property( package->db, source ))) return ERROR_SUCCESS;
1091 deformat_string( package, target, &arg );
1092 TRACE("exe %s arg %s\n", debugstr_w(exe), debugstr_w(arg));
1094 handle = execute_command( exe, arg, szCRoot );
1095 msi_free( exe );
1096 msi_free( arg );
1097 if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
1098 return wait_process_handle( package, type, handle, action );
1101 static UINT HANDLE_CustomType34( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
1102 INT type, const WCHAR *action )
1104 const WCHAR *workingdir = NULL;
1105 HANDLE handle;
1106 WCHAR *cmd;
1108 if (source)
1110 workingdir = msi_get_target_folder( package, source );
1111 if (!workingdir) return ERROR_FUNCTION_FAILED;
1113 deformat_string( package, target, &cmd );
1114 if (!cmd) return ERROR_FUNCTION_FAILED;
1116 TRACE("cmd %s dir %s\n", debugstr_w(cmd), debugstr_w(workingdir));
1118 handle = execute_command( NULL, cmd, workingdir );
1119 msi_free( cmd );
1120 if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
1121 return wait_process_handle( package, type, handle, action );
1124 static DWORD ACTION_CallScript( const GUID *guid )
1126 msi_custom_action_info *info;
1127 MSIHANDLE hPackage;
1128 UINT r = ERROR_FUNCTION_FAILED;
1130 info = find_action_by_guid( guid );
1131 if (!info)
1133 ERR("failed to find action %s\n", debugstr_guid( guid) );
1134 return ERROR_FUNCTION_FAILED;
1137 TRACE("function %s, script %s\n", debugstr_w( info->target ), debugstr_w( info->source ) );
1139 hPackage = alloc_msihandle( &info->package->hdr );
1140 if (hPackage)
1142 r = call_script( hPackage, info->type, info->source, info->target, info->action );
1143 TRACE("script returned %u\n", r);
1144 MsiCloseHandle( hPackage );
1146 else
1147 ERR("failed to create handle for %p\n", info->package );
1149 return r;
1152 static DWORD WINAPI ScriptThread( LPVOID arg )
1154 LPGUID guid = arg;
1155 DWORD rc;
1157 TRACE("custom action (%x) started\n", GetCurrentThreadId() );
1159 rc = ACTION_CallScript( guid );
1161 TRACE("custom action (%x) returned %i\n", GetCurrentThreadId(), rc );
1163 MsiCloseAllHandles();
1164 return rc;
1167 static msi_custom_action_info *do_msidbCustomActionTypeScript(
1168 MSIPACKAGE *package, INT type, LPCWSTR script, LPCWSTR function, LPCWSTR action )
1170 msi_custom_action_info *info;
1172 info = msi_alloc( sizeof *info );
1173 if (!info)
1174 return NULL;
1176 msiobj_addref( &package->hdr );
1177 info->package = package;
1178 info->type = type;
1179 info->target = strdupW( function );
1180 info->source = strdupW( script );
1181 info->action = strdupW( action );
1182 CoCreateGuid( &info->guid );
1184 EnterCriticalSection( &msi_custom_action_cs );
1185 list_add_tail( &msi_pending_custom_actions, &info->entry );
1186 LeaveCriticalSection( &msi_custom_action_cs );
1188 info->handle = CreateThread( NULL, 0, ScriptThread, &info->guid, 0, NULL );
1189 if (!info->handle)
1191 free_custom_action_data( info );
1192 return NULL;
1195 return info;
1198 static UINT HANDLE_CustomType37_38( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
1199 INT type, const WCHAR *action )
1201 msi_custom_action_info *info;
1203 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1205 info = do_msidbCustomActionTypeScript( package, type, target, NULL, action );
1206 return wait_thread_handle( info );
1209 static UINT HANDLE_CustomType5_6( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
1210 INT type, const WCHAR *action )
1212 static const WCHAR query[] = {
1213 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1214 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',' ',
1215 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
1216 MSIRECORD *row = NULL;
1217 msi_custom_action_info *info;
1218 CHAR *buffer = NULL;
1219 WCHAR *bufferw = NULL;
1220 DWORD sz = 0;
1221 UINT r;
1223 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1225 row = MSI_QueryGetRecord(package->db, query, source);
1226 if (!row)
1227 return ERROR_FUNCTION_FAILED;
1229 r = MSI_RecordReadStream(row, 2, NULL, &sz);
1230 if (r != ERROR_SUCCESS) goto done;
1232 buffer = msi_alloc( sz + 1 );
1233 if (!buffer)
1235 r = ERROR_FUNCTION_FAILED;
1236 goto done;
1239 r = MSI_RecordReadStream(row, 2, buffer, &sz);
1240 if (r != ERROR_SUCCESS)
1241 goto done;
1243 buffer[sz] = 0;
1244 bufferw = strdupAtoW(buffer);
1245 if (!bufferw)
1247 r = ERROR_FUNCTION_FAILED;
1248 goto done;
1251 info = do_msidbCustomActionTypeScript( package, type, bufferw, target, action );
1252 r = wait_thread_handle( info );
1254 done:
1255 msi_free(bufferw);
1256 msi_free(buffer);
1257 msiobj_release(&row->hdr);
1258 return r;
1261 static UINT HANDLE_CustomType21_22( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
1262 INT type, const WCHAR *action )
1264 msi_custom_action_info *info;
1265 MSIFILE *file;
1266 HANDLE hFile;
1267 DWORD sz, szHighWord = 0, read;
1268 CHAR *buffer=NULL;
1269 WCHAR *bufferw=NULL;
1270 BOOL bRet;
1271 UINT r;
1273 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1275 file = msi_get_loaded_file(package, source);
1276 if (!file)
1278 ERR("invalid file key %s\n", debugstr_w(source));
1279 return ERROR_FUNCTION_FAILED;
1282 hFile = msi_create_file( package, file->TargetPath, GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, 0 );
1283 if (hFile == INVALID_HANDLE_VALUE) return ERROR_FUNCTION_FAILED;
1285 sz = GetFileSize(hFile, &szHighWord);
1286 if (sz == INVALID_FILE_SIZE || szHighWord != 0)
1288 CloseHandle(hFile);
1289 return ERROR_FUNCTION_FAILED;
1291 buffer = msi_alloc( sz + 1 );
1292 if (!buffer)
1294 CloseHandle(hFile);
1295 return ERROR_FUNCTION_FAILED;
1297 bRet = ReadFile(hFile, buffer, sz, &read, NULL);
1298 CloseHandle(hFile);
1299 if (!bRet)
1301 r = ERROR_FUNCTION_FAILED;
1302 goto done;
1304 buffer[read] = 0;
1305 bufferw = strdupAtoW(buffer);
1306 if (!bufferw)
1308 r = ERROR_FUNCTION_FAILED;
1309 goto done;
1311 info = do_msidbCustomActionTypeScript( package, type, bufferw, target, action );
1312 r = wait_thread_handle( info );
1314 done:
1315 msi_free(bufferw);
1316 msi_free(buffer);
1317 return r;
1320 static UINT HANDLE_CustomType53_54( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
1321 INT type, const WCHAR *action )
1323 msi_custom_action_info *info;
1324 WCHAR *prop;
1326 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1328 prop = msi_dup_property( package->db, source );
1329 if (!prop) return ERROR_SUCCESS;
1331 info = do_msidbCustomActionTypeScript( package, type, prop, NULL, action );
1332 msi_free(prop);
1333 return wait_thread_handle( info );
1336 static BOOL action_type_matches_script( UINT type, UINT script )
1338 switch (script)
1340 case SCRIPT_NONE:
1341 return FALSE;
1342 case SCRIPT_INSTALL:
1343 return !(type & msidbCustomActionTypeCommit) && !(type & msidbCustomActionTypeRollback);
1344 case SCRIPT_COMMIT:
1345 return (type & msidbCustomActionTypeCommit);
1346 case SCRIPT_ROLLBACK:
1347 return (type & msidbCustomActionTypeRollback);
1348 default:
1349 ERR("unhandled script %u\n", script);
1351 return FALSE;
1354 static UINT defer_custom_action( MSIPACKAGE *package, const WCHAR *action, UINT type )
1356 WCHAR *actiondata = msi_dup_property( package->db, action );
1357 WCHAR *usersid = msi_dup_property( package->db, szUserSID );
1358 WCHAR *prodcode = msi_dup_property( package->db, szProductCode );
1359 WCHAR *deferred = msi_get_deferred_action( action, actiondata, usersid, prodcode );
1361 if (!deferred)
1363 msi_free( actiondata );
1364 msi_free( usersid );
1365 msi_free( prodcode );
1366 return ERROR_OUTOFMEMORY;
1368 if (type & msidbCustomActionTypeCommit)
1370 TRACE("deferring commit action\n");
1371 msi_schedule_action( package, SCRIPT_COMMIT, deferred );
1373 else if (type & msidbCustomActionTypeRollback)
1375 TRACE("deferring rollback action\n");
1376 msi_schedule_action( package, SCRIPT_ROLLBACK, deferred );
1378 else
1380 TRACE("deferring install action\n");
1381 msi_schedule_action( package, SCRIPT_INSTALL, deferred );
1384 msi_free( actiondata );
1385 msi_free( usersid );
1386 msi_free( prodcode );
1387 msi_free( deferred );
1388 return ERROR_SUCCESS;
1391 UINT ACTION_CustomAction(MSIPACKAGE *package, const WCHAR *action)
1393 static const WCHAR query[] = {
1394 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1395 '`','C','u','s','t','o','m','A','c','t','i','o','n','`',' ','W','H','E','R','E',' ',
1396 '`','A','c','t','i' ,'o','n','`',' ','=',' ','\'','%','s','\'',0};
1397 UINT rc = ERROR_SUCCESS;
1398 MSIRECORD *row;
1399 UINT type;
1400 const WCHAR *source, *target, *ptr, *deferred_data = NULL;
1401 WCHAR *deformated = NULL;
1402 int len;
1404 /* deferred action: [properties]Action */
1405 if ((ptr = wcsrchr(action, ']')))
1407 deferred_data = action;
1408 action = ptr + 1;
1411 row = MSI_QueryGetRecord( package->db, query, action );
1412 if (!row)
1413 return ERROR_FUNCTION_NOT_CALLED;
1415 type = MSI_RecordGetInteger(row,2);
1416 source = MSI_RecordGetString(row,3);
1417 target = MSI_RecordGetString(row,4);
1419 TRACE("Handling custom action %s (%x %s %s)\n",debugstr_w(action),type,
1420 debugstr_w(source), debugstr_w(target));
1422 /* handle some of the deferred actions */
1423 if (type & msidbCustomActionTypeTSAware)
1424 FIXME("msidbCustomActionTypeTSAware not handled\n");
1426 if (type & msidbCustomActionTypeInScript)
1428 if (type & msidbCustomActionTypeNoImpersonate)
1429 WARN("msidbCustomActionTypeNoImpersonate not handled\n");
1431 if (!action_type_matches_script(type, package->script))
1433 rc = defer_custom_action( package, action, type );
1434 goto end;
1436 else
1438 LPWSTR actiondata = msi_dup_property( package->db, action );
1440 if (type & msidbCustomActionTypeInScript)
1441 package->scheduled_action_running = TRUE;
1443 if (type & msidbCustomActionTypeCommit)
1444 package->commit_action_running = TRUE;
1446 if (type & msidbCustomActionTypeRollback)
1447 package->rollback_action_running = TRUE;
1449 if (deferred_data)
1450 set_deferred_action_props(package, deferred_data);
1451 else if (actiondata)
1452 msi_set_property( package->db, szCustomActionData, actiondata, -1 );
1453 else
1454 msi_set_property( package->db, szCustomActionData, szEmpty, -1 );
1456 msi_free(actiondata);
1459 else if (!check_execution_scheduling_options(package,action,type))
1461 rc = ERROR_SUCCESS;
1462 goto end;
1465 switch (type & CUSTOM_ACTION_TYPE_MASK)
1467 case 1: /* DLL file stored in a Binary table stream */
1468 rc = HANDLE_CustomType1( package, source, target, type, action );
1469 break;
1470 case 2: /* EXE file stored in a Binary table stream */
1471 rc = HANDLE_CustomType2( package, source, target, type, action );
1472 break;
1473 case 5:
1474 case 6: /* JScript/VBScript file stored in a Binary table stream */
1475 rc = HANDLE_CustomType5_6( package, source, target, type, action );
1476 break;
1477 case 7: /* Concurrent install from substorage */
1478 deformat_string( package, target, &deformated );
1479 rc = HANDLE_CustomType7( package, source, target, type, action );
1480 msi_free( deformated );
1481 break;
1482 case 17:
1483 rc = HANDLE_CustomType17( package, source, target, type, action );
1484 break;
1485 case 18: /* EXE file installed with package */
1486 rc = HANDLE_CustomType18( package, source, target, type, action );
1487 break;
1488 case 19: /* Error that halts install */
1489 rc = HANDLE_CustomType19( package, source, target, type, action );
1490 break;
1491 case 21: /* JScript/VBScript file installed with the product */
1492 case 22:
1493 rc = HANDLE_CustomType21_22( package, source, target, type, action );
1494 break;
1495 case 23: /* Installs another package in the source tree */
1496 deformat_string( package, target, &deformated );
1497 rc = HANDLE_CustomType23( package, source, deformated, type, action );
1498 msi_free( deformated );
1499 break;
1500 case 34: /* EXE to be run in specified directory */
1501 rc = HANDLE_CustomType34( package, source, target, type, action );
1502 break;
1503 case 35: /* Directory set with formatted text */
1504 deformat_string( package, target, &deformated );
1505 MSI_SetTargetPathW( package, source, deformated );
1506 msi_free( deformated );
1507 break;
1508 case 37: /* JScript/VBScript text stored in target column */
1509 case 38:
1510 rc = HANDLE_CustomType37_38( package, source, target, type, action );
1511 break;
1512 case 50: /* EXE file specified by a property value */
1513 rc = HANDLE_CustomType50( package, source, target, type, action );
1514 break;
1515 case 51: /* Property set with formatted text */
1516 if (!source) break;
1517 len = deformat_string( package, target, &deformated );
1518 rc = msi_set_property( package->db, source, deformated, len );
1519 if (rc == ERROR_SUCCESS && !wcscmp( source, szSourceDir )) msi_reset_source_folders( package );
1520 msi_free( deformated );
1521 break;
1522 case 53: /* JScript/VBScript text specified by a property value */
1523 case 54:
1524 rc = HANDLE_CustomType53_54( package, source, target, type, action );
1525 break;
1526 default:
1527 FIXME( "unhandled action type %u (%s %s)\n", type & CUSTOM_ACTION_TYPE_MASK, debugstr_w(source),
1528 debugstr_w(target) );
1531 end:
1532 package->scheduled_action_running = FALSE;
1533 package->commit_action_running = FALSE;
1534 package->rollback_action_running = FALSE;
1535 msiobj_release(&row->hdr);
1536 return rc;
1539 void ACTION_FinishCustomActions(const MSIPACKAGE* package)
1541 struct list *item;
1542 HANDLE *wait_handles;
1543 unsigned int handle_count, i;
1544 msi_custom_action_info *info, *cursor;
1546 while ((item = list_head( &package->RunningActions )))
1548 MSIRUNNINGACTION *action = LIST_ENTRY( item, MSIRUNNINGACTION, entry );
1550 list_remove( &action->entry );
1552 TRACE("waiting for %s\n", debugstr_w( action->name ) );
1553 msi_dialog_check_messages( action->handle );
1555 CloseHandle( action->handle );
1556 msi_free( action->name );
1557 msi_free( action );
1560 EnterCriticalSection( &msi_custom_action_cs );
1562 handle_count = list_count( &msi_pending_custom_actions );
1563 wait_handles = msi_alloc( handle_count * sizeof(HANDLE) );
1565 handle_count = 0;
1566 LIST_FOR_EACH_ENTRY_SAFE( info, cursor, &msi_pending_custom_actions, msi_custom_action_info, entry )
1568 if (info->package == package )
1570 if (DuplicateHandle(GetCurrentProcess(), info->handle, GetCurrentProcess(), &wait_handles[handle_count], SYNCHRONIZE, FALSE, 0))
1571 handle_count++;
1575 LeaveCriticalSection( &msi_custom_action_cs );
1577 for (i = 0; i < handle_count; i++)
1579 msi_dialog_check_messages( wait_handles[i] );
1580 CloseHandle( wait_handles[i] );
1582 msi_free( wait_handles );
1584 EnterCriticalSection( &msi_custom_action_cs );
1585 LIST_FOR_EACH_ENTRY_SAFE( info, cursor, &msi_pending_custom_actions, msi_custom_action_info, entry )
1587 if (info->package == package)
1588 free_custom_action_data( info );
1590 LeaveCriticalSection( &msi_custom_action_cs );
1593 UINT __cdecl s_remote_GetActionInfo(const GUID *guid, int *type, LPWSTR *dll, LPSTR *func, MSIHANDLE *hinst)
1595 msi_custom_action_info *info;
1597 info = find_action_by_guid(guid);
1598 if (!info)
1599 return ERROR_INVALID_DATA;
1601 *type = info->type;
1602 *hinst = alloc_msihandle(&info->package->hdr);
1603 *dll = strdupW(info->source);
1604 *func = strdupWtoA(info->target);
1606 return ERROR_SUCCESS;