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"
40 #include "msvcrt/fcntl.h"
47 #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
64 static UINT HANDLE_CustomType1(MSIPACKAGE *package, LPCWSTR source,
65 LPCWSTR target, const INT type, LPCWSTR action);
66 static UINT HANDLE_CustomType2(MSIPACKAGE *package, LPCWSTR source,
67 LPCWSTR target, const INT type, LPCWSTR action);
68 static UINT HANDLE_CustomType18(MSIPACKAGE *package, LPCWSTR source,
69 LPCWSTR target, const INT type, LPCWSTR action);
70 static UINT HANDLE_CustomType19(MSIPACKAGE *package, LPCWSTR source,
71 LPCWSTR target, const INT type, LPCWSTR action);
72 static UINT HANDLE_CustomType50(MSIPACKAGE *package, LPCWSTR source,
73 LPCWSTR target, const INT type, LPCWSTR action);
74 static UINT HANDLE_CustomType34(MSIPACKAGE *package, LPCWSTR source,
75 LPCWSTR target, const INT type, LPCWSTR action);
77 UINT ACTION_CustomAction(MSIPACKAGE *package,LPCWSTR action, BOOL execute)
79 UINT rc = ERROR_SUCCESS;
81 static const WCHAR ExecSeqQuery[] =
82 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
83 '`','C','u','s','t','o' ,'m','A','c','t','i','o','n','`',
84 ' ','W','H','E','R','E',' ','`','A','c','t','i' ,'o','n','`',' ',
85 '=',' ','\'','%','s','\'',0};
89 WCHAR *deformated=NULL;
91 row = MSI_QueryGetRecord( package->db, ExecSeqQuery, action );
93 return ERROR_CALL_NOT_IMPLEMENTED;
95 type = MSI_RecordGetInteger(row,2);
97 source = load_dynamic_stringW(row,3);
98 target = load_dynamic_stringW(row,4);
100 TRACE("Handling custom action %s (%x %s %s)\n",debugstr_w(action),type,
101 debugstr_w(source), debugstr_w(target));
103 /* handle some of the deferred actions */
108 FIXME("Rollback only action... rollbacks not supported yet\n");
109 schedule_action(package, ROLLBACK_SCRIPT, action);
110 HeapFree(GetProcessHeap(),0,source);
111 HeapFree(GetProcessHeap(),0,target);
112 msiobj_release(&row->hdr);
113 return ERROR_SUCCESS;
119 TRACE("Deferring Commit Action!\n");
120 schedule_action(package, COMMIT_SCRIPT, action);
124 TRACE("Deferring Action!\n");
125 schedule_action(package, INSTALL_SCRIPT, action);
128 HeapFree(GetProcessHeap(),0,source);
129 HeapFree(GetProcessHeap(),0,target);
130 msiobj_release(&row->hdr);
131 return ERROR_SUCCESS;
137 static const WCHAR szActionData[] = {
138 'C','u','s','t','o','m','A','c','t','i','o','n','D','a','t','a',0};
139 LPWSTR actiondata = load_dynamic_property(package,action,NULL);
141 MSI_SetPropertyW(package,szActionData,actiondata);
145 switch (type & CUSTOM_ACTION_TYPE_MASK)
147 case 1: /* DLL file stored in a Binary table stream */
148 rc = HANDLE_CustomType1(package,source,target,type,action);
150 case 2: /* EXE file stored in a Binary table strem */
151 rc = HANDLE_CustomType2(package,source,target,type,action);
153 case 18: /*EXE file installed with package */
154 rc = HANDLE_CustomType18(package,source,target,type,action);
156 case 19: /* Error that halts install */
157 rc = HANDLE_CustomType19(package,source,target,type,action);
159 case 50: /*EXE file specified by a property value */
160 rc = HANDLE_CustomType50(package,source,target,type,action);
162 case 34: /*EXE to be run in specified directory */
163 rc = HANDLE_CustomType34(package,source,target,type,action);
165 case 35: /* Directory set with formatted text. */
166 deformat_string(package,target,&deformated);
167 MSI_SetTargetPathW(package, source, deformated);
168 HeapFree(GetProcessHeap(),0,deformated);
170 case 51: /* Property set with formatted text. */
171 deformat_string(package,target,&deformated);
172 rc = MSI_SetPropertyW(package,source,deformated);
173 HeapFree(GetProcessHeap(),0,deformated);
176 FIXME("UNHANDLED ACTION TYPE %i (%s %s)\n",
177 type & CUSTOM_ACTION_TYPE_MASK, debugstr_w(source),
181 HeapFree(GetProcessHeap(),0,source);
182 HeapFree(GetProcessHeap(),0,target);
183 msiobj_release(&row->hdr);
188 static UINT store_binary_to_temp(MSIPACKAGE *package, LPCWSTR source,
192 static const WCHAR f1[] = {'m','s','i',0};
195 if (MSI_GetPropertyW(package, cszTempFolder, fmt, &sz)
197 GetTempPathW(MAX_PATH,fmt);
199 if (GetTempFileNameW(fmt,f1,0,tmp_file) == 0)
201 TRACE("Unable to create file\n");
202 return ERROR_FUNCTION_FAILED;
206 /* write out the file */
209 static const WCHAR fmt[] =
210 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
211 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',
212 ' ','`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
216 the_file = CreateFileW(tmp_file, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
217 FILE_ATTRIBUTE_NORMAL, NULL);
219 if (the_file == INVALID_HANDLE_VALUE)
220 return ERROR_FUNCTION_FAILED;
222 row = MSI_QueryGetRecord(package->db, fmt, source);
224 return ERROR_FUNCTION_FAILED;
230 rc = MSI_RecordReadStream(row,2,buffer,&sz);
231 if (rc != ERROR_SUCCESS)
233 ERR("Failed to get stream\n");
234 CloseHandle(the_file);
235 DeleteFileW(tmp_file);
238 WriteFile(the_file,buffer,sz,&write,NULL);
239 } while (sz == 1024);
241 CloseHandle(the_file);
243 msiobj_release(&row->hdr);
246 return ERROR_SUCCESS;
249 static void file_running_action(MSIPACKAGE* package, HANDLE Handle,
250 BOOL process, LPCWSTR name)
252 MSIRUNNINGACTION *newbuf = NULL;
254 count = package->RunningActionCount;
255 package->RunningActionCount++;
257 newbuf = HeapReAlloc(GetProcessHeap(),0,
258 package->RunningAction,
259 package->RunningActionCount * sizeof(MSIRUNNINGACTION));
261 newbuf = HeapAlloc(GetProcessHeap(),0, sizeof(MSIRUNNINGACTION));
263 newbuf[count].handle = Handle;
264 newbuf[count].process = process;
265 newbuf[count].name = strdupW(name);
267 package->RunningAction = newbuf;
270 static UINT process_action_return_value(UINT type, HANDLE ThreadHandle)
276 GetExitCodeProcess(ThreadHandle,&rc);
279 return ERROR_SUCCESS;
281 return ERROR_FUNCTION_FAILED;
284 GetExitCodeThread(ThreadHandle,&rc);
288 case ERROR_FUNCTION_NOT_CALLED:
290 case ERROR_INSTALL_USEREXIT:
291 case ERROR_INSTALL_FAILURE:
293 case ERROR_NO_MORE_ITEMS:
294 return ERROR_SUCCESS;
296 ERR("Invalid Return Code %lx\n",rc);
297 return ERROR_INSTALL_FAILURE;
301 static UINT process_handle(MSIPACKAGE* package, UINT type,
302 HANDLE ThreadHandle, HANDLE ProcessHandle,
303 LPCWSTR Name, BOOL *finished)
305 UINT rc = ERROR_SUCCESS;
310 TRACE("Synchronous Execution of action %s\n",debugstr_w(Name));
312 msi_dialog_check_messages(ProcessHandle);
314 msi_dialog_check_messages(ThreadHandle);
319 rc = process_action_return_value(2,ProcessHandle);
321 rc = process_action_return_value(1,ThreadHandle);
324 CloseHandle(ThreadHandle);
326 CloseHandle(ProcessHandle);
332 TRACE("Asynchronous Execution of action %s\n",debugstr_w(Name));
338 file_running_action(package, ProcessHandle, TRUE, Name);
339 CloseHandle(ThreadHandle);
342 file_running_action(package, ThreadHandle, FALSE, Name);
346 CloseHandle(ThreadHandle);
348 CloseHandle(ProcessHandle);
358 typedef UINT __stdcall CustomEntry(MSIHANDLE);
367 static DWORD WINAPI ACTION_CallDllFunction(thread_struct *stuff)
372 DWORD rc = ERROR_SUCCESS;
374 TRACE("calling function (%s, %s) \n", debugstr_w(stuff->source),
375 debugstr_w(stuff->target));
377 hModule = LoadLibraryW(stuff->source);
380 proc = strdupWtoA( stuff->target );
381 fn = (CustomEntry*)GetProcAddress(hModule,proc);
385 MSIPACKAGE *package = stuff->package;
387 TRACE("Calling function %s\n", proc);
388 hPackage = msiobj_findhandle( &package->hdr );
392 msiobj_release( &package->hdr );
395 ERR("Handle for object %p not found\n", package );
398 ERR("Cannot load functon\n");
400 HeapFree(GetProcessHeap(),0,proc);
401 FreeLibrary(hModule);
404 ERR("Unable to load library\n");
405 msiobj_release( &stuff->package->hdr );
406 HeapFree(GetProcessHeap(),0,stuff->source);
407 HeapFree(GetProcessHeap(),0,stuff->target);
408 HeapFree(GetProcessHeap(), 0, stuff);
412 static DWORD WINAPI DllThread(LPVOID info)
414 thread_struct *stuff;
417 TRACE("MSI Thread (0x%lx) started for custom action\n",
418 GetCurrentThreadId());
420 stuff = (thread_struct*)info;
421 rc = ACTION_CallDllFunction(stuff);
423 TRACE("MSI Thread (0x%lx) finished (rc %li)\n",GetCurrentThreadId(), rc);
424 /* clse all handles for this thread */
425 MsiCloseAllHandles();
429 static UINT HANDLE_CustomType1(MSIPACKAGE *package, LPCWSTR source,
430 LPCWSTR target, const INT type, LPCWSTR action)
432 WCHAR tmp_file[MAX_PATH];
436 UINT rc = ERROR_SUCCESS;
437 BOOL finished = FALSE;
439 store_binary_to_temp(package, source, tmp_file);
441 TRACE("Calling function %s from %s\n",debugstr_w(target),
442 debugstr_w(tmp_file));
444 if (!strchrW(tmp_file,'.'))
446 static const WCHAR dot[]={'.',0};
447 strcatW(tmp_file,dot);
450 info = HeapAlloc( GetProcessHeap(), 0, sizeof(*info) );
451 msiobj_addref( &package->hdr );
452 info->package = package;
453 info->target = strdupW(target);
454 info->source = strdupW(tmp_file);
456 ThreadHandle = CreateThread(NULL,0,DllThread,(LPVOID)info,0,&ThreadId);
458 rc = process_handle(package, type, ThreadHandle, NULL, action, &finished );
461 track_tempfile(package, tmp_file, tmp_file);
463 DeleteFileW(tmp_file);
468 static UINT HANDLE_CustomType2(MSIPACKAGE *package, LPCWSTR source,
469 LPCWSTR target, const INT type, LPCWSTR action)
471 WCHAR tmp_file[MAX_PATH];
473 PROCESS_INFORMATION info;
478 static const WCHAR spc[] = {' ',0};
479 UINT prc = ERROR_SUCCESS;
480 BOOL finished = FALSE;
482 memset(&si,0,sizeof(STARTUPINFOW));
484 store_binary_to_temp(package, source, tmp_file);
486 deformat_string(package,target,&deformated);
488 len = strlenW(tmp_file)+2;
491 len += strlenW(deformated);
493 cmd = HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*len);
495 strcpyW(cmd,tmp_file);
499 strcatW(cmd,deformated);
501 HeapFree(GetProcessHeap(),0,deformated);
504 TRACE("executing exe %s \n",debugstr_w(cmd));
506 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
507 c_collen, &si, &info);
509 HeapFree(GetProcessHeap(),0,cmd);
513 ERR("Unable to execute command\n");
514 return ERROR_SUCCESS;
517 prc = process_handle(package, type, info.hThread, info.hProcess, action,
521 track_tempfile(package, tmp_file, tmp_file);
523 DeleteFileW(tmp_file);
528 static UINT HANDLE_CustomType18(MSIPACKAGE *package, LPCWSTR source,
529 LPCWSTR target, const INT type, LPCWSTR action)
532 PROCESS_INFORMATION info;
537 static const WCHAR spc[] = {' ',0};
541 memset(&si,0,sizeof(STARTUPINFOW));
543 index = get_loaded_file(package,source);
545 len = strlenW(package->files[index].TargetPath);
547 deformat_string(package,target,&deformated);
549 len += strlenW(deformated);
552 cmd = HeapAlloc(GetProcessHeap(),0,len * sizeof(WCHAR));
554 strcpyW(cmd, package->files[index].TargetPath);
558 strcatW(cmd, deformated);
560 HeapFree(GetProcessHeap(),0,deformated);
563 TRACE("executing exe %s \n",debugstr_w(cmd));
565 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
566 c_collen, &si, &info);
568 HeapFree(GetProcessHeap(),0,cmd);
572 ERR("Unable to execute command\n");
573 return ERROR_SUCCESS;
576 prc = process_handle(package, type, info.hThread, info.hProcess, action,
582 static UINT HANDLE_CustomType19(MSIPACKAGE *package, LPCWSTR source,
583 LPCWSTR target, const INT type, LPCWSTR action)
585 static const WCHAR query[] = {
586 'S','E','L','E','C','T',' ','`','M','e','s','s','a','g','e','`',' ',
587 'F','R','O','M',' ','`','E','r','r','o','r','`',' ',
588 'W','H','E','R','E',' ','`','E','r','r','o','r','`',' ','=',' ',
592 LPWSTR deformated = NULL;
594 deformat_string( package, target, &deformated );
596 /* first try treat the error as a number */
597 row = MSI_QueryGetRecord( package->db, query, deformated );
600 LPCWSTR error = MSI_RecordGetString( row, 1 );
601 MessageBoxW( NULL, error, NULL, MB_OK );
602 msiobj_release( &row->hdr );
605 MessageBoxW( NULL, deformated, NULL, MB_OK );
607 HeapFree( GetProcessHeap(), 0, deformated );
609 return ERROR_FUNCTION_FAILED;
612 static UINT HANDLE_CustomType50(MSIPACKAGE *package, LPCWSTR source,
613 LPCWSTR target, const INT type, LPCWSTR action)
616 PROCESS_INFORMATION info;
623 static const WCHAR spc[] = {' ',0};
625 memset(&si,0,sizeof(STARTUPINFOW));
626 memset(&info,0,sizeof(PROCESS_INFORMATION));
628 prop = load_dynamic_property(package,source,&prc);
630 return ERROR_SUCCESS;
632 deformat_string(package,target,&deformated);
633 len = strlenW(prop) + 2;
635 len += strlenW(deformated);
637 cmd = HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*len);
643 strcatW(cmd,deformated);
645 HeapFree(GetProcessHeap(),0,deformated);
648 TRACE("executing exe %s \n",debugstr_w(cmd));
650 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
651 c_collen, &si, &info);
653 HeapFree(GetProcessHeap(),0,cmd);
657 ERR("Unable to execute command\n");
658 return ERROR_SUCCESS;
661 prc = process_handle(package, type, info.hThread, info.hProcess, action,
667 static UINT HANDLE_CustomType34(MSIPACKAGE *package, LPCWSTR source,
668 LPCWSTR target, const INT type, LPCWSTR action)
670 LPWSTR filename, deformated;
672 PROCESS_INFORMATION info;
676 memset(&si,0,sizeof(STARTUPINFOW));
678 filename = resolve_folder(package, source, FALSE, FALSE, NULL);
681 return ERROR_FUNCTION_FAILED;
683 SetCurrentDirectoryW(filename);
684 HeapFree(GetProcessHeap(),0,filename);
686 deformat_string(package,target,&deformated);
689 return ERROR_FUNCTION_FAILED;
691 TRACE("executing exe %s \n",debugstr_w(deformated));
693 rc = CreateProcessW(NULL, deformated, NULL, NULL, FALSE, 0, NULL,
694 c_collen, &si, &info);
695 HeapFree(GetProcessHeap(),0,deformated);
699 ERR("Unable to execute command\n");
700 return ERROR_SUCCESS;
703 prc = process_handle(package, type, info.hThread, info.hProcess, action,
710 void ACTION_FinishCustomActions(MSIPACKAGE* package)
715 for (i = 0; i < package->RunningActionCount; i++)
717 TRACE("Checking on action %s\n",
718 debugstr_w(package->RunningAction[i].name));
720 if (package->RunningAction[i].process)
721 GetExitCodeProcess(package->RunningAction[i].handle, &rc);
723 GetExitCodeThread(package->RunningAction[i].handle, &rc);
725 if (rc == STILL_ACTIVE)
727 TRACE("Waiting on action %s\n",
728 debugstr_w(package->RunningAction[i].name));
729 msi_dialog_check_messages(package->RunningAction[i].handle);
732 HeapFree(GetProcessHeap(),0,package->RunningAction[i].name);
733 CloseHandle(package->RunningAction[i].handle);
736 HeapFree(GetProcessHeap(),0,package->RunningAction);