include/mscvpdb.h: Use flexible array members for the rest of structures.
[wine.git] / dlls / msi / custom.c
blob12a7c3c3676656fa4242ef408bc249db91401e0d
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 struct running_action
48 struct list entry;
49 HANDLE handle;
50 BOOL process;
51 LPWSTR name;
54 typedef UINT (WINAPI *MsiCustomActionEntryPoint)( MSIHANDLE );
56 static CRITICAL_SECTION custom_action_cs;
57 static CRITICAL_SECTION_DEBUG custom_action_cs_debug =
59 0, 0, &custom_action_cs,
60 { &custom_action_cs_debug.ProcessLocksList,
61 &custom_action_cs_debug.ProcessLocksList },
62 0, 0, { (DWORD_PTR)(__FILE__ ": custom_action_cs") }
64 static CRITICAL_SECTION custom_action_cs = { &custom_action_cs_debug, -1, 0, 0, 0, 0 };
66 static struct list pending_custom_actions = LIST_INIT( 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 = realloc( package->script_actions[script],
98 package->script_actions_count[script] * sizeof(WCHAR *) );
99 else newbuf = malloc( sizeof(WCHAR *) );
101 newbuf[count] = wcsdup( 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 = realloc( package->unique_actions,
116 package->unique_actions_count * sizeof(WCHAR *) );
117 else newbuf = malloc( sizeof(WCHAR *) );
119 newbuf[count] = wcsdup( 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 WCHAR *get_deferred_action(const WCHAR *action, const WCHAR *actiondata, const WCHAR *usersid,
175 const WCHAR *prodcode)
177 LPWSTR deferred;
178 DWORD len;
180 if (!actiondata)
181 return wcsdup(action);
183 len = lstrlenW(action) + lstrlenW(actiondata) +
184 lstrlenW(usersid) + lstrlenW(prodcode) +
185 lstrlenW(L"[%s<=>%s<=>%s]%s") - 7;
186 deferred = malloc(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 = wcsdup( tmp ))) return NULL;
225 if ((ret = malloc( (wcslen( db->tempfolder ) + 20) * sizeof(WCHAR) )))
227 if (!GetTempFileNameW( db->tempfolder, L"msi", 0, ret ))
229 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 = calloc( 1, 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 = wcsdup( 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 free( tmpfile );
281 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 struct running_action *action;
303 action = malloc( sizeof(*action) );
305 action->handle = Handle;
306 action->process = process;
307 action->name = wcsdup(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
378 struct list entry;
379 MSIPACKAGE *package;
380 LPWSTR source;
381 LPWSTR target;
382 HANDLE handle;
383 LPWSTR action;
384 INT type;
385 GUID guid;
386 DWORD arch;
387 } custom_action_info;
389 static void free_custom_action_data( custom_action_info *info )
391 EnterCriticalSection( &custom_action_cs );
393 list_remove( &info->entry );
394 if (info->handle)
395 CloseHandle( info->handle );
396 free( info->action );
397 free( info->source );
398 free( info->target );
399 msiobj_release( &info->package->hdr );
400 free( info );
402 LeaveCriticalSection( &custom_action_cs );
405 static UINT wait_thread_handle( custom_action_info *info )
407 UINT rc = ERROR_SUCCESS;
409 if (!(info->type & msidbCustomActionTypeAsync))
411 TRACE("waiting for %s\n", debugstr_w( info->action ));
413 msi_dialog_check_messages( info->handle );
415 if (!(info->type & msidbCustomActionTypeContinue))
416 rc = custom_get_thread_return( info->package, info->handle );
418 free_custom_action_data( info );
420 else
422 TRACE("%s running in background\n", debugstr_w( info->action ));
425 return rc;
428 static custom_action_info *find_action_by_guid( const GUID *guid )
430 custom_action_info *info;
431 BOOL found = FALSE;
433 EnterCriticalSection( &custom_action_cs );
435 LIST_FOR_EACH_ENTRY( info, &pending_custom_actions, custom_action_info, entry )
437 if (IsEqualGUID( &info->guid, guid ))
439 found = TRUE;
440 break;
444 LeaveCriticalSection( &custom_action_cs );
446 if (!found)
447 return NULL;
449 return info;
452 static void handle_msi_break( const WCHAR *action )
454 const WCHAR fmt[] = L"To debug your custom action, attach your debugger to process %u (0x%x) and press OK";
455 WCHAR val[MAX_PATH], msg[100];
457 if (!GetEnvironmentVariableW( L"MsiBreak", val, MAX_PATH ) || wcscmp( val, action )) return;
459 swprintf( msg, ARRAY_SIZE(msg), fmt, GetCurrentProcessId(), GetCurrentProcessId() );
460 MessageBoxW( NULL, msg, L"Windows Installer", MB_OK );
461 DebugBreak();
464 #ifdef __i386__
465 /* wrapper for apps that don't declare the thread function correctly */
466 extern UINT custom_proc_wrapper( MsiCustomActionEntryPoint entry, MSIHANDLE hinst );
467 __ASM_GLOBAL_FUNC(custom_proc_wrapper,
468 "pushl %ebp\n\t"
469 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
470 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
471 "movl %esp,%ebp\n\t"
472 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
473 "subl $4,%esp\n\t"
474 "pushl 12(%ebp)\n\t"
475 "call *8(%ebp)\n\t"
476 "leave\n\t"
477 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
478 __ASM_CFI(".cfi_same_value %ebp\n\t")
479 "ret" )
480 #else
481 static UINT custom_proc_wrapper( MsiCustomActionEntryPoint entry, MSIHANDLE hinst )
483 return entry(hinst);
485 #endif
487 UINT CDECL __wine_msi_call_dll_function(DWORD client_pid, const GUID *guid)
489 MsiCustomActionEntryPoint fn;
490 MSIHANDLE remote_package = 0;
491 RPC_WSTR binding_str;
492 MSIHANDLE hPackage;
493 RPC_STATUS status;
494 WCHAR *dll = NULL, *action = NULL;
495 LPSTR proc = NULL;
496 HANDLE hModule;
497 INT type;
498 UINT r;
500 TRACE("%s\n", debugstr_guid( guid ));
502 if (!rpc_handle)
504 WCHAR endpoint[12];
506 swprintf(endpoint, ARRAY_SIZE(endpoint), L"msi%x", client_pid);
507 status = RpcStringBindingComposeW(NULL, (WCHAR *)L"ncalrpc", NULL, endpoint, NULL, &binding_str);
508 if (status != RPC_S_OK)
510 ERR("RpcStringBindingCompose failed: %#lx\n", status);
511 return status;
513 status = RpcBindingFromStringBindingW(binding_str, &rpc_handle);
514 if (status != RPC_S_OK)
516 ERR("RpcBindingFromStringBinding failed: %#lx\n", status);
517 return status;
519 RpcStringFreeW(&binding_str);
522 r = remote_GetActionInfo(guid, &action, &type, &dll, &proc, &remote_package);
523 if (r != ERROR_SUCCESS)
524 return r;
526 hPackage = alloc_msi_remote_handle( remote_package );
527 if (!hPackage)
529 ERR( "failed to create handle for %#lx\n", remote_package );
530 midl_user_free( action );
531 midl_user_free( dll );
532 midl_user_free( proc );
533 return ERROR_INSTALL_FAILURE;
536 hModule = LoadLibraryW( dll );
537 if (!hModule)
539 ERR( "failed to load dll %s (%lu)\n", debugstr_w( dll ), GetLastError() );
540 midl_user_free( action );
541 midl_user_free( dll );
542 midl_user_free( proc );
543 MsiCloseHandle( hPackage );
544 return ERROR_SUCCESS;
547 fn = (MsiCustomActionEntryPoint) GetProcAddress( hModule, proc );
548 if (!fn) WARN( "GetProcAddress(%s) failed\n", debugstr_a(proc) );
549 else
551 handle_msi_break(action);
553 __TRY
555 r = custom_proc_wrapper( fn, hPackage );
557 __EXCEPT_PAGE_FAULT
559 ERR( "Custom action (%s:%s) caused a page fault: %#lx\n",
560 debugstr_w(dll), debugstr_a(proc), GetExceptionCode() );
561 r = ERROR_SUCCESS;
563 __ENDTRY;
566 FreeLibrary(hModule);
568 midl_user_free(action);
569 midl_user_free(dll);
570 midl_user_free(proc);
572 MsiCloseAllHandles();
573 return r;
576 static HANDLE get_admin_token(void)
578 TOKEN_ELEVATION_TYPE type;
579 TOKEN_LINKED_TOKEN linked;
580 DWORD size;
582 if (!GetTokenInformation(GetCurrentThreadEffectiveToken(), TokenElevationType, &type, sizeof(type), &size)
583 || type == TokenElevationTypeFull)
584 return NULL;
586 if (!GetTokenInformation(GetCurrentThreadEffectiveToken(), TokenLinkedToken, &linked, sizeof(linked), &size))
587 return NULL;
588 return linked.LinkedToken;
591 static DWORD custom_start_server(MSIPACKAGE *package, DWORD arch)
593 WCHAR path[MAX_PATH], cmdline[MAX_PATH + 23];
594 PROCESS_INFORMATION pi = {0};
595 STARTUPINFOW si = {0};
596 WCHAR buffer[24];
597 HANDLE token;
598 void *cookie;
599 HANDLE pipe;
601 if ((arch == SCS_32BIT_BINARY && package->custom_server_32_process) ||
602 (arch == SCS_64BIT_BINARY && package->custom_server_64_process))
603 return ERROR_SUCCESS;
605 swprintf(buffer, ARRAY_SIZE(buffer), L"\\\\.\\pipe\\msica_%x_%d",
606 GetCurrentProcessId(), arch == SCS_32BIT_BINARY ? 32 : 64);
607 pipe = CreateNamedPipeW(buffer, PIPE_ACCESS_DUPLEX, 0, 1, sizeof(DWORD64),
608 sizeof(GUID), 0, NULL);
609 if (pipe == INVALID_HANDLE_VALUE)
610 ERR("failed to create custom action client pipe: %lu\n", GetLastError());
612 if ((sizeof(void *) == 8 || is_wow64) && arch == SCS_32BIT_BINARY)
613 GetSystemWow64DirectoryW(path, MAX_PATH - ARRAY_SIZE(L"\\msiexec.exe"));
614 else
615 GetSystemDirectoryW(path, MAX_PATH - ARRAY_SIZE(L"\\msiexec.exe"));
616 lstrcatW(path, L"\\msiexec.exe");
617 swprintf(cmdline, ARRAY_SIZE(cmdline), L"%s -Embedding %d", path, GetCurrentProcessId());
619 token = get_admin_token();
621 if (is_wow64 && arch == SCS_64BIT_BINARY)
623 Wow64DisableWow64FsRedirection(&cookie);
624 CreateProcessAsUserW(token, path, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
625 Wow64RevertWow64FsRedirection(cookie);
627 else
628 CreateProcessAsUserW(token, path, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
630 if (token) CloseHandle(token);
632 CloseHandle(pi.hThread);
634 if (arch == SCS_32BIT_BINARY)
636 package->custom_server_32_process = pi.hProcess;
637 package->custom_server_32_pipe = pipe;
639 else
641 package->custom_server_64_process = pi.hProcess;
642 package->custom_server_64_pipe = pipe;
645 if (!ConnectNamedPipe(pipe, NULL))
647 ERR("failed to connect to custom action server: %lu\n", GetLastError());
648 return GetLastError();
651 return ERROR_SUCCESS;
654 void custom_stop_server(HANDLE process, HANDLE pipe)
656 DWORD size;
658 WriteFile(pipe, &GUID_NULL, sizeof(GUID_NULL), &size, NULL);
659 WaitForSingleObject(process, INFINITE);
660 CloseHandle(process);
661 CloseHandle(pipe);
664 static DWORD WINAPI custom_client_thread(void *arg)
666 custom_action_info *info = arg;
667 DWORD64 thread64;
668 HANDLE process;
669 HANDLE thread;
670 HANDLE pipe;
671 DWORD size;
672 DWORD rc;
674 CoInitializeEx(NULL, COINIT_MULTITHREADED); /* needed to marshal streams */
676 if (info->arch == SCS_32BIT_BINARY)
678 process = info->package->custom_server_32_process;
679 pipe = info->package->custom_server_32_pipe;
681 else
683 process = info->package->custom_server_64_process;
684 pipe = info->package->custom_server_64_pipe;
687 EnterCriticalSection(&custom_action_cs);
689 if (!WriteFile(pipe, &info->guid, sizeof(info->guid), &size, NULL) ||
690 size != sizeof(info->guid))
692 ERR("failed to write to custom action client pipe: %lu\n", GetLastError());
693 LeaveCriticalSection(&custom_action_cs);
694 return GetLastError();
696 if (!ReadFile(pipe, &thread64, sizeof(thread64), &size, NULL) || size != sizeof(thread64))
698 ERR("failed to read from custom action client pipe: %lu\n", GetLastError());
699 LeaveCriticalSection(&custom_action_cs);
700 return GetLastError();
703 LeaveCriticalSection(&custom_action_cs);
705 if (DuplicateHandle(process, (HANDLE)(DWORD_PTR)thread64, GetCurrentProcess(),
706 &thread, 0, FALSE, DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE))
708 WaitForSingleObject(thread, INFINITE);
709 GetExitCodeThread(thread, &rc);
710 CloseHandle(thread);
712 else
713 rc = GetLastError();
715 CoUninitialize();
716 return rc;
719 /* based on kernel32.GetBinaryTypeW() */
720 static BOOL get_binary_type( const WCHAR *name, DWORD *type )
722 HANDLE hfile, mapping;
723 NTSTATUS status;
725 hfile = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
726 if (hfile == INVALID_HANDLE_VALUE)
727 return FALSE;
729 status = NtCreateSection( &mapping, STANDARD_RIGHTS_REQUIRED | SECTION_QUERY, NULL, NULL, PAGE_READONLY,
730 SEC_IMAGE, hfile );
731 CloseHandle( hfile );
733 switch (status)
735 case STATUS_SUCCESS:
737 SECTION_IMAGE_INFORMATION info;
739 status = NtQuerySection( mapping, SectionImageInformation, &info, sizeof(info), NULL );
740 CloseHandle( mapping );
741 if (status) return FALSE;
742 switch (info.Machine)
744 case IMAGE_FILE_MACHINE_I386:
745 case IMAGE_FILE_MACHINE_ARMNT:
746 *type = SCS_32BIT_BINARY;
747 return TRUE;
748 case IMAGE_FILE_MACHINE_AMD64:
749 case IMAGE_FILE_MACHINE_ARM64:
750 *type = SCS_64BIT_BINARY;
751 return TRUE;
752 default:
753 return FALSE;
756 case STATUS_INVALID_IMAGE_WIN_64:
757 *type = SCS_64BIT_BINARY;
758 return TRUE;
759 default:
760 return FALSE;
764 static custom_action_info *do_msidbCustomActionTypeDll(
765 MSIPACKAGE *package, INT type, LPCWSTR source, LPCWSTR target, LPCWSTR action )
767 custom_action_info *info;
768 RPC_STATUS status;
769 BOOL ret;
771 info = malloc( sizeof *info );
772 if (!info)
773 return NULL;
775 msiobj_addref( &package->hdr );
776 info->package = package;
777 info->type = type;
778 info->target = wcsdup( target );
779 info->source = wcsdup( source );
780 info->action = wcsdup( action );
781 CoCreateGuid( &info->guid );
783 EnterCriticalSection( &custom_action_cs );
784 list_add_tail( &pending_custom_actions, &info->entry );
785 LeaveCriticalSection( &custom_action_cs );
787 if (!package->rpc_server_started)
789 WCHAR endpoint[12];
791 swprintf(endpoint, ARRAY_SIZE(endpoint), L"msi%x", GetCurrentProcessId());
792 status = RpcServerUseProtseqEpW((WCHAR *)L"ncalrpc", RPC_C_PROTSEQ_MAX_REQS_DEFAULT,
793 endpoint, NULL);
794 if (status != RPC_S_OK)
796 ERR("RpcServerUseProtseqEp failed: %#lx\n", status);
797 return NULL;
800 status = RpcServerRegisterIfEx(s_IWineMsiRemote_v0_0_s_ifspec, NULL, NULL,
801 RPC_IF_AUTOLISTEN, RPC_C_LISTEN_MAX_CALLS_DEFAULT, NULL);
802 if (status != RPC_S_OK)
804 ERR("RpcServerRegisterIfEx failed: %#lx\n", status);
805 return NULL;
808 info->package->rpc_server_started = 1;
811 ret = get_binary_type(source, &info->arch);
812 if (!ret)
813 info->arch = (sizeof(void *) == 8 ? SCS_64BIT_BINARY : SCS_32BIT_BINARY);
815 if (info->arch == SCS_64BIT_BINARY && sizeof(void *) == 4 && !is_wow64)
817 ERR("Attempt to run a 64-bit custom action inside a 32-bit WINEPREFIX.\n");
818 free_custom_action_data( info );
819 return NULL;
822 custom_start_server(package, info->arch);
824 info->handle = CreateThread(NULL, 0, custom_client_thread, info, 0, NULL);
825 if (!info->handle)
827 free_custom_action_data( info );
828 return NULL;
831 return info;
834 static UINT HANDLE_CustomType1( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
835 INT type, const WCHAR *action )
837 custom_action_info *info;
838 MSIBINARY *binary;
840 if (!(binary = get_temp_binary(package, source)))
841 return ERROR_FUNCTION_FAILED;
843 TRACE("Calling function %s from %s\n", debugstr_w(target), debugstr_w(binary->tmpfile));
845 if (!(info = do_msidbCustomActionTypeDll( package, type, binary->tmpfile, target, action )))
846 return ERROR_FUNCTION_FAILED;
847 return wait_thread_handle( info );
850 static HANDLE execute_command( const WCHAR *app, WCHAR *arg, const WCHAR *dir )
852 STARTUPINFOW si;
853 PROCESS_INFORMATION info;
854 WCHAR *exe = NULL, *cmd = NULL, *p;
855 BOOL ret;
857 if (app)
859 int len_arg = 0;
860 DWORD len_exe;
862 if (!(exe = malloc( MAX_PATH * sizeof(WCHAR) ))) return INVALID_HANDLE_VALUE;
863 len_exe = SearchPathW( NULL, app, L".exe", MAX_PATH, exe, NULL );
864 if (len_exe >= MAX_PATH)
866 free( exe );
867 if (!(exe = malloc( len_exe * sizeof(WCHAR) ))) return INVALID_HANDLE_VALUE;
868 len_exe = SearchPathW( NULL, app, L".exe", len_exe, exe, NULL );
870 if (!len_exe)
872 ERR("can't find executable %lu\n", GetLastError());
873 free( exe );
874 return INVALID_HANDLE_VALUE;
877 if (arg) len_arg = lstrlenW( arg );
878 if (!(cmd = malloc( (len_exe + len_arg + 4) * sizeof(WCHAR) )))
880 free( exe );
881 return INVALID_HANDLE_VALUE;
883 p = cmd;
884 if (wcschr( exe, ' ' ))
886 *p++ = '\"';
887 memcpy( p, exe, len_exe * sizeof(WCHAR) );
888 p += len_exe;
889 *p++ = '\"';
890 *p = 0;
892 else
894 lstrcpyW( p, exe );
895 p += len_exe;
897 if (arg)
899 *p++ = ' ';
900 memcpy( p, arg, len_arg * sizeof(WCHAR) );
901 p[len_arg] = 0;
904 memset( &si, 0, sizeof(STARTUPINFOW) );
905 ret = CreateProcessW( exe, exe ? cmd : arg, NULL, NULL, FALSE, 0, NULL, dir, &si, &info );
906 free( cmd );
907 free( exe );
908 if (!ret)
910 ERR("unable to execute command %lu\n", GetLastError());
911 return INVALID_HANDLE_VALUE;
913 CloseHandle( info.hThread );
914 return info.hProcess;
917 static UINT HANDLE_CustomType2( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
918 INT type, const WCHAR *action )
920 MSIBINARY *binary;
921 HANDLE handle;
922 WCHAR *arg;
924 if (!(binary = get_temp_binary(package, source)))
925 return ERROR_FUNCTION_FAILED;
927 deformat_string( package, target, &arg );
928 TRACE("exe %s arg %s\n", debugstr_w(binary->tmpfile), debugstr_w(arg));
930 handle = execute_command( binary->tmpfile, arg, L"C:\\" );
931 free( arg );
932 if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
933 return wait_process_handle( package, type, handle, action );
936 static UINT HANDLE_CustomType17( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
937 INT type, const WCHAR *action )
939 custom_action_info *info;
940 MSIFILE *file;
942 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
944 file = msi_get_loaded_file( package, source );
945 if (!file)
947 ERR("invalid file key %s\n", debugstr_w( source ));
948 return ERROR_FUNCTION_FAILED;
951 if (!(info = do_msidbCustomActionTypeDll( package, type, file->TargetPath, target, action )))
952 return ERROR_FUNCTION_FAILED;
953 return wait_thread_handle( info );
956 static UINT HANDLE_CustomType18( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
957 INT type, const WCHAR *action )
959 MSIFILE *file;
960 HANDLE handle;
961 WCHAR *arg;
963 if (!(file = msi_get_loaded_file( package, source ))) return ERROR_FUNCTION_FAILED;
965 deformat_string( package, target, &arg );
966 TRACE("exe %s arg %s\n", debugstr_w(file->TargetPath), debugstr_w(arg));
968 handle = execute_command( file->TargetPath, arg, L"C:\\" );
969 free( arg );
970 if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
971 return wait_process_handle( package, type, handle, action );
974 static UINT HANDLE_CustomType19( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
975 INT type, const WCHAR *action )
977 MSIRECORD *row = 0;
978 LPWSTR deformated = NULL;
980 deformat_string( package, target, &deformated );
982 /* first try treat the error as a number */
983 row = MSI_QueryGetRecord( package->db, L"SELECT `Message` FROM `Error` WHERE `Error` = '%s'", deformated );
984 if( row )
986 LPCWSTR error = MSI_RecordGetString( row, 1 );
987 if ((package->ui_level & INSTALLUILEVEL_MASK) != INSTALLUILEVEL_NONE)
988 MessageBoxW( NULL, error, NULL, MB_OK );
989 msiobj_release( &row->hdr );
991 else if ((package->ui_level & INSTALLUILEVEL_MASK) != INSTALLUILEVEL_NONE)
992 MessageBoxW( NULL, deformated, NULL, MB_OK );
994 free( deformated );
996 return ERROR_INSTALL_FAILURE;
999 static WCHAR *build_msiexec_args( const WCHAR *filename, const WCHAR *params )
1001 UINT len_filename = lstrlenW( filename ), len_params = lstrlenW( params );
1002 UINT len = ARRAY_SIZE(L"/qb /i ") - 1;
1003 WCHAR *ret;
1005 if (!(ret = malloc( (len + len_filename + len_params + 4) * sizeof(WCHAR) ))) return NULL;
1006 memcpy( ret, L"/qb /i ", sizeof(L"/qb /i ") );
1007 ret[len++] = '"';
1008 memcpy( ret + len, filename, len_filename * sizeof(WCHAR) );
1009 len += len_filename;
1010 ret[len++] = '"';
1011 ret[len++] = ' ';
1012 lstrcpyW( ret + len, params );
1013 return ret;
1016 static UINT HANDLE_CustomType23( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
1017 INT type, const WCHAR *action )
1019 WCHAR *dir, *filename, *args, *p;
1020 UINT len_dir, len_source = lstrlenW( source );
1021 HANDLE handle;
1023 if (!(dir = msi_dup_property( package->db, L"OriginalDatabase" ))) return ERROR_OUTOFMEMORY;
1024 if (!(p = wcsrchr( dir, '\\' )) && !(p = wcsrchr( dir, '/' )))
1026 free( dir );
1027 return ERROR_FUNCTION_FAILED;
1029 *p = 0;
1030 len_dir = p - dir;
1031 if (!(filename = malloc( (len_dir + len_source + 2) * sizeof(WCHAR) )))
1033 free( dir );
1034 return ERROR_OUTOFMEMORY;
1036 memcpy( filename, dir, len_dir * sizeof(WCHAR) );
1037 filename[len_dir++] = '\\';
1038 memcpy( filename + len_dir, source, len_source * sizeof(WCHAR) );
1039 filename[len_dir + len_source] = 0;
1041 if (!(args = build_msiexec_args( filename, target )))
1043 free( dir );
1044 free( filename );
1045 return ERROR_OUTOFMEMORY;
1048 TRACE("installing %s concurrently\n", debugstr_w(source));
1050 handle = execute_command( L"msiexec", args, dir );
1051 free( dir );
1052 free( filename );
1053 free( args );
1054 if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
1055 return wait_process_handle( package, type, handle, action );
1058 static UINT write_substorage_to_file( MSIPACKAGE *package, const WCHAR *source, const WCHAR *filename )
1060 IStorage *src = NULL, *dst = NULL;
1061 UINT r = ERROR_FUNCTION_FAILED;
1062 HRESULT hr;
1064 hr = StgCreateDocfile( filename, STGM_CREATE|STGM_TRANSACTED|STGM_WRITE|STGM_SHARE_EXCLUSIVE, 0, &dst );
1065 if (FAILED( hr ))
1067 WARN( "can't open destination storage %s (%#lx)\n", debugstr_w(filename), hr );
1068 goto done;
1071 hr = IStorage_OpenStorage( package->db->storage, source, NULL, STGM_SHARE_EXCLUSIVE, NULL, 0, &src );
1072 if (FAILED( hr ))
1074 WARN( "can't open source storage %s (%#lx)\n", debugstr_w(source), hr );
1075 goto done;
1078 hr = IStorage_CopyTo( src, 0, NULL, NULL, dst );
1079 if (FAILED( hr ))
1081 ERR( "failed to copy storage %s (%#lx)\n", debugstr_w(source), hr );
1082 goto done;
1085 hr = IStorage_Commit( dst, 0 );
1086 if (FAILED( hr ))
1087 ERR( "failed to commit storage (%#lx)\n", hr );
1088 else
1089 r = ERROR_SUCCESS;
1091 done:
1092 if (src) IStorage_Release( src );
1093 if (dst) IStorage_Release( dst );
1094 return r;
1097 static UINT HANDLE_CustomType7( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
1098 INT type, const WCHAR *action )
1100 WCHAR *tmpfile, *args;
1101 MSIBINARY *binary = NULL;
1102 HANDLE handle;
1103 UINT r;
1105 if (!(tmpfile = msi_create_temp_file( package->db ))) return ERROR_FUNCTION_FAILED;
1107 r = write_substorage_to_file( package, source, tmpfile );
1108 if (r != ERROR_SUCCESS)
1109 goto error;
1111 if (!(binary = malloc( sizeof(*binary) ))) goto error;
1112 binary->source = NULL;
1113 binary->tmpfile = tmpfile;
1114 list_add_tail( &package->binaries, &binary->entry );
1116 if (!(args = build_msiexec_args( tmpfile, target ))) return ERROR_OUTOFMEMORY;
1118 TRACE("installing %s concurrently\n", debugstr_w(source));
1120 handle = execute_command( L"msiexec", args, L"C:\\" );
1121 free( args );
1122 if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
1123 return wait_process_handle( package, type, handle, action );
1125 error:
1126 DeleteFileW( tmpfile );
1127 free( tmpfile );
1128 return ERROR_FUNCTION_FAILED;
1131 static UINT HANDLE_CustomType50( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
1132 INT type, const WCHAR *action )
1134 WCHAR *exe, *arg;
1135 HANDLE handle;
1137 if (!(exe = msi_dup_property( package->db, source ))) return ERROR_SUCCESS;
1139 deformat_string( package, target, &arg );
1140 TRACE("exe %s arg %s\n", debugstr_w(exe), debugstr_w(arg));
1142 handle = execute_command( exe, arg, L"C:\\" );
1143 free( exe );
1144 free( arg );
1145 if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
1146 return wait_process_handle( package, type, handle, action );
1149 static UINT HANDLE_CustomType34( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
1150 INT type, const WCHAR *action )
1152 const WCHAR *workingdir = NULL;
1153 HANDLE handle;
1154 WCHAR *cmd;
1156 if (source)
1158 workingdir = msi_get_target_folder( package, source );
1159 if (!workingdir) return ERROR_FUNCTION_FAILED;
1161 deformat_string( package, target, &cmd );
1162 if (!cmd) return ERROR_FUNCTION_FAILED;
1164 TRACE("cmd %s dir %s\n", debugstr_w(cmd), debugstr_w(workingdir));
1166 handle = execute_command( NULL, cmd, workingdir );
1167 free( cmd );
1168 if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
1169 return wait_process_handle( package, type, handle, action );
1172 static DWORD ACTION_CallScript( const GUID *guid )
1174 custom_action_info *info;
1175 MSIHANDLE hPackage;
1176 UINT r = ERROR_FUNCTION_FAILED;
1178 info = find_action_by_guid( guid );
1179 if (!info)
1181 ERR("failed to find action %s\n", debugstr_guid( guid) );
1182 return ERROR_FUNCTION_FAILED;
1185 TRACE("function %s, script %s\n", debugstr_w( info->target ), debugstr_w( info->source ) );
1187 hPackage = alloc_msihandle( &info->package->hdr );
1188 if (hPackage)
1190 r = call_script( hPackage, info->type, info->source, info->target, info->action );
1191 TRACE("script returned %u\n", r);
1192 MsiCloseHandle( hPackage );
1194 else
1195 ERR("failed to create handle for %p\n", info->package );
1197 return r;
1200 static DWORD WINAPI ScriptThread( LPVOID arg )
1202 LPGUID guid = arg;
1203 DWORD rc;
1205 TRACE("custom action (%#lx) started\n", GetCurrentThreadId() );
1207 rc = ACTION_CallScript( guid );
1209 TRACE("custom action (%#lx) returned %lu\n", GetCurrentThreadId(), rc );
1211 MsiCloseAllHandles();
1212 return rc;
1215 static custom_action_info *do_msidbCustomActionTypeScript(
1216 MSIPACKAGE *package, INT type, LPCWSTR script, LPCWSTR function, LPCWSTR action )
1218 custom_action_info *info;
1220 info = malloc( sizeof *info );
1221 if (!info)
1222 return NULL;
1224 msiobj_addref( &package->hdr );
1225 info->package = package;
1226 info->type = type;
1227 info->target = wcsdup( function );
1228 info->source = wcsdup( script );
1229 info->action = wcsdup( action );
1230 CoCreateGuid( &info->guid );
1232 EnterCriticalSection( &custom_action_cs );
1233 list_add_tail( &pending_custom_actions, &info->entry );
1234 LeaveCriticalSection( &custom_action_cs );
1236 info->handle = CreateThread( NULL, 0, ScriptThread, &info->guid, 0, NULL );
1237 if (!info->handle)
1239 free_custom_action_data( info );
1240 return NULL;
1243 return info;
1246 static UINT HANDLE_CustomType37_38( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
1247 INT type, const WCHAR *action )
1249 custom_action_info *info;
1251 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1253 info = do_msidbCustomActionTypeScript( package, type, target, NULL, action );
1254 return wait_thread_handle( info );
1257 static UINT HANDLE_CustomType5_6( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
1258 INT type, const WCHAR *action )
1260 MSIRECORD *row = NULL;
1261 custom_action_info *info;
1262 CHAR *buffer = NULL;
1263 WCHAR *bufferw = NULL;
1264 DWORD sz = 0;
1265 UINT r;
1267 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1269 row = MSI_QueryGetRecord(package->db, L"SELECT * FROM `Binary` WHERE `Name` = '%s'", source);
1270 if (!row)
1271 return ERROR_FUNCTION_FAILED;
1273 r = MSI_RecordReadStream(row, 2, NULL, &sz);
1274 if (r != ERROR_SUCCESS) goto done;
1276 buffer = malloc( sz + 1 );
1277 if (!buffer)
1279 r = ERROR_FUNCTION_FAILED;
1280 goto done;
1283 r = MSI_RecordReadStream(row, 2, buffer, &sz);
1284 if (r != ERROR_SUCCESS)
1285 goto done;
1287 buffer[sz] = 0;
1288 bufferw = strdupAtoW(buffer);
1289 if (!bufferw)
1291 r = ERROR_FUNCTION_FAILED;
1292 goto done;
1295 info = do_msidbCustomActionTypeScript( package, type, bufferw, target, action );
1296 r = wait_thread_handle( info );
1298 done:
1299 free(bufferw);
1300 free(buffer);
1301 msiobj_release(&row->hdr);
1302 return r;
1305 static UINT HANDLE_CustomType21_22( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
1306 INT type, const WCHAR *action )
1308 custom_action_info *info;
1309 MSIFILE *file;
1310 HANDLE hFile;
1311 DWORD sz, szHighWord = 0, read;
1312 CHAR *buffer=NULL;
1313 WCHAR *bufferw=NULL;
1314 BOOL bRet;
1315 UINT r;
1317 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1319 file = msi_get_loaded_file(package, source);
1320 if (!file)
1322 ERR("invalid file key %s\n", debugstr_w(source));
1323 return ERROR_FUNCTION_FAILED;
1326 hFile = msi_create_file( package, file->TargetPath, GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, 0 );
1327 if (hFile == INVALID_HANDLE_VALUE) return ERROR_FUNCTION_FAILED;
1329 sz = GetFileSize(hFile, &szHighWord);
1330 if (sz == INVALID_FILE_SIZE || szHighWord != 0)
1332 CloseHandle(hFile);
1333 return ERROR_FUNCTION_FAILED;
1335 buffer = malloc( sz + 1 );
1336 if (!buffer)
1338 CloseHandle(hFile);
1339 return ERROR_FUNCTION_FAILED;
1341 bRet = ReadFile(hFile, buffer, sz, &read, NULL);
1342 CloseHandle(hFile);
1343 if (!bRet)
1345 r = ERROR_FUNCTION_FAILED;
1346 goto done;
1348 buffer[read] = 0;
1349 bufferw = strdupAtoW(buffer);
1350 if (!bufferw)
1352 r = ERROR_FUNCTION_FAILED;
1353 goto done;
1355 info = do_msidbCustomActionTypeScript( package, type, bufferw, target, action );
1356 r = wait_thread_handle( info );
1358 done:
1359 free(bufferw);
1360 free(buffer);
1361 return r;
1364 static UINT HANDLE_CustomType53_54( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
1365 INT type, const WCHAR *action )
1367 custom_action_info *info;
1368 WCHAR *prop;
1370 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1372 prop = msi_dup_property( package->db, source );
1373 if (!prop) return ERROR_SUCCESS;
1375 info = do_msidbCustomActionTypeScript( package, type, prop, NULL, action );
1376 free(prop);
1377 return wait_thread_handle( info );
1380 static BOOL action_type_matches_script( UINT type, UINT script )
1382 switch (script)
1384 case SCRIPT_NONE:
1385 return FALSE;
1386 case SCRIPT_INSTALL:
1387 return !(type & msidbCustomActionTypeCommit) && !(type & msidbCustomActionTypeRollback);
1388 case SCRIPT_COMMIT:
1389 return (type & msidbCustomActionTypeCommit);
1390 case SCRIPT_ROLLBACK:
1391 return (type & msidbCustomActionTypeRollback);
1392 default:
1393 ERR("unhandled script %u\n", script);
1395 return FALSE;
1398 static UINT defer_custom_action( MSIPACKAGE *package, const WCHAR *action, UINT type )
1400 WCHAR *actiondata = msi_dup_property( package->db, action );
1401 WCHAR *usersid = msi_dup_property( package->db, L"UserSID" );
1402 WCHAR *prodcode = msi_dup_property( package->db, L"ProductCode" );
1403 WCHAR *deferred = get_deferred_action( action, actiondata, usersid, prodcode );
1405 if (!deferred)
1407 free( actiondata );
1408 free( usersid );
1409 free( prodcode );
1410 return ERROR_OUTOFMEMORY;
1412 if (type & msidbCustomActionTypeCommit)
1414 TRACE("deferring commit action\n");
1415 msi_schedule_action( package, SCRIPT_COMMIT, deferred );
1417 else if (type & msidbCustomActionTypeRollback)
1419 TRACE("deferring rollback action\n");
1420 msi_schedule_action( package, SCRIPT_ROLLBACK, deferred );
1422 else
1424 TRACE("deferring install action\n");
1425 msi_schedule_action( package, SCRIPT_INSTALL, deferred );
1428 free( actiondata );
1429 free( usersid );
1430 free( prodcode );
1431 free( deferred );
1432 return ERROR_SUCCESS;
1435 UINT ACTION_CustomAction(MSIPACKAGE *package, const WCHAR *action)
1437 UINT rc = ERROR_SUCCESS;
1438 MSIRECORD *row;
1439 UINT type;
1440 const WCHAR *source, *target, *ptr, *deferred_data = NULL;
1441 WCHAR *deformated = NULL;
1442 int len;
1444 /* deferred action: [properties]Action */
1445 if ((ptr = wcsrchr(action, ']')))
1447 deferred_data = action;
1448 action = ptr + 1;
1451 row = MSI_QueryGetRecord( package->db, L"SELECT * FROM `CustomAction` WHERE `Action` = '%s'", action );
1452 if (!row)
1453 return ERROR_FUNCTION_NOT_CALLED;
1455 type = MSI_RecordGetInteger(row,2);
1456 source = MSI_RecordGetString(row,3);
1457 target = MSI_RecordGetString(row,4);
1459 TRACE("Handling custom action %s (%x %s %s)\n",debugstr_w(action),type,
1460 debugstr_w(source), debugstr_w(target));
1462 /* handle some of the deferred actions */
1463 if (type & msidbCustomActionTypeTSAware)
1464 FIXME("msidbCustomActionTypeTSAware not handled\n");
1466 if (type & msidbCustomActionTypeInScript)
1468 if (type & msidbCustomActionTypeNoImpersonate)
1469 WARN("msidbCustomActionTypeNoImpersonate not handled\n");
1471 if (!action_type_matches_script(type, package->script))
1473 rc = defer_custom_action( package, action, type );
1474 goto end;
1476 else
1478 LPWSTR actiondata = msi_dup_property( package->db, action );
1480 if (type & msidbCustomActionTypeInScript)
1481 package->scheduled_action_running = TRUE;
1483 if (type & msidbCustomActionTypeCommit)
1484 package->commit_action_running = TRUE;
1486 if (type & msidbCustomActionTypeRollback)
1487 package->rollback_action_running = TRUE;
1489 if (deferred_data)
1490 set_deferred_action_props(package, deferred_data);
1491 else if (actiondata)
1492 msi_set_property( package->db, L"CustomActionData", actiondata, -1 );
1493 else
1494 msi_set_property( package->db, L"CustomActionData", L"", -1 );
1496 free(actiondata);
1499 else if (!check_execution_scheduling_options(package,action,type))
1501 rc = ERROR_SUCCESS;
1502 goto end;
1505 switch (type & CUSTOM_ACTION_TYPE_MASK)
1507 case 1: /* DLL file stored in a Binary table stream */
1508 rc = HANDLE_CustomType1( package, source, target, type, action );
1509 break;
1510 case 2: /* EXE file stored in a Binary table stream */
1511 rc = HANDLE_CustomType2( package, source, target, type, action );
1512 break;
1513 case 5:
1514 case 6: /* JScript/VBScript file stored in a Binary table stream */
1515 rc = HANDLE_CustomType5_6( package, source, target, type, action );
1516 break;
1517 case 7: /* Concurrent install from substorage */
1518 deformat_string( package, target, &deformated );
1519 rc = HANDLE_CustomType7( package, source, target, type, action );
1520 free( deformated );
1521 break;
1522 case 17:
1523 rc = HANDLE_CustomType17( package, source, target, type, action );
1524 break;
1525 case 18: /* EXE file installed with package */
1526 rc = HANDLE_CustomType18( package, source, target, type, action );
1527 break;
1528 case 19: /* Error that halts install */
1529 rc = HANDLE_CustomType19( package, source, target, type, action );
1530 break;
1531 case 21: /* JScript/VBScript file installed with the product */
1532 case 22:
1533 rc = HANDLE_CustomType21_22( package, source, target, type, action );
1534 break;
1535 case 23: /* Installs another package in the source tree */
1536 deformat_string( package, target, &deformated );
1537 rc = HANDLE_CustomType23( package, source, deformated, type, action );
1538 free( deformated );
1539 break;
1540 case 34: /* EXE to be run in specified directory */
1541 rc = HANDLE_CustomType34( package, source, target, type, action );
1542 break;
1543 case 35: /* Directory set with formatted text */
1544 deformat_string( package, target, &deformated );
1545 MSI_SetTargetPathW( package, source, deformated );
1546 free( deformated );
1547 break;
1548 case 37: /* JScript/VBScript text stored in target column */
1549 case 38:
1550 rc = HANDLE_CustomType37_38( package, source, target, type, action );
1551 break;
1552 case 50: /* EXE file specified by a property value */
1553 rc = HANDLE_CustomType50( package, source, target, type, action );
1554 break;
1555 case 51: /* Property set with formatted text */
1556 if (!source) break;
1557 len = deformat_string( package, target, &deformated );
1558 rc = msi_set_property( package->db, source, deformated, len );
1559 if (rc == ERROR_SUCCESS && !wcscmp( source, L"SourceDir" )) msi_reset_source_folders( package );
1560 free( deformated );
1561 break;
1562 case 53: /* JScript/VBScript text specified by a property value */
1563 case 54:
1564 rc = HANDLE_CustomType53_54( package, source, target, type, action );
1565 break;
1566 default:
1567 FIXME( "unhandled action type %u (%s %s)\n", type & CUSTOM_ACTION_TYPE_MASK, debugstr_w(source),
1568 debugstr_w(target) );
1571 end:
1572 package->scheduled_action_running = FALSE;
1573 package->commit_action_running = FALSE;
1574 package->rollback_action_running = FALSE;
1575 msiobj_release(&row->hdr);
1576 return rc;
1579 void ACTION_FinishCustomActions(const MSIPACKAGE* package)
1581 struct list *item;
1582 HANDLE *wait_handles;
1583 unsigned int handle_count, i;
1584 custom_action_info *info, *cursor;
1586 while ((item = list_head( &package->RunningActions )))
1588 struct running_action *action = LIST_ENTRY( item, struct running_action, entry );
1590 list_remove( &action->entry );
1592 TRACE("waiting for %s\n", debugstr_w( action->name ) );
1593 msi_dialog_check_messages( action->handle );
1595 CloseHandle( action->handle );
1596 free( action->name );
1597 free( action );
1600 EnterCriticalSection( &custom_action_cs );
1602 handle_count = list_count( &pending_custom_actions );
1603 wait_handles = malloc( handle_count * sizeof(HANDLE) );
1605 handle_count = 0;
1606 LIST_FOR_EACH_ENTRY_SAFE( info, cursor, &pending_custom_actions, custom_action_info, entry )
1608 if (info->package == package )
1610 if (DuplicateHandle(GetCurrentProcess(), info->handle, GetCurrentProcess(), &wait_handles[handle_count], SYNCHRONIZE, FALSE, 0))
1611 handle_count++;
1615 LeaveCriticalSection( &custom_action_cs );
1617 for (i = 0; i < handle_count; i++)
1619 msi_dialog_check_messages( wait_handles[i] );
1620 CloseHandle( wait_handles[i] );
1622 free( wait_handles );
1624 EnterCriticalSection( &custom_action_cs );
1625 LIST_FOR_EACH_ENTRY_SAFE( info, cursor, &pending_custom_actions, custom_action_info, entry )
1627 if (info->package == package)
1628 free_custom_action_data( info );
1630 LeaveCriticalSection( &custom_action_cs );
1633 UINT __cdecl s_remote_GetActionInfo(const GUID *guid, WCHAR **name, int *type, WCHAR **dll, char **func, MSIHANDLE *hinst)
1635 custom_action_info *info;
1637 info = find_action_by_guid(guid);
1638 if (!info)
1639 return ERROR_INVALID_DATA;
1641 *name = wcsdup(info->action);
1642 *type = info->type;
1643 *hinst = alloc_msihandle(&info->package->hdr);
1644 *dll = wcsdup(info->source);
1645 *func = strdupWtoA(info->target);
1647 return ERROR_SUCCESS;