[MSI] Sync with Wine Staging 1.7.47. CORE-9924
[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 #include "msipriv.h"
22
23 #include <wine/exception.h>
24
25 #ifdef _MSC_VER
26 #include "msvchelper.h"
27 #endif
28
29 WINE_DEFAULT_DEBUG_CHANNEL(msi);
30
31 #define CUSTOM_ACTION_TYPE_MASK 0x3F
32
33 typedef struct tagMSIRUNNINGACTION
34 {
35 struct list entry;
36 HANDLE handle;
37 BOOL process;
38 LPWSTR name;
39 } MSIRUNNINGACTION;
40
41 typedef UINT (WINAPI *MsiCustomActionEntryPoint)( MSIHANDLE );
42
43 static CRITICAL_SECTION msi_custom_action_cs;
44 static CRITICAL_SECTION_DEBUG msi_custom_action_cs_debug =
45 {
46 0, 0, &msi_custom_action_cs,
47 { &msi_custom_action_cs_debug.ProcessLocksList,
48 &msi_custom_action_cs_debug.ProcessLocksList },
49 0, 0, { (DWORD_PTR)(__FILE__ ": msi_custom_action_cs") }
50 };
51 static CRITICAL_SECTION msi_custom_action_cs = { &msi_custom_action_cs_debug, -1, 0, 0, 0, 0 };
52
53 static struct list msi_pending_custom_actions = LIST_INIT( msi_pending_custom_actions );
54
55 UINT msi_schedule_action( MSIPACKAGE *package, UINT script, const WCHAR *action )
56 {
57 UINT count;
58 WCHAR **newbuf = NULL;
59
60 if (script >= SCRIPT_MAX)
61 {
62 FIXME("Unknown script requested %u\n", script);
63 return ERROR_FUNCTION_FAILED;
64 }
65 TRACE("Scheduling action %s in script %u\n", debugstr_w(action), script);
66
67 count = package->script->ActionCount[script];
68 package->script->ActionCount[script]++;
69 if (count != 0) newbuf = msi_realloc( package->script->Actions[script],
70 package->script->ActionCount[script] * sizeof(WCHAR *) );
71 else newbuf = msi_alloc( sizeof(WCHAR *) );
72
73 newbuf[count] = strdupW( action );
74 package->script->Actions[script] = newbuf;
75 return ERROR_SUCCESS;
76 }
77
78 UINT msi_register_unique_action( MSIPACKAGE *package, const WCHAR *action )
79 {
80 UINT count;
81 WCHAR **newbuf = NULL;
82
83 if (!package->script) return FALSE;
84
85 TRACE("Registering %s as unique action\n", debugstr_w(action));
86
87 count = package->script->UniqueActionsCount;
88 package->script->UniqueActionsCount++;
89 if (count != 0) newbuf = msi_realloc( package->script->UniqueActions,
90 package->script->UniqueActionsCount * sizeof(WCHAR *) );
91 else newbuf = msi_alloc( sizeof(WCHAR *) );
92
93 newbuf[count] = strdupW( action );
94 package->script->UniqueActions = newbuf;
95 return ERROR_SUCCESS;
96 }
97
98 BOOL msi_action_is_unique( const MSIPACKAGE *package, const WCHAR *action )
99 {
100 UINT i;
101
102 if (!package->script) return FALSE;
103
104 for (i = 0; i < package->script->UniqueActionsCount; i++)
105 {
106 if (!strcmpW( package->script->UniqueActions[i], action )) return TRUE;
107 }
108 return FALSE;
109 }
110
111 static BOOL check_execution_scheduling_options(MSIPACKAGE *package, LPCWSTR action, UINT options)
112 {
113 if (!package->script)
114 return TRUE;
115
116 if ((options & msidbCustomActionTypeClientRepeat) ==
117 msidbCustomActionTypeClientRepeat)
118 {
119 if (!(package->script->InWhatSequence & SEQUENCE_UI &&
120 package->script->InWhatSequence & SEQUENCE_EXEC))
121 {
122 TRACE("Skipping action due to dbCustomActionTypeClientRepeat option.\n");
123 return FALSE;
124 }
125 }
126 else if (options & msidbCustomActionTypeFirstSequence)
127 {
128 if (package->script->InWhatSequence & SEQUENCE_UI &&
129 package->script->InWhatSequence & SEQUENCE_EXEC )
130 {
131 TRACE("Skipping action due to msidbCustomActionTypeFirstSequence option.\n");
132 return FALSE;
133 }
134 }
135 else if (options & msidbCustomActionTypeOncePerProcess)
136 {
137 if (msi_action_is_unique(package, action))
138 {
139 TRACE("Skipping action due to msidbCustomActionTypeOncePerProcess option.\n");
140 return FALSE;
141 }
142 else
143 msi_register_unique_action(package, action);
144 }
145
146 return TRUE;
147 }
148
149 /* stores the following properties before the action:
150 *
151 * [CustomActionData<=>UserSID<=>ProductCode]Action
152 */
153 static LPWSTR msi_get_deferred_action(LPCWSTR action, LPCWSTR actiondata,
154 LPCWSTR usersid, LPCWSTR prodcode)
155 {
156 LPWSTR deferred;
157 DWORD len;
158
159 static const WCHAR format[] = {
160 '[','%','s','<','=','>','%','s','<','=','>','%','s',']','%','s',0
161 };
162
163 if (!actiondata)
164 return strdupW(action);
165
166 len = lstrlenW(action) + lstrlenW(actiondata) +
167 lstrlenW(usersid) + lstrlenW(prodcode) +
168 lstrlenW(format) - 7;
169 deferred = msi_alloc(len * sizeof(WCHAR));
170
171 sprintfW(deferred, format, actiondata, usersid, prodcode, action);
172 return deferred;
173 }
174
175 static void set_deferred_action_props( MSIPACKAGE *package, const WCHAR *deferred_data )
176 {
177 static const WCHAR sep[] = {'<','=','>',0};
178 const WCHAR *end, *beg = deferred_data + 1;
179
180 end = strstrW(beg, sep);
181 msi_set_property( package->db, szCustomActionData, beg, end - beg );
182 beg = end + 3;
183
184 end = strstrW(beg, sep);
185 msi_set_property( package->db, szUserSID, beg, end - beg );
186 beg = end + 3;
187
188 end = strchrW(beg, ']');
189 msi_set_property( package->db, szProductCode, beg, end - beg );
190 }
191
192 WCHAR *msi_create_temp_file( MSIDATABASE *db )
193 {
194 WCHAR *ret;
195
196 if (!db->tempfolder)
197 {
198 WCHAR tmp[MAX_PATH];
199 UINT len = sizeof(tmp)/sizeof(tmp[0]);
200
201 if (msi_get_property( db, szTempFolder, tmp, &len ) ||
202 GetFileAttributesW( tmp ) != FILE_ATTRIBUTE_DIRECTORY)
203 {
204 GetTempPathW( MAX_PATH, tmp );
205 }
206 if (!(db->tempfolder = strdupW( tmp ))) return NULL;
207 }
208
209 if ((ret = msi_alloc( (strlenW( db->tempfolder ) + 20) * sizeof(WCHAR) )))
210 {
211 if (!GetTempFileNameW( db->tempfolder, szMsi, 0, ret ))
212 {
213 msi_free( ret );
214 return NULL;
215 }
216 }
217
218 return ret;
219 }
220
221 static MSIBINARY *create_temp_binary( MSIPACKAGE *package, LPCWSTR source, BOOL dll )
222 {
223 static const WCHAR query[] = {
224 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
225 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',' ',
226 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
227 MSIRECORD *row;
228 MSIBINARY *binary = NULL;
229 HANDLE file;
230 CHAR buffer[1024];
231 WCHAR *tmpfile;
232 DWORD sz, write;
233 UINT r;
234
235 if (!(tmpfile = msi_create_temp_file( package->db ))) return NULL;
236
237 if (!(row = MSI_QueryGetRecord( package->db, query, source ))) goto error;
238 if (!(binary = msi_alloc_zero( sizeof(MSIBINARY) ))) goto error;
239
240 file = CreateFileW( tmpfile, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
241 if (file == INVALID_HANDLE_VALUE) goto error;
242
243 do
244 {
245 sz = sizeof(buffer);
246 r = MSI_RecordReadStream( row, 2, buffer, &sz );
247 if (r != ERROR_SUCCESS)
248 {
249 ERR("Failed to get stream\n");
250 break;
251 }
252 WriteFile( file, buffer, sz, &write, NULL );
253 } while (sz == sizeof buffer);
254
255 CloseHandle( file );
256 if (r != ERROR_SUCCESS) goto error;
257
258 /* keep a reference to prevent the dll from being unloaded */
259 if (dll && !(binary->module = LoadLibraryW( tmpfile )))
260 {
261 WARN( "failed to load dll %s (%u)\n", debugstr_w( tmpfile ), GetLastError() );
262 }
263 binary->source = strdupW( source );
264 binary->tmpfile = tmpfile;
265 list_add_tail( &package->binaries, &binary->entry );
266
267 msiobj_release( &row->hdr );
268 return binary;
269
270 error:
271 if (row) msiobj_release( &row->hdr );
272 DeleteFileW( tmpfile );
273 msi_free( tmpfile );
274 msi_free( binary );
275 return NULL;
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 = NULL;
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) goto done;
1024
1025 buffer = msi_alloc( sz + 1 );
1026 if (!buffer)
1027 {
1028 r = ERROR_FUNCTION_FAILED;
1029 goto done;
1030 }
1031
1032 r = MSI_RecordReadStream(row, 2, buffer, &sz);
1033 if (r != ERROR_SUCCESS)
1034 goto done;
1035
1036 buffer[sz] = 0;
1037 bufferw = strdupAtoW(buffer);
1038 if (!bufferw)
1039 {
1040 r = ERROR_FUNCTION_FAILED;
1041 goto done;
1042 }
1043
1044 info = do_msidbCustomActionTypeScript( package, type, bufferw, target, action );
1045 r = wait_thread_handle( info );
1046
1047 done:
1048 msi_free(bufferw);
1049 msi_free(buffer);
1050 msiobj_release(&row->hdr);
1051 return r;
1052 }
1053
1054 static UINT HANDLE_CustomType21_22( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
1055 INT type, const WCHAR *action )
1056 {
1057 msi_custom_action_info *info;
1058 MSIFILE *file;
1059 HANDLE hFile;
1060 DWORD sz, szHighWord = 0, read;
1061 CHAR *buffer=NULL;
1062 WCHAR *bufferw=NULL;
1063 BOOL bRet;
1064 UINT r;
1065
1066 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1067
1068 file = msi_get_loaded_file(package, source);
1069 if (!file)
1070 {
1071 ERR("invalid file key %s\n", debugstr_w(source));
1072 return ERROR_FUNCTION_FAILED;
1073 }
1074
1075 hFile = CreateFileW(file->TargetPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
1076 if (hFile == INVALID_HANDLE_VALUE) return ERROR_FUNCTION_FAILED;
1077
1078 sz = GetFileSize(hFile, &szHighWord);
1079 if (sz == INVALID_FILE_SIZE || szHighWord != 0)
1080 {
1081 CloseHandle(hFile);
1082 return ERROR_FUNCTION_FAILED;
1083 }
1084 buffer = msi_alloc( sz + 1 );
1085 if (!buffer)
1086 {
1087 CloseHandle(hFile);
1088 return ERROR_FUNCTION_FAILED;
1089 }
1090 bRet = ReadFile(hFile, buffer, sz, &read, NULL);
1091 CloseHandle(hFile);
1092 if (!bRet)
1093 {
1094 r = ERROR_FUNCTION_FAILED;
1095 goto done;
1096 }
1097 buffer[read] = 0;
1098 bufferw = strdupAtoW(buffer);
1099 if (!bufferw)
1100 {
1101 r = ERROR_FUNCTION_FAILED;
1102 goto done;
1103 }
1104 info = do_msidbCustomActionTypeScript( package, type, bufferw, target, action );
1105 r = wait_thread_handle( info );
1106
1107 done:
1108 msi_free(bufferw);
1109 msi_free(buffer);
1110 return r;
1111 }
1112
1113 static UINT HANDLE_CustomType53_54( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
1114 INT type, const WCHAR *action )
1115 {
1116 msi_custom_action_info *info;
1117 WCHAR *prop;
1118
1119 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1120
1121 prop = msi_dup_property( package->db, source );
1122 if (!prop) return ERROR_SUCCESS;
1123
1124 info = do_msidbCustomActionTypeScript( package, type, prop, NULL, action );
1125 msi_free(prop);
1126 return wait_thread_handle( info );
1127 }
1128
1129 static BOOL action_type_matches_script( UINT type, UINT script )
1130 {
1131 switch (script)
1132 {
1133 case SCRIPT_NONE:
1134 case SCRIPT_INSTALL:
1135 return !(type & msidbCustomActionTypeCommit) && !(type & msidbCustomActionTypeRollback);
1136 case SCRIPT_COMMIT:
1137 return (type & msidbCustomActionTypeCommit);
1138 case SCRIPT_ROLLBACK:
1139 return (type & msidbCustomActionTypeRollback);
1140 default:
1141 ERR("unhandled script %u\n", script);
1142 }
1143 return FALSE;
1144 }
1145
1146 static UINT defer_custom_action( MSIPACKAGE *package, const WCHAR *action, UINT type )
1147 {
1148 WCHAR *actiondata = msi_dup_property( package->db, action );
1149 WCHAR *usersid = msi_dup_property( package->db, szUserSID );
1150 WCHAR *prodcode = msi_dup_property( package->db, szProductCode );
1151 WCHAR *deferred = msi_get_deferred_action( action, actiondata, usersid, prodcode );
1152
1153 if (!deferred)
1154 {
1155 msi_free( actiondata );
1156 msi_free( usersid );
1157 msi_free( prodcode );
1158 return ERROR_OUTOFMEMORY;
1159 }
1160 if (type & msidbCustomActionTypeCommit)
1161 {
1162 TRACE("deferring commit action\n");
1163 msi_schedule_action( package, SCRIPT_COMMIT, deferred );
1164 }
1165 else if (type & msidbCustomActionTypeRollback)
1166 {
1167 TRACE("deferring rollback action\n");
1168 msi_schedule_action( package, SCRIPT_ROLLBACK, deferred );
1169 }
1170 else
1171 {
1172 TRACE("deferring install action\n");
1173 msi_schedule_action( package, SCRIPT_INSTALL, deferred );
1174 }
1175
1176 msi_free( actiondata );
1177 msi_free( usersid );
1178 msi_free( prodcode );
1179 msi_free( deferred );
1180 return ERROR_SUCCESS;
1181 }
1182
1183 UINT ACTION_CustomAction( MSIPACKAGE *package, LPCWSTR action, UINT script )
1184 {
1185 static const WCHAR query[] = {
1186 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1187 '`','C','u','s','t','o','m','A','c','t','i','o','n','`',' ','W','H','E','R','E',' ',
1188 '`','A','c','t','i' ,'o','n','`',' ','=',' ','\'','%','s','\'',0};
1189 UINT rc = ERROR_SUCCESS;
1190 MSIRECORD *row;
1191 UINT type;
1192 const WCHAR *source, *target, *ptr, *deferred_data = NULL;
1193 WCHAR *deformated = NULL;
1194 int len;
1195
1196 /* deferred action: [properties]Action */
1197 if ((ptr = strrchrW(action, ']')))
1198 {
1199 deferred_data = action;
1200 action = ptr + 1;
1201 }
1202
1203 row = MSI_QueryGetRecord( package->db, query, action );
1204 if (!row)
1205 return ERROR_CALL_NOT_IMPLEMENTED;
1206
1207 type = MSI_RecordGetInteger(row,2);
1208 source = MSI_RecordGetString(row,3);
1209 target = MSI_RecordGetString(row,4);
1210
1211 TRACE("Handling custom action %s (%x %s %s)\n",debugstr_w(action),type,
1212 debugstr_w(source), debugstr_w(target));
1213
1214 /* handle some of the deferred actions */
1215 if (type & msidbCustomActionTypeTSAware)
1216 FIXME("msidbCustomActionTypeTSAware not handled\n");
1217
1218 if (type & msidbCustomActionTypeInScript)
1219 {
1220 if (type & msidbCustomActionTypeNoImpersonate)
1221 WARN("msidbCustomActionTypeNoImpersonate not handled\n");
1222
1223 if (!action_type_matches_script( type, script ))
1224 {
1225 rc = defer_custom_action( package, action, type );
1226 goto end;
1227 }
1228 else
1229 {
1230 LPWSTR actiondata = msi_dup_property( package->db, action );
1231
1232 if (type & msidbCustomActionTypeInScript)
1233 package->scheduled_action_running = TRUE;
1234
1235 if (type & msidbCustomActionTypeCommit)
1236 package->commit_action_running = TRUE;
1237
1238 if (type & msidbCustomActionTypeRollback)
1239 package->rollback_action_running = TRUE;
1240
1241 if (deferred_data)
1242 set_deferred_action_props(package, deferred_data);
1243 else if (actiondata)
1244 msi_set_property( package->db, szCustomActionData, actiondata, -1 );
1245 else
1246 msi_set_property( package->db, szCustomActionData, szEmpty, -1 );
1247
1248 msi_free(actiondata);
1249 }
1250 }
1251 else if (!check_execution_scheduling_options(package,action,type))
1252 {
1253 rc = ERROR_SUCCESS;
1254 goto end;
1255 }
1256
1257 switch (type & CUSTOM_ACTION_TYPE_MASK)
1258 {
1259 case 1: /* DLL file stored in a Binary table stream */
1260 rc = HANDLE_CustomType1(package,source,target,type,action);
1261 break;
1262 case 2: /* EXE file stored in a Binary table stream */
1263 rc = HANDLE_CustomType2(package,source,target,type,action);
1264 break;
1265 case 18: /*EXE file installed with package */
1266 rc = HANDLE_CustomType18(package,source,target,type,action);
1267 break;
1268 case 19: /* Error that halts install */
1269 rc = HANDLE_CustomType19(package,source,target,type,action);
1270 break;
1271 case 17:
1272 rc = HANDLE_CustomType17(package,source,target,type,action);
1273 break;
1274 case 23: /* installs another package in the source tree */
1275 deformat_string(package,target,&deformated);
1276 rc = HANDLE_CustomType23(package,source,deformated,type,action);
1277 msi_free(deformated);
1278 break;
1279 case 50: /*EXE file specified by a property value */
1280 rc = HANDLE_CustomType50(package,source,target,type,action);
1281 break;
1282 case 34: /*EXE to be run in specified directory */
1283 rc = HANDLE_CustomType34(package,source,target,type,action);
1284 break;
1285 case 35: /* Directory set with formatted text. */
1286 deformat_string(package,target,&deformated);
1287 MSI_SetTargetPathW(package, source, deformated);
1288 msi_free(deformated);
1289 break;
1290 case 51: /* Property set with formatted text. */
1291 if (!source)
1292 break;
1293
1294 len = deformat_string( package, target, &deformated );
1295 rc = msi_set_property( package->db, source, deformated, len );
1296 if (rc == ERROR_SUCCESS && !strcmpW( source, szSourceDir ))
1297 msi_reset_folders( package, TRUE );
1298 msi_free(deformated);
1299 break;
1300 case 37: /* JScript/VBScript text stored in target column. */
1301 case 38:
1302 rc = HANDLE_CustomType37_38(package,source,target,type,action);
1303 break;
1304 case 5:
1305 case 6: /* JScript/VBScript file stored in a Binary table stream. */
1306 rc = HANDLE_CustomType5_6(package,source,target,type,action);
1307 break;
1308 case 21: /* JScript/VBScript file installed with the product. */
1309 case 22:
1310 rc = HANDLE_CustomType21_22(package,source,target,type,action);
1311 break;
1312 case 53: /* JScript/VBScript text specified by a property value. */
1313 case 54:
1314 rc = HANDLE_CustomType53_54(package,source,target,type,action);
1315 break;
1316 default:
1317 FIXME("unhandled action type %u (%s %s)\n", type & CUSTOM_ACTION_TYPE_MASK,
1318 debugstr_w(source), debugstr_w(target));
1319 }
1320
1321 end:
1322 package->scheduled_action_running = FALSE;
1323 package->commit_action_running = FALSE;
1324 package->rollback_action_running = FALSE;
1325 msiobj_release(&row->hdr);
1326 return rc;
1327 }
1328
1329 void ACTION_FinishCustomActions(const MSIPACKAGE* package)
1330 {
1331 struct list *item;
1332 HANDLE *wait_handles;
1333 unsigned int handle_count, i;
1334 msi_custom_action_info *info, *cursor;
1335
1336 while ((item = list_head( &package->RunningActions )))
1337 {
1338 MSIRUNNINGACTION *action = LIST_ENTRY( item, MSIRUNNINGACTION, entry );
1339
1340 list_remove( &action->entry );
1341
1342 TRACE("waiting for %s\n", debugstr_w( action->name ) );
1343 msi_dialog_check_messages( action->handle );
1344
1345 CloseHandle( action->handle );
1346 msi_free( action->name );
1347 msi_free( action );
1348 }
1349
1350 EnterCriticalSection( &msi_custom_action_cs );
1351
1352 handle_count = list_count( &msi_pending_custom_actions );
1353 wait_handles = msi_alloc( handle_count * sizeof(HANDLE) );
1354
1355 handle_count = 0;
1356 LIST_FOR_EACH_ENTRY_SAFE( info, cursor, &msi_pending_custom_actions, msi_custom_action_info, entry )
1357 {
1358 if (info->package == package )
1359 {
1360 if (DuplicateHandle(GetCurrentProcess(), info->handle, GetCurrentProcess(), &wait_handles[handle_count], SYNCHRONIZE, FALSE, 0))
1361 handle_count++;
1362 }
1363 }
1364
1365 LeaveCriticalSection( &msi_custom_action_cs );
1366
1367 for (i = 0; i < handle_count; i++)
1368 {
1369 msi_dialog_check_messages( wait_handles[i] );
1370 CloseHandle( wait_handles[i] );
1371 }
1372 msi_free( wait_handles );
1373
1374 EnterCriticalSection( &msi_custom_action_cs );
1375 LIST_FOR_EACH_ENTRY_SAFE( info, cursor, &msi_pending_custom_actions, msi_custom_action_info, entry )
1376 {
1377 if (info->package == package) release_custom_action_data( info );
1378 }
1379 LeaveCriticalSection( &msi_custom_action_cs );
1380 }
1381
1382 typedef struct _msi_custom_remote_impl {
1383 IWineMsiRemoteCustomAction IWineMsiRemoteCustomAction_iface;
1384 LONG refs;
1385 } msi_custom_remote_impl;
1386
1387 static inline msi_custom_remote_impl *impl_from_IWineMsiRemoteCustomAction( IWineMsiRemoteCustomAction *iface )
1388 {
1389 return CONTAINING_RECORD(iface, msi_custom_remote_impl, IWineMsiRemoteCustomAction_iface);
1390 }
1391
1392 static HRESULT WINAPI mcr_QueryInterface( IWineMsiRemoteCustomAction *iface,
1393 REFIID riid,LPVOID *ppobj)
1394 {
1395 if( IsEqualCLSID( riid, &IID_IUnknown ) ||
1396 IsEqualCLSID( riid, &IID_IWineMsiRemoteCustomAction ) )
1397 {
1398 IWineMsiRemoteCustomAction_AddRef( iface );
1399 *ppobj = iface;
1400 return S_OK;
1401 }
1402
1403 return E_NOINTERFACE;
1404 }
1405
1406 static ULONG WINAPI mcr_AddRef( IWineMsiRemoteCustomAction *iface )
1407 {
1408 msi_custom_remote_impl* This = impl_from_IWineMsiRemoteCustomAction( iface );
1409
1410 return InterlockedIncrement( &This->refs );
1411 }
1412
1413 static ULONG WINAPI mcr_Release( IWineMsiRemoteCustomAction *iface )
1414 {
1415 msi_custom_remote_impl* This = impl_from_IWineMsiRemoteCustomAction( iface );
1416 ULONG r;
1417
1418 r = InterlockedDecrement( &This->refs );
1419 if (r == 0)
1420 msi_free( This );
1421 return r;
1422 }
1423
1424 static HRESULT WINAPI mcr_GetActionInfo( IWineMsiRemoteCustomAction *iface, LPCGUID custom_action_guid,
1425 INT *type, MSIHANDLE *handle, BSTR *dll, BSTR *func, IWineMsiRemotePackage **remote_package )
1426 {
1427 msi_custom_action_info *info;
1428
1429 info = find_action_by_guid( custom_action_guid );
1430 if (!info)
1431 return E_FAIL;
1432
1433 *type = info->type;
1434 *handle = alloc_msihandle( &info->package->hdr );
1435 *dll = SysAllocString( info->source );
1436 *func = SysAllocString( info->target );
1437
1438 release_custom_action_data( info );
1439 return create_msi_remote_package( NULL, (LPVOID *)remote_package );
1440 }
1441
1442 static const IWineMsiRemoteCustomActionVtbl msi_custom_remote_vtbl =
1443 {
1444 mcr_QueryInterface,
1445 mcr_AddRef,
1446 mcr_Release,
1447 mcr_GetActionInfo,
1448 };
1449
1450 HRESULT create_msi_custom_remote( IUnknown *pOuter, LPVOID *ppObj )
1451 {
1452 msi_custom_remote_impl* This;
1453
1454 This = msi_alloc( sizeof *This );
1455 if (!This)
1456 return E_OUTOFMEMORY;
1457
1458 This->IWineMsiRemoteCustomAction_iface.lpVtbl = &msi_custom_remote_vtbl;
1459 This->refs = 1;
1460
1461 *ppObj = &This->IWineMsiRemoteCustomAction_iface;
1462
1463 return S_OK;
1464 }