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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 UINT ACTION_CustomAction(MSIPACKAGE *package,LPCWSTR action, BOOL execute)
122 UINT rc = ERROR_SUCCESS;
124 static const WCHAR ExecSeqQuery[] =
125 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
126 '`','C','u','s','t','o' ,'m','A','c','t','i','o','n','`',
127 ' ','W','H','E','R','E',' ','`','A','c','t','i' ,'o','n','`',' ',
128 '=',' ','\'','%','s','\'',0};
130 LPCWSTR source, target;
131 WCHAR *deformated=NULL;
133 row = MSI_QueryGetRecord( package->db, ExecSeqQuery, action );
135 return ERROR_CALL_NOT_IMPLEMENTED;
137 type = MSI_RecordGetInteger(row,2);
139 source = MSI_RecordGetString(row,3);
140 target = MSI_RecordGetString(row,4);
142 TRACE("Handling custom action %s (%x %s %s)\n",debugstr_w(action),type,
143 debugstr_w(source), debugstr_w(target));
145 /* handle some of the deferred actions */
146 if (type & msidbCustomActionTypeTSAware)
147 FIXME("msidbCustomActionTypeTSAware not handled\n");
149 if (type & msidbCustomActionTypeInScript)
151 if (type & msidbCustomActionTypeNoImpersonate)
152 FIXME("msidbCustomActionTypeNoImpersonate not handled\n");
154 if (type & msidbCustomActionTypeRollback)
156 FIXME("Rollback only action... rollbacks not supported yet\n");
157 schedule_action(package, ROLLBACK_SCRIPT, action);
163 if (type & msidbCustomActionTypeCommit)
165 TRACE("Deferring Commit Action!\n");
166 schedule_action(package, COMMIT_SCRIPT, action);
170 TRACE("Deferring Action!\n");
171 schedule_action(package, INSTALL_SCRIPT, action);
181 static const WCHAR szActionData[] = {
182 'C','u','s','t','o','m','A','c','t','i','o','n','D','a','t','a',0};
183 static const WCHAR szBlank[] = {0};
184 LPWSTR actiondata = msi_dup_property( package, action );
186 MSI_SetPropertyW(package,szActionData,actiondata);
188 MSI_SetPropertyW(package,szActionData,szBlank);
189 msi_free(actiondata);
192 else if (!check_execution_scheduling_options(package,action,type))
198 switch (type & CUSTOM_ACTION_TYPE_MASK)
200 case 1: /* DLL file stored in a Binary table stream */
201 rc = HANDLE_CustomType1(package,source,target,type,action);
203 case 2: /* EXE file stored in a Binary table strem */
204 rc = HANDLE_CustomType2(package,source,target,type,action);
206 case 18: /*EXE file installed with package */
207 rc = HANDLE_CustomType18(package,source,target,type,action);
209 case 19: /* Error that halts install */
210 rc = HANDLE_CustomType19(package,source,target,type,action);
213 rc = HANDLE_CustomType17(package,source,target,type,action);
215 case 50: /*EXE file specified by a property value */
216 rc = HANDLE_CustomType50(package,source,target,type,action);
218 case 34: /*EXE to be run in specified directory */
219 rc = HANDLE_CustomType34(package,source,target,type,action);
221 case 35: /* Directory set with formatted text. */
222 deformat_string(package,target,&deformated);
223 MSI_SetTargetPathW(package, source, deformated);
224 msi_free(deformated);
226 case 51: /* Property set with formatted text. */
227 deformat_string(package,target,&deformated);
228 rc = MSI_SetPropertyW(package,source,deformated);
229 msi_free(deformated);
232 FIXME("UNHANDLED ACTION TYPE %i (%s %s)\n",
233 type & CUSTOM_ACTION_TYPE_MASK, debugstr_w(source),
238 msiobj_release(&row->hdr);
243 static UINT store_binary_to_temp(MSIPACKAGE *package, LPCWSTR source,
247 static const WCHAR f1[] = {'m','s','i',0};
250 if (MSI_GetPropertyW(package, cszTempFolder, fmt, &sz) != ERROR_SUCCESS)
251 GetTempPathW(MAX_PATH,fmt);
253 if (GetTempFileNameW(fmt,f1,0,tmp_file) == 0)
255 TRACE("Unable to create file\n");
256 return ERROR_FUNCTION_FAILED;
260 /* write out the file */
263 static const WCHAR fmt[] =
264 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
265 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',
266 ' ','`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
270 the_file = CreateFileW(tmp_file, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
271 FILE_ATTRIBUTE_NORMAL, NULL);
273 if (the_file == INVALID_HANDLE_VALUE)
274 return ERROR_FUNCTION_FAILED;
276 row = MSI_QueryGetRecord(package->db, fmt, source);
278 return ERROR_FUNCTION_FAILED;
284 rc = MSI_RecordReadStream(row,2,buffer,&sz);
285 if (rc != ERROR_SUCCESS)
287 ERR("Failed to get stream\n");
288 CloseHandle(the_file);
289 DeleteFileW(tmp_file);
292 WriteFile(the_file,buffer,sz,&write,NULL);
293 } while (sz == 1024);
295 CloseHandle(the_file);
297 msiobj_release(&row->hdr);
300 return ERROR_SUCCESS;
303 static void file_running_action(MSIPACKAGE* package, HANDLE Handle,
304 BOOL process, LPCWSTR name)
306 MSIRUNNINGACTION *action;
308 action = msi_alloc( sizeof(MSIRUNNINGACTION) );
310 action->handle = Handle;
311 action->process = process;
312 action->name = strdupW(name);
314 list_add_tail( &package->RunningActions, &action->entry );
317 static UINT process_action_return_value(UINT type, HANDLE ThreadHandle)
323 GetExitCodeProcess(ThreadHandle,&rc);
326 return ERROR_SUCCESS;
328 return ERROR_FUNCTION_FAILED;
331 GetExitCodeThread(ThreadHandle,&rc);
335 case ERROR_FUNCTION_NOT_CALLED:
337 case ERROR_INSTALL_USEREXIT:
338 case ERROR_INSTALL_FAILURE:
340 case ERROR_NO_MORE_ITEMS:
341 return ERROR_SUCCESS;
343 ERR("Invalid Return Code %ld\n",rc);
344 return ERROR_INSTALL_FAILURE;
348 static UINT process_handle(MSIPACKAGE* package, UINT type,
349 HANDLE ThreadHandle, HANDLE ProcessHandle,
350 LPCWSTR Name, BOOL *finished)
352 UINT rc = ERROR_SUCCESS;
354 if (!(type & msidbCustomActionTypeAsync))
357 TRACE("Synchronous Execution of action %s\n",debugstr_w(Name));
359 msi_dialog_check_messages(ProcessHandle);
361 msi_dialog_check_messages(ThreadHandle);
363 if (!(type & msidbCustomActionTypeContinue))
366 rc = process_action_return_value(2,ProcessHandle);
368 rc = process_action_return_value(1,ThreadHandle);
371 CloseHandle(ThreadHandle);
373 CloseHandle(ProcessHandle);
379 TRACE("Asynchronous Execution of action %s\n",debugstr_w(Name));
381 if (type & msidbCustomActionTypeContinue)
385 file_running_action(package, ProcessHandle, TRUE, Name);
386 CloseHandle(ThreadHandle);
389 file_running_action(package, ThreadHandle, FALSE, Name);
393 CloseHandle(ThreadHandle);
395 CloseHandle(ProcessHandle);
405 typedef UINT __stdcall CustomEntry(MSIHANDLE);
414 static DWORD WINAPI ACTION_CallDllFunction(thread_struct *stuff)
419 DWORD rc = ERROR_SUCCESS;
421 TRACE("calling function (%s, %s)\n", debugstr_w(stuff->source),
422 debugstr_w(stuff->target));
424 hModule = LoadLibraryW(stuff->source);
427 proc = strdupWtoA( stuff->target );
428 fn = (CustomEntry*)GetProcAddress(hModule,proc);
432 MSIPACKAGE *package = stuff->package;
434 TRACE("Calling function %s\n", proc);
435 hPackage = alloc_msihandle( &package->hdr );
439 MsiCloseHandle( hPackage );
442 ERR("Handle for object %p not found\n", package );
445 ERR("failed to resolve functon %s\n", debugstr_a(proc));
448 FreeLibrary(hModule);
451 ERR("failed to load dll %s\n", debugstr_w(stuff->source));
452 msiobj_release( &stuff->package->hdr );
453 msi_free(stuff->source);
454 msi_free(stuff->target);
459 static DWORD WINAPI DllThread(LPVOID info)
461 thread_struct *stuff;
464 TRACE("MSI Thread (0x%lx) started for custom action\n",
465 GetCurrentThreadId());
467 stuff = (thread_struct*)info;
468 rc = ACTION_CallDllFunction(stuff);
470 TRACE("MSI Thread (0x%lx) finished (rc %li)\n",GetCurrentThreadId(), rc);
471 /* clse all handles for this thread */
472 MsiCloseAllHandles();
476 static HANDLE do_msidbCustomActionTypeDll(MSIPACKAGE *package, LPCWSTR dll, LPCWSTR target)
480 info = msi_alloc( sizeof(*info) );
481 msiobj_addref( &package->hdr );
482 info->package = package;
483 info->target = strdupW(target);
484 info->source = strdupW(dll);
486 return CreateThread(NULL, 0, DllThread, info, 0, NULL);
489 static UINT HANDLE_CustomType1(MSIPACKAGE *package, LPCWSTR source,
490 LPCWSTR target, const INT type, LPCWSTR action)
492 WCHAR tmp_file[MAX_PATH];
493 UINT rc = ERROR_SUCCESS;
494 BOOL finished = FALSE;
497 store_binary_to_temp(package, source, tmp_file);
499 TRACE("Calling function %s from %s\n",debugstr_w(target),
500 debugstr_w(tmp_file));
502 if (!strchrW(tmp_file,'.'))
504 static const WCHAR dot[]={'.',0};
505 strcatW(tmp_file,dot);
508 ThreadHandle = do_msidbCustomActionTypeDll( package, tmp_file, target );
510 rc = process_handle(package, type, ThreadHandle, NULL, action, &finished );
513 track_tempfile(package, tmp_file, tmp_file);
515 DeleteFileW(tmp_file);
520 static UINT HANDLE_CustomType2(MSIPACKAGE *package, LPCWSTR source,
521 LPCWSTR target, const INT type, LPCWSTR action)
523 WCHAR tmp_file[MAX_PATH];
525 PROCESS_INFORMATION info;
530 static const WCHAR spc[] = {' ',0};
531 UINT prc = ERROR_SUCCESS;
532 BOOL finished = FALSE;
534 memset(&si,0,sizeof(STARTUPINFOW));
536 store_binary_to_temp(package, source, tmp_file);
538 deformat_string(package,target,&deformated);
540 len = strlenW(tmp_file)+2;
543 len += strlenW(deformated);
545 cmd = msi_alloc(sizeof(WCHAR)*len);
547 strcpyW(cmd,tmp_file);
551 strcatW(cmd,deformated);
553 msi_free(deformated);
556 TRACE("executing exe %s\n", debugstr_w(cmd));
558 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
559 c_collen, &si, &info);
564 ERR("Unable to execute command %s\n", debugstr_w(cmd));
566 return ERROR_SUCCESS;
570 prc = process_handle(package, type, info.hThread, info.hProcess, action,
574 track_tempfile(package, tmp_file, tmp_file);
576 DeleteFileW(tmp_file);
581 static UINT HANDLE_CustomType17(MSIPACKAGE *package, LPCWSTR source,
582 LPCWSTR target, const INT type, LPCWSTR action)
587 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
589 file = get_loaded_file( package, source );
592 ERR("invalid file key %s\n", debugstr_w( source ));
593 return ERROR_FUNCTION_FAILED;
596 hThread = do_msidbCustomActionTypeDll( package, file->TargetPath, target );
598 return process_handle(package, type, hThread, NULL, action, NULL );
601 static UINT HANDLE_CustomType18(MSIPACKAGE *package, LPCWSTR source,
602 LPCWSTR target, const INT type, LPCWSTR action)
605 PROCESS_INFORMATION info;
610 static const WCHAR spc[] = {' ',0};
614 memset(&si,0,sizeof(STARTUPINFOW));
616 file = get_loaded_file(package,source);
618 return ERROR_FUNCTION_FAILED;
620 len = lstrlenW( file->TargetPath );
622 deformat_string(package,target,&deformated);
624 len += strlenW(deformated);
627 cmd = msi_alloc(len * sizeof(WCHAR));
629 lstrcpyW( cmd, file->TargetPath);
633 strcatW(cmd, deformated);
635 msi_free(deformated);
638 TRACE("executing exe %s\n", debugstr_w(cmd));
640 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
641 c_collen, &si, &info);
646 ERR("Unable to execute command %s\n", debugstr_w(cmd));
648 return ERROR_SUCCESS;
652 prc = process_handle(package, type, info.hThread, info.hProcess, action,
658 static UINT HANDLE_CustomType19(MSIPACKAGE *package, LPCWSTR source,
659 LPCWSTR target, const INT type, LPCWSTR action)
661 static const WCHAR query[] = {
662 'S','E','L','E','C','T',' ','`','M','e','s','s','a','g','e','`',' ',
663 'F','R','O','M',' ','`','E','r','r','o','r','`',' ',
664 'W','H','E','R','E',' ','`','E','r','r','o','r','`',' ','=',' ',
668 LPWSTR deformated = NULL;
670 deformat_string( package, target, &deformated );
672 /* first try treat the error as a number */
673 row = MSI_QueryGetRecord( package->db, query, deformated );
676 LPCWSTR error = MSI_RecordGetString( row, 1 );
677 MessageBoxW( NULL, error, NULL, MB_OK );
678 msiobj_release( &row->hdr );
681 MessageBoxW( NULL, deformated, NULL, MB_OK );
683 msi_free( deformated );
685 return ERROR_FUNCTION_FAILED;
688 static UINT HANDLE_CustomType50(MSIPACKAGE *package, LPCWSTR source,
689 LPCWSTR target, const INT type, LPCWSTR action)
692 PROCESS_INFORMATION info;
698 static const WCHAR spc[] = {' ',0};
700 memset(&si,0,sizeof(STARTUPINFOW));
701 memset(&info,0,sizeof(PROCESS_INFORMATION));
703 prop = msi_dup_property( package, source );
705 return ERROR_SUCCESS;
707 deformat_string(package,target,&deformated);
708 len = strlenW(prop) + 2;
710 len += strlenW(deformated);
712 cmd = msi_alloc(sizeof(WCHAR)*len);
718 strcatW(cmd,deformated);
720 msi_free(deformated);
724 TRACE("executing exe %s\n", debugstr_w(cmd));
726 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
727 c_collen, &si, &info);
732 ERR("Unable to execute command %s\n", debugstr_w(cmd));
734 return ERROR_SUCCESS;
738 return process_handle(package, type, info.hThread, info.hProcess, action, NULL);
741 static UINT HANDLE_CustomType34(MSIPACKAGE *package, LPCWSTR source,
742 LPCWSTR target, const INT type, LPCWSTR action)
744 LPWSTR filename, deformated;
746 PROCESS_INFORMATION info;
750 memset(&si,0,sizeof(STARTUPINFOW));
752 filename = resolve_folder(package, source, FALSE, FALSE, NULL);
755 return ERROR_FUNCTION_FAILED;
757 SetCurrentDirectoryW(filename);
760 deformat_string(package,target,&deformated);
763 return ERROR_FUNCTION_FAILED;
765 TRACE("executing exe %s\n", debugstr_w(deformated));
767 rc = CreateProcessW(NULL, deformated, NULL, NULL, FALSE, 0, NULL,
768 c_collen, &si, &info);
772 ERR("Unable to execute command %s\n", debugstr_w(deformated));
773 msi_free(deformated);
774 return ERROR_SUCCESS;
776 msi_free(deformated);
778 prc = process_handle(package, type, info.hThread, info.hProcess, action,
785 void ACTION_FinishCustomActions(MSIPACKAGE* package)
787 struct list *item, *cursor;
790 LIST_FOR_EACH_SAFE( item, cursor, &package->RunningActions )
792 MSIRUNNINGACTION *action = LIST_ENTRY( item, MSIRUNNINGACTION, entry );
794 TRACE("Checking on action %s\n", debugstr_w(action->name));
796 list_remove( &action->entry );
799 GetExitCodeProcess( action->handle, &rc );
801 GetExitCodeThread( action->handle, &rc );
803 if (rc == STILL_ACTIVE)
805 TRACE("Waiting on action %s\n", debugstr_w( action->name) );
806 msi_dialog_check_messages( action->handle );
809 CloseHandle( action->handle );
810 msi_free( action->name );