vbscript: Handle index read access to array properties.
[wine.git] / dlls / msi / custom.c
blob8ff3d90fa580b6305d8b22601aa1c652b86e92a7
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>
26 #include "ntstatus.h"
27 #define WIN32_NO_STATUS
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winerror.h"
31 #include "msidefs.h"
32 #include "winuser.h"
33 #include "objbase.h"
34 #include "oleauto.h"
36 #include "msipriv.h"
37 #include "winemsi.h"
38 #include "wine/asm.h"
39 #include "wine/debug.h"
40 #include "wine/exception.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(msi);
44 #define CUSTOM_ACTION_TYPE_MASK 0x3F
46 typedef struct tagMSIRUNNINGACTION
48 struct list entry;
49 HANDLE handle;
50 BOOL process;
51 LPWSTR name;
52 } MSIRUNNINGACTION;
54 typedef UINT (WINAPI *MsiCustomActionEntryPoint)( MSIHANDLE );
56 static CRITICAL_SECTION msi_custom_action_cs;
57 static CRITICAL_SECTION_DEBUG msi_custom_action_cs_debug =
59 0, 0, &msi_custom_action_cs,
60 { &msi_custom_action_cs_debug.ProcessLocksList,
61 &msi_custom_action_cs_debug.ProcessLocksList },
62 0, 0, { (DWORD_PTR)(__FILE__ ": msi_custom_action_cs") }
64 static CRITICAL_SECTION msi_custom_action_cs = { &msi_custom_action_cs_debug, -1, 0, 0, 0, 0 };
66 static struct list msi_pending_custom_actions = LIST_INIT( msi_pending_custom_actions );
68 void __RPC_FAR * __RPC_USER MIDL_user_allocate(SIZE_T len)
70 return malloc(len);
73 void __RPC_USER MIDL_user_free(void __RPC_FAR * ptr)
75 free(ptr);
78 LONG WINAPI rpc_filter(EXCEPTION_POINTERS *eptr)
80 return I_RpcExceptionFilter(eptr->ExceptionRecord->ExceptionCode);
83 UINT msi_schedule_action( MSIPACKAGE *package, UINT script, const WCHAR *action )
85 UINT count;
86 WCHAR **newbuf = NULL;
88 if (script >= SCRIPT_MAX)
90 FIXME("Unknown script requested %u\n", script);
91 return ERROR_FUNCTION_FAILED;
93 TRACE("Scheduling action %s in script %u\n", debugstr_w(action), script);
95 count = package->script_actions_count[script];
96 package->script_actions_count[script]++;
97 if (count != 0) newbuf = msi_realloc( package->script_actions[script],
98 package->script_actions_count[script] * sizeof(WCHAR *) );
99 else newbuf = msi_alloc( sizeof(WCHAR *) );
101 newbuf[count] = strdupW( action );
102 package->script_actions[script] = newbuf;
103 return ERROR_SUCCESS;
106 UINT msi_register_unique_action( MSIPACKAGE *package, const WCHAR *action )
108 UINT count;
109 WCHAR **newbuf = NULL;
111 TRACE("Registering %s as unique action\n", debugstr_w(action));
113 count = package->unique_actions_count;
114 package->unique_actions_count++;
115 if (count != 0) newbuf = msi_realloc( package->unique_actions,
116 package->unique_actions_count * sizeof(WCHAR *) );
117 else newbuf = msi_alloc( sizeof(WCHAR *) );
119 newbuf[count] = strdupW( action );
120 package->unique_actions = newbuf;
121 return ERROR_SUCCESS;
124 BOOL msi_action_is_unique( const MSIPACKAGE *package, const WCHAR *action )
126 UINT i;
128 for (i = 0; i < package->unique_actions_count; i++)
130 if (!wcscmp( package->unique_actions[i], action )) return TRUE;
132 return FALSE;
135 static BOOL check_execution_scheduling_options(MSIPACKAGE *package, LPCWSTR action, UINT options)
137 if ((options & msidbCustomActionTypeClientRepeat) ==
138 msidbCustomActionTypeClientRepeat)
140 if (!(package->InWhatSequence & SEQUENCE_UI &&
141 package->InWhatSequence & SEQUENCE_EXEC))
143 TRACE("Skipping action due to dbCustomActionTypeClientRepeat option.\n");
144 return FALSE;
147 else if (options & msidbCustomActionTypeFirstSequence)
149 if (package->InWhatSequence & SEQUENCE_UI &&
150 package->InWhatSequence & SEQUENCE_EXEC )
152 TRACE("Skipping action due to msidbCustomActionTypeFirstSequence option.\n");
153 return FALSE;
156 else if (options & msidbCustomActionTypeOncePerProcess)
158 if (msi_action_is_unique(package, action))
160 TRACE("Skipping action due to msidbCustomActionTypeOncePerProcess option.\n");
161 return FALSE;
163 else
164 msi_register_unique_action(package, action);
167 return TRUE;
170 /* stores the following properties before the action:
172 * [CustomActionData<=>UserSID<=>ProductCode]Action
174 static LPWSTR msi_get_deferred_action(LPCWSTR action, LPCWSTR actiondata,
175 LPCWSTR usersid, LPCWSTR prodcode)
177 LPWSTR deferred;
178 DWORD len;
180 if (!actiondata)
181 return strdupW(action);
183 len = lstrlenW(action) + lstrlenW(actiondata) +
184 lstrlenW(usersid) + lstrlenW(prodcode) +
185 lstrlenW(L"[%s<=>%s<=>%s]%s") - 7;
186 deferred = msi_alloc(len * sizeof(WCHAR));
188 swprintf(deferred, len, L"[%s<=>%s<=>%s]%s", actiondata, usersid, prodcode, action);
189 return deferred;
192 static void set_deferred_action_props( MSIPACKAGE *package, const WCHAR *deferred_data )
194 const WCHAR *end, *beg = deferred_data + 1;
196 end = wcsstr(beg, L"<=>");
197 msi_set_property( package->db, L"CustomActionData", beg, end - beg );
198 beg = end + 3;
200 end = wcsstr(beg, L"<=>");
201 msi_set_property( package->db, L"UserSID", beg, end - beg );
202 beg = end + 3;
204 end = wcschr(beg, ']');
205 msi_set_property( package->db, L"ProductCode", beg, end - beg );
208 WCHAR *msi_create_temp_file( MSIDATABASE *db )
210 WCHAR *ret;
212 if (!db->tempfolder)
214 WCHAR tmp[MAX_PATH];
215 DWORD len = ARRAY_SIZE( tmp );
217 if (msi_get_property( db, L"TempFolder", tmp, &len ) ||
218 GetFileAttributesW( tmp ) != FILE_ATTRIBUTE_DIRECTORY)
220 GetTempPathW( MAX_PATH, tmp );
222 if (!(db->tempfolder = strdupW( tmp ))) return NULL;
225 if ((ret = msi_alloc( (lstrlenW( db->tempfolder ) + 20) * sizeof(WCHAR) )))
227 if (!GetTempFileNameW( db->tempfolder, L"msi", 0, ret ))
229 msi_free( ret );
230 return NULL;
234 return ret;
237 static MSIBINARY *create_temp_binary(MSIPACKAGE *package, LPCWSTR source)
239 MSIRECORD *row;
240 MSIBINARY *binary = NULL;
241 HANDLE file;
242 CHAR buffer[1024];
243 WCHAR *tmpfile;
244 DWORD sz, write;
245 UINT r;
247 if (!(tmpfile = msi_create_temp_file( package->db ))) return NULL;
249 if (!(row = MSI_QueryGetRecord( package->db, L"SELECT * FROM `Binary` WHERE `Name` = '%s'", source ))) goto error;
250 if (!(binary = msi_alloc_zero( sizeof(MSIBINARY) ))) goto error;
252 file = CreateFileW( tmpfile, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
253 if (file == INVALID_HANDLE_VALUE) goto error;
257 sz = sizeof(buffer);
258 r = MSI_RecordReadStream( row, 2, buffer, &sz );
259 if (r != ERROR_SUCCESS)
261 ERR("Failed to get stream\n");
262 break;
264 WriteFile( file, buffer, sz, &write, NULL );
265 } while (sz == sizeof buffer);
267 CloseHandle( file );
268 if (r != ERROR_SUCCESS) goto error;
270 binary->source = strdupW( source );
271 binary->tmpfile = tmpfile;
272 list_add_tail( &package->binaries, &binary->entry );
274 msiobj_release( &row->hdr );
275 return binary;
277 error:
278 if (row) msiobj_release( &row->hdr );
279 DeleteFileW( tmpfile );
280 msi_free( tmpfile );
281 msi_free( binary );
282 return NULL;
285 static MSIBINARY *get_temp_binary(MSIPACKAGE *package, LPCWSTR source)
287 MSIBINARY *binary;
289 LIST_FOR_EACH_ENTRY( binary, &package->binaries, MSIBINARY, entry )
291 if (!wcscmp( binary->source, source ))
292 return binary;
295 return create_temp_binary(package, source);
298 static void file_running_action(MSIPACKAGE* package, HANDLE Handle,
299 BOOL process, LPCWSTR name)
301 MSIRUNNINGACTION *action;
303 action = msi_alloc( sizeof(MSIRUNNINGACTION) );
305 action->handle = Handle;
306 action->process = process;
307 action->name = strdupW(name);
309 list_add_tail( &package->RunningActions, &action->entry );
312 static UINT custom_get_process_return( HANDLE process )
314 DWORD rc = 0;
316 GetExitCodeProcess( process, &rc );
317 TRACE( "exit code is %lu\n", rc );
318 if (rc != 0)
319 return ERROR_FUNCTION_FAILED;
320 return ERROR_SUCCESS;
323 static UINT custom_get_thread_return( MSIPACKAGE *package, HANDLE thread )
325 DWORD rc = 0;
327 GetExitCodeThread( thread, &rc );
329 switch (rc)
331 case ERROR_FUNCTION_NOT_CALLED:
332 case ERROR_SUCCESS:
333 case ERROR_INSTALL_USEREXIT:
334 case ERROR_INSTALL_FAILURE:
335 return rc;
336 case ERROR_NO_MORE_ITEMS:
337 return ERROR_SUCCESS;
338 case ERROR_INSTALL_SUSPEND:
339 ACTION_ForceReboot( package );
340 return ERROR_SUCCESS;
341 default:
342 ERR( "invalid Return Code %lu\n", rc );
343 return ERROR_INSTALL_FAILURE;
347 static UINT wait_process_handle(MSIPACKAGE* package, UINT type,
348 HANDLE ProcessHandle, LPCWSTR name)
350 UINT rc = ERROR_SUCCESS;
352 if (!(type & msidbCustomActionTypeAsync))
354 TRACE("waiting for %s\n", debugstr_w(name));
356 msi_dialog_check_messages(ProcessHandle);
358 if (!(type & msidbCustomActionTypeContinue))
359 rc = custom_get_process_return(ProcessHandle);
361 CloseHandle(ProcessHandle);
363 else
365 TRACE("%s running in background\n", debugstr_w(name));
367 if (!(type & msidbCustomActionTypeContinue))
368 file_running_action(package, ProcessHandle, TRUE, name);
369 else
370 CloseHandle(ProcessHandle);
373 return rc;
376 typedef struct _msi_custom_action_info {
377 struct list entry;
378 MSIPACKAGE *package;
379 LPWSTR source;
380 LPWSTR target;
381 HANDLE handle;
382 LPWSTR action;
383 INT type;
384 GUID guid;
385 DWORD arch;
386 } msi_custom_action_info;
388 static void free_custom_action_data( msi_custom_action_info *info )
390 EnterCriticalSection( &msi_custom_action_cs );
392 list_remove( &info->entry );
393 if (info->handle)
394 CloseHandle( info->handle );
395 msi_free( info->action );
396 msi_free( info->source );
397 msi_free( info->target );
398 msiobj_release( &info->package->hdr );
399 msi_free( info );
401 LeaveCriticalSection( &msi_custom_action_cs );
404 static UINT wait_thread_handle( msi_custom_action_info *info )
406 UINT rc = ERROR_SUCCESS;
408 if (!(info->type & msidbCustomActionTypeAsync))
410 TRACE("waiting for %s\n", debugstr_w( info->action ));
412 msi_dialog_check_messages( info->handle );
414 if (!(info->type & msidbCustomActionTypeContinue))
415 rc = custom_get_thread_return( info->package, info->handle );
417 free_custom_action_data( info );
419 else
421 TRACE("%s running in background\n", debugstr_w( info->action ));
424 return rc;
427 static msi_custom_action_info *find_action_by_guid( const GUID *guid )
429 msi_custom_action_info *info;
430 BOOL found = FALSE;
432 EnterCriticalSection( &msi_custom_action_cs );
434 LIST_FOR_EACH_ENTRY( info, &msi_pending_custom_actions, msi_custom_action_info, entry )
436 if (IsEqualGUID( &info->guid, guid ))
438 found = TRUE;
439 break;
443 LeaveCriticalSection( &msi_custom_action_cs );
445 if (!found)
446 return NULL;
448 return info;
451 static void handle_msi_break( const WCHAR *action )
453 const WCHAR fmt[] = L"To debug your custom action, attach your debugger to process %u (0x%x) and press OK";
454 WCHAR val[MAX_PATH], msg[100];
456 if (!GetEnvironmentVariableW( L"MsiBreak", val, MAX_PATH ) || wcscmp( val, action )) return;
458 swprintf( msg, ARRAY_SIZE(msg), fmt, GetCurrentProcessId(), GetCurrentProcessId() );
459 MessageBoxW( NULL, msg, L"Windows Installer", MB_OK );
460 DebugBreak();
463 #ifdef __i386__
464 /* wrapper for apps that don't declare the thread function correctly */
465 extern UINT custom_proc_wrapper( MsiCustomActionEntryPoint entry, MSIHANDLE hinst );
466 __ASM_GLOBAL_FUNC(custom_proc_wrapper,
467 "pushl %ebp\n\t"
468 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
469 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
470 "movl %esp,%ebp\n\t"
471 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
472 "subl $4,%esp\n\t"
473 "pushl 12(%ebp)\n\t"
474 "call *8(%ebp)\n\t"
475 "leave\n\t"
476 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
477 __ASM_CFI(".cfi_same_value %ebp\n\t")
478 "ret" )
479 #else
480 static UINT custom_proc_wrapper( MsiCustomActionEntryPoint entry, MSIHANDLE hinst )
482 return entry(hinst);
484 #endif
486 UINT CDECL __wine_msi_call_dll_function(DWORD client_pid, const GUID *guid)
488 MsiCustomActionEntryPoint fn;
489 MSIHANDLE remote_package = 0;
490 RPC_WSTR binding_str;
491 MSIHANDLE hPackage;
492 RPC_STATUS status;
493 WCHAR *dll = NULL, *action = NULL;
494 LPSTR proc = NULL;
495 HANDLE hModule;
496 INT type;
497 UINT r;
499 TRACE("%s\n", debugstr_guid( guid ));
501 if (!rpc_handle)
503 WCHAR endpoint[12];
505 swprintf(endpoint, ARRAY_SIZE(endpoint), L"msi%x", client_pid);
506 status = RpcStringBindingComposeW(NULL, (WCHAR *)L"ncalrpc", NULL, endpoint, NULL, &binding_str);
507 if (status != RPC_S_OK)
509 ERR("RpcStringBindingCompose failed: %#lx\n", status);
510 return status;
512 status = RpcBindingFromStringBindingW(binding_str, &rpc_handle);
513 if (status != RPC_S_OK)
515 ERR("RpcBindingFromStringBinding failed: %#lx\n", status);
516 return status;
518 RpcStringFreeW(&binding_str);
521 r = remote_GetActionInfo(guid, &action, &type, &dll, &proc, &remote_package);
522 if (r != ERROR_SUCCESS)
523 return r;
525 hPackage = alloc_msi_remote_handle( remote_package );
526 if (!hPackage)
528 ERR( "failed to create handle for %#lx\n", remote_package );
529 midl_user_free( action );
530 midl_user_free( dll );
531 midl_user_free( proc );
532 return ERROR_INSTALL_FAILURE;
535 hModule = LoadLibraryW( dll );
536 if (!hModule)
538 ERR( "failed to load dll %s (%lu)\n", debugstr_w( dll ), GetLastError() );
539 midl_user_free( action );
540 midl_user_free( dll );
541 midl_user_free( proc );
542 MsiCloseHandle( hPackage );
543 return ERROR_SUCCESS;
546 fn = (MsiCustomActionEntryPoint) GetProcAddress( hModule, proc );
547 if (!fn) WARN( "GetProcAddress(%s) failed\n", debugstr_a(proc) );
548 else
550 handle_msi_break(action);
552 __TRY
554 r = custom_proc_wrapper( fn, hPackage );
556 __EXCEPT_PAGE_FAULT
558 ERR( "Custom action (%s:%s) caused a page fault: %#lx\n",
559 debugstr_w(dll), debugstr_a(proc), GetExceptionCode() );
560 r = ERROR_SUCCESS;
562 __ENDTRY;
565 FreeLibrary(hModule);
567 midl_user_free(action);
568 midl_user_free(dll);
569 midl_user_free(proc);
571 MsiCloseAllHandles();
572 return r;
575 static DWORD custom_start_server(MSIPACKAGE *package, DWORD arch)
577 WCHAR path[MAX_PATH], cmdline[MAX_PATH + 23];
578 PROCESS_INFORMATION pi = {0};
579 STARTUPINFOW si = {0};
580 WCHAR buffer[24];
581 void *cookie;
582 HANDLE pipe;
584 if ((arch == SCS_32BIT_BINARY && package->custom_server_32_process) ||
585 (arch == SCS_64BIT_BINARY && package->custom_server_64_process))
586 return ERROR_SUCCESS;
588 swprintf(buffer, ARRAY_SIZE(buffer), L"\\\\.\\pipe\\msica_%x_%d",
589 GetCurrentProcessId(), arch == SCS_32BIT_BINARY ? 32 : 64);
590 pipe = CreateNamedPipeW(buffer, PIPE_ACCESS_DUPLEX, 0, 1, sizeof(DWORD64),
591 sizeof(GUID), 0, NULL);
592 if (pipe == INVALID_HANDLE_VALUE)
593 ERR("failed to create custom action client pipe: %lu\n", GetLastError());
595 if ((sizeof(void *) == 8 || is_wow64) && arch == SCS_32BIT_BINARY)
596 GetSystemWow64DirectoryW(path, MAX_PATH - ARRAY_SIZE(L"\\msiexec.exe"));
597 else
598 GetSystemDirectoryW(path, MAX_PATH - ARRAY_SIZE(L"\\msiexec.exe"));
599 lstrcatW(path, L"\\msiexec.exe");
600 swprintf(cmdline, ARRAY_SIZE(cmdline), L"%s -Embedding %d", path, GetCurrentProcessId());
602 if (is_wow64 && arch == SCS_64BIT_BINARY)
604 Wow64DisableWow64FsRedirection(&cookie);
605 CreateProcessW(path, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
606 Wow64RevertWow64FsRedirection(cookie);
608 else
609 CreateProcessW(path, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
611 CloseHandle(pi.hThread);
613 if (arch == SCS_32BIT_BINARY)
615 package->custom_server_32_process = pi.hProcess;
616 package->custom_server_32_pipe = pipe;
618 else
620 package->custom_server_64_process = pi.hProcess;
621 package->custom_server_64_pipe = pipe;
624 if (!ConnectNamedPipe(pipe, NULL))
626 ERR("failed to connect to custom action server: %lu\n", GetLastError());
627 return GetLastError();
630 return ERROR_SUCCESS;
633 void custom_stop_server(HANDLE process, HANDLE pipe)
635 DWORD size;
637 WriteFile(pipe, &GUID_NULL, sizeof(GUID_NULL), &size, NULL);
638 WaitForSingleObject(process, INFINITE);
639 CloseHandle(process);
640 CloseHandle(pipe);
643 static DWORD WINAPI custom_client_thread(void *arg)
645 msi_custom_action_info *info = arg;
646 DWORD64 thread64;
647 HANDLE process;
648 HANDLE thread;
649 HANDLE pipe;
650 DWORD size;
651 DWORD rc;
653 CoInitializeEx(NULL, COINIT_MULTITHREADED); /* needed to marshal streams */
655 if (info->arch == SCS_32BIT_BINARY)
657 process = info->package->custom_server_32_process;
658 pipe = info->package->custom_server_32_pipe;
660 else
662 process = info->package->custom_server_64_process;
663 pipe = info->package->custom_server_64_pipe;
666 EnterCriticalSection(&msi_custom_action_cs);
668 if (!WriteFile(pipe, &info->guid, sizeof(info->guid), &size, NULL) ||
669 size != sizeof(info->guid))
671 ERR("failed to write to custom action client pipe: %lu\n", GetLastError());
672 LeaveCriticalSection(&msi_custom_action_cs);
673 return GetLastError();
675 if (!ReadFile(pipe, &thread64, sizeof(thread64), &size, NULL) || size != sizeof(thread64))
677 ERR("failed to read from custom action client pipe: %lu\n", GetLastError());
678 LeaveCriticalSection(&msi_custom_action_cs);
679 return GetLastError();
682 LeaveCriticalSection(&msi_custom_action_cs);
684 if (DuplicateHandle(process, (HANDLE)(DWORD_PTR)thread64, GetCurrentProcess(),
685 &thread, 0, FALSE, DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE))
687 WaitForSingleObject(thread, INFINITE);
688 GetExitCodeThread(thread, &rc);
689 CloseHandle(thread);
691 else
692 rc = GetLastError();
694 CoUninitialize();
695 return rc;
698 /* based on kernel32.GetBinaryTypeW() */
699 static BOOL get_binary_type( const WCHAR *name, DWORD *type )
701 HANDLE hfile, mapping;
702 NTSTATUS status;
704 hfile = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
705 if (hfile == INVALID_HANDLE_VALUE)
706 return FALSE;
708 status = NtCreateSection( &mapping, STANDARD_RIGHTS_REQUIRED | SECTION_QUERY, NULL, NULL, PAGE_READONLY,
709 SEC_IMAGE, hfile );
710 CloseHandle( hfile );
712 switch (status)
714 case STATUS_SUCCESS:
716 SECTION_IMAGE_INFORMATION info;
718 status = NtQuerySection( mapping, SectionImageInformation, &info, sizeof(info), NULL );
719 CloseHandle( mapping );
720 if (status) return FALSE;
721 switch (info.Machine)
723 case IMAGE_FILE_MACHINE_I386:
724 case IMAGE_FILE_MACHINE_ARMNT:
725 *type = SCS_32BIT_BINARY;
726 return TRUE;
727 case IMAGE_FILE_MACHINE_AMD64:
728 case IMAGE_FILE_MACHINE_ARM64:
729 *type = SCS_64BIT_BINARY;
730 return TRUE;
731 default:
732 return FALSE;
735 case STATUS_INVALID_IMAGE_WIN_64:
736 *type = SCS_64BIT_BINARY;
737 return TRUE;
738 default:
739 return FALSE;
743 static msi_custom_action_info *do_msidbCustomActionTypeDll(
744 MSIPACKAGE *package, INT type, LPCWSTR source, LPCWSTR target, LPCWSTR action )
746 msi_custom_action_info *info;
747 RPC_STATUS status;
748 BOOL ret;
750 info = msi_alloc( sizeof *info );
751 if (!info)
752 return NULL;
754 msiobj_addref( &package->hdr );
755 info->package = package;
756 info->type = type;
757 info->target = strdupW( target );
758 info->source = strdupW( source );
759 info->action = strdupW( action );
760 CoCreateGuid( &info->guid );
762 EnterCriticalSection( &msi_custom_action_cs );
763 list_add_tail( &msi_pending_custom_actions, &info->entry );
764 LeaveCriticalSection( &msi_custom_action_cs );
766 if (!package->rpc_server_started)
768 WCHAR endpoint[12];
770 swprintf(endpoint, ARRAY_SIZE(endpoint), L"msi%x", GetCurrentProcessId());
771 status = RpcServerUseProtseqEpW((WCHAR *)L"ncalrpc", RPC_C_PROTSEQ_MAX_REQS_DEFAULT,
772 endpoint, NULL);
773 if (status != RPC_S_OK)
775 ERR("RpcServerUseProtseqEp failed: %#lx\n", status);
776 return NULL;
779 status = RpcServerRegisterIfEx(s_IWineMsiRemote_v0_0_s_ifspec, NULL, NULL,
780 RPC_IF_AUTOLISTEN, RPC_C_LISTEN_MAX_CALLS_DEFAULT, NULL);
781 if (status != RPC_S_OK)
783 ERR("RpcServerRegisterIfEx failed: %#lx\n", status);
784 return NULL;
787 info->package->rpc_server_started = 1;
790 ret = get_binary_type(source, &info->arch);
791 if (!ret)
792 info->arch = (sizeof(void *) == 8 ? SCS_64BIT_BINARY : SCS_32BIT_BINARY);
794 if (info->arch == SCS_64BIT_BINARY && sizeof(void *) == 4 && !is_wow64)
796 ERR("Attempt to run a 64-bit custom action inside a 32-bit WINEPREFIX.\n");
797 free_custom_action_data( info );
798 return NULL;
801 custom_start_server(package, info->arch);
803 info->handle = CreateThread(NULL, 0, custom_client_thread, info, 0, NULL);
804 if (!info->handle)
806 free_custom_action_data( info );
807 return NULL;
810 return info;
813 static UINT HANDLE_CustomType1( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
814 INT type, const WCHAR *action )
816 msi_custom_action_info *info;
817 MSIBINARY *binary;
819 if (!(binary = get_temp_binary(package, source)))
820 return ERROR_FUNCTION_FAILED;
822 TRACE("Calling function %s from %s\n", debugstr_w(target), debugstr_w(binary->tmpfile));
824 if (!(info = do_msidbCustomActionTypeDll( package, type, binary->tmpfile, target, action )))
825 return ERROR_FUNCTION_FAILED;
826 return wait_thread_handle( info );
829 static HANDLE execute_command( const WCHAR *app, WCHAR *arg, const WCHAR *dir )
831 STARTUPINFOW si;
832 PROCESS_INFORMATION info;
833 WCHAR *exe = NULL, *cmd = NULL, *p;
834 BOOL ret;
836 if (app)
838 int len_arg = 0;
839 DWORD len_exe;
841 if (!(exe = msi_alloc( MAX_PATH * sizeof(WCHAR) ))) return INVALID_HANDLE_VALUE;
842 len_exe = SearchPathW( NULL, app, L".exe", MAX_PATH, exe, NULL );
843 if (len_exe >= MAX_PATH)
845 msi_free( exe );
846 if (!(exe = msi_alloc( len_exe * sizeof(WCHAR) ))) return INVALID_HANDLE_VALUE;
847 len_exe = SearchPathW( NULL, app, L".exe", len_exe, exe, NULL );
849 if (!len_exe)
851 ERR("can't find executable %lu\n", GetLastError());
852 msi_free( exe );
853 return INVALID_HANDLE_VALUE;
856 if (arg) len_arg = lstrlenW( arg );
857 if (!(cmd = msi_alloc( (len_exe + len_arg + 4) * sizeof(WCHAR) )))
859 msi_free( exe );
860 return INVALID_HANDLE_VALUE;
862 p = cmd;
863 if (wcschr( exe, ' ' ))
865 *p++ = '\"';
866 memcpy( p, exe, len_exe * sizeof(WCHAR) );
867 p += len_exe;
868 *p++ = '\"';
869 *p = 0;
871 else
873 lstrcpyW( p, exe );
874 p += len_exe;
876 if (arg)
878 *p++ = ' ';
879 memcpy( p, arg, len_arg * sizeof(WCHAR) );
880 p[len_arg] = 0;
883 memset( &si, 0, sizeof(STARTUPINFOW) );
884 ret = CreateProcessW( exe, exe ? cmd : arg, NULL, NULL, FALSE, 0, NULL, dir, &si, &info );
885 msi_free( cmd );
886 msi_free( exe );
887 if (!ret)
889 ERR("unable to execute command %lu\n", GetLastError());
890 return INVALID_HANDLE_VALUE;
892 CloseHandle( info.hThread );
893 return info.hProcess;
896 static UINT HANDLE_CustomType2( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
897 INT type, const WCHAR *action )
899 MSIBINARY *binary;
900 HANDLE handle;
901 WCHAR *arg;
903 if (!(binary = get_temp_binary(package, source)))
904 return ERROR_FUNCTION_FAILED;
906 deformat_string( package, target, &arg );
907 TRACE("exe %s arg %s\n", debugstr_w(binary->tmpfile), debugstr_w(arg));
909 handle = execute_command( binary->tmpfile, arg, L"C:\\" );
910 msi_free( arg );
911 if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
912 return wait_process_handle( package, type, handle, action );
915 static UINT HANDLE_CustomType17( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
916 INT type, const WCHAR *action )
918 msi_custom_action_info *info;
919 MSIFILE *file;
921 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
923 file = msi_get_loaded_file( package, source );
924 if (!file)
926 ERR("invalid file key %s\n", debugstr_w( source ));
927 return ERROR_FUNCTION_FAILED;
930 if (!(info = do_msidbCustomActionTypeDll( package, type, file->TargetPath, target, action )))
931 return ERROR_FUNCTION_FAILED;
932 return wait_thread_handle( info );
935 static UINT HANDLE_CustomType18( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
936 INT type, const WCHAR *action )
938 MSIFILE *file;
939 HANDLE handle;
940 WCHAR *arg;
942 if (!(file = msi_get_loaded_file( package, source ))) return ERROR_FUNCTION_FAILED;
944 deformat_string( package, target, &arg );
945 TRACE("exe %s arg %s\n", debugstr_w(file->TargetPath), debugstr_w(arg));
947 handle = execute_command( file->TargetPath, arg, L"C:\\" );
948 msi_free( arg );
949 if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
950 return wait_process_handle( package, type, handle, action );
953 static UINT HANDLE_CustomType19( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
954 INT type, const WCHAR *action )
956 MSIRECORD *row = 0;
957 LPWSTR deformated = NULL;
959 deformat_string( package, target, &deformated );
961 /* first try treat the error as a number */
962 row = MSI_QueryGetRecord( package->db, L"SELECT `Message` FROM `Error` WHERE `Error` = '%s'", deformated );
963 if( row )
965 LPCWSTR error = MSI_RecordGetString( row, 1 );
966 if ((package->ui_level & INSTALLUILEVEL_MASK) != INSTALLUILEVEL_NONE)
967 MessageBoxW( NULL, error, NULL, MB_OK );
968 msiobj_release( &row->hdr );
970 else if ((package->ui_level & INSTALLUILEVEL_MASK) != INSTALLUILEVEL_NONE)
971 MessageBoxW( NULL, deformated, NULL, MB_OK );
973 msi_free( deformated );
975 return ERROR_INSTALL_FAILURE;
978 static WCHAR *build_msiexec_args( const WCHAR *filename, const WCHAR *params )
980 UINT len_filename = lstrlenW( filename ), len_params = lstrlenW( params );
981 UINT len = ARRAY_SIZE(L"/qb /i ") - 1;
982 WCHAR *ret;
984 if (!(ret = msi_alloc( (len + len_filename + len_params + 4) * sizeof(WCHAR) ))) return NULL;
985 memcpy( ret, L"/qb /i ", sizeof(L"/qb /i ") );
986 ret[len++] = '"';
987 memcpy( ret + len, filename, len_filename * sizeof(WCHAR) );
988 len += len_filename;
989 ret[len++] = '"';
990 ret[len++] = ' ';
991 lstrcpyW( ret + len, params );
992 return ret;
995 static UINT HANDLE_CustomType23( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
996 INT type, const WCHAR *action )
998 WCHAR *dir, *filename, *args, *p;
999 UINT len_dir, len_source = lstrlenW( source );
1000 HANDLE handle;
1002 if (!(dir = msi_dup_property( package->db, L"OriginalDatabase" ))) return ERROR_OUTOFMEMORY;
1003 if (!(p = wcsrchr( dir, '\\' )) && !(p = wcsrchr( dir, '/' )))
1005 msi_free( dir );
1006 return ERROR_FUNCTION_FAILED;
1008 *p = 0;
1009 len_dir = p - dir;
1010 if (!(filename = msi_alloc( (len_dir + len_source + 2) * sizeof(WCHAR) )))
1012 msi_free( dir );
1013 return ERROR_OUTOFMEMORY;
1015 memcpy( filename, dir, len_dir * sizeof(WCHAR) );
1016 filename[len_dir++] = '\\';
1017 memcpy( filename + len_dir, source, len_source * sizeof(WCHAR) );
1018 filename[len_dir + len_source] = 0;
1020 if (!(args = build_msiexec_args( filename, target )))
1022 msi_free( dir );
1023 return ERROR_OUTOFMEMORY;
1026 TRACE("installing %s concurrently\n", debugstr_w(source));
1028 handle = execute_command( L"msiexec", args, dir );
1029 msi_free( dir );
1030 msi_free( args );
1031 if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
1032 return wait_process_handle( package, type, handle, action );
1035 static UINT write_substorage_to_file( MSIPACKAGE *package, const WCHAR *source, const WCHAR *filename )
1037 IStorage *src = NULL, *dst = NULL;
1038 UINT r = ERROR_FUNCTION_FAILED;
1039 HRESULT hr;
1041 hr = StgCreateDocfile( filename, STGM_CREATE|STGM_TRANSACTED|STGM_WRITE|STGM_SHARE_EXCLUSIVE, 0, &dst );
1042 if (FAILED( hr ))
1044 WARN( "can't open destination storage %s (%#lx)\n", debugstr_w(filename), hr );
1045 goto done;
1048 hr = IStorage_OpenStorage( package->db->storage, source, NULL, STGM_SHARE_EXCLUSIVE, NULL, 0, &src );
1049 if (FAILED( hr ))
1051 WARN( "can't open source storage %s (%#lx)\n", debugstr_w(source), hr );
1052 goto done;
1055 hr = IStorage_CopyTo( src, 0, NULL, NULL, dst );
1056 if (FAILED( hr ))
1058 ERR( "failed to copy storage %s (%#lx)\n", debugstr_w(source), hr );
1059 goto done;
1062 hr = IStorage_Commit( dst, 0 );
1063 if (FAILED( hr ))
1064 ERR( "failed to commit storage (%#lx)\n", hr );
1065 else
1066 r = ERROR_SUCCESS;
1068 done:
1069 if (src) IStorage_Release( src );
1070 if (dst) IStorage_Release( dst );
1071 return r;
1074 static UINT HANDLE_CustomType7( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
1075 INT type, const WCHAR *action )
1077 WCHAR *tmpfile, *args;
1078 MSIBINARY *binary = NULL;
1079 HANDLE handle;
1080 UINT r;
1082 if (!(tmpfile = msi_create_temp_file( package->db ))) return ERROR_FUNCTION_FAILED;
1084 r = write_substorage_to_file( package, source, tmpfile );
1085 if (r != ERROR_SUCCESS)
1086 goto error;
1088 if (!(binary = msi_alloc( sizeof(*binary) ))) goto error;
1089 binary->source = NULL;
1090 binary->tmpfile = tmpfile;
1091 list_add_tail( &package->binaries, &binary->entry );
1093 if (!(args = build_msiexec_args( tmpfile, target ))) return ERROR_OUTOFMEMORY;
1095 TRACE("installing %s concurrently\n", debugstr_w(source));
1097 handle = execute_command( L"msiexec", args, L"C:\\" );
1098 msi_free( args );
1099 if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
1100 return wait_process_handle( package, type, handle, action );
1102 error:
1103 DeleteFileW( tmpfile );
1104 msi_free( tmpfile );
1105 return ERROR_FUNCTION_FAILED;
1108 static UINT HANDLE_CustomType50( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
1109 INT type, const WCHAR *action )
1111 WCHAR *exe, *arg;
1112 HANDLE handle;
1114 if (!(exe = msi_dup_property( package->db, source ))) return ERROR_SUCCESS;
1116 deformat_string( package, target, &arg );
1117 TRACE("exe %s arg %s\n", debugstr_w(exe), debugstr_w(arg));
1119 handle = execute_command( exe, arg, L"C:\\" );
1120 msi_free( exe );
1121 msi_free( arg );
1122 if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
1123 return wait_process_handle( package, type, handle, action );
1126 static UINT HANDLE_CustomType34( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
1127 INT type, const WCHAR *action )
1129 const WCHAR *workingdir = NULL;
1130 HANDLE handle;
1131 WCHAR *cmd;
1133 if (source)
1135 workingdir = msi_get_target_folder( package, source );
1136 if (!workingdir) return ERROR_FUNCTION_FAILED;
1138 deformat_string( package, target, &cmd );
1139 if (!cmd) return ERROR_FUNCTION_FAILED;
1141 TRACE("cmd %s dir %s\n", debugstr_w(cmd), debugstr_w(workingdir));
1143 handle = execute_command( NULL, cmd, workingdir );
1144 msi_free( cmd );
1145 if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
1146 return wait_process_handle( package, type, handle, action );
1149 static DWORD ACTION_CallScript( const GUID *guid )
1151 msi_custom_action_info *info;
1152 MSIHANDLE hPackage;
1153 UINT r = ERROR_FUNCTION_FAILED;
1155 info = find_action_by_guid( guid );
1156 if (!info)
1158 ERR("failed to find action %s\n", debugstr_guid( guid) );
1159 return ERROR_FUNCTION_FAILED;
1162 TRACE("function %s, script %s\n", debugstr_w( info->target ), debugstr_w( info->source ) );
1164 hPackage = alloc_msihandle( &info->package->hdr );
1165 if (hPackage)
1167 r = call_script( hPackage, info->type, info->source, info->target, info->action );
1168 TRACE("script returned %u\n", r);
1169 MsiCloseHandle( hPackage );
1171 else
1172 ERR("failed to create handle for %p\n", info->package );
1174 return r;
1177 static DWORD WINAPI ScriptThread( LPVOID arg )
1179 LPGUID guid = arg;
1180 DWORD rc;
1182 TRACE("custom action (%#lx) started\n", GetCurrentThreadId() );
1184 rc = ACTION_CallScript( guid );
1186 TRACE("custom action (%#lx) returned %lu\n", GetCurrentThreadId(), rc );
1188 MsiCloseAllHandles();
1189 return rc;
1192 static msi_custom_action_info *do_msidbCustomActionTypeScript(
1193 MSIPACKAGE *package, INT type, LPCWSTR script, LPCWSTR function, LPCWSTR action )
1195 msi_custom_action_info *info;
1197 info = msi_alloc( sizeof *info );
1198 if (!info)
1199 return NULL;
1201 msiobj_addref( &package->hdr );
1202 info->package = package;
1203 info->type = type;
1204 info->target = strdupW( function );
1205 info->source = strdupW( script );
1206 info->action = strdupW( action );
1207 CoCreateGuid( &info->guid );
1209 EnterCriticalSection( &msi_custom_action_cs );
1210 list_add_tail( &msi_pending_custom_actions, &info->entry );
1211 LeaveCriticalSection( &msi_custom_action_cs );
1213 info->handle = CreateThread( NULL, 0, ScriptThread, &info->guid, 0, NULL );
1214 if (!info->handle)
1216 free_custom_action_data( info );
1217 return NULL;
1220 return info;
1223 static UINT HANDLE_CustomType37_38( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
1224 INT type, const WCHAR *action )
1226 msi_custom_action_info *info;
1228 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1230 info = do_msidbCustomActionTypeScript( package, type, target, NULL, action );
1231 return wait_thread_handle( info );
1234 static UINT HANDLE_CustomType5_6( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
1235 INT type, const WCHAR *action )
1237 MSIRECORD *row = NULL;
1238 msi_custom_action_info *info;
1239 CHAR *buffer = NULL;
1240 WCHAR *bufferw = NULL;
1241 DWORD sz = 0;
1242 UINT r;
1244 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1246 row = MSI_QueryGetRecord(package->db, L"SELECT * FROM `Binary` WHERE `Name` = '%s'", source);
1247 if (!row)
1248 return ERROR_FUNCTION_FAILED;
1250 r = MSI_RecordReadStream(row, 2, NULL, &sz);
1251 if (r != ERROR_SUCCESS) goto done;
1253 buffer = msi_alloc( sz + 1 );
1254 if (!buffer)
1256 r = ERROR_FUNCTION_FAILED;
1257 goto done;
1260 r = MSI_RecordReadStream(row, 2, buffer, &sz);
1261 if (r != ERROR_SUCCESS)
1262 goto done;
1264 buffer[sz] = 0;
1265 bufferw = strdupAtoW(buffer);
1266 if (!bufferw)
1268 r = ERROR_FUNCTION_FAILED;
1269 goto done;
1272 info = do_msidbCustomActionTypeScript( package, type, bufferw, target, action );
1273 r = wait_thread_handle( info );
1275 done:
1276 msi_free(bufferw);
1277 msi_free(buffer);
1278 msiobj_release(&row->hdr);
1279 return r;
1282 static UINT HANDLE_CustomType21_22( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
1283 INT type, const WCHAR *action )
1285 msi_custom_action_info *info;
1286 MSIFILE *file;
1287 HANDLE hFile;
1288 DWORD sz, szHighWord = 0, read;
1289 CHAR *buffer=NULL;
1290 WCHAR *bufferw=NULL;
1291 BOOL bRet;
1292 UINT r;
1294 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1296 file = msi_get_loaded_file(package, source);
1297 if (!file)
1299 ERR("invalid file key %s\n", debugstr_w(source));
1300 return ERROR_FUNCTION_FAILED;
1303 hFile = msi_create_file( package, file->TargetPath, GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, 0 );
1304 if (hFile == INVALID_HANDLE_VALUE) return ERROR_FUNCTION_FAILED;
1306 sz = GetFileSize(hFile, &szHighWord);
1307 if (sz == INVALID_FILE_SIZE || szHighWord != 0)
1309 CloseHandle(hFile);
1310 return ERROR_FUNCTION_FAILED;
1312 buffer = msi_alloc( sz + 1 );
1313 if (!buffer)
1315 CloseHandle(hFile);
1316 return ERROR_FUNCTION_FAILED;
1318 bRet = ReadFile(hFile, buffer, sz, &read, NULL);
1319 CloseHandle(hFile);
1320 if (!bRet)
1322 r = ERROR_FUNCTION_FAILED;
1323 goto done;
1325 buffer[read] = 0;
1326 bufferw = strdupAtoW(buffer);
1327 if (!bufferw)
1329 r = ERROR_FUNCTION_FAILED;
1330 goto done;
1332 info = do_msidbCustomActionTypeScript( package, type, bufferw, target, action );
1333 r = wait_thread_handle( info );
1335 done:
1336 msi_free(bufferw);
1337 msi_free(buffer);
1338 return r;
1341 static UINT HANDLE_CustomType53_54( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
1342 INT type, const WCHAR *action )
1344 msi_custom_action_info *info;
1345 WCHAR *prop;
1347 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1349 prop = msi_dup_property( package->db, source );
1350 if (!prop) return ERROR_SUCCESS;
1352 info = do_msidbCustomActionTypeScript( package, type, prop, NULL, action );
1353 msi_free(prop);
1354 return wait_thread_handle( info );
1357 static BOOL action_type_matches_script( UINT type, UINT script )
1359 switch (script)
1361 case SCRIPT_NONE:
1362 return FALSE;
1363 case SCRIPT_INSTALL:
1364 return !(type & msidbCustomActionTypeCommit) && !(type & msidbCustomActionTypeRollback);
1365 case SCRIPT_COMMIT:
1366 return (type & msidbCustomActionTypeCommit);
1367 case SCRIPT_ROLLBACK:
1368 return (type & msidbCustomActionTypeRollback);
1369 default:
1370 ERR("unhandled script %u\n", script);
1372 return FALSE;
1375 static UINT defer_custom_action( MSIPACKAGE *package, const WCHAR *action, UINT type )
1377 WCHAR *actiondata = msi_dup_property( package->db, action );
1378 WCHAR *usersid = msi_dup_property( package->db, L"UserSID" );
1379 WCHAR *prodcode = msi_dup_property( package->db, L"ProductCode" );
1380 WCHAR *deferred = msi_get_deferred_action( action, actiondata, usersid, prodcode );
1382 if (!deferred)
1384 msi_free( actiondata );
1385 msi_free( usersid );
1386 msi_free( prodcode );
1387 return ERROR_OUTOFMEMORY;
1389 if (type & msidbCustomActionTypeCommit)
1391 TRACE("deferring commit action\n");
1392 msi_schedule_action( package, SCRIPT_COMMIT, deferred );
1394 else if (type & msidbCustomActionTypeRollback)
1396 TRACE("deferring rollback action\n");
1397 msi_schedule_action( package, SCRIPT_ROLLBACK, deferred );
1399 else
1401 TRACE("deferring install action\n");
1402 msi_schedule_action( package, SCRIPT_INSTALL, deferred );
1405 msi_free( actiondata );
1406 msi_free( usersid );
1407 msi_free( prodcode );
1408 msi_free( deferred );
1409 return ERROR_SUCCESS;
1412 UINT ACTION_CustomAction(MSIPACKAGE *package, const WCHAR *action)
1414 UINT rc = ERROR_SUCCESS;
1415 MSIRECORD *row;
1416 UINT type;
1417 const WCHAR *source, *target, *ptr, *deferred_data = NULL;
1418 WCHAR *deformated = NULL;
1419 int len;
1421 /* deferred action: [properties]Action */
1422 if ((ptr = wcsrchr(action, ']')))
1424 deferred_data = action;
1425 action = ptr + 1;
1428 row = MSI_QueryGetRecord( package->db, L"SELECT * FROM `CustomAction` WHERE `Action` = '%s'", action );
1429 if (!row)
1430 return ERROR_FUNCTION_NOT_CALLED;
1432 type = MSI_RecordGetInteger(row,2);
1433 source = MSI_RecordGetString(row,3);
1434 target = MSI_RecordGetString(row,4);
1436 TRACE("Handling custom action %s (%x %s %s)\n",debugstr_w(action),type,
1437 debugstr_w(source), debugstr_w(target));
1439 /* handle some of the deferred actions */
1440 if (type & msidbCustomActionTypeTSAware)
1441 FIXME("msidbCustomActionTypeTSAware not handled\n");
1443 if (type & msidbCustomActionTypeInScript)
1445 if (type & msidbCustomActionTypeNoImpersonate)
1446 WARN("msidbCustomActionTypeNoImpersonate not handled\n");
1448 if (!action_type_matches_script(type, package->script))
1450 rc = defer_custom_action( package, action, type );
1451 goto end;
1453 else
1455 LPWSTR actiondata = msi_dup_property( package->db, action );
1457 if (type & msidbCustomActionTypeInScript)
1458 package->scheduled_action_running = TRUE;
1460 if (type & msidbCustomActionTypeCommit)
1461 package->commit_action_running = TRUE;
1463 if (type & msidbCustomActionTypeRollback)
1464 package->rollback_action_running = TRUE;
1466 if (deferred_data)
1467 set_deferred_action_props(package, deferred_data);
1468 else if (actiondata)
1469 msi_set_property( package->db, L"CustomActionData", actiondata, -1 );
1470 else
1471 msi_set_property( package->db, L"CustomActionData", L"", -1 );
1473 msi_free(actiondata);
1476 else if (!check_execution_scheduling_options(package,action,type))
1478 rc = ERROR_SUCCESS;
1479 goto end;
1482 switch (type & CUSTOM_ACTION_TYPE_MASK)
1484 case 1: /* DLL file stored in a Binary table stream */
1485 rc = HANDLE_CustomType1( package, source, target, type, action );
1486 break;
1487 case 2: /* EXE file stored in a Binary table stream */
1488 rc = HANDLE_CustomType2( package, source, target, type, action );
1489 break;
1490 case 5:
1491 case 6: /* JScript/VBScript file stored in a Binary table stream */
1492 rc = HANDLE_CustomType5_6( package, source, target, type, action );
1493 break;
1494 case 7: /* Concurrent install from substorage */
1495 deformat_string( package, target, &deformated );
1496 rc = HANDLE_CustomType7( package, source, target, type, action );
1497 msi_free( deformated );
1498 break;
1499 case 17:
1500 rc = HANDLE_CustomType17( package, source, target, type, action );
1501 break;
1502 case 18: /* EXE file installed with package */
1503 rc = HANDLE_CustomType18( package, source, target, type, action );
1504 break;
1505 case 19: /* Error that halts install */
1506 rc = HANDLE_CustomType19( package, source, target, type, action );
1507 break;
1508 case 21: /* JScript/VBScript file installed with the product */
1509 case 22:
1510 rc = HANDLE_CustomType21_22( package, source, target, type, action );
1511 break;
1512 case 23: /* Installs another package in the source tree */
1513 deformat_string( package, target, &deformated );
1514 rc = HANDLE_CustomType23( package, source, deformated, type, action );
1515 msi_free( deformated );
1516 break;
1517 case 34: /* EXE to be run in specified directory */
1518 rc = HANDLE_CustomType34( package, source, target, type, action );
1519 break;
1520 case 35: /* Directory set with formatted text */
1521 deformat_string( package, target, &deformated );
1522 MSI_SetTargetPathW( package, source, deformated );
1523 msi_free( deformated );
1524 break;
1525 case 37: /* JScript/VBScript text stored in target column */
1526 case 38:
1527 rc = HANDLE_CustomType37_38( package, source, target, type, action );
1528 break;
1529 case 50: /* EXE file specified by a property value */
1530 rc = HANDLE_CustomType50( package, source, target, type, action );
1531 break;
1532 case 51: /* Property set with formatted text */
1533 if (!source) break;
1534 len = deformat_string( package, target, &deformated );
1535 rc = msi_set_property( package->db, source, deformated, len );
1536 if (rc == ERROR_SUCCESS && !wcscmp( source, L"SourceDir" )) msi_reset_source_folders( package );
1537 msi_free( deformated );
1538 break;
1539 case 53: /* JScript/VBScript text specified by a property value */
1540 case 54:
1541 rc = HANDLE_CustomType53_54( package, source, target, type, action );
1542 break;
1543 default:
1544 FIXME( "unhandled action type %u (%s %s)\n", type & CUSTOM_ACTION_TYPE_MASK, debugstr_w(source),
1545 debugstr_w(target) );
1548 end:
1549 package->scheduled_action_running = FALSE;
1550 package->commit_action_running = FALSE;
1551 package->rollback_action_running = FALSE;
1552 msiobj_release(&row->hdr);
1553 return rc;
1556 void ACTION_FinishCustomActions(const MSIPACKAGE* package)
1558 struct list *item;
1559 HANDLE *wait_handles;
1560 unsigned int handle_count, i;
1561 msi_custom_action_info *info, *cursor;
1563 while ((item = list_head( &package->RunningActions )))
1565 MSIRUNNINGACTION *action = LIST_ENTRY( item, MSIRUNNINGACTION, entry );
1567 list_remove( &action->entry );
1569 TRACE("waiting for %s\n", debugstr_w( action->name ) );
1570 msi_dialog_check_messages( action->handle );
1572 CloseHandle( action->handle );
1573 msi_free( action->name );
1574 msi_free( action );
1577 EnterCriticalSection( &msi_custom_action_cs );
1579 handle_count = list_count( &msi_pending_custom_actions );
1580 wait_handles = msi_alloc( handle_count * sizeof(HANDLE) );
1582 handle_count = 0;
1583 LIST_FOR_EACH_ENTRY_SAFE( info, cursor, &msi_pending_custom_actions, msi_custom_action_info, entry )
1585 if (info->package == package )
1587 if (DuplicateHandle(GetCurrentProcess(), info->handle, GetCurrentProcess(), &wait_handles[handle_count], SYNCHRONIZE, FALSE, 0))
1588 handle_count++;
1592 LeaveCriticalSection( &msi_custom_action_cs );
1594 for (i = 0; i < handle_count; i++)
1596 msi_dialog_check_messages( wait_handles[i] );
1597 CloseHandle( wait_handles[i] );
1599 msi_free( wait_handles );
1601 EnterCriticalSection( &msi_custom_action_cs );
1602 LIST_FOR_EACH_ENTRY_SAFE( info, cursor, &msi_pending_custom_actions, msi_custom_action_info, entry )
1604 if (info->package == package)
1605 free_custom_action_data( info );
1607 LeaveCriticalSection( &msi_custom_action_cs );
1610 UINT __cdecl s_remote_GetActionInfo(const GUID *guid, WCHAR **name, int *type, WCHAR **dll, char **func, MSIHANDLE *hinst)
1612 msi_custom_action_info *info;
1614 info = find_action_by_guid(guid);
1615 if (!info)
1616 return ERROR_INVALID_DATA;
1618 *name = strdupW(info->action);
1619 *type = info->type;
1620 *hinst = alloc_msihandle(&info->package->hdr);
1621 *dll = strdupW(info->source);
1622 *func = strdupWtoA(info->target);
1624 return ERROR_SUCCESS;