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);
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);
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 = msi_dup_property( package, action );
189 MSI_SetPropertyW(package,szActionData,actiondata);
191 MSI_SetPropertyW(package,szActionData,szBlank);
192 msi_free(actiondata);
195 else if (!check_execution_scheduling_options(package,action,type))
197 msiobj_release(&row->hdr);
198 return ERROR_SUCCESS;
201 switch (type & CUSTOM_ACTION_TYPE_MASK)
203 case 1: /* DLL file stored in a Binary table stream */
204 rc = HANDLE_CustomType1(package,source,target,type,action);
206 case 2: /* EXE file stored in a Binary table strem */
207 rc = HANDLE_CustomType2(package,source,target,type,action);
209 case 18: /*EXE file installed with package */
210 rc = HANDLE_CustomType18(package,source,target,type,action);
212 case 19: /* Error that halts install */
213 rc = HANDLE_CustomType19(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),
239 msiobj_release(&row->hdr);
244 static UINT store_binary_to_temp(MSIPACKAGE *package, LPCWSTR source,
248 static const WCHAR f1[] = {'m','s','i',0};
251 if (MSI_GetPropertyW(package, cszTempFolder, fmt, &sz)
253 GetTempPathW(MAX_PATH,fmt);
255 if (GetTempFileNameW(fmt,f1,0,tmp_file) == 0)
257 TRACE("Unable to create file\n");
258 return ERROR_FUNCTION_FAILED;
262 /* write out the file */
265 static const WCHAR fmt[] =
266 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
267 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',
268 ' ','`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
272 the_file = CreateFileW(tmp_file, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
273 FILE_ATTRIBUTE_NORMAL, NULL);
275 if (the_file == INVALID_HANDLE_VALUE)
276 return ERROR_FUNCTION_FAILED;
278 row = MSI_QueryGetRecord(package->db, fmt, source);
280 return ERROR_FUNCTION_FAILED;
286 rc = MSI_RecordReadStream(row,2,buffer,&sz);
287 if (rc != ERROR_SUCCESS)
289 ERR("Failed to get stream\n");
290 CloseHandle(the_file);
291 DeleteFileW(tmp_file);
294 WriteFile(the_file,buffer,sz,&write,NULL);
295 } while (sz == 1024);
297 CloseHandle(the_file);
299 msiobj_release(&row->hdr);
302 return ERROR_SUCCESS;
305 static void file_running_action(MSIPACKAGE* package, HANDLE Handle,
306 BOOL process, LPCWSTR name)
308 MSIRUNNINGACTION *action;
310 action = msi_alloc( sizeof(MSIRUNNINGACTION) );
312 action->handle = Handle;
313 action->process = process;
314 action->name = strdupW(name);
316 list_add_tail( &package->RunningActions, &action->entry );
319 static UINT process_action_return_value(UINT type, HANDLE ThreadHandle)
325 GetExitCodeProcess(ThreadHandle,&rc);
328 return ERROR_SUCCESS;
330 return ERROR_FUNCTION_FAILED;
333 GetExitCodeThread(ThreadHandle,&rc);
337 case ERROR_FUNCTION_NOT_CALLED:
339 case ERROR_INSTALL_USEREXIT:
340 case ERROR_INSTALL_FAILURE:
342 case ERROR_NO_MORE_ITEMS:
343 return ERROR_SUCCESS;
345 ERR("Invalid Return Code %lx\n",rc);
346 return ERROR_INSTALL_FAILURE;
350 static UINT process_handle(MSIPACKAGE* package, UINT type,
351 HANDLE ThreadHandle, HANDLE ProcessHandle,
352 LPCWSTR Name, BOOL *finished)
354 UINT rc = ERROR_SUCCESS;
356 if (!(type & msidbCustomActionTypeAsync))
359 TRACE("Synchronous Execution of action %s\n",debugstr_w(Name));
361 msi_dialog_check_messages(ProcessHandle);
363 msi_dialog_check_messages(ThreadHandle);
365 if (!(type & msidbCustomActionTypeContinue))
368 rc = process_action_return_value(2,ProcessHandle);
370 rc = process_action_return_value(1,ThreadHandle);
373 CloseHandle(ThreadHandle);
375 CloseHandle(ProcessHandle);
381 TRACE("Asynchronous Execution of action %s\n",debugstr_w(Name));
383 if (type & msidbCustomActionTypeContinue)
387 file_running_action(package, ProcessHandle, TRUE, Name);
388 CloseHandle(ThreadHandle);
391 file_running_action(package, ThreadHandle, FALSE, Name);
395 CloseHandle(ThreadHandle);
397 CloseHandle(ProcessHandle);
407 typedef UINT __stdcall CustomEntry(MSIHANDLE);
416 static DWORD WINAPI ACTION_CallDllFunction(thread_struct *stuff)
421 DWORD rc = ERROR_SUCCESS;
423 TRACE("calling function (%s, %s) \n", debugstr_w(stuff->source),
424 debugstr_w(stuff->target));
426 hModule = LoadLibraryW(stuff->source);
429 proc = strdupWtoA( stuff->target );
430 fn = (CustomEntry*)GetProcAddress(hModule,proc);
434 MSIPACKAGE *package = stuff->package;
436 TRACE("Calling function %s\n", proc);
437 hPackage = msiobj_findhandle( &package->hdr );
441 msiobj_release( &package->hdr );
444 ERR("Handle for object %p not found\n", package );
447 ERR("Cannot load functon\n");
450 FreeLibrary(hModule);
453 ERR("Unable to load library\n");
454 msiobj_release( &stuff->package->hdr );
455 msi_free(stuff->source);
456 msi_free(stuff->target);
461 static DWORD WINAPI DllThread(LPVOID info)
463 thread_struct *stuff;
466 TRACE("MSI Thread (0x%lx) started for custom action\n",
467 GetCurrentThreadId());
469 stuff = (thread_struct*)info;
470 rc = ACTION_CallDllFunction(stuff);
472 TRACE("MSI Thread (0x%lx) finished (rc %li)\n",GetCurrentThreadId(), rc);
473 /* clse all handles for this thread */
474 MsiCloseAllHandles();
478 static UINT HANDLE_CustomType1(MSIPACKAGE *package, LPCWSTR source,
479 LPCWSTR target, const INT type, LPCWSTR action)
481 WCHAR tmp_file[MAX_PATH];
485 UINT rc = ERROR_SUCCESS;
486 BOOL finished = FALSE;
488 store_binary_to_temp(package, source, tmp_file);
490 TRACE("Calling function %s from %s\n",debugstr_w(target),
491 debugstr_w(tmp_file));
493 if (!strchrW(tmp_file,'.'))
495 static const WCHAR dot[]={'.',0};
496 strcatW(tmp_file,dot);
499 info = msi_alloc( sizeof(*info) );
500 msiobj_addref( &package->hdr );
501 info->package = package;
502 info->target = strdupW(target);
503 info->source = strdupW(tmp_file);
505 ThreadHandle = CreateThread(NULL,0,DllThread,(LPVOID)info,0,&ThreadId);
507 rc = process_handle(package, type, ThreadHandle, NULL, action, &finished );
510 track_tempfile(package, tmp_file, tmp_file);
512 DeleteFileW(tmp_file);
517 static UINT HANDLE_CustomType2(MSIPACKAGE *package, LPCWSTR source,
518 LPCWSTR target, const INT type, LPCWSTR action)
520 WCHAR tmp_file[MAX_PATH];
522 PROCESS_INFORMATION info;
527 static const WCHAR spc[] = {' ',0};
528 UINT prc = ERROR_SUCCESS;
529 BOOL finished = FALSE;
531 memset(&si,0,sizeof(STARTUPINFOW));
533 store_binary_to_temp(package, source, tmp_file);
535 deformat_string(package,target,&deformated);
537 len = strlenW(tmp_file)+2;
540 len += strlenW(deformated);
542 cmd = msi_alloc(sizeof(WCHAR)*len);
544 strcpyW(cmd,tmp_file);
548 strcatW(cmd,deformated);
550 msi_free(deformated);
553 TRACE("executing exe %s \n",debugstr_w(cmd));
555 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
556 c_collen, &si, &info);
562 ERR("Unable to execute command\n");
563 return ERROR_SUCCESS;
566 prc = process_handle(package, type, info.hThread, info.hProcess, action,
570 track_tempfile(package, tmp_file, tmp_file);
572 DeleteFileW(tmp_file);
577 static UINT HANDLE_CustomType18(MSIPACKAGE *package, LPCWSTR source,
578 LPCWSTR target, const INT type, LPCWSTR action)
581 PROCESS_INFORMATION info;
586 static const WCHAR spc[] = {' ',0};
590 memset(&si,0,sizeof(STARTUPINFOW));
592 file = get_loaded_file(package,source);
594 return ERROR_FUNCTION_FAILED;
596 len = lstrlenW( file->TargetPath );
598 deformat_string(package,target,&deformated);
600 len += strlenW(deformated);
603 cmd = msi_alloc(len * sizeof(WCHAR));
605 lstrcpyW( cmd, file->TargetPath);
609 strcatW(cmd, deformated);
611 msi_free(deformated);
614 TRACE("executing exe %s \n",debugstr_w(cmd));
616 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
617 c_collen, &si, &info);
623 ERR("Unable to execute command\n");
624 return ERROR_SUCCESS;
627 prc = process_handle(package, type, info.hThread, info.hProcess, action,
633 static UINT HANDLE_CustomType19(MSIPACKAGE *package, LPCWSTR source,
634 LPCWSTR target, const INT type, LPCWSTR action)
636 static const WCHAR query[] = {
637 'S','E','L','E','C','T',' ','`','M','e','s','s','a','g','e','`',' ',
638 'F','R','O','M',' ','`','E','r','r','o','r','`',' ',
639 'W','H','E','R','E',' ','`','E','r','r','o','r','`',' ','=',' ',
643 LPWSTR deformated = NULL;
645 deformat_string( package, target, &deformated );
647 /* first try treat the error as a number */
648 row = MSI_QueryGetRecord( package->db, query, deformated );
651 LPCWSTR error = MSI_RecordGetString( row, 1 );
652 MessageBoxW( NULL, error, NULL, MB_OK );
653 msiobj_release( &row->hdr );
656 MessageBoxW( NULL, deformated, NULL, MB_OK );
658 msi_free( deformated );
660 return ERROR_FUNCTION_FAILED;
663 static UINT HANDLE_CustomType50(MSIPACKAGE *package, LPCWSTR source,
664 LPCWSTR target, const INT type, LPCWSTR action)
667 PROCESS_INFORMATION info;
673 static const WCHAR spc[] = {' ',0};
675 memset(&si,0,sizeof(STARTUPINFOW));
676 memset(&info,0,sizeof(PROCESS_INFORMATION));
678 prop = msi_dup_property( package, source );
680 return ERROR_SUCCESS;
682 deformat_string(package,target,&deformated);
683 len = strlenW(prop) + 2;
685 len += strlenW(deformated);
687 cmd = msi_alloc(sizeof(WCHAR)*len);
693 strcatW(cmd,deformated);
695 msi_free(deformated);
699 TRACE("executing exe %s \n",debugstr_w(cmd));
701 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
702 c_collen, &si, &info);
708 ERR("Unable to execute command\n");
709 return ERROR_SUCCESS;
712 return process_handle(package, type, info.hThread, info.hProcess, action, NULL);
715 static UINT HANDLE_CustomType34(MSIPACKAGE *package, LPCWSTR source,
716 LPCWSTR target, const INT type, LPCWSTR action)
718 LPWSTR filename, deformated;
720 PROCESS_INFORMATION info;
724 memset(&si,0,sizeof(STARTUPINFOW));
726 filename = resolve_folder(package, source, FALSE, FALSE, NULL);
729 return ERROR_FUNCTION_FAILED;
731 SetCurrentDirectoryW(filename);
734 deformat_string(package,target,&deformated);
737 return ERROR_FUNCTION_FAILED;
739 TRACE("executing exe %s \n",debugstr_w(deformated));
741 rc = CreateProcessW(NULL, deformated, NULL, NULL, FALSE, 0, NULL,
742 c_collen, &si, &info);
743 msi_free(deformated);
747 ERR("Unable to execute command\n");
748 return ERROR_SUCCESS;
751 prc = process_handle(package, type, info.hThread, info.hProcess, action,
758 void ACTION_FinishCustomActions(MSIPACKAGE* package)
760 struct list *item, *cursor;
763 LIST_FOR_EACH_SAFE( item, cursor, &package->RunningActions )
765 MSIRUNNINGACTION *action = LIST_ENTRY( item, MSIRUNNINGACTION, entry );
767 TRACE("Checking on action %s\n", debugstr_w(action->name));
769 list_remove( &action->entry );
772 GetExitCodeProcess( action->handle, &rc );
774 GetExitCodeThread( action->handle, &rc );
776 if (rc == STILL_ACTIVE)
778 TRACE("Waiting on action %s\n", debugstr_w( action->name) );
779 msi_dialog_check_messages( action->handle );
782 CloseHandle( action->handle );
783 msi_free( action->name );