dswave: Remove some unused functions.
[wine/multimedia.git] / dlls / msi / custom.c
blob66a7ce6a89f02f49198909a2a480503568818a27
1 /*
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
21 #include "config.h"
22 #include "wine/port.h"
24 #define COBJMACROS
26 #include <stdarg.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winerror.h"
30 #include "msidefs.h"
31 #include "winuser.h"
32 #include "objbase.h"
33 #include "oleauto.h"
35 #include "msipriv.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
44 static const WCHAR c_collen[] = {'C',':','\\',0};
45 static const WCHAR cszTempFolder[]= {'T','e','m','p','F','o','l','d','e','r',0};
48 static const WCHAR szActionData[] = {
49 'C','u','s','t','o','m','A','c','t','i','o','n','D','a','t','a',0
51 static const WCHAR ProdCode[] = {
52 'P','r','o','d','u','c','t','C','o','d','e',0
54 static const WCHAR UserSID[] = {'U','s','e','r','S','I','D',0};
56 typedef struct tagMSIRUNNINGACTION
58 struct list entry;
59 HANDLE handle;
60 BOOL process;
61 LPWSTR name;
62 } MSIRUNNINGACTION;
64 static UINT HANDLE_CustomType1(MSIPACKAGE *package, LPCWSTR source,
65 LPCWSTR target, const INT type, LPCWSTR action);
66 static UINT HANDLE_CustomType2(MSIPACKAGE *package, LPCWSTR source,
67 LPCWSTR target, const INT type, LPCWSTR action);
68 static UINT HANDLE_CustomType17(MSIPACKAGE *package, LPCWSTR source,
69 LPCWSTR target, const INT type, LPCWSTR action);
70 static UINT HANDLE_CustomType18(MSIPACKAGE *package, LPCWSTR source,
71 LPCWSTR target, const INT type, LPCWSTR action);
72 static UINT HANDLE_CustomType19(MSIPACKAGE *package, LPCWSTR source,
73 LPCWSTR target, const INT type, LPCWSTR action);
74 static UINT HANDLE_CustomType23(MSIPACKAGE *package, LPCWSTR source,
75 LPCWSTR target, const INT type, LPCWSTR action);
76 static UINT HANDLE_CustomType50(MSIPACKAGE *package, LPCWSTR source,
77 LPCWSTR target, const INT type, LPCWSTR action);
78 static UINT HANDLE_CustomType34(MSIPACKAGE *package, LPCWSTR source,
79 LPCWSTR target, const INT type, LPCWSTR action);
80 static UINT HANDLE_CustomType37_38(MSIPACKAGE *package, LPCWSTR source,
81 LPCWSTR target, const INT type, LPCWSTR action);
82 static UINT HANDLE_CustomType5_6(MSIPACKAGE *package, LPCWSTR source,
83 LPCWSTR target, const INT type, LPCWSTR action);
84 static UINT HANDLE_CustomType21_22(MSIPACKAGE *package, LPCWSTR source,
85 LPCWSTR target, const INT type, LPCWSTR action);
86 static UINT HANDLE_CustomType53_54(MSIPACKAGE *package, LPCWSTR source,
87 LPCWSTR target, const INT type, LPCWSTR action);
89 typedef UINT (WINAPI *MsiCustomActionEntryPoint)( MSIHANDLE );
91 static CRITICAL_SECTION msi_custom_action_cs;
92 static CRITICAL_SECTION_DEBUG msi_custom_action_cs_debug =
94 0, 0, &msi_custom_action_cs,
95 { &msi_custom_action_cs_debug.ProcessLocksList,
96 &msi_custom_action_cs_debug.ProcessLocksList },
97 0, 0, { (DWORD_PTR)(__FILE__ ": msi_custom_action_cs") }
99 static CRITICAL_SECTION msi_custom_action_cs = { &msi_custom_action_cs_debug, -1, 0, 0, 0, 0 };
101 static struct list msi_pending_custom_actions = LIST_INIT( msi_pending_custom_actions );
103 static BOOL check_execution_scheduling_options(MSIPACKAGE *package, LPCWSTR action, UINT options)
105 if (!package->script)
106 return TRUE;
108 if ((options & msidbCustomActionTypeClientRepeat) ==
109 msidbCustomActionTypeClientRepeat)
111 if (!(package->script->InWhatSequence & SEQUENCE_UI &&
112 package->script->InWhatSequence & SEQUENCE_EXEC))
114 TRACE("Skipping action due to dbCustomActionTypeClientRepeat option.\n");
115 return FALSE;
118 else if (options & msidbCustomActionTypeFirstSequence)
120 if (package->script->InWhatSequence & SEQUENCE_UI &&
121 package->script->InWhatSequence & SEQUENCE_EXEC )
123 TRACE("Skipping action due to msidbCustomActionTypeFirstSequence option.\n");
124 return FALSE;
127 else if (options & msidbCustomActionTypeOncePerProcess)
129 if (check_unique_action(package,action))
131 TRACE("Skipping action due to msidbCustomActionTypeOncePerProcess option.\n");
132 return FALSE;
134 else
135 register_unique_action(package,action);
138 return TRUE;
141 /* stores the following properties before the action:
143 * [CustomActionData<=>UserSID<=>ProductCode]Action
145 static LPWSTR msi_get_deferred_action(LPCWSTR action, LPCWSTR actiondata,
146 LPCWSTR usersid, LPCWSTR prodcode)
148 LPWSTR deferred;
149 DWORD len;
151 static const WCHAR format[] = {
152 '[','%','s','<','=','>','%','s','<','=','>','%','s',']','%','s',0
155 if (!actiondata)
156 return strdupW(action);
158 len = lstrlenW(action) + lstrlenW(actiondata) +
159 lstrlenW(usersid) + lstrlenW(prodcode) +
160 lstrlenW(format) - 7;
161 deferred = msi_alloc(len * sizeof(WCHAR));
163 sprintfW(deferred, format, actiondata, usersid, prodcode, action);
164 return deferred;
167 static void set_deferred_action_props(MSIPACKAGE *package, LPWSTR deferred_data)
169 LPWSTR end, beg = deferred_data + 1;
171 static const WCHAR sep[] = {'<','=','>',0};
173 end = strstrW(beg, sep);
174 *end = '\0';
175 MSI_SetPropertyW(package, szActionData, beg);
176 beg = end + 3;
178 end = strstrW(beg, sep);
179 *end = '\0';
180 MSI_SetPropertyW(package, UserSID, beg);
181 beg = end + 3;
183 end = strchrW(beg, ']');
184 *end = '\0';
185 MSI_SetPropertyW(package, ProdCode, beg);
188 UINT ACTION_CustomAction(MSIPACKAGE *package, LPCWSTR action, UINT script, BOOL execute)
190 UINT rc = ERROR_SUCCESS;
191 MSIRECORD * row = 0;
192 static const WCHAR ExecSeqQuery[] =
193 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
194 '`','C','u','s','t','o' ,'m','A','c','t','i','o','n','`',
195 ' ','W','H','E','R','E',' ','`','A','c','t','i' ,'o','n','`',' ',
196 '=',' ','\'','%','s','\'',0};
197 UINT type;
198 LPCWSTR source, target;
199 LPWSTR ptr, deferred_data = NULL;
200 LPWSTR action_copy = strdupW(action);
201 WCHAR *deformated=NULL;
203 /* deferred action: [properties]Action */
204 if ((ptr = strrchrW(action_copy, ']')))
206 deferred_data = action_copy;
207 action = ptr + 1;
210 row = MSI_QueryGetRecord( package->db, ExecSeqQuery, action );
211 if (!row)
213 msi_free(action_copy);
214 return ERROR_CALL_NOT_IMPLEMENTED;
217 type = MSI_RecordGetInteger(row,2);
219 source = MSI_RecordGetString(row,3);
220 target = MSI_RecordGetString(row,4);
222 TRACE("Handling custom action %s (%x %s %s)\n",debugstr_w(action),type,
223 debugstr_w(source), debugstr_w(target));
225 /* handle some of the deferred actions */
226 if (type & msidbCustomActionTypeTSAware)
227 FIXME("msidbCustomActionTypeTSAware not handled\n");
229 if (type & msidbCustomActionTypeInScript)
231 if (type & msidbCustomActionTypeNoImpersonate)
232 WARN("msidbCustomActionTypeNoImpersonate not handled\n");
234 if (type & msidbCustomActionTypeRollback)
236 FIXME("Rollback only action... rollbacks not supported yet\n");
237 schedule_action(package, ROLLBACK_SCRIPT, action);
238 rc = ERROR_SUCCESS;
239 goto end;
241 if (!execute)
243 LPWSTR actiondata = msi_dup_property(package, action);
244 LPWSTR usersid = msi_dup_property(package, UserSID);
245 LPWSTR prodcode = msi_dup_property(package, ProdCode);
246 LPWSTR deferred = msi_get_deferred_action(action, actiondata, usersid, prodcode);
248 if (type & msidbCustomActionTypeCommit)
250 TRACE("Deferring Commit Action!\n");
251 schedule_action(package, COMMIT_SCRIPT, deferred);
253 else
255 TRACE("Deferring Action!\n");
256 schedule_action(package, INSTALL_SCRIPT, deferred);
259 rc = ERROR_SUCCESS;
260 msi_free(actiondata);
261 msi_free(usersid);
262 msi_free(prodcode);
263 msi_free(deferred);
264 goto end;
266 else
268 static const WCHAR szBlank[] = {0};
270 LPWSTR actiondata = msi_dup_property( package, action );
272 switch (script)
274 case INSTALL_SCRIPT:
275 package->scheduled_action_running = TRUE;
276 break;
277 case COMMIT_SCRIPT:
278 package->commit_action_running = TRUE;
279 break;
280 case ROLLBACK_SCRIPT:
281 package->rollback_action_running = TRUE;
282 break;
283 default:
284 break;
287 if (deferred_data)
288 set_deferred_action_props(package, deferred_data);
289 else if (actiondata)
290 MSI_SetPropertyW(package,szActionData,actiondata);
291 else
292 MSI_SetPropertyW(package,szActionData,szBlank);
294 msi_free(actiondata);
297 else if (!check_execution_scheduling_options(package,action,type))
299 rc = ERROR_SUCCESS;
300 goto end;
303 switch (type & CUSTOM_ACTION_TYPE_MASK)
305 case 1: /* DLL file stored in a Binary table stream */
306 rc = HANDLE_CustomType1(package,source,target,type,action);
307 break;
308 case 2: /* EXE file stored in a Binary table stream */
309 rc = HANDLE_CustomType2(package,source,target,type,action);
310 break;
311 case 18: /*EXE file installed with package */
312 rc = HANDLE_CustomType18(package,source,target,type,action);
313 break;
314 case 19: /* Error that halts install */
315 rc = HANDLE_CustomType19(package,source,target,type,action);
316 break;
317 case 17:
318 rc = HANDLE_CustomType17(package,source,target,type,action);
319 break;
320 case 23: /* installs another package in the source tree */
321 deformat_string(package,target,&deformated);
322 rc = HANDLE_CustomType23(package,source,deformated,type,action);
323 msi_free(deformated);
324 break;
325 case 50: /*EXE file specified by a property value */
326 rc = HANDLE_CustomType50(package,source,target,type,action);
327 break;
328 case 34: /*EXE to be run in specified directory */
329 rc = HANDLE_CustomType34(package,source,target,type,action);
330 break;
331 case 35: /* Directory set with formatted text. */
332 deformat_string(package,target,&deformated);
333 MSI_SetTargetPathW(package, source, deformated);
334 msi_free(deformated);
335 break;
336 case 51: /* Property set with formatted text. */
337 if (!source)
338 break;
340 deformat_string(package,target,&deformated);
341 rc = MSI_SetPropertyW(package,source,deformated);
342 msi_free(deformated);
343 break;
344 case 37: /* JScript/VBScript text stored in target column. */
345 case 38:
346 rc = HANDLE_CustomType37_38(package,source,target,type,action);
347 break;
348 case 5:
349 case 6: /* JScript/VBScript file stored in a Binary table stream. */
350 rc = HANDLE_CustomType5_6(package,source,target,type,action);
351 break;
352 case 21: /* JScript/VBScript file installed with the product. */
353 case 22:
354 rc = HANDLE_CustomType21_22(package,source,target,type,action);
355 break;
356 case 53: /* JScript/VBScript text specified by a property value. */
357 case 54:
358 rc = HANDLE_CustomType53_54(package,source,target,type,action);
359 break;
360 default:
361 FIXME("UNHANDLED ACTION TYPE %i (%s %s)\n",
362 type & CUSTOM_ACTION_TYPE_MASK, debugstr_w(source),
363 debugstr_w(target));
366 end:
367 package->scheduled_action_running = FALSE;
368 package->commit_action_running = FALSE;
369 package->rollback_action_running = FALSE;
370 msi_free(action_copy);
371 msiobj_release(&row->hdr);
372 return rc;
376 static UINT store_binary_to_temp(MSIPACKAGE *package, LPCWSTR source,
377 LPWSTR tmp_file)
379 static const WCHAR query[] = {
380 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
381 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',' ',
382 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
383 MSIRECORD *row = 0;
384 HANDLE file;
385 CHAR buffer[1024];
386 static const WCHAR f1[] = {'m','s','i',0};
387 WCHAR fmt[MAX_PATH];
388 DWORD sz = MAX_PATH;
389 UINT r;
391 if (MSI_GetPropertyW(package, cszTempFolder, fmt, &sz) != ERROR_SUCCESS)
392 GetTempPathW(MAX_PATH, fmt);
394 if (GetTempFileNameW(fmt, f1, 0, tmp_file) == 0)
396 TRACE("Unable to create file\n");
397 return ERROR_FUNCTION_FAILED;
399 track_tempfile(package, tmp_file);
401 row = MSI_QueryGetRecord(package->db, query, source);
402 if (!row)
403 return ERROR_FUNCTION_FAILED;
405 /* write out the file */
406 file = CreateFileW(tmp_file, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
407 FILE_ATTRIBUTE_NORMAL, NULL);
408 if (file == INVALID_HANDLE_VALUE)
409 r = ERROR_FUNCTION_FAILED;
410 else
414 DWORD write;
415 sz = sizeof buffer;
416 r = MSI_RecordReadStream(row, 2, buffer, &sz);
417 if (r != ERROR_SUCCESS)
419 ERR("Failed to get stream\n");
420 break;
422 WriteFile(file, buffer, sz, &write, NULL);
423 } while (sz == sizeof buffer);
424 CloseHandle(file);
427 msiobj_release(&row->hdr);
429 return r;
432 static void file_running_action(MSIPACKAGE* package, HANDLE Handle,
433 BOOL process, LPCWSTR name)
435 MSIRUNNINGACTION *action;
437 action = msi_alloc( sizeof(MSIRUNNINGACTION) );
439 action->handle = Handle;
440 action->process = process;
441 action->name = strdupW(name);
443 list_add_tail( &package->RunningActions, &action->entry );
446 static UINT custom_get_process_return( HANDLE process )
448 DWORD rc = 0;
450 GetExitCodeProcess( process, &rc );
451 if (rc != 0)
452 return ERROR_FUNCTION_FAILED;
453 return ERROR_SUCCESS;
456 static UINT custom_get_thread_return( MSIPACKAGE *package, HANDLE thread )
458 DWORD rc = 0;
460 GetExitCodeThread( thread, &rc );
462 switch (rc)
464 case ERROR_FUNCTION_NOT_CALLED:
465 case ERROR_SUCCESS:
466 case ERROR_INSTALL_USEREXIT:
467 case ERROR_INSTALL_FAILURE:
468 return rc;
469 case ERROR_NO_MORE_ITEMS:
470 return ERROR_SUCCESS;
471 case ERROR_INSTALL_SUSPEND:
472 ACTION_ForceReboot( package );
473 return ERROR_SUCCESS;
474 default:
475 ERR("Invalid Return Code %d\n",rc);
476 return ERROR_INSTALL_FAILURE;
480 static UINT wait_process_handle(MSIPACKAGE* package, UINT type,
481 HANDLE ProcessHandle, LPCWSTR name)
483 UINT rc = ERROR_SUCCESS;
485 if (!(type & msidbCustomActionTypeAsync))
487 TRACE("waiting for %s\n", debugstr_w(name));
489 msi_dialog_check_messages(ProcessHandle);
491 if (!(type & msidbCustomActionTypeContinue))
492 rc = custom_get_process_return(ProcessHandle);
494 CloseHandle(ProcessHandle);
496 else
498 TRACE("%s running in background\n", debugstr_w(name));
500 if (!(type & msidbCustomActionTypeContinue))
501 file_running_action(package, ProcessHandle, TRUE, name);
502 else
503 CloseHandle(ProcessHandle);
506 return rc;
509 typedef struct _msi_custom_action_info {
510 struct list entry;
511 LONG refs;
512 MSIPACKAGE *package;
513 LPWSTR source;
514 LPWSTR target;
515 HANDLE handle;
516 LPWSTR action;
517 INT type;
518 GUID guid;
519 } msi_custom_action_info;
521 static void release_custom_action_data( msi_custom_action_info *info )
523 EnterCriticalSection( &msi_custom_action_cs );
525 if (!--info->refs)
527 list_remove( &info->entry );
528 if (info->handle)
529 CloseHandle( info->handle );
530 msi_free( info->action );
531 msi_free( info->source );
532 msi_free( info->target );
533 msiobj_release( &info->package->hdr );
534 msi_free( info );
537 LeaveCriticalSection( &msi_custom_action_cs );
540 /* must be called inside msi_custom_action_cs if info is in the pending custom actions list */
541 static void addref_custom_action_data( msi_custom_action_info *info )
543 info->refs++;
546 static UINT wait_thread_handle( msi_custom_action_info *info )
548 UINT rc = ERROR_SUCCESS;
550 if (!(info->type & msidbCustomActionTypeAsync))
552 TRACE("waiting for %s\n", debugstr_w( info->action ));
554 msi_dialog_check_messages( info->handle );
556 if (!(info->type & msidbCustomActionTypeContinue))
557 rc = custom_get_thread_return( info->package, info->handle );
559 release_custom_action_data( info );
561 else
563 TRACE("%s running in background\n", debugstr_w( info->action ));
566 return rc;
569 static msi_custom_action_info *find_action_by_guid( const GUID *guid )
571 msi_custom_action_info *info;
572 BOOL found = FALSE;
574 EnterCriticalSection( &msi_custom_action_cs );
576 LIST_FOR_EACH_ENTRY( info, &msi_pending_custom_actions, msi_custom_action_info, entry )
578 if (IsEqualGUID( &info->guid, guid ))
580 addref_custom_action_data( info );
581 found = TRUE;
582 break;
586 LeaveCriticalSection( &msi_custom_action_cs );
588 if (!found)
589 return NULL;
591 return info;
594 static void handle_msi_break( LPCWSTR target )
596 LPWSTR msg;
597 WCHAR val[MAX_PATH];
599 static const WCHAR MsiBreak[] = { 'M','s','i','B','r','e','a','k',0 };
600 static const WCHAR WindowsInstaller[] = {
601 'W','i','n','d','o','w','s',' ','I','n','s','t','a','l','l','e','r',0
604 static const WCHAR format[] = {
605 'T','o',' ','d','e','b','u','g',' ','y','o','u','r',' ',
606 'c','u','s','t','o','m',' ','a','c','t','i','o','n',',',' ',
607 'a','t','t','a','c','h',' ','y','o','u','r',' ','d','e','b','u','g','g','e','r',' ',
608 't','o',' ','p','r','o','c','e','s','s',' ','%','i',' ','(','0','x','%','X',')',' ',
609 'a','n','d',' ','p','r','e','s','s',' ','O','K',0
612 if( !GetEnvironmentVariableW( MsiBreak, val, MAX_PATH ))
613 return;
615 if( lstrcmpiW( val, target ))
616 return;
618 msg = msi_alloc( (lstrlenW(format) + 10) * sizeof(WCHAR) );
619 if (!msg)
620 return;
622 wsprintfW( msg, format, GetCurrentProcessId(), GetCurrentProcessId());
623 MessageBoxW( NULL, msg, WindowsInstaller, MB_OK);
624 msi_free(msg);
625 DebugBreak();
628 static UINT get_action_info( const GUID *guid, INT *type, MSIHANDLE *handle,
629 BSTR *dll, BSTR *funcname,
630 IWineMsiRemotePackage **package )
632 IClassFactory *cf = NULL;
633 IWineMsiRemoteCustomAction *rca = NULL;
634 HRESULT r;
636 r = DllGetClassObject( &CLSID_IWineMsiRemoteCustomAction,
637 &IID_IClassFactory, (LPVOID *)&cf );
638 if (FAILED(r))
640 ERR("failed to get IClassFactory interface\n");
641 return ERROR_FUNCTION_FAILED;
644 r = IClassFactory_CreateInstance( cf, NULL, &IID_IWineMsiRemoteCustomAction, (LPVOID *)&rca );
645 if (FAILED(r))
647 ERR("failed to get IWineMsiRemoteCustomAction interface\n");
648 return ERROR_FUNCTION_FAILED;
651 r = IWineMsiRemoteCustomAction_GetActionInfo( rca, guid, type, handle, dll, funcname, package );
652 IWineMsiRemoteCustomAction_Release( rca );
653 if (FAILED(r))
655 ERR("GetActionInfo failed\n");
656 return ERROR_FUNCTION_FAILED;
659 return ERROR_SUCCESS;
662 static DWORD ACTION_CallDllFunction( const GUID *guid )
664 MsiCustomActionEntryPoint fn;
665 MSIHANDLE hPackage, handle;
666 HANDLE hModule;
667 LPSTR proc;
668 UINT r = ERROR_FUNCTION_FAILED;
669 BSTR dll = NULL, function = NULL;
670 INT type;
671 IWineMsiRemotePackage *remote_package = NULL;
673 TRACE("%s\n", debugstr_guid( guid ));
675 r = get_action_info( guid, &type, &handle, &dll, &function, &remote_package );
676 if (r != ERROR_SUCCESS)
677 return r;
679 hModule = LoadLibraryW( dll );
680 if (!hModule)
682 ERR("failed to load dll %s\n", debugstr_w( dll ) );
683 return r;
686 proc = strdupWtoA( function );
687 fn = (MsiCustomActionEntryPoint) GetProcAddress( hModule, proc );
688 msi_free( proc );
689 if (fn)
691 hPackage = alloc_msi_remote_handle( (IUnknown *)remote_package );
692 if (hPackage)
694 IWineMsiRemotePackage_SetMsiHandle( remote_package, handle );
695 TRACE("calling %s\n", debugstr_w( function ) );
696 handle_msi_break( function );
698 CoInitialize(NULL);
700 __TRY
702 r = fn( hPackage );
704 __EXCEPT_PAGE_FAULT
706 ERR("Custom action (%s:%s) caused a page fault: %08x\n",
707 debugstr_w(dll), debugstr_w(function), GetExceptionCode());
708 r = ERROR_SUCCESS;
710 __ENDTRY;
712 CoUninitialize();
714 MsiCloseHandle( hPackage );
716 else
717 ERR("failed to create handle for %p\n", remote_package );
719 else
720 ERR("GetProcAddress(%s) failed\n", debugstr_w( function ) );
722 FreeLibrary(hModule);
724 IWineMsiRemotePackage_Release( remote_package );
725 SysFreeString( dll );
726 SysFreeString( function );
727 MsiCloseHandle( handle );
729 return r;
732 static DWORD WINAPI DllThread( LPVOID arg )
734 LPGUID guid = arg;
735 DWORD rc = 0;
737 TRACE("custom action (%x) started\n", GetCurrentThreadId() );
739 rc = ACTION_CallDllFunction( guid );
741 TRACE("custom action (%x) returned %i\n", GetCurrentThreadId(), rc );
743 MsiCloseAllHandles();
744 return rc;
747 static DWORD ACTION_CAInstallPackage(const GUID *guid)
749 msi_custom_action_info *info;
750 UINT r = ERROR_FUNCTION_FAILED;
751 INSTALLUILEVEL old_level;
753 info = find_action_by_guid(guid);
754 if (!info)
756 ERR("failed to find action %s\n", debugstr_guid(guid));
757 return r;
760 old_level = MsiSetInternalUI(INSTALLUILEVEL_BASIC, NULL);
761 r = MsiInstallProductW(info->source, info->target);
762 MsiSetInternalUI(old_level, NULL);
764 release_custom_action_data(info);
766 return r;
769 static DWORD WINAPI ConcurrentInstallThread(LPVOID arg)
771 LPGUID guid = arg;
772 DWORD rc;
774 TRACE("concurrent installation (%x) started\n", GetCurrentThreadId());
776 rc = ACTION_CAInstallPackage(guid);
778 TRACE("concurrent installation (%x) returned %i\n", GetCurrentThreadId(), rc);
780 MsiCloseAllHandles();
781 return rc;
784 static msi_custom_action_info *do_msidbCustomActionTypeDll(
785 MSIPACKAGE *package, INT type, LPCWSTR source, LPCWSTR target, LPCWSTR action )
787 msi_custom_action_info *info;
789 info = msi_alloc( sizeof *info );
790 if (!info)
791 return NULL;
793 msiobj_addref( &package->hdr );
794 info->refs = 2; /* 1 for our caller and 1 for thread we created */
795 info->package = package;
796 info->type = type;
797 info->target = strdupW( target );
798 info->source = strdupW( source );
799 info->action = strdupW( action );
800 CoCreateGuid( &info->guid );
802 EnterCriticalSection( &msi_custom_action_cs );
803 list_add_tail( &msi_pending_custom_actions, &info->entry );
804 LeaveCriticalSection( &msi_custom_action_cs );
806 info->handle = CreateThread( NULL, 0, DllThread, &info->guid, 0, NULL );
807 if (!info->handle)
809 /* release both references */
810 release_custom_action_data( info );
811 release_custom_action_data( info );
812 return NULL;
815 return info;
818 static msi_custom_action_info *do_msidbCAConcurrentInstall(
819 MSIPACKAGE *package, INT type, LPCWSTR source, LPCWSTR target, LPCWSTR action)
821 msi_custom_action_info *info;
823 info = msi_alloc( sizeof *info );
824 if (!info)
825 return NULL;
827 msiobj_addref( &package->hdr );
828 info->refs = 2; /* 1 for our caller and 1 for thread we created */
829 info->package = package;
830 info->type = type;
831 info->target = strdupW( target );
832 info->source = strdupW( source );
833 info->action = strdupW( action );
834 CoCreateGuid( &info->guid );
836 EnterCriticalSection( &msi_custom_action_cs );
837 list_add_tail( &msi_pending_custom_actions, &info->entry );
838 LeaveCriticalSection( &msi_custom_action_cs );
840 info->handle = CreateThread( NULL, 0, ConcurrentInstallThread, &info->guid, 0, NULL );
841 if (!info->handle)
843 /* release both references */
844 release_custom_action_data( info );
845 release_custom_action_data( info );
846 return NULL;
849 return info;
852 static UINT HANDLE_CustomType23(MSIPACKAGE *package, LPCWSTR source,
853 LPCWSTR target, const INT type, LPCWSTR action)
855 msi_custom_action_info *info;
856 WCHAR package_path[MAX_PATH];
857 DWORD size;
859 static const WCHAR backslash[] = {'\\',0};
861 size = MAX_PATH;
862 MSI_GetPropertyW(package, cszSourceDir, package_path, &size);
863 lstrcatW(package_path, backslash);
864 lstrcatW(package_path, source);
866 TRACE("Installing package %s concurrently\n", debugstr_w(package_path));
868 info = do_msidbCAConcurrentInstall(package, type, package_path, target, action);
870 return wait_thread_handle(info);
873 static UINT HANDLE_CustomType1(MSIPACKAGE *package, LPCWSTR source,
874 LPCWSTR target, const INT type, LPCWSTR action)
876 msi_custom_action_info *info;
877 WCHAR tmp_file[MAX_PATH];
878 UINT r;
880 r = store_binary_to_temp(package, source, tmp_file);
881 if (r != ERROR_SUCCESS)
882 return r;
884 TRACE("Calling function %s from %s\n",debugstr_w(target),
885 debugstr_w(tmp_file));
887 if (!strchrW(tmp_file,'.'))
889 static const WCHAR dot[]={'.',0};
890 strcatW(tmp_file,dot);
893 info = do_msidbCustomActionTypeDll( package, type, tmp_file, target, action );
895 return wait_thread_handle( info );
898 static UINT HANDLE_CustomType2(MSIPACKAGE *package, LPCWSTR source,
899 LPCWSTR target, const INT type, LPCWSTR action)
901 WCHAR tmp_file[MAX_PATH];
902 STARTUPINFOW si;
903 PROCESS_INFORMATION info;
904 BOOL rc;
905 INT len;
906 WCHAR *deformated = NULL;
907 WCHAR *cmd;
908 static const WCHAR spc[] = {' ',0};
909 UINT r;
911 memset(&si,0,sizeof(STARTUPINFOW));
913 r = store_binary_to_temp(package, source, tmp_file);
914 if (r != ERROR_SUCCESS)
915 return r;
917 deformat_string(package,target,&deformated);
919 len = strlenW(tmp_file)+2;
921 if (deformated)
922 len += strlenW(deformated);
924 cmd = msi_alloc(sizeof(WCHAR)*len);
926 strcpyW(cmd,tmp_file);
927 if (deformated)
929 strcatW(cmd,spc);
930 strcatW(cmd,deformated);
932 msi_free(deformated);
935 TRACE("executing exe %s\n", debugstr_w(cmd));
937 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
938 c_collen, &si, &info);
939 msi_free(cmd);
941 if ( !rc )
943 ERR("Unable to execute command %s\n", debugstr_w(cmd));
944 return ERROR_SUCCESS;
946 CloseHandle( info.hThread );
948 r = wait_process_handle(package, type, info.hProcess, action);
950 return r;
953 static UINT HANDLE_CustomType17(MSIPACKAGE *package, LPCWSTR source,
954 LPCWSTR target, const INT type, LPCWSTR action)
956 msi_custom_action_info *info;
957 MSIFILE *file;
959 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
961 file = get_loaded_file( package, source );
962 if (!file)
964 ERR("invalid file key %s\n", debugstr_w( source ));
965 return ERROR_FUNCTION_FAILED;
968 info = do_msidbCustomActionTypeDll( package, type, file->TargetPath, target, action );
970 return wait_thread_handle( info );
973 static UINT HANDLE_CustomType18(MSIPACKAGE *package, LPCWSTR source,
974 LPCWSTR target, const INT type, LPCWSTR action)
976 STARTUPINFOW si;
977 PROCESS_INFORMATION info;
978 BOOL rc;
979 WCHAR *deformated;
980 WCHAR *cmd;
981 INT len;
982 static const WCHAR spc[] = {' ',0};
983 MSIFILE *file;
985 memset(&si,0,sizeof(STARTUPINFOW));
987 file = get_loaded_file(package,source);
988 if( !file )
989 return ERROR_FUNCTION_FAILED;
991 len = lstrlenW( file->TargetPath );
993 deformat_string(package,target,&deformated);
994 if (deformated)
995 len += strlenW(deformated);
996 len += 2;
998 cmd = msi_alloc(len * sizeof(WCHAR));
1000 lstrcpyW( cmd, file->TargetPath);
1001 if (deformated)
1003 strcatW(cmd, spc);
1004 strcatW(cmd, deformated);
1006 msi_free(deformated);
1009 TRACE("executing exe %s\n", debugstr_w(cmd));
1011 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
1012 c_collen, &si, &info);
1014 if ( !rc )
1016 ERR("Unable to execute command %s\n", debugstr_w(cmd));
1017 msi_free(cmd);
1018 return ERROR_SUCCESS;
1020 msi_free(cmd);
1021 CloseHandle( info.hThread );
1023 return wait_process_handle(package, type, info.hProcess, action);
1026 static UINT HANDLE_CustomType19(MSIPACKAGE *package, LPCWSTR source,
1027 LPCWSTR target, const INT type, LPCWSTR action)
1029 static const WCHAR query[] = {
1030 'S','E','L','E','C','T',' ','`','M','e','s','s','a','g','e','`',' ',
1031 'F','R','O','M',' ','`','E','r','r','o','r','`',' ',
1032 'W','H','E','R','E',' ','`','E','r','r','o','r','`',' ','=',' ',
1033 '%','s',0
1035 MSIRECORD *row = 0;
1036 LPWSTR deformated = NULL;
1038 deformat_string( package, target, &deformated );
1040 /* first try treat the error as a number */
1041 row = MSI_QueryGetRecord( package->db, query, deformated );
1042 if( row )
1044 LPCWSTR error = MSI_RecordGetString( row, 1 );
1045 MessageBoxW( NULL, error, NULL, MB_OK );
1046 msiobj_release( &row->hdr );
1048 else
1049 MessageBoxW( NULL, deformated, NULL, MB_OK );
1051 msi_free( deformated );
1053 return ERROR_FUNCTION_FAILED;
1056 static UINT HANDLE_CustomType50(MSIPACKAGE *package, LPCWSTR source,
1057 LPCWSTR target, const INT type, LPCWSTR action)
1059 STARTUPINFOW si;
1060 PROCESS_INFORMATION info;
1061 WCHAR *prop;
1062 BOOL rc;
1063 WCHAR *deformated;
1064 WCHAR *cmd;
1065 INT len;
1066 static const WCHAR spc[] = {' ',0};
1068 memset(&si,0,sizeof(STARTUPINFOW));
1069 memset(&info,0,sizeof(PROCESS_INFORMATION));
1071 prop = msi_dup_property( package, source );
1072 if (!prop)
1073 return ERROR_SUCCESS;
1075 deformat_string(package,target,&deformated);
1076 len = strlenW(prop) + 2;
1077 if (deformated)
1078 len += strlenW(deformated);
1080 cmd = msi_alloc(sizeof(WCHAR)*len);
1082 strcpyW(cmd,prop);
1083 if (deformated)
1085 strcatW(cmd,spc);
1086 strcatW(cmd,deformated);
1088 msi_free(deformated);
1090 msi_free(prop);
1092 TRACE("executing exe %s\n", debugstr_w(cmd));
1094 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
1095 c_collen, &si, &info);
1097 if ( !rc )
1099 ERR("Unable to execute command %s\n", debugstr_w(cmd));
1100 msi_free(cmd);
1101 return ERROR_SUCCESS;
1103 msi_free(cmd);
1105 CloseHandle( info.hThread );
1107 return wait_process_handle(package, type, info.hProcess, action);
1110 static UINT HANDLE_CustomType34(MSIPACKAGE *package, LPCWSTR source,
1111 LPCWSTR target, const INT type, LPCWSTR action)
1113 LPWSTR filename, deformated;
1114 STARTUPINFOW si;
1115 PROCESS_INFORMATION info;
1116 BOOL rc;
1118 memset(&si,0,sizeof(STARTUPINFOW));
1120 filename = resolve_folder(package, source, FALSE, FALSE, TRUE, NULL);
1122 if (!filename)
1123 return ERROR_FUNCTION_FAILED;
1125 SetCurrentDirectoryW(filename);
1126 msi_free(filename);
1128 deformat_string(package,target,&deformated);
1130 if (!deformated)
1131 return ERROR_FUNCTION_FAILED;
1133 TRACE("executing exe %s\n", debugstr_w(deformated));
1135 rc = CreateProcessW(NULL, deformated, NULL, NULL, FALSE, 0, NULL,
1136 c_collen, &si, &info);
1138 if ( !rc )
1140 ERR("Unable to execute command %s\n", debugstr_w(deformated));
1141 msi_free(deformated);
1142 return ERROR_SUCCESS;
1144 msi_free(deformated);
1145 CloseHandle( info.hThread );
1147 return wait_process_handle(package, type, info.hProcess, action);
1150 static DWORD ACTION_CallScript( const GUID *guid )
1152 msi_custom_action_info *info;
1153 MSIHANDLE hPackage;
1154 UINT r = ERROR_FUNCTION_FAILED;
1156 info = find_action_by_guid( guid );
1157 if (!info)
1159 ERR("failed to find action %s\n", debugstr_guid( guid) );
1160 return r;
1163 TRACE("function %s, script %s\n", debugstr_w( info->target ), debugstr_w( info->source ) );
1165 hPackage = alloc_msihandle( &info->package->hdr );
1166 if (hPackage)
1168 r = call_script( hPackage, info->type, info->source, info->target, info->action );
1169 MsiCloseHandle( hPackage );
1171 else
1172 ERR("failed to create handle for %p\n", info->package );
1174 if (info->type & msidbCustomActionTypeAsync &&
1175 info->type & msidbCustomActionTypeContinue)
1176 release_custom_action_data( info );
1178 return S_OK;
1181 static DWORD WINAPI ScriptThread( LPVOID arg )
1183 LPGUID guid = arg;
1184 DWORD rc = 0;
1186 TRACE("custom action (%x) started\n", GetCurrentThreadId() );
1188 rc = ACTION_CallScript( guid );
1190 TRACE("custom action (%x) returned %i\n", GetCurrentThreadId(), rc );
1192 MsiCloseAllHandles();
1193 return rc;
1196 static msi_custom_action_info *do_msidbCustomActionTypeScript(
1197 MSIPACKAGE *package, INT type, LPCWSTR script, LPCWSTR function, LPCWSTR action )
1199 msi_custom_action_info *info;
1201 info = msi_alloc( sizeof *info );
1202 if (!info)
1203 return NULL;
1205 msiobj_addref( &package->hdr );
1206 info->refs = 2; /* 1 for our caller and 1 for thread we created */
1207 info->package = package;
1208 info->type = type;
1209 info->target = strdupW( function );
1210 info->source = strdupW( script );
1211 info->action = strdupW( action );
1212 CoCreateGuid( &info->guid );
1214 EnterCriticalSection( &msi_custom_action_cs );
1215 list_add_tail( &msi_pending_custom_actions, &info->entry );
1216 LeaveCriticalSection( &msi_custom_action_cs );
1218 info->handle = CreateThread( NULL, 0, ScriptThread, &info->guid, 0, NULL );
1219 if (!info->handle)
1221 /* release both references */
1222 release_custom_action_data( info );
1223 release_custom_action_data( info );
1224 return NULL;
1227 return info;
1230 static UINT HANDLE_CustomType37_38(MSIPACKAGE *package, LPCWSTR source,
1231 LPCWSTR target, const INT type, LPCWSTR action)
1233 msi_custom_action_info *info;
1235 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1237 info = do_msidbCustomActionTypeScript( package, type, target, NULL, action );
1239 return wait_thread_handle( info );
1242 static UINT HANDLE_CustomType5_6(MSIPACKAGE *package, LPCWSTR source,
1243 LPCWSTR target, const INT type, LPCWSTR action)
1245 static const WCHAR query[] = {
1246 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1247 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',' ',
1248 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
1249 MSIRECORD *row = 0;
1250 msi_custom_action_info *info;
1251 CHAR *buffer = NULL;
1252 WCHAR *bufferw = NULL;
1253 DWORD sz = 0;
1254 UINT r;
1256 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1258 row = MSI_QueryGetRecord(package->db, query, source);
1259 if (!row)
1260 return ERROR_FUNCTION_FAILED;
1262 r = MSI_RecordReadStream(row, 2, NULL, &sz);
1263 if (r != ERROR_SUCCESS)
1264 return r;
1266 buffer = msi_alloc(sizeof(CHAR)*(sz+1));
1267 if (!buffer)
1268 return ERROR_FUNCTION_FAILED;
1270 r = MSI_RecordReadStream(row, 2, buffer, &sz);
1271 if (r != ERROR_SUCCESS)
1272 goto done;
1274 buffer[sz] = 0;
1275 bufferw = strdupAtoW(buffer);
1276 if (!bufferw)
1278 r = ERROR_FUNCTION_FAILED;
1279 goto done;
1282 info = do_msidbCustomActionTypeScript( package, type, bufferw, target, action );
1283 r = wait_thread_handle( info );
1285 done:
1286 msi_free(bufferw);
1287 msi_free(buffer);
1288 return r;
1291 static UINT HANDLE_CustomType21_22(MSIPACKAGE *package, LPCWSTR source,
1292 LPCWSTR target, const INT type, LPCWSTR action)
1294 msi_custom_action_info *info;
1295 MSIFILE *file;
1296 HANDLE hFile;
1297 DWORD sz, szHighWord = 0, read;
1298 CHAR *buffer=NULL;
1299 WCHAR *bufferw=NULL;
1300 BOOL bRet;
1301 UINT r;
1303 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1305 file = get_loaded_file(package,source);
1306 if (!file)
1308 ERR("invalid file key %s\n", debugstr_w(source));
1309 return ERROR_FUNCTION_FAILED;
1312 hFile = CreateFileW(file->TargetPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
1313 if (hFile == INVALID_HANDLE_VALUE)
1314 return ERROR_FUNCTION_FAILED;
1316 sz = GetFileSize(hFile, &szHighWord);
1317 if (sz == INVALID_FILE_SIZE || szHighWord != 0)
1319 CloseHandle(hFile);
1320 return ERROR_FUNCTION_FAILED;
1323 buffer = msi_alloc(sizeof(CHAR)*(sz+1));
1324 if (!buffer)
1326 CloseHandle(hFile);
1327 return ERROR_FUNCTION_FAILED;
1330 bRet = ReadFile(hFile, buffer, sz, &read, NULL);
1331 CloseHandle(hFile);
1332 if (!bRet)
1334 r = ERROR_FUNCTION_FAILED;
1335 goto done;
1338 buffer[read] = 0;
1339 bufferw = strdupAtoW(buffer);
1340 if (!bufferw)
1342 r = ERROR_FUNCTION_FAILED;
1343 goto done;
1346 info = do_msidbCustomActionTypeScript( package, type, bufferw, target, action );
1347 r = wait_thread_handle( info );
1349 done:
1350 msi_free(bufferw);
1351 msi_free(buffer);
1352 return r;
1355 static UINT HANDLE_CustomType53_54(MSIPACKAGE *package, LPCWSTR source,
1356 LPCWSTR target, const INT type, LPCWSTR action)
1358 msi_custom_action_info *info;
1359 WCHAR *prop;
1361 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1363 prop = msi_dup_property(package,source);
1364 if (!prop)
1365 return ERROR_SUCCESS;
1367 info = do_msidbCustomActionTypeScript( package, type, prop, NULL, action );
1368 msi_free(prop);
1369 return wait_thread_handle( info );
1372 void ACTION_FinishCustomActions(const MSIPACKAGE* package)
1374 struct list *item;
1375 HANDLE *wait_handles;
1376 unsigned int handle_count, i;
1377 msi_custom_action_info *info, *cursor;
1379 while ((item = list_head( &package->RunningActions )))
1381 MSIRUNNINGACTION *action = LIST_ENTRY( item, MSIRUNNINGACTION, entry );
1383 list_remove( &action->entry );
1385 TRACE("waiting for %s\n", debugstr_w( action->name ) );
1386 msi_dialog_check_messages( action->handle );
1388 CloseHandle( action->handle );
1389 msi_free( action->name );
1390 msi_free( action );
1393 EnterCriticalSection( &msi_custom_action_cs );
1395 handle_count = list_count( &msi_pending_custom_actions );
1396 wait_handles = HeapAlloc( GetProcessHeap(), 0, handle_count * sizeof(HANDLE) );
1398 handle_count = 0;
1399 LIST_FOR_EACH_ENTRY_SAFE( info, cursor, &msi_pending_custom_actions, msi_custom_action_info, entry )
1401 if (info->package == package )
1403 if (DuplicateHandle(GetCurrentProcess(), info->handle, GetCurrentProcess(), &wait_handles[handle_count], SYNCHRONIZE, FALSE, 0))
1404 handle_count++;
1408 LeaveCriticalSection( &msi_custom_action_cs );
1410 for (i = 0; i < handle_count; i++)
1412 msi_dialog_check_messages( wait_handles[i] );
1413 CloseHandle( wait_handles[i] );
1416 HeapFree( GetProcessHeap(), 0, wait_handles );
1419 typedef struct _msi_custom_remote_impl {
1420 const IWineMsiRemoteCustomActionVtbl *lpVtbl;
1421 LONG refs;
1422 } msi_custom_remote_impl;
1424 static inline msi_custom_remote_impl* mcr_from_IWineMsiRemoteCustomAction( IWineMsiRemoteCustomAction* iface )
1426 return (msi_custom_remote_impl*) iface;
1429 static HRESULT WINAPI mcr_QueryInterface( IWineMsiRemoteCustomAction *iface,
1430 REFIID riid,LPVOID *ppobj)
1432 if( IsEqualCLSID( riid, &IID_IUnknown ) ||
1433 IsEqualCLSID( riid, &IID_IWineMsiRemoteCustomAction ) )
1435 IUnknown_AddRef( iface );
1436 *ppobj = iface;
1437 return S_OK;
1440 return E_NOINTERFACE;
1443 static ULONG WINAPI mcr_AddRef( IWineMsiRemoteCustomAction *iface )
1445 msi_custom_remote_impl* This = mcr_from_IWineMsiRemoteCustomAction( iface );
1447 return InterlockedIncrement( &This->refs );
1450 static ULONG WINAPI mcr_Release( IWineMsiRemoteCustomAction *iface )
1452 msi_custom_remote_impl* This = mcr_from_IWineMsiRemoteCustomAction( iface );
1453 ULONG r;
1455 r = InterlockedDecrement( &This->refs );
1456 if (r == 0)
1457 msi_free( This );
1458 return r;
1461 static HRESULT WINAPI mcr_GetActionInfo( IWineMsiRemoteCustomAction *iface, LPCGUID custom_action_guid,
1462 INT *type, MSIHANDLE *handle, BSTR *dll, BSTR *func, IWineMsiRemotePackage **remote_package )
1464 msi_custom_action_info *info;
1466 info = find_action_by_guid( custom_action_guid );
1467 if (!info)
1468 return E_FAIL;
1470 *type = info->type;
1471 *handle = alloc_msihandle( &info->package->hdr );
1472 *dll = SysAllocString( info->source );
1473 *func = SysAllocString( info->target );
1475 release_custom_action_data( info );
1476 return create_msi_remote_package( NULL, (LPVOID *)remote_package );
1479 static const IWineMsiRemoteCustomActionVtbl msi_custom_remote_vtbl =
1481 mcr_QueryInterface,
1482 mcr_AddRef,
1483 mcr_Release,
1484 mcr_GetActionInfo,
1487 HRESULT create_msi_custom_remote( IUnknown *pOuter, LPVOID *ppObj )
1489 msi_custom_remote_impl* This;
1491 This = msi_alloc( sizeof *This );
1492 if (!This)
1493 return E_OUTOFMEMORY;
1495 This->lpVtbl = &msi_custom_remote_vtbl;
1496 This->refs = 1;
1498 *ppObj = This;
1500 return S_OK;