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 if (track_tempfile(package, tmp_file, tmp_file)!=0)
217 FIXME("File Name in temp tracking collision\n");
219 the_file = CreateFileW(tmp_file, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
220 FILE_ATTRIBUTE_NORMAL, NULL);
222 if (the_file == INVALID_HANDLE_VALUE)
223 return ERROR_FUNCTION_FAILED;
225 row = MSI_QueryGetRecord(package->db, fmt, source);
227 return ERROR_FUNCTION_FAILED;
233 rc = MSI_RecordReadStream(row,2,buffer,&sz);
234 if (rc != ERROR_SUCCESS)
236 ERR("Failed to get stream\n");
237 CloseHandle(the_file);
238 DeleteFileW(tmp_file);
241 WriteFile(the_file,buffer,sz,&write,NULL);
242 } while (sz == 1024);
244 CloseHandle(the_file);
246 msiobj_release(&row->hdr);
249 return ERROR_SUCCESS;
252 static void file_running_action(MSIPACKAGE* package, HANDLE Handle,
253 BOOL process, LPCWSTR name)
255 MSIRUNNINGACTION *newbuf = NULL;
257 count = package->RunningActionCount;
258 package->RunningActionCount++;
260 newbuf = HeapReAlloc(GetProcessHeap(),0,
261 package->RunningAction,
262 package->RunningActionCount * sizeof(MSIRUNNINGACTION));
264 newbuf = HeapAlloc(GetProcessHeap(),0, sizeof(MSIRUNNINGACTION));
266 newbuf[count].handle = Handle;
267 newbuf[count].process = process;
268 newbuf[count].name = strdupW(name);
270 package->RunningAction = newbuf;
273 static UINT process_action_return_value(UINT type, HANDLE ThreadHandle)
279 GetExitCodeProcess(ThreadHandle,&rc);
282 return ERROR_SUCCESS;
284 return ERROR_FUNCTION_FAILED;
287 GetExitCodeThread(ThreadHandle,&rc);
291 case ERROR_FUNCTION_NOT_CALLED:
293 case ERROR_INSTALL_USEREXIT:
294 case ERROR_INSTALL_FAILURE:
296 case ERROR_NO_MORE_ITEMS:
297 return ERROR_SUCCESS;
299 ERR("Invalid Return Code %lx\n",rc);
300 return ERROR_INSTALL_FAILURE;
304 static UINT process_handle(MSIPACKAGE* package, UINT type,
305 HANDLE ThreadHandle, HANDLE ProcessHandle,
308 UINT rc = ERROR_SUCCESS;
313 TRACE("Synchronous Execution of action %s\n",debugstr_w(Name));
315 msi_dialog_check_messages(ProcessHandle);
317 msi_dialog_check_messages(ThreadHandle);
322 rc = process_action_return_value(2,ProcessHandle);
324 rc = process_action_return_value(1,ThreadHandle);
327 CloseHandle(ThreadHandle);
329 CloseHandle(ProcessHandle);
333 TRACE("Asynchronous Execution of action %s\n",debugstr_w(Name));
339 file_running_action(package, ProcessHandle, TRUE, Name);
340 CloseHandle(ThreadHandle);
343 file_running_action(package, ThreadHandle, FALSE, Name);
347 CloseHandle(ThreadHandle);
349 CloseHandle(ProcessHandle);
357 typedef UINT __stdcall CustomEntry(MSIHANDLE);
366 static DWORD WINAPI ACTION_CallDllFunction(thread_struct *stuff)
371 DWORD rc = ERROR_SUCCESS;
373 TRACE("calling function (%s, %s) \n", debugstr_w(stuff->source),
374 debugstr_w(stuff->target));
376 hModule = LoadLibraryW(stuff->source);
379 proc = strdupWtoA( stuff->target );
380 fn = (CustomEntry*)GetProcAddress(hModule,proc);
384 MSIPACKAGE *package = stuff->package;
386 TRACE("Calling function %s\n", proc);
387 hPackage = msiobj_findhandle( &package->hdr );
391 msiobj_release( &package->hdr );
394 ERR("Handle for object %p not found\n", package );
397 ERR("Cannot load functon\n");
399 HeapFree(GetProcessHeap(),0,proc);
400 FreeLibrary(hModule);
403 ERR("Unable to load library\n");
404 msiobj_release( &stuff->package->hdr );
405 HeapFree(GetProcessHeap(),0,stuff->source);
406 HeapFree(GetProcessHeap(),0,stuff->target);
407 HeapFree(GetProcessHeap(), 0, stuff);
411 static DWORD WINAPI DllThread(LPVOID info)
413 thread_struct *stuff;
416 TRACE("MSI Thread (0x%lx) started for custom action\n",
417 GetCurrentThreadId());
419 stuff = (thread_struct*)info;
420 rc = ACTION_CallDllFunction(stuff);
422 TRACE("MSI Thread (0x%lx) finished (rc %li)\n",GetCurrentThreadId(), rc);
423 /* clse all handles for this thread */
424 MsiCloseAllHandles();
428 static UINT HANDLE_CustomType1(MSIPACKAGE *package, LPCWSTR source,
429 LPCWSTR target, const INT type, LPCWSTR action)
431 WCHAR tmp_file[MAX_PATH];
435 UINT rc = ERROR_SUCCESS;
437 store_binary_to_temp(package, source, tmp_file);
439 TRACE("Calling function %s from %s\n",debugstr_w(target),
440 debugstr_w(tmp_file));
442 if (!strchrW(tmp_file,'.'))
444 static const WCHAR dot[]={'.',0};
445 strcatW(tmp_file,dot);
448 info = HeapAlloc( GetProcessHeap(), 0, sizeof(*info) );
449 msiobj_addref( &package->hdr );
450 info->package = package;
451 info->target = strdupW(target);
452 info->source = strdupW(tmp_file);
454 ThreadHandle = CreateThread(NULL,0,DllThread,(LPVOID)info,0,&ThreadId);
456 rc = process_handle(package, type, ThreadHandle, NULL, action);
461 static UINT HANDLE_CustomType2(MSIPACKAGE *package, LPCWSTR source,
462 LPCWSTR target, const INT type, LPCWSTR action)
464 WCHAR tmp_file[MAX_PATH];
466 PROCESS_INFORMATION info;
471 static const WCHAR spc[] = {' ',0};
472 UINT prc = ERROR_SUCCESS;
474 memset(&si,0,sizeof(STARTUPINFOW));
476 store_binary_to_temp(package, source, tmp_file);
478 deformat_string(package,target,&deformated);
480 len = strlenW(tmp_file)+2;
483 len += strlenW(deformated);
485 cmd = HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*len);
487 strcpyW(cmd,tmp_file);
491 strcatW(cmd,deformated);
493 HeapFree(GetProcessHeap(),0,deformated);
496 TRACE("executing exe %s \n",debugstr_w(cmd));
498 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
499 c_collen, &si, &info);
501 HeapFree(GetProcessHeap(),0,cmd);
505 ERR("Unable to execute command\n");
506 return ERROR_SUCCESS;
509 prc = process_handle(package, type, info.hThread, info.hProcess, action);
514 static UINT HANDLE_CustomType18(MSIPACKAGE *package, LPCWSTR source,
515 LPCWSTR target, const INT type, LPCWSTR action)
518 PROCESS_INFORMATION info;
523 static const WCHAR spc[] = {' ',0};
527 memset(&si,0,sizeof(STARTUPINFOW));
529 index = get_loaded_file(package,source);
531 len = strlenW(package->files[index].TargetPath);
533 deformat_string(package,target,&deformated);
535 len += strlenW(deformated);
538 cmd = HeapAlloc(GetProcessHeap(),0,len * sizeof(WCHAR));
540 strcpyW(cmd, package->files[index].TargetPath);
544 strcatW(cmd, deformated);
546 HeapFree(GetProcessHeap(),0,deformated);
549 TRACE("executing exe %s \n",debugstr_w(cmd));
551 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
552 c_collen, &si, &info);
554 HeapFree(GetProcessHeap(),0,cmd);
558 ERR("Unable to execute command\n");
559 return ERROR_SUCCESS;
562 prc = process_handle(package, type, info.hThread, info.hProcess, action);
567 static UINT HANDLE_CustomType19(MSIPACKAGE *package, LPCWSTR source,
568 LPCWSTR target, const INT type, LPCWSTR action)
570 static const WCHAR query[] = {
571 'S','E','L','E','C','T',' ','`','M','e','s','s','a','g','e','`',' ',
572 'F','R','O','M',' ','`','E','r','r','o','r','`',' ',
573 'W','H','E','R','E',' ','`','E','r','r','o','r','`',' ','=',' ',
577 LPWSTR deformated = NULL;
579 deformat_string( package, target, &deformated );
581 /* first try treat the error as a number */
582 row = MSI_QueryGetRecord( package->db, query, deformated );
585 LPCWSTR error = MSI_RecordGetString( row, 1 );
586 MessageBoxW( NULL, error, NULL, MB_OK );
587 msiobj_release( &row->hdr );
590 MessageBoxW( NULL, deformated, NULL, MB_OK );
592 HeapFree( GetProcessHeap(), 0, deformated );
594 return ERROR_FUNCTION_FAILED;
597 static UINT HANDLE_CustomType50(MSIPACKAGE *package, LPCWSTR source,
598 LPCWSTR target, const INT type, LPCWSTR action)
601 PROCESS_INFORMATION info;
608 static const WCHAR spc[] = {' ',0};
610 memset(&si,0,sizeof(STARTUPINFOW));
611 memset(&info,0,sizeof(PROCESS_INFORMATION));
613 prop = load_dynamic_property(package,source,&prc);
615 return ERROR_SUCCESS;
617 deformat_string(package,target,&deformated);
618 len = strlenW(prop) + 2;
620 len += strlenW(deformated);
622 cmd = HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*len);
628 strcatW(cmd,deformated);
630 HeapFree(GetProcessHeap(),0,deformated);
633 TRACE("executing exe %s \n",debugstr_w(cmd));
635 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
636 c_collen, &si, &info);
638 HeapFree(GetProcessHeap(),0,cmd);
642 ERR("Unable to execute command\n");
643 return ERROR_SUCCESS;
646 prc = process_handle(package, type, info.hThread, info.hProcess, action);
651 static UINT HANDLE_CustomType34(MSIPACKAGE *package, LPCWSTR source,
652 LPCWSTR target, const INT type, LPCWSTR action)
654 LPWSTR filename, deformated;
656 PROCESS_INFORMATION info;
660 memset(&si,0,sizeof(STARTUPINFOW));
662 filename = resolve_folder(package, source, FALSE, FALSE, NULL);
665 return ERROR_FUNCTION_FAILED;
667 SetCurrentDirectoryW(filename);
668 HeapFree(GetProcessHeap(),0,filename);
670 deformat_string(package,target,&deformated);
673 return ERROR_FUNCTION_FAILED;
675 TRACE("executing exe %s \n",debugstr_w(deformated));
677 rc = CreateProcessW(NULL, deformated, NULL, NULL, FALSE, 0, NULL,
678 c_collen, &si, &info);
679 HeapFree(GetProcessHeap(),0,deformated);
683 ERR("Unable to execute command\n");
684 return ERROR_SUCCESS;
687 prc = process_handle(package, type, info.hThread, info.hProcess, action);
693 void ACTION_FinishCustomActions(MSIPACKAGE* package)
698 for (i = 0; i < package->RunningActionCount; i++)
700 TRACE("Checking on action %s\n",
701 debugstr_w(package->RunningAction[i].name));
703 if (package->RunningAction[i].process)
704 GetExitCodeProcess(package->RunningAction[i].handle, &rc);
706 GetExitCodeThread(package->RunningAction[i].handle, &rc);
708 if (rc == STILL_ACTIVE)
710 TRACE("Waiting on action %s\n",
711 debugstr_w(package->RunningAction[i].name));
712 msi_dialog_check_messages(package->RunningAction[i].handle);
715 HeapFree(GetProcessHeap(),0,package->RunningAction[i].name);
716 CloseHandle(package->RunningAction[i].handle);
719 HeapFree(GetProcessHeap(),0,package->RunningAction);