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")
544 "movl 8(%ebp),%eax\n\t"
547 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
548 __ASM_CFI(".cfi_same_value %ebp\n\t")
551 static inline UINT
CUSTOMPROC_wrapper( MsiCustomActionEntryPoint proc
, MSIHANDLE handle
)
557 static DWORD
ACTION_CallDllFunction( const GUID
*guid
)
559 MsiCustomActionEntryPoint fn
;
560 MSIHANDLE hPackage
, handle
;
563 UINT r
= ERROR_FUNCTION_FAILED
;
564 BSTR dll
= NULL
, function
= NULL
;
566 IWineMsiRemotePackage
*remote_package
= NULL
;
568 TRACE("%s\n", debugstr_guid( guid
));
570 r
= get_action_info( guid
, &type
, &handle
, &dll
, &function
, &remote_package
);
571 if (r
!= ERROR_SUCCESS
)
574 hModule
= LoadLibraryW( dll
);
577 WARN( "failed to load dll %s (%u)\n", debugstr_w( dll
), GetLastError() );
578 return ERROR_SUCCESS
;
581 proc
= strdupWtoA( function
);
582 fn
= (MsiCustomActionEntryPoint
) GetProcAddress( hModule
, proc
);
586 hPackage
= alloc_msi_remote_handle( (IUnknown
*)remote_package
);
589 IWineMsiRemotePackage_SetMsiHandle( remote_package
, handle
);
590 TRACE("calling %s\n", debugstr_w( function
) );
591 handle_msi_break( function
);
595 r
= CUSTOMPROC_wrapper( fn
, hPackage
);
599 ERR("Custom action (%s:%s) caused a page fault: %08x\n",
600 debugstr_w(dll
), debugstr_w(function
), GetExceptionCode());
605 MsiCloseHandle( hPackage
);
608 ERR("failed to create handle for %p\n", remote_package
);
611 ERR("GetProcAddress(%s) failed\n", debugstr_w( function
) );
613 FreeLibrary(hModule
);
615 IWineMsiRemotePackage_Release( remote_package
);
616 SysFreeString( dll
);
617 SysFreeString( function
);
618 MsiCloseHandle( handle
);
623 static DWORD WINAPI
DllThread( LPVOID arg
)
628 TRACE("custom action (%x) started\n", GetCurrentThreadId() );
630 rc
= ACTION_CallDllFunction( guid
);
632 TRACE("custom action (%x) returned %i\n", GetCurrentThreadId(), rc
);
634 MsiCloseAllHandles();
638 static msi_custom_action_info
*do_msidbCustomActionTypeDll(
639 MSIPACKAGE
*package
, INT type
, LPCWSTR source
, LPCWSTR target
, LPCWSTR action
)
641 msi_custom_action_info
*info
;
643 info
= msi_alloc( sizeof *info
);
647 msiobj_addref( &package
->hdr
);
648 info
->refs
= 2; /* 1 for our caller and 1 for thread we created */
649 info
->package
= package
;
651 info
->target
= strdupW( target
);
652 info
->source
= strdupW( source
);
653 info
->action
= strdupW( action
);
654 CoCreateGuid( &info
->guid
);
656 EnterCriticalSection( &msi_custom_action_cs
);
657 list_add_tail( &msi_pending_custom_actions
, &info
->entry
);
658 LeaveCriticalSection( &msi_custom_action_cs
);
660 info
->handle
= CreateThread( NULL
, 0, DllThread
, &info
->guid
, 0, NULL
);
663 /* release both references */
664 release_custom_action_data( info
);
665 release_custom_action_data( info
);
672 static UINT
HANDLE_CustomType1( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
673 INT type
, const WCHAR
*action
)
675 msi_custom_action_info
*info
;
678 if (!(binary
= get_temp_binary( package
, source
, TRUE
)))
679 return ERROR_FUNCTION_FAILED
;
681 TRACE("Calling function %s from %s\n", debugstr_w(target
), debugstr_w(binary
->tmpfile
));
683 info
= do_msidbCustomActionTypeDll( package
, type
, binary
->tmpfile
, target
, action
);
684 return wait_thread_handle( info
);
687 static HANDLE
execute_command( const WCHAR
*app
, WCHAR
*arg
, const WCHAR
*dir
)
689 static const WCHAR dotexeW
[] = {'.','e','x','e',0};
691 PROCESS_INFORMATION info
;
692 WCHAR
*exe
= NULL
, *cmd
= NULL
, *p
;
700 if (!(exe
= msi_alloc( MAX_PATH
* sizeof(WCHAR
) ))) return INVALID_HANDLE_VALUE
;
701 len_exe
= SearchPathW( NULL
, app
, dotexeW
, MAX_PATH
, exe
, NULL
);
702 if (len_exe
>= MAX_PATH
)
705 if (!(exe
= msi_alloc( len_exe
* sizeof(WCHAR
) ))) return INVALID_HANDLE_VALUE
;
706 len_exe
= SearchPathW( NULL
, app
, dotexeW
, len_exe
, exe
, NULL
);
710 WARN("can't find executable %u\n", GetLastError());
712 return INVALID_HANDLE_VALUE
;
715 if (arg
) len_arg
= strlenW( arg
);
716 if (!(cmd
= msi_alloc( (len_exe
+ len_arg
+ 4) * sizeof(WCHAR
) )))
719 return INVALID_HANDLE_VALUE
;
722 if (strchrW( exe
, ' ' ))
725 memcpy( p
, exe
, len_exe
* sizeof(WCHAR
) );
738 memcpy( p
, arg
, len_arg
* sizeof(WCHAR
) );
742 memset( &si
, 0, sizeof(STARTUPINFOW
) );
743 ret
= CreateProcessW( exe
, exe
? cmd
: arg
, NULL
, NULL
, FALSE
, 0, NULL
, dir
, &si
, &info
);
748 WARN("unable to execute command %u\n", GetLastError());
749 return INVALID_HANDLE_VALUE
;
751 CloseHandle( info
.hThread
);
752 return info
.hProcess
;
755 static UINT
HANDLE_CustomType2( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
756 INT type
, const WCHAR
*action
)
762 if (!(binary
= get_temp_binary( package
, source
, FALSE
))) return ERROR_FUNCTION_FAILED
;
764 deformat_string( package
, target
, &arg
);
765 TRACE("exe %s arg %s\n", debugstr_w(binary
->tmpfile
), debugstr_w(arg
));
767 handle
= execute_command( binary
->tmpfile
, arg
, szCRoot
);
769 if (handle
== INVALID_HANDLE_VALUE
) return ERROR_SUCCESS
;
770 return wait_process_handle( package
, type
, handle
, action
);
773 static UINT
HANDLE_CustomType17( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
774 INT type
, const WCHAR
*action
)
776 msi_custom_action_info
*info
;
779 TRACE("%s %s\n", debugstr_w(source
), debugstr_w(target
));
781 file
= msi_get_loaded_file( package
, source
);
784 ERR("invalid file key %s\n", debugstr_w( source
));
785 return ERROR_FUNCTION_FAILED
;
788 info
= do_msidbCustomActionTypeDll( package
, type
, file
->TargetPath
, target
, action
);
789 return wait_thread_handle( info
);
792 static UINT
HANDLE_CustomType18( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
793 INT type
, const WCHAR
*action
)
799 if (!(file
= msi_get_loaded_file( package
, source
))) return ERROR_FUNCTION_FAILED
;
801 deformat_string( package
, target
, &arg
);
802 TRACE("exe %s arg %s\n", debugstr_w(file
->TargetPath
), debugstr_w(arg
));
804 handle
= execute_command( file
->TargetPath
, arg
, szCRoot
);
806 if (handle
== INVALID_HANDLE_VALUE
) return ERROR_SUCCESS
;
807 return wait_process_handle( package
, type
, handle
, action
);
810 static UINT
HANDLE_CustomType19( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
811 INT type
, const WCHAR
*action
)
813 static const WCHAR query
[] = {
814 'S','E','L','E','C','T',' ','`','M','e','s','s','a','g','e','`',' ',
815 'F','R','O','M',' ','`','E','r','r','o','r','`',' ',
816 'W','H','E','R','E',' ','`','E','r','r','o','r','`',' ','=',' ',
820 LPWSTR deformated
= NULL
;
822 deformat_string( package
, target
, &deformated
);
824 /* first try treat the error as a number */
825 row
= MSI_QueryGetRecord( package
->db
, query
, deformated
);
828 LPCWSTR error
= MSI_RecordGetString( row
, 1 );
829 if ((package
->ui_level
& INSTALLUILEVEL_MASK
) != INSTALLUILEVEL_NONE
)
830 MessageBoxW( NULL
, error
, NULL
, MB_OK
);
831 msiobj_release( &row
->hdr
);
833 else if ((package
->ui_level
& INSTALLUILEVEL_MASK
) != INSTALLUILEVEL_NONE
)
834 MessageBoxW( NULL
, deformated
, NULL
, MB_OK
);
836 msi_free( deformated
);
838 return ERROR_INSTALL_FAILURE
;
841 static UINT
HANDLE_CustomType23( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
842 INT type
, const WCHAR
*action
)
844 static const WCHAR msiexecW
[] = {'m','s','i','e','x','e','c',0};
845 static const WCHAR paramsW
[] = {'/','q','b',' ','/','i',' '};
846 WCHAR
*dir
, *arg
, *p
;
847 UINT len_src
, len_dir
, len_tgt
, len
= sizeof(paramsW
)/sizeof(paramsW
[0]);
850 if (!(dir
= msi_dup_property( package
->db
, szOriginalDatabase
))) return ERROR_OUTOFMEMORY
;
851 if (!(p
= strrchrW( dir
, '\\' )) && !(p
= strrchrW( dir
, '/' )))
854 return ERROR_FUNCTION_FAILED
;
858 len_src
= strlenW( source
);
859 len_tgt
= strlenW( target
);
860 if (!(arg
= msi_alloc( (len
+ len_dir
+ len_src
+ len_tgt
+ 5) * sizeof(WCHAR
) )))
863 return ERROR_OUTOFMEMORY
;
865 memcpy( arg
, paramsW
, sizeof(paramsW
) );
867 memcpy( arg
+ len
, dir
, len_dir
* sizeof(WCHAR
) );
870 memcpy( arg
+ len
, source
, len_src
* sizeof(WCHAR
) );
874 strcpyW( arg
+ len
, target
);
876 TRACE("installing %s concurrently\n", debugstr_w(source
));
878 handle
= execute_command( msiexecW
, arg
, dir
);
881 if (handle
== INVALID_HANDLE_VALUE
) return ERROR_SUCCESS
;
882 return wait_process_handle( package
, type
, handle
, action
);
885 static UINT
HANDLE_CustomType50( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
886 INT type
, const WCHAR
*action
)
891 if (!(exe
= msi_dup_property( package
->db
, source
))) return ERROR_SUCCESS
;
893 deformat_string( package
, target
, &arg
);
894 TRACE("exe %s arg %s\n", debugstr_w(exe
), debugstr_w(arg
));
896 handle
= execute_command( exe
, arg
, szCRoot
);
899 if (handle
== INVALID_HANDLE_VALUE
) return ERROR_SUCCESS
;
900 return wait_process_handle( package
, type
, handle
, action
);
903 static UINT
HANDLE_CustomType34( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
904 INT type
, const WCHAR
*action
)
906 const WCHAR
*workingdir
= NULL
;
912 workingdir
= msi_get_target_folder( package
, source
);
913 if (!workingdir
) return ERROR_FUNCTION_FAILED
;
915 deformat_string( package
, target
, &cmd
);
916 if (!cmd
) return ERROR_FUNCTION_FAILED
;
918 TRACE("cmd %s dir %s\n", debugstr_w(cmd
), debugstr_w(workingdir
));
920 handle
= execute_command( NULL
, cmd
, workingdir
);
922 if (handle
== INVALID_HANDLE_VALUE
) return ERROR_SUCCESS
;
923 return wait_process_handle( package
, type
, handle
, action
);
926 static DWORD
ACTION_CallScript( const GUID
*guid
)
928 msi_custom_action_info
*info
;
930 UINT r
= ERROR_FUNCTION_FAILED
;
932 info
= find_action_by_guid( guid
);
935 ERR("failed to find action %s\n", debugstr_guid( guid
) );
936 return ERROR_FUNCTION_FAILED
;
939 TRACE("function %s, script %s\n", debugstr_w( info
->target
), debugstr_w( info
->source
) );
941 hPackage
= alloc_msihandle( &info
->package
->hdr
);
944 r
= call_script( hPackage
, info
->type
, info
->source
, info
->target
, info
->action
);
945 TRACE("script returned %u\n", r
);
946 MsiCloseHandle( hPackage
);
949 ERR("failed to create handle for %p\n", info
->package
);
951 release_custom_action_data( info
);
955 static DWORD WINAPI
ScriptThread( LPVOID arg
)
960 TRACE("custom action (%x) started\n", GetCurrentThreadId() );
962 rc
= ACTION_CallScript( guid
);
964 TRACE("custom action (%x) returned %i\n", GetCurrentThreadId(), rc
);
966 MsiCloseAllHandles();
970 static msi_custom_action_info
*do_msidbCustomActionTypeScript(
971 MSIPACKAGE
*package
, INT type
, LPCWSTR script
, LPCWSTR function
, LPCWSTR action
)
973 msi_custom_action_info
*info
;
975 info
= msi_alloc( sizeof *info
);
979 msiobj_addref( &package
->hdr
);
980 info
->refs
= 2; /* 1 for our caller and 1 for thread we created */
981 info
->package
= package
;
983 info
->target
= strdupW( function
);
984 info
->source
= strdupW( script
);
985 info
->action
= strdupW( action
);
986 CoCreateGuid( &info
->guid
);
988 EnterCriticalSection( &msi_custom_action_cs
);
989 list_add_tail( &msi_pending_custom_actions
, &info
->entry
);
990 LeaveCriticalSection( &msi_custom_action_cs
);
992 info
->handle
= CreateThread( NULL
, 0, ScriptThread
, &info
->guid
, 0, NULL
);
995 /* release both references */
996 release_custom_action_data( info
);
997 release_custom_action_data( info
);
1004 static UINT
HANDLE_CustomType37_38( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
1005 INT type
, const WCHAR
*action
)
1007 msi_custom_action_info
*info
;
1009 TRACE("%s %s\n", debugstr_w(source
), debugstr_w(target
));
1011 info
= do_msidbCustomActionTypeScript( package
, type
, target
, NULL
, action
);
1012 return wait_thread_handle( info
);
1015 static UINT
HANDLE_CustomType5_6( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
1016 INT type
, const WCHAR
*action
)
1018 static const WCHAR query
[] = {
1019 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1020 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',' ',
1021 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
1022 MSIRECORD
*row
= NULL
;
1023 msi_custom_action_info
*info
;
1024 CHAR
*buffer
= NULL
;
1025 WCHAR
*bufferw
= NULL
;
1029 TRACE("%s %s\n", debugstr_w(source
), debugstr_w(target
));
1031 row
= MSI_QueryGetRecord(package
->db
, query
, source
);
1033 return ERROR_FUNCTION_FAILED
;
1035 r
= MSI_RecordReadStream(row
, 2, NULL
, &sz
);
1036 if (r
!= ERROR_SUCCESS
) goto done
;
1038 buffer
= msi_alloc( sz
+ 1 );
1041 r
= ERROR_FUNCTION_FAILED
;
1045 r
= MSI_RecordReadStream(row
, 2, buffer
, &sz
);
1046 if (r
!= ERROR_SUCCESS
)
1050 bufferw
= strdupAtoW(buffer
);
1053 r
= ERROR_FUNCTION_FAILED
;
1057 info
= do_msidbCustomActionTypeScript( package
, type
, bufferw
, target
, action
);
1058 r
= wait_thread_handle( info
);
1063 msiobj_release(&row
->hdr
);
1067 static UINT
HANDLE_CustomType21_22( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
1068 INT type
, const WCHAR
*action
)
1070 msi_custom_action_info
*info
;
1073 DWORD sz
, szHighWord
= 0, read
;
1075 WCHAR
*bufferw
=NULL
;
1079 TRACE("%s %s\n", debugstr_w(source
), debugstr_w(target
));
1081 file
= msi_get_loaded_file(package
, source
);
1084 ERR("invalid file key %s\n", debugstr_w(source
));
1085 return ERROR_FUNCTION_FAILED
;
1088 hFile
= CreateFileW(file
->TargetPath
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, NULL
);
1089 if (hFile
== INVALID_HANDLE_VALUE
) return ERROR_FUNCTION_FAILED
;
1091 sz
= GetFileSize(hFile
, &szHighWord
);
1092 if (sz
== INVALID_FILE_SIZE
|| szHighWord
!= 0)
1095 return ERROR_FUNCTION_FAILED
;
1097 buffer
= msi_alloc( sz
+ 1 );
1101 return ERROR_FUNCTION_FAILED
;
1103 bRet
= ReadFile(hFile
, buffer
, sz
, &read
, NULL
);
1107 r
= ERROR_FUNCTION_FAILED
;
1111 bufferw
= strdupAtoW(buffer
);
1114 r
= ERROR_FUNCTION_FAILED
;
1117 info
= do_msidbCustomActionTypeScript( package
, type
, bufferw
, target
, action
);
1118 r
= wait_thread_handle( info
);
1126 static UINT
HANDLE_CustomType53_54( MSIPACKAGE
*package
, const WCHAR
*source
, const WCHAR
*target
,
1127 INT type
, const WCHAR
*action
)
1129 msi_custom_action_info
*info
;
1132 TRACE("%s %s\n", debugstr_w(source
), debugstr_w(target
));
1134 prop
= msi_dup_property( package
->db
, source
);
1135 if (!prop
) return ERROR_SUCCESS
;
1137 info
= do_msidbCustomActionTypeScript( package
, type
, prop
, NULL
, action
);
1139 return wait_thread_handle( info
);
1142 static BOOL
action_type_matches_script( UINT type
, UINT script
)
1147 case SCRIPT_INSTALL
:
1148 return !(type
& msidbCustomActionTypeCommit
) && !(type
& msidbCustomActionTypeRollback
);
1150 return (type
& msidbCustomActionTypeCommit
);
1151 case SCRIPT_ROLLBACK
:
1152 return (type
& msidbCustomActionTypeRollback
);
1154 ERR("unhandled script %u\n", script
);
1159 static UINT
defer_custom_action( MSIPACKAGE
*package
, const WCHAR
*action
, UINT type
)
1161 WCHAR
*actiondata
= msi_dup_property( package
->db
, action
);
1162 WCHAR
*usersid
= msi_dup_property( package
->db
, szUserSID
);
1163 WCHAR
*prodcode
= msi_dup_property( package
->db
, szProductCode
);
1164 WCHAR
*deferred
= msi_get_deferred_action( action
, actiondata
, usersid
, prodcode
);
1168 msi_free( actiondata
);
1169 msi_free( usersid
);
1170 msi_free( prodcode
);
1171 return ERROR_OUTOFMEMORY
;
1173 if (type
& msidbCustomActionTypeCommit
)
1175 TRACE("deferring commit action\n");
1176 msi_schedule_action( package
, SCRIPT_COMMIT
, deferred
);
1178 else if (type
& msidbCustomActionTypeRollback
)
1180 TRACE("deferring rollback action\n");
1181 msi_schedule_action( package
, SCRIPT_ROLLBACK
, deferred
);
1185 TRACE("deferring install action\n");
1186 msi_schedule_action( package
, SCRIPT_INSTALL
, deferred
);
1189 msi_free( actiondata
);
1190 msi_free( usersid
);
1191 msi_free( prodcode
);
1192 msi_free( deferred
);
1193 return ERROR_SUCCESS
;
1196 UINT
ACTION_CustomAction( MSIPACKAGE
*package
, LPCWSTR action
, UINT script
)
1198 static const WCHAR query
[] = {
1199 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1200 '`','C','u','s','t','o','m','A','c','t','i','o','n','`',' ','W','H','E','R','E',' ',
1201 '`','A','c','t','i' ,'o','n','`',' ','=',' ','\'','%','s','\'',0};
1202 UINT rc
= ERROR_SUCCESS
;
1205 const WCHAR
*source
, *target
, *ptr
, *deferred_data
= NULL
;
1206 WCHAR
*deformated
= NULL
;
1209 /* deferred action: [properties]Action */
1210 if ((ptr
= strrchrW(action
, ']')))
1212 deferred_data
= action
;
1216 row
= MSI_QueryGetRecord( package
->db
, query
, action
);
1218 return ERROR_CALL_NOT_IMPLEMENTED
;
1220 type
= MSI_RecordGetInteger(row
,2);
1221 source
= MSI_RecordGetString(row
,3);
1222 target
= MSI_RecordGetString(row
,4);
1224 TRACE("Handling custom action %s (%x %s %s)\n",debugstr_w(action
),type
,
1225 debugstr_w(source
), debugstr_w(target
));
1227 /* handle some of the deferred actions */
1228 if (type
& msidbCustomActionTypeTSAware
)
1229 FIXME("msidbCustomActionTypeTSAware not handled\n");
1231 if (type
& msidbCustomActionTypeInScript
)
1233 if (type
& msidbCustomActionTypeNoImpersonate
)
1234 WARN("msidbCustomActionTypeNoImpersonate not handled\n");
1236 if (!action_type_matches_script( type
, script
))
1238 rc
= defer_custom_action( package
, action
, type
);
1243 LPWSTR actiondata
= msi_dup_property( package
->db
, action
);
1245 if (type
& msidbCustomActionTypeInScript
)
1246 package
->scheduled_action_running
= TRUE
;
1248 if (type
& msidbCustomActionTypeCommit
)
1249 package
->commit_action_running
= TRUE
;
1251 if (type
& msidbCustomActionTypeRollback
)
1252 package
->rollback_action_running
= TRUE
;
1255 set_deferred_action_props(package
, deferred_data
);
1256 else if (actiondata
)
1257 msi_set_property( package
->db
, szCustomActionData
, actiondata
, -1 );
1259 msi_set_property( package
->db
, szCustomActionData
, szEmpty
, -1 );
1261 msi_free(actiondata
);
1264 else if (!check_execution_scheduling_options(package
,action
,type
))
1270 switch (type
& CUSTOM_ACTION_TYPE_MASK
)
1272 case 1: /* DLL file stored in a Binary table stream */
1273 rc
= HANDLE_CustomType1(package
,source
,target
,type
,action
);
1275 case 2: /* EXE file stored in a Binary table stream */
1276 rc
= HANDLE_CustomType2(package
,source
,target
,type
,action
);
1278 case 18: /*EXE file installed with package */
1279 rc
= HANDLE_CustomType18(package
,source
,target
,type
,action
);
1281 case 19: /* Error that halts install */
1282 rc
= HANDLE_CustomType19(package
,source
,target
,type
,action
);
1285 rc
= HANDLE_CustomType17(package
,source
,target
,type
,action
);
1287 case 23: /* installs another package in the source tree */
1288 deformat_string(package
,target
,&deformated
);
1289 rc
= HANDLE_CustomType23(package
,source
,deformated
,type
,action
);
1290 msi_free(deformated
);
1292 case 50: /*EXE file specified by a property value */
1293 rc
= HANDLE_CustomType50(package
,source
,target
,type
,action
);
1295 case 34: /*EXE to be run in specified directory */
1296 rc
= HANDLE_CustomType34(package
,source
,target
,type
,action
);
1298 case 35: /* Directory set with formatted text. */
1299 deformat_string(package
,target
,&deformated
);
1300 MSI_SetTargetPathW(package
, source
, deformated
);
1301 msi_free(deformated
);
1303 case 51: /* Property set with formatted text. */
1307 len
= deformat_string( package
, target
, &deformated
);
1308 rc
= msi_set_property( package
->db
, source
, deformated
, len
);
1309 if (rc
== ERROR_SUCCESS
&& !strcmpW( source
, szSourceDir
))
1310 msi_reset_folders( package
, TRUE
);
1311 msi_free(deformated
);
1313 case 37: /* JScript/VBScript text stored in target column. */
1315 rc
= HANDLE_CustomType37_38(package
,source
,target
,type
,action
);
1318 case 6: /* JScript/VBScript file stored in a Binary table stream. */
1319 rc
= HANDLE_CustomType5_6(package
,source
,target
,type
,action
);
1321 case 21: /* JScript/VBScript file installed with the product. */
1323 rc
= HANDLE_CustomType21_22(package
,source
,target
,type
,action
);
1325 case 53: /* JScript/VBScript text specified by a property value. */
1327 rc
= HANDLE_CustomType53_54(package
,source
,target
,type
,action
);
1330 FIXME("unhandled action type %u (%s %s)\n", type
& CUSTOM_ACTION_TYPE_MASK
,
1331 debugstr_w(source
), debugstr_w(target
));
1335 package
->scheduled_action_running
= FALSE
;
1336 package
->commit_action_running
= FALSE
;
1337 package
->rollback_action_running
= FALSE
;
1338 msiobj_release(&row
->hdr
);
1342 void ACTION_FinishCustomActions(const MSIPACKAGE
* package
)
1345 HANDLE
*wait_handles
;
1346 unsigned int handle_count
, i
;
1347 msi_custom_action_info
*info
, *cursor
;
1349 while ((item
= list_head( &package
->RunningActions
)))
1351 MSIRUNNINGACTION
*action
= LIST_ENTRY( item
, MSIRUNNINGACTION
, entry
);
1353 list_remove( &action
->entry
);
1355 TRACE("waiting for %s\n", debugstr_w( action
->name
) );
1356 msi_dialog_check_messages( action
->handle
);
1358 CloseHandle( action
->handle
);
1359 msi_free( action
->name
);
1363 EnterCriticalSection( &msi_custom_action_cs
);
1365 handle_count
= list_count( &msi_pending_custom_actions
);
1366 wait_handles
= msi_alloc( handle_count
* sizeof(HANDLE
) );
1369 LIST_FOR_EACH_ENTRY_SAFE( info
, cursor
, &msi_pending_custom_actions
, msi_custom_action_info
, entry
)
1371 if (info
->package
== package
)
1373 if (DuplicateHandle(GetCurrentProcess(), info
->handle
, GetCurrentProcess(), &wait_handles
[handle_count
], SYNCHRONIZE
, FALSE
, 0))
1378 LeaveCriticalSection( &msi_custom_action_cs
);
1380 for (i
= 0; i
< handle_count
; i
++)
1382 msi_dialog_check_messages( wait_handles
[i
] );
1383 CloseHandle( wait_handles
[i
] );
1385 msi_free( wait_handles
);
1387 EnterCriticalSection( &msi_custom_action_cs
);
1388 LIST_FOR_EACH_ENTRY_SAFE( info
, cursor
, &msi_pending_custom_actions
, msi_custom_action_info
, entry
)
1390 if (info
->package
== package
) release_custom_action_data( info
);
1392 LeaveCriticalSection( &msi_custom_action_cs
);
1395 typedef struct _msi_custom_remote_impl
{
1396 IWineMsiRemoteCustomAction IWineMsiRemoteCustomAction_iface
;
1398 } msi_custom_remote_impl
;
1400 static inline msi_custom_remote_impl
*impl_from_IWineMsiRemoteCustomAction( IWineMsiRemoteCustomAction
*iface
)
1402 return CONTAINING_RECORD(iface
, msi_custom_remote_impl
, IWineMsiRemoteCustomAction_iface
);
1405 static HRESULT WINAPI
mcr_QueryInterface( IWineMsiRemoteCustomAction
*iface
,
1406 REFIID riid
,LPVOID
*ppobj
)
1408 if( IsEqualCLSID( riid
, &IID_IUnknown
) ||
1409 IsEqualCLSID( riid
, &IID_IWineMsiRemoteCustomAction
) )
1411 IWineMsiRemoteCustomAction_AddRef( iface
);
1416 return E_NOINTERFACE
;
1419 static ULONG WINAPI
mcr_AddRef( IWineMsiRemoteCustomAction
*iface
)
1421 msi_custom_remote_impl
* This
= impl_from_IWineMsiRemoteCustomAction( iface
);
1423 return InterlockedIncrement( &This
->refs
);
1426 static ULONG WINAPI
mcr_Release( IWineMsiRemoteCustomAction
*iface
)
1428 msi_custom_remote_impl
* This
= impl_from_IWineMsiRemoteCustomAction( iface
);
1431 r
= InterlockedDecrement( &This
->refs
);
1437 static HRESULT WINAPI
mcr_GetActionInfo( IWineMsiRemoteCustomAction
*iface
, LPCGUID custom_action_guid
,
1438 INT
*type
, MSIHANDLE
*handle
, BSTR
*dll
, BSTR
*func
, IWineMsiRemotePackage
**remote_package
)
1440 msi_custom_action_info
*info
;
1442 info
= find_action_by_guid( custom_action_guid
);
1447 *handle
= alloc_msihandle( &info
->package
->hdr
);
1448 *dll
= SysAllocString( info
->source
);
1449 *func
= SysAllocString( info
->target
);
1451 release_custom_action_data( info
);
1452 return create_msi_remote_package( NULL
, (LPVOID
*)remote_package
);
1455 static const IWineMsiRemoteCustomActionVtbl msi_custom_remote_vtbl
=
1463 HRESULT
create_msi_custom_remote( IUnknown
*pOuter
, LPVOID
*ppObj
)
1465 msi_custom_remote_impl
* This
;
1467 This
= msi_alloc( sizeof *This
);
1469 return E_OUTOFMEMORY
;
1471 This
->IWineMsiRemoteCustomAction_iface
.lpVtbl
= &msi_custom_remote_vtbl
;
1474 *ppObj
= &This
->IWineMsiRemoteCustomAction_iface
;