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
23 #include <wine/exception.h>
26 #include "msvchelper.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(msi
);
31 #define CUSTOM_ACTION_TYPE_MASK 0x3F
33 typedef struct tagMSIRUNNINGACTION
41 typedef UINT (WINAPI
*MsiCustomActionEntryPoint
)( MSIHANDLE
);
43 static CRITICAL_SECTION msi_custom_action_cs
;
44 static CRITICAL_SECTION_DEBUG msi_custom_action_cs_debug
=
46 0, 0, &msi_custom_action_cs
,
47 { &msi_custom_action_cs_debug
.ProcessLocksList
,
48 &msi_custom_action_cs_debug
.ProcessLocksList
},
49 0, 0, { (DWORD_PTR
)(__FILE__
": msi_custom_action_cs") }
51 static CRITICAL_SECTION msi_custom_action_cs
= { &msi_custom_action_cs_debug
, -1, 0, 0, 0, 0 };
53 static struct list msi_pending_custom_actions
= LIST_INIT( msi_pending_custom_actions
);
55 UINT
msi_schedule_action( MSIPACKAGE
*package
, UINT script
, const WCHAR
*action
)
58 WCHAR
**newbuf
= NULL
;
60 if (script
>= SCRIPT_MAX
)
62 FIXME("Unknown script requested %u\n", script
);
63 return ERROR_FUNCTION_FAILED
;
65 TRACE("Scheduling action %s in script %u\n", debugstr_w(action
), script
);
67 count
= package
->script
->ActionCount
[script
];
68 package
->script
->ActionCount
[script
]++;
69 if (count
!= 0) newbuf
= msi_realloc( package
->script
->Actions
[script
],
70 package
->script
->ActionCount
[script
] * sizeof(WCHAR
*) );
71 else newbuf
= msi_alloc( sizeof(WCHAR
*) );
73 newbuf
[count
] = strdupW( action
);
74 package
->script
->Actions
[script
] = newbuf
;
78 UINT
msi_register_unique_action( MSIPACKAGE
*package
, const WCHAR
*action
)
81 WCHAR
**newbuf
= NULL
;
83 if (!package
->script
) return FALSE
;
85 TRACE("Registering %s as unique action\n", debugstr_w(action
));
87 count
= package
->script
->UniqueActionsCount
;
88 package
->script
->UniqueActionsCount
++;
89 if (count
!= 0) newbuf
= msi_realloc( package
->script
->UniqueActions
,
90 package
->script
->UniqueActionsCount
* sizeof(WCHAR
*) );
91 else newbuf
= msi_alloc( sizeof(WCHAR
*) );
93 newbuf
[count
] = strdupW( action
);
94 package
->script
->UniqueActions
= newbuf
;
98 BOOL
msi_action_is_unique( const MSIPACKAGE
*package
, const WCHAR
*action
)
102 if (!package
->script
) return FALSE
;
104 for (i
= 0; i
< package
->script
->UniqueActionsCount
; i
++)
106 if (!strcmpW( package
->script
->UniqueActions
[i
], action
)) return TRUE
;
111 static BOOL
check_execution_scheduling_options(MSIPACKAGE
*package
, LPCWSTR action
, UINT options
)
113 if (!package
->script
)
116 if ((options
& msidbCustomActionTypeClientRepeat
) ==
117 msidbCustomActionTypeClientRepeat
)
119 if (!(package
->script
->InWhatSequence
& SEQUENCE_UI
&&
120 package
->script
->InWhatSequence
& SEQUENCE_EXEC
))
122 TRACE("Skipping action due to dbCustomActionTypeClientRepeat option.\n");
126 else if (options
& msidbCustomActionTypeFirstSequence
)
128 if (package
->script
->InWhatSequence
& SEQUENCE_UI
&&
129 package
->script
->InWhatSequence
& SEQUENCE_EXEC
)
131 TRACE("Skipping action due to msidbCustomActionTypeFirstSequence option.\n");
135 else if (options
& msidbCustomActionTypeOncePerProcess
)
137 if (msi_action_is_unique(package
, action
))
139 TRACE("Skipping action due to msidbCustomActionTypeOncePerProcess option.\n");
143 msi_register_unique_action(package
, action
);
149 /* stores the following properties before the action:
151 * [CustomActionData<=>UserSID<=>ProductCode]Action
153 static LPWSTR
msi_get_deferred_action(LPCWSTR action
, LPCWSTR actiondata
,
154 LPCWSTR usersid
, LPCWSTR prodcode
)
159 static const WCHAR format
[] = {
160 '[','%','s','<','=','>','%','s','<','=','>','%','s',']','%','s',0
164 return strdupW(action
);
166 len
= lstrlenW(action
) + lstrlenW(actiondata
) +
167 lstrlenW(usersid
) + lstrlenW(prodcode
) +
168 lstrlenW(format
) - 7;
169 deferred
= msi_alloc(len
* sizeof(WCHAR
));
171 sprintfW(deferred
, format
, actiondata
, usersid
, prodcode
, action
);
175 static void set_deferred_action_props( MSIPACKAGE
*package
, const WCHAR
*deferred_data
)
177 static const WCHAR sep
[] = {'<','=','>',0};
178 const WCHAR
*end
, *beg
= deferred_data
+ 1;
180 end
= strstrW(beg
, sep
);
181 msi_set_property( package
->db
, szCustomActionData
, beg
, end
- beg
);
184 end
= strstrW(beg
, sep
);
185 msi_set_property( package
->db
, szUserSID
, beg
, end
- beg
);
188 end
= strchrW(beg
, ']');
189 msi_set_property( package
->db
, szProductCode
, beg
, end
- beg
);
192 WCHAR
*msi_create_temp_file( MSIDATABASE
*db
)
199 UINT len
= sizeof(tmp
)/sizeof(tmp
[0]);
201 if (msi_get_property( db
, szTempFolder
, tmp
, &len
) ||
202 GetFileAttributesW( tmp
) != FILE_ATTRIBUTE_DIRECTORY
)
204 GetTempPathW( MAX_PATH
, tmp
);
206 if (!(db
->tempfolder
= strdupW( tmp
))) return NULL
;
209 if ((ret
= msi_alloc( (strlenW( db
->tempfolder
) + 20) * sizeof(WCHAR
) )))
211 if (!GetTempFileNameW( db
->tempfolder
, szMsi
, 0, ret
))
221 static MSIBINARY
*create_temp_binary( MSIPACKAGE
*package
, LPCWSTR source
, BOOL dll
)
223 static const WCHAR query
[] = {
224 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
225 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',' ',
226 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
228 MSIBINARY
*binary
= NULL
;
235 if (!(tmpfile
= msi_create_temp_file( package
->db
))) return NULL
;
237 if (!(row
= MSI_QueryGetRecord( package
->db
, query
, source
))) goto error
;
238 if (!(binary
= msi_alloc_zero( sizeof(MSIBINARY
) ))) goto error
;
240 file
= CreateFileW( tmpfile
, GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
241 if (file
== INVALID_HANDLE_VALUE
) goto error
;
246 r
= MSI_RecordReadStream( row
, 2, buffer
, &sz
);
247 if (r
!= ERROR_SUCCESS
)
249 ERR("Failed to get stream\n");
252 WriteFile( file
, buffer
, sz
, &write
, NULL
);
253 } while (sz
== sizeof buffer
);
256 if (r
!= ERROR_SUCCESS
) goto error
;
258 /* keep a reference to prevent the dll from being unloaded */
259 if (dll
&& !(binary
->module
= LoadLibraryW( tmpfile
)))
261 WARN( "failed to load dll %s (%u)\n", debugstr_w( tmpfile
), GetLastError() );
263 binary
->source
= strdupW( source
);
264 binary
->tmpfile
= tmpfile
;
265 list_add_tail( &package
->binaries
, &binary
->entry
);
267 msiobj_release( &row
->hdr
);
271 if (row
) msiobj_release( &row
->hdr
);
272 DeleteFileW( tmpfile
);
278 static MSIBINARY
*get_temp_binary( MSIPACKAGE
*package
, LPCWSTR source
, BOOL dll
)
282 LIST_FOR_EACH_ENTRY( binary
, &package
->binaries
, MSIBINARY
, entry
)
284 if (!strcmpW( binary
->source
, source
))
288 return create_temp_binary( package
, source
, dll
);
291 static void file_running_action(MSIPACKAGE
* package
, HANDLE Handle
,
292 BOOL process
, LPCWSTR name
)
294 MSIRUNNINGACTION
*action
;
296 action
= msi_alloc( sizeof(MSIRUNNINGACTION
) );
298 action
->handle
= Handle
;
299 action
->process
= process
;
300 action
->name
= strdupW(name
);
302 list_add_tail( &package
->RunningActions
, &action
->entry
);
305 static UINT
custom_get_process_return( HANDLE process
)
309 GetExitCodeProcess( process
, &rc
);
310 TRACE("exit code is %u\n", rc
);
312 return ERROR_FUNCTION_FAILED
;
313 return ERROR_SUCCESS
;
316 static UINT
custom_get_thread_return( MSIPACKAGE
*package
, HANDLE thread
)
320 GetExitCodeThread( thread
, &rc
);
324 case ERROR_FUNCTION_NOT_CALLED
:
326 case ERROR_INSTALL_USEREXIT
:
327 case ERROR_INSTALL_FAILURE
:
329 case ERROR_NO_MORE_ITEMS
:
330 return ERROR_SUCCESS
;
331 case ERROR_INSTALL_SUSPEND
:
332 ACTION_ForceReboot( package
);
333 return ERROR_SUCCESS
;
335 ERR("Invalid Return Code %d\n",rc
);
336 return ERROR_INSTALL_FAILURE
;
340 static UINT
wait_process_handle(MSIPACKAGE
* package
, UINT type
,
341 HANDLE ProcessHandle
, LPCWSTR name
)
343 UINT rc
= ERROR_SUCCESS
;
345 if (!(type
& msidbCustomActionTypeAsync
))
347 TRACE("waiting for %s\n", debugstr_w(name
));
349 msi_dialog_check_messages(ProcessHandle
);
351 if (!(type
& msidbCustomActionTypeContinue
))
352 rc
= custom_get_process_return(ProcessHandle
);
354 CloseHandle(ProcessHandle
);
358 TRACE("%s running in background\n", debugstr_w(name
));
360 if (!(type
& msidbCustomActionTypeContinue
))
361 file_running_action(package
, ProcessHandle
, TRUE
, name
);
363 CloseHandle(ProcessHandle
);
369 typedef struct _msi_custom_action_info
{
379 } msi_custom_action_info
;
381 static void release_custom_action_data( msi_custom_action_info
*info
)
383 EnterCriticalSection( &msi_custom_action_cs
);
387 list_remove( &info
->entry
);
389 CloseHandle( info
->handle
);
390 msi_free( info
->action
);
391 msi_free( info
->source
);
392 msi_free( info
->target
);
393 msiobj_release( &info
->package
->hdr
);
397 LeaveCriticalSection( &msi_custom_action_cs
);
400 /* must be called inside msi_custom_action_cs if info is in the pending custom actions list */
401 static void addref_custom_action_data( msi_custom_action_info
*info
)
406 static UINT
wait_thread_handle( msi_custom_action_info
*info
)
408 UINT rc
= ERROR_SUCCESS
;
410 if (!(info
->type
& msidbCustomActionTypeAsync
))
412 TRACE("waiting for %s\n", debugstr_w( info
->action
));
414 msi_dialog_check_messages( info
->handle
);
416 if (!(info
->type
& msidbCustomActionTypeContinue
))
417 rc
= custom_get_thread_return( info
->package
, info
->handle
);
419 release_custom_action_data( info
);
423 TRACE("%s running in background\n", debugstr_w( info
->action
));
429 static msi_custom_action_info
*find_action_by_guid( const GUID
*guid
)
431 msi_custom_action_info
*info
;
434 EnterCriticalSection( &msi_custom_action_cs
);
436 LIST_FOR_EACH_ENTRY( info
, &msi_pending_custom_actions
, msi_custom_action_info
, entry
)
438 if (IsEqualGUID( &info
->guid
, guid
))
440 addref_custom_action_data( info
);
446 LeaveCriticalSection( &msi_custom_action_cs
);
454 static void handle_msi_break( LPCWSTR target
)
459 static const WCHAR MsiBreak
[] = { 'M','s','i','B','r','e','a','k',0 };
460 static const WCHAR WindowsInstaller
[] = {
461 'W','i','n','d','o','w','s',' ','I','n','s','t','a','l','l','e','r',0
464 static const WCHAR format
[] = {
465 'T','o',' ','d','e','b','u','g',' ','y','o','u','r',' ',
466 'c','u','s','t','o','m',' ','a','c','t','i','o','n',',',' ',
467 'a','t','t','a','c','h',' ','y','o','u','r',' ','d','e','b','u','g','g','e','r',' ',
468 't','o',' ','p','r','o','c','e','s','s',' ','%','i',' ','(','0','x','%','X',')',' ',
469 'a','n','d',' ','p','r','e','s','s',' ','O','K',0
472 if( !GetEnvironmentVariableW( MsiBreak
, val
, MAX_PATH
))
475 if( strcmpiW( val
, target
))
478 msg
= msi_alloc( (lstrlenW(format
) + 10) * sizeof(WCHAR
) );
482 wsprintfW( msg
, format
, GetCurrentProcessId(), GetCurrentProcessId());
483 MessageBoxW( NULL
, msg
, WindowsInstaller
, MB_OK
);
488 static UINT
get_action_info( const GUID
*guid
, INT
*type
, MSIHANDLE
*handle
,
489 BSTR
*dll
, BSTR
*funcname
,
490 IWineMsiRemotePackage
**package
)
492 IClassFactory
*cf
= NULL
;
493 IWineMsiRemoteCustomAction
*rca
= NULL
;
496 r
= DllGetClassObject( &CLSID_WineMsiRemoteCustomAction
,
497 &IID_IClassFactory
, (LPVOID
*)&cf
);
500 ERR("failed to get IClassFactory interface\n");
501 return ERROR_FUNCTION_FAILED
;
504 r
= IClassFactory_CreateInstance( cf
, NULL
, &IID_IWineMsiRemoteCustomAction
, (LPVOID
*)&rca
);
507 ERR("failed to get IWineMsiRemoteCustomAction interface\n");
508 return ERROR_FUNCTION_FAILED
;
511 r
= IWineMsiRemoteCustomAction_GetActionInfo( rca
, guid
, type
, handle
, dll
, funcname
, package
);
512 IWineMsiRemoteCustomAction_Release( rca
);
515 ERR("GetActionInfo failed\n");
516 return ERROR_FUNCTION_FAILED
;
519 return ERROR_SUCCESS
;
523 extern UINT
CUSTOMPROC_wrapper( MsiCustomActionEntryPoint proc
, MSIHANDLE handle
);
524 __ASM_GLOBAL_FUNC( CUSTOMPROC_wrapper
,
526 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
527 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
529 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
532 "movl 8(%ebp),%eax\n\t"
535 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
536 __ASM_CFI(".cfi_same_value %ebp\n\t")
539 static inline UINT
CUSTOMPROC_wrapper( MsiCustomActionEntryPoint proc
, MSIHANDLE handle
)
545 static DWORD
ACTION_CallDllFunction( const GUID
*guid
)
547 MsiCustomActionEntryPoint fn
;
548 MSIHANDLE hPackage
, handle
;
551 UINT r
= ERROR_FUNCTION_FAILED
;
552 BSTR dll
= NULL
, function
= NULL
;
554 IWineMsiRemotePackage
*remote_package
= NULL
;
556 TRACE("%s\n", debugstr_guid( guid
));
558 r
= get_action_info( guid
, &type
, &handle
, &dll
, &function
, &remote_package
);
559 if (r
!= ERROR_SUCCESS
)
562 hModule
= LoadLibraryW( dll
);
565 WARN( "failed to load dll %s (%u)\n", debugstr_w( dll
), GetLastError() );
566 return ERROR_SUCCESS
;
569 proc
= strdupWtoA( function
);
570 fn
= (MsiCustomActionEntryPoint
) GetProcAddress( hModule
, proc
);
574 hPackage
= alloc_msi_remote_handle( (IUnknown
*)remote_package
);
577 IWineMsiRemotePackage_SetMsiHandle( remote_package
, handle
);
578 TRACE("calling %s\n", debugstr_w( function
) );
579 handle_msi_break( function
);
583 r
= CUSTOMPROC_wrapper( fn
, hPackage
);
587 ERR("Custom action (%s:%s) caused a page fault: %08x\n",
588 debugstr_w(dll
), debugstr_w(function
), GetExceptionCode());
593 MsiCloseHandle( hPackage
);
596 ERR("failed to create handle for %p\n", remote_package
);
599 ERR("GetProcAddress(%s) failed\n", debugstr_w( function
) );
601 FreeLibrary(hModule
);
603 IWineMsiRemotePackage_Release( remote_package
);
604 SysFreeString( dll
);
605 SysFreeString( function
);
606 MsiCloseHandle( handle
);
611 static DWORD WINAPI
DllThread( LPVOID arg
)
616 TRACE("custom action (%x) started\n", GetCurrentThreadId() );
618 rc
= ACTION_CallDllFunction( guid
);
620 TRACE("custom action (%x) returned %i\n", GetCurrentThreadId(), rc
);
622 MsiCloseAllHandles();
626 static msi_custom_action_info
*do_msidbCustomActionTypeDll(
627 MSIPACKAGE
*package
, INT type
, LPCWSTR source
, LPCWSTR target
, LPCWSTR action
)
629 msi_custom_action_info
*info
;
631 info
= msi_alloc( sizeof *info
);
635 msiobj_addref( &package
->hdr
);
636 info
->refs
= 2; /* 1 for our caller and 1 for thread we created */
637 info
->package
= package
;
639 info
->target
= strdupW( target
);
640 info
->source
= strdupW( source
);
641 info
->action
= strdupW( action
);
642 CoCreateGuid( &info
->guid
);
644 EnterCriticalSection( &msi_custom_action_cs
);
645 list_add_tail( &msi_pending_custom_actions
, &info
->entry
);
646 LeaveCriticalSection( &msi_custom_action_cs
);
648 info
->handle
= CreateThread( NULL
, 0, DllThread
, &info
->guid
, 0, NULL
);
651 /* release both references */
652 release_custom_action_data( info
);
653 release_custom_action_data( info
);
660 static UINT
HANDLE_CustomType1( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
661 INT type
, const WCHAR
*action
)
663 msi_custom_action_info
*info
;
666 if (!(binary
= get_temp_binary( package
, source
, TRUE
)))
667 return ERROR_FUNCTION_FAILED
;
669 TRACE("Calling function %s from %s\n", debugstr_w(target
), debugstr_w(binary
->tmpfile
));
671 info
= do_msidbCustomActionTypeDll( package
, type
, binary
->tmpfile
, target
, action
);
672 return wait_thread_handle( info
);
675 static HANDLE
execute_command( const WCHAR
*app
, WCHAR
*arg
, const WCHAR
*dir
)
677 static const WCHAR dotexeW
[] = {'.','e','x','e',0};
679 PROCESS_INFORMATION info
;
680 WCHAR
*exe
= NULL
, *cmd
= NULL
, *p
;
688 if (!(exe
= msi_alloc( MAX_PATH
* sizeof(WCHAR
) ))) return INVALID_HANDLE_VALUE
;
689 len_exe
= SearchPathW( NULL
, app
, dotexeW
, MAX_PATH
, exe
, NULL
);
690 if (len_exe
>= MAX_PATH
)
693 if (!(exe
= msi_alloc( len_exe
* sizeof(WCHAR
) ))) return INVALID_HANDLE_VALUE
;
694 len_exe
= SearchPathW( NULL
, app
, dotexeW
, len_exe
, exe
, NULL
);
698 WARN("can't find executable %u\n", GetLastError());
700 return INVALID_HANDLE_VALUE
;
703 if (arg
) len_arg
= strlenW( arg
);
704 if (!(cmd
= msi_alloc( (len_exe
+ len_arg
+ 4) * sizeof(WCHAR
) )))
707 return INVALID_HANDLE_VALUE
;
710 if (strchrW( exe
, ' ' ))
713 memcpy( p
, exe
, len_exe
* sizeof(WCHAR
) );
726 memcpy( p
, arg
, len_arg
* sizeof(WCHAR
) );
730 memset( &si
, 0, sizeof(STARTUPINFOW
) );
731 ret
= CreateProcessW( exe
, exe
? cmd
: arg
, NULL
, NULL
, FALSE
, 0, NULL
, dir
, &si
, &info
);
736 WARN("unable to execute command %u\n", GetLastError());
737 return INVALID_HANDLE_VALUE
;
739 CloseHandle( info
.hThread
);
740 return info
.hProcess
;
743 static UINT
HANDLE_CustomType2( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
744 INT type
, const WCHAR
*action
)
750 if (!(binary
= get_temp_binary( package
, source
, FALSE
))) return ERROR_FUNCTION_FAILED
;
752 deformat_string( package
, target
, &arg
);
753 TRACE("exe %s arg %s\n", debugstr_w(binary
->tmpfile
), debugstr_w(arg
));
755 handle
= execute_command( binary
->tmpfile
, arg
, szCRoot
);
757 if (handle
== INVALID_HANDLE_VALUE
) return ERROR_SUCCESS
;
758 return wait_process_handle( package
, type
, handle
, action
);
761 static UINT
HANDLE_CustomType17( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
762 INT type
, const WCHAR
*action
)
764 msi_custom_action_info
*info
;
767 TRACE("%s %s\n", debugstr_w(source
), debugstr_w(target
));
769 file
= msi_get_loaded_file( package
, source
);
772 ERR("invalid file key %s\n", debugstr_w( source
));
773 return ERROR_FUNCTION_FAILED
;
776 info
= do_msidbCustomActionTypeDll( package
, type
, file
->TargetPath
, target
, action
);
777 return wait_thread_handle( info
);
780 static UINT
HANDLE_CustomType18( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
781 INT type
, const WCHAR
*action
)
787 if (!(file
= msi_get_loaded_file( package
, source
))) return ERROR_FUNCTION_FAILED
;
789 deformat_string( package
, target
, &arg
);
790 TRACE("exe %s arg %s\n", debugstr_w(file
->TargetPath
), debugstr_w(arg
));
792 handle
= execute_command( file
->TargetPath
, arg
, szCRoot
);
794 if (handle
== INVALID_HANDLE_VALUE
) return ERROR_SUCCESS
;
795 return wait_process_handle( package
, type
, handle
, action
);
798 static UINT
HANDLE_CustomType19( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
799 INT type
, const WCHAR
*action
)
801 static const WCHAR query
[] = {
802 'S','E','L','E','C','T',' ','`','M','e','s','s','a','g','e','`',' ',
803 'F','R','O','M',' ','`','E','r','r','o','r','`',' ',
804 'W','H','E','R','E',' ','`','E','r','r','o','r','`',' ','=',' ',
808 LPWSTR deformated
= NULL
;
810 deformat_string( package
, target
, &deformated
);
812 /* first try treat the error as a number */
813 row
= MSI_QueryGetRecord( package
->db
, query
, deformated
);
816 LPCWSTR error
= MSI_RecordGetString( row
, 1 );
817 if ((package
->ui_level
& INSTALLUILEVEL_MASK
) != INSTALLUILEVEL_NONE
)
818 MessageBoxW( NULL
, error
, NULL
, MB_OK
);
819 msiobj_release( &row
->hdr
);
821 else if ((package
->ui_level
& INSTALLUILEVEL_MASK
) != INSTALLUILEVEL_NONE
)
822 MessageBoxW( NULL
, deformated
, NULL
, MB_OK
);
824 msi_free( deformated
);
826 return ERROR_INSTALL_FAILURE
;
829 static UINT
HANDLE_CustomType23( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
830 INT type
, const WCHAR
*action
)
832 static const WCHAR msiexecW
[] = {'m','s','i','e','x','e','c',0};
833 static const WCHAR paramsW
[] = {'/','q','b',' ','/','i',' '};
834 WCHAR
*dir
, *arg
, *p
;
835 UINT len_src
, len_dir
, len_tgt
, len
= sizeof(paramsW
)/sizeof(paramsW
[0]);
838 if (!(dir
= msi_dup_property( package
->db
, szOriginalDatabase
))) return ERROR_OUTOFMEMORY
;
839 if (!(p
= strrchrW( dir
, '\\' )) && !(p
= strrchrW( dir
, '/' )))
842 return ERROR_FUNCTION_FAILED
;
846 len_src
= strlenW( source
);
847 len_tgt
= strlenW( target
);
848 if (!(arg
= msi_alloc( (len
+ len_dir
+ len_src
+ len_tgt
+ 5) * sizeof(WCHAR
) )))
851 return ERROR_OUTOFMEMORY
;
853 memcpy( arg
, paramsW
, sizeof(paramsW
) );
855 memcpy( arg
+ len
, dir
, len_dir
* sizeof(WCHAR
) );
858 memcpy( arg
+ len
, source
, len_src
* sizeof(WCHAR
) );
862 strcpyW( arg
+ len
, target
);
864 TRACE("installing %s concurrently\n", debugstr_w(source
));
866 handle
= execute_command( msiexecW
, arg
, dir
);
869 if (handle
== INVALID_HANDLE_VALUE
) return ERROR_SUCCESS
;
870 return wait_process_handle( package
, type
, handle
, action
);
873 static UINT
HANDLE_CustomType50( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
874 INT type
, const WCHAR
*action
)
879 if (!(exe
= msi_dup_property( package
->db
, source
))) return ERROR_SUCCESS
;
881 deformat_string( package
, target
, &arg
);
882 TRACE("exe %s arg %s\n", debugstr_w(exe
), debugstr_w(arg
));
884 handle
= execute_command( exe
, arg
, szCRoot
);
887 if (handle
== INVALID_HANDLE_VALUE
) return ERROR_SUCCESS
;
888 return wait_process_handle( package
, type
, handle
, action
);
891 static UINT
HANDLE_CustomType34( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
892 INT type
, const WCHAR
*action
)
894 const WCHAR
*workingdir
= NULL
;
900 workingdir
= msi_get_target_folder( package
, source
);
901 if (!workingdir
) return ERROR_FUNCTION_FAILED
;
903 deformat_string( package
, target
, &cmd
);
904 if (!cmd
) return ERROR_FUNCTION_FAILED
;
906 TRACE("cmd %s dir %s\n", debugstr_w(cmd
), debugstr_w(workingdir
));
908 handle
= execute_command( NULL
, cmd
, workingdir
);
910 if (handle
== INVALID_HANDLE_VALUE
) return ERROR_SUCCESS
;
911 return wait_process_handle( package
, type
, handle
, action
);
914 static DWORD
ACTION_CallScript( const GUID
*guid
)
916 msi_custom_action_info
*info
;
918 UINT r
= ERROR_FUNCTION_FAILED
;
920 info
= find_action_by_guid( guid
);
923 ERR("failed to find action %s\n", debugstr_guid( guid
) );
924 return ERROR_FUNCTION_FAILED
;
927 TRACE("function %s, script %s\n", debugstr_w( info
->target
), debugstr_w( info
->source
) );
929 hPackage
= alloc_msihandle( &info
->package
->hdr
);
932 r
= call_script( hPackage
, info
->type
, info
->source
, info
->target
, info
->action
);
933 TRACE("script returned %u\n", r
);
934 MsiCloseHandle( hPackage
);
937 ERR("failed to create handle for %p\n", info
->package
);
939 release_custom_action_data( info
);
943 static DWORD WINAPI
ScriptThread( LPVOID arg
)
948 TRACE("custom action (%x) started\n", GetCurrentThreadId() );
950 rc
= ACTION_CallScript( guid
);
952 TRACE("custom action (%x) returned %i\n", GetCurrentThreadId(), rc
);
954 MsiCloseAllHandles();
958 static msi_custom_action_info
*do_msidbCustomActionTypeScript(
959 MSIPACKAGE
*package
, INT type
, LPCWSTR script
, LPCWSTR function
, LPCWSTR action
)
961 msi_custom_action_info
*info
;
963 info
= msi_alloc( sizeof *info
);
967 msiobj_addref( &package
->hdr
);
968 info
->refs
= 2; /* 1 for our caller and 1 for thread we created */
969 info
->package
= package
;
971 info
->target
= strdupW( function
);
972 info
->source
= strdupW( script
);
973 info
->action
= strdupW( action
);
974 CoCreateGuid( &info
->guid
);
976 EnterCriticalSection( &msi_custom_action_cs
);
977 list_add_tail( &msi_pending_custom_actions
, &info
->entry
);
978 LeaveCriticalSection( &msi_custom_action_cs
);
980 info
->handle
= CreateThread( NULL
, 0, ScriptThread
, &info
->guid
, 0, NULL
);
983 /* release both references */
984 release_custom_action_data( info
);
985 release_custom_action_data( info
);
992 static UINT
HANDLE_CustomType37_38( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
993 INT type
, const WCHAR
*action
)
995 msi_custom_action_info
*info
;
997 TRACE("%s %s\n", debugstr_w(source
), debugstr_w(target
));
999 info
= do_msidbCustomActionTypeScript( package
, type
, target
, NULL
, action
);
1000 return wait_thread_handle( info
);
1003 static UINT
HANDLE_CustomType5_6( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
1004 INT type
, const WCHAR
*action
)
1006 static const WCHAR query
[] = {
1007 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1008 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',' ',
1009 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
1010 MSIRECORD
*row
= NULL
;
1011 msi_custom_action_info
*info
;
1012 CHAR
*buffer
= NULL
;
1013 WCHAR
*bufferw
= NULL
;
1017 TRACE("%s %s\n", debugstr_w(source
), debugstr_w(target
));
1019 row
= MSI_QueryGetRecord(package
->db
, query
, source
);
1021 return ERROR_FUNCTION_FAILED
;
1023 r
= MSI_RecordReadStream(row
, 2, NULL
, &sz
);
1024 if (r
!= ERROR_SUCCESS
) goto done
;
1026 buffer
= msi_alloc( sz
+ 1 );
1029 r
= ERROR_FUNCTION_FAILED
;
1033 r
= MSI_RecordReadStream(row
, 2, buffer
, &sz
);
1034 if (r
!= ERROR_SUCCESS
)
1038 bufferw
= strdupAtoW(buffer
);
1041 r
= ERROR_FUNCTION_FAILED
;
1045 info
= do_msidbCustomActionTypeScript( package
, type
, bufferw
, target
, action
);
1046 r
= wait_thread_handle( info
);
1051 msiobj_release(&row
->hdr
);
1055 static UINT
HANDLE_CustomType21_22( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
1056 INT type
, const WCHAR
*action
)
1058 msi_custom_action_info
*info
;
1061 DWORD sz
, szHighWord
= 0, read
;
1063 WCHAR
*bufferw
=NULL
;
1067 TRACE("%s %s\n", debugstr_w(source
), debugstr_w(target
));
1069 file
= msi_get_loaded_file(package
, source
);
1072 ERR("invalid file key %s\n", debugstr_w(source
));
1073 return ERROR_FUNCTION_FAILED
;
1076 hFile
= CreateFileW(file
->TargetPath
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, NULL
);
1077 if (hFile
== INVALID_HANDLE_VALUE
) return ERROR_FUNCTION_FAILED
;
1079 sz
= GetFileSize(hFile
, &szHighWord
);
1080 if (sz
== INVALID_FILE_SIZE
|| szHighWord
!= 0)
1083 return ERROR_FUNCTION_FAILED
;
1085 buffer
= msi_alloc( sz
+ 1 );
1089 return ERROR_FUNCTION_FAILED
;
1091 bRet
= ReadFile(hFile
, buffer
, sz
, &read
, NULL
);
1095 r
= ERROR_FUNCTION_FAILED
;
1099 bufferw
= strdupAtoW(buffer
);
1102 r
= ERROR_FUNCTION_FAILED
;
1105 info
= do_msidbCustomActionTypeScript( package
, type
, bufferw
, target
, action
);
1106 r
= wait_thread_handle( info
);
1114 static UINT
HANDLE_CustomType53_54( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
1115 INT type
, const WCHAR
*action
)
1117 msi_custom_action_info
*info
;
1120 TRACE("%s %s\n", debugstr_w(source
), debugstr_w(target
));
1122 prop
= msi_dup_property( package
->db
, source
);
1123 if (!prop
) return ERROR_SUCCESS
;
1125 info
= do_msidbCustomActionTypeScript( package
, type
, prop
, NULL
, action
);
1127 return wait_thread_handle( info
);
1130 static BOOL
action_type_matches_script( UINT type
, UINT script
)
1135 case SCRIPT_INSTALL
:
1136 return !(type
& msidbCustomActionTypeCommit
) && !(type
& msidbCustomActionTypeRollback
);
1138 return (type
& msidbCustomActionTypeCommit
);
1139 case SCRIPT_ROLLBACK
:
1140 return (type
& msidbCustomActionTypeRollback
);
1142 ERR("unhandled script %u\n", script
);
1147 static UINT
defer_custom_action( MSIPACKAGE
*package
, const WCHAR
*action
, UINT type
)
1149 WCHAR
*actiondata
= msi_dup_property( package
->db
, action
);
1150 WCHAR
*usersid
= msi_dup_property( package
->db
, szUserSID
);
1151 WCHAR
*prodcode
= msi_dup_property( package
->db
, szProductCode
);
1152 WCHAR
*deferred
= msi_get_deferred_action( action
, actiondata
, usersid
, prodcode
);
1156 msi_free( actiondata
);
1157 msi_free( usersid
);
1158 msi_free( prodcode
);
1159 return ERROR_OUTOFMEMORY
;
1161 if (type
& msidbCustomActionTypeCommit
)
1163 TRACE("deferring commit action\n");
1164 msi_schedule_action( package
, SCRIPT_COMMIT
, deferred
);
1166 else if (type
& msidbCustomActionTypeRollback
)
1168 TRACE("deferring rollback action\n");
1169 msi_schedule_action( package
, SCRIPT_ROLLBACK
, deferred
);
1173 TRACE("deferring install action\n");
1174 msi_schedule_action( package
, SCRIPT_INSTALL
, deferred
);
1177 msi_free( actiondata
);
1178 msi_free( usersid
);
1179 msi_free( prodcode
);
1180 msi_free( deferred
);
1181 return ERROR_SUCCESS
;
1184 UINT
ACTION_CustomAction( MSIPACKAGE
*package
, LPCWSTR action
, UINT script
)
1186 static const WCHAR query
[] = {
1187 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1188 '`','C','u','s','t','o','m','A','c','t','i','o','n','`',' ','W','H','E','R','E',' ',
1189 '`','A','c','t','i' ,'o','n','`',' ','=',' ','\'','%','s','\'',0};
1190 UINT rc
= ERROR_SUCCESS
;
1193 const WCHAR
*source
, *target
, *ptr
, *deferred_data
= NULL
;
1194 WCHAR
*deformated
= NULL
;
1197 /* deferred action: [properties]Action */
1198 if ((ptr
= strrchrW(action
, ']')))
1200 deferred_data
= action
;
1204 row
= MSI_QueryGetRecord( package
->db
, query
, action
);
1206 return ERROR_CALL_NOT_IMPLEMENTED
;
1208 type
= MSI_RecordGetInteger(row
,2);
1209 source
= MSI_RecordGetString(row
,3);
1210 target
= MSI_RecordGetString(row
,4);
1212 TRACE("Handling custom action %s (%x %s %s)\n",debugstr_w(action
),type
,
1213 debugstr_w(source
), debugstr_w(target
));
1215 /* handle some of the deferred actions */
1216 if (type
& msidbCustomActionTypeTSAware
)
1217 FIXME("msidbCustomActionTypeTSAware not handled\n");
1219 if (type
& msidbCustomActionTypeInScript
)
1221 if (type
& msidbCustomActionTypeNoImpersonate
)
1222 WARN("msidbCustomActionTypeNoImpersonate not handled\n");
1224 if (!action_type_matches_script( type
, script
))
1226 rc
= defer_custom_action( package
, action
, type
);
1231 LPWSTR actiondata
= msi_dup_property( package
->db
, action
);
1233 if (type
& msidbCustomActionTypeInScript
)
1234 package
->scheduled_action_running
= TRUE
;
1236 if (type
& msidbCustomActionTypeCommit
)
1237 package
->commit_action_running
= TRUE
;
1239 if (type
& msidbCustomActionTypeRollback
)
1240 package
->rollback_action_running
= TRUE
;
1243 set_deferred_action_props(package
, deferred_data
);
1244 else if (actiondata
)
1245 msi_set_property( package
->db
, szCustomActionData
, actiondata
, -1 );
1247 msi_set_property( package
->db
, szCustomActionData
, szEmpty
, -1 );
1249 msi_free(actiondata
);
1252 else if (!check_execution_scheduling_options(package
,action
,type
))
1258 switch (type
& CUSTOM_ACTION_TYPE_MASK
)
1260 case 1: /* DLL file stored in a Binary table stream */
1261 rc
= HANDLE_CustomType1(package
,source
,target
,type
,action
);
1263 case 2: /* EXE file stored in a Binary table stream */
1264 rc
= HANDLE_CustomType2(package
,source
,target
,type
,action
);
1266 case 18: /*EXE file installed with package */
1267 rc
= HANDLE_CustomType18(package
,source
,target
,type
,action
);
1269 case 19: /* Error that halts install */
1270 rc
= HANDLE_CustomType19(package
,source
,target
,type
,action
);
1273 rc
= HANDLE_CustomType17(package
,source
,target
,type
,action
);
1275 case 23: /* installs another package in the source tree */
1276 deformat_string(package
,target
,&deformated
);
1277 rc
= HANDLE_CustomType23(package
,source
,deformated
,type
,action
);
1278 msi_free(deformated
);
1280 case 50: /*EXE file specified by a property value */
1281 rc
= HANDLE_CustomType50(package
,source
,target
,type
,action
);
1283 case 34: /*EXE to be run in specified directory */
1284 rc
= HANDLE_CustomType34(package
,source
,target
,type
,action
);
1286 case 35: /* Directory set with formatted text. */
1287 deformat_string(package
,target
,&deformated
);
1288 MSI_SetTargetPathW(package
, source
, deformated
);
1289 msi_free(deformated
);
1291 case 51: /* Property set with formatted text. */
1295 len
= deformat_string( package
, target
, &deformated
);
1296 rc
= msi_set_property( package
->db
, source
, deformated
, len
);
1297 if (rc
== ERROR_SUCCESS
&& !strcmpW( source
, szSourceDir
))
1298 msi_reset_folders( package
, TRUE
);
1299 msi_free(deformated
);
1301 case 37: /* JScript/VBScript text stored in target column. */
1303 rc
= HANDLE_CustomType37_38(package
,source
,target
,type
,action
);
1306 case 6: /* JScript/VBScript file stored in a Binary table stream. */
1307 rc
= HANDLE_CustomType5_6(package
,source
,target
,type
,action
);
1309 case 21: /* JScript/VBScript file installed with the product. */
1311 rc
= HANDLE_CustomType21_22(package
,source
,target
,type
,action
);
1313 case 53: /* JScript/VBScript text specified by a property value. */
1315 rc
= HANDLE_CustomType53_54(package
,source
,target
,type
,action
);
1318 FIXME("unhandled action type %u (%s %s)\n", type
& CUSTOM_ACTION_TYPE_MASK
,
1319 debugstr_w(source
), debugstr_w(target
));
1323 package
->scheduled_action_running
= FALSE
;
1324 package
->commit_action_running
= FALSE
;
1325 package
->rollback_action_running
= FALSE
;
1326 msiobj_release(&row
->hdr
);
1330 void ACTION_FinishCustomActions(const MSIPACKAGE
* package
)
1333 HANDLE
*wait_handles
;
1334 unsigned int handle_count
, i
;
1335 msi_custom_action_info
*info
, *cursor
;
1337 while ((item
= list_head( &package
->RunningActions
)))
1339 MSIRUNNINGACTION
*action
= LIST_ENTRY( item
, MSIRUNNINGACTION
, entry
);
1341 list_remove( &action
->entry
);
1343 TRACE("waiting for %s\n", debugstr_w( action
->name
) );
1344 msi_dialog_check_messages( action
->handle
);
1346 CloseHandle( action
->handle
);
1347 msi_free( action
->name
);
1351 EnterCriticalSection( &msi_custom_action_cs
);
1353 handle_count
= list_count( &msi_pending_custom_actions
);
1354 wait_handles
= msi_alloc( handle_count
* sizeof(HANDLE
) );
1357 LIST_FOR_EACH_ENTRY_SAFE( info
, cursor
, &msi_pending_custom_actions
, msi_custom_action_info
, entry
)
1359 if (info
->package
== package
)
1361 if (DuplicateHandle(GetCurrentProcess(), info
->handle
, GetCurrentProcess(), &wait_handles
[handle_count
], SYNCHRONIZE
, FALSE
, 0))
1366 LeaveCriticalSection( &msi_custom_action_cs
);
1368 for (i
= 0; i
< handle_count
; i
++)
1370 msi_dialog_check_messages( wait_handles
[i
] );
1371 CloseHandle( wait_handles
[i
] );
1373 msi_free( wait_handles
);
1375 EnterCriticalSection( &msi_custom_action_cs
);
1376 LIST_FOR_EACH_ENTRY_SAFE( info
, cursor
, &msi_pending_custom_actions
, msi_custom_action_info
, entry
)
1378 if (info
->package
== package
) release_custom_action_data( info
);
1380 LeaveCriticalSection( &msi_custom_action_cs
);
1383 typedef struct _msi_custom_remote_impl
{
1384 IWineMsiRemoteCustomAction IWineMsiRemoteCustomAction_iface
;
1386 } msi_custom_remote_impl
;
1388 static inline msi_custom_remote_impl
*impl_from_IWineMsiRemoteCustomAction( IWineMsiRemoteCustomAction
*iface
)
1390 return CONTAINING_RECORD(iface
, msi_custom_remote_impl
, IWineMsiRemoteCustomAction_iface
);
1393 static HRESULT WINAPI
mcr_QueryInterface( IWineMsiRemoteCustomAction
*iface
,
1394 REFIID riid
,LPVOID
*ppobj
)
1396 if( IsEqualCLSID( riid
, &IID_IUnknown
) ||
1397 IsEqualCLSID( riid
, &IID_IWineMsiRemoteCustomAction
) )
1399 IWineMsiRemoteCustomAction_AddRef( iface
);
1404 return E_NOINTERFACE
;
1407 static ULONG WINAPI
mcr_AddRef( IWineMsiRemoteCustomAction
*iface
)
1409 msi_custom_remote_impl
* This
= impl_from_IWineMsiRemoteCustomAction( iface
);
1411 return InterlockedIncrement( &This
->refs
);
1414 static ULONG WINAPI
mcr_Release( IWineMsiRemoteCustomAction
*iface
)
1416 msi_custom_remote_impl
* This
= impl_from_IWineMsiRemoteCustomAction( iface
);
1419 r
= InterlockedDecrement( &This
->refs
);
1425 static HRESULT WINAPI
mcr_GetActionInfo( IWineMsiRemoteCustomAction
*iface
, LPCGUID custom_action_guid
,
1426 INT
*type
, MSIHANDLE
*handle
, BSTR
*dll
, BSTR
*func
, IWineMsiRemotePackage
**remote_package
)
1428 msi_custom_action_info
*info
;
1430 info
= find_action_by_guid( custom_action_guid
);
1435 *handle
= alloc_msihandle( &info
->package
->hdr
);
1436 *dll
= SysAllocString( info
->source
);
1437 *func
= SysAllocString( info
->target
);
1439 release_custom_action_data( info
);
1440 return create_msi_remote_package( NULL
, (LPVOID
*)remote_package
);
1443 static const IWineMsiRemoteCustomActionVtbl msi_custom_remote_vtbl
=
1451 HRESULT
create_msi_custom_remote( IUnknown
*pOuter
, LPVOID
*ppObj
)
1453 msi_custom_remote_impl
* This
;
1455 This
= msi_alloc( sizeof *This
);
1457 return E_OUTOFMEMORY
;
1459 This
->IWineMsiRemoteCustomAction_iface
.lpVtbl
= &msi_custom_remote_vtbl
;
1462 *ppObj
= &This
->IWineMsiRemoteCustomAction_iface
;