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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 #include "wine/debug.h"
31 #include "wine/unicode.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(msi
);
35 #define CUSTOM_ACTION_TYPE_MASK 0x3F
36 static const WCHAR c_collen
[] = {'C',':','\\',0};
37 static const WCHAR cszTempFolder
[]= {'T','e','m','p','F','o','l','d','e','r',0};
39 typedef struct tagMSIRUNNINGACTION
47 static UINT
HANDLE_CustomType1(MSIPACKAGE
*package
, LPCWSTR source
,
48 LPCWSTR target
, const INT type
, LPCWSTR action
);
49 static UINT
HANDLE_CustomType2(MSIPACKAGE
*package
, LPCWSTR source
,
50 LPCWSTR target
, const INT type
, LPCWSTR action
);
51 static UINT
HANDLE_CustomType17(MSIPACKAGE
*package
, LPCWSTR source
,
52 LPCWSTR target
, const INT type
, LPCWSTR action
);
53 static UINT
HANDLE_CustomType18(MSIPACKAGE
*package
, LPCWSTR source
,
54 LPCWSTR target
, const INT type
, LPCWSTR action
);
55 static UINT
HANDLE_CustomType19(MSIPACKAGE
*package
, LPCWSTR source
,
56 LPCWSTR target
, const INT type
, LPCWSTR action
);
57 static UINT
HANDLE_CustomType50(MSIPACKAGE
*package
, LPCWSTR source
,
58 LPCWSTR target
, const INT type
, LPCWSTR action
);
59 static UINT
HANDLE_CustomType34(MSIPACKAGE
*package
, LPCWSTR source
,
60 LPCWSTR target
, const INT type
, LPCWSTR action
);
63 static BOOL
check_execution_scheduling_options(MSIPACKAGE
*package
, LPCWSTR action
, UINT options
)
68 if ((options
& msidbCustomActionTypeClientRepeat
) ==
69 msidbCustomActionTypeClientRepeat
)
71 if (!(package
->script
->InWhatSequence
& SEQUENCE_UI
&&
72 package
->script
->InWhatSequence
& SEQUENCE_EXEC
))
74 TRACE("Skipping action due to dbCustomActionTypeClientRepeat option.\n");
78 else if (options
& msidbCustomActionTypeFirstSequence
)
80 if (package
->script
->InWhatSequence
& SEQUENCE_UI
&&
81 package
->script
->InWhatSequence
& SEQUENCE_EXEC
)
83 TRACE("Skipping action due to msidbCustomActionTypeFirstSequence option.\n");
87 else if (options
& msidbCustomActionTypeOncePerProcess
)
89 if (check_unique_action(package
,action
))
91 TRACE("Skipping action due to msidbCustomActionTypeOncePerProcess option.\n");
95 register_unique_action(package
,action
);
101 /* stores the CustomActionData before the action:
102 * [CustomActionData]Action
104 static LPWSTR
msi_get_deferred_action(LPCWSTR action
, LPWSTR actiondata
)
109 static const WCHAR begin
[] = {'[',0};
110 static const WCHAR end
[] = {']',0};
113 return strdupW(action
);
115 len
= lstrlenW(action
) + lstrlenW(actiondata
) + 3;
116 deferred
= msi_alloc(len
* sizeof(WCHAR
));
118 lstrcpyW(deferred
, begin
);
119 lstrcatW(deferred
, actiondata
);
120 lstrcatW(deferred
, end
);
121 lstrcatW(deferred
, action
);
126 UINT
ACTION_CustomAction(MSIPACKAGE
*package
,LPCWSTR action
, BOOL execute
)
128 UINT rc
= ERROR_SUCCESS
;
130 static const WCHAR ExecSeqQuery
[] =
131 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
132 '`','C','u','s','t','o' ,'m','A','c','t','i','o','n','`',
133 ' ','W','H','E','R','E',' ','`','A','c','t','i' ,'o','n','`',' ',
134 '=',' ','\'','%','s','\'',0};
136 LPCWSTR source
, target
;
137 LPWSTR ptr
, deferred_data
= NULL
;
138 LPWSTR action_copy
= strdupW(action
);
139 WCHAR
*deformated
=NULL
;
141 /* deferred action: [CustomActionData]Action */
142 if ((ptr
= strchrW(action_copy
, ']')))
144 deferred_data
= action_copy
+ 1;
149 row
= MSI_QueryGetRecord( package
->db
, ExecSeqQuery
, action
);
152 msi_free(action_copy
);
153 return ERROR_CALL_NOT_IMPLEMENTED
;
156 type
= MSI_RecordGetInteger(row
,2);
158 source
= MSI_RecordGetString(row
,3);
159 target
= MSI_RecordGetString(row
,4);
161 TRACE("Handling custom action %s (%x %s %s)\n",debugstr_w(action
),type
,
162 debugstr_w(source
), debugstr_w(target
));
164 /* handle some of the deferred actions */
165 if (type
& msidbCustomActionTypeTSAware
)
166 FIXME("msidbCustomActionTypeTSAware not handled\n");
168 if (type
& msidbCustomActionTypeInScript
)
170 if (type
& msidbCustomActionTypeNoImpersonate
)
171 FIXME("msidbCustomActionTypeNoImpersonate not handled\n");
173 if (type
& msidbCustomActionTypeRollback
)
175 FIXME("Rollback only action... rollbacks not supported yet\n");
176 schedule_action(package
, ROLLBACK_SCRIPT
, action
);
182 LPWSTR actiondata
= msi_dup_property(package
, action
);
183 LPWSTR deferred
= msi_get_deferred_action(action
, actiondata
);
185 if (type
& msidbCustomActionTypeCommit
)
187 TRACE("Deferring Commit Action!\n");
188 schedule_action(package
, COMMIT_SCRIPT
, deferred
);
192 TRACE("Deferring Action!\n");
193 schedule_action(package
, INSTALL_SCRIPT
, deferred
);
204 static const WCHAR szActionData
[] = {
205 'C','u','s','t','o','m','A','c','t','i','o','n','D','a','t','a',0};
206 static const WCHAR szBlank
[] = {0};
207 LPWSTR actiondata
= msi_dup_property( package
, action
);
209 MSI_SetPropertyW(package
,szActionData
,deferred_data
);
211 MSI_SetPropertyW(package
,szActionData
,actiondata
);
213 MSI_SetPropertyW(package
,szActionData
,szBlank
);
214 msi_free(actiondata
);
217 else if (!check_execution_scheduling_options(package
,action
,type
))
223 switch (type
& CUSTOM_ACTION_TYPE_MASK
)
225 case 1: /* DLL file stored in a Binary table stream */
226 rc
= HANDLE_CustomType1(package
,source
,target
,type
,action
);
228 case 2: /* EXE file stored in a Binary table stream */
229 rc
= HANDLE_CustomType2(package
,source
,target
,type
,action
);
231 case 18: /*EXE file installed with package */
232 rc
= HANDLE_CustomType18(package
,source
,target
,type
,action
);
234 case 19: /* Error that halts install */
235 rc
= HANDLE_CustomType19(package
,source
,target
,type
,action
);
238 rc
= HANDLE_CustomType17(package
,source
,target
,type
,action
);
240 case 50: /*EXE file specified by a property value */
241 rc
= HANDLE_CustomType50(package
,source
,target
,type
,action
);
243 case 34: /*EXE to be run in specified directory */
244 rc
= HANDLE_CustomType34(package
,source
,target
,type
,action
);
246 case 35: /* Directory set with formatted text. */
247 deformat_string(package
,target
,&deformated
);
248 MSI_SetTargetPathW(package
, source
, deformated
);
249 msi_free(deformated
);
251 case 51: /* Property set with formatted text. */
252 deformat_string(package
,target
,&deformated
);
253 rc
= MSI_SetPropertyW(package
,source
,deformated
);
254 msi_free(deformated
);
257 FIXME("UNHANDLED ACTION TYPE %i (%s %s)\n",
258 type
& CUSTOM_ACTION_TYPE_MASK
, debugstr_w(source
),
263 msi_free(action_copy
);
264 msiobj_release(&row
->hdr
);
269 static UINT
store_binary_to_temp(MSIPACKAGE
*package
, LPCWSTR source
,
272 static const WCHAR query
[] = {
273 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
274 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',' ',
275 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
279 static const WCHAR f1
[] = {'m','s','i',0};
284 if (MSI_GetPropertyW(package
, cszTempFolder
, fmt
, &sz
) != ERROR_SUCCESS
)
285 GetTempPathW(MAX_PATH
, fmt
);
287 if (GetTempFileNameW(fmt
, f1
, 0, tmp_file
) == 0)
289 TRACE("Unable to create file\n");
290 return ERROR_FUNCTION_FAILED
;
292 track_tempfile(package
, tmp_file
);
294 row
= MSI_QueryGetRecord(package
->db
, query
, source
);
296 return ERROR_FUNCTION_FAILED
;
298 /* write out the file */
299 file
= CreateFileW(tmp_file
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
,
300 FILE_ATTRIBUTE_NORMAL
, NULL
);
301 if (file
== INVALID_HANDLE_VALUE
)
302 r
= ERROR_FUNCTION_FAILED
;
309 r
= MSI_RecordReadStream(row
, 2, buffer
, &sz
);
310 if (r
!= ERROR_SUCCESS
)
312 ERR("Failed to get stream\n");
315 WriteFile(file
, buffer
, sz
, &write
, NULL
);
316 } while (sz
== sizeof buffer
);
320 msiobj_release(&row
->hdr
);
325 static void file_running_action(MSIPACKAGE
* package
, HANDLE Handle
,
326 BOOL process
, LPCWSTR name
)
328 MSIRUNNINGACTION
*action
;
330 action
= msi_alloc( sizeof(MSIRUNNINGACTION
) );
332 action
->handle
= Handle
;
333 action
->process
= process
;
334 action
->name
= strdupW(name
);
336 list_add_tail( &package
->RunningActions
, &action
->entry
);
339 static UINT
custom_get_process_return( HANDLE process
)
343 GetExitCodeProcess( process
, &rc
);
345 return ERROR_FUNCTION_FAILED
;
346 return ERROR_SUCCESS
;
349 static UINT
custom_get_thread_return( HANDLE thread
)
353 GetExitCodeThread( thread
, &rc
);
357 case ERROR_FUNCTION_NOT_CALLED
:
359 case ERROR_INSTALL_USEREXIT
:
360 case ERROR_INSTALL_FAILURE
:
362 case ERROR_NO_MORE_ITEMS
:
363 return ERROR_SUCCESS
;
365 ERR("Invalid Return Code %d\n",rc
);
366 return ERROR_INSTALL_FAILURE
;
370 static UINT
process_handle(MSIPACKAGE
* package
, UINT type
,
371 HANDLE ThreadHandle
, HANDLE ProcessHandle
,
374 UINT rc
= ERROR_SUCCESS
;
376 if (!(type
& msidbCustomActionTypeAsync
))
379 TRACE("Synchronous Execution of action %s\n",debugstr_w(Name
));
381 msi_dialog_check_messages(ProcessHandle
);
383 msi_dialog_check_messages(ThreadHandle
);
385 if (!(type
& msidbCustomActionTypeContinue
))
388 rc
= custom_get_process_return(ProcessHandle
);
390 rc
= custom_get_thread_return(ThreadHandle
);
393 CloseHandle(ThreadHandle
);
395 CloseHandle(ProcessHandle
);
399 TRACE("Asynchronous Execution of action %s\n",debugstr_w(Name
));
401 if (!(type
& msidbCustomActionTypeContinue
))
405 file_running_action(package
, ProcessHandle
, TRUE
, Name
);
406 CloseHandle(ThreadHandle
);
409 file_running_action(package
, ThreadHandle
, FALSE
, Name
);
413 CloseHandle(ThreadHandle
);
415 CloseHandle(ProcessHandle
);
423 typedef UINT __stdcall
CustomEntry(MSIHANDLE
);
432 static DWORD WINAPI
ACTION_CallDllFunction(thread_struct
*stuff
)
437 DWORD rc
= ERROR_SUCCESS
;
439 TRACE("calling function (%s, %s)\n", debugstr_w(stuff
->source
),
440 debugstr_w(stuff
->target
));
442 hModule
= LoadLibraryW(stuff
->source
);
445 proc
= strdupWtoA( stuff
->target
);
446 fn
= (CustomEntry
*)GetProcAddress(hModule
,proc
);
450 MSIPACKAGE
*package
= stuff
->package
;
452 TRACE("Calling function %s\n", proc
);
453 hPackage
= alloc_msihandle( &package
->hdr
);
457 MsiCloseHandle( hPackage
);
460 ERR("Handle for object %p not found\n", package
);
463 ERR("failed to resolve functon %s\n", debugstr_a(proc
));
466 FreeLibrary(hModule
);
469 ERR("failed to load dll %s\n", debugstr_w(stuff
->source
));
470 msiobj_release( &stuff
->package
->hdr
);
471 msi_free(stuff
->source
);
472 msi_free(stuff
->target
);
477 static DWORD WINAPI
DllThread(LPVOID info
)
479 thread_struct
*stuff
;
482 TRACE("MSI Thread (%x) started for custom action\n", GetCurrentThreadId());
484 stuff
= (thread_struct
*)info
;
485 rc
= ACTION_CallDllFunction(stuff
);
487 TRACE("MSI Thread (%x) finished (rc %i)\n",GetCurrentThreadId(), rc
);
488 /* close all handles for this thread */
489 MsiCloseAllHandles();
493 static HANDLE
do_msidbCustomActionTypeDll(MSIPACKAGE
*package
, LPCWSTR dll
, LPCWSTR target
)
497 info
= msi_alloc( sizeof(*info
) );
498 msiobj_addref( &package
->hdr
);
499 info
->package
= package
;
500 info
->target
= strdupW(target
);
501 info
->source
= strdupW(dll
);
503 return CreateThread(NULL
, 0, DllThread
, info
, 0, NULL
);
506 static UINT
HANDLE_CustomType1(MSIPACKAGE
*package
, LPCWSTR source
,
507 LPCWSTR target
, const INT type
, LPCWSTR action
)
509 WCHAR tmp_file
[MAX_PATH
];
510 UINT r
= ERROR_SUCCESS
;
513 r
= store_binary_to_temp(package
, source
, tmp_file
);
514 if (r
!= ERROR_SUCCESS
)
517 TRACE("Calling function %s from %s\n",debugstr_w(target
),
518 debugstr_w(tmp_file
));
520 if (!strchrW(tmp_file
,'.'))
522 static const WCHAR dot
[]={'.',0};
523 strcatW(tmp_file
,dot
);
526 ThreadHandle
= do_msidbCustomActionTypeDll( package
, tmp_file
, target
);
528 r
= process_handle(package
, type
, ThreadHandle
, NULL
, action
);
533 static UINT
HANDLE_CustomType2(MSIPACKAGE
*package
, LPCWSTR source
,
534 LPCWSTR target
, const INT type
, LPCWSTR action
)
536 WCHAR tmp_file
[MAX_PATH
];
538 PROCESS_INFORMATION info
;
541 WCHAR
*deformated
= NULL
;
543 static const WCHAR spc
[] = {' ',0};
544 UINT r
= ERROR_SUCCESS
;
546 memset(&si
,0,sizeof(STARTUPINFOW
));
548 r
= store_binary_to_temp(package
, source
, tmp_file
);
549 if (r
!= ERROR_SUCCESS
)
552 deformat_string(package
,target
,&deformated
);
554 len
= strlenW(tmp_file
)+2;
557 len
+= strlenW(deformated
);
559 cmd
= msi_alloc(sizeof(WCHAR
)*len
);
561 strcpyW(cmd
,tmp_file
);
565 strcatW(cmd
,deformated
);
567 msi_free(deformated
);
570 TRACE("executing exe %s\n", debugstr_w(cmd
));
572 rc
= CreateProcessW(NULL
, cmd
, NULL
, NULL
, FALSE
, 0, NULL
,
573 c_collen
, &si
, &info
);
578 ERR("Unable to execute command %s\n", debugstr_w(cmd
));
579 return ERROR_SUCCESS
;
582 r
= process_handle(package
, type
, info
.hThread
, info
.hProcess
, action
);
587 static UINT
HANDLE_CustomType17(MSIPACKAGE
*package
, LPCWSTR source
,
588 LPCWSTR target
, const INT type
, LPCWSTR action
)
593 TRACE("%s %s\n", debugstr_w(source
), debugstr_w(target
));
595 file
= get_loaded_file( package
, source
);
598 ERR("invalid file key %s\n", debugstr_w( source
));
599 return ERROR_FUNCTION_FAILED
;
602 hThread
= do_msidbCustomActionTypeDll( package
, file
->TargetPath
, target
);
604 return process_handle(package
, type
, hThread
, NULL
, action
);
607 static UINT
HANDLE_CustomType18(MSIPACKAGE
*package
, LPCWSTR source
,
608 LPCWSTR target
, const INT type
, LPCWSTR action
)
611 PROCESS_INFORMATION info
;
616 static const WCHAR spc
[] = {' ',0};
620 memset(&si
,0,sizeof(STARTUPINFOW
));
622 file
= get_loaded_file(package
,source
);
624 return ERROR_FUNCTION_FAILED
;
626 len
= lstrlenW( file
->TargetPath
);
628 deformat_string(package
,target
,&deformated
);
630 len
+= strlenW(deformated
);
633 cmd
= msi_alloc(len
* sizeof(WCHAR
));
635 lstrcpyW( cmd
, file
->TargetPath
);
639 strcatW(cmd
, deformated
);
641 msi_free(deformated
);
644 TRACE("executing exe %s\n", debugstr_w(cmd
));
646 rc
= CreateProcessW(NULL
, cmd
, NULL
, NULL
, FALSE
, 0, NULL
,
647 c_collen
, &si
, &info
);
652 ERR("Unable to execute command %s\n", debugstr_w(cmd
));
654 return ERROR_SUCCESS
;
658 prc
= process_handle(package
, type
, info
.hThread
, info
.hProcess
, action
);
663 static UINT
HANDLE_CustomType19(MSIPACKAGE
*package
, LPCWSTR source
,
664 LPCWSTR target
, const INT type
, LPCWSTR action
)
666 static const WCHAR query
[] = {
667 'S','E','L','E','C','T',' ','`','M','e','s','s','a','g','e','`',' ',
668 'F','R','O','M',' ','`','E','r','r','o','r','`',' ',
669 'W','H','E','R','E',' ','`','E','r','r','o','r','`',' ','=',' ',
673 LPWSTR deformated
= NULL
;
675 deformat_string( package
, target
, &deformated
);
677 /* first try treat the error as a number */
678 row
= MSI_QueryGetRecord( package
->db
, query
, deformated
);
681 LPCWSTR error
= MSI_RecordGetString( row
, 1 );
682 MessageBoxW( NULL
, error
, NULL
, MB_OK
);
683 msiobj_release( &row
->hdr
);
686 MessageBoxW( NULL
, deformated
, NULL
, MB_OK
);
688 msi_free( deformated
);
690 return ERROR_FUNCTION_FAILED
;
693 static UINT
HANDLE_CustomType50(MSIPACKAGE
*package
, LPCWSTR source
,
694 LPCWSTR target
, const INT type
, LPCWSTR action
)
697 PROCESS_INFORMATION info
;
703 static const WCHAR spc
[] = {' ',0};
705 memset(&si
,0,sizeof(STARTUPINFOW
));
706 memset(&info
,0,sizeof(PROCESS_INFORMATION
));
708 prop
= msi_dup_property( package
, source
);
710 return ERROR_SUCCESS
;
712 deformat_string(package
,target
,&deformated
);
713 len
= strlenW(prop
) + 2;
715 len
+= strlenW(deformated
);
717 cmd
= msi_alloc(sizeof(WCHAR
)*len
);
723 strcatW(cmd
,deformated
);
725 msi_free(deformated
);
729 TRACE("executing exe %s\n", debugstr_w(cmd
));
731 rc
= CreateProcessW(NULL
, cmd
, NULL
, NULL
, FALSE
, 0, NULL
,
732 c_collen
, &si
, &info
);
737 ERR("Unable to execute command %s\n", debugstr_w(cmd
));
739 return ERROR_SUCCESS
;
743 return process_handle(package
, type
, info
.hThread
, info
.hProcess
, action
);
746 static UINT
HANDLE_CustomType34(MSIPACKAGE
*package
, LPCWSTR source
,
747 LPCWSTR target
, const INT type
, LPCWSTR action
)
749 LPWSTR filename
, deformated
;
751 PROCESS_INFORMATION info
;
755 memset(&si
,0,sizeof(STARTUPINFOW
));
757 filename
= resolve_folder(package
, source
, FALSE
, FALSE
, NULL
);
760 return ERROR_FUNCTION_FAILED
;
762 SetCurrentDirectoryW(filename
);
765 deformat_string(package
,target
,&deformated
);
768 return ERROR_FUNCTION_FAILED
;
770 TRACE("executing exe %s\n", debugstr_w(deformated
));
772 rc
= CreateProcessW(NULL
, deformated
, NULL
, NULL
, FALSE
, 0, NULL
,
773 c_collen
, &si
, &info
);
777 ERR("Unable to execute command %s\n", debugstr_w(deformated
));
778 msi_free(deformated
);
779 return ERROR_SUCCESS
;
781 msi_free(deformated
);
783 prc
= process_handle(package
, type
, info
.hThread
, info
.hProcess
, action
);
789 void ACTION_FinishCustomActions(MSIPACKAGE
* package
)
791 struct list
*item
, *cursor
;
794 LIST_FOR_EACH_SAFE( item
, cursor
, &package
->RunningActions
)
796 MSIRUNNINGACTION
*action
= LIST_ENTRY( item
, MSIRUNNINGACTION
, entry
);
798 TRACE("Checking on action %s\n", debugstr_w(action
->name
));
800 list_remove( &action
->entry
);
803 GetExitCodeProcess( action
->handle
, &rc
);
805 GetExitCodeThread( action
->handle
, &rc
);
807 if (rc
== STILL_ACTIVE
)
809 TRACE("Waiting on action %s\n", debugstr_w( action
->name
) );
810 msi_dialog_check_messages( action
->handle
);
813 CloseHandle( action
->handle
);
814 msi_free( action
->name
);