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
;
82 static const WCHAR ExecSeqQuery
[] =
83 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
84 '`','C','u','s','t','o' ,'m','A','c','t','i','o','n','`',
85 ' ','W','H','E','R','E',' ','`','A','c','t','i' ,'o','n','`',' ',
86 '=',' ','\'','%','s','\'',0};
90 WCHAR
*deformated
=NULL
;
92 rc
= MSI_OpenQuery(package
->db
, &view
, ExecSeqQuery
, action
);
93 if (rc
!= ERROR_SUCCESS
)
96 rc
= MSI_ViewExecute(view
, 0);
97 if (rc
!= ERROR_SUCCESS
)
100 msiobj_release(&view
->hdr
);
104 rc
= MSI_ViewFetch(view
,&row
);
105 if (rc
!= ERROR_SUCCESS
)
108 msiobj_release(&view
->hdr
);
109 return ERROR_CALL_NOT_IMPLEMENTED
;
112 type
= MSI_RecordGetInteger(row
,2);
114 source
= load_dynamic_stringW(row
,3);
115 target
= load_dynamic_stringW(row
,4);
117 TRACE("Handling custom action %s (%x %s %s)\n",debugstr_w(action
),type
,
118 debugstr_w(source
), debugstr_w(target
));
120 /* handle some of the deferred actions */
125 FIXME("Rollback only action... rollbacks not supported yet\n");
126 HeapFree(GetProcessHeap(),0,source
);
127 HeapFree(GetProcessHeap(),0,target
);
128 msiobj_release(&row
->hdr
);
130 msiobj_release(&view
->hdr
);
131 return ERROR_SUCCESS
;
135 LPWSTR
*newbuf
= NULL
;
139 TRACE("Deferring Commit Action!\n");
140 count
= package
->CommitActionCount
;
141 package
->CommitActionCount
++;
143 newbuf
= HeapReAlloc(GetProcessHeap(),0,
144 package
->CommitAction
,
145 package
->CommitActionCount
* sizeof(LPWSTR
));
147 newbuf
= HeapAlloc(GetProcessHeap(),0, sizeof(LPWSTR
));
149 newbuf
[count
] = strdupW(action
);
150 package
->CommitAction
= newbuf
;
154 TRACE("Deferring Action!\n");
155 count
= package
->DeferredActionCount
;
156 package
->DeferredActionCount
++;
158 newbuf
= HeapReAlloc(GetProcessHeap(),0,
159 package
->DeferredAction
,
160 package
->DeferredActionCount
* sizeof(LPWSTR
));
162 newbuf
= HeapAlloc(GetProcessHeap(),0, sizeof(LPWSTR
));
164 newbuf
[count
] = strdupW(action
);
165 package
->DeferredAction
= newbuf
;
168 HeapFree(GetProcessHeap(),0,source
);
169 HeapFree(GetProcessHeap(),0,target
);
170 msiobj_release(&row
->hdr
);
172 msiobj_release(&view
->hdr
);
173 return ERROR_SUCCESS
;
179 static const WCHAR szActionData
[] = {
180 'C','u','s','t','o','m','A','c','t','i','o','n','D','a','t','a',0};
181 LPWSTR actiondata
= load_dynamic_property(package
,action
,NULL
);
183 MSI_SetPropertyW(package
,szActionData
,actiondata
);
187 switch (type
& CUSTOM_ACTION_TYPE_MASK
)
189 case 1: /* DLL file stored in a Binary table stream */
190 rc
= HANDLE_CustomType1(package
,source
,target
,type
,action
);
192 case 2: /* EXE file stored in a Binary table strem */
193 rc
= HANDLE_CustomType2(package
,source
,target
,type
,action
);
195 case 18: /*EXE file installed with package */
196 rc
= HANDLE_CustomType18(package
,source
,target
,type
,action
);
198 case 19: /* Error that halts install */
199 rc
= HANDLE_CustomType19(package
,source
,target
,type
,action
);
201 case 50: /*EXE file specified by a property value */
202 rc
= HANDLE_CustomType50(package
,source
,target
,type
,action
);
204 case 34: /*EXE to be run in specified directory */
205 rc
= HANDLE_CustomType34(package
,source
,target
,type
,action
);
207 case 35: /* Directory set with formatted text. */
208 deformat_string(package
,target
,&deformated
);
209 MSI_SetTargetPathW(package
, source
, deformated
);
210 HeapFree(GetProcessHeap(),0,deformated
);
212 case 51: /* Property set with formatted text. */
213 deformat_string(package
,target
,&deformated
);
214 rc
= MSI_SetPropertyW(package
,source
,deformated
);
215 HeapFree(GetProcessHeap(),0,deformated
);
218 FIXME("UNHANDLED ACTION TYPE %i (%s %s)\n",
219 type
& CUSTOM_ACTION_TYPE_MASK
, debugstr_w(source
),
223 HeapFree(GetProcessHeap(),0,source
);
224 HeapFree(GetProcessHeap(),0,target
);
225 msiobj_release(&row
->hdr
);
227 msiobj_release(&view
->hdr
);
232 static UINT
store_binary_to_temp(MSIPACKAGE
*package
, LPCWSTR source
,
236 static const WCHAR f1
[] = {'m','s','i',0};
239 if (MSI_GetPropertyW(package
, cszTempFolder
, fmt
, &sz
)
241 GetTempPathW(MAX_PATH
,fmt
);
243 if (GetTempFileNameW(fmt
,f1
,0,tmp_file
) == 0)
245 TRACE("Unable to create file\n");
246 return ERROR_FUNCTION_FAILED
;
250 /* write out the file */
254 static const WCHAR fmt
[] =
255 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
256 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',
257 ' ','`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
261 if (track_tempfile(package
, tmp_file
, tmp_file
)!=0)
262 FIXME("File Name in temp tracking collision\n");
264 the_file
= CreateFileW(tmp_file
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
,
265 FILE_ATTRIBUTE_NORMAL
, NULL
);
267 if (the_file
== INVALID_HANDLE_VALUE
)
268 return ERROR_FUNCTION_FAILED
;
270 rc
= MSI_OpenQuery(package
->db
, &view
, fmt
, source
);
271 if (rc
!= ERROR_SUCCESS
)
274 rc
= MSI_ViewExecute(view
, 0);
275 if (rc
!= ERROR_SUCCESS
)
278 msiobj_release(&view
->hdr
);
282 rc
= MSI_ViewFetch(view
,&row
);
283 if (rc
!= ERROR_SUCCESS
)
286 msiobj_release(&view
->hdr
);
294 rc
= MSI_RecordReadStream(row
,2,buffer
,&sz
);
295 if (rc
!= ERROR_SUCCESS
)
297 ERR("Failed to get stream\n");
298 CloseHandle(the_file
);
299 DeleteFileW(tmp_file
);
302 WriteFile(the_file
,buffer
,sz
,&write
,NULL
);
303 } while (sz
== 1024);
305 CloseHandle(the_file
);
307 msiobj_release(&row
->hdr
);
309 msiobj_release(&view
->hdr
);
312 return ERROR_SUCCESS
;
315 static void file_running_action(MSIPACKAGE
* package
, HANDLE Handle
,
316 BOOL process
, LPCWSTR name
)
318 MSIRUNNINGACTION
*newbuf
= NULL
;
320 count
= package
->RunningActionCount
;
321 package
->RunningActionCount
++;
323 newbuf
= HeapReAlloc(GetProcessHeap(),0,
324 package
->RunningAction
,
325 package
->RunningActionCount
* sizeof(MSIRUNNINGACTION
));
327 newbuf
= HeapAlloc(GetProcessHeap(),0, sizeof(MSIRUNNINGACTION
));
329 newbuf
[count
].handle
= Handle
;
330 newbuf
[count
].process
= process
;
331 newbuf
[count
].name
= strdupW(name
);
333 package
->RunningAction
= newbuf
;
336 static UINT
process_action_return_value(UINT type
, HANDLE ThreadHandle
)
342 GetExitCodeProcess(ThreadHandle
,&rc
);
345 return ERROR_SUCCESS
;
347 return ERROR_FUNCTION_FAILED
;
350 GetExitCodeThread(ThreadHandle
,&rc
);
354 case ERROR_FUNCTION_NOT_CALLED
:
356 case ERROR_INSTALL_USEREXIT
:
357 case ERROR_INSTALL_FAILURE
:
359 case ERROR_NO_MORE_ITEMS
:
360 return ERROR_SUCCESS
;
362 ERR("Invalid Return Code %lx\n",rc
);
363 return ERROR_INSTALL_FAILURE
;
367 static UINT
process_handle(MSIPACKAGE
* package
, UINT type
,
368 HANDLE ThreadHandle
, HANDLE ProcessHandle
,
371 UINT rc
= ERROR_SUCCESS
;
376 TRACE("Synchronous Execution of action %s\n",debugstr_w(Name
));
378 msi_dialog_check_messages(ProcessHandle
);
380 msi_dialog_check_messages(ThreadHandle
);
385 rc
= process_action_return_value(2,ProcessHandle
);
387 rc
= process_action_return_value(1,ThreadHandle
);
390 CloseHandle(ThreadHandle
);
392 CloseHandle(ProcessHandle
);
396 TRACE("Asynchronous Execution of action %s\n",debugstr_w(Name
));
402 file_running_action(package
, ProcessHandle
, TRUE
, Name
);
403 CloseHandle(ThreadHandle
);
406 file_running_action(package
, ThreadHandle
, FALSE
, Name
);
410 CloseHandle(ThreadHandle
);
412 CloseHandle(ProcessHandle
);
420 typedef UINT __stdcall
CustomEntry(MSIHANDLE
);
429 static DWORD WINAPI
ACTION_CallDllFunction(thread_struct
*stuff
)
434 DWORD rc
= ERROR_SUCCESS
;
436 TRACE("calling function (%s, %s) \n", debugstr_w(stuff
->source
),
437 debugstr_w(stuff
->target
));
439 hModule
= LoadLibraryW(stuff
->source
);
442 proc
= strdupWtoA( stuff
->target
);
443 fn
= (CustomEntry
*)GetProcAddress(hModule
,proc
);
447 MSIPACKAGE
*package
= stuff
->package
;
449 TRACE("Calling function %s\n", proc
);
450 hPackage
= msiobj_findhandle( &package
->hdr
);
454 msiobj_release( &package
->hdr
);
457 ERR("Handle for object %p not found\n", package
);
460 ERR("Cannot load functon\n");
462 HeapFree(GetProcessHeap(),0,proc
);
463 FreeLibrary(hModule
);
466 ERR("Unable to load library\n");
467 msiobj_release( &stuff
->package
->hdr
);
468 HeapFree(GetProcessHeap(),0,stuff
->source
);
469 HeapFree(GetProcessHeap(),0,stuff
->target
);
470 HeapFree(GetProcessHeap(), 0, stuff
);
474 static DWORD WINAPI
DllThread(LPVOID info
)
476 thread_struct
*stuff
;
479 TRACE("MSI Thread (0x%lx) started for custom action\n",
480 GetCurrentThreadId());
482 stuff
= (thread_struct
*)info
;
483 rc
= ACTION_CallDllFunction(stuff
);
485 TRACE("MSI Thread (0x%lx) finished (rc %li)\n",GetCurrentThreadId(), rc
);
486 /* clse all handles for this thread */
487 MsiCloseAllHandles();
491 static UINT
HANDLE_CustomType1(MSIPACKAGE
*package
, LPCWSTR source
,
492 LPCWSTR target
, const INT type
, LPCWSTR action
)
494 WCHAR tmp_file
[MAX_PATH
];
498 UINT rc
= ERROR_SUCCESS
;
500 store_binary_to_temp(package
, source
, tmp_file
);
502 TRACE("Calling function %s from %s\n",debugstr_w(target
),
503 debugstr_w(tmp_file
));
505 if (!strchrW(tmp_file
,'.'))
507 static const WCHAR dot
[]={'.',0};
508 strcatW(tmp_file
,dot
);
511 info
= HeapAlloc( GetProcessHeap(), 0, sizeof(*info
) );
512 msiobj_addref( &package
->hdr
);
513 info
->package
= package
;
514 info
->target
= strdupW(target
);
515 info
->source
= strdupW(tmp_file
);
517 ThreadHandle
= CreateThread(NULL
,0,DllThread
,(LPVOID
)info
,0,&ThreadId
);
519 rc
= process_handle(package
, type
, ThreadHandle
, NULL
, action
);
524 static UINT
HANDLE_CustomType2(MSIPACKAGE
*package
, LPCWSTR source
,
525 LPCWSTR target
, const INT type
, LPCWSTR action
)
527 WCHAR tmp_file
[MAX_PATH
];
529 PROCESS_INFORMATION info
;
534 static const WCHAR spc
[] = {' ',0};
535 UINT prc
= ERROR_SUCCESS
;
537 memset(&si
,0,sizeof(STARTUPINFOW
));
539 store_binary_to_temp(package
, source
, tmp_file
);
541 deformat_string(package
,target
,&deformated
);
543 len
= strlenW(tmp_file
)+2;
546 len
+= strlenW(deformated
);
548 cmd
= HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR
)*len
);
550 strcpyW(cmd
,tmp_file
);
554 strcatW(cmd
,deformated
);
556 HeapFree(GetProcessHeap(),0,deformated
);
559 TRACE("executing exe %s \n",debugstr_w(cmd
));
561 rc
= CreateProcessW(NULL
, cmd
, NULL
, NULL
, FALSE
, 0, NULL
,
562 c_collen
, &si
, &info
);
564 HeapFree(GetProcessHeap(),0,cmd
);
568 ERR("Unable to execute command\n");
569 return ERROR_SUCCESS
;
572 prc
= process_handle(package
, type
, info
.hThread
, info
.hProcess
, action
);
577 static UINT
HANDLE_CustomType18(MSIPACKAGE
*package
, LPCWSTR source
,
578 LPCWSTR target
, const INT type
, LPCWSTR action
)
581 PROCESS_INFORMATION info
;
586 static const WCHAR spc
[] = {' ',0};
590 memset(&si
,0,sizeof(STARTUPINFOW
));
592 index
= get_loaded_file(package
,source
);
594 len
= strlenW(package
->files
[index
].TargetPath
);
596 deformat_string(package
,target
,&deformated
);
598 len
+= strlenW(deformated
);
601 cmd
= HeapAlloc(GetProcessHeap(),0,len
* sizeof(WCHAR
));
603 strcpyW(cmd
, package
->files
[index
].TargetPath
);
607 strcatW(cmd
, deformated
);
609 HeapFree(GetProcessHeap(),0,deformated
);
612 TRACE("executing exe %s \n",debugstr_w(cmd
));
614 rc
= CreateProcessW(NULL
, cmd
, NULL
, NULL
, FALSE
, 0, NULL
,
615 c_collen
, &si
, &info
);
617 HeapFree(GetProcessHeap(),0,cmd
);
621 ERR("Unable to execute command\n");
622 return ERROR_SUCCESS
;
625 prc
= process_handle(package
, type
, info
.hThread
, info
.hProcess
, action
);
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','`',' ','=',' ',
639 MSIQUERY
*view
= NULL
;
642 LPWSTR deformated
= NULL
;
644 deformat_string( package
, target
, &deformated
);
646 /* first try treat the error as a number */
647 r
= MSI_OpenQuery( package
->db
, &view
, query
, deformated
);
648 if( r
== ERROR_SUCCESS
)
650 r
= MSI_ViewExecute( view
, 0 );
651 if( r
== ERROR_SUCCESS
)
653 r
= MSI_ViewFetch( view
, &row
);
654 if( r
== ERROR_SUCCESS
)
656 LPCWSTR error
= MSI_RecordGetString( row
, 1 );
657 MessageBoxW( NULL
, error
, NULL
, MB_OK
);
658 msiobj_release( &row
->hdr
);
661 MSI_ViewClose( view
);
662 msiobj_release( &view
->hdr
);
665 if (r
!= ERROR_SUCCESS
)
667 MessageBoxW( NULL
, deformated
, NULL
, MB_OK
);
668 HeapFree( GetProcessHeap(), 0, deformated
);
671 return ERROR_FUNCTION_FAILED
;
674 static UINT
HANDLE_CustomType50(MSIPACKAGE
*package
, LPCWSTR source
,
675 LPCWSTR target
, const INT type
, LPCWSTR action
)
678 PROCESS_INFORMATION info
;
685 static const WCHAR spc
[] = {' ',0};
687 memset(&si
,0,sizeof(STARTUPINFOW
));
688 memset(&info
,0,sizeof(PROCESS_INFORMATION
));
690 prop
= load_dynamic_property(package
,source
,&prc
);
692 return ERROR_SUCCESS
;
694 deformat_string(package
,target
,&deformated
);
695 len
= strlenW(prop
) + 2;
697 len
+= strlenW(deformated
);
699 cmd
= HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR
)*len
);
705 strcatW(cmd
,deformated
);
707 HeapFree(GetProcessHeap(),0,deformated
);
710 TRACE("executing exe %s \n",debugstr_w(cmd
));
712 rc
= CreateProcessW(NULL
, cmd
, NULL
, NULL
, FALSE
, 0, NULL
,
713 c_collen
, &si
, &info
);
715 HeapFree(GetProcessHeap(),0,cmd
);
719 ERR("Unable to execute command\n");
720 return ERROR_SUCCESS
;
723 prc
= process_handle(package
, type
, info
.hThread
, info
.hProcess
, action
);
728 static UINT
HANDLE_CustomType34(MSIPACKAGE
*package
, LPCWSTR source
,
729 LPCWSTR target
, const INT type
, LPCWSTR action
)
731 LPWSTR filename
, deformated
;
733 PROCESS_INFORMATION info
;
737 memset(&si
,0,sizeof(STARTUPINFOW
));
739 filename
= resolve_folder(package
, source
, FALSE
, FALSE
, NULL
);
742 return ERROR_FUNCTION_FAILED
;
744 SetCurrentDirectoryW(filename
);
745 HeapFree(GetProcessHeap(),0,filename
);
747 deformat_string(package
,target
,&deformated
);
750 return ERROR_FUNCTION_FAILED
;
752 TRACE("executing exe %s \n",debugstr_w(deformated
));
754 rc
= CreateProcessW(NULL
, deformated
, NULL
, NULL
, FALSE
, 0, NULL
,
755 c_collen
, &si
, &info
);
756 HeapFree(GetProcessHeap(),0,deformated
);
760 ERR("Unable to execute command\n");
761 return ERROR_SUCCESS
;
764 prc
= process_handle(package
, type
, info
.hThread
, info
.hProcess
, action
);
770 void ACTION_FinishCustomActions(MSIPACKAGE
* package
)
775 for (i
= 0; i
< package
->RunningActionCount
; i
++)
777 TRACE("Checking on action %s\n",
778 debugstr_w(package
->RunningAction
[i
].name
));
780 if (package
->RunningAction
[i
].process
)
781 GetExitCodeProcess(package
->RunningAction
[i
].handle
, &rc
);
783 GetExitCodeThread(package
->RunningAction
[i
].handle
, &rc
);
785 if (rc
== STILL_ACTIVE
)
787 TRACE("Waiting on action %s\n",
788 debugstr_w(package
->RunningAction
[i
].name
));
789 msi_dialog_check_messages(package
->RunningAction
[i
].handle
);
792 HeapFree(GetProcessHeap(),0,package
->RunningAction
[i
].name
);
793 CloseHandle(package
->RunningAction
[i
].handle
);
796 HeapFree(GetProcessHeap(),0,package
->RunningAction
);