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
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_CustomType18(MSIPACKAGE *package, LPCWSTR source,
70 LPCWSTR target, const INT type, LPCWSTR action);
71 static UINT HANDLE_CustomType19(MSIPACKAGE *package, LPCWSTR source,
72 LPCWSTR target, const INT type, LPCWSTR action);
73 static UINT HANDLE_CustomType50(MSIPACKAGE *package, LPCWSTR source,
74 LPCWSTR target, const INT type, LPCWSTR action);
75 static UINT HANDLE_CustomType34(MSIPACKAGE *package, LPCWSTR source,
76 LPCWSTR target, const INT type, LPCWSTR action);
79 static BOOL check_execution_scheduling_options(MSIPACKAGE *package, LPCWSTR action, UINT options)
84 if ((options & msidbCustomActionTypeClientRepeat) ==
85 msidbCustomActionTypeClientRepeat)
87 if (!(package->script->InWhatSequence & SEQUENCE_UI &&
88 package->script->InWhatSequence & SEQUENCE_EXEC))
90 TRACE("Skipping action due to dbCustomActionTypeClientRepeat option.\n");
94 else if (options & msidbCustomActionTypeFirstSequence)
96 if (package->script->InWhatSequence & SEQUENCE_UI &&
97 package->script->InWhatSequence & SEQUENCE_EXEC )
99 TRACE("Skipping action due to msidbCustomActionTypeFirstSequence option.\n");
103 else if (options & msidbCustomActionTypeOncePerProcess)
105 if (check_unique_action(package,action))
107 TRACE("Skipping action due to msidbCustomActionTypeOncePerProcess option.\n");
111 register_unique_action(package,action);
117 UINT ACTION_CustomAction(MSIPACKAGE *package,LPCWSTR action, BOOL execute)
119 UINT rc = ERROR_SUCCESS;
121 static const WCHAR ExecSeqQuery[] =
122 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
123 '`','C','u','s','t','o' ,'m','A','c','t','i','o','n','`',
124 ' ','W','H','E','R','E',' ','`','A','c','t','i' ,'o','n','`',' ',
125 '=',' ','\'','%','s','\'',0};
129 WCHAR *deformated=NULL;
131 row = MSI_QueryGetRecord( package->db, ExecSeqQuery, action );
133 return ERROR_CALL_NOT_IMPLEMENTED;
135 type = MSI_RecordGetInteger(row,2);
137 source = load_dynamic_stringW(row,3);
138 target = load_dynamic_stringW(row,4);
140 TRACE("Handling custom action %s (%x %s %s)\n",debugstr_w(action),type,
141 debugstr_w(source), debugstr_w(target));
143 /* handle some of the deferred actions */
144 if (type & msidbCustomActionTypeTSAware)
145 FIXME("msidbCustomActionTypeTSAware not handled\n");
147 if (type & msidbCustomActionTypeInScript)
149 if (type & msidbCustomActionTypeNoImpersonate)
150 FIXME("msidbCustomActionTypeNoImpersonate not handled\n");
152 if (type & msidbCustomActionTypeRollback)
154 FIXME("Rollback only action... rollbacks not supported yet\n");
155 schedule_action(package, ROLLBACK_SCRIPT, action);
156 HeapFree(GetProcessHeap(),0,source);
157 HeapFree(GetProcessHeap(),0,target);
158 msiobj_release(&row->hdr);
159 return ERROR_SUCCESS;
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);
174 HeapFree(GetProcessHeap(),0,source);
175 HeapFree(GetProcessHeap(),0,target);
176 msiobj_release(&row->hdr);
177 return ERROR_SUCCESS;
183 static const WCHAR szActionData[] = {
184 'C','u','s','t','o','m','A','c','t','i','o','n','D','a','t','a',0};
185 static const WCHAR szBlank[] = {0};
186 LPWSTR actiondata = load_dynamic_property(package,action,NULL);
188 MSI_SetPropertyW(package,szActionData,actiondata);
190 MSI_SetPropertyW(package,szActionData,szBlank);
193 else if (!check_execution_scheduling_options(package,action,type))
194 return ERROR_SUCCESS;
196 switch (type & CUSTOM_ACTION_TYPE_MASK)
198 case 1: /* DLL file stored in a Binary table stream */
199 rc = HANDLE_CustomType1(package,source,target,type,action);
201 case 2: /* EXE file stored in a Binary table strem */
202 rc = HANDLE_CustomType2(package,source,target,type,action);
204 case 18: /*EXE file installed with package */
205 rc = HANDLE_CustomType18(package,source,target,type,action);
207 case 19: /* Error that halts install */
208 rc = HANDLE_CustomType19(package,source,target,type,action);
210 case 50: /*EXE file specified by a property value */
211 rc = HANDLE_CustomType50(package,source,target,type,action);
213 case 34: /*EXE to be run in specified directory */
214 rc = HANDLE_CustomType34(package,source,target,type,action);
216 case 35: /* Directory set with formatted text. */
217 deformat_string(package,target,&deformated);
218 MSI_SetTargetPathW(package, source, deformated);
219 HeapFree(GetProcessHeap(),0,deformated);
221 case 51: /* Property set with formatted text. */
222 deformat_string(package,target,&deformated);
223 rc = MSI_SetPropertyW(package,source,deformated);
224 HeapFree(GetProcessHeap(),0,deformated);
227 FIXME("UNHANDLED ACTION TYPE %i (%s %s)\n",
228 type & CUSTOM_ACTION_TYPE_MASK, debugstr_w(source),
232 HeapFree(GetProcessHeap(),0,source);
233 HeapFree(GetProcessHeap(),0,target);
234 msiobj_release(&row->hdr);
239 static UINT store_binary_to_temp(MSIPACKAGE *package, LPCWSTR source,
243 static const WCHAR f1[] = {'m','s','i',0};
246 if (MSI_GetPropertyW(package, cszTempFolder, fmt, &sz)
248 GetTempPathW(MAX_PATH,fmt);
250 if (GetTempFileNameW(fmt,f1,0,tmp_file) == 0)
252 TRACE("Unable to create file\n");
253 return ERROR_FUNCTION_FAILED;
257 /* write out the file */
260 static const WCHAR fmt[] =
261 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
262 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',
263 ' ','`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
267 the_file = CreateFileW(tmp_file, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
268 FILE_ATTRIBUTE_NORMAL, NULL);
270 if (the_file == INVALID_HANDLE_VALUE)
271 return ERROR_FUNCTION_FAILED;
273 row = MSI_QueryGetRecord(package->db, fmt, source);
275 return ERROR_FUNCTION_FAILED;
281 rc = MSI_RecordReadStream(row,2,buffer,&sz);
282 if (rc != ERROR_SUCCESS)
284 ERR("Failed to get stream\n");
285 CloseHandle(the_file);
286 DeleteFileW(tmp_file);
289 WriteFile(the_file,buffer,sz,&write,NULL);
290 } while (sz == 1024);
292 CloseHandle(the_file);
294 msiobj_release(&row->hdr);
297 return ERROR_SUCCESS;
300 static void file_running_action(MSIPACKAGE* package, HANDLE Handle,
301 BOOL process, LPCWSTR name)
303 MSIRUNNINGACTION *newbuf = NULL;
305 count = package->RunningActionCount;
306 package->RunningActionCount++;
308 newbuf = HeapReAlloc(GetProcessHeap(),0,
309 package->RunningAction,
310 package->RunningActionCount * sizeof(MSIRUNNINGACTION));
312 newbuf = HeapAlloc(GetProcessHeap(),0, sizeof(MSIRUNNINGACTION));
314 newbuf[count].handle = Handle;
315 newbuf[count].process = process;
316 newbuf[count].name = strdupW(name);
318 package->RunningAction = newbuf;
321 static UINT process_action_return_value(UINT type, HANDLE ThreadHandle)
327 GetExitCodeProcess(ThreadHandle,&rc);
330 return ERROR_SUCCESS;
332 return ERROR_FUNCTION_FAILED;
335 GetExitCodeThread(ThreadHandle,&rc);
339 case ERROR_FUNCTION_NOT_CALLED:
341 case ERROR_INSTALL_USEREXIT:
342 case ERROR_INSTALL_FAILURE:
344 case ERROR_NO_MORE_ITEMS:
345 return ERROR_SUCCESS;
347 ERR("Invalid Return Code %lx\n",rc);
348 return ERROR_INSTALL_FAILURE;
352 static UINT process_handle(MSIPACKAGE* package, UINT type,
353 HANDLE ThreadHandle, HANDLE ProcessHandle,
354 LPCWSTR Name, BOOL *finished)
356 UINT rc = ERROR_SUCCESS;
358 if (!(type & msidbCustomActionTypeAsync))
361 TRACE("Synchronous Execution of action %s\n",debugstr_w(Name));
363 msi_dialog_check_messages(ProcessHandle);
365 msi_dialog_check_messages(ThreadHandle);
367 if (!(type & msidbCustomActionTypeContinue))
370 rc = process_action_return_value(2,ProcessHandle);
372 rc = process_action_return_value(1,ThreadHandle);
375 CloseHandle(ThreadHandle);
377 CloseHandle(ProcessHandle);
383 TRACE("Asynchronous Execution of action %s\n",debugstr_w(Name));
385 if (type & msidbCustomActionTypeContinue)
389 file_running_action(package, ProcessHandle, TRUE, Name);
390 CloseHandle(ThreadHandle);
393 file_running_action(package, ThreadHandle, FALSE, Name);
397 CloseHandle(ThreadHandle);
399 CloseHandle(ProcessHandle);
409 typedef UINT __stdcall CustomEntry(MSIHANDLE);
418 static DWORD WINAPI ACTION_CallDllFunction(thread_struct *stuff)
423 DWORD rc = ERROR_SUCCESS;
425 TRACE("calling function (%s, %s) \n", debugstr_w(stuff->source),
426 debugstr_w(stuff->target));
428 hModule = LoadLibraryW(stuff->source);
431 proc = strdupWtoA( stuff->target );
432 fn = (CustomEntry*)GetProcAddress(hModule,proc);
436 MSIPACKAGE *package = stuff->package;
438 TRACE("Calling function %s\n", proc);
439 hPackage = msiobj_findhandle( &package->hdr );
443 msiobj_release( &package->hdr );
446 ERR("Handle for object %p not found\n", package );
449 ERR("Cannot load functon\n");
451 HeapFree(GetProcessHeap(),0,proc);
452 FreeLibrary(hModule);
455 ERR("Unable to load library\n");
456 msiobj_release( &stuff->package->hdr );
457 HeapFree(GetProcessHeap(),0,stuff->source);
458 HeapFree(GetProcessHeap(),0,stuff->target);
459 HeapFree(GetProcessHeap(), 0, stuff);
463 static DWORD WINAPI DllThread(LPVOID info)
465 thread_struct *stuff;
468 TRACE("MSI Thread (0x%lx) started for custom action\n",
469 GetCurrentThreadId());
471 stuff = (thread_struct*)info;
472 rc = ACTION_CallDllFunction(stuff);
474 TRACE("MSI Thread (0x%lx) finished (rc %li)\n",GetCurrentThreadId(), rc);
475 /* clse all handles for this thread */
476 MsiCloseAllHandles();
480 static UINT HANDLE_CustomType1(MSIPACKAGE *package, LPCWSTR source,
481 LPCWSTR target, const INT type, LPCWSTR action)
483 WCHAR tmp_file[MAX_PATH];
487 UINT rc = ERROR_SUCCESS;
488 BOOL finished = FALSE;
490 store_binary_to_temp(package, source, tmp_file);
492 TRACE("Calling function %s from %s\n",debugstr_w(target),
493 debugstr_w(tmp_file));
495 if (!strchrW(tmp_file,'.'))
497 static const WCHAR dot[]={'.',0};
498 strcatW(tmp_file,dot);
501 info = HeapAlloc( GetProcessHeap(), 0, sizeof(*info) );
502 msiobj_addref( &package->hdr );
503 info->package = package;
504 info->target = strdupW(target);
505 info->source = strdupW(tmp_file);
507 ThreadHandle = CreateThread(NULL,0,DllThread,(LPVOID)info,0,&ThreadId);
509 rc = process_handle(package, type, ThreadHandle, NULL, action, &finished );
512 track_tempfile(package, tmp_file, tmp_file);
514 DeleteFileW(tmp_file);
519 static UINT HANDLE_CustomType2(MSIPACKAGE *package, LPCWSTR source,
520 LPCWSTR target, const INT type, LPCWSTR action)
522 WCHAR tmp_file[MAX_PATH];
524 PROCESS_INFORMATION info;
529 static const WCHAR spc[] = {' ',0};
530 UINT prc = ERROR_SUCCESS;
531 BOOL finished = FALSE;
533 memset(&si,0,sizeof(STARTUPINFOW));
535 store_binary_to_temp(package, source, tmp_file);
537 deformat_string(package,target,&deformated);
539 len = strlenW(tmp_file)+2;
542 len += strlenW(deformated);
544 cmd = HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*len);
546 strcpyW(cmd,tmp_file);
550 strcatW(cmd,deformated);
552 HeapFree(GetProcessHeap(),0,deformated);
555 TRACE("executing exe %s \n",debugstr_w(cmd));
557 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
558 c_collen, &si, &info);
560 HeapFree(GetProcessHeap(),0,cmd);
564 ERR("Unable to execute command\n");
565 return ERROR_SUCCESS;
568 prc = process_handle(package, type, info.hThread, info.hProcess, action,
572 track_tempfile(package, tmp_file, tmp_file);
574 DeleteFileW(tmp_file);
579 static UINT HANDLE_CustomType18(MSIPACKAGE *package, LPCWSTR source,
580 LPCWSTR target, const INT type, LPCWSTR action)
583 PROCESS_INFORMATION info;
588 static const WCHAR spc[] = {' ',0};
592 memset(&si,0,sizeof(STARTUPINFOW));
594 file = get_loaded_file(package,source);
596 return ERROR_FUNCTION_FAILED;
598 len = lstrlenW( file->TargetPath );
600 deformat_string(package,target,&deformated);
602 len += strlenW(deformated);
605 cmd = HeapAlloc(GetProcessHeap(),0,len * sizeof(WCHAR));
607 lstrcpyW( cmd, file->TargetPath);
611 strcatW(cmd, deformated);
613 HeapFree(GetProcessHeap(),0,deformated);
616 TRACE("executing exe %s \n",debugstr_w(cmd));
618 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
619 c_collen, &si, &info);
621 HeapFree(GetProcessHeap(),0,cmd);
625 ERR("Unable to execute command\n");
626 return ERROR_SUCCESS;
629 prc = process_handle(package, type, info.hThread, info.hProcess, action,
635 static UINT HANDLE_CustomType19(MSIPACKAGE *package, LPCWSTR source,
636 LPCWSTR target, const INT type, LPCWSTR action)
638 static const WCHAR query[] = {
639 'S','E','L','E','C','T',' ','`','M','e','s','s','a','g','e','`',' ',
640 'F','R','O','M',' ','`','E','r','r','o','r','`',' ',
641 'W','H','E','R','E',' ','`','E','r','r','o','r','`',' ','=',' ',
645 LPWSTR deformated = NULL;
647 deformat_string( package, target, &deformated );
649 /* first try treat the error as a number */
650 row = MSI_QueryGetRecord( package->db, query, deformated );
653 LPCWSTR error = MSI_RecordGetString( row, 1 );
654 MessageBoxW( NULL, error, NULL, MB_OK );
655 msiobj_release( &row->hdr );
658 MessageBoxW( NULL, deformated, NULL, MB_OK );
660 HeapFree( GetProcessHeap(), 0, deformated );
662 return ERROR_FUNCTION_FAILED;
665 static UINT HANDLE_CustomType50(MSIPACKAGE *package, LPCWSTR source,
666 LPCWSTR target, const INT type, LPCWSTR action)
669 PROCESS_INFORMATION info;
676 static const WCHAR spc[] = {' ',0};
678 memset(&si,0,sizeof(STARTUPINFOW));
679 memset(&info,0,sizeof(PROCESS_INFORMATION));
681 prop = load_dynamic_property(package,source,&prc);
683 return ERROR_SUCCESS;
685 deformat_string(package,target,&deformated);
686 len = strlenW(prop) + 2;
688 len += strlenW(deformated);
690 cmd = HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*len);
696 strcatW(cmd,deformated);
698 HeapFree(GetProcessHeap(),0,deformated);
701 TRACE("executing exe %s \n",debugstr_w(cmd));
703 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
704 c_collen, &si, &info);
706 HeapFree(GetProcessHeap(),0,cmd);
710 ERR("Unable to execute command\n");
711 return ERROR_SUCCESS;
714 prc = process_handle(package, type, info.hThread, info.hProcess, action,
720 static UINT HANDLE_CustomType34(MSIPACKAGE *package, LPCWSTR source,
721 LPCWSTR target, const INT type, LPCWSTR action)
723 LPWSTR filename, deformated;
725 PROCESS_INFORMATION info;
729 memset(&si,0,sizeof(STARTUPINFOW));
731 filename = resolve_folder(package, source, FALSE, FALSE, NULL);
734 return ERROR_FUNCTION_FAILED;
736 SetCurrentDirectoryW(filename);
737 HeapFree(GetProcessHeap(),0,filename);
739 deformat_string(package,target,&deformated);
742 return ERROR_FUNCTION_FAILED;
744 TRACE("executing exe %s \n",debugstr_w(deformated));
746 rc = CreateProcessW(NULL, deformated, NULL, NULL, FALSE, 0, NULL,
747 c_collen, &si, &info);
748 HeapFree(GetProcessHeap(),0,deformated);
752 ERR("Unable to execute command\n");
753 return ERROR_SUCCESS;
756 prc = process_handle(package, type, info.hThread, info.hProcess, action,
763 void ACTION_FinishCustomActions(MSIPACKAGE* package)
768 for (i = 0; i < package->RunningActionCount; i++)
770 TRACE("Checking on action %s\n",
771 debugstr_w(package->RunningAction[i].name));
773 if (package->RunningAction[i].process)
774 GetExitCodeProcess(package->RunningAction[i].handle, &rc);
776 GetExitCodeThread(package->RunningAction[i].handle, &rc);
778 if (rc == STILL_ACTIVE)
780 TRACE("Waiting on action %s\n",
781 debugstr_w(package->RunningAction[i].name));
782 msi_dialog_check_messages(package->RunningAction[i].handle);
785 HeapFree(GetProcessHeap(),0,package->RunningAction[i].name);
786 CloseHandle(package->RunningAction[i].handle);
789 HeapFree(GetProcessHeap(),0,package->RunningAction);