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
24 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/msi/setup/summary_list_of_all_custom_action_types.asp
36 #include "wine/debug.h"
41 #include "msvcrt/fcntl.h"
48 #include "wine/unicode.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(msi);
54 #define CUSTOM_ACTION_TYPE_MASK 0x3F
55 static const WCHAR c_collen[] = {'C',':','\\',0};
56 static const WCHAR cszTempFolder[]= {'T','e','m','p','F','o','l','d','e','r',0};
58 typedef struct tagMSIRUNNINGACTION
66 static UINT HANDLE_CustomType1(MSIPACKAGE *package, LPCWSTR source,
67 LPCWSTR target, const INT type, LPCWSTR action);
68 static UINT HANDLE_CustomType2(MSIPACKAGE *package, LPCWSTR source,
69 LPCWSTR target, const INT type, LPCWSTR action);
70 static UINT HANDLE_CustomType17(MSIPACKAGE *package, LPCWSTR source,
71 LPCWSTR target, const INT type, LPCWSTR action);
72 static UINT HANDLE_CustomType18(MSIPACKAGE *package, LPCWSTR source,
73 LPCWSTR target, const INT type, LPCWSTR action);
74 static UINT HANDLE_CustomType19(MSIPACKAGE *package, LPCWSTR source,
75 LPCWSTR target, const INT type, LPCWSTR action);
76 static UINT HANDLE_CustomType50(MSIPACKAGE *package, LPCWSTR source,
77 LPCWSTR target, const INT type, LPCWSTR action);
78 static UINT HANDLE_CustomType34(MSIPACKAGE *package, LPCWSTR source,
79 LPCWSTR target, const INT type, LPCWSTR action);
82 static BOOL check_execution_scheduling_options(MSIPACKAGE *package, LPCWSTR action, UINT options)
87 if ((options & msidbCustomActionTypeClientRepeat) ==
88 msidbCustomActionTypeClientRepeat)
90 if (!(package->script->InWhatSequence & SEQUENCE_UI &&
91 package->script->InWhatSequence & SEQUENCE_EXEC))
93 TRACE("Skipping action due to dbCustomActionTypeClientRepeat option.\n");
97 else if (options & msidbCustomActionTypeFirstSequence)
99 if (package->script->InWhatSequence & SEQUENCE_UI &&
100 package->script->InWhatSequence & SEQUENCE_EXEC )
102 TRACE("Skipping action due to msidbCustomActionTypeFirstSequence option.\n");
106 else if (options & msidbCustomActionTypeOncePerProcess)
108 if (check_unique_action(package,action))
110 TRACE("Skipping action due to msidbCustomActionTypeOncePerProcess option.\n");
114 register_unique_action(package,action);
120 /* stores the CustomActionData before the action:
121 * [CustomActionData]Action
123 LPWSTR msi_get_deferred_action(LPCWSTR action, LPWSTR actiondata)
128 static const WCHAR begin[] = {'[',0};
129 static const WCHAR end[] = {']',0};
132 return strdupW(action);
134 len = lstrlenW(action) + lstrlenW(actiondata) + 3;
135 deferred = msi_alloc(len * sizeof(WCHAR));
137 lstrcpyW(deferred, begin);
138 lstrcatW(deferred, actiondata);
139 lstrcatW(deferred, end);
140 lstrcatW(deferred, action);
145 UINT ACTION_CustomAction(MSIPACKAGE *package,LPCWSTR action, BOOL execute)
147 UINT rc = ERROR_SUCCESS;
149 static const WCHAR ExecSeqQuery[] =
150 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
151 '`','C','u','s','t','o' ,'m','A','c','t','i','o','n','`',
152 ' ','W','H','E','R','E',' ','`','A','c','t','i' ,'o','n','`',' ',
153 '=',' ','\'','%','s','\'',0};
155 LPCWSTR source, target;
156 LPWSTR ptr, deferred_data = NULL;
157 LPWSTR action_copy = strdupW(action);
158 WCHAR *deformated=NULL;
160 /* deferred action: [CustomActionData]Action */
161 if ((ptr = strchrW(action_copy, ']')))
163 deferred_data = action_copy + 1;
168 row = MSI_QueryGetRecord( package->db, ExecSeqQuery, action );
170 return ERROR_CALL_NOT_IMPLEMENTED;
172 type = MSI_RecordGetInteger(row,2);
174 source = MSI_RecordGetString(row,3);
175 target = MSI_RecordGetString(row,4);
177 TRACE("Handling custom action %s (%x %s %s)\n",debugstr_w(action),type,
178 debugstr_w(source), debugstr_w(target));
180 /* handle some of the deferred actions */
181 if (type & msidbCustomActionTypeTSAware)
182 FIXME("msidbCustomActionTypeTSAware not handled\n");
184 if (type & msidbCustomActionTypeInScript)
186 if (type & msidbCustomActionTypeNoImpersonate)
187 FIXME("msidbCustomActionTypeNoImpersonate not handled\n");
189 if (type & msidbCustomActionTypeRollback)
191 FIXME("Rollback only action... rollbacks not supported yet\n");
192 schedule_action(package, ROLLBACK_SCRIPT, action);
198 LPWSTR actiondata = msi_dup_property(package, action);
199 LPWSTR deferred = msi_get_deferred_action(action, actiondata);
201 if (type & msidbCustomActionTypeCommit)
203 TRACE("Deferring Commit Action!\n");
204 schedule_action(package, COMMIT_SCRIPT, deferred);
208 TRACE("Deferring Action!\n");
209 schedule_action(package, INSTALL_SCRIPT, deferred);
220 static const WCHAR szActionData[] = {
221 'C','u','s','t','o','m','A','c','t','i','o','n','D','a','t','a',0};
222 static const WCHAR szBlank[] = {0};
223 LPWSTR actiondata = msi_dup_property( package, action );
225 MSI_SetPropertyW(package,szActionData,deferred_data);
227 MSI_SetPropertyW(package,szActionData,actiondata);
229 MSI_SetPropertyW(package,szActionData,szBlank);
230 msi_free(actiondata);
233 else if (!check_execution_scheduling_options(package,action,type))
239 switch (type & CUSTOM_ACTION_TYPE_MASK)
241 case 1: /* DLL file stored in a Binary table stream */
242 rc = HANDLE_CustomType1(package,source,target,type,action);
244 case 2: /* EXE file stored in a Binary table strem */
245 rc = HANDLE_CustomType2(package,source,target,type,action);
247 case 18: /*EXE file installed with package */
248 rc = HANDLE_CustomType18(package,source,target,type,action);
250 case 19: /* Error that halts install */
251 rc = HANDLE_CustomType19(package,source,target,type,action);
254 rc = HANDLE_CustomType17(package,source,target,type,action);
256 case 50: /*EXE file specified by a property value */
257 rc = HANDLE_CustomType50(package,source,target,type,action);
259 case 34: /*EXE to be run in specified directory */
260 rc = HANDLE_CustomType34(package,source,target,type,action);
262 case 35: /* Directory set with formatted text. */
263 deformat_string(package,target,&deformated);
264 MSI_SetTargetPathW(package, source, deformated);
265 msi_free(deformated);
267 case 51: /* Property set with formatted text. */
268 deformat_string(package,target,&deformated);
269 rc = MSI_SetPropertyW(package,source,deformated);
270 msi_free(deformated);
273 FIXME("UNHANDLED ACTION TYPE %i (%s %s)\n",
274 type & CUSTOM_ACTION_TYPE_MASK, debugstr_w(source),
279 msi_free(action_copy);
280 msiobj_release(&row->hdr);
285 static UINT store_binary_to_temp(MSIPACKAGE *package, LPCWSTR source,
289 static const WCHAR f1[] = {'m','s','i',0};
292 if (MSI_GetPropertyW(package, cszTempFolder, fmt, &sz) != ERROR_SUCCESS)
293 GetTempPathW(MAX_PATH,fmt);
295 if (GetTempFileNameW(fmt,f1,0,tmp_file) == 0)
297 TRACE("Unable to create file\n");
298 return ERROR_FUNCTION_FAILED;
302 /* write out the file */
305 static const WCHAR fmt[] =
306 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
307 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',
308 ' ','`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
312 the_file = CreateFileW(tmp_file, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
313 FILE_ATTRIBUTE_NORMAL, NULL);
315 if (the_file == INVALID_HANDLE_VALUE)
316 return ERROR_FUNCTION_FAILED;
318 row = MSI_QueryGetRecord(package->db, fmt, source);
320 return ERROR_FUNCTION_FAILED;
326 rc = MSI_RecordReadStream(row,2,buffer,&sz);
327 if (rc != ERROR_SUCCESS)
329 ERR("Failed to get stream\n");
330 CloseHandle(the_file);
331 DeleteFileW(tmp_file);
334 WriteFile(the_file,buffer,sz,&write,NULL);
335 } while (sz == 1024);
337 CloseHandle(the_file);
339 msiobj_release(&row->hdr);
342 return ERROR_SUCCESS;
345 static void file_running_action(MSIPACKAGE* package, HANDLE Handle,
346 BOOL process, LPCWSTR name)
348 MSIRUNNINGACTION *action;
350 action = msi_alloc( sizeof(MSIRUNNINGACTION) );
352 action->handle = Handle;
353 action->process = process;
354 action->name = strdupW(name);
356 list_add_tail( &package->RunningActions, &action->entry );
359 static UINT process_action_return_value(UINT type, HANDLE ThreadHandle)
365 GetExitCodeProcess(ThreadHandle,&rc);
368 return ERROR_SUCCESS;
370 return ERROR_FUNCTION_FAILED;
373 GetExitCodeThread(ThreadHandle,&rc);
377 case ERROR_FUNCTION_NOT_CALLED:
379 case ERROR_INSTALL_USEREXIT:
380 case ERROR_INSTALL_FAILURE:
382 case ERROR_NO_MORE_ITEMS:
383 return ERROR_SUCCESS;
385 ERR("Invalid Return Code %ld\n",rc);
386 return ERROR_INSTALL_FAILURE;
390 static UINT process_handle(MSIPACKAGE* package, UINT type,
391 HANDLE ThreadHandle, HANDLE ProcessHandle,
392 LPCWSTR Name, BOOL *finished)
394 UINT rc = ERROR_SUCCESS;
396 if (!(type & msidbCustomActionTypeAsync))
399 TRACE("Synchronous Execution of action %s\n",debugstr_w(Name));
401 msi_dialog_check_messages(ProcessHandle);
403 msi_dialog_check_messages(ThreadHandle);
405 if (!(type & msidbCustomActionTypeContinue))
408 rc = process_action_return_value(2,ProcessHandle);
410 rc = process_action_return_value(1,ThreadHandle);
413 CloseHandle(ThreadHandle);
415 CloseHandle(ProcessHandle);
421 TRACE("Asynchronous Execution of action %s\n",debugstr_w(Name));
423 if (type & msidbCustomActionTypeContinue)
427 file_running_action(package, ProcessHandle, TRUE, Name);
428 CloseHandle(ThreadHandle);
431 file_running_action(package, ThreadHandle, FALSE, Name);
435 CloseHandle(ThreadHandle);
437 CloseHandle(ProcessHandle);
447 typedef UINT __stdcall CustomEntry(MSIHANDLE);
456 static DWORD WINAPI ACTION_CallDllFunction(thread_struct *stuff)
461 DWORD rc = ERROR_SUCCESS;
463 TRACE("calling function (%s, %s)\n", debugstr_w(stuff->source),
464 debugstr_w(stuff->target));
466 hModule = LoadLibraryW(stuff->source);
469 proc = strdupWtoA( stuff->target );
470 fn = (CustomEntry*)GetProcAddress(hModule,proc);
474 MSIPACKAGE *package = stuff->package;
476 TRACE("Calling function %s\n", proc);
477 hPackage = alloc_msihandle( &package->hdr );
481 MsiCloseHandle( hPackage );
484 ERR("Handle for object %p not found\n", package );
487 ERR("failed to resolve functon %s\n", debugstr_a(proc));
490 FreeLibrary(hModule);
493 ERR("failed to load dll %s\n", debugstr_w(stuff->source));
494 msiobj_release( &stuff->package->hdr );
495 msi_free(stuff->source);
496 msi_free(stuff->target);
501 static DWORD WINAPI DllThread(LPVOID info)
503 thread_struct *stuff;
506 TRACE("MSI Thread (0x%lx) started for custom action\n",
507 GetCurrentThreadId());
509 stuff = (thread_struct*)info;
510 rc = ACTION_CallDllFunction(stuff);
512 TRACE("MSI Thread (0x%lx) finished (rc %li)\n",GetCurrentThreadId(), rc);
513 /* clse all handles for this thread */
514 MsiCloseAllHandles();
518 static HANDLE do_msidbCustomActionTypeDll(MSIPACKAGE *package, LPCWSTR dll, LPCWSTR target)
522 info = msi_alloc( sizeof(*info) );
523 msiobj_addref( &package->hdr );
524 info->package = package;
525 info->target = strdupW(target);
526 info->source = strdupW(dll);
528 return CreateThread(NULL, 0, DllThread, info, 0, NULL);
531 static UINT HANDLE_CustomType1(MSIPACKAGE *package, LPCWSTR source,
532 LPCWSTR target, const INT type, LPCWSTR action)
534 WCHAR tmp_file[MAX_PATH];
535 UINT rc = ERROR_SUCCESS;
536 BOOL finished = FALSE;
539 store_binary_to_temp(package, source, tmp_file);
541 TRACE("Calling function %s from %s\n",debugstr_w(target),
542 debugstr_w(tmp_file));
544 if (!strchrW(tmp_file,'.'))
546 static const WCHAR dot[]={'.',0};
547 strcatW(tmp_file,dot);
550 ThreadHandle = do_msidbCustomActionTypeDll( package, tmp_file, target );
552 rc = process_handle(package, type, ThreadHandle, NULL, action, &finished );
555 track_tempfile(package, tmp_file, tmp_file);
557 DeleteFileW(tmp_file);
562 static UINT HANDLE_CustomType2(MSIPACKAGE *package, LPCWSTR source,
563 LPCWSTR target, const INT type, LPCWSTR action)
565 WCHAR tmp_file[MAX_PATH];
567 PROCESS_INFORMATION info;
572 static const WCHAR spc[] = {' ',0};
573 UINT prc = ERROR_SUCCESS;
574 BOOL finished = FALSE;
576 memset(&si,0,sizeof(STARTUPINFOW));
578 store_binary_to_temp(package, source, tmp_file);
580 deformat_string(package,target,&deformated);
582 len = strlenW(tmp_file)+2;
585 len += strlenW(deformated);
587 cmd = msi_alloc(sizeof(WCHAR)*len);
589 strcpyW(cmd,tmp_file);
593 strcatW(cmd,deformated);
595 msi_free(deformated);
598 TRACE("executing exe %s\n", debugstr_w(cmd));
600 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
601 c_collen, &si, &info);
606 ERR("Unable to execute command %s\n", debugstr_w(cmd));
608 return ERROR_SUCCESS;
612 prc = process_handle(package, type, info.hThread, info.hProcess, action,
616 track_tempfile(package, tmp_file, tmp_file);
618 DeleteFileW(tmp_file);
623 static UINT HANDLE_CustomType17(MSIPACKAGE *package, LPCWSTR source,
624 LPCWSTR target, const INT type, LPCWSTR action)
629 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
631 file = get_loaded_file( package, source );
634 ERR("invalid file key %s\n", debugstr_w( source ));
635 return ERROR_FUNCTION_FAILED;
638 hThread = do_msidbCustomActionTypeDll( package, file->TargetPath, target );
640 return process_handle(package, type, hThread, NULL, action, NULL );
643 static UINT HANDLE_CustomType18(MSIPACKAGE *package, LPCWSTR source,
644 LPCWSTR target, const INT type, LPCWSTR action)
647 PROCESS_INFORMATION info;
652 static const WCHAR spc[] = {' ',0};
656 memset(&si,0,sizeof(STARTUPINFOW));
658 file = get_loaded_file(package,source);
660 return ERROR_FUNCTION_FAILED;
662 len = lstrlenW( file->TargetPath );
664 deformat_string(package,target,&deformated);
666 len += strlenW(deformated);
669 cmd = msi_alloc(len * sizeof(WCHAR));
671 lstrcpyW( cmd, file->TargetPath);
675 strcatW(cmd, deformated);
677 msi_free(deformated);
680 TRACE("executing exe %s\n", debugstr_w(cmd));
682 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
683 c_collen, &si, &info);
688 ERR("Unable to execute command %s\n", debugstr_w(cmd));
690 return ERROR_SUCCESS;
694 prc = process_handle(package, type, info.hThread, info.hProcess, action,
700 static UINT HANDLE_CustomType19(MSIPACKAGE *package, LPCWSTR source,
701 LPCWSTR target, const INT type, LPCWSTR action)
703 static const WCHAR query[] = {
704 'S','E','L','E','C','T',' ','`','M','e','s','s','a','g','e','`',' ',
705 'F','R','O','M',' ','`','E','r','r','o','r','`',' ',
706 'W','H','E','R','E',' ','`','E','r','r','o','r','`',' ','=',' ',
710 LPWSTR deformated = NULL;
712 deformat_string( package, target, &deformated );
714 /* first try treat the error as a number */
715 row = MSI_QueryGetRecord( package->db, query, deformated );
718 LPCWSTR error = MSI_RecordGetString( row, 1 );
719 MessageBoxW( NULL, error, NULL, MB_OK );
720 msiobj_release( &row->hdr );
723 MessageBoxW( NULL, deformated, NULL, MB_OK );
725 msi_free( deformated );
727 return ERROR_FUNCTION_FAILED;
730 static UINT HANDLE_CustomType50(MSIPACKAGE *package, LPCWSTR source,
731 LPCWSTR target, const INT type, LPCWSTR action)
734 PROCESS_INFORMATION info;
740 static const WCHAR spc[] = {' ',0};
742 memset(&si,0,sizeof(STARTUPINFOW));
743 memset(&info,0,sizeof(PROCESS_INFORMATION));
745 prop = msi_dup_property( package, source );
747 return ERROR_SUCCESS;
749 deformat_string(package,target,&deformated);
750 len = strlenW(prop) + 2;
752 len += strlenW(deformated);
754 cmd = msi_alloc(sizeof(WCHAR)*len);
760 strcatW(cmd,deformated);
762 msi_free(deformated);
766 TRACE("executing exe %s\n", debugstr_w(cmd));
768 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
769 c_collen, &si, &info);
774 ERR("Unable to execute command %s\n", debugstr_w(cmd));
776 return ERROR_SUCCESS;
780 return process_handle(package, type, info.hThread, info.hProcess, action, NULL);
783 static UINT HANDLE_CustomType34(MSIPACKAGE *package, LPCWSTR source,
784 LPCWSTR target, const INT type, LPCWSTR action)
786 LPWSTR filename, deformated;
788 PROCESS_INFORMATION info;
792 memset(&si,0,sizeof(STARTUPINFOW));
794 filename = resolve_folder(package, source, FALSE, FALSE, NULL);
797 return ERROR_FUNCTION_FAILED;
799 SetCurrentDirectoryW(filename);
802 deformat_string(package,target,&deformated);
805 return ERROR_FUNCTION_FAILED;
807 TRACE("executing exe %s\n", debugstr_w(deformated));
809 rc = CreateProcessW(NULL, deformated, NULL, NULL, FALSE, 0, NULL,
810 c_collen, &si, &info);
814 ERR("Unable to execute command %s\n", debugstr_w(deformated));
815 msi_free(deformated);
816 return ERROR_SUCCESS;
818 msi_free(deformated);
820 prc = process_handle(package, type, info.hThread, info.hProcess, action,
827 void ACTION_FinishCustomActions(MSIPACKAGE* package)
829 struct list *item, *cursor;
832 LIST_FOR_EACH_SAFE( item, cursor, &package->RunningActions )
834 MSIRUNNINGACTION *action = LIST_ENTRY( item, MSIRUNNINGACTION, entry );
836 TRACE("Checking on action %s\n", debugstr_w(action->name));
838 list_remove( &action->entry );
841 GetExitCodeProcess( action->handle, &rc );
843 GetExitCodeThread( action->handle, &rc );
845 if (rc == STILL_ACTIVE)
847 TRACE("Waiting on action %s\n", debugstr_w( action->name) );
848 msi_dialog_check_messages( action->handle );
851 CloseHandle( action->handle );
852 msi_free( action->name );