Added memory allocation inline functions (part 2).
[wine/wine-kai.git] / dlls / msi / custom.c
blob624d03d793e7624639b467ef5624e69c220b520c
1 /*
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
22 * Pages I need
24 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/msi/setup/summary_list_of_all_custom_action_types.asp
27 #include <stdarg.h>
28 #include <stdio.h>
30 #define COBJMACROS
32 #include "windef.h"
33 #include "winbase.h"
34 #include "winerror.h"
35 #include "winreg.h"
36 #include "wine/debug.h"
37 #include "fdi.h"
38 #include "msi.h"
39 #include "msidefs.h"
40 #include "msiquery.h"
41 #include "msvcrt/fcntl.h"
42 #include "objbase.h"
43 #include "objidl.h"
44 #include "msipriv.h"
45 #include "winnls.h"
46 #include "winuser.h"
47 #include "shlobj.h"
48 #include "wine/unicode.h"
49 #include "winver.h"
50 #include "action.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
60 struct list entry;
61 HANDLE handle;
62 BOOL process;
63 LPWSTR name;
64 } MSIRUNNINGACTION;
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)
82 if (!package->script)
83 return TRUE;
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");
92 return FALSE;
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");
101 return FALSE;
104 else if (options & msidbCustomActionTypeOncePerProcess)
106 if (check_unique_action(package,action))
108 TRACE("Skipping action due to msidbCustomActionTypeOncePerProcess option.\n");
109 return FALSE;
111 else
112 register_unique_action(package,action);
115 return TRUE;
118 UINT ACTION_CustomAction(MSIPACKAGE *package,LPCWSTR action, BOOL execute)
120 UINT rc = ERROR_SUCCESS;
121 MSIRECORD * row = 0;
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};
127 UINT type;
128 LPWSTR source;
129 LPWSTR target;
130 WCHAR *deformated=NULL;
132 row = MSI_QueryGetRecord( package->db, ExecSeqQuery, action );
133 if (!row)
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 msi_free(source);
158 msi_free(target);
159 msiobj_release(&row->hdr);
160 return ERROR_SUCCESS;
162 if (!execute)
164 if (type & msidbCustomActionTypeCommit)
166 TRACE("Deferring Commit Action!\n");
167 schedule_action(package, COMMIT_SCRIPT, action);
169 else
171 TRACE("Deferring Action!\n");
172 schedule_action(package, INSTALL_SCRIPT, action);
175 msi_free(source);
176 msi_free(target);
177 msiobj_release(&row->hdr);
178 return ERROR_SUCCESS;
180 else
182 /*Set ActionData*/
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 );
188 if (actiondata)
189 MSI_SetPropertyW(package,szActionData,actiondata);
190 else
191 MSI_SetPropertyW(package,szActionData,szBlank);
192 msi_free(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);
202 break;
203 case 2: /* EXE file stored in a Binary table strem */
204 rc = HANDLE_CustomType2(package,source,target,type,action);
205 break;
206 case 18: /*EXE file installed with package */
207 rc = HANDLE_CustomType18(package,source,target,type,action);
208 break;
209 case 19: /* Error that halts install */
210 rc = HANDLE_CustomType19(package,source,target,type,action);
211 break;
212 case 50: /*EXE file specified by a property value */
213 rc = HANDLE_CustomType50(package,source,target,type,action);
214 break;
215 case 34: /*EXE to be run in specified directory */
216 rc = HANDLE_CustomType34(package,source,target,type,action);
217 break;
218 case 35: /* Directory set with formatted text. */
219 deformat_string(package,target,&deformated);
220 MSI_SetTargetPathW(package, source, deformated);
221 msi_free(deformated);
222 break;
223 case 51: /* Property set with formatted text. */
224 deformat_string(package,target,&deformated);
225 rc = MSI_SetPropertyW(package,source,deformated);
226 msi_free(deformated);
227 break;
228 default:
229 FIXME("UNHANDLED ACTION TYPE %i (%s %s)\n",
230 type & CUSTOM_ACTION_TYPE_MASK, debugstr_w(source),
231 debugstr_w(target));
234 msi_free(source);
235 msi_free(target);
236 msiobj_release(&row->hdr);
237 return rc;
241 static UINT store_binary_to_temp(MSIPACKAGE *package, LPCWSTR source,
242 LPWSTR tmp_file)
244 DWORD sz=MAX_PATH;
245 static const WCHAR f1[] = {'m','s','i',0};
246 WCHAR fmt[MAX_PATH];
248 if (MSI_GetPropertyW(package, cszTempFolder, fmt, &sz)
249 != ERROR_SUCCESS)
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;
257 else
259 /* write out the file */
260 UINT rc;
261 MSIRECORD * row = 0;
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};
266 HANDLE the_file;
267 CHAR buffer[1024];
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);
276 if (!row)
277 return ERROR_FUNCTION_FAILED;
281 DWORD write;
282 sz = 1024;
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);
289 break;
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 = msi_alloc( 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)
318 DWORD rc=0;
320 if (type == 2)
322 GetExitCodeProcess(ThreadHandle,&rc);
324 if (rc == 0)
325 return ERROR_SUCCESS;
326 else
327 return ERROR_FUNCTION_FAILED;
330 GetExitCodeThread(ThreadHandle,&rc);
332 switch (rc)
334 case ERROR_FUNCTION_NOT_CALLED:
335 case ERROR_SUCCESS:
336 case ERROR_INSTALL_USEREXIT:
337 case ERROR_INSTALL_FAILURE:
338 return rc;
339 case ERROR_NO_MORE_ITEMS:
340 return ERROR_SUCCESS;
341 default:
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))
355 /* synchronous */
356 TRACE("Synchronous Execution of action %s\n",debugstr_w(Name));
357 if (ProcessHandle)
358 msi_dialog_check_messages(ProcessHandle);
359 else
360 msi_dialog_check_messages(ThreadHandle);
362 if (!(type & msidbCustomActionTypeContinue))
364 if (ProcessHandle)
365 rc = process_action_return_value(2,ProcessHandle);
366 else
367 rc = process_action_return_value(1,ThreadHandle);
370 CloseHandle(ThreadHandle);
371 if (ProcessHandle)
372 CloseHandle(ProcessHandle);
373 if (finished)
374 *finished = TRUE;
376 else
378 TRACE("Asynchronous Execution of action %s\n",debugstr_w(Name));
379 /* asynchronous */
380 if (type & msidbCustomActionTypeContinue)
382 if (ProcessHandle)
384 file_running_action(package, ProcessHandle, TRUE, Name);
385 CloseHandle(ThreadHandle);
387 else
388 file_running_action(package, ThreadHandle, FALSE, Name);
390 else
392 CloseHandle(ThreadHandle);
393 if (ProcessHandle)
394 CloseHandle(ProcessHandle);
396 if (finished)
397 *finished = FALSE;
400 return rc;
404 typedef UINT __stdcall CustomEntry(MSIHANDLE);
406 typedef struct
408 MSIPACKAGE *package;
409 WCHAR *target;
410 WCHAR *source;
411 } thread_struct;
413 static DWORD WINAPI ACTION_CallDllFunction(thread_struct *stuff)
415 HANDLE hModule;
416 LPSTR proc;
417 CustomEntry *fn;
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);
424 if (hModule)
426 proc = strdupWtoA( stuff->target );
427 fn = (CustomEntry*)GetProcAddress(hModule,proc);
428 if (fn)
430 MSIHANDLE hPackage;
431 MSIPACKAGE *package = stuff->package;
433 TRACE("Calling function %s\n", proc);
434 hPackage = msiobj_findhandle( &package->hdr );
435 if (hPackage )
437 rc = fn(hPackage);
438 msiobj_release( &package->hdr );
440 else
441 ERR("Handle for object %p not found\n", package );
443 else
444 ERR("Cannot load functon\n");
446 msi_free(proc);
447 FreeLibrary(hModule);
449 else
450 ERR("Unable to load library\n");
451 msiobj_release( &stuff->package->hdr );
452 msi_free(stuff->source);
453 msi_free(stuff->target);
454 msi_free(stuff);
455 return rc;
458 static DWORD WINAPI DllThread(LPVOID info)
460 thread_struct *stuff;
461 DWORD rc = 0;
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();
472 return rc;
475 static UINT HANDLE_CustomType1(MSIPACKAGE *package, LPCWSTR source,
476 LPCWSTR target, const INT type, LPCWSTR action)
478 WCHAR tmp_file[MAX_PATH];
479 thread_struct *info;
480 DWORD ThreadId;
481 HANDLE ThreadHandle;
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 = msi_alloc( 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 );
506 if (!finished)
507 track_tempfile(package, tmp_file, tmp_file);
508 else
509 DeleteFileW(tmp_file);
511 return rc;
514 static UINT HANDLE_CustomType2(MSIPACKAGE *package, LPCWSTR source,
515 LPCWSTR target, const INT type, LPCWSTR action)
517 WCHAR tmp_file[MAX_PATH];
518 STARTUPINFOW si;
519 PROCESS_INFORMATION info;
520 BOOL rc;
521 INT len;
522 WCHAR *deformated;
523 WCHAR *cmd;
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;
536 if (deformated)
537 len += strlenW(deformated);
539 cmd = msi_alloc(sizeof(WCHAR)*len);
541 strcpyW(cmd,tmp_file);
542 if (deformated)
544 strcatW(cmd,spc);
545 strcatW(cmd,deformated);
547 msi_free(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 msi_free(cmd);
557 if ( !rc )
559 ERR("Unable to execute command\n");
560 return ERROR_SUCCESS;
563 prc = process_handle(package, type, info.hThread, info.hProcess, action,
564 &finished);
566 if (!finished)
567 track_tempfile(package, tmp_file, tmp_file);
568 else
569 DeleteFileW(tmp_file);
571 return prc;
574 static UINT HANDLE_CustomType18(MSIPACKAGE *package, LPCWSTR source,
575 LPCWSTR target, const INT type, LPCWSTR action)
577 STARTUPINFOW si;
578 PROCESS_INFORMATION info;
579 BOOL rc;
580 WCHAR *deformated;
581 WCHAR *cmd;
582 INT len;
583 static const WCHAR spc[] = {' ',0};
584 MSIFILE *file;
585 UINT prc;
587 memset(&si,0,sizeof(STARTUPINFOW));
589 file = get_loaded_file(package,source);
590 if( !file )
591 return ERROR_FUNCTION_FAILED;
593 len = lstrlenW( file->TargetPath );
595 deformat_string(package,target,&deformated);
596 if (deformated)
597 len += strlenW(deformated);
598 len += 2;
600 cmd = msi_alloc(len * sizeof(WCHAR));
602 lstrcpyW( cmd, file->TargetPath);
603 if (deformated)
605 strcatW(cmd, spc);
606 strcatW(cmd, deformated);
608 msi_free(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 msi_free(cmd);
618 if ( !rc )
620 ERR("Unable to execute command\n");
621 return ERROR_SUCCESS;
624 prc = process_handle(package, type, info.hThread, info.hProcess, action,
625 NULL);
627 return prc;
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','`',' ','=',' ',
637 '\'','%','s','\'',0
639 MSIRECORD *row = 0;
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 );
646 if( row )
648 LPCWSTR error = MSI_RecordGetString( row, 1 );
649 MessageBoxW( NULL, error, NULL, MB_OK );
650 msiobj_release( &row->hdr );
652 else
653 MessageBoxW( NULL, deformated, NULL, MB_OK );
655 msi_free( deformated );
657 return ERROR_FUNCTION_FAILED;
660 static UINT HANDLE_CustomType50(MSIPACKAGE *package, LPCWSTR source,
661 LPCWSTR target, const INT type, LPCWSTR action)
663 STARTUPINFOW si;
664 PROCESS_INFORMATION info;
665 WCHAR *prop;
666 BOOL rc;
667 WCHAR *deformated;
668 WCHAR *cmd;
669 INT len;
670 static const WCHAR spc[] = {' ',0};
672 memset(&si,0,sizeof(STARTUPINFOW));
673 memset(&info,0,sizeof(PROCESS_INFORMATION));
675 prop = msi_dup_property( package, source );
676 if (!prop)
677 return ERROR_SUCCESS;
679 deformat_string(package,target,&deformated);
680 len = strlenW(prop) + 2;
681 if (deformated)
682 len += strlenW(deformated);
684 cmd = msi_alloc(sizeof(WCHAR)*len);
686 strcpyW(cmd,prop);
687 if (deformated)
689 strcatW(cmd,spc);
690 strcatW(cmd,deformated);
692 msi_free(deformated);
694 msi_free(prop);
696 TRACE("executing exe %s \n",debugstr_w(cmd));
698 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
699 c_collen, &si, &info);
701 msi_free(cmd);
703 if ( !rc )
705 ERR("Unable to execute command\n");
706 return ERROR_SUCCESS;
709 return process_handle(package, type, info.hThread, info.hProcess, action, NULL);
712 static UINT HANDLE_CustomType34(MSIPACKAGE *package, LPCWSTR source,
713 LPCWSTR target, const INT type, LPCWSTR action)
715 LPWSTR filename, deformated;
716 STARTUPINFOW si;
717 PROCESS_INFORMATION info;
718 BOOL rc;
719 UINT prc;
721 memset(&si,0,sizeof(STARTUPINFOW));
723 filename = resolve_folder(package, source, FALSE, FALSE, NULL);
725 if (!filename)
726 return ERROR_FUNCTION_FAILED;
728 SetCurrentDirectoryW(filename);
729 msi_free(filename);
731 deformat_string(package,target,&deformated);
733 if (!deformated)
734 return ERROR_FUNCTION_FAILED;
736 TRACE("executing exe %s \n",debugstr_w(deformated));
738 rc = CreateProcessW(NULL, deformated, NULL, NULL, FALSE, 0, NULL,
739 c_collen, &si, &info);
740 msi_free(deformated);
742 if ( !rc )
744 ERR("Unable to execute command\n");
745 return ERROR_SUCCESS;
748 prc = process_handle(package, type, info.hThread, info.hProcess, action,
749 NULL);
751 return prc;
755 void ACTION_FinishCustomActions(MSIPACKAGE* package)
757 struct list *item, *cursor;
758 DWORD rc;
760 LIST_FOR_EACH_SAFE( item, cursor, &package->RunningActions )
762 MSIRUNNINGACTION *action = LIST_ENTRY( item, MSIRUNNINGACTION, entry );
764 TRACE("Checking on action %s\n", debugstr_w(action->name));
766 list_remove( &action->entry );
768 if (action->process)
769 GetExitCodeProcess( action->handle, &rc );
770 else
771 GetExitCodeThread( action->handle, &rc );
773 if (rc == STILL_ACTIVE)
775 TRACE("Waiting on action %s\n", debugstr_w( action->name) );
776 msi_dialog_check_messages( action->handle );
779 CloseHandle( action->handle );
780 msi_free( action->name );
781 msi_free( action );