ecc3b53afbb6d7c66e680663ac0ed5df37eb70ae
[reactos.git] / reactos / dll / win32 / msi / custom.c
1 /*
2 * Custom Action processing for the Microsoft Installer (msi.dll)
3 *
4 * Copyright 2005 Aric Stewart for CodeWeavers
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #define _INC_WINDOWS
22 #define COM_NO_WINDOWS_H
23
24 #include <config.h>
25 //#include "wine/port.h"
26
27 #define COBJMACROS
28
29 #include <stdarg.h>
30 #include <windef.h>
31 #include <winbase.h>
32 //#include "winerror.h"
33 //#include "msidefs.h"
34 #include <winuser.h>
35 #include <objbase.h>
36 #include <oleauto.h>
37
38 #include "msipriv.h"
39 #include <msiserver.h>
40 #include <wine/debug.h>
41 #include <wine/unicode.h>
42 #include <wine/exception.h>
43
44 WINE_DEFAULT_DEBUG_CHANNEL(msi);
45
46 #define CUSTOM_ACTION_TYPE_MASK 0x3F
47
48 typedef struct tagMSIRUNNINGACTION
49 {
50 struct list entry;
51 HANDLE handle;
52 BOOL process;
53 LPWSTR name;
54 } MSIRUNNINGACTION;
55
56 typedef UINT (WINAPI *MsiCustomActionEntryPoint)( MSIHANDLE );
57
58 static CRITICAL_SECTION msi_custom_action_cs;
59 static CRITICAL_SECTION_DEBUG msi_custom_action_cs_debug =
60 {
61 0, 0, &msi_custom_action_cs,
62 { &msi_custom_action_cs_debug.ProcessLocksList,
63 &msi_custom_action_cs_debug.ProcessLocksList },
64 0, 0, { (DWORD_PTR)(__FILE__ ": msi_custom_action_cs") }
65 };
66 static CRITICAL_SECTION msi_custom_action_cs = { &msi_custom_action_cs_debug, -1, 0, 0, 0, 0 };
67
68 static struct list msi_pending_custom_actions = LIST_INIT( msi_pending_custom_actions );
69
70 UINT msi_schedule_action( MSIPACKAGE *package, UINT script, const WCHAR *action )
71 {
72 UINT count;
73 WCHAR **newbuf = NULL;
74
75 if (script >= SCRIPT_MAX)
76 {
77 FIXME("Unknown script requested %u\n", script);
78 return ERROR_FUNCTION_FAILED;
79 }
80 TRACE("Scheduling action %s in script %u\n", debugstr_w(action), script);
81
82 count = package->script->ActionCount[script];
83 package->script->ActionCount[script]++;
84 if (count != 0) newbuf = msi_realloc( package->script->Actions[script],
85 package->script->ActionCount[script] * sizeof(WCHAR *) );
86 else newbuf = msi_alloc( sizeof(WCHAR *) );
87
88 newbuf[count] = strdupW( action );
89 package->script->Actions[script] = newbuf;
90 return ERROR_SUCCESS;
91 }
92
93 UINT msi_register_unique_action( MSIPACKAGE *package, const WCHAR *action )
94 {
95 UINT count;
96 WCHAR **newbuf = NULL;
97
98 if (!package->script) return FALSE;
99
100 TRACE("Registering %s as unique action\n", debugstr_w(action));
101
102 count = package->script->UniqueActionsCount;
103 package->script->UniqueActionsCount++;
104 if (count != 0) newbuf = msi_realloc( package->script->UniqueActions,
105 package->script->UniqueActionsCount * sizeof(WCHAR *) );
106 else newbuf = msi_alloc( sizeof(WCHAR *) );
107
108 newbuf[count] = strdupW( action );
109 package->script->UniqueActions = newbuf;
110 return ERROR_SUCCESS;
111 }
112
113 BOOL msi_action_is_unique( const MSIPACKAGE *package, const WCHAR *action )
114 {
115 UINT i;
116
117 if (!package->script) return FALSE;
118
119 for (i = 0; i < package->script->UniqueActionsCount; i++)
120 {
121 if (!strcmpW( package->script->UniqueActions[i], action )) return TRUE;
122 }
123 return FALSE;
124 }
125
126 static BOOL check_execution_scheduling_options(MSIPACKAGE *package, LPCWSTR action, UINT options)
127 {
128 if (!package->script)
129 return TRUE;
130
131 if ((options & msidbCustomActionTypeClientRepeat) ==
132 msidbCustomActionTypeClientRepeat)
133 {
134 if (!(package->script->InWhatSequence & SEQUENCE_UI &&
135 package->script->InWhatSequence & SEQUENCE_EXEC))
136 {
137 TRACE("Skipping action due to dbCustomActionTypeClientRepeat option.\n");
138 return FALSE;
139 }
140 }
141 else if (options & msidbCustomActionTypeFirstSequence)
142 {
143 if (package->script->InWhatSequence & SEQUENCE_UI &&
144 package->script->InWhatSequence & SEQUENCE_EXEC )
145 {
146 TRACE("Skipping action due to msidbCustomActionTypeFirstSequence option.\n");
147 return FALSE;
148 }
149 }
150 else if (options & msidbCustomActionTypeOncePerProcess)
151 {
152 if (msi_action_is_unique(package, action))
153 {
154 TRACE("Skipping action due to msidbCustomActionTypeOncePerProcess option.\n");
155 return FALSE;
156 }
157 else
158 msi_register_unique_action(package, action);
159 }
160
161 return TRUE;
162 }
163
164 /* stores the following properties before the action:
165 *
166 * [CustomActionData<=>UserSID<=>ProductCode]Action
167 */
168 static LPWSTR msi_get_deferred_action(LPCWSTR action, LPCWSTR actiondata,
169 LPCWSTR usersid, LPCWSTR prodcode)
170 {
171 LPWSTR deferred;
172 DWORD len;
173
174 static const WCHAR format[] = {
175 '[','%','s','<','=','>','%','s','<','=','>','%','s',']','%','s',0
176 };
177
178 if (!actiondata)
179 return strdupW(action);
180
181 len = lstrlenW(action) + lstrlenW(actiondata) +
182 lstrlenW(usersid) + lstrlenW(prodcode) +
183 lstrlenW(format) - 7;
184 deferred = msi_alloc(len * sizeof(WCHAR));
185
186 sprintfW(deferred, format, actiondata, usersid, prodcode, action);
187 return deferred;
188 }
189
190 static void set_deferred_action_props( MSIPACKAGE *package, const WCHAR *deferred_data )
191 {
192 static const WCHAR sep[] = {'<','=','>',0};
193 const WCHAR *end, *beg = deferred_data + 1;
194
195 end = strstrW(beg, sep);
196 msi_set_property( package->db, szCustomActionData, beg, end - beg );
197 beg = end + 3;
198
199 end = strstrW(beg, sep);
200 msi_set_property( package->db, szUserSID, beg, end - beg );
201 beg = end + 3;
202
203 end = strchrW(beg, ']');
204 msi_set_property( package->db, szProductCode, beg, end - beg );
205 }
206
207 static MSIBINARY *create_temp_binary( MSIPACKAGE *package, LPCWSTR source, BOOL dll )
208 {
209 static const WCHAR query[] = {
210 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
211 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',' ',
212 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
213 MSIRECORD *row;
214 MSIBINARY *binary;
215 HANDLE file;
216 CHAR buffer[1024];
217 WCHAR fmt[MAX_PATH], tmpfile[MAX_PATH];
218 DWORD sz = MAX_PATH, write;
219 UINT r;
220
221 if (msi_get_property(package->db, szTempFolder, fmt, &sz) != ERROR_SUCCESS)
222 GetTempPathW(MAX_PATH, fmt);
223
224 if (!GetTempFileNameW( fmt, szMsi, 0, tmpfile ))
225 {
226 TRACE("unable to create temp file %s (%u)\n", debugstr_w(tmpfile), GetLastError());
227 return NULL;
228 }
229
230 row = MSI_QueryGetRecord(package->db, query, source);
231 if (!row)
232 return NULL;
233
234 if (!(binary = msi_alloc_zero( sizeof(MSIBINARY) )))
235 {
236 msiobj_release( &row->hdr );
237 return NULL;
238 }
239 file = CreateFileW( tmpfile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
240 if (file == INVALID_HANDLE_VALUE)
241 {
242 msiobj_release( &row->hdr );
243 msi_free( binary );
244 return NULL;
245 }
246 do
247 {
248 sz = sizeof(buffer);
249 r = MSI_RecordReadStream( row, 2, buffer, &sz );
250 if (r != ERROR_SUCCESS)
251 {
252 ERR("Failed to get stream\n");
253 break;
254 }
255 WriteFile( file, buffer, sz, &write, NULL );
256 } while (sz == sizeof buffer);
257
258 CloseHandle( file );
259 msiobj_release( &row->hdr );
260 if (r != ERROR_SUCCESS)
261 {
262 DeleteFileW( tmpfile );
263 msi_free( binary );
264 return NULL;
265 }
266
267 /* keep a reference to prevent the dll from being unloaded */
268 if (dll && !(binary->module = LoadLibraryW( tmpfile )))
269 {
270 WARN( "failed to load dll %s (%u)\n", debugstr_w( tmpfile ), GetLastError() );
271 }
272 binary->source = strdupW( source );
273 binary->tmpfile = strdupW( tmpfile );
274 list_add_tail( &package->binaries, &binary->entry );
275 return binary;
276 }
277
278 static MSIBINARY *get_temp_binary( MSIPACKAGE *package, LPCWSTR source, BOOL dll )
279 {
280 MSIBINARY *binary;
281
282 LIST_FOR_EACH_ENTRY( binary, &package->binaries, MSIBINARY, entry )
283 {
284 if (!strcmpW( binary->source, source ))
285 return binary;
286 }
287
288 return create_temp_binary( package, source, dll );
289 }
290
291 static void file_running_action(MSIPACKAGE* package, HANDLE Handle,
292 BOOL process, LPCWSTR name)
293 {
294 MSIRUNNINGACTION *action;
295
296 action = msi_alloc( sizeof(MSIRUNNINGACTION) );
297
298 action->handle = Handle;
299 action->process = process;
300 action->name = strdupW(name);
301
302 list_add_tail( &package->RunningActions, &action->entry );
303 }
304
305 static UINT custom_get_process_return( HANDLE process )
306 {
307 DWORD rc = 0;
308
309 GetExitCodeProcess( process, &rc );
310 TRACE("exit code is %u\n", rc);
311 if (rc != 0)
312 return ERROR_FUNCTION_FAILED;
313 return ERROR_SUCCESS;
314 }
315
316 static UINT custom_get_thread_return( MSIPACKAGE *package, HANDLE thread )
317 {
318 DWORD rc = 0;
319
320 GetExitCodeThread( thread, &rc );
321
322 switch (rc)
323 {
324 case ERROR_FUNCTION_NOT_CALLED:
325 case ERROR_SUCCESS:
326 case ERROR_INSTALL_USEREXIT:
327 case ERROR_INSTALL_FAILURE:
328 return rc;
329 case ERROR_NO_MORE_ITEMS:
330 return ERROR_SUCCESS;
331 case ERROR_INSTALL_SUSPEND:
332 ACTION_ForceReboot( package );
333 return ERROR_SUCCESS;
334 default:
335 ERR("Invalid Return Code %d\n",rc);
336 return ERROR_INSTALL_FAILURE;
337 }
338 }
339
340 static UINT wait_process_handle(MSIPACKAGE* package, UINT type,
341 HANDLE ProcessHandle, LPCWSTR name)
342 {
343 UINT rc = ERROR_SUCCESS;
344
345 if (!(type & msidbCustomActionTypeAsync))
346 {
347 TRACE("waiting for %s\n", debugstr_w(name));
348
349 msi_dialog_check_messages(ProcessHandle);
350
351 if (!(type & msidbCustomActionTypeContinue))
352 rc = custom_get_process_return(ProcessHandle);
353
354 CloseHandle(ProcessHandle);
355 }
356 else
357 {
358 TRACE("%s running in background\n", debugstr_w(name));
359
360 if (!(type & msidbCustomActionTypeContinue))
361 file_running_action(package, ProcessHandle, TRUE, name);
362 else
363 CloseHandle(ProcessHandle);
364 }
365
366 return rc;
367 }
368
369 typedef struct _msi_custom_action_info {
370 struct list entry;
371 LONG refs;
372 MSIPACKAGE *package;
373 LPWSTR source;
374 LPWSTR target;
375 HANDLE handle;
376 LPWSTR action;
377 INT type;
378 GUID guid;
379 } msi_custom_action_info;
380
381 static void release_custom_action_data( msi_custom_action_info *info )
382 {
383 EnterCriticalSection( &msi_custom_action_cs );
384
385 if (!--info->refs)
386 {
387 list_remove( &info->entry );
388 if (info->handle)
389 CloseHandle( info->handle );
390 msi_free( info->action );
391 msi_free( info->source );
392 msi_free( info->target );
393 msiobj_release( &info->package->hdr );
394 msi_free( info );
395 }
396
397 LeaveCriticalSection( &msi_custom_action_cs );
398 }
399
400 /* must be called inside msi_custom_action_cs if info is in the pending custom actions list */
401 static void addref_custom_action_data( msi_custom_action_info *info )
402 {
403 info->refs++;
404 }
405
406 static UINT wait_thread_handle( msi_custom_action_info *info )
407 {
408 UINT rc = ERROR_SUCCESS;
409
410 if (!(info->type & msidbCustomActionTypeAsync))
411 {
412 TRACE("waiting for %s\n", debugstr_w( info->action ));
413
414 msi_dialog_check_messages( info->handle );
415
416 if (!(info->type & msidbCustomActionTypeContinue))
417 rc = custom_get_thread_return( info->package, info->handle );
418
419 release_custom_action_data( info );
420 }
421 else
422 {
423 TRACE("%s running in background\n", debugstr_w( info->action ));
424 }
425
426 return rc;
427 }
428
429 static msi_custom_action_info *find_action_by_guid( const GUID *guid )
430 {
431 msi_custom_action_info *info;
432 BOOL found = FALSE;
433
434 EnterCriticalSection( &msi_custom_action_cs );
435
436 LIST_FOR_EACH_ENTRY( info, &msi_pending_custom_actions, msi_custom_action_info, entry )
437 {
438 if (IsEqualGUID( &info->guid, guid ))
439 {
440 addref_custom_action_data( info );
441 found = TRUE;
442 break;
443 }
444 }
445
446 LeaveCriticalSection( &msi_custom_action_cs );
447
448 if (!found)
449 return NULL;
450
451 return info;
452 }
453
454 static void handle_msi_break( LPCWSTR target )
455 {
456 LPWSTR msg;
457 WCHAR val[MAX_PATH];
458
459 static const WCHAR MsiBreak[] = { 'M','s','i','B','r','e','a','k',0 };
460 static const WCHAR WindowsInstaller[] = {
461 'W','i','n','d','o','w','s',' ','I','n','s','t','a','l','l','e','r',0
462 };
463
464 static const WCHAR format[] = {
465 'T','o',' ','d','e','b','u','g',' ','y','o','u','r',' ',
466 'c','u','s','t','o','m',' ','a','c','t','i','o','n',',',' ',
467 'a','t','t','a','c','h',' ','y','o','u','r',' ','d','e','b','u','g','g','e','r',' ',
468 't','o',' ','p','r','o','c','e','s','s',' ','%','i',' ','(','0','x','%','X',')',' ',
469 'a','n','d',' ','p','r','e','s','s',' ','O','K',0
470 };
471
472 if( !GetEnvironmentVariableW( MsiBreak, val, MAX_PATH ))
473 return;
474
475 if( strcmpiW( val, target ))
476 return;
477
478 msg = msi_alloc( (lstrlenW(format) + 10) * sizeof(WCHAR) );
479 if (!msg)
480 return;
481
482 wsprintfW( msg, format, GetCurrentProcessId(), GetCurrentProcessId());
483 MessageBoxW( NULL, msg, WindowsInstaller, MB_OK);
484 msi_free(msg);
485 DebugBreak();
486 }
487
488 static UINT get_action_info( const GUID *guid, INT *type, MSIHANDLE *handle,
489 BSTR *dll, BSTR *funcname,
490 IWineMsiRemotePackage **package )
491 {
492 IClassFactory *cf = NULL;
493 IWineMsiRemoteCustomAction *rca = NULL;
494 HRESULT r;
495
496 r = DllGetClassObject( &CLSID_WineMsiRemoteCustomAction,
497 &IID_IClassFactory, (LPVOID *)&cf );
498 if (FAILED(r))
499 {
500 ERR("failed to get IClassFactory interface\n");
501 return ERROR_FUNCTION_FAILED;
502 }
503
504 r = IClassFactory_CreateInstance( cf, NULL, &IID_IWineMsiRemoteCustomAction, (LPVOID *)&rca );
505 if (FAILED(r))
506 {
507 ERR("failed to get IWineMsiRemoteCustomAction interface\n");
508 return ERROR_FUNCTION_FAILED;
509 }
510
511 r = IWineMsiRemoteCustomAction_GetActionInfo( rca, guid, type, handle, dll, funcname, package );
512 IWineMsiRemoteCustomAction_Release( rca );
513 if (FAILED(r))
514 {
515 ERR("GetActionInfo failed\n");
516 return ERROR_FUNCTION_FAILED;
517 }
518
519 return ERROR_SUCCESS;
520 }
521
522 #ifdef __i386__
523 extern UINT CUSTOMPROC_wrapper( MsiCustomActionEntryPoint proc, MSIHANDLE handle );
524 __ASM_GLOBAL_FUNC( CUSTOMPROC_wrapper,
525 "pushl %ebp\n\t"
526 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
527 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
528 "movl %esp,%ebp\n\t"
529 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
530 "pushl 12(%ebp)\n\t"
531 "movl 8(%ebp),%eax\n\t"
532 "call *%eax\n\t"
533 "leave\n\t"
534 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
535 __ASM_CFI(".cfi_same_value %ebp\n\t")
536 "ret" )
537 #else
538 static inline UINT CUSTOMPROC_wrapper( MsiCustomActionEntryPoint proc, MSIHANDLE handle )
539 {
540 return proc(handle);
541 }
542 #endif
543
544 static DWORD ACTION_CallDllFunction( const GUID *guid )
545 {
546 MsiCustomActionEntryPoint fn;
547 MSIHANDLE hPackage, handle;
548 HANDLE hModule;
549 LPSTR proc;
550 UINT r = ERROR_FUNCTION_FAILED;
551 BSTR dll = NULL, function = NULL;
552 INT type;
553 IWineMsiRemotePackage *remote_package = NULL;
554
555 TRACE("%s\n", debugstr_guid( guid ));
556
557 r = get_action_info( guid, &type, &handle, &dll, &function, &remote_package );
558 if (r != ERROR_SUCCESS)
559 return r;
560
561 hModule = LoadLibraryW( dll );
562 if (!hModule)
563 {
564 WARN( "failed to load dll %s (%u)\n", debugstr_w( dll ), GetLastError() );
565 return ERROR_SUCCESS;
566 }
567
568 proc = strdupWtoA( function );
569 fn = (MsiCustomActionEntryPoint) GetProcAddress( hModule, proc );
570 msi_free( proc );
571 if (fn)
572 {
573 hPackage = alloc_msi_remote_handle( (IUnknown *)remote_package );
574 if (hPackage)
575 {
576 IWineMsiRemotePackage_SetMsiHandle( remote_package, handle );
577 TRACE("calling %s\n", debugstr_w( function ) );
578 handle_msi_break( function );
579
580 __TRY
581 {
582 r = CUSTOMPROC_wrapper( fn, hPackage );
583 }
584 __EXCEPT_PAGE_FAULT
585 {
586 ERR("Custom action (%s:%s) caused a page fault: %08x\n",
587 debugstr_w(dll), debugstr_w(function), GetExceptionCode());
588 r = ERROR_SUCCESS;
589 }
590 __ENDTRY;
591
592 MsiCloseHandle( hPackage );
593 }
594 else
595 ERR("failed to create handle for %p\n", remote_package );
596 }
597 else
598 ERR("GetProcAddress(%s) failed\n", debugstr_w( function ) );
599
600 FreeLibrary(hModule);
601
602 IWineMsiRemotePackage_Release( remote_package );
603 SysFreeString( dll );
604 SysFreeString( function );
605 MsiCloseHandle( handle );
606
607 return r;
608 }
609
610 static DWORD WINAPI DllThread( LPVOID arg )
611 {
612 LPGUID guid = arg;
613 DWORD rc = 0;
614
615 TRACE("custom action (%x) started\n", GetCurrentThreadId() );
616
617 rc = ACTION_CallDllFunction( guid );
618
619 TRACE("custom action (%x) returned %i\n", GetCurrentThreadId(), rc );
620
621 MsiCloseAllHandles();
622 return rc;
623 }
624
625 static msi_custom_action_info *do_msidbCustomActionTypeDll(
626 MSIPACKAGE *package, INT type, LPCWSTR source, LPCWSTR target, LPCWSTR action )
627 {
628 msi_custom_action_info *info;
629
630 info = msi_alloc( sizeof *info );
631 if (!info)
632 return NULL;
633
634 msiobj_addref( &package->hdr );
635 info->refs = 2; /* 1 for our caller and 1 for thread we created */
636 info->package = package;
637 info->type = type;
638 info->target = strdupW( target );
639 info->source = strdupW( source );
640 info->action = strdupW( action );
641 CoCreateGuid( &info->guid );
642
643 EnterCriticalSection( &msi_custom_action_cs );
644 list_add_tail( &msi_pending_custom_actions, &info->entry );
645 LeaveCriticalSection( &msi_custom_action_cs );
646
647 info->handle = CreateThread( NULL, 0, DllThread, &info->guid, 0, NULL );
648 if (!info->handle)
649 {
650 /* release both references */
651 release_custom_action_data( info );
652 release_custom_action_data( info );
653 return NULL;
654 }
655
656 return info;
657 }
658
659 static UINT HANDLE_CustomType1( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
660 INT type, const WCHAR *action )
661 {
662 msi_custom_action_info *info;
663 MSIBINARY *binary;
664
665 if (!(binary = get_temp_binary( package, source, TRUE )))
666 return ERROR_FUNCTION_FAILED;
667
668 TRACE("Calling function %s from %s\n", debugstr_w(target), debugstr_w(binary->tmpfile));
669
670 info = do_msidbCustomActionTypeDll( package, type, binary->tmpfile, target, action );
671 return wait_thread_handle( info );
672 }
673
674 static HANDLE execute_command( const WCHAR *app, WCHAR *arg, const WCHAR *dir )
675 {
676 static const WCHAR dotexeW[] = {'.','e','x','e',0};
677 STARTUPINFOW si;
678 PROCESS_INFORMATION info;
679 WCHAR *exe = NULL, *cmd = NULL, *p;
680 BOOL ret;
681
682 if (app)
683 {
684 int len_arg = 0;
685 DWORD len_exe;
686
687 if (!(exe = msi_alloc( MAX_PATH * sizeof(WCHAR) ))) return INVALID_HANDLE_VALUE;
688 len_exe = SearchPathW( NULL, app, dotexeW, MAX_PATH, exe, NULL );
689 if (len_exe >= MAX_PATH)
690 {
691 msi_free( exe );
692 if (!(exe = msi_alloc( len_exe * sizeof(WCHAR) ))) return INVALID_HANDLE_VALUE;
693 len_exe = SearchPathW( NULL, app, dotexeW, len_exe, exe, NULL );
694 }
695 if (!len_exe)
696 {
697 WARN("can't find executable %u\n", GetLastError());
698 msi_free( exe );
699 return INVALID_HANDLE_VALUE;
700 }
701
702 if (arg) len_arg = strlenW( arg );
703 if (!(cmd = msi_alloc( (len_exe + len_arg + 4) * sizeof(WCHAR) )))
704 {
705 msi_free( exe );
706 return INVALID_HANDLE_VALUE;
707 }
708 p = cmd;
709 if (strchrW( exe, ' ' ))
710 {
711 *p++ = '\"';
712 memcpy( p, exe, len_exe * sizeof(WCHAR) );
713 p += len_exe;
714 *p++ = '\"';
715 *p = 0;
716 }
717 else
718 {
719 strcpyW( p, exe );
720 p += len_exe;
721 }
722 if (arg)
723 {
724 *p++ = ' ';
725 memcpy( p, arg, len_arg * sizeof(WCHAR) );
726 p[len_arg] = 0;
727 }
728 }
729 memset( &si, 0, sizeof(STARTUPINFOW) );
730 ret = CreateProcessW( exe, exe ? cmd : arg, NULL, NULL, FALSE, 0, NULL, dir, &si, &info );
731 msi_free( cmd );
732 msi_free( exe );
733 if (!ret)
734 {
735 WARN("unable to execute command %u\n", GetLastError());
736 return INVALID_HANDLE_VALUE;
737 }
738 CloseHandle( info.hThread );
739 return info.hProcess;
740 }
741
742 static UINT HANDLE_CustomType2( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
743 INT type, const WCHAR *action )
744 {
745 MSIBINARY *binary;
746 HANDLE handle;
747 WCHAR *arg;
748
749 if (!(binary = get_temp_binary( package, source, FALSE ))) return ERROR_FUNCTION_FAILED;
750
751 deformat_string( package, target, &arg );
752 TRACE("exe %s arg %s\n", debugstr_w(binary->tmpfile), debugstr_w(arg));
753
754 handle = execute_command( binary->tmpfile, arg, szCRoot );
755 msi_free( arg );
756 if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
757 return wait_process_handle( package, type, handle, action );
758 }
759
760 static UINT HANDLE_CustomType17( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
761 INT type, const WCHAR *action )
762 {
763 msi_custom_action_info *info;
764 MSIFILE *file;
765
766 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
767
768 file = msi_get_loaded_file( package, source );
769 if (!file)
770 {
771 ERR("invalid file key %s\n", debugstr_w( source ));
772 return ERROR_FUNCTION_FAILED;
773 }
774
775 info = do_msidbCustomActionTypeDll( package, type, file->TargetPath, target, action );
776 return wait_thread_handle( info );
777 }
778
779 static UINT HANDLE_CustomType18( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
780 INT type, const WCHAR *action )
781 {
782 MSIFILE *file;
783 HANDLE handle;
784 WCHAR *arg;
785
786 if (!(file = msi_get_loaded_file( package, source ))) return ERROR_FUNCTION_FAILED;
787
788 deformat_string( package, target, &arg );
789 TRACE("exe %s arg %s\n", debugstr_w(file->TargetPath), debugstr_w(arg));
790
791 handle = execute_command( file->TargetPath, arg, szCRoot );
792 msi_free( arg );
793 if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
794 return wait_process_handle( package, type, handle, action );
795 }
796
797 static UINT HANDLE_CustomType19( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
798 INT type, const WCHAR *action )
799 {
800 static const WCHAR query[] = {
801 'S','E','L','E','C','T',' ','`','M','e','s','s','a','g','e','`',' ',
802 'F','R','O','M',' ','`','E','r','r','o','r','`',' ',
803 'W','H','E','R','E',' ','`','E','r','r','o','r','`',' ','=',' ',
804 '%','s',0
805 };
806 MSIRECORD *row = 0;
807 LPWSTR deformated = NULL;
808
809 deformat_string( package, target, &deformated );
810
811 /* first try treat the error as a number */
812 row = MSI_QueryGetRecord( package->db, query, deformated );
813 if( row )
814 {
815 LPCWSTR error = MSI_RecordGetString( row, 1 );
816 if ((package->ui_level & INSTALLUILEVEL_MASK) != INSTALLUILEVEL_NONE)
817 MessageBoxW( NULL, error, NULL, MB_OK );
818 msiobj_release( &row->hdr );
819 }
820 else if ((package->ui_level & INSTALLUILEVEL_MASK) != INSTALLUILEVEL_NONE)
821 MessageBoxW( NULL, deformated, NULL, MB_OK );
822
823 msi_free( deformated );
824
825 return ERROR_INSTALL_FAILURE;
826 }
827
828 static UINT HANDLE_CustomType23( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
829 INT type, const WCHAR *action )
830 {
831 static const WCHAR msiexecW[] = {'m','s','i','e','x','e','c',0};
832 static const WCHAR paramsW[] = {'/','q','b',' ','/','i',' '};
833 WCHAR *dir, *arg, *p;
834 UINT len_src, len_dir, len_tgt, len = sizeof(paramsW)/sizeof(paramsW[0]);
835 HANDLE handle;
836
837 if (!(dir = msi_dup_property( package->db, szOriginalDatabase ))) return ERROR_OUTOFMEMORY;
838 if (!(p = strrchrW( dir, '\\' )) && !(p = strrchrW( dir, '/' )))
839 {
840 msi_free( dir );
841 return ERROR_FUNCTION_FAILED;
842 }
843 *p = 0;
844 len_dir = p - dir;
845 len_src = strlenW( source );
846 len_tgt = strlenW( target );
847 if (!(arg = msi_alloc( (len + len_dir + len_src + len_tgt + 5) * sizeof(WCHAR) )))
848 {
849 msi_free( dir );
850 return ERROR_OUTOFMEMORY;
851 }
852 memcpy( arg, paramsW, sizeof(paramsW) );
853 arg[len++] = '"';
854 memcpy( arg + len, dir, len_dir * sizeof(WCHAR) );
855 len += len_dir;
856 arg[len++] = '\\';
857 memcpy( arg + len, source, len_src * sizeof(WCHAR) );
858 len += len_src;
859 arg[len++] = '"';
860 arg[len++] = ' ';
861 strcpyW( arg + len, target );
862
863 TRACE("installing %s concurrently\n", debugstr_w(source));
864
865 handle = execute_command( msiexecW, arg, dir );
866 msi_free( dir );
867 msi_free( arg );
868 if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
869 return wait_process_handle( package, type, handle, action );
870 }
871
872 static UINT HANDLE_CustomType50( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
873 INT type, const WCHAR *action )
874 {
875 WCHAR *exe, *arg;
876 HANDLE handle;
877
878 if (!(exe = msi_dup_property( package->db, source ))) return ERROR_SUCCESS;
879
880 deformat_string( package, target, &arg );
881 TRACE("exe %s arg %s\n", debugstr_w(exe), debugstr_w(arg));
882
883 handle = execute_command( exe, arg, szCRoot );
884 msi_free( exe );
885 msi_free( arg );
886 if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
887 return wait_process_handle( package, type, handle, action );
888 }
889
890 static UINT HANDLE_CustomType34( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
891 INT type, const WCHAR *action )
892 {
893 const WCHAR *workingdir = NULL;
894 HANDLE handle;
895 WCHAR *cmd;
896
897 if (source)
898 {
899 workingdir = msi_get_target_folder( package, source );
900 if (!workingdir) return ERROR_FUNCTION_FAILED;
901 }
902 deformat_string( package, target, &cmd );
903 if (!cmd) return ERROR_FUNCTION_FAILED;
904
905 TRACE("cmd %s dir %s\n", debugstr_w(cmd), debugstr_w(workingdir));
906
907 handle = execute_command( NULL, cmd, workingdir );
908 msi_free( cmd );
909 if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
910 return wait_process_handle( package, type, handle, action );
911 }
912
913 static DWORD ACTION_CallScript( const GUID *guid )
914 {
915 msi_custom_action_info *info;
916 MSIHANDLE hPackage;
917 UINT r = ERROR_FUNCTION_FAILED;
918
919 info = find_action_by_guid( guid );
920 if (!info)
921 {
922 ERR("failed to find action %s\n", debugstr_guid( guid) );
923 return ERROR_FUNCTION_FAILED;
924 }
925
926 TRACE("function %s, script %s\n", debugstr_w( info->target ), debugstr_w( info->source ) );
927
928 hPackage = alloc_msihandle( &info->package->hdr );
929 if (hPackage)
930 {
931 r = call_script( hPackage, info->type, info->source, info->target, info->action );
932 TRACE("script returned %u\n", r);
933 MsiCloseHandle( hPackage );
934 }
935 else
936 ERR("failed to create handle for %p\n", info->package );
937
938 release_custom_action_data( info );
939 return r;
940 }
941
942 static DWORD WINAPI ScriptThread( LPVOID arg )
943 {
944 LPGUID guid = arg;
945 DWORD rc;
946
947 TRACE("custom action (%x) started\n", GetCurrentThreadId() );
948
949 rc = ACTION_CallScript( guid );
950
951 TRACE("custom action (%x) returned %i\n", GetCurrentThreadId(), rc );
952
953 MsiCloseAllHandles();
954 return rc;
955 }
956
957 static msi_custom_action_info *do_msidbCustomActionTypeScript(
958 MSIPACKAGE *package, INT type, LPCWSTR script, LPCWSTR function, LPCWSTR action )
959 {
960 msi_custom_action_info *info;
961
962 info = msi_alloc( sizeof *info );
963 if (!info)
964 return NULL;
965
966 msiobj_addref( &package->hdr );
967 info->refs = 2; /* 1 for our caller and 1 for thread we created */
968 info->package = package;
969 info->type = type;
970 info->target = strdupW( function );
971 info->source = strdupW( script );
972 info->action = strdupW( action );
973 CoCreateGuid( &info->guid );
974
975 EnterCriticalSection( &msi_custom_action_cs );
976 list_add_tail( &msi_pending_custom_actions, &info->entry );
977 LeaveCriticalSection( &msi_custom_action_cs );
978
979 info->handle = CreateThread( NULL, 0, ScriptThread, &info->guid, 0, NULL );
980 if (!info->handle)
981 {
982 /* release both references */
983 release_custom_action_data( info );
984 release_custom_action_data( info );
985 return NULL;
986 }
987
988 return info;
989 }
990
991 static UINT HANDLE_CustomType37_38( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
992 INT type, const WCHAR *action )
993 {
994 msi_custom_action_info *info;
995
996 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
997
998 info = do_msidbCustomActionTypeScript( package, type, target, NULL, action );
999 return wait_thread_handle( info );
1000 }
1001
1002 static UINT HANDLE_CustomType5_6( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
1003 INT type, const WCHAR *action )
1004 {
1005 static const WCHAR query[] = {
1006 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1007 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',' ',
1008 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
1009 MSIRECORD *row = 0;
1010 msi_custom_action_info *info;
1011 CHAR *buffer = NULL;
1012 WCHAR *bufferw = NULL;
1013 DWORD sz = 0;
1014 UINT r;
1015
1016 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1017
1018 row = MSI_QueryGetRecord(package->db, query, source);
1019 if (!row)
1020 return ERROR_FUNCTION_FAILED;
1021
1022 r = MSI_RecordReadStream(row, 2, NULL, &sz);
1023 if (r != ERROR_SUCCESS) return r;
1024
1025 buffer = msi_alloc( sz + 1 );
1026 if (!buffer) return ERROR_FUNCTION_FAILED;
1027
1028 r = MSI_RecordReadStream(row, 2, buffer, &sz);
1029 if (r != ERROR_SUCCESS)
1030 goto done;
1031
1032 buffer[sz] = 0;
1033 bufferw = strdupAtoW(buffer);
1034 if (!bufferw)
1035 {
1036 r = ERROR_FUNCTION_FAILED;
1037 goto done;
1038 }
1039
1040 info = do_msidbCustomActionTypeScript( package, type, bufferw, target, action );
1041 r = wait_thread_handle( info );
1042
1043 done:
1044 msi_free(bufferw);
1045 msi_free(buffer);
1046 return r;
1047 }
1048
1049 static UINT HANDLE_CustomType21_22( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
1050 INT type, const WCHAR *action )
1051 {
1052 msi_custom_action_info *info;
1053 MSIFILE *file;
1054 HANDLE hFile;
1055 DWORD sz, szHighWord = 0, read;
1056 CHAR *buffer=NULL;
1057 WCHAR *bufferw=NULL;
1058 BOOL bRet;
1059 UINT r;
1060
1061 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1062
1063 file = msi_get_loaded_file(package, source);
1064 if (!file)
1065 {
1066 ERR("invalid file key %s\n", debugstr_w(source));
1067 return ERROR_FUNCTION_FAILED;
1068 }
1069
1070 hFile = CreateFileW(file->TargetPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
1071 if (hFile == INVALID_HANDLE_VALUE) return ERROR_FUNCTION_FAILED;
1072
1073 sz = GetFileSize(hFile, &szHighWord);
1074 if (sz == INVALID_FILE_SIZE || szHighWord != 0)
1075 {
1076 CloseHandle(hFile);
1077 return ERROR_FUNCTION_FAILED;
1078 }
1079 buffer = msi_alloc( sz + 1 );
1080 if (!buffer)
1081 {
1082 CloseHandle(hFile);
1083 return ERROR_FUNCTION_FAILED;
1084 }
1085 bRet = ReadFile(hFile, buffer, sz, &read, NULL);
1086 CloseHandle(hFile);
1087 if (!bRet)
1088 {
1089 r = ERROR_FUNCTION_FAILED;
1090 goto done;
1091 }
1092 buffer[read] = 0;
1093 bufferw = strdupAtoW(buffer);
1094 if (!bufferw)
1095 {
1096 r = ERROR_FUNCTION_FAILED;
1097 goto done;
1098 }
1099 info = do_msidbCustomActionTypeScript( package, type, bufferw, target, action );
1100 r = wait_thread_handle( info );
1101
1102 done:
1103 msi_free(bufferw);
1104 msi_free(buffer);
1105 return r;
1106 }
1107
1108 static UINT HANDLE_CustomType53_54( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
1109 INT type, const WCHAR *action )
1110 {
1111 msi_custom_action_info *info;
1112 WCHAR *prop;
1113
1114 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1115
1116 prop = msi_dup_property( package->db, source );
1117 if (!prop) return ERROR_SUCCESS;
1118
1119 info = do_msidbCustomActionTypeScript( package, type, prop, NULL, action );
1120 msi_free(prop);
1121 return wait_thread_handle( info );
1122 }
1123
1124 static BOOL action_type_matches_script( UINT type, UINT script )
1125 {
1126 switch (script)
1127 {
1128 case SCRIPT_NONE:
1129 case SCRIPT_INSTALL:
1130 return !(type & msidbCustomActionTypeCommit) && !(type & msidbCustomActionTypeRollback);
1131 case SCRIPT_COMMIT:
1132 return (type & msidbCustomActionTypeCommit);
1133 case SCRIPT_ROLLBACK:
1134 return (type & msidbCustomActionTypeRollback);
1135 default:
1136 ERR("unhandled script %u\n", script);
1137 }
1138 return FALSE;
1139 }
1140
1141 static UINT defer_custom_action( MSIPACKAGE *package, const WCHAR *action, UINT type )
1142 {
1143 WCHAR *actiondata = msi_dup_property( package->db, action );
1144 WCHAR *usersid = msi_dup_property( package->db, szUserSID );
1145 WCHAR *prodcode = msi_dup_property( package->db, szProductCode );
1146 WCHAR *deferred = msi_get_deferred_action( action, actiondata, usersid, prodcode );
1147
1148 if (!deferred)
1149 {
1150 msi_free( actiondata );
1151 msi_free( usersid );
1152 msi_free( prodcode );
1153 return ERROR_OUTOFMEMORY;
1154 }
1155 if (type & msidbCustomActionTypeCommit)
1156 {
1157 TRACE("deferring commit action\n");
1158 msi_schedule_action( package, SCRIPT_COMMIT, deferred );
1159 }
1160 else if (type & msidbCustomActionTypeRollback)
1161 {
1162 TRACE("deferring rollback action\n");
1163 msi_schedule_action( package, SCRIPT_ROLLBACK, deferred );
1164 }
1165 else
1166 {
1167 TRACE("deferring install action\n");
1168 msi_schedule_action( package, SCRIPT_INSTALL, deferred );
1169 }
1170
1171 msi_free( actiondata );
1172 msi_free( usersid );
1173 msi_free( prodcode );
1174 msi_free( deferred );
1175 return ERROR_SUCCESS;
1176 }
1177
1178 UINT ACTION_CustomAction( MSIPACKAGE *package, LPCWSTR action, UINT script )
1179 {
1180 static const WCHAR query[] = {
1181 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1182 '`','C','u','s','t','o','m','A','c','t','i','o','n','`',' ','W','H','E','R','E',' ',
1183 '`','A','c','t','i' ,'o','n','`',' ','=',' ','\'','%','s','\'',0};
1184 UINT rc = ERROR_SUCCESS;
1185 MSIRECORD *row;
1186 UINT type;
1187 const WCHAR *source, *target, *ptr, *deferred_data = NULL;
1188 WCHAR *deformated = NULL;
1189 int len;
1190
1191 /* deferred action: [properties]Action */
1192 if ((ptr = strrchrW(action, ']')))
1193 {
1194 deferred_data = action;
1195 action = ptr + 1;
1196 }
1197
1198 row = MSI_QueryGetRecord( package->db, query, action );
1199 if (!row)
1200 return ERROR_CALL_NOT_IMPLEMENTED;
1201
1202 type = MSI_RecordGetInteger(row,2);
1203 source = MSI_RecordGetString(row,3);
1204 target = MSI_RecordGetString(row,4);
1205
1206 TRACE("Handling custom action %s (%x %s %s)\n",debugstr_w(action),type,
1207 debugstr_w(source), debugstr_w(target));
1208
1209 /* handle some of the deferred actions */
1210 if (type & msidbCustomActionTypeTSAware)
1211 FIXME("msidbCustomActionTypeTSAware not handled\n");
1212
1213 if (type & msidbCustomActionTypeInScript)
1214 {
1215 if (type & msidbCustomActionTypeNoImpersonate)
1216 WARN("msidbCustomActionTypeNoImpersonate not handled\n");
1217
1218 if (!action_type_matches_script( type, script ))
1219 {
1220 rc = defer_custom_action( package, action, type );
1221 goto end;
1222 }
1223 else
1224 {
1225 LPWSTR actiondata = msi_dup_property( package->db, action );
1226
1227 if (type & msidbCustomActionTypeInScript)
1228 package->scheduled_action_running = TRUE;
1229
1230 if (type & msidbCustomActionTypeCommit)
1231 package->commit_action_running = TRUE;
1232
1233 if (type & msidbCustomActionTypeRollback)
1234 package->rollback_action_running = TRUE;
1235
1236 if (deferred_data)
1237 set_deferred_action_props(package, deferred_data);
1238 else if (actiondata)
1239 msi_set_property( package->db, szCustomActionData, actiondata, -1 );
1240 else
1241 msi_set_property( package->db, szCustomActionData, szEmpty, -1 );
1242
1243 msi_free(actiondata);
1244 }
1245 }
1246 else if (!check_execution_scheduling_options(package,action,type))
1247 {
1248 rc = ERROR_SUCCESS;
1249 goto end;
1250 }
1251
1252 switch (type & CUSTOM_ACTION_TYPE_MASK)
1253 {
1254 case 1: /* DLL file stored in a Binary table stream */
1255 rc = HANDLE_CustomType1(package,source,target,type,action);
1256 break;
1257 case 2: /* EXE file stored in a Binary table stream */
1258 rc = HANDLE_CustomType2(package,source,target,type,action);
1259 break;
1260 case 18: /*EXE file installed with package */
1261 rc = HANDLE_CustomType18(package,source,target,type,action);
1262 break;
1263 case 19: /* Error that halts install */
1264 rc = HANDLE_CustomType19(package,source,target,type,action);
1265 break;
1266 case 17:
1267 rc = HANDLE_CustomType17(package,source,target,type,action);
1268 break;
1269 case 23: /* installs another package in the source tree */
1270 deformat_string(package,target,&deformated);
1271 rc = HANDLE_CustomType23(package,source,deformated,type,action);
1272 msi_free(deformated);
1273 break;
1274 case 50: /*EXE file specified by a property value */
1275 rc = HANDLE_CustomType50(package,source,target,type,action);
1276 break;
1277 case 34: /*EXE to be run in specified directory */
1278 rc = HANDLE_CustomType34(package,source,target,type,action);
1279 break;
1280 case 35: /* Directory set with formatted text. */
1281 deformat_string(package,target,&deformated);
1282 MSI_SetTargetPathW(package, source, deformated);
1283 msi_free(deformated);
1284 break;
1285 case 51: /* Property set with formatted text. */
1286 if (!source)
1287 break;
1288
1289 len = deformat_string( package, target, &deformated );
1290 rc = msi_set_property( package->db, source, deformated, len );
1291 if (rc == ERROR_SUCCESS && !strcmpW( source, szSourceDir ))
1292 msi_reset_folders( package, TRUE );
1293 msi_free(deformated);
1294 break;
1295 case 37: /* JScript/VBScript text stored in target column. */
1296 case 38:
1297 rc = HANDLE_CustomType37_38(package,source,target,type,action);
1298 break;
1299 case 5:
1300 case 6: /* JScript/VBScript file stored in a Binary table stream. */
1301 rc = HANDLE_CustomType5_6(package,source,target,type,action);
1302 break;
1303 case 21: /* JScript/VBScript file installed with the product. */
1304 case 22:
1305 rc = HANDLE_CustomType21_22(package,source,target,type,action);
1306 break;
1307 case 53: /* JScript/VBScript text specified by a property value. */
1308 case 54:
1309 rc = HANDLE_CustomType53_54(package,source,target,type,action);
1310 break;
1311 default:
1312 FIXME("unhandled action type %u (%s %s)\n", type & CUSTOM_ACTION_TYPE_MASK,
1313 debugstr_w(source), debugstr_w(target));
1314 }
1315
1316 end:
1317 package->scheduled_action_running = FALSE;
1318 package->commit_action_running = FALSE;
1319 package->rollback_action_running = FALSE;
1320 msiobj_release(&row->hdr);
1321 return rc;
1322 }
1323
1324 void ACTION_FinishCustomActions(const MSIPACKAGE* package)
1325 {
1326 struct list *item;
1327 HANDLE *wait_handles;
1328 unsigned int handle_count, i;
1329 msi_custom_action_info *info, *cursor;
1330
1331 while ((item = list_head( &package->RunningActions )))
1332 {
1333 MSIRUNNINGACTION *action = LIST_ENTRY( item, MSIRUNNINGACTION, entry );
1334
1335 list_remove( &action->entry );
1336
1337 TRACE("waiting for %s\n", debugstr_w( action->name ) );
1338 msi_dialog_check_messages( action->handle );
1339
1340 CloseHandle( action->handle );
1341 msi_free( action->name );
1342 msi_free( action );
1343 }
1344
1345 EnterCriticalSection( &msi_custom_action_cs );
1346
1347 handle_count = list_count( &msi_pending_custom_actions );
1348 wait_handles = msi_alloc( handle_count * sizeof(HANDLE) );
1349
1350 handle_count = 0;
1351 LIST_FOR_EACH_ENTRY_SAFE( info, cursor, &msi_pending_custom_actions, msi_custom_action_info, entry )
1352 {
1353 if (info->package == package )
1354 {
1355 if (DuplicateHandle(GetCurrentProcess(), info->handle, GetCurrentProcess(), &wait_handles[handle_count], SYNCHRONIZE, FALSE, 0))
1356 handle_count++;
1357 }
1358 }
1359
1360 LeaveCriticalSection( &msi_custom_action_cs );
1361
1362 for (i = 0; i < handle_count; i++)
1363 {
1364 msi_dialog_check_messages( wait_handles[i] );
1365 CloseHandle( wait_handles[i] );
1366 }
1367 msi_free( wait_handles );
1368
1369 EnterCriticalSection( &msi_custom_action_cs );
1370 LIST_FOR_EACH_ENTRY_SAFE( info, cursor, &msi_pending_custom_actions, msi_custom_action_info, entry )
1371 {
1372 if (info->package == package) release_custom_action_data( info );
1373 }
1374 LeaveCriticalSection( &msi_custom_action_cs );
1375 }
1376
1377 typedef struct _msi_custom_remote_impl {
1378 IWineMsiRemoteCustomAction IWineMsiRemoteCustomAction_iface;
1379 LONG refs;
1380 } msi_custom_remote_impl;
1381
1382 static inline msi_custom_remote_impl *impl_from_IWineMsiRemoteCustomAction( IWineMsiRemoteCustomAction *iface )
1383 {
1384 return CONTAINING_RECORD(iface, msi_custom_remote_impl, IWineMsiRemoteCustomAction_iface);
1385 }
1386
1387 static HRESULT WINAPI mcr_QueryInterface( IWineMsiRemoteCustomAction *iface,
1388 REFIID riid,LPVOID *ppobj)
1389 {
1390 if( IsEqualCLSID( riid, &IID_IUnknown ) ||
1391 IsEqualCLSID( riid, &IID_IWineMsiRemoteCustomAction ) )
1392 {
1393 IWineMsiRemoteCustomAction_AddRef( iface );
1394 *ppobj = iface;
1395 return S_OK;
1396 }
1397
1398 return E_NOINTERFACE;
1399 }
1400
1401 static ULONG WINAPI mcr_AddRef( IWineMsiRemoteCustomAction *iface )
1402 {
1403 msi_custom_remote_impl* This = impl_from_IWineMsiRemoteCustomAction( iface );
1404
1405 return InterlockedIncrement( &This->refs );
1406 }
1407
1408 static ULONG WINAPI mcr_Release( IWineMsiRemoteCustomAction *iface )
1409 {
1410 msi_custom_remote_impl* This = impl_from_IWineMsiRemoteCustomAction( iface );
1411 ULONG r;
1412
1413 r = InterlockedDecrement( &This->refs );
1414 if (r == 0)
1415 msi_free( This );
1416 return r;
1417 }
1418
1419 static HRESULT WINAPI mcr_GetActionInfo( IWineMsiRemoteCustomAction *iface, LPCGUID custom_action_guid,
1420 INT *type, MSIHANDLE *handle, BSTR *dll, BSTR *func, IWineMsiRemotePackage **remote_package )
1421 {
1422 msi_custom_action_info *info;
1423
1424 info = find_action_by_guid( custom_action_guid );
1425 if (!info)
1426 return E_FAIL;
1427
1428 *type = info->type;
1429 *handle = alloc_msihandle( &info->package->hdr );
1430 *dll = SysAllocString( info->source );
1431 *func = SysAllocString( info->target );
1432
1433 release_custom_action_data( info );
1434 return create_msi_remote_package( NULL, (LPVOID *)remote_package );
1435 }
1436
1437 static const IWineMsiRemoteCustomActionVtbl msi_custom_remote_vtbl =
1438 {
1439 mcr_QueryInterface,
1440 mcr_AddRef,
1441 mcr_Release,
1442 mcr_GetActionInfo,
1443 };
1444
1445 HRESULT create_msi_custom_remote( IUnknown *pOuter, LPVOID *ppObj )
1446 {
1447 msi_custom_remote_impl* This;
1448
1449 This = msi_alloc( sizeof *This );
1450 if (!This)
1451 return E_OUTOFMEMORY;
1452
1453 This->IWineMsiRemoteCustomAction_iface.lpVtbl = &msi_custom_remote_vtbl;
1454 This->refs = 1;
1455
1456 *ppObj = This;
1457
1458 return S_OK;
1459 }