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
22 #include "wine/port.h"
36 #include "msiserver.h"
37 #include "wine/debug.h"
38 #include "wine/unicode.h"
39 #include "wine/exception.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(msi
);
43 #define CUSTOM_ACTION_TYPE_MASK 0x3F
45 typedef struct tagMSIRUNNINGACTION
53 typedef UINT (WINAPI
*MsiCustomActionEntryPoint
)( MSIHANDLE
);
55 static CRITICAL_SECTION msi_custom_action_cs
;
56 static CRITICAL_SECTION_DEBUG msi_custom_action_cs_debug
=
58 0, 0, &msi_custom_action_cs
,
59 { &msi_custom_action_cs_debug
.ProcessLocksList
,
60 &msi_custom_action_cs_debug
.ProcessLocksList
},
61 0, 0, { (DWORD_PTR
)(__FILE__
": msi_custom_action_cs") }
63 static CRITICAL_SECTION msi_custom_action_cs
= { &msi_custom_action_cs_debug
, -1, 0, 0, 0, 0 };
65 static struct list msi_pending_custom_actions
= LIST_INIT( msi_pending_custom_actions
);
67 UINT
msi_schedule_action( MSIPACKAGE
*package
, UINT script
, const WCHAR
*action
)
70 WCHAR
**newbuf
= NULL
;
72 if (script
>= SCRIPT_MAX
)
74 FIXME("Unknown script requested %u\n", script
);
75 return ERROR_FUNCTION_FAILED
;
77 TRACE("Scheduling action %s in script %u\n", debugstr_w(action
), script
);
79 count
= package
->script
->ActionCount
[script
];
80 package
->script
->ActionCount
[script
]++;
81 if (count
!= 0) newbuf
= msi_realloc( package
->script
->Actions
[script
],
82 package
->script
->ActionCount
[script
] * sizeof(WCHAR
*) );
83 else newbuf
= msi_alloc( sizeof(WCHAR
*) );
85 newbuf
[count
] = strdupW( action
);
86 package
->script
->Actions
[script
] = newbuf
;
90 UINT
msi_register_unique_action( MSIPACKAGE
*package
, const WCHAR
*action
)
93 WCHAR
**newbuf
= NULL
;
95 if (!package
->script
) return FALSE
;
97 TRACE("Registering %s as unique action\n", debugstr_w(action
));
99 count
= package
->script
->UniqueActionsCount
;
100 package
->script
->UniqueActionsCount
++;
101 if (count
!= 0) newbuf
= msi_realloc( package
->script
->UniqueActions
,
102 package
->script
->UniqueActionsCount
* sizeof(WCHAR
*) );
103 else newbuf
= msi_alloc( sizeof(WCHAR
*) );
105 newbuf
[count
] = strdupW( action
);
106 package
->script
->UniqueActions
= newbuf
;
107 return ERROR_SUCCESS
;
110 BOOL
msi_action_is_unique( const MSIPACKAGE
*package
, const WCHAR
*action
)
114 if (!package
->script
) return FALSE
;
116 for (i
= 0; i
< package
->script
->UniqueActionsCount
; i
++)
118 if (!strcmpW( package
->script
->UniqueActions
[i
], action
)) return TRUE
;
123 static BOOL
check_execution_scheduling_options(MSIPACKAGE
*package
, LPCWSTR action
, UINT options
)
125 if (!package
->script
)
128 if ((options
& msidbCustomActionTypeClientRepeat
) ==
129 msidbCustomActionTypeClientRepeat
)
131 if (!(package
->script
->InWhatSequence
& SEQUENCE_UI
&&
132 package
->script
->InWhatSequence
& SEQUENCE_EXEC
))
134 TRACE("Skipping action due to dbCustomActionTypeClientRepeat option.\n");
138 else if (options
& msidbCustomActionTypeFirstSequence
)
140 if (package
->script
->InWhatSequence
& SEQUENCE_UI
&&
141 package
->script
->InWhatSequence
& SEQUENCE_EXEC
)
143 TRACE("Skipping action due to msidbCustomActionTypeFirstSequence option.\n");
147 else if (options
& msidbCustomActionTypeOncePerProcess
)
149 if (msi_action_is_unique(package
, action
))
151 TRACE("Skipping action due to msidbCustomActionTypeOncePerProcess option.\n");
155 msi_register_unique_action(package
, action
);
161 /* stores the following properties before the action:
163 * [CustomActionData<=>UserSID<=>ProductCode]Action
165 static LPWSTR
msi_get_deferred_action(LPCWSTR action
, LPCWSTR actiondata
,
166 LPCWSTR usersid
, LPCWSTR prodcode
)
171 static const WCHAR format
[] = {
172 '[','%','s','<','=','>','%','s','<','=','>','%','s',']','%','s',0
176 return strdupW(action
);
178 len
= lstrlenW(action
) + lstrlenW(actiondata
) +
179 lstrlenW(usersid
) + lstrlenW(prodcode
) +
180 lstrlenW(format
) - 7;
181 deferred
= msi_alloc(len
* sizeof(WCHAR
));
183 sprintfW(deferred
, format
, actiondata
, usersid
, prodcode
, action
);
187 static void set_deferred_action_props( MSIPACKAGE
*package
, const WCHAR
*deferred_data
)
189 static const WCHAR sep
[] = {'<','=','>',0};
190 const WCHAR
*end
, *beg
= deferred_data
+ 1;
192 end
= strstrW(beg
, sep
);
193 msi_set_property( package
->db
, szCustomActionData
, beg
, end
- beg
);
196 end
= strstrW(beg
, sep
);
197 msi_set_property( package
->db
, szUserSID
, beg
, end
- beg
);
200 end
= strchrW(beg
, ']');
201 msi_set_property( package
->db
, szProductCode
, beg
, end
- beg
);
204 WCHAR
*msi_create_temp_file( MSIDATABASE
*db
)
211 UINT len
= sizeof(tmp
)/sizeof(tmp
[0]);
213 if (msi_get_property( db
, szTempFolder
, tmp
, &len
) ||
214 GetFileAttributesW( tmp
) != FILE_ATTRIBUTE_DIRECTORY
)
216 GetTempPathW( MAX_PATH
, tmp
);
218 if (!(db
->tempfolder
= strdupW( tmp
))) return NULL
;
221 if ((ret
= msi_alloc( (strlenW( db
->tempfolder
) + 20) * sizeof(WCHAR
) )))
223 if (!GetTempFileNameW( db
->tempfolder
, szMsi
, 0, ret
))
233 static MSIBINARY
*create_temp_binary( MSIPACKAGE
*package
, LPCWSTR source
, BOOL dll
)
235 static const WCHAR query
[] = {
236 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
237 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',' ',
238 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
240 MSIBINARY
*binary
= NULL
;
247 if (!(tmpfile
= msi_create_temp_file( package
->db
))) return NULL
;
249 if (!(row
= MSI_QueryGetRecord( package
->db
, query
, source
))) goto error
;
250 if (!(binary
= msi_alloc_zero( sizeof(MSIBINARY
) ))) goto error
;
252 file
= CreateFileW( tmpfile
, GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
253 if (file
== INVALID_HANDLE_VALUE
) goto error
;
258 r
= MSI_RecordReadStream( row
, 2, buffer
, &sz
);
259 if (r
!= ERROR_SUCCESS
)
261 ERR("Failed to get stream\n");
264 WriteFile( file
, buffer
, sz
, &write
, NULL
);
265 } while (sz
== sizeof buffer
);
268 if (r
!= ERROR_SUCCESS
) goto error
;
270 /* keep a reference to prevent the dll from being unloaded */
271 if (dll
&& !(binary
->module
= LoadLibraryW( tmpfile
)))
273 WARN( "failed to load dll %s (%u)\n", debugstr_w( tmpfile
), GetLastError() );
275 binary
->source
= strdupW( source
);
276 binary
->tmpfile
= tmpfile
;
277 list_add_tail( &package
->binaries
, &binary
->entry
);
279 msiobj_release( &row
->hdr
);
283 if (row
) msiobj_release( &row
->hdr
);
284 DeleteFileW( tmpfile
);
290 static MSIBINARY
*get_temp_binary( MSIPACKAGE
*package
, LPCWSTR source
, BOOL dll
)
294 LIST_FOR_EACH_ENTRY( binary
, &package
->binaries
, MSIBINARY
, entry
)
296 if (!strcmpW( binary
->source
, source
))
300 return create_temp_binary( package
, source
, dll
);
303 static void file_running_action(MSIPACKAGE
* package
, HANDLE Handle
,
304 BOOL process
, LPCWSTR name
)
306 MSIRUNNINGACTION
*action
;
308 action
= msi_alloc( sizeof(MSIRUNNINGACTION
) );
310 action
->handle
= Handle
;
311 action
->process
= process
;
312 action
->name
= strdupW(name
);
314 list_add_tail( &package
->RunningActions
, &action
->entry
);
317 static UINT
custom_get_process_return( HANDLE process
)
321 GetExitCodeProcess( process
, &rc
);
322 TRACE("exit code is %u\n", rc
);
324 return ERROR_FUNCTION_FAILED
;
325 return ERROR_SUCCESS
;
328 static UINT
custom_get_thread_return( MSIPACKAGE
*package
, HANDLE thread
)
332 GetExitCodeThread( thread
, &rc
);
336 case ERROR_FUNCTION_NOT_CALLED
:
338 case ERROR_INSTALL_USEREXIT
:
339 case ERROR_INSTALL_FAILURE
:
341 case ERROR_NO_MORE_ITEMS
:
342 return ERROR_SUCCESS
;
343 case ERROR_INSTALL_SUSPEND
:
344 ACTION_ForceReboot( package
);
345 return ERROR_SUCCESS
;
347 ERR("Invalid Return Code %d\n",rc
);
348 return ERROR_INSTALL_FAILURE
;
352 static UINT
wait_process_handle(MSIPACKAGE
* package
, UINT type
,
353 HANDLE ProcessHandle
, LPCWSTR name
)
355 UINT rc
= ERROR_SUCCESS
;
357 if (!(type
& msidbCustomActionTypeAsync
))
359 TRACE("waiting for %s\n", debugstr_w(name
));
361 msi_dialog_check_messages(ProcessHandle
);
363 if (!(type
& msidbCustomActionTypeContinue
))
364 rc
= custom_get_process_return(ProcessHandle
);
366 CloseHandle(ProcessHandle
);
370 TRACE("%s running in background\n", debugstr_w(name
));
372 if (!(type
& msidbCustomActionTypeContinue
))
373 file_running_action(package
, ProcessHandle
, TRUE
, name
);
375 CloseHandle(ProcessHandle
);
381 typedef struct _msi_custom_action_info
{
391 } msi_custom_action_info
;
393 static void release_custom_action_data( msi_custom_action_info
*info
)
395 EnterCriticalSection( &msi_custom_action_cs
);
399 list_remove( &info
->entry
);
401 CloseHandle( info
->handle
);
402 msi_free( info
->action
);
403 msi_free( info
->source
);
404 msi_free( info
->target
);
405 msiobj_release( &info
->package
->hdr
);
409 LeaveCriticalSection( &msi_custom_action_cs
);
412 /* must be called inside msi_custom_action_cs if info is in the pending custom actions list */
413 static void addref_custom_action_data( msi_custom_action_info
*info
)
418 static UINT
wait_thread_handle( msi_custom_action_info
*info
)
420 UINT rc
= ERROR_SUCCESS
;
422 if (!(info
->type
& msidbCustomActionTypeAsync
))
424 TRACE("waiting for %s\n", debugstr_w( info
->action
));
426 msi_dialog_check_messages( info
->handle
);
428 if (!(info
->type
& msidbCustomActionTypeContinue
))
429 rc
= custom_get_thread_return( info
->package
, info
->handle
);
431 release_custom_action_data( info
);
435 TRACE("%s running in background\n", debugstr_w( info
->action
));
441 static msi_custom_action_info
*find_action_by_guid( const GUID
*guid
)
443 msi_custom_action_info
*info
;
446 EnterCriticalSection( &msi_custom_action_cs
);
448 LIST_FOR_EACH_ENTRY( info
, &msi_pending_custom_actions
, msi_custom_action_info
, entry
)
450 if (IsEqualGUID( &info
->guid
, guid
))
452 addref_custom_action_data( info
);
458 LeaveCriticalSection( &msi_custom_action_cs
);
466 static void handle_msi_break( LPCWSTR target
)
471 static const WCHAR MsiBreak
[] = { 'M','s','i','B','r','e','a','k',0 };
472 static const WCHAR WindowsInstaller
[] = {
473 'W','i','n','d','o','w','s',' ','I','n','s','t','a','l','l','e','r',0
476 static const WCHAR format
[] = {
477 'T','o',' ','d','e','b','u','g',' ','y','o','u','r',' ',
478 'c','u','s','t','o','m',' ','a','c','t','i','o','n',',',' ',
479 'a','t','t','a','c','h',' ','y','o','u','r',' ','d','e','b','u','g','g','e','r',' ',
480 't','o',' ','p','r','o','c','e','s','s',' ','%','i',' ','(','0','x','%','X',')',' ',
481 'a','n','d',' ','p','r','e','s','s',' ','O','K',0
484 if( !GetEnvironmentVariableW( MsiBreak
, val
, MAX_PATH
))
487 if( strcmpiW( val
, target
))
490 msg
= msi_alloc( (lstrlenW(format
) + 10) * sizeof(WCHAR
) );
494 wsprintfW( msg
, format
, GetCurrentProcessId(), GetCurrentProcessId());
495 MessageBoxW( NULL
, msg
, WindowsInstaller
, MB_OK
);
500 static UINT
get_action_info( const GUID
*guid
, INT
*type
, MSIHANDLE
*handle
,
501 BSTR
*dll
, BSTR
*funcname
,
502 IWineMsiRemotePackage
**package
)
504 IClassFactory
*cf
= NULL
;
505 IWineMsiRemoteCustomAction
*rca
= NULL
;
508 r
= DllGetClassObject( &CLSID_WineMsiRemoteCustomAction
,
509 &IID_IClassFactory
, (LPVOID
*)&cf
);
512 ERR("failed to get IClassFactory interface\n");
513 return ERROR_FUNCTION_FAILED
;
516 r
= IClassFactory_CreateInstance( cf
, NULL
, &IID_IWineMsiRemoteCustomAction
, (LPVOID
*)&rca
);
519 ERR("failed to get IWineMsiRemoteCustomAction interface\n");
520 return ERROR_FUNCTION_FAILED
;
523 r
= IWineMsiRemoteCustomAction_GetActionInfo( rca
, guid
, type
, handle
, dll
, funcname
, package
);
524 IWineMsiRemoteCustomAction_Release( rca
);
527 ERR("GetActionInfo failed\n");
528 return ERROR_FUNCTION_FAILED
;
531 return ERROR_SUCCESS
;
535 extern UINT
CUSTOMPROC_wrapper( MsiCustomActionEntryPoint proc
, MSIHANDLE handle
);
536 __ASM_GLOBAL_FUNC( CUSTOMPROC_wrapper
,
538 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
539 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
541 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
543 "movl 8(%ebp),%eax\n\t"
546 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
547 __ASM_CFI(".cfi_same_value %ebp\n\t")
550 static inline UINT
CUSTOMPROC_wrapper( MsiCustomActionEntryPoint proc
, MSIHANDLE handle
)
556 static DWORD
ACTION_CallDllFunction( const GUID
*guid
)
558 MsiCustomActionEntryPoint fn
;
559 MSIHANDLE hPackage
, handle
;
562 UINT r
= ERROR_FUNCTION_FAILED
;
563 BSTR dll
= NULL
, function
= NULL
;
565 IWineMsiRemotePackage
*remote_package
= NULL
;
567 TRACE("%s\n", debugstr_guid( guid
));
569 r
= get_action_info( guid
, &type
, &handle
, &dll
, &function
, &remote_package
);
570 if (r
!= ERROR_SUCCESS
)
573 hModule
= LoadLibraryW( dll
);
576 WARN( "failed to load dll %s (%u)\n", debugstr_w( dll
), GetLastError() );
577 return ERROR_SUCCESS
;
580 proc
= strdupWtoA( function
);
581 fn
= (MsiCustomActionEntryPoint
) GetProcAddress( hModule
, proc
);
585 hPackage
= alloc_msi_remote_handle( (IUnknown
*)remote_package
);
588 IWineMsiRemotePackage_SetMsiHandle( remote_package
, handle
);
589 TRACE("calling %s\n", debugstr_w( function
) );
590 handle_msi_break( function
);
594 r
= CUSTOMPROC_wrapper( fn
, hPackage
);
598 ERR("Custom action (%s:%s) caused a page fault: %08x\n",
599 debugstr_w(dll
), debugstr_w(function
), GetExceptionCode());
604 MsiCloseHandle( hPackage
);
607 ERR("failed to create handle for %p\n", remote_package
);
610 ERR("GetProcAddress(%s) failed\n", debugstr_w( function
) );
612 FreeLibrary(hModule
);
614 IWineMsiRemotePackage_Release( remote_package
);
615 SysFreeString( dll
);
616 SysFreeString( function
);
617 MsiCloseHandle( handle
);
622 static DWORD WINAPI
DllThread( LPVOID arg
)
627 TRACE("custom action (%x) started\n", GetCurrentThreadId() );
629 rc
= ACTION_CallDllFunction( guid
);
631 TRACE("custom action (%x) returned %i\n", GetCurrentThreadId(), rc
);
633 MsiCloseAllHandles();
637 static msi_custom_action_info
*do_msidbCustomActionTypeDll(
638 MSIPACKAGE
*package
, INT type
, LPCWSTR source
, LPCWSTR target
, LPCWSTR action
)
640 msi_custom_action_info
*info
;
642 info
= msi_alloc( sizeof *info
);
646 msiobj_addref( &package
->hdr
);
647 info
->refs
= 2; /* 1 for our caller and 1 for thread we created */
648 info
->package
= package
;
650 info
->target
= strdupW( target
);
651 info
->source
= strdupW( source
);
652 info
->action
= strdupW( action
);
653 CoCreateGuid( &info
->guid
);
655 EnterCriticalSection( &msi_custom_action_cs
);
656 list_add_tail( &msi_pending_custom_actions
, &info
->entry
);
657 LeaveCriticalSection( &msi_custom_action_cs
);
659 info
->handle
= CreateThread( NULL
, 0, DllThread
, &info
->guid
, 0, NULL
);
662 /* release both references */
663 release_custom_action_data( info
);
664 release_custom_action_data( info
);
671 static UINT
HANDLE_CustomType1( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
672 INT type
, const WCHAR
*action
)
674 msi_custom_action_info
*info
;
677 if (!(binary
= get_temp_binary( package
, source
, TRUE
)))
678 return ERROR_FUNCTION_FAILED
;
680 TRACE("Calling function %s from %s\n", debugstr_w(target
), debugstr_w(binary
->tmpfile
));
682 info
= do_msidbCustomActionTypeDll( package
, type
, binary
->tmpfile
, target
, action
);
683 return wait_thread_handle( info
);
686 static HANDLE
execute_command( const WCHAR
*app
, WCHAR
*arg
, const WCHAR
*dir
)
688 static const WCHAR dotexeW
[] = {'.','e','x','e',0};
690 PROCESS_INFORMATION info
;
691 WCHAR
*exe
= NULL
, *cmd
= NULL
, *p
;
699 if (!(exe
= msi_alloc( MAX_PATH
* sizeof(WCHAR
) ))) return INVALID_HANDLE_VALUE
;
700 len_exe
= SearchPathW( NULL
, app
, dotexeW
, MAX_PATH
, exe
, NULL
);
701 if (len_exe
>= MAX_PATH
)
704 if (!(exe
= msi_alloc( len_exe
* sizeof(WCHAR
) ))) return INVALID_HANDLE_VALUE
;
705 len_exe
= SearchPathW( NULL
, app
, dotexeW
, len_exe
, exe
, NULL
);
709 WARN("can't find executable %u\n", GetLastError());
711 return INVALID_HANDLE_VALUE
;
714 if (arg
) len_arg
= strlenW( arg
);
715 if (!(cmd
= msi_alloc( (len_exe
+ len_arg
+ 4) * sizeof(WCHAR
) )))
718 return INVALID_HANDLE_VALUE
;
721 if (strchrW( exe
, ' ' ))
724 memcpy( p
, exe
, len_exe
* sizeof(WCHAR
) );
737 memcpy( p
, arg
, len_arg
* sizeof(WCHAR
) );
741 memset( &si
, 0, sizeof(STARTUPINFOW
) );
742 ret
= CreateProcessW( exe
, exe
? cmd
: arg
, NULL
, NULL
, FALSE
, 0, NULL
, dir
, &si
, &info
);
747 WARN("unable to execute command %u\n", GetLastError());
748 return INVALID_HANDLE_VALUE
;
750 CloseHandle( info
.hThread
);
751 return info
.hProcess
;
754 static UINT
HANDLE_CustomType2( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
755 INT type
, const WCHAR
*action
)
761 if (!(binary
= get_temp_binary( package
, source
, FALSE
))) return ERROR_FUNCTION_FAILED
;
763 deformat_string( package
, target
, &arg
);
764 TRACE("exe %s arg %s\n", debugstr_w(binary
->tmpfile
), debugstr_w(arg
));
766 handle
= execute_command( binary
->tmpfile
, arg
, szCRoot
);
768 if (handle
== INVALID_HANDLE_VALUE
) return ERROR_SUCCESS
;
769 return wait_process_handle( package
, type
, handle
, action
);
772 static UINT
HANDLE_CustomType17( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
773 INT type
, const WCHAR
*action
)
775 msi_custom_action_info
*info
;
778 TRACE("%s %s\n", debugstr_w(source
), debugstr_w(target
));
780 file
= msi_get_loaded_file( package
, source
);
783 ERR("invalid file key %s\n", debugstr_w( source
));
784 return ERROR_FUNCTION_FAILED
;
787 info
= do_msidbCustomActionTypeDll( package
, type
, file
->TargetPath
, target
, action
);
788 return wait_thread_handle( info
);
791 static UINT
HANDLE_CustomType18( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
792 INT type
, const WCHAR
*action
)
798 if (!(file
= msi_get_loaded_file( package
, source
))) return ERROR_FUNCTION_FAILED
;
800 deformat_string( package
, target
, &arg
);
801 TRACE("exe %s arg %s\n", debugstr_w(file
->TargetPath
), debugstr_w(arg
));
803 handle
= execute_command( file
->TargetPath
, arg
, szCRoot
);
805 if (handle
== INVALID_HANDLE_VALUE
) return ERROR_SUCCESS
;
806 return wait_process_handle( package
, type
, handle
, action
);
809 static UINT
HANDLE_CustomType19( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
810 INT type
, const WCHAR
*action
)
812 static const WCHAR query
[] = {
813 'S','E','L','E','C','T',' ','`','M','e','s','s','a','g','e','`',' ',
814 'F','R','O','M',' ','`','E','r','r','o','r','`',' ',
815 'W','H','E','R','E',' ','`','E','r','r','o','r','`',' ','=',' ',
819 LPWSTR deformated
= NULL
;
821 deformat_string( package
, target
, &deformated
);
823 /* first try treat the error as a number */
824 row
= MSI_QueryGetRecord( package
->db
, query
, deformated
);
827 LPCWSTR error
= MSI_RecordGetString( row
, 1 );
828 if ((package
->ui_level
& INSTALLUILEVEL_MASK
) != INSTALLUILEVEL_NONE
)
829 MessageBoxW( NULL
, error
, NULL
, MB_OK
);
830 msiobj_release( &row
->hdr
);
832 else if ((package
->ui_level
& INSTALLUILEVEL_MASK
) != INSTALLUILEVEL_NONE
)
833 MessageBoxW( NULL
, deformated
, NULL
, MB_OK
);
835 msi_free( deformated
);
837 return ERROR_INSTALL_FAILURE
;
840 static UINT
HANDLE_CustomType23( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
841 INT type
, const WCHAR
*action
)
843 static const WCHAR msiexecW
[] = {'m','s','i','e','x','e','c',0};
844 static const WCHAR paramsW
[] = {'/','q','b',' ','/','i',' '};
845 WCHAR
*dir
, *arg
, *p
;
846 UINT len_src
, len_dir
, len_tgt
, len
= sizeof(paramsW
)/sizeof(paramsW
[0]);
849 if (!(dir
= msi_dup_property( package
->db
, szOriginalDatabase
))) return ERROR_OUTOFMEMORY
;
850 if (!(p
= strrchrW( dir
, '\\' )) && !(p
= strrchrW( dir
, '/' )))
853 return ERROR_FUNCTION_FAILED
;
857 len_src
= strlenW( source
);
858 len_tgt
= strlenW( target
);
859 if (!(arg
= msi_alloc( (len
+ len_dir
+ len_src
+ len_tgt
+ 5) * sizeof(WCHAR
) )))
862 return ERROR_OUTOFMEMORY
;
864 memcpy( arg
, paramsW
, sizeof(paramsW
) );
866 memcpy( arg
+ len
, dir
, len_dir
* sizeof(WCHAR
) );
869 memcpy( arg
+ len
, source
, len_src
* sizeof(WCHAR
) );
873 strcpyW( arg
+ len
, target
);
875 TRACE("installing %s concurrently\n", debugstr_w(source
));
877 handle
= execute_command( msiexecW
, arg
, dir
);
880 if (handle
== INVALID_HANDLE_VALUE
) return ERROR_SUCCESS
;
881 return wait_process_handle( package
, type
, handle
, action
);
884 static UINT
HANDLE_CustomType50( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
885 INT type
, const WCHAR
*action
)
890 if (!(exe
= msi_dup_property( package
->db
, source
))) return ERROR_SUCCESS
;
892 deformat_string( package
, target
, &arg
);
893 TRACE("exe %s arg %s\n", debugstr_w(exe
), debugstr_w(arg
));
895 handle
= execute_command( exe
, arg
, szCRoot
);
898 if (handle
== INVALID_HANDLE_VALUE
) return ERROR_SUCCESS
;
899 return wait_process_handle( package
, type
, handle
, action
);
902 static UINT
HANDLE_CustomType34( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
903 INT type
, const WCHAR
*action
)
905 const WCHAR
*workingdir
= NULL
;
911 workingdir
= msi_get_target_folder( package
, source
);
912 if (!workingdir
) return ERROR_FUNCTION_FAILED
;
914 deformat_string( package
, target
, &cmd
);
915 if (!cmd
) return ERROR_FUNCTION_FAILED
;
917 TRACE("cmd %s dir %s\n", debugstr_w(cmd
), debugstr_w(workingdir
));
919 handle
= execute_command( NULL
, cmd
, workingdir
);
921 if (handle
== INVALID_HANDLE_VALUE
) return ERROR_SUCCESS
;
922 return wait_process_handle( package
, type
, handle
, action
);
925 static DWORD
ACTION_CallScript( const GUID
*guid
)
927 msi_custom_action_info
*info
;
929 UINT r
= ERROR_FUNCTION_FAILED
;
931 info
= find_action_by_guid( guid
);
934 ERR("failed to find action %s\n", debugstr_guid( guid
) );
935 return ERROR_FUNCTION_FAILED
;
938 TRACE("function %s, script %s\n", debugstr_w( info
->target
), debugstr_w( info
->source
) );
940 hPackage
= alloc_msihandle( &info
->package
->hdr
);
943 r
= call_script( hPackage
, info
->type
, info
->source
, info
->target
, info
->action
);
944 TRACE("script returned %u\n", r
);
945 MsiCloseHandle( hPackage
);
948 ERR("failed to create handle for %p\n", info
->package
);
950 release_custom_action_data( info
);
954 static DWORD WINAPI
ScriptThread( LPVOID arg
)
959 TRACE("custom action (%x) started\n", GetCurrentThreadId() );
961 rc
= ACTION_CallScript( guid
);
963 TRACE("custom action (%x) returned %i\n", GetCurrentThreadId(), rc
);
965 MsiCloseAllHandles();
969 static msi_custom_action_info
*do_msidbCustomActionTypeScript(
970 MSIPACKAGE
*package
, INT type
, LPCWSTR script
, LPCWSTR function
, LPCWSTR action
)
972 msi_custom_action_info
*info
;
974 info
= msi_alloc( sizeof *info
);
978 msiobj_addref( &package
->hdr
);
979 info
->refs
= 2; /* 1 for our caller and 1 for thread we created */
980 info
->package
= package
;
982 info
->target
= strdupW( function
);
983 info
->source
= strdupW( script
);
984 info
->action
= strdupW( action
);
985 CoCreateGuid( &info
->guid
);
987 EnterCriticalSection( &msi_custom_action_cs
);
988 list_add_tail( &msi_pending_custom_actions
, &info
->entry
);
989 LeaveCriticalSection( &msi_custom_action_cs
);
991 info
->handle
= CreateThread( NULL
, 0, ScriptThread
, &info
->guid
, 0, NULL
);
994 /* release both references */
995 release_custom_action_data( info
);
996 release_custom_action_data( info
);
1003 static UINT
HANDLE_CustomType37_38( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
1004 INT type
, const WCHAR
*action
)
1006 msi_custom_action_info
*info
;
1008 TRACE("%s %s\n", debugstr_w(source
), debugstr_w(target
));
1010 info
= do_msidbCustomActionTypeScript( package
, type
, target
, NULL
, action
);
1011 return wait_thread_handle( info
);
1014 static UINT
HANDLE_CustomType5_6( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
1015 INT type
, const WCHAR
*action
)
1017 static const WCHAR query
[] = {
1018 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1019 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',' ',
1020 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
1021 MSIRECORD
*row
= NULL
;
1022 msi_custom_action_info
*info
;
1023 CHAR
*buffer
= NULL
;
1024 WCHAR
*bufferw
= NULL
;
1028 TRACE("%s %s\n", debugstr_w(source
), debugstr_w(target
));
1030 row
= MSI_QueryGetRecord(package
->db
, query
, source
);
1032 return ERROR_FUNCTION_FAILED
;
1034 r
= MSI_RecordReadStream(row
, 2, NULL
, &sz
);
1035 if (r
!= ERROR_SUCCESS
) goto done
;
1037 buffer
= msi_alloc( sz
+ 1 );
1040 r
= ERROR_FUNCTION_FAILED
;
1044 r
= MSI_RecordReadStream(row
, 2, buffer
, &sz
);
1045 if (r
!= ERROR_SUCCESS
)
1049 bufferw
= strdupAtoW(buffer
);
1052 r
= ERROR_FUNCTION_FAILED
;
1056 info
= do_msidbCustomActionTypeScript( package
, type
, bufferw
, target
, action
);
1057 r
= wait_thread_handle( info
);
1062 msiobj_release(&row
->hdr
);
1066 static UINT
HANDLE_CustomType21_22( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
1067 INT type
, const WCHAR
*action
)
1069 msi_custom_action_info
*info
;
1072 DWORD sz
, szHighWord
= 0, read
;
1074 WCHAR
*bufferw
=NULL
;
1078 TRACE("%s %s\n", debugstr_w(source
), debugstr_w(target
));
1080 file
= msi_get_loaded_file(package
, source
);
1083 ERR("invalid file key %s\n", debugstr_w(source
));
1084 return ERROR_FUNCTION_FAILED
;
1087 hFile
= CreateFileW(file
->TargetPath
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, NULL
);
1088 if (hFile
== INVALID_HANDLE_VALUE
) return ERROR_FUNCTION_FAILED
;
1090 sz
= GetFileSize(hFile
, &szHighWord
);
1091 if (sz
== INVALID_FILE_SIZE
|| szHighWord
!= 0)
1094 return ERROR_FUNCTION_FAILED
;
1096 buffer
= msi_alloc( sz
+ 1 );
1100 return ERROR_FUNCTION_FAILED
;
1102 bRet
= ReadFile(hFile
, buffer
, sz
, &read
, NULL
);
1106 r
= ERROR_FUNCTION_FAILED
;
1110 bufferw
= strdupAtoW(buffer
);
1113 r
= ERROR_FUNCTION_FAILED
;
1116 info
= do_msidbCustomActionTypeScript( package
, type
, bufferw
, target
, action
);
1117 r
= wait_thread_handle( info
);
1125 static UINT
HANDLE_CustomType53_54( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
1126 INT type
, const WCHAR
*action
)
1128 msi_custom_action_info
*info
;
1131 TRACE("%s %s\n", debugstr_w(source
), debugstr_w(target
));
1133 prop
= msi_dup_property( package
->db
, source
);
1134 if (!prop
) return ERROR_SUCCESS
;
1136 info
= do_msidbCustomActionTypeScript( package
, type
, prop
, NULL
, action
);
1138 return wait_thread_handle( info
);
1141 static BOOL
action_type_matches_script( UINT type
, UINT script
)
1146 case SCRIPT_INSTALL
:
1147 return !(type
& msidbCustomActionTypeCommit
) && !(type
& msidbCustomActionTypeRollback
);
1149 return (type
& msidbCustomActionTypeCommit
);
1150 case SCRIPT_ROLLBACK
:
1151 return (type
& msidbCustomActionTypeRollback
);
1153 ERR("unhandled script %u\n", script
);
1158 static UINT
defer_custom_action( MSIPACKAGE
*package
, const WCHAR
*action
, UINT type
)
1160 WCHAR
*actiondata
= msi_dup_property( package
->db
, action
);
1161 WCHAR
*usersid
= msi_dup_property( package
->db
, szUserSID
);
1162 WCHAR
*prodcode
= msi_dup_property( package
->db
, szProductCode
);
1163 WCHAR
*deferred
= msi_get_deferred_action( action
, actiondata
, usersid
, prodcode
);
1167 msi_free( actiondata
);
1168 msi_free( usersid
);
1169 msi_free( prodcode
);
1170 return ERROR_OUTOFMEMORY
;
1172 if (type
& msidbCustomActionTypeCommit
)
1174 TRACE("deferring commit action\n");
1175 msi_schedule_action( package
, SCRIPT_COMMIT
, deferred
);
1177 else if (type
& msidbCustomActionTypeRollback
)
1179 TRACE("deferring rollback action\n");
1180 msi_schedule_action( package
, SCRIPT_ROLLBACK
, deferred
);
1184 TRACE("deferring install action\n");
1185 msi_schedule_action( package
, SCRIPT_INSTALL
, deferred
);
1188 msi_free( actiondata
);
1189 msi_free( usersid
);
1190 msi_free( prodcode
);
1191 msi_free( deferred
);
1192 return ERROR_SUCCESS
;
1195 UINT
ACTION_CustomAction( MSIPACKAGE
*package
, LPCWSTR action
, UINT script
)
1197 static const WCHAR query
[] = {
1198 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1199 '`','C','u','s','t','o','m','A','c','t','i','o','n','`',' ','W','H','E','R','E',' ',
1200 '`','A','c','t','i' ,'o','n','`',' ','=',' ','\'','%','s','\'',0};
1201 UINT rc
= ERROR_SUCCESS
;
1204 const WCHAR
*source
, *target
, *ptr
, *deferred_data
= NULL
;
1205 WCHAR
*deformated
= NULL
;
1208 /* deferred action: [properties]Action */
1209 if ((ptr
= strrchrW(action
, ']')))
1211 deferred_data
= action
;
1215 row
= MSI_QueryGetRecord( package
->db
, query
, action
);
1217 return ERROR_CALL_NOT_IMPLEMENTED
;
1219 type
= MSI_RecordGetInteger(row
,2);
1220 source
= MSI_RecordGetString(row
,3);
1221 target
= MSI_RecordGetString(row
,4);
1223 TRACE("Handling custom action %s (%x %s %s)\n",debugstr_w(action
),type
,
1224 debugstr_w(source
), debugstr_w(target
));
1226 /* handle some of the deferred actions */
1227 if (type
& msidbCustomActionTypeTSAware
)
1228 FIXME("msidbCustomActionTypeTSAware not handled\n");
1230 if (type
& msidbCustomActionTypeInScript
)
1232 if (type
& msidbCustomActionTypeNoImpersonate
)
1233 WARN("msidbCustomActionTypeNoImpersonate not handled\n");
1235 if (!action_type_matches_script( type
, script
))
1237 rc
= defer_custom_action( package
, action
, type
);
1242 LPWSTR actiondata
= msi_dup_property( package
->db
, action
);
1244 if (type
& msidbCustomActionTypeInScript
)
1245 package
->scheduled_action_running
= TRUE
;
1247 if (type
& msidbCustomActionTypeCommit
)
1248 package
->commit_action_running
= TRUE
;
1250 if (type
& msidbCustomActionTypeRollback
)
1251 package
->rollback_action_running
= TRUE
;
1254 set_deferred_action_props(package
, deferred_data
);
1255 else if (actiondata
)
1256 msi_set_property( package
->db
, szCustomActionData
, actiondata
, -1 );
1258 msi_set_property( package
->db
, szCustomActionData
, szEmpty
, -1 );
1260 msi_free(actiondata
);
1263 else if (!check_execution_scheduling_options(package
,action
,type
))
1269 switch (type
& CUSTOM_ACTION_TYPE_MASK
)
1271 case 1: /* DLL file stored in a Binary table stream */
1272 rc
= HANDLE_CustomType1(package
,source
,target
,type
,action
);
1274 case 2: /* EXE file stored in a Binary table stream */
1275 rc
= HANDLE_CustomType2(package
,source
,target
,type
,action
);
1277 case 18: /*EXE file installed with package */
1278 rc
= HANDLE_CustomType18(package
,source
,target
,type
,action
);
1280 case 19: /* Error that halts install */
1281 rc
= HANDLE_CustomType19(package
,source
,target
,type
,action
);
1284 rc
= HANDLE_CustomType17(package
,source
,target
,type
,action
);
1286 case 23: /* installs another package in the source tree */
1287 deformat_string(package
,target
,&deformated
);
1288 rc
= HANDLE_CustomType23(package
,source
,deformated
,type
,action
);
1289 msi_free(deformated
);
1291 case 50: /*EXE file specified by a property value */
1292 rc
= HANDLE_CustomType50(package
,source
,target
,type
,action
);
1294 case 34: /*EXE to be run in specified directory */
1295 rc
= HANDLE_CustomType34(package
,source
,target
,type
,action
);
1297 case 35: /* Directory set with formatted text. */
1298 deformat_string(package
,target
,&deformated
);
1299 MSI_SetTargetPathW(package
, source
, deformated
);
1300 msi_free(deformated
);
1302 case 51: /* Property set with formatted text. */
1306 len
= deformat_string( package
, target
, &deformated
);
1307 rc
= msi_set_property( package
->db
, source
, deformated
, len
);
1308 if (rc
== ERROR_SUCCESS
&& !strcmpW( source
, szSourceDir
))
1309 msi_reset_folders( package
, TRUE
);
1310 msi_free(deformated
);
1312 case 37: /* JScript/VBScript text stored in target column. */
1314 rc
= HANDLE_CustomType37_38(package
,source
,target
,type
,action
);
1317 case 6: /* JScript/VBScript file stored in a Binary table stream. */
1318 rc
= HANDLE_CustomType5_6(package
,source
,target
,type
,action
);
1320 case 21: /* JScript/VBScript file installed with the product. */
1322 rc
= HANDLE_CustomType21_22(package
,source
,target
,type
,action
);
1324 case 53: /* JScript/VBScript text specified by a property value. */
1326 rc
= HANDLE_CustomType53_54(package
,source
,target
,type
,action
);
1329 FIXME("unhandled action type %u (%s %s)\n", type
& CUSTOM_ACTION_TYPE_MASK
,
1330 debugstr_w(source
), debugstr_w(target
));
1334 package
->scheduled_action_running
= FALSE
;
1335 package
->commit_action_running
= FALSE
;
1336 package
->rollback_action_running
= FALSE
;
1337 msiobj_release(&row
->hdr
);
1341 void ACTION_FinishCustomActions(const MSIPACKAGE
* package
)
1344 HANDLE
*wait_handles
;
1345 unsigned int handle_count
, i
;
1346 msi_custom_action_info
*info
, *cursor
;
1348 while ((item
= list_head( &package
->RunningActions
)))
1350 MSIRUNNINGACTION
*action
= LIST_ENTRY( item
, MSIRUNNINGACTION
, entry
);
1352 list_remove( &action
->entry
);
1354 TRACE("waiting for %s\n", debugstr_w( action
->name
) );
1355 msi_dialog_check_messages( action
->handle
);
1357 CloseHandle( action
->handle
);
1358 msi_free( action
->name
);
1362 EnterCriticalSection( &msi_custom_action_cs
);
1364 handle_count
= list_count( &msi_pending_custom_actions
);
1365 wait_handles
= msi_alloc( handle_count
* sizeof(HANDLE
) );
1368 LIST_FOR_EACH_ENTRY_SAFE( info
, cursor
, &msi_pending_custom_actions
, msi_custom_action_info
, entry
)
1370 if (info
->package
== package
)
1372 if (DuplicateHandle(GetCurrentProcess(), info
->handle
, GetCurrentProcess(), &wait_handles
[handle_count
], SYNCHRONIZE
, FALSE
, 0))
1377 LeaveCriticalSection( &msi_custom_action_cs
);
1379 for (i
= 0; i
< handle_count
; i
++)
1381 msi_dialog_check_messages( wait_handles
[i
] );
1382 CloseHandle( wait_handles
[i
] );
1384 msi_free( wait_handles
);
1386 EnterCriticalSection( &msi_custom_action_cs
);
1387 LIST_FOR_EACH_ENTRY_SAFE( info
, cursor
, &msi_pending_custom_actions
, msi_custom_action_info
, entry
)
1389 if (info
->package
== package
) release_custom_action_data( info
);
1391 LeaveCriticalSection( &msi_custom_action_cs
);
1394 typedef struct _msi_custom_remote_impl
{
1395 IWineMsiRemoteCustomAction IWineMsiRemoteCustomAction_iface
;
1397 } msi_custom_remote_impl
;
1399 static inline msi_custom_remote_impl
*impl_from_IWineMsiRemoteCustomAction( IWineMsiRemoteCustomAction
*iface
)
1401 return CONTAINING_RECORD(iface
, msi_custom_remote_impl
, IWineMsiRemoteCustomAction_iface
);
1404 static HRESULT WINAPI
mcr_QueryInterface( IWineMsiRemoteCustomAction
*iface
,
1405 REFIID riid
,LPVOID
*ppobj
)
1407 if( IsEqualCLSID( riid
, &IID_IUnknown
) ||
1408 IsEqualCLSID( riid
, &IID_IWineMsiRemoteCustomAction
) )
1410 IWineMsiRemoteCustomAction_AddRef( iface
);
1415 return E_NOINTERFACE
;
1418 static ULONG WINAPI
mcr_AddRef( IWineMsiRemoteCustomAction
*iface
)
1420 msi_custom_remote_impl
* This
= impl_from_IWineMsiRemoteCustomAction( iface
);
1422 return InterlockedIncrement( &This
->refs
);
1425 static ULONG WINAPI
mcr_Release( IWineMsiRemoteCustomAction
*iface
)
1427 msi_custom_remote_impl
* This
= impl_from_IWineMsiRemoteCustomAction( iface
);
1430 r
= InterlockedDecrement( &This
->refs
);
1436 static HRESULT WINAPI
mcr_GetActionInfo( IWineMsiRemoteCustomAction
*iface
, LPCGUID custom_action_guid
,
1437 INT
*type
, MSIHANDLE
*handle
, BSTR
*dll
, BSTR
*func
, IWineMsiRemotePackage
**remote_package
)
1439 msi_custom_action_info
*info
;
1441 info
= find_action_by_guid( custom_action_guid
);
1446 *handle
= alloc_msihandle( &info
->package
->hdr
);
1447 *dll
= SysAllocString( info
->source
);
1448 *func
= SysAllocString( info
->target
);
1450 release_custom_action_data( info
);
1451 return create_msi_remote_package( NULL
, (LPVOID
*)remote_package
);
1454 static const IWineMsiRemoteCustomActionVtbl msi_custom_remote_vtbl
=
1462 HRESULT
create_msi_custom_remote( IUnknown
*pOuter
, LPVOID
*ppObj
)
1464 msi_custom_remote_impl
* This
;
1466 This
= msi_alloc( sizeof *This
);
1468 return E_OUTOFMEMORY
;
1470 This
->IWineMsiRemoteCustomAction_iface
.lpVtbl
= &msi_custom_remote_vtbl
;
1473 *ppObj
= &This
->IWineMsiRemoteCustomAction_iface
;