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
);
162 if (type
& msidbCustomActionTypeCommit
)
164 TRACE("Deferring Commit Action!\n");
165 schedule_action(package
, COMMIT_SCRIPT
, action
);
169 TRACE("Deferring Action!\n");
170 schedule_action(package
, INSTALL_SCRIPT
, action
);
180 static const WCHAR szActionData
[] = {
181 'C','u','s','t','o','m','A','c','t','i','o','n','D','a','t','a',0};
182 static const WCHAR szBlank
[] = {0};
183 LPWSTR actiondata
= msi_dup_property( package
, action
);
185 MSI_SetPropertyW(package
,szActionData
,actiondata
);
187 MSI_SetPropertyW(package
,szActionData
,szBlank
);
188 msi_free(actiondata
);
191 else if (!check_execution_scheduling_options(package
,action
,type
))
197 switch (type
& CUSTOM_ACTION_TYPE_MASK
)
199 case 1: /* DLL file stored in a Binary table stream */
200 rc
= HANDLE_CustomType1(package
,source
,target
,type
,action
);
202 case 2: /* EXE file stored in a Binary table strem */
203 rc
= HANDLE_CustomType2(package
,source
,target
,type
,action
);
205 case 18: /*EXE file installed with package */
206 rc
= HANDLE_CustomType18(package
,source
,target
,type
,action
);
208 case 19: /* Error that halts install */
209 rc
= HANDLE_CustomType19(package
,source
,target
,type
,action
);
211 case 50: /*EXE file specified by a property value */
212 rc
= HANDLE_CustomType50(package
,source
,target
,type
,action
);
214 case 34: /*EXE to be run in specified directory */
215 rc
= HANDLE_CustomType34(package
,source
,target
,type
,action
);
217 case 35: /* Directory set with formatted text. */
218 deformat_string(package
,target
,&deformated
);
219 MSI_SetTargetPathW(package
, source
, deformated
);
220 msi_free(deformated
);
222 case 51: /* Property set with formatted text. */
223 deformat_string(package
,target
,&deformated
);
224 rc
= MSI_SetPropertyW(package
,source
,deformated
);
225 msi_free(deformated
);
228 FIXME("UNHANDLED ACTION TYPE %i (%s %s)\n",
229 type
& CUSTOM_ACTION_TYPE_MASK
, debugstr_w(source
),
236 msiobj_release(&row
->hdr
);
241 static UINT
store_binary_to_temp(MSIPACKAGE
*package
, LPCWSTR source
,
245 static const WCHAR f1
[] = {'m','s','i',0};
248 if (MSI_GetPropertyW(package
, cszTempFolder
, fmt
, &sz
) != ERROR_SUCCESS
)
249 GetTempPathW(MAX_PATH
,fmt
);
251 if (GetTempFileNameW(fmt
,f1
,0,tmp_file
) == 0)
253 TRACE("Unable to create file\n");
254 return ERROR_FUNCTION_FAILED
;
258 /* write out the file */
261 static const WCHAR fmt
[] =
262 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
263 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',
264 ' ','`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
268 the_file
= CreateFileW(tmp_file
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
,
269 FILE_ATTRIBUTE_NORMAL
, NULL
);
271 if (the_file
== INVALID_HANDLE_VALUE
)
272 return ERROR_FUNCTION_FAILED
;
274 row
= MSI_QueryGetRecord(package
->db
, fmt
, source
);
276 return ERROR_FUNCTION_FAILED
;
282 rc
= MSI_RecordReadStream(row
,2,buffer
,&sz
);
283 if (rc
!= ERROR_SUCCESS
)
285 ERR("Failed to get stream\n");
286 CloseHandle(the_file
);
287 DeleteFileW(tmp_file
);
290 WriteFile(the_file
,buffer
,sz
,&write
,NULL
);
291 } while (sz
== 1024);
293 CloseHandle(the_file
);
295 msiobj_release(&row
->hdr
);
298 return ERROR_SUCCESS
;
301 static void file_running_action(MSIPACKAGE
* package
, HANDLE Handle
,
302 BOOL process
, LPCWSTR name
)
304 MSIRUNNINGACTION
*action
;
306 action
= msi_alloc( sizeof(MSIRUNNINGACTION
) );
308 action
->handle
= Handle
;
309 action
->process
= process
;
310 action
->name
= strdupW(name
);
312 list_add_tail( &package
->RunningActions
, &action
->entry
);
315 static UINT
process_action_return_value(UINT type
, HANDLE ThreadHandle
)
321 GetExitCodeProcess(ThreadHandle
,&rc
);
324 return ERROR_SUCCESS
;
326 return ERROR_FUNCTION_FAILED
;
329 GetExitCodeThread(ThreadHandle
,&rc
);
333 case ERROR_FUNCTION_NOT_CALLED
:
335 case ERROR_INSTALL_USEREXIT
:
336 case ERROR_INSTALL_FAILURE
:
338 case ERROR_NO_MORE_ITEMS
:
339 return ERROR_SUCCESS
;
341 ERR("Invalid Return Code %lx\n",rc
);
342 return ERROR_INSTALL_FAILURE
;
346 static UINT
process_handle(MSIPACKAGE
* package
, UINT type
,
347 HANDLE ThreadHandle
, HANDLE ProcessHandle
,
348 LPCWSTR Name
, BOOL
*finished
)
350 UINT rc
= ERROR_SUCCESS
;
352 if (!(type
& msidbCustomActionTypeAsync
))
355 TRACE("Synchronous Execution of action %s\n",debugstr_w(Name
));
357 msi_dialog_check_messages(ProcessHandle
);
359 msi_dialog_check_messages(ThreadHandle
);
361 if (!(type
& msidbCustomActionTypeContinue
))
364 rc
= process_action_return_value(2,ProcessHandle
);
366 rc
= process_action_return_value(1,ThreadHandle
);
369 CloseHandle(ThreadHandle
);
371 CloseHandle(ProcessHandle
);
377 TRACE("Asynchronous Execution of action %s\n",debugstr_w(Name
));
379 if (type
& msidbCustomActionTypeContinue
)
383 file_running_action(package
, ProcessHandle
, TRUE
, Name
);
384 CloseHandle(ThreadHandle
);
387 file_running_action(package
, ThreadHandle
, FALSE
, Name
);
391 CloseHandle(ThreadHandle
);
393 CloseHandle(ProcessHandle
);
403 typedef UINT __stdcall
CustomEntry(MSIHANDLE
);
412 static DWORD WINAPI
ACTION_CallDllFunction(thread_struct
*stuff
)
417 DWORD rc
= ERROR_SUCCESS
;
419 TRACE("calling function (%s, %s) \n", debugstr_w(stuff
->source
),
420 debugstr_w(stuff
->target
));
422 hModule
= LoadLibraryW(stuff
->source
);
425 proc
= strdupWtoA( stuff
->target
);
426 fn
= (CustomEntry
*)GetProcAddress(hModule
,proc
);
430 MSIPACKAGE
*package
= stuff
->package
;
432 TRACE("Calling function %s\n", proc
);
433 hPackage
= alloc_msihandle( &package
->hdr
);
437 MsiCloseHandle( hPackage
);
440 ERR("Handle for object %p not found\n", package
);
443 ERR("Cannot load functon\n");
446 FreeLibrary(hModule
);
449 ERR("Unable to load library\n");
450 msiobj_release( &stuff
->package
->hdr
);
451 msi_free(stuff
->source
);
452 msi_free(stuff
->target
);
457 static DWORD WINAPI
DllThread(LPVOID info
)
459 thread_struct
*stuff
;
462 TRACE("MSI Thread (0x%lx) started for custom action\n",
463 GetCurrentThreadId());
465 stuff
= (thread_struct
*)info
;
466 rc
= ACTION_CallDllFunction(stuff
);
468 TRACE("MSI Thread (0x%lx) finished (rc %li)\n",GetCurrentThreadId(), rc
);
469 /* clse all handles for this thread */
470 MsiCloseAllHandles();
474 static UINT
HANDLE_CustomType1(MSIPACKAGE
*package
, LPCWSTR source
,
475 LPCWSTR target
, const INT type
, LPCWSTR action
)
477 WCHAR tmp_file
[MAX_PATH
];
481 UINT rc
= ERROR_SUCCESS
;
482 BOOL finished
= FALSE
;
484 store_binary_to_temp(package
, source
, tmp_file
);
486 TRACE("Calling function %s from %s\n",debugstr_w(target
),
487 debugstr_w(tmp_file
));
489 if (!strchrW(tmp_file
,'.'))
491 static const WCHAR dot
[]={'.',0};
492 strcatW(tmp_file
,dot
);
495 info
= msi_alloc( sizeof(*info
) );
496 msiobj_addref( &package
->hdr
);
497 info
->package
= package
;
498 info
->target
= strdupW(target
);
499 info
->source
= strdupW(tmp_file
);
501 ThreadHandle
= CreateThread(NULL
,0,DllThread
,(LPVOID
)info
,0,&ThreadId
);
503 rc
= process_handle(package
, type
, ThreadHandle
, NULL
, action
, &finished
);
506 track_tempfile(package
, tmp_file
, tmp_file
);
508 DeleteFileW(tmp_file
);
513 static UINT
HANDLE_CustomType2(MSIPACKAGE
*package
, LPCWSTR source
,
514 LPCWSTR target
, const INT type
, LPCWSTR action
)
516 WCHAR tmp_file
[MAX_PATH
];
518 PROCESS_INFORMATION info
;
523 static const WCHAR spc
[] = {' ',0};
524 UINT prc
= ERROR_SUCCESS
;
525 BOOL finished
= FALSE
;
527 memset(&si
,0,sizeof(STARTUPINFOW
));
529 store_binary_to_temp(package
, source
, tmp_file
);
531 deformat_string(package
,target
,&deformated
);
533 len
= strlenW(tmp_file
)+2;
536 len
+= strlenW(deformated
);
538 cmd
= msi_alloc(sizeof(WCHAR
)*len
);
540 strcpyW(cmd
,tmp_file
);
544 strcatW(cmd
,deformated
);
546 msi_free(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
);
558 ERR("Unable to execute command\n");
559 return ERROR_SUCCESS
;
562 prc
= process_handle(package
, type
, info
.hThread
, info
.hProcess
, action
,
566 track_tempfile(package
, tmp_file
, tmp_file
);
568 DeleteFileW(tmp_file
);
573 static UINT
HANDLE_CustomType18(MSIPACKAGE
*package
, LPCWSTR source
,
574 LPCWSTR target
, const INT type
, LPCWSTR action
)
577 PROCESS_INFORMATION info
;
582 static const WCHAR spc
[] = {' ',0};
586 memset(&si
,0,sizeof(STARTUPINFOW
));
588 file
= get_loaded_file(package
,source
);
590 return ERROR_FUNCTION_FAILED
;
592 len
= lstrlenW( file
->TargetPath
);
594 deformat_string(package
,target
,&deformated
);
596 len
+= strlenW(deformated
);
599 cmd
= msi_alloc(len
* sizeof(WCHAR
));
601 lstrcpyW( cmd
, file
->TargetPath
);
605 strcatW(cmd
, deformated
);
607 msi_free(deformated
);
610 TRACE("executing exe %s \n",debugstr_w(cmd
));
612 rc
= CreateProcessW(NULL
, cmd
, NULL
, NULL
, FALSE
, 0, NULL
,
613 c_collen
, &si
, &info
);
619 ERR("Unable to execute command\n");
620 return ERROR_SUCCESS
;
623 prc
= process_handle(package
, type
, info
.hThread
, info
.hProcess
, action
,
629 static UINT
HANDLE_CustomType19(MSIPACKAGE
*package
, LPCWSTR source
,
630 LPCWSTR target
, const INT type
, LPCWSTR action
)
632 static const WCHAR query
[] = {
633 'S','E','L','E','C','T',' ','`','M','e','s','s','a','g','e','`',' ',
634 'F','R','O','M',' ','`','E','r','r','o','r','`',' ',
635 'W','H','E','R','E',' ','`','E','r','r','o','r','`',' ','=',' ',
639 LPWSTR deformated
= NULL
;
641 deformat_string( package
, target
, &deformated
);
643 /* first try treat the error as a number */
644 row
= MSI_QueryGetRecord( package
->db
, query
, deformated
);
647 LPCWSTR error
= MSI_RecordGetString( row
, 1 );
648 MessageBoxW( NULL
, error
, NULL
, MB_OK
);
649 msiobj_release( &row
->hdr
);
652 MessageBoxW( NULL
, deformated
, NULL
, MB_OK
);
654 msi_free( deformated
);
656 return ERROR_FUNCTION_FAILED
;
659 static UINT
HANDLE_CustomType50(MSIPACKAGE
*package
, LPCWSTR source
,
660 LPCWSTR target
, const INT type
, LPCWSTR action
)
663 PROCESS_INFORMATION info
;
669 static const WCHAR spc
[] = {' ',0};
671 memset(&si
,0,sizeof(STARTUPINFOW
));
672 memset(&info
,0,sizeof(PROCESS_INFORMATION
));
674 prop
= msi_dup_property( package
, source
);
676 return ERROR_SUCCESS
;
678 deformat_string(package
,target
,&deformated
);
679 len
= strlenW(prop
) + 2;
681 len
+= strlenW(deformated
);
683 cmd
= msi_alloc(sizeof(WCHAR
)*len
);
689 strcatW(cmd
,deformated
);
691 msi_free(deformated
);
695 TRACE("executing exe %s \n",debugstr_w(cmd
));
697 rc
= CreateProcessW(NULL
, cmd
, NULL
, NULL
, FALSE
, 0, NULL
,
698 c_collen
, &si
, &info
);
704 ERR("Unable to execute command\n");
705 return ERROR_SUCCESS
;
708 return process_handle(package
, type
, info
.hThread
, info
.hProcess
, action
, NULL
);
711 static UINT
HANDLE_CustomType34(MSIPACKAGE
*package
, LPCWSTR source
,
712 LPCWSTR target
, const INT type
, LPCWSTR action
)
714 LPWSTR filename
, deformated
;
716 PROCESS_INFORMATION info
;
720 memset(&si
,0,sizeof(STARTUPINFOW
));
722 filename
= resolve_folder(package
, source
, FALSE
, FALSE
, NULL
);
725 return ERROR_FUNCTION_FAILED
;
727 SetCurrentDirectoryW(filename
);
730 deformat_string(package
,target
,&deformated
);
733 return ERROR_FUNCTION_FAILED
;
735 TRACE("executing exe %s \n",debugstr_w(deformated
));
737 rc
= CreateProcessW(NULL
, deformated
, NULL
, NULL
, FALSE
, 0, NULL
,
738 c_collen
, &si
, &info
);
739 msi_free(deformated
);
743 ERR("Unable to execute command\n");
744 return ERROR_SUCCESS
;
747 prc
= process_handle(package
, type
, info
.hThread
, info
.hProcess
, action
,
754 void ACTION_FinishCustomActions(MSIPACKAGE
* package
)
756 struct list
*item
, *cursor
;
759 LIST_FOR_EACH_SAFE( item
, cursor
, &package
->RunningActions
)
761 MSIRUNNINGACTION
*action
= LIST_ENTRY( item
, MSIRUNNINGACTION
, entry
);
763 TRACE("Checking on action %s\n", debugstr_w(action
->name
));
765 list_remove( &action
->entry
);
768 GetExitCodeProcess( action
->handle
, &rc
);
770 GetExitCodeThread( action
->handle
, &rc
);
772 if (rc
== STILL_ACTIVE
)
774 TRACE("Waiting on action %s\n", debugstr_w( action
->name
) );
775 msi_dialog_check_messages( action
->handle
);
778 CloseHandle( action
->handle
);
779 msi_free( action
->name
);