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_CustomType50(MSIPACKAGE
*package
, LPCWSTR source
,
71 LPCWSTR target
, const INT type
, LPCWSTR action
);
72 static UINT
HANDLE_CustomType34(MSIPACKAGE
*package
, LPCWSTR source
,
73 LPCWSTR target
, const INT type
, LPCWSTR action
);
75 UINT
ACTION_CustomAction(MSIPACKAGE
*package
,LPCWSTR action
, BOOL execute
)
77 UINT rc
= ERROR_SUCCESS
;
80 static const WCHAR ExecSeqQuery
[] =
81 {'s','e','l','e','c','t',' ','*',' ','f','r','o','m',' ','C','u','s','t','o'
82 ,'m','A','c','t','i','o','n',' ','w','h','e','r','e',' ','`','A','c','t','i'
83 ,'o','n','`',' ','=',' ','`','%','s','`',0};
87 WCHAR
*deformated
=NULL
;
89 rc
= MSI_OpenQuery(package
->db
, &view
, ExecSeqQuery
, action
);
90 if (rc
!= ERROR_SUCCESS
)
93 rc
= MSI_ViewExecute(view
, 0);
94 if (rc
!= ERROR_SUCCESS
)
97 msiobj_release(&view
->hdr
);
101 rc
= MSI_ViewFetch(view
,&row
);
102 if (rc
!= ERROR_SUCCESS
)
105 msiobj_release(&view
->hdr
);
106 return ERROR_CALL_NOT_IMPLEMENTED
;
109 type
= MSI_RecordGetInteger(row
,2);
111 source
= load_dynamic_stringW(row
,3);
112 target
= load_dynamic_stringW(row
,4);
114 TRACE("Handling custom action %s (%x %s %s)\n",debugstr_w(action
),type
,
115 debugstr_w(source
), debugstr_w(target
));
117 /* handle some of the deferred actions */
122 FIXME("Rollback only action... rollbacks not supported yet\n");
123 HeapFree(GetProcessHeap(),0,source
);
124 HeapFree(GetProcessHeap(),0,target
);
125 msiobj_release(&row
->hdr
);
127 msiobj_release(&view
->hdr
);
128 return ERROR_SUCCESS
;
132 LPWSTR
*newbuf
= NULL
;
136 TRACE("Deferring Commit Action!\n");
137 count
= package
->CommitActionCount
;
138 package
->CommitActionCount
++;
140 newbuf
= HeapReAlloc(GetProcessHeap(),0,
141 package
->CommitAction
,
142 package
->CommitActionCount
* sizeof(LPWSTR
));
144 newbuf
= HeapAlloc(GetProcessHeap(),0, sizeof(LPWSTR
));
146 newbuf
[count
] = dupstrW(action
);
147 package
->CommitAction
= newbuf
;
151 TRACE("Deferring Action!\n");
152 count
= package
->DeferredActionCount
;
153 package
->DeferredActionCount
++;
155 newbuf
= HeapReAlloc(GetProcessHeap(),0,
156 package
->DeferredAction
,
157 package
->DeferredActionCount
* sizeof(LPWSTR
));
159 newbuf
= HeapAlloc(GetProcessHeap(),0, sizeof(LPWSTR
));
161 newbuf
[count
] = dupstrW(action
);
162 package
->DeferredAction
= newbuf
;
165 HeapFree(GetProcessHeap(),0,source
);
166 HeapFree(GetProcessHeap(),0,target
);
167 msiobj_release(&row
->hdr
);
169 msiobj_release(&view
->hdr
);
170 return ERROR_SUCCESS
;
176 static const WCHAR szActionData
[] = {
177 'C','u','s','t','o','m','A','c','t','i','o','n','D','a','t','a',0};
178 LPWSTR actiondata
= load_dynamic_property(package
,action
,NULL
);
180 MSI_SetPropertyW(package
,szActionData
,actiondata
);
184 switch (type
& CUSTOM_ACTION_TYPE_MASK
)
186 case 1: /* DLL file stored in a Binary table stream */
187 rc
= HANDLE_CustomType1(package
,source
,target
,type
,action
);
189 case 2: /* EXE file stored in a Binary table strem */
190 rc
= HANDLE_CustomType2(package
,source
,target
,type
,action
);
192 case 18: /*EXE file installed with package */
193 rc
= HANDLE_CustomType18(package
,source
,target
,type
,action
);
195 case 19: /* Error that halts install */
196 deformat_string(package
,target
,&deformated
);
197 MessageBoxW(NULL
,deformated
,NULL
,MB_OK
);
198 rc
= ERROR_FUNCTION_FAILED
;
200 case 50: /*EXE file specified by a property value */
201 rc
= HANDLE_CustomType50(package
,source
,target
,type
,action
);
203 case 34: /*EXE to be run in specified directory */
204 rc
= HANDLE_CustomType34(package
,source
,target
,type
,action
);
206 case 35: /* Directory set with formatted text. */
207 deformat_string(package
,target
,&deformated
);
208 MSI_SetTargetPathW(package
, source
, deformated
);
209 HeapFree(GetProcessHeap(),0,deformated
);
211 case 51: /* Property set with formatted text. */
212 deformat_string(package
,target
,&deformated
);
213 rc
= MSI_SetPropertyW(package
,source
,deformated
);
214 HeapFree(GetProcessHeap(),0,deformated
);
217 FIXME("UNHANDLED ACTION TYPE %i (%s %s)\n",
218 type
& CUSTOM_ACTION_TYPE_MASK
, debugstr_w(source
),
222 HeapFree(GetProcessHeap(),0,source
);
223 HeapFree(GetProcessHeap(),0,target
);
224 msiobj_release(&row
->hdr
);
226 msiobj_release(&view
->hdr
);
231 static UINT
store_binary_to_temp(MSIPACKAGE
*package
, LPCWSTR source
,
236 if (MSI_GetPropertyW(package
, cszTempFolder
, tmp_file
, &sz
)
238 GetTempPathW(MAX_PATH
,tmp_file
);
240 strcatW(tmp_file
,source
);
242 if (GetFileAttributesW(tmp_file
) != INVALID_FILE_ATTRIBUTES
)
244 TRACE("File already exists\n");
245 return ERROR_SUCCESS
;
249 /* write out the file */
253 static const WCHAR fmt
[] =
254 {'s','e','l','e','c','t',' ','*',' ','f','r','o','m',' ','B','i'
255 ,'n','a','r','y',' ','w','h','e','r','e',' ','N','a','m','e','=','`','%','s','`',0};
259 if (track_tempfile(package
, source
, tmp_file
)!=0)
260 FIXME("File Name in temp tracking collision\n");
262 the_file
= CreateFileW(tmp_file
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
,
263 FILE_ATTRIBUTE_NORMAL
, NULL
);
265 if (the_file
== INVALID_HANDLE_VALUE
)
266 return ERROR_FUNCTION_FAILED
;
268 rc
= MSI_OpenQuery(package
->db
, &view
, fmt
, source
);
269 if (rc
!= ERROR_SUCCESS
)
272 rc
= MSI_ViewExecute(view
, 0);
273 if (rc
!= ERROR_SUCCESS
)
276 msiobj_release(&view
->hdr
);
280 rc
= MSI_ViewFetch(view
,&row
);
281 if (rc
!= ERROR_SUCCESS
)
284 msiobj_release(&view
->hdr
);
292 rc
= MSI_RecordReadStream(row
,2,buffer
,&sz
);
293 if (rc
!= ERROR_SUCCESS
)
295 ERR("Failed to get stream\n");
296 CloseHandle(the_file
);
297 DeleteFileW(tmp_file
);
300 WriteFile(the_file
,buffer
,sz
,&write
,NULL
);
301 } while (sz
== 1024);
303 CloseHandle(the_file
);
305 msiobj_release(&row
->hdr
);
307 msiobj_release(&view
->hdr
);
310 return ERROR_SUCCESS
;
313 static void file_running_action(MSIPACKAGE
* package
, HANDLE Handle
,
314 BOOL process
, LPCWSTR name
)
316 MSIRUNNINGACTION
*newbuf
= NULL
;
318 count
= package
->RunningActionCount
;
319 package
->RunningActionCount
++;
321 newbuf
= HeapReAlloc(GetProcessHeap(),0,
322 package
->RunningAction
,
323 package
->RunningActionCount
* sizeof(MSIRUNNINGACTION
));
325 newbuf
= HeapAlloc(GetProcessHeap(),0, sizeof(MSIRUNNINGACTION
));
327 newbuf
[count
].handle
= Handle
;
328 newbuf
[count
].process
= process
;
329 newbuf
[count
].name
= dupstrW(name
);
331 package
->RunningAction
= newbuf
;
334 static UINT
process_action_return_value(UINT type
, HANDLE ThreadHandle
)
340 GetExitCodeProcess(ThreadHandle
,&rc
);
343 return ERROR_SUCCESS
;
345 return ERROR_FUNCTION_FAILED
;
348 GetExitCodeThread(ThreadHandle
,&rc
);
352 case ERROR_FUNCTION_NOT_CALLED
:
354 case ERROR_INSTALL_USEREXIT
:
355 case ERROR_INSTALL_FAILURE
:
357 case ERROR_NO_MORE_ITEMS
:
358 return ERROR_SUCCESS
;
360 return ERROR_FUNCTION_FAILED
;
364 static UINT
process_handle(MSIPACKAGE
* package
, UINT type
,
365 HANDLE ThreadHandle
, HANDLE ProcessHandle
,
368 UINT rc
= ERROR_SUCCESS
;
373 TRACE("Synchronous Execution of action %s\n",debugstr_w(Name
));
375 WaitForSingleObject(ProcessHandle
,INFINITE
);
377 WaitForSingleObject(ThreadHandle
,INFINITE
);
382 rc
= process_action_return_value(2,ProcessHandle
);
384 rc
= process_action_return_value(1,ThreadHandle
);
387 CloseHandle(ThreadHandle
);
389 CloseHandle(ProcessHandle
);
393 TRACE("Asynchronous Execution of action %s\n",debugstr_w(Name
));
399 file_running_action(package
, ProcessHandle
, TRUE
, Name
);
400 CloseHandle(ThreadHandle
);
403 file_running_action(package
, ThreadHandle
, FALSE
, Name
);
407 CloseHandle(ThreadHandle
);
409 CloseHandle(ProcessHandle
);
417 typedef UINT __stdcall
CustomEntry(MSIHANDLE
);
426 static DWORD WINAPI
ACTION_CallDllFunction(thread_struct
*stuff
)
431 DWORD rc
= ERROR_SUCCESS
;
433 TRACE("calling function (%s, %s) \n", debugstr_w(stuff
->source
),
434 debugstr_w(stuff
->target
));
436 hModule
= LoadLibraryW(stuff
->source
);
439 proc
= strdupWtoA( stuff
->target
);
440 fn
= (CustomEntry
*)GetProcAddress(hModule
,proc
);
444 MSIPACKAGE
*package
= stuff
->package
;
446 TRACE("Calling function %s\n", proc
);
447 hPackage
= msiobj_findhandle( &package
->hdr
);
451 msiobj_release( &package
->hdr
);
454 ERR("Handle for object %p not found\n", package
);
457 ERR("Cannot load functon\n");
459 HeapFree(GetProcessHeap(),0,proc
);
460 FreeLibrary(hModule
);
463 ERR("Unable to load library\n");
464 msiobj_release( &stuff
->package
->hdr
);
465 HeapFree(GetProcessHeap(),0,stuff
->source
);
466 HeapFree(GetProcessHeap(),0,stuff
->target
);
467 HeapFree(GetProcessHeap(), 0, stuff
);
471 static DWORD WINAPI
DllThread(LPVOID info
)
473 thread_struct
*stuff
;
476 TRACE("MSI Thread (0x%lx) started for custom action\n",
477 GetCurrentThreadId());
479 stuff
= (thread_struct
*)info
;
480 rc
= ACTION_CallDllFunction(stuff
);
482 TRACE("MSI Thread (0x%lx) finished\n",GetCurrentThreadId());
483 /* clse all handles for this thread */
484 MsiCloseAllHandles();
488 static UINT
HANDLE_CustomType1(MSIPACKAGE
*package
, LPCWSTR source
,
489 LPCWSTR target
, const INT type
, LPCWSTR action
)
491 WCHAR tmp_file
[MAX_PATH
];
495 UINT rc
= ERROR_SUCCESS
;
497 store_binary_to_temp(package
, source
, tmp_file
);
499 TRACE("Calling function %s from %s\n",debugstr_w(target
),
500 debugstr_w(tmp_file
));
502 if (!strchrW(tmp_file
,'.'))
504 static const WCHAR dot
[]={'.',0};
505 strcatW(tmp_file
,dot
);
508 info
= HeapAlloc( GetProcessHeap(), 0, sizeof(*info
) );
509 msiobj_addref( &package
->hdr
);
510 info
->package
= package
;
511 info
->target
= dupstrW(target
);
512 info
->source
= dupstrW(tmp_file
);
514 ThreadHandle
= CreateThread(NULL
,0,DllThread
,(LPVOID
)info
,0,&ThreadId
);
516 rc
= process_handle(package
, type
, ThreadHandle
, NULL
, action
);
521 static UINT
HANDLE_CustomType2(MSIPACKAGE
*package
, LPCWSTR source
,
522 LPCWSTR target
, const INT type
, LPCWSTR action
)
524 WCHAR tmp_file
[MAX_PATH
];
526 PROCESS_INFORMATION info
;
531 static const WCHAR spc
[] = {' ',0};
532 UINT prc
= ERROR_SUCCESS
;
534 memset(&si
,0,sizeof(STARTUPINFOW
));
536 store_binary_to_temp(package
, source
, tmp_file
);
538 deformat_string(package
,target
,&deformated
);
540 len
= strlenW(tmp_file
)+2;
543 len
+= strlenW(deformated
);
545 cmd
= (WCHAR
*)HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR
)*len
);
547 strcpyW(cmd
,tmp_file
);
551 strcatW(cmd
,deformated
);
553 HeapFree(GetProcessHeap(),0,deformated
);
556 TRACE("executing exe %s \n",debugstr_w(cmd
));
558 rc
= CreateProcessW(NULL
, cmd
, NULL
, NULL
, FALSE
, 0, NULL
,
559 c_collen
, &si
, &info
);
561 HeapFree(GetProcessHeap(),0,cmd
);
565 ERR("Unable to execute command\n");
566 return ERROR_SUCCESS
;
569 prc
= process_handle(package
, type
, info
.hThread
, info
.hProcess
, action
);
574 static UINT
HANDLE_CustomType18(MSIPACKAGE
*package
, LPCWSTR source
,
575 LPCWSTR target
, const INT type
, LPCWSTR action
)
578 PROCESS_INFORMATION info
;
583 static const WCHAR spc
[] = {' ',0};
587 memset(&si
,0,sizeof(STARTUPINFOW
));
589 index
= get_loaded_file(package
,source
);
591 len
= strlenW(package
->files
[index
].TargetPath
);
593 deformat_string(package
,target
,&deformated
);
595 len
+= strlenW(deformated
);
598 cmd
= (WCHAR
*)HeapAlloc(GetProcessHeap(),0,len
* sizeof(WCHAR
));
600 strcpyW(cmd
, package
->files
[index
].TargetPath
);
604 strcatW(cmd
, deformated
);
606 HeapFree(GetProcessHeap(),0,deformated
);
609 TRACE("executing exe %s \n",debugstr_w(cmd
));
611 rc
= CreateProcessW(NULL
, cmd
, NULL
, NULL
, FALSE
, 0, NULL
,
612 c_collen
, &si
, &info
);
614 HeapFree(GetProcessHeap(),0,cmd
);
618 ERR("Unable to execute command\n");
619 return ERROR_SUCCESS
;
622 prc
= process_handle(package
, type
, info
.hThread
, info
.hProcess
, action
);
627 static UINT
HANDLE_CustomType50(MSIPACKAGE
*package
, LPCWSTR source
,
628 LPCWSTR target
, const INT type
, LPCWSTR action
)
631 PROCESS_INFORMATION info
;
638 static const WCHAR spc
[] = {' ',0};
640 memset(&si
,0,sizeof(STARTUPINFOW
));
641 memset(&info
,0,sizeof(PROCESS_INFORMATION
));
643 prop
= load_dynamic_property(package
,source
,&prc
);
647 deformat_string(package
,target
,&deformated
);
648 len
= strlenW(prop
) + 2;
650 len
+= strlenW(deformated
);
652 cmd
= (WCHAR
*)HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR
)*len
);
658 strcatW(cmd
,deformated
);
660 HeapFree(GetProcessHeap(),0,deformated
);
663 TRACE("executing exe %s \n",debugstr_w(cmd
));
665 rc
= CreateProcessW(NULL
, cmd
, NULL
, NULL
, FALSE
, 0, NULL
,
666 c_collen
, &si
, &info
);
668 HeapFree(GetProcessHeap(),0,cmd
);
672 ERR("Unable to execute command\n");
673 return ERROR_SUCCESS
;
676 prc
= process_handle(package
, type
, info
.hThread
, info
.hProcess
, action
);
681 static UINT
HANDLE_CustomType34(MSIPACKAGE
*package
, LPCWSTR source
,
682 LPCWSTR target
, const INT type
, LPCWSTR action
)
684 LPWSTR filename
, deformated
;
686 PROCESS_INFORMATION info
;
690 memset(&si
,0,sizeof(STARTUPINFOW
));
692 filename
= resolve_folder(package
, source
, FALSE
, FALSE
, NULL
);
695 return ERROR_FUNCTION_FAILED
;
697 SetCurrentDirectoryW(filename
);
698 HeapFree(GetProcessHeap(),0,filename
);
700 deformat_string(package
,target
,&deformated
);
703 return ERROR_FUNCTION_FAILED
;
705 TRACE("executing exe %s \n",debugstr_w(deformated
));
707 rc
= CreateProcessW(NULL
, deformated
, NULL
, NULL
, FALSE
, 0, NULL
,
708 c_collen
, &si
, &info
);
709 HeapFree(GetProcessHeap(),0,deformated
);
713 ERR("Unable to execute command\n");
714 return ERROR_SUCCESS
;
717 prc
= process_handle(package
, type
, info
.hThread
, info
.hProcess
, action
);
723 void ACTION_FinishCustomActions(MSIPACKAGE
* package
)
728 for (i
= 0; i
< package
->RunningActionCount
; i
++)
730 TRACE("Checking on action %s\n",
731 debugstr_w(package
->RunningAction
[i
].name
));
733 if (package
->RunningAction
[i
].process
)
734 GetExitCodeProcess(package
->RunningAction
[i
].handle
, &rc
);
736 GetExitCodeThread(package
->RunningAction
[i
].handle
, &rc
);
738 if (rc
== STILL_ACTIVE
)
740 TRACE("Waiting on action %s\n",
741 debugstr_w(package
->RunningAction
[i
].name
));
742 WaitForSingleObject(package
->RunningAction
[i
].handle
, INFINITE
);
745 HeapFree(GetProcessHeap(),0,package
->RunningAction
[i
].name
);
746 CloseHandle(package
->RunningAction
[i
].handle
);
749 HeapFree(GetProcessHeap(),0,package
->RunningAction
);