msi: Use an array instead of a hashtable for WHEREVIEW.
[wine] / dlls / 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 "config.h"
22 #include "wine/port.h"
23
24 #define COBJMACROS
25
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"
34
35 #include "msipriv.h"
36 #include "msiserver.h"
37 #include "wine/debug.h"
38 #include "wine/unicode.h"
39 #include "wine/exception.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(msi);
42
43 #define CUSTOM_ACTION_TYPE_MASK 0x3F
44
45 typedef struct tagMSIRUNNINGACTION
46 {
47     struct list entry;
48     HANDLE handle;
49     BOOL   process;
50     LPWSTR name;
51 } MSIRUNNINGACTION;
52
53 typedef UINT (WINAPI *MsiCustomActionEntryPoint)( MSIHANDLE );
54
55 static CRITICAL_SECTION msi_custom_action_cs;
56 static CRITICAL_SECTION_DEBUG msi_custom_action_cs_debug =
57 {
58     0, 0, &msi_custom_action_cs,
59     { &msi_custom_action_cs_debug.ProcessLocksList,
60       &msi_custom_action_cs_debug.ProcessLocksList },
61       0, 0, { (DWORD_PTR)(__FILE__ ": msi_custom_action_cs") }
62 };
63 static CRITICAL_SECTION msi_custom_action_cs = { &msi_custom_action_cs_debug, -1, 0, 0, 0, 0 };
64
65 static struct list msi_pending_custom_actions = LIST_INIT( msi_pending_custom_actions );
66
67 UINT msi_schedule_action( MSIPACKAGE *package, UINT script, const WCHAR *action )
68 {
69     UINT count;
70     WCHAR **newbuf = NULL;
71
72     if (script >= TOTAL_SCRIPTS)
73     {
74         FIXME("Unknown script requested %u\n", script);
75         return ERROR_FUNCTION_FAILED;
76     }
77     TRACE("Scheduling action %s in script %u\n", debugstr_w(action), script);
78
79     count = package->script->ActionCount[script];
80     package->script->ActionCount[script]++;
81     if (count != 0) newbuf = msi_realloc( package->script->Actions[script],
82                                           package->script->ActionCount[script] * sizeof(WCHAR *) );
83     else newbuf = msi_alloc( sizeof(WCHAR *) );
84
85     newbuf[count] = strdupW( action );
86     package->script->Actions[script] = newbuf;
87     return ERROR_SUCCESS;
88 }
89
90 UINT msi_register_unique_action( MSIPACKAGE *package, const WCHAR *action )
91 {
92     UINT count;
93     WCHAR **newbuf = NULL;
94
95     if (!package->script) return FALSE;
96
97     TRACE("Registering %s as unique action\n", debugstr_w(action));
98
99     count = package->script->UniqueActionsCount;
100     package->script->UniqueActionsCount++;
101     if (count != 0) newbuf = msi_realloc( package->script->UniqueActions,
102                                           package->script->UniqueActionsCount * sizeof(WCHAR *) );
103     else newbuf = msi_alloc( sizeof(WCHAR *) );
104
105     newbuf[count] = strdupW( action );
106     package->script->UniqueActions = newbuf;
107     return ERROR_SUCCESS;
108 }
109
110 BOOL msi_action_is_unique( const MSIPACKAGE *package, const WCHAR *action )
111 {
112     UINT i;
113
114     if (!package->script) return FALSE;
115
116     for (i = 0; i < package->script->UniqueActionsCount; i++)
117     {
118         if (!strcmpW( package->script->UniqueActions[i], action )) return TRUE;
119     }
120     return FALSE;
121 }
122
123 static BOOL check_execution_scheduling_options(MSIPACKAGE *package, LPCWSTR action, UINT options)
124 {
125     if (!package->script)
126         return TRUE;
127
128     if ((options & msidbCustomActionTypeClientRepeat) ==
129             msidbCustomActionTypeClientRepeat)
130     {
131         if (!(package->script->InWhatSequence & SEQUENCE_UI &&
132             package->script->InWhatSequence & SEQUENCE_EXEC))
133         {
134             TRACE("Skipping action due to dbCustomActionTypeClientRepeat option.\n");
135             return FALSE;
136         }
137     }
138     else if (options & msidbCustomActionTypeFirstSequence)
139     {
140         if (package->script->InWhatSequence & SEQUENCE_UI &&
141             package->script->InWhatSequence & SEQUENCE_EXEC )
142         {
143             TRACE("Skipping action due to msidbCustomActionTypeFirstSequence option.\n");
144             return FALSE;
145         }
146     }
147     else if (options & msidbCustomActionTypeOncePerProcess)
148     {
149         if (msi_action_is_unique(package, action))
150         {
151             TRACE("Skipping action due to msidbCustomActionTypeOncePerProcess option.\n");
152             return FALSE;
153         }
154         else
155             msi_register_unique_action(package, action);
156     }
157
158     return TRUE;
159 }
160
161 /* stores the following properties before the action:
162  *
163  *    [CustomActionData<=>UserSID<=>ProductCode]Action
164  */
165 static LPWSTR msi_get_deferred_action(LPCWSTR action, LPCWSTR actiondata,
166                                       LPCWSTR usersid, LPCWSTR prodcode)
167 {
168     LPWSTR deferred;
169     DWORD len;
170
171     static const WCHAR format[] = {
172             '[','%','s','<','=','>','%','s','<','=','>','%','s',']','%','s',0
173     };
174
175     if (!actiondata)
176         return strdupW(action);
177
178     len = lstrlenW(action) + lstrlenW(actiondata) +
179           lstrlenW(usersid) + lstrlenW(prodcode) +
180           lstrlenW(format) - 7;
181     deferred = msi_alloc(len * sizeof(WCHAR));
182
183     sprintfW(deferred, format, actiondata, usersid, prodcode, action);
184     return deferred;
185 }
186
187 static void set_deferred_action_props(MSIPACKAGE *package, LPWSTR deferred_data)
188 {
189     LPWSTR end, beg = deferred_data + 1;
190
191     static const WCHAR sep[] = {'<','=','>',0};
192
193     end = strstrW(beg, sep);
194     *end = '\0';
195     msi_set_property(package->db, szCustomActionData, beg);
196     beg = end + 3;
197
198     end = strstrW(beg, sep);
199     *end = '\0';
200     msi_set_property(package->db, szUserSID, beg);
201     beg = end + 3;
202
203     end = strchrW(beg, ']');
204     *end = '\0';
205     msi_set_property(package->db, szProductCode, beg);
206 }
207
208 static MSIBINARY *create_temp_binary( MSIPACKAGE *package, LPCWSTR source, BOOL dll )
209 {
210     static const WCHAR query[] = {
211         'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
212         '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',' ',
213         '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
214     MSIRECORD *row;
215     MSIBINARY *binary;
216     HANDLE file;
217     CHAR buffer[1024];
218     WCHAR fmt[MAX_PATH], tmpfile[MAX_PATH];
219     DWORD sz = MAX_PATH, write;
220     UINT r;
221
222     if (msi_get_property(package->db, szTempFolder, fmt, &sz) != ERROR_SUCCESS)
223         GetTempPathW(MAX_PATH, fmt);
224
225     if (!GetTempFileNameW( fmt, szMsi, 0, tmpfile ))
226     {
227         TRACE("unable to create temp file %s (%u)\n", debugstr_w(tmpfile), GetLastError());
228         return NULL;
229     }
230
231     row = MSI_QueryGetRecord(package->db, query, source);
232     if (!row)
233         return NULL;
234
235     if (!(binary = msi_alloc_zero( sizeof(MSIBINARY) )))
236     {
237         msiobj_release( &row->hdr );
238         return NULL;
239     }
240     file = CreateFileW( tmpfile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
241     if (file == INVALID_HANDLE_VALUE)
242     {
243         msiobj_release( &row->hdr );
244         msi_free( binary );
245         return NULL;
246     }
247     do
248     {
249         sz = sizeof(buffer);
250         r = MSI_RecordReadStream( row, 2, buffer, &sz );
251         if (r != ERROR_SUCCESS)
252         {
253             ERR("Failed to get stream\n");
254             break;
255         }
256         WriteFile( file, buffer, sz, &write, NULL );
257     } while (sz == sizeof buffer);
258
259     CloseHandle( file );
260     msiobj_release( &row->hdr );
261     if (r != ERROR_SUCCESS)
262     {
263         DeleteFileW( tmpfile );
264         msi_free( binary );
265         return NULL;
266     }
267
268     /* keep a reference to prevent the dll from being unloaded */
269     if (dll && !(binary->module = LoadLibraryW( tmpfile )))
270     {
271         WARN( "failed to load dll %s (%u)\n", debugstr_w( tmpfile ), GetLastError() );
272     }
273     binary->source = strdupW( source );
274     binary->tmpfile = strdupW( tmpfile );
275     list_add_tail( &package->binaries, &binary->entry );
276     return binary;
277 }
278
279 static MSIBINARY *get_temp_binary( MSIPACKAGE *package, LPCWSTR source, BOOL dll )
280 {
281     MSIBINARY *binary;
282
283     LIST_FOR_EACH_ENTRY( binary, &package->binaries, MSIBINARY, entry )
284     {
285         if (!strcmpW( binary->source, source ))
286             return binary;
287     }
288
289     return create_temp_binary( package, source, dll );
290 }
291
292 static void file_running_action(MSIPACKAGE* package, HANDLE Handle,
293                                 BOOL process, LPCWSTR name)
294 {
295     MSIRUNNINGACTION *action;
296
297     action = msi_alloc( sizeof(MSIRUNNINGACTION) );
298
299     action->handle = Handle;
300     action->process = process;
301     action->name = strdupW(name);
302
303     list_add_tail( &package->RunningActions, &action->entry );
304 }
305
306 static UINT custom_get_process_return( HANDLE process )
307 {
308     DWORD rc = 0;
309
310     GetExitCodeProcess( process, &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 DWORD ACTION_CAInstallPackage(const GUID *guid)
626 {
627     msi_custom_action_info *info;
628     UINT r = ERROR_FUNCTION_FAILED;
629     INSTALLUILEVEL old_level;
630
631     info = find_action_by_guid(guid);
632     if (!info)
633     {
634         ERR("failed to find action %s\n", debugstr_guid(guid));
635         return r;
636     }
637
638     old_level = MsiSetInternalUI(INSTALLUILEVEL_BASIC, NULL);
639     r = MsiInstallProductW(info->source, info->target);
640     MsiSetInternalUI(old_level, NULL);
641
642     release_custom_action_data(info);
643
644     return r;
645 }
646
647 static DWORD WINAPI ConcurrentInstallThread(LPVOID arg)
648 {
649     LPGUID guid = arg;
650     DWORD rc;
651
652     TRACE("concurrent installation (%x) started\n", GetCurrentThreadId());
653
654     rc = ACTION_CAInstallPackage(guid);
655
656     TRACE("concurrent installation (%x) returned %i\n", GetCurrentThreadId(), rc);
657
658     MsiCloseAllHandles();
659     return rc;
660 }
661
662 static msi_custom_action_info *do_msidbCustomActionTypeDll(
663     MSIPACKAGE *package, INT type, LPCWSTR source, LPCWSTR target, LPCWSTR action )
664 {
665     msi_custom_action_info *info;
666
667     info = msi_alloc( sizeof *info );
668     if (!info)
669         return NULL;
670
671     msiobj_addref( &package->hdr );
672     info->refs = 2; /* 1 for our caller and 1 for thread we created */
673     info->package = package;
674     info->type = type;
675     info->target = strdupW( target );
676     info->source = strdupW( source );
677     info->action = strdupW( action );
678     CoCreateGuid( &info->guid );
679
680     EnterCriticalSection( &msi_custom_action_cs );
681     list_add_tail( &msi_pending_custom_actions, &info->entry );
682     LeaveCriticalSection( &msi_custom_action_cs );
683
684     info->handle = CreateThread( NULL, 0, DllThread, &info->guid, 0, NULL );
685     if (!info->handle)
686     {
687         /* release both references */
688         release_custom_action_data( info );
689         release_custom_action_data( info );
690         return NULL;
691     }
692
693     return info;
694 }
695
696 static msi_custom_action_info *do_msidbCAConcurrentInstall(
697     MSIPACKAGE *package, INT type, LPCWSTR source, LPCWSTR target, LPCWSTR action)
698 {
699     msi_custom_action_info *info;
700
701     info = msi_alloc( sizeof *info );
702     if (!info)
703         return NULL;
704
705     msiobj_addref( &package->hdr );
706     info->refs = 2; /* 1 for our caller and 1 for thread we created */
707     info->package = package;
708     info->type = type;
709     info->target = strdupW( target );
710     info->source = strdupW( source );
711     info->action = strdupW( action );
712     CoCreateGuid( &info->guid );
713
714     EnterCriticalSection( &msi_custom_action_cs );
715     list_add_tail( &msi_pending_custom_actions, &info->entry );
716     LeaveCriticalSection( &msi_custom_action_cs );
717
718     info->handle = CreateThread( NULL, 0, ConcurrentInstallThread, &info->guid, 0, NULL );
719     if (!info->handle)
720     {
721         /* release both references */
722         release_custom_action_data( info );
723         release_custom_action_data( info );
724         return NULL;
725     }
726
727     return info;
728 }
729
730 static UINT HANDLE_CustomType23(MSIPACKAGE *package, LPCWSTR source,
731                                 LPCWSTR target, const INT type, LPCWSTR action)
732 {
733     msi_custom_action_info *info;
734     WCHAR package_path[MAX_PATH];
735     DWORD size;
736     UINT r;
737
738     size = MAX_PATH;
739     msi_get_property(package->db, szSourceDir, package_path, &size);
740     lstrcatW(package_path, szBackSlash);
741     lstrcatW(package_path, source);
742
743     TRACE("Installing package %s concurrently\n", debugstr_w(package_path));
744
745     info = do_msidbCAConcurrentInstall(package, type, package_path, target, action);
746
747     r = wait_thread_handle(info);
748     release_custom_action_data( info );
749     return r;
750 }
751
752 static UINT HANDLE_CustomType1(MSIPACKAGE *package, LPCWSTR source,
753                                LPCWSTR target, const INT type, LPCWSTR action)
754 {
755     msi_custom_action_info *info;
756     MSIBINARY *binary;
757     UINT r;
758
759     if (!(binary = get_temp_binary( package, source, TRUE )))
760         return ERROR_FUNCTION_FAILED;
761
762     TRACE("Calling function %s from %s\n", debugstr_w(target), debugstr_w(binary->tmpfile));
763
764     info = do_msidbCustomActionTypeDll( package, type, binary->tmpfile, target, action );
765
766     r = wait_thread_handle( info );
767     release_custom_action_data( info );
768     return r;
769 }
770
771 static HANDLE execute_command( const WCHAR *app, WCHAR *arg, const WCHAR *dir )
772 {
773     static const WCHAR dotexeW[] = {'.','e','x','e',0};
774     STARTUPINFOW si;
775     PROCESS_INFORMATION info;
776     WCHAR *exe = NULL, *cmd = NULL, *p;
777     BOOL ret;
778
779     if (app)
780     {
781         int len_arg = 0;
782         DWORD len_exe;
783
784         if (!(exe = msi_alloc( MAX_PATH * sizeof(WCHAR) ))) return INVALID_HANDLE_VALUE;
785         len_exe = SearchPathW( NULL, app, dotexeW, MAX_PATH, exe, NULL );
786         if (len_exe >= MAX_PATH)
787         {
788             msi_free( exe );
789             if (!(exe = msi_alloc( len_exe * sizeof(WCHAR) ))) return INVALID_HANDLE_VALUE;
790             len_exe = SearchPathW( NULL, app, dotexeW, len_exe, exe, NULL );
791         }
792         if (!len_exe)
793         {
794             WARN("can't find executable %u\n", GetLastError());
795             msi_free( exe );
796             return INVALID_HANDLE_VALUE;
797         }
798
799         if (arg) len_arg = strlenW( arg );
800         if (!(cmd = msi_alloc( (len_exe + len_arg + 4) * sizeof(WCHAR) )))
801         {
802             msi_free( exe );
803             return INVALID_HANDLE_VALUE;
804         }
805         p = cmd;
806         if (strchrW( exe, ' ' ))
807         {
808             *p++ = '\"';
809             memcpy( p, exe, len_exe * sizeof(WCHAR) );
810             p += len_exe;
811             *p++ = '\"';
812             *p = 0;
813         }
814         else
815         {
816             strcpyW( p, exe );
817             p += len_exe;
818         }
819         if (arg)
820         {
821             *p++ = ' ';
822             memcpy( p, arg, len_arg * sizeof(WCHAR) );
823             p[len_arg] = 0;
824         }
825     }
826     memset( &si, 0, sizeof(STARTUPINFOW) );
827     ret = CreateProcessW( exe, exe ? cmd : arg, NULL, NULL, FALSE, 0, NULL, dir, &si, &info );
828     msi_free( cmd );
829     msi_free( exe );
830     if (!ret)
831     {
832         WARN("unable to execute command %u\n", GetLastError());
833         return INVALID_HANDLE_VALUE;
834     }
835     CloseHandle( info.hThread );
836     return info.hProcess;
837 }
838
839 static UINT HANDLE_CustomType2(MSIPACKAGE *package, LPCWSTR source,
840                                LPCWSTR target, const INT type, LPCWSTR action)
841 {
842     MSIBINARY *binary;
843     HANDLE handle;
844     WCHAR *arg;
845
846     if (!(binary = get_temp_binary( package, source, FALSE ))) return ERROR_FUNCTION_FAILED;
847
848     deformat_string( package, target, &arg );
849     TRACE("exe %s arg %s\n", debugstr_w(binary->tmpfile), debugstr_w(arg));
850
851     handle = execute_command( binary->tmpfile, arg, szCRoot );
852     msi_free( arg );
853     if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
854     return wait_process_handle( package, type, handle, action );
855 }
856
857 static UINT HANDLE_CustomType17(MSIPACKAGE *package, LPCWSTR source,
858                                 LPCWSTR target, const INT type, LPCWSTR action)
859 {
860     msi_custom_action_info *info;
861     MSIFILE *file;
862     UINT r;
863
864     TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
865
866     file = msi_get_loaded_file( package, source );
867     if (!file)
868     {
869         ERR("invalid file key %s\n", debugstr_w( source ));
870         return ERROR_FUNCTION_FAILED;
871     }
872
873     info = do_msidbCustomActionTypeDll( package, type, file->TargetPath, target, action );
874
875     r = wait_thread_handle( info );
876     release_custom_action_data( info );
877     return r;
878 }
879
880 static UINT HANDLE_CustomType18(MSIPACKAGE *package, LPCWSTR source,
881                                 LPCWSTR target, const INT type, LPCWSTR action)
882 {
883     MSIFILE *file;
884     HANDLE handle;
885     WCHAR *arg;
886
887     if (!(file = msi_get_loaded_file( package, source ))) return ERROR_FUNCTION_FAILED;
888
889     deformat_string( package, target, &arg );
890     TRACE("exe %s arg %s\n", debugstr_w(file->TargetPath), debugstr_w(arg));
891
892     handle = execute_command( file->TargetPath, arg, szCRoot );
893     msi_free( arg );
894     if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
895     return wait_process_handle( package, type, handle, action );
896 }
897
898 static UINT HANDLE_CustomType19(MSIPACKAGE *package, LPCWSTR source,
899                                 LPCWSTR target, const INT type, LPCWSTR action)
900 {
901     static const WCHAR query[] = {
902       'S','E','L','E','C','T',' ','`','M','e','s','s','a','g','e','`',' ',
903       'F','R','O','M',' ','`','E','r','r','o','r','`',' ',
904       'W','H','E','R','E',' ','`','E','r','r','o','r','`',' ','=',' ',
905       '%','s',0
906     };
907     MSIRECORD *row = 0;
908     LPWSTR deformated = NULL;
909
910     deformat_string( package, target, &deformated );
911
912     /* first try treat the error as a number */
913     row = MSI_QueryGetRecord( package->db, query, deformated );
914     if( row )
915     {
916         LPCWSTR error = MSI_RecordGetString( row, 1 );
917         if ((gUILevel & INSTALLUILEVEL_MASK) != INSTALLUILEVEL_NONE)
918             MessageBoxW( NULL, error, NULL, MB_OK );
919         msiobj_release( &row->hdr );
920     }
921     else if ((gUILevel & INSTALLUILEVEL_MASK) != INSTALLUILEVEL_NONE)
922         MessageBoxW( NULL, deformated, NULL, MB_OK );
923
924     msi_free( deformated );
925
926     return ERROR_INSTALL_FAILURE;
927 }
928
929 static UINT HANDLE_CustomType50(MSIPACKAGE *package, LPCWSTR source,
930                                 LPCWSTR target, const INT type, LPCWSTR action)
931 {
932     WCHAR *exe, *arg;
933     HANDLE handle;
934
935     if (!(exe = msi_dup_property( package->db, source ))) return ERROR_SUCCESS;
936
937     deformat_string( package, target, &arg );
938     TRACE("exe %s arg %s\n", debugstr_w(exe), debugstr_w(arg));
939
940     handle = execute_command( exe, arg, szCRoot );
941     msi_free( arg );
942     if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
943     return wait_process_handle( package, type, handle, action );
944 }
945
946 static UINT HANDLE_CustomType34(MSIPACKAGE *package, LPCWSTR source,
947                                 LPCWSTR target, const INT type, LPCWSTR action)
948 {
949     const WCHAR *workingdir;
950     HANDLE handle;
951     WCHAR *cmd;
952
953     workingdir = msi_get_target_folder( package, source );
954     if (!workingdir) return ERROR_FUNCTION_FAILED;
955
956     deformat_string( package, target, &cmd );
957     if (!cmd) return ERROR_FUNCTION_FAILED;
958
959     TRACE("cmd %s dir %s\n", debugstr_w(cmd), debugstr_w(workingdir));
960
961     handle = execute_command( NULL, cmd, workingdir );
962     msi_free( cmd );
963     if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
964     return wait_process_handle( package, type, handle, action );
965 }
966
967 static DWORD ACTION_CallScript( const GUID *guid )
968 {
969     msi_custom_action_info *info;
970     MSIHANDLE hPackage;
971     UINT r;
972
973     info = find_action_by_guid( guid );
974     if (!info)
975     {
976         ERR("failed to find action %s\n", debugstr_guid( guid) );
977         return ERROR_FUNCTION_FAILED;
978     }
979
980     TRACE("function %s, script %s\n", debugstr_w( info->target ), debugstr_w( info->source ) );
981
982     hPackage = alloc_msihandle( &info->package->hdr );
983     if (hPackage)
984     {
985         r = call_script( hPackage, info->type, info->source, info->target, info->action );
986         TRACE("script returned %u\n", r);
987         MsiCloseHandle( hPackage );
988     }
989     else
990         ERR("failed to create handle for %p\n", info->package );
991
992     release_custom_action_data( info );
993     return S_OK;
994 }
995
996 static DWORD WINAPI ScriptThread( LPVOID arg )
997 {
998     LPGUID guid = arg;
999     DWORD rc = 0;
1000
1001     TRACE("custom action (%x) started\n", GetCurrentThreadId() );
1002
1003     rc = ACTION_CallScript( guid );
1004
1005     TRACE("custom action (%x) returned %i\n", GetCurrentThreadId(), rc );
1006
1007     MsiCloseAllHandles();
1008     return rc;
1009 }
1010
1011 static msi_custom_action_info *do_msidbCustomActionTypeScript(
1012     MSIPACKAGE *package, INT type, LPCWSTR script, LPCWSTR function, LPCWSTR action )
1013 {
1014     msi_custom_action_info *info;
1015
1016     info = msi_alloc( sizeof *info );
1017     if (!info)
1018         return NULL;
1019
1020     msiobj_addref( &package->hdr );
1021     info->refs = 2; /* 1 for our caller and 1 for thread we created */
1022     info->package = package;
1023     info->type = type;
1024     info->target = strdupW( function );
1025     info->source = strdupW( script );
1026     info->action = strdupW( action );
1027     CoCreateGuid( &info->guid );
1028
1029     EnterCriticalSection( &msi_custom_action_cs );
1030     list_add_tail( &msi_pending_custom_actions, &info->entry );
1031     LeaveCriticalSection( &msi_custom_action_cs );
1032
1033     info->handle = CreateThread( NULL, 0, ScriptThread, &info->guid, 0, NULL );
1034     if (!info->handle)
1035     {
1036         /* release both references */
1037         release_custom_action_data( info );
1038         release_custom_action_data( info );
1039         return NULL;
1040     }
1041
1042     return info;
1043 }
1044
1045 static UINT HANDLE_CustomType37_38(MSIPACKAGE *package, LPCWSTR source,
1046                                LPCWSTR target, const INT type, LPCWSTR action)
1047 {
1048     UINT r;
1049     msi_custom_action_info *info;
1050
1051     TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1052
1053     info = do_msidbCustomActionTypeScript( package, type, target, NULL, action );
1054
1055     r = wait_thread_handle( info );
1056     release_custom_action_data( info );
1057     return r;
1058 }
1059
1060 static UINT HANDLE_CustomType5_6(MSIPACKAGE *package, LPCWSTR source,
1061                                LPCWSTR target, const INT type, LPCWSTR action)
1062 {
1063     static const WCHAR query[] = {
1064         'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1065         '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',' ',
1066         '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
1067     MSIRECORD *row = 0;
1068     msi_custom_action_info *info;
1069     CHAR *buffer = NULL;
1070     WCHAR *bufferw = NULL;
1071     DWORD sz = 0;
1072     UINT r;
1073
1074     TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1075
1076     row = MSI_QueryGetRecord(package->db, query, source);
1077     if (!row)
1078         return ERROR_FUNCTION_FAILED;
1079
1080     r = MSI_RecordReadStream(row, 2, NULL, &sz);
1081     if (r != ERROR_SUCCESS) return r;
1082
1083     buffer = msi_alloc( sz + 1 );
1084     if (!buffer) return ERROR_FUNCTION_FAILED;
1085
1086     r = MSI_RecordReadStream(row, 2, buffer, &sz);
1087     if (r != ERROR_SUCCESS)
1088         goto done;
1089
1090     buffer[sz] = 0;
1091     bufferw = strdupAtoW(buffer);
1092     if (!bufferw)
1093     {
1094         r = ERROR_FUNCTION_FAILED;
1095         goto done;
1096     }
1097
1098     info = do_msidbCustomActionTypeScript( package, type, bufferw, target, action );
1099     r = wait_thread_handle( info );
1100     release_custom_action_data( info );
1101
1102 done:
1103     msi_free(bufferw);
1104     msi_free(buffer);
1105     return r;
1106 }
1107
1108 static UINT HANDLE_CustomType21_22(MSIPACKAGE *package, LPCWSTR source,
1109                                LPCWSTR target, const INT type, LPCWSTR action)
1110 {
1111     msi_custom_action_info *info;
1112     MSIFILE *file;
1113     HANDLE hFile;
1114     DWORD sz, szHighWord = 0, read;
1115     CHAR *buffer=NULL;
1116     WCHAR *bufferw=NULL;
1117     BOOL bRet;
1118     UINT r;
1119
1120     TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1121
1122     file = msi_get_loaded_file(package, source);
1123     if (!file)
1124     {
1125         ERR("invalid file key %s\n", debugstr_w(source));
1126         return ERROR_FUNCTION_FAILED;
1127     }
1128
1129     hFile = CreateFileW(file->TargetPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
1130     if (hFile == INVALID_HANDLE_VALUE) return ERROR_FUNCTION_FAILED;
1131
1132     sz = GetFileSize(hFile, &szHighWord);
1133     if (sz == INVALID_FILE_SIZE || szHighWord != 0)
1134     {
1135         CloseHandle(hFile);
1136         return ERROR_FUNCTION_FAILED;
1137     }
1138     buffer = msi_alloc( sz + 1 );
1139     if (!buffer)
1140     {
1141         CloseHandle(hFile);
1142         return ERROR_FUNCTION_FAILED;
1143     }
1144     bRet = ReadFile(hFile, buffer, sz, &read, NULL);
1145     CloseHandle(hFile);
1146     if (!bRet)
1147     {
1148         r = ERROR_FUNCTION_FAILED;
1149         goto done;
1150     }
1151     buffer[read] = 0;
1152     bufferw = strdupAtoW(buffer);
1153     if (!bufferw)
1154     {
1155         r = ERROR_FUNCTION_FAILED;
1156         goto done;
1157     }
1158     info = do_msidbCustomActionTypeScript( package, type, bufferw, target, action );
1159     r = wait_thread_handle( info );
1160     release_custom_action_data( info );
1161
1162 done:
1163     msi_free(bufferw);
1164     msi_free(buffer);
1165     return r;
1166 }
1167
1168 static UINT HANDLE_CustomType53_54(MSIPACKAGE *package, LPCWSTR source,
1169                                LPCWSTR target, const INT type, LPCWSTR action)
1170 {
1171     msi_custom_action_info *info;
1172     WCHAR *prop;
1173     UINT r;
1174
1175     TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1176
1177     prop = msi_dup_property( package->db, source );
1178     if (!prop) return ERROR_SUCCESS;
1179
1180     info = do_msidbCustomActionTypeScript( package, type, prop, NULL, action );
1181     msi_free(prop);
1182     r = wait_thread_handle( info );
1183     release_custom_action_data( info );
1184     return r;
1185 }
1186
1187 UINT ACTION_CustomAction(MSIPACKAGE *package, LPCWSTR action, UINT script, BOOL execute)
1188 {
1189     static const WCHAR query[] = {
1190         'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1191         '`','C','u','s','t','o','m','A','c','t','i','o','n','`',' ','W','H','E','R','E',' ',
1192         '`','A','c','t','i' ,'o','n','`',' ','=',' ','\'','%','s','\'',0};
1193     UINT rc = ERROR_SUCCESS;
1194     MSIRECORD *row;
1195     UINT type;
1196     LPCWSTR source, target;
1197     LPWSTR ptr, deferred_data = NULL;
1198     LPWSTR deformated = NULL, action_copy = strdupW(action);
1199
1200     /* deferred action: [properties]Action */
1201     if ((ptr = strrchrW(action_copy, ']')))
1202     {
1203         deferred_data = action_copy;
1204         action = ptr + 1;
1205     }
1206
1207     row = MSI_QueryGetRecord( package->db, query, action );
1208     if (!row)
1209     {
1210         msi_free(action_copy);
1211         return ERROR_CALL_NOT_IMPLEMENTED;
1212     }
1213
1214     type = MSI_RecordGetInteger(row,2);
1215
1216     source = MSI_RecordGetString(row,3);
1217     target = MSI_RecordGetString(row,4);
1218
1219     TRACE("Handling custom action %s (%x %s %s)\n",debugstr_w(action),type,
1220           debugstr_w(source), debugstr_w(target));
1221
1222     /* handle some of the deferred actions */
1223     if (type & msidbCustomActionTypeTSAware)
1224         FIXME("msidbCustomActionTypeTSAware not handled\n");
1225
1226     if (type & msidbCustomActionTypeInScript)
1227     {
1228         if (type & msidbCustomActionTypeNoImpersonate)
1229             WARN("msidbCustomActionTypeNoImpersonate not handled\n");
1230
1231         if (!execute)
1232         {
1233             LPWSTR actiondata = msi_dup_property(package->db, action);
1234             LPWSTR usersid = msi_dup_property(package->db, szUserSID);
1235             LPWSTR prodcode = msi_dup_property(package->db, szProductCode);
1236             LPWSTR deferred = msi_get_deferred_action(action, actiondata, usersid, prodcode);
1237
1238             if (type & msidbCustomActionTypeCommit)
1239             {
1240                 TRACE("Deferring commit action\n");
1241                 msi_schedule_action(package, COMMIT_SCRIPT, deferred);
1242             }
1243             else if (type & msidbCustomActionTypeRollback)
1244             {
1245                 TRACE("Deferring rollback action\n");
1246                 msi_schedule_action(package, ROLLBACK_SCRIPT, deferred);
1247             }
1248             else
1249             {
1250                 TRACE("Deferring action\n");
1251                 msi_schedule_action(package, INSTALL_SCRIPT, deferred);
1252             }
1253
1254             rc = ERROR_SUCCESS;
1255             msi_free(actiondata);
1256             msi_free(usersid);
1257             msi_free(prodcode);
1258             msi_free(deferred);
1259             goto end;
1260         }
1261         else
1262         {
1263             LPWSTR actiondata = msi_dup_property( package->db, action );
1264
1265             if (type & msidbCustomActionTypeInScript)
1266                 package->scheduled_action_running = TRUE;
1267
1268             if (type & msidbCustomActionTypeCommit)
1269                 package->commit_action_running = TRUE;
1270
1271             if (type & msidbCustomActionTypeRollback)
1272                 package->rollback_action_running = TRUE;
1273
1274             if (deferred_data)
1275                 set_deferred_action_props(package, deferred_data);
1276             else if (actiondata)
1277                 msi_set_property(package->db, szCustomActionData, actiondata);
1278             else
1279                 msi_set_property(package->db, szCustomActionData, szEmpty);
1280
1281             msi_free(actiondata);
1282         }
1283     }
1284     else if (!check_execution_scheduling_options(package,action,type))
1285     {
1286         rc = ERROR_SUCCESS;
1287         goto end;
1288     }
1289
1290     switch (type & CUSTOM_ACTION_TYPE_MASK)
1291     {
1292         case 1: /* DLL file stored in a Binary table stream */
1293             rc = HANDLE_CustomType1(package,source,target,type,action);
1294             break;
1295         case 2: /* EXE file stored in a Binary table stream */
1296             rc = HANDLE_CustomType2(package,source,target,type,action);
1297             break;
1298         case 18: /*EXE file installed with package */
1299             rc = HANDLE_CustomType18(package,source,target,type,action);
1300             break;
1301         case 19: /* Error that halts install */
1302             rc = HANDLE_CustomType19(package,source,target,type,action);
1303             break;
1304         case 17:
1305             rc = HANDLE_CustomType17(package,source,target,type,action);
1306             break;
1307         case 23: /* installs another package in the source tree */
1308             deformat_string(package,target,&deformated);
1309             rc = HANDLE_CustomType23(package,source,deformated,type,action);
1310             msi_free(deformated);
1311             break;
1312         case 50: /*EXE file specified by a property value */
1313             rc = HANDLE_CustomType50(package,source,target,type,action);
1314             break;
1315         case 34: /*EXE to be run in specified directory */
1316             rc = HANDLE_CustomType34(package,source,target,type,action);
1317             break;
1318         case 35: /* Directory set with formatted text. */
1319             deformat_string(package,target,&deformated);
1320             MSI_SetTargetPathW(package, source, deformated);
1321             msi_free(deformated);
1322             break;
1323         case 51: /* Property set with formatted text. */
1324             if (!source)
1325                 break;
1326
1327             deformat_string(package,target,&deformated);
1328             rc = msi_set_property( package->db, source, deformated );
1329             if (rc == ERROR_SUCCESS && !strcmpW( source, szSourceDir ))
1330                 msi_reset_folders( package, TRUE );
1331             msi_free(deformated);
1332             break;
1333     case 37: /* JScript/VBScript text stored in target column. */
1334     case 38:
1335         rc = HANDLE_CustomType37_38(package,source,target,type,action);
1336         break;
1337     case 5:
1338     case 6: /* JScript/VBScript file stored in a Binary table stream. */
1339         rc = HANDLE_CustomType5_6(package,source,target,type,action);
1340         break;
1341     case 21: /* JScript/VBScript file installed with the product. */
1342     case 22:
1343         rc = HANDLE_CustomType21_22(package,source,target,type,action);
1344         break;
1345     case 53: /* JScript/VBScript text specified by a property value. */
1346     case 54:
1347         rc = HANDLE_CustomType53_54(package,source,target,type,action);
1348         break;
1349     default:
1350         FIXME("unhandled action type %u (%s %s)\n", type & CUSTOM_ACTION_TYPE_MASK,
1351               debugstr_w(source), debugstr_w(target));
1352     }
1353
1354 end:
1355     package->scheduled_action_running = FALSE;
1356     package->commit_action_running = FALSE;
1357     package->rollback_action_running = FALSE;
1358     msi_free(action_copy);
1359     msiobj_release(&row->hdr);
1360     return rc;
1361 }
1362
1363 void ACTION_FinishCustomActions(const MSIPACKAGE* package)
1364 {
1365     struct list *item;
1366     HANDLE *wait_handles;
1367     unsigned int handle_count, i;
1368     msi_custom_action_info *info, *cursor;
1369
1370     while ((item = list_head( &package->RunningActions )))
1371     {
1372         MSIRUNNINGACTION *action = LIST_ENTRY( item, MSIRUNNINGACTION, entry );
1373
1374         list_remove( &action->entry );
1375
1376         TRACE("waiting for %s\n", debugstr_w( action->name ) );
1377         msi_dialog_check_messages( action->handle );
1378
1379         CloseHandle( action->handle );
1380         msi_free( action->name );
1381         msi_free( action );
1382     }
1383
1384     EnterCriticalSection( &msi_custom_action_cs );
1385
1386     handle_count = list_count( &msi_pending_custom_actions );
1387     wait_handles = msi_alloc( handle_count * sizeof(HANDLE) );
1388
1389     handle_count = 0;
1390     LIST_FOR_EACH_ENTRY_SAFE( info, cursor, &msi_pending_custom_actions, msi_custom_action_info, entry )
1391     {
1392         if (info->package == package )
1393         {
1394             if (DuplicateHandle(GetCurrentProcess(), info->handle, GetCurrentProcess(), &wait_handles[handle_count], SYNCHRONIZE, FALSE, 0))
1395                 handle_count++;
1396         }
1397     }
1398
1399     LeaveCriticalSection( &msi_custom_action_cs );
1400
1401     for (i = 0; i < handle_count; i++)
1402     {
1403         msi_dialog_check_messages( wait_handles[i] );
1404         CloseHandle( wait_handles[i] );
1405     }
1406
1407     msi_free( wait_handles );
1408 }
1409
1410 typedef struct _msi_custom_remote_impl {
1411     IWineMsiRemoteCustomAction IWineMsiRemoteCustomAction_iface;
1412     LONG refs;
1413 } msi_custom_remote_impl;
1414
1415 static inline msi_custom_remote_impl *impl_from_IWineMsiRemoteCustomAction( IWineMsiRemoteCustomAction *iface )
1416 {
1417     return CONTAINING_RECORD(iface, msi_custom_remote_impl, IWineMsiRemoteCustomAction_iface);
1418 }
1419
1420 static HRESULT WINAPI mcr_QueryInterface( IWineMsiRemoteCustomAction *iface,
1421                 REFIID riid,LPVOID *ppobj)
1422 {
1423     if( IsEqualCLSID( riid, &IID_IUnknown ) ||
1424         IsEqualCLSID( riid, &IID_IWineMsiRemoteCustomAction ) )
1425     {
1426         IUnknown_AddRef( iface );
1427         *ppobj = iface;
1428         return S_OK;
1429     }
1430
1431     return E_NOINTERFACE;
1432 }
1433
1434 static ULONG WINAPI mcr_AddRef( IWineMsiRemoteCustomAction *iface )
1435 {
1436     msi_custom_remote_impl* This = impl_from_IWineMsiRemoteCustomAction( iface );
1437
1438     return InterlockedIncrement( &This->refs );
1439 }
1440
1441 static ULONG WINAPI mcr_Release( IWineMsiRemoteCustomAction *iface )
1442 {
1443     msi_custom_remote_impl* This = impl_from_IWineMsiRemoteCustomAction( iface );
1444     ULONG r;
1445
1446     r = InterlockedDecrement( &This->refs );
1447     if (r == 0)
1448         msi_free( This );
1449     return r;
1450 }
1451
1452 static HRESULT WINAPI mcr_GetActionInfo( IWineMsiRemoteCustomAction *iface, LPCGUID custom_action_guid,
1453          INT *type, MSIHANDLE *handle, BSTR *dll, BSTR *func, IWineMsiRemotePackage **remote_package )
1454 {
1455     msi_custom_action_info *info;
1456
1457     info = find_action_by_guid( custom_action_guid );
1458     if (!info)
1459         return E_FAIL;
1460
1461     *type = info->type;
1462     *handle = alloc_msihandle( &info->package->hdr );
1463     *dll = SysAllocString( info->source );
1464     *func = SysAllocString( info->target );
1465
1466     release_custom_action_data( info );
1467     return create_msi_remote_package( NULL, (LPVOID *)remote_package );
1468 }
1469
1470 static const IWineMsiRemoteCustomActionVtbl msi_custom_remote_vtbl =
1471 {
1472     mcr_QueryInterface,
1473     mcr_AddRef,
1474     mcr_Release,
1475     mcr_GetActionInfo,
1476 };
1477
1478 HRESULT create_msi_custom_remote( IUnknown *pOuter, LPVOID *ppObj )
1479 {
1480     msi_custom_remote_impl* This;
1481
1482     This = msi_alloc( sizeof *This );
1483     if (!This)
1484         return E_OUTOFMEMORY;
1485
1486     This->IWineMsiRemoteCustomAction_iface.lpVtbl = &msi_custom_remote_vtbl;
1487     This->refs = 1;
1488
1489     *ppObj = This;
1490
1491     return S_OK;
1492 }