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_CustomType18(MSIPACKAGE *package, LPCWSTR source,
71 LPCWSTR target, const INT type, LPCWSTR action);
72 static UINT HANDLE_CustomType19(MSIPACKAGE *package, LPCWSTR source,
73 LPCWSTR target, const INT type, LPCWSTR action);
74 static UINT HANDLE_CustomType50(MSIPACKAGE *package, LPCWSTR source,
75 LPCWSTR target, const INT type, LPCWSTR action);
76 static UINT HANDLE_CustomType34(MSIPACKAGE *package, LPCWSTR source,
77 LPCWSTR target, const INT type, LPCWSTR action);
80 static BOOL check_execution_scheduling_options(MSIPACKAGE *package, LPCWSTR action, UINT options)
85 if ((options & msidbCustomActionTypeClientRepeat) ==
86 msidbCustomActionTypeClientRepeat)
88 if (!(package->script->InWhatSequence & SEQUENCE_UI &&
89 package->script->InWhatSequence & SEQUENCE_EXEC))
91 TRACE("Skipping action due to dbCustomActionTypeClientRepeat option.\n");
95 else if (options & msidbCustomActionTypeFirstSequence)
97 if (package->script->InWhatSequence & SEQUENCE_UI &&
98 package->script->InWhatSequence & SEQUENCE_EXEC )
100 TRACE("Skipping action due to msidbCustomActionTypeFirstSequence option.\n");
104 else if (options & msidbCustomActionTypeOncePerProcess)
106 if (check_unique_action(package,action))
108 TRACE("Skipping action due to msidbCustomActionTypeOncePerProcess option.\n");
112 register_unique_action(package,action);
118 UINT ACTION_CustomAction(MSIPACKAGE *package,LPCWSTR action, BOOL execute)
120 UINT rc = ERROR_SUCCESS;
122 static const WCHAR ExecSeqQuery[] =
123 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
124 '`','C','u','s','t','o' ,'m','A','c','t','i','o','n','`',
125 ' ','W','H','E','R','E',' ','`','A','c','t','i' ,'o','n','`',' ',
126 '=',' ','\'','%','s','\'',0};
130 WCHAR *deformated=NULL;
132 row = MSI_QueryGetRecord( package->db, ExecSeqQuery, action );
134 return ERROR_CALL_NOT_IMPLEMENTED;
136 type = MSI_RecordGetInteger(row,2);
138 source = load_dynamic_stringW(row,3);
139 target = load_dynamic_stringW(row,4);
141 TRACE("Handling custom action %s (%x %s %s)\n",debugstr_w(action),type,
142 debugstr_w(source), debugstr_w(target));
144 /* handle some of the deferred actions */
145 if (type & msidbCustomActionTypeTSAware)
146 FIXME("msidbCustomActionTypeTSAware not handled\n");
148 if (type & msidbCustomActionTypeInScript)
150 if (type & msidbCustomActionTypeNoImpersonate)
151 FIXME("msidbCustomActionTypeNoImpersonate not handled\n");
153 if (type & msidbCustomActionTypeRollback)
155 FIXME("Rollback only action... rollbacks not supported yet\n");
156 schedule_action(package, ROLLBACK_SCRIPT, action);
157 HeapFree(GetProcessHeap(),0,source);
158 HeapFree(GetProcessHeap(),0,target);
159 msiobj_release(&row->hdr);
160 return ERROR_SUCCESS;
164 if (type & msidbCustomActionTypeCommit)
166 TRACE("Deferring Commit Action!\n");
167 schedule_action(package, COMMIT_SCRIPT, action);
171 TRACE("Deferring Action!\n");
172 schedule_action(package, INSTALL_SCRIPT, action);
175 HeapFree(GetProcessHeap(),0,source);
176 HeapFree(GetProcessHeap(),0,target);
177 msiobj_release(&row->hdr);
178 return ERROR_SUCCESS;
184 static const WCHAR szActionData[] = {
185 'C','u','s','t','o','m','A','c','t','i','o','n','D','a','t','a',0};
186 static const WCHAR szBlank[] = {0};
187 LPWSTR actiondata = load_dynamic_property(package,action,NULL);
189 MSI_SetPropertyW(package,szActionData,actiondata);
191 MSI_SetPropertyW(package,szActionData,szBlank);
192 HeapFree(GetProcessHeap(),0,actiondata);
195 else if (!check_execution_scheduling_options(package,action,type))
196 return ERROR_SUCCESS;
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);
212 case 50: /*EXE file specified by a property value */
213 rc = HANDLE_CustomType50(package,source,target,type,action);
215 case 34: /*EXE to be run in specified directory */
216 rc = HANDLE_CustomType34(package,source,target,type,action);
218 case 35: /* Directory set with formatted text. */
219 deformat_string(package,target,&deformated);
220 MSI_SetTargetPathW(package, source, deformated);
221 HeapFree(GetProcessHeap(),0,deformated);
223 case 51: /* Property set with formatted text. */
224 deformat_string(package,target,&deformated);
225 rc = MSI_SetPropertyW(package,source,deformated);
226 HeapFree(GetProcessHeap(),0,deformated);
229 FIXME("UNHANDLED ACTION TYPE %i (%s %s)\n",
230 type & CUSTOM_ACTION_TYPE_MASK, debugstr_w(source),
234 HeapFree(GetProcessHeap(),0,source);
235 HeapFree(GetProcessHeap(),0,target);
236 msiobj_release(&row->hdr);
241 static UINT store_binary_to_temp(MSIPACKAGE *package, LPCWSTR source,
245 static const WCHAR f1[] = {'m','s','i',0};
248 if (MSI_GetPropertyW(package, cszTempFolder, fmt, &sz)
250 GetTempPathW(MAX_PATH,fmt);
252 if (GetTempFileNameW(fmt,f1,0,tmp_file) == 0)
254 TRACE("Unable to create file\n");
255 return ERROR_FUNCTION_FAILED;
259 /* write out the file */
262 static const WCHAR fmt[] =
263 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
264 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',
265 ' ','`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
269 the_file = CreateFileW(tmp_file, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
270 FILE_ATTRIBUTE_NORMAL, NULL);
272 if (the_file == INVALID_HANDLE_VALUE)
273 return ERROR_FUNCTION_FAILED;
275 row = MSI_QueryGetRecord(package->db, fmt, source);
277 return ERROR_FUNCTION_FAILED;
283 rc = MSI_RecordReadStream(row,2,buffer,&sz);
284 if (rc != ERROR_SUCCESS)
286 ERR("Failed to get stream\n");
287 CloseHandle(the_file);
288 DeleteFileW(tmp_file);
291 WriteFile(the_file,buffer,sz,&write,NULL);
292 } while (sz == 1024);
294 CloseHandle(the_file);
296 msiobj_release(&row->hdr);
299 return ERROR_SUCCESS;
302 static void file_running_action(MSIPACKAGE* package, HANDLE Handle,
303 BOOL process, LPCWSTR name)
305 MSIRUNNINGACTION *action;
307 action = HeapAlloc( GetProcessHeap(), 0, sizeof(MSIRUNNINGACTION) );
309 action->handle = Handle;
310 action->process = process;
311 action->name = strdupW(name);
313 list_add_tail( &package->RunningActions, &action->entry );
316 static UINT process_action_return_value(UINT type, HANDLE ThreadHandle)
322 GetExitCodeProcess(ThreadHandle,&rc);
325 return ERROR_SUCCESS;
327 return ERROR_FUNCTION_FAILED;
330 GetExitCodeThread(ThreadHandle,&rc);
334 case ERROR_FUNCTION_NOT_CALLED:
336 case ERROR_INSTALL_USEREXIT:
337 case ERROR_INSTALL_FAILURE:
339 case ERROR_NO_MORE_ITEMS:
340 return ERROR_SUCCESS;
342 ERR("Invalid Return Code %lx\n",rc);
343 return ERROR_INSTALL_FAILURE;
347 static UINT process_handle(MSIPACKAGE* package, UINT type,
348 HANDLE ThreadHandle, HANDLE ProcessHandle,
349 LPCWSTR Name, BOOL *finished)
351 UINT rc = ERROR_SUCCESS;
353 if (!(type & msidbCustomActionTypeAsync))
356 TRACE("Synchronous Execution of action %s\n",debugstr_w(Name));
358 msi_dialog_check_messages(ProcessHandle);
360 msi_dialog_check_messages(ThreadHandle);
362 if (!(type & msidbCustomActionTypeContinue))
365 rc = process_action_return_value(2,ProcessHandle);
367 rc = process_action_return_value(1,ThreadHandle);
370 CloseHandle(ThreadHandle);
372 CloseHandle(ProcessHandle);
378 TRACE("Asynchronous Execution of action %s\n",debugstr_w(Name));
380 if (type & msidbCustomActionTypeContinue)
384 file_running_action(package, ProcessHandle, TRUE, Name);
385 CloseHandle(ThreadHandle);
388 file_running_action(package, ThreadHandle, FALSE, Name);
392 CloseHandle(ThreadHandle);
394 CloseHandle(ProcessHandle);
404 typedef UINT __stdcall CustomEntry(MSIHANDLE);
413 static DWORD WINAPI ACTION_CallDllFunction(thread_struct *stuff)
418 DWORD rc = ERROR_SUCCESS;
420 TRACE("calling function (%s, %s) \n", debugstr_w(stuff->source),
421 debugstr_w(stuff->target));
423 hModule = LoadLibraryW(stuff->source);
426 proc = strdupWtoA( stuff->target );
427 fn = (CustomEntry*)GetProcAddress(hModule,proc);
431 MSIPACKAGE *package = stuff->package;
433 TRACE("Calling function %s\n", proc);
434 hPackage = msiobj_findhandle( &package->hdr );
438 msiobj_release( &package->hdr );
441 ERR("Handle for object %p not found\n", package );
444 ERR("Cannot load functon\n");
446 HeapFree(GetProcessHeap(),0,proc);
447 FreeLibrary(hModule);
450 ERR("Unable to load library\n");
451 msiobj_release( &stuff->package->hdr );
452 HeapFree(GetProcessHeap(),0,stuff->source);
453 HeapFree(GetProcessHeap(),0,stuff->target);
454 HeapFree(GetProcessHeap(), 0, stuff);
458 static DWORD WINAPI DllThread(LPVOID info)
460 thread_struct *stuff;
463 TRACE("MSI Thread (0x%lx) started for custom action\n",
464 GetCurrentThreadId());
466 stuff = (thread_struct*)info;
467 rc = ACTION_CallDllFunction(stuff);
469 TRACE("MSI Thread (0x%lx) finished (rc %li)\n",GetCurrentThreadId(), rc);
470 /* clse all handles for this thread */
471 MsiCloseAllHandles();
475 static UINT HANDLE_CustomType1(MSIPACKAGE *package, LPCWSTR source,
476 LPCWSTR target, const INT type, LPCWSTR action)
478 WCHAR tmp_file[MAX_PATH];
482 UINT rc = ERROR_SUCCESS;
483 BOOL finished = FALSE;
485 store_binary_to_temp(package, source, tmp_file);
487 TRACE("Calling function %s from %s\n",debugstr_w(target),
488 debugstr_w(tmp_file));
490 if (!strchrW(tmp_file,'.'))
492 static const WCHAR dot[]={'.',0};
493 strcatW(tmp_file,dot);
496 info = HeapAlloc( GetProcessHeap(), 0, sizeof(*info) );
497 msiobj_addref( &package->hdr );
498 info->package = package;
499 info->target = strdupW(target);
500 info->source = strdupW(tmp_file);
502 ThreadHandle = CreateThread(NULL,0,DllThread,(LPVOID)info,0,&ThreadId);
504 rc = process_handle(package, type, ThreadHandle, NULL, action, &finished );
507 track_tempfile(package, tmp_file, tmp_file);
509 DeleteFileW(tmp_file);
514 static UINT HANDLE_CustomType2(MSIPACKAGE *package, LPCWSTR source,
515 LPCWSTR target, const INT type, LPCWSTR action)
517 WCHAR tmp_file[MAX_PATH];
519 PROCESS_INFORMATION info;
524 static const WCHAR spc[] = {' ',0};
525 UINT prc = ERROR_SUCCESS;
526 BOOL finished = FALSE;
528 memset(&si,0,sizeof(STARTUPINFOW));
530 store_binary_to_temp(package, source, tmp_file);
532 deformat_string(package,target,&deformated);
534 len = strlenW(tmp_file)+2;
537 len += strlenW(deformated);
539 cmd = HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*len);
541 strcpyW(cmd,tmp_file);
545 strcatW(cmd,deformated);
547 HeapFree(GetProcessHeap(),0,deformated);
550 TRACE("executing exe %s \n",debugstr_w(cmd));
552 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
553 c_collen, &si, &info);
555 HeapFree(GetProcessHeap(),0,cmd);
559 ERR("Unable to execute command\n");
560 return ERROR_SUCCESS;
563 prc = process_handle(package, type, info.hThread, info.hProcess, action,
567 track_tempfile(package, tmp_file, tmp_file);
569 DeleteFileW(tmp_file);
574 static UINT HANDLE_CustomType18(MSIPACKAGE *package, LPCWSTR source,
575 LPCWSTR target, const INT type, LPCWSTR action)
578 PROCESS_INFORMATION info;
583 static const WCHAR spc[] = {' ',0};
587 memset(&si,0,sizeof(STARTUPINFOW));
589 file = get_loaded_file(package,source);
591 return ERROR_FUNCTION_FAILED;
593 len = lstrlenW( file->TargetPath );
595 deformat_string(package,target,&deformated);
597 len += strlenW(deformated);
600 cmd = HeapAlloc(GetProcessHeap(),0,len * sizeof(WCHAR));
602 lstrcpyW( cmd, file->TargetPath);
606 strcatW(cmd, deformated);
608 HeapFree(GetProcessHeap(),0,deformated);
611 TRACE("executing exe %s \n",debugstr_w(cmd));
613 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
614 c_collen, &si, &info);
616 HeapFree(GetProcessHeap(),0,cmd);
620 ERR("Unable to execute command\n");
621 return ERROR_SUCCESS;
624 prc = process_handle(package, type, info.hThread, info.hProcess, action,
630 static UINT HANDLE_CustomType19(MSIPACKAGE *package, LPCWSTR source,
631 LPCWSTR target, const INT type, LPCWSTR action)
633 static const WCHAR query[] = {
634 'S','E','L','E','C','T',' ','`','M','e','s','s','a','g','e','`',' ',
635 'F','R','O','M',' ','`','E','r','r','o','r','`',' ',
636 'W','H','E','R','E',' ','`','E','r','r','o','r','`',' ','=',' ',
640 LPWSTR deformated = NULL;
642 deformat_string( package, target, &deformated );
644 /* first try treat the error as a number */
645 row = MSI_QueryGetRecord( package->db, query, deformated );
648 LPCWSTR error = MSI_RecordGetString( row, 1 );
649 MessageBoxW( NULL, error, NULL, MB_OK );
650 msiobj_release( &row->hdr );
653 MessageBoxW( NULL, deformated, NULL, MB_OK );
655 HeapFree( GetProcessHeap(), 0, deformated );
657 return ERROR_FUNCTION_FAILED;
660 static UINT HANDLE_CustomType50(MSIPACKAGE *package, LPCWSTR source,
661 LPCWSTR target, const INT type, LPCWSTR action)
664 PROCESS_INFORMATION info;
671 static const WCHAR spc[] = {' ',0};
673 memset(&si,0,sizeof(STARTUPINFOW));
674 memset(&info,0,sizeof(PROCESS_INFORMATION));
676 prop = load_dynamic_property(package,source,&prc);
678 return ERROR_SUCCESS;
680 deformat_string(package,target,&deformated);
681 len = strlenW(prop) + 2;
683 len += strlenW(deformated);
685 cmd = HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*len);
691 strcatW(cmd,deformated);
693 HeapFree(GetProcessHeap(),0,deformated);
695 HeapFree(GetProcessHeap(),0,prop);
697 TRACE("executing exe %s \n",debugstr_w(cmd));
699 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
700 c_collen, &si, &info);
702 HeapFree(GetProcessHeap(),0,cmd);
706 ERR("Unable to execute command\n");
707 return ERROR_SUCCESS;
710 prc = process_handle(package, type, info.hThread, info.hProcess, action,
716 static UINT HANDLE_CustomType34(MSIPACKAGE *package, LPCWSTR source,
717 LPCWSTR target, const INT type, LPCWSTR action)
719 LPWSTR filename, deformated;
721 PROCESS_INFORMATION info;
725 memset(&si,0,sizeof(STARTUPINFOW));
727 filename = resolve_folder(package, source, FALSE, FALSE, NULL);
730 return ERROR_FUNCTION_FAILED;
732 SetCurrentDirectoryW(filename);
733 HeapFree(GetProcessHeap(),0,filename);
735 deformat_string(package,target,&deformated);
738 return ERROR_FUNCTION_FAILED;
740 TRACE("executing exe %s \n",debugstr_w(deformated));
742 rc = CreateProcessW(NULL, deformated, NULL, NULL, FALSE, 0, NULL,
743 c_collen, &si, &info);
744 HeapFree(GetProcessHeap(),0,deformated);
748 ERR("Unable to execute command\n");
749 return ERROR_SUCCESS;
752 prc = process_handle(package, type, info.hThread, info.hProcess, action,
759 void ACTION_FinishCustomActions(MSIPACKAGE* package)
761 struct list *item, *cursor;
764 LIST_FOR_EACH_SAFE( item, cursor, &package->RunningActions )
766 MSIRUNNINGACTION *action = LIST_ENTRY( item, MSIRUNNINGACTION, entry );
768 TRACE("Checking on action %s\n", debugstr_w(action->name));
770 list_remove( &action->entry );
773 GetExitCodeProcess( action->handle, &rc );
775 GetExitCodeThread( action->handle, &rc );
777 if (rc == STILL_ACTIVE)
779 TRACE("Waiting on action %s\n", debugstr_w( action->name) );
780 msi_dialog_check_messages( action->handle );
783 CloseHandle( action->handle );
784 HeapFree( GetProcessHeap(), 0, action->name );
785 HeapFree( GetProcessHeap(), 0, action );