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};
128 LPCWSTR source
, target
;
129 WCHAR
*deformated
=NULL
;
131 row
= MSI_QueryGetRecord( package
->db
, ExecSeqQuery
, action
);
133 return ERROR_CALL_NOT_IMPLEMENTED
;
135 type
= MSI_RecordGetInteger(row
,2);
137 source
= MSI_RecordGetString(row
,3);
138 target
= MSI_RecordGetString(row
,4);
140 TRACE("Handling custom action %s (%x %s %s)\n",debugstr_w(action
),type
,
141 debugstr_w(source
), debugstr_w(target
));
143 /* handle some of the deferred actions */
144 if (type
& msidbCustomActionTypeTSAware
)
145 FIXME("msidbCustomActionTypeTSAware not handled\n");
147 if (type
& msidbCustomActionTypeInScript
)
149 if (type
& msidbCustomActionTypeNoImpersonate
)
150 FIXME("msidbCustomActionTypeNoImpersonate not handled\n");
152 if (type
& msidbCustomActionTypeRollback
)
154 FIXME("Rollback only action... rollbacks not supported yet\n");
155 schedule_action(package
, ROLLBACK_SCRIPT
, action
);
161 if (type
& msidbCustomActionTypeCommit
)
163 TRACE("Deferring Commit Action!\n");
164 schedule_action(package
, COMMIT_SCRIPT
, action
);
168 TRACE("Deferring Action!\n");
169 schedule_action(package
, INSTALL_SCRIPT
, action
);
179 static const WCHAR szActionData
[] = {
180 'C','u','s','t','o','m','A','c','t','i','o','n','D','a','t','a',0};
181 static const WCHAR szBlank
[] = {0};
182 LPWSTR actiondata
= msi_dup_property( package
, action
);
184 MSI_SetPropertyW(package
,szActionData
,actiondata
);
186 MSI_SetPropertyW(package
,szActionData
,szBlank
);
187 msi_free(actiondata
);
190 else if (!check_execution_scheduling_options(package
,action
,type
))
196 switch (type
& CUSTOM_ACTION_TYPE_MASK
)
198 case 1: /* DLL file stored in a Binary table stream */
199 rc
= HANDLE_CustomType1(package
,source
,target
,type
,action
);
201 case 2: /* EXE file stored in a Binary table strem */
202 rc
= HANDLE_CustomType2(package
,source
,target
,type
,action
);
204 case 18: /*EXE file installed with package */
205 rc
= HANDLE_CustomType18(package
,source
,target
,type
,action
);
207 case 19: /* Error that halts install */
208 rc
= HANDLE_CustomType19(package
,source
,target
,type
,action
);
210 case 50: /*EXE file specified by a property value */
211 rc
= HANDLE_CustomType50(package
,source
,target
,type
,action
);
213 case 34: /*EXE to be run in specified directory */
214 rc
= HANDLE_CustomType34(package
,source
,target
,type
,action
);
216 case 35: /* Directory set with formatted text. */
217 deformat_string(package
,target
,&deformated
);
218 MSI_SetTargetPathW(package
, source
, deformated
);
219 msi_free(deformated
);
221 case 51: /* Property set with formatted text. */
222 deformat_string(package
,target
,&deformated
);
223 rc
= MSI_SetPropertyW(package
,source
,deformated
);
224 msi_free(deformated
);
227 FIXME("UNHANDLED ACTION TYPE %i (%s %s)\n",
228 type
& CUSTOM_ACTION_TYPE_MASK
, debugstr_w(source
),
233 msiobj_release(&row
->hdr
);
238 static UINT
store_binary_to_temp(MSIPACKAGE
*package
, LPCWSTR source
,
242 static const WCHAR f1
[] = {'m','s','i',0};
245 if (MSI_GetPropertyW(package
, cszTempFolder
, fmt
, &sz
) != ERROR_SUCCESS
)
246 GetTempPathW(MAX_PATH
,fmt
);
248 if (GetTempFileNameW(fmt
,f1
,0,tmp_file
) == 0)
250 TRACE("Unable to create file\n");
251 return ERROR_FUNCTION_FAILED
;
255 /* write out the file */
258 static const WCHAR fmt
[] =
259 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
260 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',
261 ' ','`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
265 the_file
= CreateFileW(tmp_file
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
,
266 FILE_ATTRIBUTE_NORMAL
, NULL
);
268 if (the_file
== INVALID_HANDLE_VALUE
)
269 return ERROR_FUNCTION_FAILED
;
271 row
= MSI_QueryGetRecord(package
->db
, fmt
, source
);
273 return ERROR_FUNCTION_FAILED
;
279 rc
= MSI_RecordReadStream(row
,2,buffer
,&sz
);
280 if (rc
!= ERROR_SUCCESS
)
282 ERR("Failed to get stream\n");
283 CloseHandle(the_file
);
284 DeleteFileW(tmp_file
);
287 WriteFile(the_file
,buffer
,sz
,&write
,NULL
);
288 } while (sz
== 1024);
290 CloseHandle(the_file
);
292 msiobj_release(&row
->hdr
);
295 return ERROR_SUCCESS
;
298 static void file_running_action(MSIPACKAGE
* package
, HANDLE Handle
,
299 BOOL process
, LPCWSTR name
)
301 MSIRUNNINGACTION
*action
;
303 action
= msi_alloc( sizeof(MSIRUNNINGACTION
) );
305 action
->handle
= Handle
;
306 action
->process
= process
;
307 action
->name
= strdupW(name
);
309 list_add_tail( &package
->RunningActions
, &action
->entry
);
312 static UINT
process_action_return_value(UINT type
, HANDLE ThreadHandle
)
318 GetExitCodeProcess(ThreadHandle
,&rc
);
321 return ERROR_SUCCESS
;
323 return ERROR_FUNCTION_FAILED
;
326 GetExitCodeThread(ThreadHandle
,&rc
);
330 case ERROR_FUNCTION_NOT_CALLED
:
332 case ERROR_INSTALL_USEREXIT
:
333 case ERROR_INSTALL_FAILURE
:
335 case ERROR_NO_MORE_ITEMS
:
336 return ERROR_SUCCESS
;
338 ERR("Invalid Return Code %ld\n",rc
);
339 return ERROR_INSTALL_FAILURE
;
343 static UINT
process_handle(MSIPACKAGE
* package
, UINT type
,
344 HANDLE ThreadHandle
, HANDLE ProcessHandle
,
345 LPCWSTR Name
, BOOL
*finished
)
347 UINT rc
= ERROR_SUCCESS
;
349 if (!(type
& msidbCustomActionTypeAsync
))
352 TRACE("Synchronous Execution of action %s\n",debugstr_w(Name
));
354 msi_dialog_check_messages(ProcessHandle
);
356 msi_dialog_check_messages(ThreadHandle
);
358 if (!(type
& msidbCustomActionTypeContinue
))
361 rc
= process_action_return_value(2,ProcessHandle
);
363 rc
= process_action_return_value(1,ThreadHandle
);
366 CloseHandle(ThreadHandle
);
368 CloseHandle(ProcessHandle
);
374 TRACE("Asynchronous Execution of action %s\n",debugstr_w(Name
));
376 if (type
& msidbCustomActionTypeContinue
)
380 file_running_action(package
, ProcessHandle
, TRUE
, Name
);
381 CloseHandle(ThreadHandle
);
384 file_running_action(package
, ThreadHandle
, FALSE
, Name
);
388 CloseHandle(ThreadHandle
);
390 CloseHandle(ProcessHandle
);
400 typedef UINT __stdcall
CustomEntry(MSIHANDLE
);
409 static DWORD WINAPI
ACTION_CallDllFunction(thread_struct
*stuff
)
414 DWORD rc
= ERROR_SUCCESS
;
416 TRACE("calling function (%s, %s)\n", debugstr_w(stuff
->source
),
417 debugstr_w(stuff
->target
));
419 hModule
= LoadLibraryW(stuff
->source
);
422 proc
= strdupWtoA( stuff
->target
);
423 fn
= (CustomEntry
*)GetProcAddress(hModule
,proc
);
427 MSIPACKAGE
*package
= stuff
->package
;
429 TRACE("Calling function %s\n", proc
);
430 hPackage
= alloc_msihandle( &package
->hdr
);
434 MsiCloseHandle( hPackage
);
437 ERR("Handle for object %p not found\n", package
);
440 ERR("Cannot load functon\n");
443 FreeLibrary(hModule
);
446 ERR("Unable to load library\n");
447 msiobj_release( &stuff
->package
->hdr
);
448 msi_free(stuff
->source
);
449 msi_free(stuff
->target
);
454 static DWORD WINAPI
DllThread(LPVOID info
)
456 thread_struct
*stuff
;
459 TRACE("MSI Thread (0x%lx) started for custom action\n",
460 GetCurrentThreadId());
462 stuff
= (thread_struct
*)info
;
463 rc
= ACTION_CallDllFunction(stuff
);
465 TRACE("MSI Thread (0x%lx) finished (rc %li)\n",GetCurrentThreadId(), rc
);
466 /* clse all handles for this thread */
467 MsiCloseAllHandles();
471 static UINT
HANDLE_CustomType1(MSIPACKAGE
*package
, LPCWSTR source
,
472 LPCWSTR target
, const INT type
, LPCWSTR action
)
474 WCHAR tmp_file
[MAX_PATH
];
478 UINT rc
= ERROR_SUCCESS
;
479 BOOL finished
= FALSE
;
481 store_binary_to_temp(package
, source
, tmp_file
);
483 TRACE("Calling function %s from %s\n",debugstr_w(target
),
484 debugstr_w(tmp_file
));
486 if (!strchrW(tmp_file
,'.'))
488 static const WCHAR dot
[]={'.',0};
489 strcatW(tmp_file
,dot
);
492 info
= msi_alloc( sizeof(*info
) );
493 msiobj_addref( &package
->hdr
);
494 info
->package
= package
;
495 info
->target
= strdupW(target
);
496 info
->source
= strdupW(tmp_file
);
498 ThreadHandle
= CreateThread(NULL
,0,DllThread
,(LPVOID
)info
,0,&ThreadId
);
500 rc
= process_handle(package
, type
, ThreadHandle
, NULL
, action
, &finished
);
503 track_tempfile(package
, tmp_file
, tmp_file
);
505 DeleteFileW(tmp_file
);
510 static UINT
HANDLE_CustomType2(MSIPACKAGE
*package
, LPCWSTR source
,
511 LPCWSTR target
, const INT type
, LPCWSTR action
)
513 WCHAR tmp_file
[MAX_PATH
];
515 PROCESS_INFORMATION info
;
520 static const WCHAR spc
[] = {' ',0};
521 UINT prc
= ERROR_SUCCESS
;
522 BOOL finished
= FALSE
;
524 memset(&si
,0,sizeof(STARTUPINFOW
));
526 store_binary_to_temp(package
, source
, tmp_file
);
528 deformat_string(package
,target
,&deformated
);
530 len
= strlenW(tmp_file
)+2;
533 len
+= strlenW(deformated
);
535 cmd
= msi_alloc(sizeof(WCHAR
)*len
);
537 strcpyW(cmd
,tmp_file
);
541 strcatW(cmd
,deformated
);
543 msi_free(deformated
);
546 TRACE("executing exe %s\n", debugstr_w(cmd
));
548 rc
= CreateProcessW(NULL
, cmd
, NULL
, NULL
, FALSE
, 0, NULL
,
549 c_collen
, &si
, &info
);
554 ERR("Unable to execute command %s\n", debugstr_w(cmd
));
556 return ERROR_SUCCESS
;
560 prc
= process_handle(package
, type
, info
.hThread
, info
.hProcess
, action
,
564 track_tempfile(package
, tmp_file
, tmp_file
);
566 DeleteFileW(tmp_file
);
571 static UINT
HANDLE_CustomType18(MSIPACKAGE
*package
, LPCWSTR source
,
572 LPCWSTR target
, const INT type
, LPCWSTR action
)
575 PROCESS_INFORMATION info
;
580 static const WCHAR spc
[] = {' ',0};
584 memset(&si
,0,sizeof(STARTUPINFOW
));
586 file
= get_loaded_file(package
,source
);
588 return ERROR_FUNCTION_FAILED
;
590 len
= lstrlenW( file
->TargetPath
);
592 deformat_string(package
,target
,&deformated
);
594 len
+= strlenW(deformated
);
597 cmd
= msi_alloc(len
* sizeof(WCHAR
));
599 lstrcpyW( cmd
, file
->TargetPath
);
603 strcatW(cmd
, deformated
);
605 msi_free(deformated
);
608 TRACE("executing exe %s\n", debugstr_w(cmd
));
610 rc
= CreateProcessW(NULL
, cmd
, NULL
, NULL
, FALSE
, 0, NULL
,
611 c_collen
, &si
, &info
);
616 ERR("Unable to execute command %s\n", debugstr_w(cmd
));
618 return ERROR_SUCCESS
;
622 prc
= process_handle(package
, type
, info
.hThread
, info
.hProcess
, action
,
628 static UINT
HANDLE_CustomType19(MSIPACKAGE
*package
, LPCWSTR source
,
629 LPCWSTR target
, const INT type
, LPCWSTR action
)
631 static const WCHAR query
[] = {
632 'S','E','L','E','C','T',' ','`','M','e','s','s','a','g','e','`',' ',
633 'F','R','O','M',' ','`','E','r','r','o','r','`',' ',
634 'W','H','E','R','E',' ','`','E','r','r','o','r','`',' ','=',' ',
638 LPWSTR deformated
= NULL
;
640 deformat_string( package
, target
, &deformated
);
642 /* first try treat the error as a number */
643 row
= MSI_QueryGetRecord( package
->db
, query
, deformated
);
646 LPCWSTR error
= MSI_RecordGetString( row
, 1 );
647 MessageBoxW( NULL
, error
, NULL
, MB_OK
);
648 msiobj_release( &row
->hdr
);
651 MessageBoxW( NULL
, deformated
, NULL
, MB_OK
);
653 msi_free( deformated
);
655 return ERROR_FUNCTION_FAILED
;
658 static UINT
HANDLE_CustomType50(MSIPACKAGE
*package
, LPCWSTR source
,
659 LPCWSTR target
, const INT type
, LPCWSTR action
)
662 PROCESS_INFORMATION info
;
668 static const WCHAR spc
[] = {' ',0};
670 memset(&si
,0,sizeof(STARTUPINFOW
));
671 memset(&info
,0,sizeof(PROCESS_INFORMATION
));
673 prop
= msi_dup_property( package
, source
);
675 return ERROR_SUCCESS
;
677 deformat_string(package
,target
,&deformated
);
678 len
= strlenW(prop
) + 2;
680 len
+= strlenW(deformated
);
682 cmd
= msi_alloc(sizeof(WCHAR
)*len
);
688 strcatW(cmd
,deformated
);
690 msi_free(deformated
);
694 TRACE("executing exe %s\n", debugstr_w(cmd
));
696 rc
= CreateProcessW(NULL
, cmd
, NULL
, NULL
, FALSE
, 0, NULL
,
697 c_collen
, &si
, &info
);
702 ERR("Unable to execute command %s\n", debugstr_w(cmd
));
704 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
);
742 ERR("Unable to execute command %s\n", debugstr_w(deformated
));
743 msi_free(deformated
);
744 return ERROR_SUCCESS
;
746 msi_free(deformated
);
748 prc
= process_handle(package
, type
, info
.hThread
, info
.hProcess
, action
,
755 void ACTION_FinishCustomActions(MSIPACKAGE
* package
)
757 struct list
*item
, *cursor
;
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
);
769 GetExitCodeProcess( action
->handle
, &rc
);
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
);