mfmediaengine: Remove unnecessary import library.
[wine.git] / dlls / msi / custom.c
blobee4f59b5faa08f88cbd11f0faadff365d198c7bf
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/heap.h"
40 #include "wine/debug.h"
41 #include "wine/exception.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(msi);
45 #define CUSTOM_ACTION_TYPE_MASK 0x3F
47 typedef struct tagMSIRUNNINGACTION
49 struct list entry;
50 HANDLE handle;
51 BOOL process;
52 LPWSTR name;
53 } MSIRUNNINGACTION;
55 typedef UINT (WINAPI *MsiCustomActionEntryPoint)( MSIHANDLE );
57 static CRITICAL_SECTION msi_custom_action_cs;
58 static CRITICAL_SECTION_DEBUG msi_custom_action_cs_debug =
60 0, 0, &msi_custom_action_cs,
61 { &msi_custom_action_cs_debug.ProcessLocksList,
62 &msi_custom_action_cs_debug.ProcessLocksList },
63 0, 0, { (DWORD_PTR)(__FILE__ ": msi_custom_action_cs") }
65 static CRITICAL_SECTION msi_custom_action_cs = { &msi_custom_action_cs_debug, -1, 0, 0, 0, 0 };
67 static struct list msi_pending_custom_actions = LIST_INIT( msi_pending_custom_actions );
69 void __RPC_FAR * __RPC_USER MIDL_user_allocate(SIZE_T len)
71 return heap_alloc(len);
74 void __RPC_USER MIDL_user_free(void __RPC_FAR * ptr)
76 heap_free(ptr);
79 LONG WINAPI rpc_filter(EXCEPTION_POINTERS *eptr)
81 return I_RpcExceptionFilter(eptr->ExceptionRecord->ExceptionCode);
84 UINT msi_schedule_action( MSIPACKAGE *package, UINT script, const WCHAR *action )
86 UINT count;
87 WCHAR **newbuf = NULL;
89 if (script >= SCRIPT_MAX)
91 FIXME("Unknown script requested %u\n", script);
92 return ERROR_FUNCTION_FAILED;
94 TRACE("Scheduling action %s in script %u\n", debugstr_w(action), script);
96 count = package->script_actions_count[script];
97 package->script_actions_count[script]++;
98 if (count != 0) newbuf = msi_realloc( package->script_actions[script],
99 package->script_actions_count[script] * sizeof(WCHAR *) );
100 else newbuf = msi_alloc( sizeof(WCHAR *) );
102 newbuf[count] = strdupW( action );
103 package->script_actions[script] = newbuf;
104 return ERROR_SUCCESS;
107 UINT msi_register_unique_action( MSIPACKAGE *package, const WCHAR *action )
109 UINT count;
110 WCHAR **newbuf = NULL;
112 TRACE("Registering %s as unique action\n", debugstr_w(action));
114 count = package->unique_actions_count;
115 package->unique_actions_count++;
116 if (count != 0) newbuf = msi_realloc( package->unique_actions,
117 package->unique_actions_count * sizeof(WCHAR *) );
118 else newbuf = msi_alloc( sizeof(WCHAR *) );
120 newbuf[count] = strdupW( action );
121 package->unique_actions = newbuf;
122 return ERROR_SUCCESS;
125 BOOL msi_action_is_unique( const MSIPACKAGE *package, const WCHAR *action )
127 UINT i;
129 for (i = 0; i < package->unique_actions_count; i++)
131 if (!wcscmp( package->unique_actions[i], action )) return TRUE;
133 return FALSE;
136 static BOOL check_execution_scheduling_options(MSIPACKAGE *package, LPCWSTR action, UINT options)
138 if ((options & msidbCustomActionTypeClientRepeat) ==
139 msidbCustomActionTypeClientRepeat)
141 if (!(package->InWhatSequence & SEQUENCE_UI &&
142 package->InWhatSequence & SEQUENCE_EXEC))
144 TRACE("Skipping action due to dbCustomActionTypeClientRepeat option.\n");
145 return FALSE;
148 else if (options & msidbCustomActionTypeFirstSequence)
150 if (package->InWhatSequence & SEQUENCE_UI &&
151 package->InWhatSequence & SEQUENCE_EXEC )
153 TRACE("Skipping action due to msidbCustomActionTypeFirstSequence option.\n");
154 return FALSE;
157 else if (options & msidbCustomActionTypeOncePerProcess)
159 if (msi_action_is_unique(package, action))
161 TRACE("Skipping action due to msidbCustomActionTypeOncePerProcess option.\n");
162 return FALSE;
164 else
165 msi_register_unique_action(package, action);
168 return TRUE;
171 /* stores the following properties before the action:
173 * [CustomActionData<=>UserSID<=>ProductCode]Action
175 static LPWSTR msi_get_deferred_action(LPCWSTR action, LPCWSTR actiondata,
176 LPCWSTR usersid, LPCWSTR prodcode)
178 LPWSTR deferred;
179 DWORD len;
181 if (!actiondata)
182 return strdupW(action);
184 len = lstrlenW(action) + lstrlenW(actiondata) +
185 lstrlenW(usersid) + lstrlenW(prodcode) +
186 lstrlenW(L"[%s<=>%s<=>%s]%s") - 7;
187 deferred = msi_alloc(len * sizeof(WCHAR));
189 swprintf(deferred, len, L"[%s<=>%s<=>%s]%s", actiondata, usersid, prodcode, action);
190 return deferred;
193 static void set_deferred_action_props( MSIPACKAGE *package, const WCHAR *deferred_data )
195 const WCHAR *end, *beg = deferred_data + 1;
197 end = wcsstr(beg, L"<=>");
198 msi_set_property( package->db, L"CustomActionData", beg, end - beg );
199 beg = end + 3;
201 end = wcsstr(beg, L"<=>");
202 msi_set_property( package->db, L"UserSID", beg, end - beg );
203 beg = end + 3;
205 end = wcschr(beg, ']');
206 msi_set_property( package->db, L"ProductCode", beg, end - beg );
209 WCHAR *msi_create_temp_file( MSIDATABASE *db )
211 WCHAR *ret;
213 if (!db->tempfolder)
215 WCHAR tmp[MAX_PATH];
216 UINT len = ARRAY_SIZE( tmp );
218 if (msi_get_property( db, L"TempFolder", tmp, &len ) ||
219 GetFileAttributesW( tmp ) != FILE_ATTRIBUTE_DIRECTORY)
221 GetTempPathW( MAX_PATH, tmp );
223 if (!(db->tempfolder = strdupW( tmp ))) return NULL;
226 if ((ret = msi_alloc( (lstrlenW( db->tempfolder ) + 20) * sizeof(WCHAR) )))
228 if (!GetTempFileNameW( db->tempfolder, L"msi", 0, ret ))
230 msi_free( ret );
231 return NULL;
235 return ret;
238 static MSIBINARY *create_temp_binary(MSIPACKAGE *package, LPCWSTR source)
240 MSIRECORD *row;
241 MSIBINARY *binary = NULL;
242 HANDLE file;
243 CHAR buffer[1024];
244 WCHAR *tmpfile;
245 DWORD sz, write;
246 UINT r;
248 if (!(tmpfile = msi_create_temp_file( package->db ))) return NULL;
250 if (!(row = MSI_QueryGetRecord( package->db, L"SELECT * FROM `Binary` WHERE `Name` = '%s'", source ))) goto error;
251 if (!(binary = msi_alloc_zero( sizeof(MSIBINARY) ))) goto error;
253 file = CreateFileW( tmpfile, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
254 if (file == INVALID_HANDLE_VALUE) goto error;
258 sz = sizeof(buffer);
259 r = MSI_RecordReadStream( row, 2, buffer, &sz );
260 if (r != ERROR_SUCCESS)
262 ERR("Failed to get stream\n");
263 break;
265 WriteFile( file, buffer, sz, &write, NULL );
266 } while (sz == sizeof buffer);
268 CloseHandle( file );
269 if (r != ERROR_SUCCESS) goto error;
271 binary->source = strdupW( source );
272 binary->tmpfile = tmpfile;
273 list_add_tail( &package->binaries, &binary->entry );
275 msiobj_release( &row->hdr );
276 return binary;
278 error:
279 if (row) msiobj_release( &row->hdr );
280 DeleteFileW( tmpfile );
281 msi_free( tmpfile );
282 msi_free( binary );
283 return NULL;
286 static MSIBINARY *get_temp_binary(MSIPACKAGE *package, LPCWSTR source)
288 MSIBINARY *binary;
290 LIST_FOR_EACH_ENTRY( binary, &package->binaries, MSIBINARY, entry )
292 if (!wcscmp( binary->source, source ))
293 return binary;
296 return create_temp_binary(package, source);
299 static void file_running_action(MSIPACKAGE* package, HANDLE Handle,
300 BOOL process, LPCWSTR name)
302 MSIRUNNINGACTION *action;
304 action = msi_alloc( sizeof(MSIRUNNINGACTION) );
306 action->handle = Handle;
307 action->process = process;
308 action->name = strdupW(name);
310 list_add_tail( &package->RunningActions, &action->entry );
313 static UINT custom_get_process_return( HANDLE process )
315 DWORD rc = 0;
317 GetExitCodeProcess( process, &rc );
318 TRACE("exit code is %u\n", rc);
319 if (rc != 0)
320 return ERROR_FUNCTION_FAILED;
321 return ERROR_SUCCESS;
324 static UINT custom_get_thread_return( MSIPACKAGE *package, HANDLE thread )
326 DWORD rc = 0;
328 GetExitCodeThread( thread, &rc );
330 switch (rc)
332 case ERROR_FUNCTION_NOT_CALLED:
333 case ERROR_SUCCESS:
334 case ERROR_INSTALL_USEREXIT:
335 case ERROR_INSTALL_FAILURE:
336 return rc;
337 case ERROR_NO_MORE_ITEMS:
338 return ERROR_SUCCESS;
339 case ERROR_INSTALL_SUSPEND:
340 ACTION_ForceReboot( package );
341 return ERROR_SUCCESS;
342 default:
343 ERR("Invalid Return Code %d\n",rc);
344 return ERROR_INSTALL_FAILURE;
348 static UINT wait_process_handle(MSIPACKAGE* package, UINT type,
349 HANDLE ProcessHandle, LPCWSTR name)
351 UINT rc = ERROR_SUCCESS;
353 if (!(type & msidbCustomActionTypeAsync))
355 TRACE("waiting for %s\n", debugstr_w(name));
357 msi_dialog_check_messages(ProcessHandle);
359 if (!(type & msidbCustomActionTypeContinue))
360 rc = custom_get_process_return(ProcessHandle);
362 CloseHandle(ProcessHandle);
364 else
366 TRACE("%s running in background\n", debugstr_w(name));
368 if (!(type & msidbCustomActionTypeContinue))
369 file_running_action(package, ProcessHandle, TRUE, name);
370 else
371 CloseHandle(ProcessHandle);
374 return rc;
377 typedef struct _msi_custom_action_info {
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 } msi_custom_action_info;
389 static void free_custom_action_data( msi_custom_action_info *info )
391 EnterCriticalSection( &msi_custom_action_cs );
393 list_remove( &info->entry );
394 if (info->handle)
395 CloseHandle( info->handle );
396 msi_free( info->action );
397 msi_free( info->source );
398 msi_free( info->target );
399 msiobj_release( &info->package->hdr );
400 msi_free( info );
402 LeaveCriticalSection( &msi_custom_action_cs );
405 static UINT wait_thread_handle( msi_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 msi_custom_action_info *find_action_by_guid( const GUID *guid )
430 msi_custom_action_info *info;
431 BOOL found = FALSE;
433 EnterCriticalSection( &msi_custom_action_cs );
435 LIST_FOR_EACH_ENTRY( info, &msi_pending_custom_actions, msi_custom_action_info, entry )
437 if (IsEqualGUID( &info->guid, guid ))
439 found = TRUE;
440 break;
444 LeaveCriticalSection( &msi_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: %#x\n", status);
511 return status;
513 status = RpcBindingFromStringBindingW(binding_str, &rpc_handle);
514 if (status != RPC_S_OK)
516 ERR("RpcBindingFromStringBinding failed: %#x\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 %x\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 (%u)\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: %08x\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 DWORD custom_start_server(MSIPACKAGE *package, DWORD arch)
578 WCHAR path[MAX_PATH], cmdline[MAX_PATH + 23];
579 PROCESS_INFORMATION pi = {0};
580 STARTUPINFOW si = {0};
581 WCHAR buffer[24];
582 void *cookie;
583 HANDLE pipe;
585 if ((arch == SCS_32BIT_BINARY && package->custom_server_32_process) ||
586 (arch == SCS_64BIT_BINARY && package->custom_server_64_process))
587 return ERROR_SUCCESS;
589 swprintf(buffer, ARRAY_SIZE(buffer), L"\\\\.\\pipe\\msica_%x_%d",
590 GetCurrentProcessId(), arch == SCS_32BIT_BINARY ? 32 : 64);
591 pipe = CreateNamedPipeW(buffer, PIPE_ACCESS_DUPLEX, 0, 1, sizeof(DWORD64),
592 sizeof(GUID), 0, NULL);
593 if (pipe == INVALID_HANDLE_VALUE)
594 ERR("Failed to create custom action client pipe: %u\n", GetLastError());
596 if ((sizeof(void *) == 8 || is_wow64) && arch == SCS_32BIT_BINARY)
597 GetSystemWow64DirectoryW(path, MAX_PATH - ARRAY_SIZE(L"\\msiexec.exe"));
598 else
599 GetSystemDirectoryW(path, MAX_PATH - ARRAY_SIZE(L"\\msiexec.exe"));
600 lstrcatW(path, L"\\msiexec.exe");
601 swprintf(cmdline, ARRAY_SIZE(cmdline), L"%s -Embedding %d", path, GetCurrentProcessId());
603 if (is_wow64 && arch == SCS_64BIT_BINARY)
605 Wow64DisableWow64FsRedirection(&cookie);
606 CreateProcessW(path, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
607 Wow64RevertWow64FsRedirection(cookie);
609 else
610 CreateProcessW(path, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
612 CloseHandle(pi.hThread);
614 if (arch == SCS_32BIT_BINARY)
616 package->custom_server_32_process = pi.hProcess;
617 package->custom_server_32_pipe = pipe;
619 else
621 package->custom_server_64_process = pi.hProcess;
622 package->custom_server_64_pipe = pipe;
625 if (!ConnectNamedPipe(pipe, NULL))
627 ERR("Failed to connect to custom action server: %u\n", GetLastError());
628 return GetLastError();
631 return ERROR_SUCCESS;
634 void custom_stop_server(HANDLE process, HANDLE pipe)
636 DWORD size;
638 WriteFile(pipe, &GUID_NULL, sizeof(GUID_NULL), &size, NULL);
639 WaitForSingleObject(process, INFINITE);
640 CloseHandle(process);
641 CloseHandle(pipe);
644 static DWORD WINAPI custom_client_thread(void *arg)
646 msi_custom_action_info *info = arg;
647 DWORD64 thread64;
648 HANDLE process;
649 HANDLE thread;
650 HANDLE pipe;
651 DWORD size;
652 UINT rc;
654 CoInitializeEx(NULL, COINIT_MULTITHREADED); /* needed to marshal streams */
656 if (info->arch == SCS_32BIT_BINARY)
658 process = info->package->custom_server_32_process;
659 pipe = info->package->custom_server_32_pipe;
661 else
663 process = info->package->custom_server_64_process;
664 pipe = info->package->custom_server_64_pipe;
667 EnterCriticalSection(&msi_custom_action_cs);
669 if (!WriteFile(pipe, &info->guid, sizeof(info->guid), &size, NULL) ||
670 size != sizeof(info->guid))
672 ERR("Failed to write to custom action client pipe: %u\n", GetLastError());
673 LeaveCriticalSection(&msi_custom_action_cs);
674 return GetLastError();
676 if (!ReadFile(pipe, &thread64, sizeof(thread64), &size, NULL) || size != sizeof(thread64))
678 ERR("Failed to read from custom action client pipe: %u\n", GetLastError());
679 LeaveCriticalSection(&msi_custom_action_cs);
680 return GetLastError();
683 LeaveCriticalSection(&msi_custom_action_cs);
685 if (DuplicateHandle(process, (HANDLE)(DWORD_PTR)thread64, GetCurrentProcess(),
686 &thread, 0, FALSE, DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE))
688 WaitForSingleObject(thread, INFINITE);
689 GetExitCodeThread(thread, &rc);
690 CloseHandle(thread);
692 else
693 rc = GetLastError();
695 CoUninitialize();
696 return rc;
699 /* based on kernel32.GetBinaryTypeW() */
700 static BOOL get_binary_type( const WCHAR *name, DWORD *type )
702 HANDLE hfile, mapping;
703 NTSTATUS status;
705 hfile = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
706 if (hfile == INVALID_HANDLE_VALUE)
707 return FALSE;
709 status = NtCreateSection( &mapping, STANDARD_RIGHTS_REQUIRED | SECTION_QUERY, NULL, NULL, PAGE_READONLY,
710 SEC_IMAGE, hfile );
711 CloseHandle( hfile );
713 switch (status)
715 case STATUS_SUCCESS:
717 SECTION_IMAGE_INFORMATION info;
719 status = NtQuerySection( mapping, SectionImageInformation, &info, sizeof(info), NULL );
720 CloseHandle( mapping );
721 if (status) return FALSE;
722 switch (info.Machine)
724 case IMAGE_FILE_MACHINE_I386:
725 case IMAGE_FILE_MACHINE_ARMNT:
726 *type = SCS_32BIT_BINARY;
727 return TRUE;
728 case IMAGE_FILE_MACHINE_AMD64:
729 case IMAGE_FILE_MACHINE_ARM64:
730 *type = SCS_64BIT_BINARY;
731 return TRUE;
732 default:
733 return FALSE;
736 case STATUS_INVALID_IMAGE_WIN_64:
737 *type = SCS_64BIT_BINARY;
738 return TRUE;
739 default:
740 return FALSE;
744 static msi_custom_action_info *do_msidbCustomActionTypeDll(
745 MSIPACKAGE *package, INT type, LPCWSTR source, LPCWSTR target, LPCWSTR action )
747 msi_custom_action_info *info;
748 RPC_STATUS status;
749 BOOL ret;
751 info = msi_alloc( sizeof *info );
752 if (!info)
753 return NULL;
755 msiobj_addref( &package->hdr );
756 info->package = package;
757 info->type = type;
758 info->target = strdupW( target );
759 info->source = strdupW( source );
760 info->action = strdupW( action );
761 CoCreateGuid( &info->guid );
763 EnterCriticalSection( &msi_custom_action_cs );
764 list_add_tail( &msi_pending_custom_actions, &info->entry );
765 LeaveCriticalSection( &msi_custom_action_cs );
767 if (!package->rpc_server_started)
769 WCHAR endpoint[12];
771 swprintf(endpoint, ARRAY_SIZE(endpoint), L"msi%x", GetCurrentProcessId());
772 status = RpcServerUseProtseqEpW((WCHAR *)L"ncalrpc", RPC_C_PROTSEQ_MAX_REQS_DEFAULT,
773 endpoint, NULL);
774 if (status != RPC_S_OK)
776 ERR("RpcServerUseProtseqEp failed: %#x\n", status);
777 return NULL;
780 status = RpcServerRegisterIfEx(s_IWineMsiRemote_v0_0_s_ifspec, NULL, NULL,
781 RPC_IF_AUTOLISTEN, RPC_C_LISTEN_MAX_CALLS_DEFAULT, NULL);
782 if (status != RPC_S_OK)
784 ERR("RpcServerRegisterIfEx failed: %#x\n", status);
785 return NULL;
788 info->package->rpc_server_started = 1;
791 ret = get_binary_type(source, &info->arch);
792 if (!ret)
793 info->arch = (sizeof(void *) == 8 ? SCS_64BIT_BINARY : SCS_32BIT_BINARY);
795 if (info->arch == SCS_64BIT_BINARY && sizeof(void *) == 4 && !is_wow64)
797 ERR("Attempt to run a 64-bit custom action inside a 32-bit WINEPREFIX.\n");
798 free_custom_action_data( info );
799 return NULL;
802 custom_start_server(package, info->arch);
804 info->handle = CreateThread(NULL, 0, custom_client_thread, info, 0, NULL);
805 if (!info->handle)
807 free_custom_action_data( info );
808 return NULL;
811 return info;
814 static UINT HANDLE_CustomType1( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
815 INT type, const WCHAR *action )
817 msi_custom_action_info *info;
818 MSIBINARY *binary;
820 if (!(binary = get_temp_binary(package, source)))
821 return ERROR_FUNCTION_FAILED;
823 TRACE("Calling function %s from %s\n", debugstr_w(target), debugstr_w(binary->tmpfile));
825 if (!(info = do_msidbCustomActionTypeDll( package, type, binary->tmpfile, target, action )))
826 return ERROR_FUNCTION_FAILED;
827 return wait_thread_handle( info );
830 static HANDLE execute_command( const WCHAR *app, WCHAR *arg, const WCHAR *dir )
832 STARTUPINFOW si;
833 PROCESS_INFORMATION info;
834 WCHAR *exe = NULL, *cmd = NULL, *p;
835 BOOL ret;
837 if (app)
839 int len_arg = 0;
840 DWORD len_exe;
842 if (!(exe = msi_alloc( MAX_PATH * sizeof(WCHAR) ))) return INVALID_HANDLE_VALUE;
843 len_exe = SearchPathW( NULL, app, L".exe", MAX_PATH, exe, NULL );
844 if (len_exe >= MAX_PATH)
846 msi_free( exe );
847 if (!(exe = msi_alloc( len_exe * sizeof(WCHAR) ))) return INVALID_HANDLE_VALUE;
848 len_exe = SearchPathW( NULL, app, L".exe", len_exe, exe, NULL );
850 if (!len_exe)
852 ERR("can't find executable %u\n", GetLastError());
853 msi_free( exe );
854 return INVALID_HANDLE_VALUE;
857 if (arg) len_arg = lstrlenW( arg );
858 if (!(cmd = msi_alloc( (len_exe + len_arg + 4) * sizeof(WCHAR) )))
860 msi_free( exe );
861 return INVALID_HANDLE_VALUE;
863 p = cmd;
864 if (wcschr( exe, ' ' ))
866 *p++ = '\"';
867 memcpy( p, exe, len_exe * sizeof(WCHAR) );
868 p += len_exe;
869 *p++ = '\"';
870 *p = 0;
872 else
874 lstrcpyW( p, exe );
875 p += len_exe;
877 if (arg)
879 *p++ = ' ';
880 memcpy( p, arg, len_arg * sizeof(WCHAR) );
881 p[len_arg] = 0;
884 memset( &si, 0, sizeof(STARTUPINFOW) );
885 ret = CreateProcessW( exe, exe ? cmd : arg, NULL, NULL, FALSE, 0, NULL, dir, &si, &info );
886 msi_free( cmd );
887 msi_free( exe );
888 if (!ret)
890 ERR("unable to execute command %u\n", GetLastError());
891 return INVALID_HANDLE_VALUE;
893 CloseHandle( info.hThread );
894 return info.hProcess;
897 static UINT HANDLE_CustomType2( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
898 INT type, const WCHAR *action )
900 MSIBINARY *binary;
901 HANDLE handle;
902 WCHAR *arg;
904 if (!(binary = get_temp_binary(package, source)))
905 return ERROR_FUNCTION_FAILED;
907 deformat_string( package, target, &arg );
908 TRACE("exe %s arg %s\n", debugstr_w(binary->tmpfile), debugstr_w(arg));
910 handle = execute_command( binary->tmpfile, arg, L"C:\\" );
911 msi_free( arg );
912 if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
913 return wait_process_handle( package, type, handle, action );
916 static UINT HANDLE_CustomType17( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
917 INT type, const WCHAR *action )
919 msi_custom_action_info *info;
920 MSIFILE *file;
922 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
924 file = msi_get_loaded_file( package, source );
925 if (!file)
927 ERR("invalid file key %s\n", debugstr_w( source ));
928 return ERROR_FUNCTION_FAILED;
931 if (!(info = do_msidbCustomActionTypeDll( package, type, file->TargetPath, target, action )))
932 return ERROR_FUNCTION_FAILED;
933 return wait_thread_handle( info );
936 static UINT HANDLE_CustomType18( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
937 INT type, const WCHAR *action )
939 MSIFILE *file;
940 HANDLE handle;
941 WCHAR *arg;
943 if (!(file = msi_get_loaded_file( package, source ))) return ERROR_FUNCTION_FAILED;
945 deformat_string( package, target, &arg );
946 TRACE("exe %s arg %s\n", debugstr_w(file->TargetPath), debugstr_w(arg));
948 handle = execute_command( file->TargetPath, arg, L"C:\\" );
949 msi_free( arg );
950 if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
951 return wait_process_handle( package, type, handle, action );
954 static UINT HANDLE_CustomType19( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
955 INT type, const WCHAR *action )
957 MSIRECORD *row = 0;
958 LPWSTR deformated = NULL;
960 deformat_string( package, target, &deformated );
962 /* first try treat the error as a number */
963 row = MSI_QueryGetRecord( package->db, L"SELECT `Message` FROM `Error` WHERE `Error` = '%s'", deformated );
964 if( row )
966 LPCWSTR error = MSI_RecordGetString( row, 1 );
967 if ((package->ui_level & INSTALLUILEVEL_MASK) != INSTALLUILEVEL_NONE)
968 MessageBoxW( NULL, error, NULL, MB_OK );
969 msiobj_release( &row->hdr );
971 else if ((package->ui_level & INSTALLUILEVEL_MASK) != INSTALLUILEVEL_NONE)
972 MessageBoxW( NULL, deformated, NULL, MB_OK );
974 msi_free( deformated );
976 return ERROR_INSTALL_FAILURE;
979 static WCHAR *build_msiexec_args( const WCHAR *filename, const WCHAR *params )
981 UINT len_filename = lstrlenW( filename ), len_params = lstrlenW( params );
982 UINT len = ARRAY_SIZE(L"/qb /i ") - 1;
983 WCHAR *ret;
985 if (!(ret = msi_alloc( (len + len_filename + len_params + 4) * sizeof(WCHAR) ))) return NULL;
986 memcpy( ret, L"/qb /i ", sizeof(L"/qb /i ") );
987 ret[len++] = '"';
988 memcpy( ret + len, filename, len_filename * sizeof(WCHAR) );
989 len += len_filename;
990 ret[len++] = '"';
991 ret[len++] = ' ';
992 lstrcpyW( ret + len, params );
993 return ret;
996 static UINT HANDLE_CustomType23( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
997 INT type, const WCHAR *action )
999 WCHAR *dir, *filename, *args, *p;
1000 UINT len_dir, len_source = lstrlenW( source );
1001 HANDLE handle;
1003 if (!(dir = msi_dup_property( package->db, L"OriginalDatabase" ))) return ERROR_OUTOFMEMORY;
1004 if (!(p = wcsrchr( dir, '\\' )) && !(p = wcsrchr( dir, '/' )))
1006 msi_free( dir );
1007 return ERROR_FUNCTION_FAILED;
1009 *p = 0;
1010 len_dir = p - dir;
1011 if (!(filename = msi_alloc( (len_dir + len_source + 2) * sizeof(WCHAR) )))
1013 msi_free( dir );
1014 return ERROR_OUTOFMEMORY;
1016 memcpy( filename, dir, len_dir * sizeof(WCHAR) );
1017 filename[len_dir++] = '\\';
1018 memcpy( filename + len_dir, source, len_source * sizeof(WCHAR) );
1019 filename[len_dir + len_source] = 0;
1021 if (!(args = build_msiexec_args( filename, target )))
1023 msi_free( dir );
1024 return ERROR_OUTOFMEMORY;
1027 TRACE("installing %s concurrently\n", debugstr_w(source));
1029 handle = execute_command( L"msiexec", args, dir );
1030 msi_free( dir );
1031 msi_free( args );
1032 if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
1033 return wait_process_handle( package, type, handle, action );
1036 static UINT write_substorage_to_file( MSIPACKAGE *package, const WCHAR *source, const WCHAR *filename )
1038 IStorage *src = NULL, *dst = NULL;
1039 UINT r = ERROR_FUNCTION_FAILED;
1040 HRESULT hr;
1042 hr = StgCreateDocfile( filename, STGM_CREATE|STGM_TRANSACTED|STGM_WRITE|STGM_SHARE_EXCLUSIVE, 0, &dst );
1043 if (FAILED( hr ))
1045 WARN( "can't open destination storage %s (%08x)\n", debugstr_w(filename), hr );
1046 goto done;
1049 hr = IStorage_OpenStorage( package->db->storage, source, NULL, STGM_SHARE_EXCLUSIVE, NULL, 0, &src );
1050 if (FAILED( hr ))
1052 WARN( "can't open source storage %s (%08x)\n", debugstr_w(source), hr );
1053 goto done;
1056 hr = IStorage_CopyTo( src, 0, NULL, NULL, dst );
1057 if (FAILED( hr ))
1059 ERR( "failed to copy storage %s (%08x)\n", debugstr_w(source), hr );
1060 goto done;
1063 hr = IStorage_Commit( dst, 0 );
1064 if (FAILED( hr ))
1065 ERR( "failed to commit storage (%08x)\n", hr );
1066 else
1067 r = ERROR_SUCCESS;
1069 done:
1070 if (src) IStorage_Release( src );
1071 if (dst) IStorage_Release( dst );
1072 return r;
1075 static UINT HANDLE_CustomType7( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
1076 INT type, const WCHAR *action )
1078 WCHAR *tmpfile, *args;
1079 MSIBINARY *binary = NULL;
1080 HANDLE handle;
1081 UINT r;
1083 if (!(tmpfile = msi_create_temp_file( package->db ))) return ERROR_FUNCTION_FAILED;
1085 r = write_substorage_to_file( package, source, tmpfile );
1086 if (r != ERROR_SUCCESS)
1087 goto error;
1089 if (!(binary = msi_alloc( sizeof(*binary) ))) goto error;
1090 binary->source = NULL;
1091 binary->tmpfile = tmpfile;
1092 list_add_tail( &package->binaries, &binary->entry );
1094 if (!(args = build_msiexec_args( tmpfile, target ))) return ERROR_OUTOFMEMORY;
1096 TRACE("installing %s concurrently\n", debugstr_w(source));
1098 handle = execute_command( L"msiexec", args, L"C:\\" );
1099 msi_free( args );
1100 if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
1101 return wait_process_handle( package, type, handle, action );
1103 error:
1104 DeleteFileW( tmpfile );
1105 msi_free( tmpfile );
1106 return ERROR_FUNCTION_FAILED;
1109 static UINT HANDLE_CustomType50( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
1110 INT type, const WCHAR *action )
1112 WCHAR *exe, *arg;
1113 HANDLE handle;
1115 if (!(exe = msi_dup_property( package->db, source ))) return ERROR_SUCCESS;
1117 deformat_string( package, target, &arg );
1118 TRACE("exe %s arg %s\n", debugstr_w(exe), debugstr_w(arg));
1120 handle = execute_command( exe, arg, L"C:\\" );
1121 msi_free( exe );
1122 msi_free( arg );
1123 if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
1124 return wait_process_handle( package, type, handle, action );
1127 static UINT HANDLE_CustomType34( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
1128 INT type, const WCHAR *action )
1130 const WCHAR *workingdir = NULL;
1131 HANDLE handle;
1132 WCHAR *cmd;
1134 if (source)
1136 workingdir = msi_get_target_folder( package, source );
1137 if (!workingdir) return ERROR_FUNCTION_FAILED;
1139 deformat_string( package, target, &cmd );
1140 if (!cmd) return ERROR_FUNCTION_FAILED;
1142 TRACE("cmd %s dir %s\n", debugstr_w(cmd), debugstr_w(workingdir));
1144 handle = execute_command( NULL, cmd, workingdir );
1145 msi_free( cmd );
1146 if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
1147 return wait_process_handle( package, type, handle, action );
1150 static DWORD ACTION_CallScript( const GUID *guid )
1152 msi_custom_action_info *info;
1153 MSIHANDLE hPackage;
1154 UINT r = ERROR_FUNCTION_FAILED;
1156 info = find_action_by_guid( guid );
1157 if (!info)
1159 ERR("failed to find action %s\n", debugstr_guid( guid) );
1160 return ERROR_FUNCTION_FAILED;
1163 TRACE("function %s, script %s\n", debugstr_w( info->target ), debugstr_w( info->source ) );
1165 hPackage = alloc_msihandle( &info->package->hdr );
1166 if (hPackage)
1168 r = call_script( hPackage, info->type, info->source, info->target, info->action );
1169 TRACE("script returned %u\n", r);
1170 MsiCloseHandle( hPackage );
1172 else
1173 ERR("failed to create handle for %p\n", info->package );
1175 return r;
1178 static DWORD WINAPI ScriptThread( LPVOID arg )
1180 LPGUID guid = arg;
1181 DWORD rc;
1183 TRACE("custom action (%x) started\n", GetCurrentThreadId() );
1185 rc = ACTION_CallScript( guid );
1187 TRACE("custom action (%x) returned %i\n", GetCurrentThreadId(), rc );
1189 MsiCloseAllHandles();
1190 return rc;
1193 static msi_custom_action_info *do_msidbCustomActionTypeScript(
1194 MSIPACKAGE *package, INT type, LPCWSTR script, LPCWSTR function, LPCWSTR action )
1196 msi_custom_action_info *info;
1198 info = msi_alloc( sizeof *info );
1199 if (!info)
1200 return NULL;
1202 msiobj_addref( &package->hdr );
1203 info->package = package;
1204 info->type = type;
1205 info->target = strdupW( function );
1206 info->source = strdupW( script );
1207 info->action = strdupW( action );
1208 CoCreateGuid( &info->guid );
1210 EnterCriticalSection( &msi_custom_action_cs );
1211 list_add_tail( &msi_pending_custom_actions, &info->entry );
1212 LeaveCriticalSection( &msi_custom_action_cs );
1214 info->handle = CreateThread( NULL, 0, ScriptThread, &info->guid, 0, NULL );
1215 if (!info->handle)
1217 free_custom_action_data( info );
1218 return NULL;
1221 return info;
1224 static UINT HANDLE_CustomType37_38( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
1225 INT type, const WCHAR *action )
1227 msi_custom_action_info *info;
1229 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1231 info = do_msidbCustomActionTypeScript( package, type, target, NULL, action );
1232 return wait_thread_handle( info );
1235 static UINT HANDLE_CustomType5_6( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
1236 INT type, const WCHAR *action )
1238 MSIRECORD *row = NULL;
1239 msi_custom_action_info *info;
1240 CHAR *buffer = NULL;
1241 WCHAR *bufferw = NULL;
1242 DWORD sz = 0;
1243 UINT r;
1245 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1247 row = MSI_QueryGetRecord(package->db, L"SELECT * FROM `Binary` WHERE `Name` = '%s'", source);
1248 if (!row)
1249 return ERROR_FUNCTION_FAILED;
1251 r = MSI_RecordReadStream(row, 2, NULL, &sz);
1252 if (r != ERROR_SUCCESS) goto done;
1254 buffer = msi_alloc( sz + 1 );
1255 if (!buffer)
1257 r = ERROR_FUNCTION_FAILED;
1258 goto done;
1261 r = MSI_RecordReadStream(row, 2, buffer, &sz);
1262 if (r != ERROR_SUCCESS)
1263 goto done;
1265 buffer[sz] = 0;
1266 bufferw = strdupAtoW(buffer);
1267 if (!bufferw)
1269 r = ERROR_FUNCTION_FAILED;
1270 goto done;
1273 info = do_msidbCustomActionTypeScript( package, type, bufferw, target, action );
1274 r = wait_thread_handle( info );
1276 done:
1277 msi_free(bufferw);
1278 msi_free(buffer);
1279 msiobj_release(&row->hdr);
1280 return r;
1283 static UINT HANDLE_CustomType21_22( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
1284 INT type, const WCHAR *action )
1286 msi_custom_action_info *info;
1287 MSIFILE *file;
1288 HANDLE hFile;
1289 DWORD sz, szHighWord = 0, read;
1290 CHAR *buffer=NULL;
1291 WCHAR *bufferw=NULL;
1292 BOOL bRet;
1293 UINT r;
1295 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1297 file = msi_get_loaded_file(package, source);
1298 if (!file)
1300 ERR("invalid file key %s\n", debugstr_w(source));
1301 return ERROR_FUNCTION_FAILED;
1304 hFile = msi_create_file( package, file->TargetPath, GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, 0 );
1305 if (hFile == INVALID_HANDLE_VALUE) return ERROR_FUNCTION_FAILED;
1307 sz = GetFileSize(hFile, &szHighWord);
1308 if (sz == INVALID_FILE_SIZE || szHighWord != 0)
1310 CloseHandle(hFile);
1311 return ERROR_FUNCTION_FAILED;
1313 buffer = msi_alloc( sz + 1 );
1314 if (!buffer)
1316 CloseHandle(hFile);
1317 return ERROR_FUNCTION_FAILED;
1319 bRet = ReadFile(hFile, buffer, sz, &read, NULL);
1320 CloseHandle(hFile);
1321 if (!bRet)
1323 r = ERROR_FUNCTION_FAILED;
1324 goto done;
1326 buffer[read] = 0;
1327 bufferw = strdupAtoW(buffer);
1328 if (!bufferw)
1330 r = ERROR_FUNCTION_FAILED;
1331 goto done;
1333 info = do_msidbCustomActionTypeScript( package, type, bufferw, target, action );
1334 r = wait_thread_handle( info );
1336 done:
1337 msi_free(bufferw);
1338 msi_free(buffer);
1339 return r;
1342 static UINT HANDLE_CustomType53_54( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
1343 INT type, const WCHAR *action )
1345 msi_custom_action_info *info;
1346 WCHAR *prop;
1348 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1350 prop = msi_dup_property( package->db, source );
1351 if (!prop) return ERROR_SUCCESS;
1353 info = do_msidbCustomActionTypeScript( package, type, prop, NULL, action );
1354 msi_free(prop);
1355 return wait_thread_handle( info );
1358 static BOOL action_type_matches_script( UINT type, UINT script )
1360 switch (script)
1362 case SCRIPT_NONE:
1363 return FALSE;
1364 case SCRIPT_INSTALL:
1365 return !(type & msidbCustomActionTypeCommit) && !(type & msidbCustomActionTypeRollback);
1366 case SCRIPT_COMMIT:
1367 return (type & msidbCustomActionTypeCommit);
1368 case SCRIPT_ROLLBACK:
1369 return (type & msidbCustomActionTypeRollback);
1370 default:
1371 ERR("unhandled script %u\n", script);
1373 return FALSE;
1376 static UINT defer_custom_action( MSIPACKAGE *package, const WCHAR *action, UINT type )
1378 WCHAR *actiondata = msi_dup_property( package->db, action );
1379 WCHAR *usersid = msi_dup_property( package->db, L"UserSID" );
1380 WCHAR *prodcode = msi_dup_property( package->db, L"ProductCode" );
1381 WCHAR *deferred = msi_get_deferred_action( action, actiondata, usersid, prodcode );
1383 if (!deferred)
1385 msi_free( actiondata );
1386 msi_free( usersid );
1387 msi_free( prodcode );
1388 return ERROR_OUTOFMEMORY;
1390 if (type & msidbCustomActionTypeCommit)
1392 TRACE("deferring commit action\n");
1393 msi_schedule_action( package, SCRIPT_COMMIT, deferred );
1395 else if (type & msidbCustomActionTypeRollback)
1397 TRACE("deferring rollback action\n");
1398 msi_schedule_action( package, SCRIPT_ROLLBACK, deferred );
1400 else
1402 TRACE("deferring install action\n");
1403 msi_schedule_action( package, SCRIPT_INSTALL, deferred );
1406 msi_free( actiondata );
1407 msi_free( usersid );
1408 msi_free( prodcode );
1409 msi_free( deferred );
1410 return ERROR_SUCCESS;
1413 UINT ACTION_CustomAction(MSIPACKAGE *package, const WCHAR *action)
1415 UINT rc = ERROR_SUCCESS;
1416 MSIRECORD *row;
1417 UINT type;
1418 const WCHAR *source, *target, *ptr, *deferred_data = NULL;
1419 WCHAR *deformated = NULL;
1420 int len;
1422 /* deferred action: [properties]Action */
1423 if ((ptr = wcsrchr(action, ']')))
1425 deferred_data = action;
1426 action = ptr + 1;
1429 row = MSI_QueryGetRecord( package->db, L"SELECT * FROM `CustomAction` WHERE `Action` = '%s'", action );
1430 if (!row)
1431 return ERROR_FUNCTION_NOT_CALLED;
1433 type = MSI_RecordGetInteger(row,2);
1434 source = MSI_RecordGetString(row,3);
1435 target = MSI_RecordGetString(row,4);
1437 TRACE("Handling custom action %s (%x %s %s)\n",debugstr_w(action),type,
1438 debugstr_w(source), debugstr_w(target));
1440 /* handle some of the deferred actions */
1441 if (type & msidbCustomActionTypeTSAware)
1442 FIXME("msidbCustomActionTypeTSAware not handled\n");
1444 if (type & msidbCustomActionTypeInScript)
1446 if (type & msidbCustomActionTypeNoImpersonate)
1447 WARN("msidbCustomActionTypeNoImpersonate not handled\n");
1449 if (!action_type_matches_script(type, package->script))
1451 rc = defer_custom_action( package, action, type );
1452 goto end;
1454 else
1456 LPWSTR actiondata = msi_dup_property( package->db, action );
1458 if (type & msidbCustomActionTypeInScript)
1459 package->scheduled_action_running = TRUE;
1461 if (type & msidbCustomActionTypeCommit)
1462 package->commit_action_running = TRUE;
1464 if (type & msidbCustomActionTypeRollback)
1465 package->rollback_action_running = TRUE;
1467 if (deferred_data)
1468 set_deferred_action_props(package, deferred_data);
1469 else if (actiondata)
1470 msi_set_property( package->db, L"CustomActionData", actiondata, -1 );
1471 else
1472 msi_set_property( package->db, L"CustomActionData", L"", -1 );
1474 msi_free(actiondata);
1477 else if (!check_execution_scheduling_options(package,action,type))
1479 rc = ERROR_SUCCESS;
1480 goto end;
1483 switch (type & CUSTOM_ACTION_TYPE_MASK)
1485 case 1: /* DLL file stored in a Binary table stream */
1486 rc = HANDLE_CustomType1( package, source, target, type, action );
1487 break;
1488 case 2: /* EXE file stored in a Binary table stream */
1489 rc = HANDLE_CustomType2( package, source, target, type, action );
1490 break;
1491 case 5:
1492 case 6: /* JScript/VBScript file stored in a Binary table stream */
1493 rc = HANDLE_CustomType5_6( package, source, target, type, action );
1494 break;
1495 case 7: /* Concurrent install from substorage */
1496 deformat_string( package, target, &deformated );
1497 rc = HANDLE_CustomType7( package, source, target, type, action );
1498 msi_free( deformated );
1499 break;
1500 case 17:
1501 rc = HANDLE_CustomType17( package, source, target, type, action );
1502 break;
1503 case 18: /* EXE file installed with package */
1504 rc = HANDLE_CustomType18( package, source, target, type, action );
1505 break;
1506 case 19: /* Error that halts install */
1507 rc = HANDLE_CustomType19( package, source, target, type, action );
1508 break;
1509 case 21: /* JScript/VBScript file installed with the product */
1510 case 22:
1511 rc = HANDLE_CustomType21_22( package, source, target, type, action );
1512 break;
1513 case 23: /* Installs another package in the source tree */
1514 deformat_string( package, target, &deformated );
1515 rc = HANDLE_CustomType23( package, source, deformated, type, action );
1516 msi_free( deformated );
1517 break;
1518 case 34: /* EXE to be run in specified directory */
1519 rc = HANDLE_CustomType34( package, source, target, type, action );
1520 break;
1521 case 35: /* Directory set with formatted text */
1522 deformat_string( package, target, &deformated );
1523 MSI_SetTargetPathW( package, source, deformated );
1524 msi_free( deformated );
1525 break;
1526 case 37: /* JScript/VBScript text stored in target column */
1527 case 38:
1528 rc = HANDLE_CustomType37_38( package, source, target, type, action );
1529 break;
1530 case 50: /* EXE file specified by a property value */
1531 rc = HANDLE_CustomType50( package, source, target, type, action );
1532 break;
1533 case 51: /* Property set with formatted text */
1534 if (!source) break;
1535 len = deformat_string( package, target, &deformated );
1536 rc = msi_set_property( package->db, source, deformated, len );
1537 if (rc == ERROR_SUCCESS && !wcscmp( source, L"SourceDir" )) msi_reset_source_folders( package );
1538 msi_free( deformated );
1539 break;
1540 case 53: /* JScript/VBScript text specified by a property value */
1541 case 54:
1542 rc = HANDLE_CustomType53_54( package, source, target, type, action );
1543 break;
1544 default:
1545 FIXME( "unhandled action type %u (%s %s)\n", type & CUSTOM_ACTION_TYPE_MASK, debugstr_w(source),
1546 debugstr_w(target) );
1549 end:
1550 package->scheduled_action_running = FALSE;
1551 package->commit_action_running = FALSE;
1552 package->rollback_action_running = FALSE;
1553 msiobj_release(&row->hdr);
1554 return rc;
1557 void ACTION_FinishCustomActions(const MSIPACKAGE* package)
1559 struct list *item;
1560 HANDLE *wait_handles;
1561 unsigned int handle_count, i;
1562 msi_custom_action_info *info, *cursor;
1564 while ((item = list_head( &package->RunningActions )))
1566 MSIRUNNINGACTION *action = LIST_ENTRY( item, MSIRUNNINGACTION, entry );
1568 list_remove( &action->entry );
1570 TRACE("waiting for %s\n", debugstr_w( action->name ) );
1571 msi_dialog_check_messages( action->handle );
1573 CloseHandle( action->handle );
1574 msi_free( action->name );
1575 msi_free( action );
1578 EnterCriticalSection( &msi_custom_action_cs );
1580 handle_count = list_count( &msi_pending_custom_actions );
1581 wait_handles = msi_alloc( handle_count * sizeof(HANDLE) );
1583 handle_count = 0;
1584 LIST_FOR_EACH_ENTRY_SAFE( info, cursor, &msi_pending_custom_actions, msi_custom_action_info, entry )
1586 if (info->package == package )
1588 if (DuplicateHandle(GetCurrentProcess(), info->handle, GetCurrentProcess(), &wait_handles[handle_count], SYNCHRONIZE, FALSE, 0))
1589 handle_count++;
1593 LeaveCriticalSection( &msi_custom_action_cs );
1595 for (i = 0; i < handle_count; i++)
1597 msi_dialog_check_messages( wait_handles[i] );
1598 CloseHandle( wait_handles[i] );
1600 msi_free( wait_handles );
1602 EnterCriticalSection( &msi_custom_action_cs );
1603 LIST_FOR_EACH_ENTRY_SAFE( info, cursor, &msi_pending_custom_actions, msi_custom_action_info, entry )
1605 if (info->package == package)
1606 free_custom_action_data( info );
1608 LeaveCriticalSection( &msi_custom_action_cs );
1611 UINT __cdecl s_remote_GetActionInfo(const GUID *guid, WCHAR **name, int *type, WCHAR **dll, char **func, MSIHANDLE *hinst)
1613 msi_custom_action_info *info;
1615 info = find_action_by_guid(guid);
1616 if (!info)
1617 return ERROR_INVALID_DATA;
1619 *name = strdupW(info->action);
1620 *type = info->type;
1621 *hinst = alloc_msihandle(&info->package->hdr);
1622 *dll = strdupW(info->source);
1623 *func = strdupWtoA(info->target);
1625 return ERROR_SUCCESS;