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"
51 WINE_DEFAULT_DEBUG_CHANNEL(msi);
53 #define CUSTOM_ACTION_TYPE_MASK 0x3F
54 static const WCHAR c_collen[] = {'C',':','\\',0};
55 static const WCHAR cszTempFolder[]= {'T','e','m','p','F','o','l','d','e','r',0};
57 typedef struct tagMSIRUNNINGACTION
65 static UINT HANDLE_CustomType1(MSIPACKAGE *package, LPCWSTR source,
66 LPCWSTR target, const INT type, LPCWSTR action);
67 static UINT HANDLE_CustomType2(MSIPACKAGE *package, LPCWSTR source,
68 LPCWSTR target, const INT type, LPCWSTR action);
69 static UINT HANDLE_CustomType17(MSIPACKAGE *package, LPCWSTR source,
70 LPCWSTR target, const INT type, LPCWSTR action);
71 static UINT HANDLE_CustomType18(MSIPACKAGE *package, LPCWSTR source,
72 LPCWSTR target, const INT type, LPCWSTR action);
73 static UINT HANDLE_CustomType19(MSIPACKAGE *package, LPCWSTR source,
74 LPCWSTR target, const INT type, LPCWSTR action);
75 static UINT HANDLE_CustomType50(MSIPACKAGE *package, LPCWSTR source,
76 LPCWSTR target, const INT type, LPCWSTR action);
77 static UINT HANDLE_CustomType34(MSIPACKAGE *package, LPCWSTR source,
78 LPCWSTR target, const INT type, LPCWSTR action);
81 static BOOL check_execution_scheduling_options(MSIPACKAGE *package, LPCWSTR action, UINT options)
86 if ((options & msidbCustomActionTypeClientRepeat) ==
87 msidbCustomActionTypeClientRepeat)
89 if (!(package->script->InWhatSequence & SEQUENCE_UI &&
90 package->script->InWhatSequence & SEQUENCE_EXEC))
92 TRACE("Skipping action due to dbCustomActionTypeClientRepeat option.\n");
96 else if (options & msidbCustomActionTypeFirstSequence)
98 if (package->script->InWhatSequence & SEQUENCE_UI &&
99 package->script->InWhatSequence & SEQUENCE_EXEC )
101 TRACE("Skipping action due to msidbCustomActionTypeFirstSequence option.\n");
105 else if (options & msidbCustomActionTypeOncePerProcess)
107 if (check_unique_action(package,action))
109 TRACE("Skipping action due to msidbCustomActionTypeOncePerProcess option.\n");
113 register_unique_action(package,action);
119 /* stores the CustomActionData before the action:
120 * [CustomActionData]Action
122 static LPWSTR msi_get_deferred_action(LPCWSTR action, LPWSTR actiondata)
127 static const WCHAR begin[] = {'[',0};
128 static const WCHAR end[] = {']',0};
131 return strdupW(action);
133 len = lstrlenW(action) + lstrlenW(actiondata) + 3;
134 deferred = msi_alloc(len * sizeof(WCHAR));
136 lstrcpyW(deferred, begin);
137 lstrcatW(deferred, actiondata);
138 lstrcatW(deferred, end);
139 lstrcatW(deferred, action);
144 UINT ACTION_CustomAction(MSIPACKAGE *package,LPCWSTR action, BOOL execute)
146 UINT rc = ERROR_SUCCESS;
148 static const WCHAR ExecSeqQuery[] =
149 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
150 '`','C','u','s','t','o' ,'m','A','c','t','i','o','n','`',
151 ' ','W','H','E','R','E',' ','`','A','c','t','i' ,'o','n','`',' ',
152 '=',' ','\'','%','s','\'',0};
154 LPCWSTR source, target;
155 LPWSTR ptr, deferred_data = NULL;
156 LPWSTR action_copy = strdupW(action);
157 WCHAR *deformated=NULL;
159 /* deferred action: [CustomActionData]Action */
160 if ((ptr = strchrW(action_copy, ']')))
162 deferred_data = action_copy + 1;
167 row = MSI_QueryGetRecord( package->db, ExecSeqQuery, action );
170 msi_free(action_copy);
171 return ERROR_CALL_NOT_IMPLEMENTED;
174 type = MSI_RecordGetInteger(row,2);
176 source = MSI_RecordGetString(row,3);
177 target = MSI_RecordGetString(row,4);
179 TRACE("Handling custom action %s (%x %s %s)\n",debugstr_w(action),type,
180 debugstr_w(source), debugstr_w(target));
182 /* handle some of the deferred actions */
183 if (type & msidbCustomActionTypeTSAware)
184 FIXME("msidbCustomActionTypeTSAware not handled\n");
186 if (type & msidbCustomActionTypeInScript)
188 if (type & msidbCustomActionTypeNoImpersonate)
189 FIXME("msidbCustomActionTypeNoImpersonate not handled\n");
191 if (type & msidbCustomActionTypeRollback)
193 FIXME("Rollback only action... rollbacks not supported yet\n");
194 schedule_action(package, ROLLBACK_SCRIPT, action);
200 LPWSTR actiondata = msi_dup_property(package, action);
201 LPWSTR deferred = msi_get_deferred_action(action, actiondata);
203 if (type & msidbCustomActionTypeCommit)
205 TRACE("Deferring Commit Action!\n");
206 schedule_action(package, COMMIT_SCRIPT, deferred);
210 TRACE("Deferring Action!\n");
211 schedule_action(package, INSTALL_SCRIPT, deferred);
222 static const WCHAR szActionData[] = {
223 'C','u','s','t','o','m','A','c','t','i','o','n','D','a','t','a',0};
224 static const WCHAR szBlank[] = {0};
225 LPWSTR actiondata = msi_dup_property( package, action );
227 MSI_SetPropertyW(package,szActionData,deferred_data);
229 MSI_SetPropertyW(package,szActionData,actiondata);
231 MSI_SetPropertyW(package,szActionData,szBlank);
232 msi_free(actiondata);
235 else if (!check_execution_scheduling_options(package,action,type))
241 switch (type & CUSTOM_ACTION_TYPE_MASK)
243 case 1: /* DLL file stored in a Binary table stream */
244 rc = HANDLE_CustomType1(package,source,target,type,action);
246 case 2: /* EXE file stored in a Binary table strem */
247 rc = HANDLE_CustomType2(package,source,target,type,action);
249 case 18: /*EXE file installed with package */
250 rc = HANDLE_CustomType18(package,source,target,type,action);
252 case 19: /* Error that halts install */
253 rc = HANDLE_CustomType19(package,source,target,type,action);
256 rc = HANDLE_CustomType17(package,source,target,type,action);
258 case 50: /*EXE file specified by a property value */
259 rc = HANDLE_CustomType50(package,source,target,type,action);
261 case 34: /*EXE to be run in specified directory */
262 rc = HANDLE_CustomType34(package,source,target,type,action);
264 case 35: /* Directory set with formatted text. */
265 deformat_string(package,target,&deformated);
266 MSI_SetTargetPathW(package, source, deformated);
267 msi_free(deformated);
269 case 51: /* Property set with formatted text. */
270 deformat_string(package,target,&deformated);
271 rc = MSI_SetPropertyW(package,source,deformated);
272 msi_free(deformated);
275 FIXME("UNHANDLED ACTION TYPE %i (%s %s)\n",
276 type & CUSTOM_ACTION_TYPE_MASK, debugstr_w(source),
281 msi_free(action_copy);
282 msiobj_release(&row->hdr);
287 static UINT store_binary_to_temp(MSIPACKAGE *package, LPCWSTR source,
291 static const WCHAR f1[] = {'m','s','i',0};
294 if (MSI_GetPropertyW(package, cszTempFolder, fmt, &sz) != ERROR_SUCCESS)
295 GetTempPathW(MAX_PATH,fmt);
297 if (GetTempFileNameW(fmt,f1,0,tmp_file) == 0)
299 TRACE("Unable to create file\n");
300 return ERROR_FUNCTION_FAILED;
304 /* write out the file */
307 static const WCHAR fmt[] =
308 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
309 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',
310 ' ','`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
314 the_file = CreateFileW(tmp_file, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
315 FILE_ATTRIBUTE_NORMAL, NULL);
317 if (the_file == INVALID_HANDLE_VALUE)
318 return ERROR_FUNCTION_FAILED;
320 row = MSI_QueryGetRecord(package->db, fmt, source);
322 return ERROR_FUNCTION_FAILED;
328 rc = MSI_RecordReadStream(row,2,buffer,&sz);
329 if (rc != ERROR_SUCCESS)
331 ERR("Failed to get stream\n");
332 CloseHandle(the_file);
333 DeleteFileW(tmp_file);
336 WriteFile(the_file,buffer,sz,&write,NULL);
337 } while (sz == 1024);
339 CloseHandle(the_file);
341 msiobj_release(&row->hdr);
344 return ERROR_SUCCESS;
347 static void file_running_action(MSIPACKAGE* package, HANDLE Handle,
348 BOOL process, LPCWSTR name)
350 MSIRUNNINGACTION *action;
352 action = msi_alloc( sizeof(MSIRUNNINGACTION) );
354 action->handle = Handle;
355 action->process = process;
356 action->name = strdupW(name);
358 list_add_tail( &package->RunningActions, &action->entry );
361 static UINT process_action_return_value(UINT type, HANDLE ThreadHandle)
367 GetExitCodeProcess(ThreadHandle,&rc);
370 return ERROR_SUCCESS;
372 return ERROR_FUNCTION_FAILED;
375 GetExitCodeThread(ThreadHandle,&rc);
379 case ERROR_FUNCTION_NOT_CALLED:
381 case ERROR_INSTALL_USEREXIT:
382 case ERROR_INSTALL_FAILURE:
384 case ERROR_NO_MORE_ITEMS:
385 return ERROR_SUCCESS;
387 ERR("Invalid Return Code %d\n",rc);
388 return ERROR_INSTALL_FAILURE;
392 static UINT process_handle(MSIPACKAGE* package, UINT type,
393 HANDLE ThreadHandle, HANDLE ProcessHandle,
394 LPCWSTR Name, BOOL *finished)
396 UINT rc = ERROR_SUCCESS;
398 if (!(type & msidbCustomActionTypeAsync))
401 TRACE("Synchronous Execution of action %s\n",debugstr_w(Name));
403 msi_dialog_check_messages(ProcessHandle);
405 msi_dialog_check_messages(ThreadHandle);
407 if (!(type & msidbCustomActionTypeContinue))
410 rc = process_action_return_value(2,ProcessHandle);
412 rc = process_action_return_value(1,ThreadHandle);
415 CloseHandle(ThreadHandle);
417 CloseHandle(ProcessHandle);
423 TRACE("Asynchronous Execution of action %s\n",debugstr_w(Name));
425 if (type & msidbCustomActionTypeContinue)
429 file_running_action(package, ProcessHandle, TRUE, Name);
430 CloseHandle(ThreadHandle);
433 file_running_action(package, ThreadHandle, FALSE, Name);
437 CloseHandle(ThreadHandle);
439 CloseHandle(ProcessHandle);
449 typedef UINT __stdcall CustomEntry(MSIHANDLE);
458 static DWORD WINAPI ACTION_CallDllFunction(thread_struct *stuff)
463 DWORD rc = ERROR_SUCCESS;
465 TRACE("calling function (%s, %s)\n", debugstr_w(stuff->source),
466 debugstr_w(stuff->target));
468 hModule = LoadLibraryW(stuff->source);
471 proc = strdupWtoA( stuff->target );
472 fn = (CustomEntry*)GetProcAddress(hModule,proc);
476 MSIPACKAGE *package = stuff->package;
478 TRACE("Calling function %s\n", proc);
479 hPackage = alloc_msihandle( &package->hdr );
483 MsiCloseHandle( hPackage );
486 ERR("Handle for object %p not found\n", package );
489 ERR("failed to resolve functon %s\n", debugstr_a(proc));
492 FreeLibrary(hModule);
495 ERR("failed to load dll %s\n", debugstr_w(stuff->source));
496 msiobj_release( &stuff->package->hdr );
497 msi_free(stuff->source);
498 msi_free(stuff->target);
503 static DWORD WINAPI DllThread(LPVOID info)
505 thread_struct *stuff;
508 TRACE("MSI Thread (%x) started for custom action\n", GetCurrentThreadId());
510 stuff = (thread_struct*)info;
511 rc = ACTION_CallDllFunction(stuff);
513 TRACE("MSI Thread (%x) finished (rc %i)\n",GetCurrentThreadId(), rc);
514 /* clse all handles for this thread */
515 MsiCloseAllHandles();
519 static HANDLE do_msidbCustomActionTypeDll(MSIPACKAGE *package, LPCWSTR dll, LPCWSTR target)
523 info = msi_alloc( sizeof(*info) );
524 msiobj_addref( &package->hdr );
525 info->package = package;
526 info->target = strdupW(target);
527 info->source = strdupW(dll);
529 return CreateThread(NULL, 0, DllThread, info, 0, NULL);
532 static UINT HANDLE_CustomType1(MSIPACKAGE *package, LPCWSTR source,
533 LPCWSTR target, const INT type, LPCWSTR action)
535 WCHAR tmp_file[MAX_PATH];
536 UINT rc = ERROR_SUCCESS;
537 BOOL finished = FALSE;
540 store_binary_to_temp(package, source, tmp_file);
542 TRACE("Calling function %s from %s\n",debugstr_w(target),
543 debugstr_w(tmp_file));
545 if (!strchrW(tmp_file,'.'))
547 static const WCHAR dot[]={'.',0};
548 strcatW(tmp_file,dot);
551 ThreadHandle = do_msidbCustomActionTypeDll( package, tmp_file, target );
553 rc = process_handle(package, type, ThreadHandle, NULL, action, &finished );
556 track_tempfile(package, tmp_file, tmp_file);
558 DeleteFileW(tmp_file);
563 static UINT HANDLE_CustomType2(MSIPACKAGE *package, LPCWSTR source,
564 LPCWSTR target, const INT type, LPCWSTR action)
566 WCHAR tmp_file[MAX_PATH];
568 PROCESS_INFORMATION info;
573 static const WCHAR spc[] = {' ',0};
574 UINT prc = ERROR_SUCCESS;
575 BOOL finished = FALSE;
577 memset(&si,0,sizeof(STARTUPINFOW));
579 store_binary_to_temp(package, source, tmp_file);
581 deformat_string(package,target,&deformated);
583 len = strlenW(tmp_file)+2;
586 len += strlenW(deformated);
588 cmd = msi_alloc(sizeof(WCHAR)*len);
590 strcpyW(cmd,tmp_file);
594 strcatW(cmd,deformated);
596 msi_free(deformated);
599 TRACE("executing exe %s\n", debugstr_w(cmd));
601 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
602 c_collen, &si, &info);
607 ERR("Unable to execute command %s\n", debugstr_w(cmd));
609 return ERROR_SUCCESS;
613 prc = process_handle(package, type, info.hThread, info.hProcess, action,
617 track_tempfile(package, tmp_file, tmp_file);
619 DeleteFileW(tmp_file);
624 static UINT HANDLE_CustomType17(MSIPACKAGE *package, LPCWSTR source,
625 LPCWSTR target, const INT type, LPCWSTR action)
630 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
632 file = get_loaded_file( package, source );
635 ERR("invalid file key %s\n", debugstr_w( source ));
636 return ERROR_FUNCTION_FAILED;
639 hThread = do_msidbCustomActionTypeDll( package, file->TargetPath, target );
641 return process_handle(package, type, hThread, NULL, action, NULL );
644 static UINT HANDLE_CustomType18(MSIPACKAGE *package, LPCWSTR source,
645 LPCWSTR target, const INT type, LPCWSTR action)
648 PROCESS_INFORMATION info;
653 static const WCHAR spc[] = {' ',0};
657 memset(&si,0,sizeof(STARTUPINFOW));
659 file = get_loaded_file(package,source);
661 return ERROR_FUNCTION_FAILED;
663 len = lstrlenW( file->TargetPath );
665 deformat_string(package,target,&deformated);
667 len += strlenW(deformated);
670 cmd = msi_alloc(len * sizeof(WCHAR));
672 lstrcpyW( cmd, file->TargetPath);
676 strcatW(cmd, deformated);
678 msi_free(deformated);
681 TRACE("executing exe %s\n", debugstr_w(cmd));
683 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
684 c_collen, &si, &info);
689 ERR("Unable to execute command %s\n", debugstr_w(cmd));
691 return ERROR_SUCCESS;
695 prc = process_handle(package, type, info.hThread, info.hProcess, action,
701 static UINT HANDLE_CustomType19(MSIPACKAGE *package, LPCWSTR source,
702 LPCWSTR target, const INT type, LPCWSTR action)
704 static const WCHAR query[] = {
705 'S','E','L','E','C','T',' ','`','M','e','s','s','a','g','e','`',' ',
706 'F','R','O','M',' ','`','E','r','r','o','r','`',' ',
707 'W','H','E','R','E',' ','`','E','r','r','o','r','`',' ','=',' ',
711 LPWSTR deformated = NULL;
713 deformat_string( package, target, &deformated );
715 /* first try treat the error as a number */
716 row = MSI_QueryGetRecord( package->db, query, deformated );
719 LPCWSTR error = MSI_RecordGetString( row, 1 );
720 MessageBoxW( NULL, error, NULL, MB_OK );
721 msiobj_release( &row->hdr );
724 MessageBoxW( NULL, deformated, NULL, MB_OK );
726 msi_free( deformated );
728 return ERROR_FUNCTION_FAILED;
731 static UINT HANDLE_CustomType50(MSIPACKAGE *package, LPCWSTR source,
732 LPCWSTR target, const INT type, LPCWSTR action)
735 PROCESS_INFORMATION info;
741 static const WCHAR spc[] = {' ',0};
743 memset(&si,0,sizeof(STARTUPINFOW));
744 memset(&info,0,sizeof(PROCESS_INFORMATION));
746 prop = msi_dup_property( package, source );
748 return ERROR_SUCCESS;
750 deformat_string(package,target,&deformated);
751 len = strlenW(prop) + 2;
753 len += strlenW(deformated);
755 cmd = msi_alloc(sizeof(WCHAR)*len);
761 strcatW(cmd,deformated);
763 msi_free(deformated);
767 TRACE("executing exe %s\n", debugstr_w(cmd));
769 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
770 c_collen, &si, &info);
775 ERR("Unable to execute command %s\n", debugstr_w(cmd));
777 return ERROR_SUCCESS;
781 return process_handle(package, type, info.hThread, info.hProcess, action, NULL);
784 static UINT HANDLE_CustomType34(MSIPACKAGE *package, LPCWSTR source,
785 LPCWSTR target, const INT type, LPCWSTR action)
787 LPWSTR filename, deformated;
789 PROCESS_INFORMATION info;
793 memset(&si,0,sizeof(STARTUPINFOW));
795 filename = resolve_folder(package, source, FALSE, FALSE, NULL);
798 return ERROR_FUNCTION_FAILED;
800 SetCurrentDirectoryW(filename);
803 deformat_string(package,target,&deformated);
806 return ERROR_FUNCTION_FAILED;
808 TRACE("executing exe %s\n", debugstr_w(deformated));
810 rc = CreateProcessW(NULL, deformated, NULL, NULL, FALSE, 0, NULL,
811 c_collen, &si, &info);
815 ERR("Unable to execute command %s\n", debugstr_w(deformated));
816 msi_free(deformated);
817 return ERROR_SUCCESS;
819 msi_free(deformated);
821 prc = process_handle(package, type, info.hThread, info.hProcess, action,
828 void ACTION_FinishCustomActions(MSIPACKAGE* package)
830 struct list *item, *cursor;
833 LIST_FOR_EACH_SAFE( item, cursor, &package->RunningActions )
835 MSIRUNNINGACTION *action = LIST_ENTRY( item, MSIRUNNINGACTION, entry );
837 TRACE("Checking on action %s\n", debugstr_w(action->name));
839 list_remove( &action->entry );
842 GetExitCodeProcess( action->handle, &rc );
844 GetExitCodeThread( action->handle, &rc );
846 if (rc == STILL_ACTIVE)
848 TRACE("Waiting on action %s\n", debugstr_w( action->name) );
849 msi_dialog_check_messages( action->handle );
852 CloseHandle( action->handle );
853 msi_free( action->name );