winex11: Always ignore alpha channel with XRender gradients.
[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     TRACE("exit code is %u\n", rc);
312     if (rc != 0)
313         return ERROR_FUNCTION_FAILED;
314     return ERROR_SUCCESS;
315 }
316
317 static UINT custom_get_thread_return( MSIPACKAGE *package, HANDLE thread )
318 {
319     DWORD rc = 0;
320
321     GetExitCodeThread( thread, &rc );
322
323     switch (rc)
324     {
325     case ERROR_FUNCTION_NOT_CALLED:
326     case ERROR_SUCCESS:
327     case ERROR_INSTALL_USEREXIT:
328     case ERROR_INSTALL_FAILURE:
329         return rc;
330     case ERROR_NO_MORE_ITEMS:
331         return ERROR_SUCCESS;
332     case ERROR_INSTALL_SUSPEND:
333         ACTION_ForceReboot( package );
334         return ERROR_SUCCESS;
335     default:
336         ERR("Invalid Return Code %d\n",rc);
337         return ERROR_INSTALL_FAILURE;
338     }
339 }
340
341 static UINT wait_process_handle(MSIPACKAGE* package, UINT type,
342                            HANDLE ProcessHandle, LPCWSTR name)
343 {
344     UINT rc = ERROR_SUCCESS;
345
346     if (!(type & msidbCustomActionTypeAsync))
347     {
348         TRACE("waiting for %s\n", debugstr_w(name));
349
350         msi_dialog_check_messages(ProcessHandle);
351
352         if (!(type & msidbCustomActionTypeContinue))
353             rc = custom_get_process_return(ProcessHandle);
354
355         CloseHandle(ProcessHandle);
356     }
357     else
358     {
359         TRACE("%s running in background\n", debugstr_w(name));
360
361         if (!(type & msidbCustomActionTypeContinue))
362             file_running_action(package, ProcessHandle, TRUE, name);
363         else
364             CloseHandle(ProcessHandle);
365     }
366
367     return rc;
368 }
369
370 typedef struct _msi_custom_action_info {
371     struct list entry;
372     LONG refs;
373     MSIPACKAGE *package;
374     LPWSTR source;
375     LPWSTR target;
376     HANDLE handle;
377     LPWSTR action;
378     INT type;
379     GUID guid;
380 } msi_custom_action_info;
381
382 static void release_custom_action_data( msi_custom_action_info *info )
383 {
384     EnterCriticalSection( &msi_custom_action_cs );
385
386     if (!--info->refs)
387     {
388         list_remove( &info->entry );
389         if (info->handle)
390             CloseHandle( info->handle );
391         msi_free( info->action );
392         msi_free( info->source );
393         msi_free( info->target );
394         msiobj_release( &info->package->hdr );
395         msi_free( info );
396     }
397
398     LeaveCriticalSection( &msi_custom_action_cs );
399 }
400
401 /* must be called inside msi_custom_action_cs if info is in the pending custom actions list */
402 static void addref_custom_action_data( msi_custom_action_info *info )
403 {
404     info->refs++;
405  }
406
407 static UINT wait_thread_handle( msi_custom_action_info *info )
408 {
409     UINT rc = ERROR_SUCCESS;
410
411     if (!(info->type & msidbCustomActionTypeAsync))
412     {
413         TRACE("waiting for %s\n", debugstr_w( info->action ));
414
415         msi_dialog_check_messages( info->handle );
416
417         if (!(info->type & msidbCustomActionTypeContinue))
418             rc = custom_get_thread_return( info->package, info->handle );
419
420         release_custom_action_data( info );
421     }
422     else
423     {
424         TRACE("%s running in background\n", debugstr_w( info->action ));
425     }
426
427     return rc;
428 }
429
430 static msi_custom_action_info *find_action_by_guid( const GUID *guid )
431 {
432     msi_custom_action_info *info;
433     BOOL found = FALSE;
434
435     EnterCriticalSection( &msi_custom_action_cs );
436
437     LIST_FOR_EACH_ENTRY( info, &msi_pending_custom_actions, msi_custom_action_info, entry )
438     {
439         if (IsEqualGUID( &info->guid, guid ))
440         {
441             addref_custom_action_data( info );
442             found = TRUE;
443             break;
444         }
445     }
446
447     LeaveCriticalSection( &msi_custom_action_cs );
448
449     if (!found)
450         return NULL;
451
452     return info;
453 }
454
455 static void handle_msi_break( LPCWSTR target )
456 {
457     LPWSTR msg;
458     WCHAR val[MAX_PATH];
459
460     static const WCHAR MsiBreak[] = { 'M','s','i','B','r','e','a','k',0 };
461     static const WCHAR WindowsInstaller[] = {
462         'W','i','n','d','o','w','s',' ','I','n','s','t','a','l','l','e','r',0
463     };
464
465     static const WCHAR format[] = {
466         'T','o',' ','d','e','b','u','g',' ','y','o','u','r',' ',
467         'c','u','s','t','o','m',' ','a','c','t','i','o','n',',',' ',
468         'a','t','t','a','c','h',' ','y','o','u','r',' ','d','e','b','u','g','g','e','r',' ',
469         't','o',' ','p','r','o','c','e','s','s',' ','%','i',' ','(','0','x','%','X',')',' ',
470         'a','n','d',' ','p','r','e','s','s',' ','O','K',0
471     };
472
473     if( !GetEnvironmentVariableW( MsiBreak, val, MAX_PATH ))
474         return;
475
476     if( strcmpiW( val, target ))
477         return;
478
479     msg = msi_alloc( (lstrlenW(format) + 10) * sizeof(WCHAR) );
480     if (!msg)
481         return;
482
483     wsprintfW( msg, format, GetCurrentProcessId(), GetCurrentProcessId());
484     MessageBoxW( NULL, msg, WindowsInstaller, MB_OK);
485     msi_free(msg);
486     DebugBreak();
487 }
488
489 static UINT get_action_info( const GUID *guid, INT *type, MSIHANDLE *handle,
490                              BSTR *dll, BSTR *funcname,
491                              IWineMsiRemotePackage **package )
492 {
493     IClassFactory *cf = NULL;
494     IWineMsiRemoteCustomAction *rca = NULL;
495     HRESULT r;
496
497     r = DllGetClassObject( &CLSID_WineMsiRemoteCustomAction,
498                            &IID_IClassFactory, (LPVOID *)&cf );
499     if (FAILED(r))
500     {
501         ERR("failed to get IClassFactory interface\n");
502         return ERROR_FUNCTION_FAILED;
503     }
504
505     r = IClassFactory_CreateInstance( cf, NULL, &IID_IWineMsiRemoteCustomAction, (LPVOID *)&rca );
506     if (FAILED(r))
507     {
508         ERR("failed to get IWineMsiRemoteCustomAction interface\n");
509         return ERROR_FUNCTION_FAILED;
510     }
511
512     r = IWineMsiRemoteCustomAction_GetActionInfo( rca, guid, type, handle, dll, funcname, package );
513     IWineMsiRemoteCustomAction_Release( rca );
514     if (FAILED(r))
515     {
516         ERR("GetActionInfo failed\n");
517         return ERROR_FUNCTION_FAILED;
518     }
519
520     return ERROR_SUCCESS;
521 }
522
523 #ifdef __i386__
524 extern UINT CUSTOMPROC_wrapper( MsiCustomActionEntryPoint proc, MSIHANDLE handle );
525 __ASM_GLOBAL_FUNC( CUSTOMPROC_wrapper,
526     "pushl %ebp\n\t"
527     __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
528     __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
529     "movl %esp,%ebp\n\t"
530     __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
531     "pushl 12(%ebp)\n\t"
532     "movl 8(%ebp),%eax\n\t"
533     "call *%eax\n\t"
534     "leave\n\t"
535     __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
536     __ASM_CFI(".cfi_same_value %ebp\n\t")
537     "ret" )
538 #else
539 static inline UINT CUSTOMPROC_wrapper( MsiCustomActionEntryPoint proc, MSIHANDLE handle )
540 {
541     return proc(handle);
542 }
543 #endif
544
545 static DWORD ACTION_CallDllFunction( const GUID *guid )
546 {
547     MsiCustomActionEntryPoint fn;
548     MSIHANDLE hPackage, handle;
549     HANDLE hModule;
550     LPSTR proc;
551     UINT r = ERROR_FUNCTION_FAILED;
552     BSTR dll = NULL, function = NULL;
553     INT type;
554     IWineMsiRemotePackage *remote_package = NULL;
555
556     TRACE("%s\n", debugstr_guid( guid ));
557
558     r = get_action_info( guid, &type, &handle, &dll, &function, &remote_package );
559     if (r != ERROR_SUCCESS)
560         return r;
561
562     hModule = LoadLibraryW( dll );
563     if (!hModule)
564     {
565         WARN( "failed to load dll %s (%u)\n", debugstr_w( dll ), GetLastError() );
566         return ERROR_SUCCESS;
567     }
568
569     proc = strdupWtoA( function );
570     fn = (MsiCustomActionEntryPoint) GetProcAddress( hModule, proc );
571     msi_free( proc );
572     if (fn)
573     {
574         hPackage = alloc_msi_remote_handle( (IUnknown *)remote_package );
575         if (hPackage)
576         {
577             IWineMsiRemotePackage_SetMsiHandle( remote_package, handle );
578             TRACE("calling %s\n", debugstr_w( function ) );
579             handle_msi_break( function );
580
581             __TRY
582             {
583                 r = CUSTOMPROC_wrapper( fn, hPackage );
584             }
585             __EXCEPT_PAGE_FAULT
586             {
587                 ERR("Custom action (%s:%s) caused a page fault: %08x\n",
588                     debugstr_w(dll), debugstr_w(function), GetExceptionCode());
589                 r = ERROR_SUCCESS;
590             }
591             __ENDTRY;
592
593             MsiCloseHandle( hPackage );
594         }
595         else
596             ERR("failed to create handle for %p\n", remote_package );
597     }
598     else
599         ERR("GetProcAddress(%s) failed\n", debugstr_w( function ) );
600
601     FreeLibrary(hModule);
602
603     IWineMsiRemotePackage_Release( remote_package );
604     SysFreeString( dll );
605     SysFreeString( function );
606     MsiCloseHandle( handle );
607
608     return r;
609 }
610
611 static DWORD WINAPI DllThread( LPVOID arg )
612 {
613     LPGUID guid = arg;
614     DWORD rc = 0;
615
616     TRACE("custom action (%x) started\n", GetCurrentThreadId() );
617
618     rc = ACTION_CallDllFunction( guid );
619
620     TRACE("custom action (%x) returned %i\n", GetCurrentThreadId(), rc );
621
622     MsiCloseAllHandles();
623     return rc;
624 }
625
626 static DWORD ACTION_CAInstallPackage(const GUID *guid)
627 {
628     msi_custom_action_info *info;
629     UINT r = ERROR_FUNCTION_FAILED;
630     INSTALLUILEVEL old_level;
631
632     info = find_action_by_guid(guid);
633     if (!info)
634     {
635         ERR("failed to find action %s\n", debugstr_guid(guid));
636         return r;
637     }
638
639     old_level = MsiSetInternalUI(INSTALLUILEVEL_BASIC, NULL);
640     r = MsiInstallProductW(info->source, info->target);
641     MsiSetInternalUI(old_level, NULL);
642
643     release_custom_action_data(info);
644
645     return r;
646 }
647
648 static DWORD WINAPI ConcurrentInstallThread(LPVOID arg)
649 {
650     LPGUID guid = arg;
651     DWORD rc;
652
653     TRACE("concurrent installation (%x) started\n", GetCurrentThreadId());
654
655     rc = ACTION_CAInstallPackage(guid);
656
657     TRACE("concurrent installation (%x) returned %i\n", GetCurrentThreadId(), rc);
658
659     MsiCloseAllHandles();
660     return rc;
661 }
662
663 static msi_custom_action_info *do_msidbCustomActionTypeDll(
664     MSIPACKAGE *package, INT type, LPCWSTR source, LPCWSTR target, LPCWSTR action )
665 {
666     msi_custom_action_info *info;
667
668     info = msi_alloc( sizeof *info );
669     if (!info)
670         return NULL;
671
672     msiobj_addref( &package->hdr );
673     info->refs = 2; /* 1 for our caller and 1 for thread we created */
674     info->package = package;
675     info->type = type;
676     info->target = strdupW( target );
677     info->source = strdupW( source );
678     info->action = strdupW( action );
679     CoCreateGuid( &info->guid );
680
681     EnterCriticalSection( &msi_custom_action_cs );
682     list_add_tail( &msi_pending_custom_actions, &info->entry );
683     LeaveCriticalSection( &msi_custom_action_cs );
684
685     info->handle = CreateThread( NULL, 0, DllThread, &info->guid, 0, NULL );
686     if (!info->handle)
687     {
688         /* release both references */
689         release_custom_action_data( info );
690         release_custom_action_data( info );
691         return NULL;
692     }
693
694     return info;
695 }
696
697 static msi_custom_action_info *do_msidbCAConcurrentInstall(
698     MSIPACKAGE *package, INT type, LPCWSTR source, LPCWSTR target, LPCWSTR action)
699 {
700     msi_custom_action_info *info;
701
702     info = msi_alloc( sizeof *info );
703     if (!info)
704         return NULL;
705
706     msiobj_addref( &package->hdr );
707     info->refs = 2; /* 1 for our caller and 1 for thread we created */
708     info->package = package;
709     info->type = type;
710     info->target = strdupW( target );
711     info->source = strdupW( source );
712     info->action = strdupW( action );
713     CoCreateGuid( &info->guid );
714
715     EnterCriticalSection( &msi_custom_action_cs );
716     list_add_tail( &msi_pending_custom_actions, &info->entry );
717     LeaveCriticalSection( &msi_custom_action_cs );
718
719     info->handle = CreateThread( NULL, 0, ConcurrentInstallThread, &info->guid, 0, NULL );
720     if (!info->handle)
721     {
722         /* release both references */
723         release_custom_action_data( info );
724         release_custom_action_data( info );
725         return NULL;
726     }
727
728     return info;
729 }
730
731 static UINT HANDLE_CustomType23(MSIPACKAGE *package, LPCWSTR source,
732                                 LPCWSTR target, const INT type, LPCWSTR action)
733 {
734     msi_custom_action_info *info;
735     WCHAR package_path[MAX_PATH];
736     DWORD size;
737     UINT r;
738
739     size = MAX_PATH;
740     msi_get_property(package->db, szSourceDir, package_path, &size);
741     lstrcatW(package_path, szBackSlash);
742     lstrcatW(package_path, source);
743
744     TRACE("Installing package %s concurrently\n", debugstr_w(package_path));
745
746     info = do_msidbCAConcurrentInstall(package, type, package_path, target, action);
747
748     r = wait_thread_handle(info);
749     release_custom_action_data( info );
750     return r;
751 }
752
753 static UINT HANDLE_CustomType1(MSIPACKAGE *package, LPCWSTR source,
754                                LPCWSTR target, const INT type, LPCWSTR action)
755 {
756     msi_custom_action_info *info;
757     MSIBINARY *binary;
758     UINT r;
759
760     if (!(binary = get_temp_binary( package, source, TRUE )))
761         return ERROR_FUNCTION_FAILED;
762
763     TRACE("Calling function %s from %s\n", debugstr_w(target), debugstr_w(binary->tmpfile));
764
765     info = do_msidbCustomActionTypeDll( package, type, binary->tmpfile, target, action );
766
767     r = wait_thread_handle( info );
768     release_custom_action_data( info );
769     return r;
770 }
771
772 static HANDLE execute_command( const WCHAR *app, WCHAR *arg, const WCHAR *dir )
773 {
774     static const WCHAR dotexeW[] = {'.','e','x','e',0};
775     STARTUPINFOW si;
776     PROCESS_INFORMATION info;
777     WCHAR *exe = NULL, *cmd = NULL, *p;
778     BOOL ret;
779
780     if (app)
781     {
782         int len_arg = 0;
783         DWORD len_exe;
784
785         if (!(exe = msi_alloc( MAX_PATH * sizeof(WCHAR) ))) return INVALID_HANDLE_VALUE;
786         len_exe = SearchPathW( NULL, app, dotexeW, MAX_PATH, exe, NULL );
787         if (len_exe >= MAX_PATH)
788         {
789             msi_free( exe );
790             if (!(exe = msi_alloc( len_exe * sizeof(WCHAR) ))) return INVALID_HANDLE_VALUE;
791             len_exe = SearchPathW( NULL, app, dotexeW, len_exe, exe, NULL );
792         }
793         if (!len_exe)
794         {
795             WARN("can't find executable %u\n", GetLastError());
796             msi_free( exe );
797             return INVALID_HANDLE_VALUE;
798         }
799
800         if (arg) len_arg = strlenW( arg );
801         if (!(cmd = msi_alloc( (len_exe + len_arg + 4) * sizeof(WCHAR) )))
802         {
803             msi_free( exe );
804             return INVALID_HANDLE_VALUE;
805         }
806         p = cmd;
807         if (strchrW( exe, ' ' ))
808         {
809             *p++ = '\"';
810             memcpy( p, exe, len_exe * sizeof(WCHAR) );
811             p += len_exe;
812             *p++ = '\"';
813             *p = 0;
814         }
815         else
816         {
817             strcpyW( p, exe );
818             p += len_exe;
819         }
820         if (arg)
821         {
822             *p++ = ' ';
823             memcpy( p, arg, len_arg * sizeof(WCHAR) );
824             p[len_arg] = 0;
825         }
826     }
827     memset( &si, 0, sizeof(STARTUPINFOW) );
828     ret = CreateProcessW( exe, exe ? cmd : arg, NULL, NULL, FALSE, 0, NULL, dir, &si, &info );
829     msi_free( cmd );
830     msi_free( exe );
831     if (!ret)
832     {
833         WARN("unable to execute command %u\n", GetLastError());
834         return INVALID_HANDLE_VALUE;
835     }
836     CloseHandle( info.hThread );
837     return info.hProcess;
838 }
839
840 static UINT HANDLE_CustomType2(MSIPACKAGE *package, LPCWSTR source,
841                                LPCWSTR target, const INT type, LPCWSTR action)
842 {
843     MSIBINARY *binary;
844     HANDLE handle;
845     WCHAR *arg;
846
847     if (!(binary = get_temp_binary( package, source, FALSE ))) return ERROR_FUNCTION_FAILED;
848
849     deformat_string( package, target, &arg );
850     TRACE("exe %s arg %s\n", debugstr_w(binary->tmpfile), debugstr_w(arg));
851
852     handle = execute_command( binary->tmpfile, arg, szCRoot );
853     msi_free( arg );
854     if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
855     return wait_process_handle( package, type, handle, action );
856 }
857
858 static UINT HANDLE_CustomType17(MSIPACKAGE *package, LPCWSTR source,
859                                 LPCWSTR target, const INT type, LPCWSTR action)
860 {
861     msi_custom_action_info *info;
862     MSIFILE *file;
863     UINT r;
864
865     TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
866
867     file = msi_get_loaded_file( package, source );
868     if (!file)
869     {
870         ERR("invalid file key %s\n", debugstr_w( source ));
871         return ERROR_FUNCTION_FAILED;
872     }
873
874     info = do_msidbCustomActionTypeDll( package, type, file->TargetPath, target, action );
875
876     r = wait_thread_handle( info );
877     release_custom_action_data( info );
878     return r;
879 }
880
881 static UINT HANDLE_CustomType18(MSIPACKAGE *package, LPCWSTR source,
882                                 LPCWSTR target, const INT type, LPCWSTR action)
883 {
884     MSIFILE *file;
885     HANDLE handle;
886     WCHAR *arg;
887
888     if (!(file = msi_get_loaded_file( package, source ))) return ERROR_FUNCTION_FAILED;
889
890     deformat_string( package, target, &arg );
891     TRACE("exe %s arg %s\n", debugstr_w(file->TargetPath), debugstr_w(arg));
892
893     handle = execute_command( file->TargetPath, arg, szCRoot );
894     msi_free( arg );
895     if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
896     return wait_process_handle( package, type, handle, action );
897 }
898
899 static UINT HANDLE_CustomType19(MSIPACKAGE *package, LPCWSTR source,
900                                 LPCWSTR target, const INT type, LPCWSTR action)
901 {
902     static const WCHAR query[] = {
903       'S','E','L','E','C','T',' ','`','M','e','s','s','a','g','e','`',' ',
904       'F','R','O','M',' ','`','E','r','r','o','r','`',' ',
905       'W','H','E','R','E',' ','`','E','r','r','o','r','`',' ','=',' ',
906       '%','s',0
907     };
908     MSIRECORD *row = 0;
909     LPWSTR deformated = NULL;
910
911     deformat_string( package, target, &deformated );
912
913     /* first try treat the error as a number */
914     row = MSI_QueryGetRecord( package->db, query, deformated );
915     if( row )
916     {
917         LPCWSTR error = MSI_RecordGetString( row, 1 );
918         if ((gUILevel & INSTALLUILEVEL_MASK) != INSTALLUILEVEL_NONE)
919             MessageBoxW( NULL, error, NULL, MB_OK );
920         msiobj_release( &row->hdr );
921     }
922     else if ((gUILevel & INSTALLUILEVEL_MASK) != INSTALLUILEVEL_NONE)
923         MessageBoxW( NULL, deformated, NULL, MB_OK );
924
925     msi_free( deformated );
926
927     return ERROR_INSTALL_FAILURE;
928 }
929
930 static UINT HANDLE_CustomType50(MSIPACKAGE *package, LPCWSTR source,
931                                 LPCWSTR target, const INT type, LPCWSTR action)
932 {
933     WCHAR *exe, *arg;
934     HANDLE handle;
935
936     if (!(exe = msi_dup_property( package->db, source ))) return ERROR_SUCCESS;
937
938     deformat_string( package, target, &arg );
939     TRACE("exe %s arg %s\n", debugstr_w(exe), debugstr_w(arg));
940
941     handle = execute_command( exe, arg, szCRoot );
942     msi_free( arg );
943     if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
944     return wait_process_handle( package, type, handle, action );
945 }
946
947 static UINT HANDLE_CustomType34(MSIPACKAGE *package, LPCWSTR source,
948                                 LPCWSTR target, const INT type, LPCWSTR action)
949 {
950     const WCHAR *workingdir;
951     HANDLE handle;
952     WCHAR *cmd;
953
954     workingdir = msi_get_target_folder( package, source );
955     if (!workingdir) return ERROR_FUNCTION_FAILED;
956
957     deformat_string( package, target, &cmd );
958     if (!cmd) return ERROR_FUNCTION_FAILED;
959
960     TRACE("cmd %s dir %s\n", debugstr_w(cmd), debugstr_w(workingdir));
961
962     handle = execute_command( NULL, cmd, workingdir );
963     msi_free( cmd );
964     if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
965     return wait_process_handle( package, type, handle, action );
966 }
967
968 static DWORD ACTION_CallScript( const GUID *guid )
969 {
970     msi_custom_action_info *info;
971     MSIHANDLE hPackage;
972     UINT r;
973
974     info = find_action_by_guid( guid );
975     if (!info)
976     {
977         ERR("failed to find action %s\n", debugstr_guid( guid) );
978         return ERROR_FUNCTION_FAILED;
979     }
980
981     TRACE("function %s, script %s\n", debugstr_w( info->target ), debugstr_w( info->source ) );
982
983     hPackage = alloc_msihandle( &info->package->hdr );
984     if (hPackage)
985     {
986         r = call_script( hPackage, info->type, info->source, info->target, info->action );
987         TRACE("script returned %u\n", r);
988         MsiCloseHandle( hPackage );
989     }
990     else
991         ERR("failed to create handle for %p\n", info->package );
992
993     release_custom_action_data( info );
994     return S_OK;
995 }
996
997 static DWORD WINAPI ScriptThread( LPVOID arg )
998 {
999     LPGUID guid = arg;
1000     DWORD rc = 0;
1001
1002     TRACE("custom action (%x) started\n", GetCurrentThreadId() );
1003
1004     rc = ACTION_CallScript( guid );
1005
1006     TRACE("custom action (%x) returned %i\n", GetCurrentThreadId(), rc );
1007
1008     MsiCloseAllHandles();
1009     return rc;
1010 }
1011
1012 static msi_custom_action_info *do_msidbCustomActionTypeScript(
1013     MSIPACKAGE *package, INT type, LPCWSTR script, LPCWSTR function, LPCWSTR action )
1014 {
1015     msi_custom_action_info *info;
1016
1017     info = msi_alloc( sizeof *info );
1018     if (!info)
1019         return NULL;
1020
1021     msiobj_addref( &package->hdr );
1022     info->refs = 2; /* 1 for our caller and 1 for thread we created */
1023     info->package = package;
1024     info->type = type;
1025     info->target = strdupW( function );
1026     info->source = strdupW( script );
1027     info->action = strdupW( action );
1028     CoCreateGuid( &info->guid );
1029
1030     EnterCriticalSection( &msi_custom_action_cs );
1031     list_add_tail( &msi_pending_custom_actions, &info->entry );
1032     LeaveCriticalSection( &msi_custom_action_cs );
1033
1034     info->handle = CreateThread( NULL, 0, ScriptThread, &info->guid, 0, NULL );
1035     if (!info->handle)
1036     {
1037         /* release both references */
1038         release_custom_action_data( info );
1039         release_custom_action_data( info );
1040         return NULL;
1041     }
1042
1043     return info;
1044 }
1045
1046 static UINT HANDLE_CustomType37_38(MSIPACKAGE *package, LPCWSTR source,
1047                                LPCWSTR target, const INT type, LPCWSTR action)
1048 {
1049     UINT r;
1050     msi_custom_action_info *info;
1051
1052     TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1053
1054     info = do_msidbCustomActionTypeScript( package, type, target, NULL, action );
1055
1056     r = wait_thread_handle( info );
1057     release_custom_action_data( info );
1058     return r;
1059 }
1060
1061 static UINT HANDLE_CustomType5_6(MSIPACKAGE *package, LPCWSTR source,
1062                                LPCWSTR target, const INT type, LPCWSTR action)
1063 {
1064     static const WCHAR query[] = {
1065         'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1066         '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',' ',
1067         '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
1068     MSIRECORD *row = 0;
1069     msi_custom_action_info *info;
1070     CHAR *buffer = NULL;
1071     WCHAR *bufferw = NULL;
1072     DWORD sz = 0;
1073     UINT r;
1074
1075     TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1076
1077     row = MSI_QueryGetRecord(package->db, query, source);
1078     if (!row)
1079         return ERROR_FUNCTION_FAILED;
1080
1081     r = MSI_RecordReadStream(row, 2, NULL, &sz);
1082     if (r != ERROR_SUCCESS) return r;
1083
1084     buffer = msi_alloc( sz + 1 );
1085     if (!buffer) return ERROR_FUNCTION_FAILED;
1086
1087     r = MSI_RecordReadStream(row, 2, buffer, &sz);
1088     if (r != ERROR_SUCCESS)
1089         goto done;
1090
1091     buffer[sz] = 0;
1092     bufferw = strdupAtoW(buffer);
1093     if (!bufferw)
1094     {
1095         r = ERROR_FUNCTION_FAILED;
1096         goto done;
1097     }
1098
1099     info = do_msidbCustomActionTypeScript( package, type, bufferw, target, action );
1100     r = wait_thread_handle( info );
1101     release_custom_action_data( info );
1102
1103 done:
1104     msi_free(bufferw);
1105     msi_free(buffer);
1106     return r;
1107 }
1108
1109 static UINT HANDLE_CustomType21_22(MSIPACKAGE *package, LPCWSTR source,
1110                                LPCWSTR target, const INT type, LPCWSTR action)
1111 {
1112     msi_custom_action_info *info;
1113     MSIFILE *file;
1114     HANDLE hFile;
1115     DWORD sz, szHighWord = 0, read;
1116     CHAR *buffer=NULL;
1117     WCHAR *bufferw=NULL;
1118     BOOL bRet;
1119     UINT r;
1120
1121     TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1122
1123     file = msi_get_loaded_file(package, source);
1124     if (!file)
1125     {
1126         ERR("invalid file key %s\n", debugstr_w(source));
1127         return ERROR_FUNCTION_FAILED;
1128     }
1129
1130     hFile = CreateFileW(file->TargetPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
1131     if (hFile == INVALID_HANDLE_VALUE) return ERROR_FUNCTION_FAILED;
1132
1133     sz = GetFileSize(hFile, &szHighWord);
1134     if (sz == INVALID_FILE_SIZE || szHighWord != 0)
1135     {
1136         CloseHandle(hFile);
1137         return ERROR_FUNCTION_FAILED;
1138     }
1139     buffer = msi_alloc( sz + 1 );
1140     if (!buffer)
1141     {
1142         CloseHandle(hFile);
1143         return ERROR_FUNCTION_FAILED;
1144     }
1145     bRet = ReadFile(hFile, buffer, sz, &read, NULL);
1146     CloseHandle(hFile);
1147     if (!bRet)
1148     {
1149         r = ERROR_FUNCTION_FAILED;
1150         goto done;
1151     }
1152     buffer[read] = 0;
1153     bufferw = strdupAtoW(buffer);
1154     if (!bufferw)
1155     {
1156         r = ERROR_FUNCTION_FAILED;
1157         goto done;
1158     }
1159     info = do_msidbCustomActionTypeScript( package, type, bufferw, target, action );
1160     r = wait_thread_handle( info );
1161     release_custom_action_data( info );
1162
1163 done:
1164     msi_free(bufferw);
1165     msi_free(buffer);
1166     return r;
1167 }
1168
1169 static UINT HANDLE_CustomType53_54(MSIPACKAGE *package, LPCWSTR source,
1170                                LPCWSTR target, const INT type, LPCWSTR action)
1171 {
1172     msi_custom_action_info *info;
1173     WCHAR *prop;
1174     UINT r;
1175
1176     TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1177
1178     prop = msi_dup_property( package->db, source );
1179     if (!prop) return ERROR_SUCCESS;
1180
1181     info = do_msidbCustomActionTypeScript( package, type, prop, NULL, action );
1182     msi_free(prop);
1183     r = wait_thread_handle( info );
1184     release_custom_action_data( info );
1185     return r;
1186 }
1187
1188 UINT ACTION_CustomAction(MSIPACKAGE *package, LPCWSTR action, UINT script, BOOL execute)
1189 {
1190     static const WCHAR query[] = {
1191         'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1192         '`','C','u','s','t','o','m','A','c','t','i','o','n','`',' ','W','H','E','R','E',' ',
1193         '`','A','c','t','i' ,'o','n','`',' ','=',' ','\'','%','s','\'',0};
1194     UINT rc = ERROR_SUCCESS;
1195     MSIRECORD *row;
1196     UINT type;
1197     LPCWSTR source, target;
1198     LPWSTR ptr, deferred_data = NULL;
1199     LPWSTR deformated = NULL, action_copy = strdupW(action);
1200
1201     /* deferred action: [properties]Action */
1202     if ((ptr = strrchrW(action_copy, ']')))
1203     {
1204         deferred_data = action_copy;
1205         action = ptr + 1;
1206     }
1207
1208     row = MSI_QueryGetRecord( package->db, query, action );
1209     if (!row)
1210     {
1211         msi_free(action_copy);
1212         return ERROR_CALL_NOT_IMPLEMENTED;
1213     }
1214
1215     type = MSI_RecordGetInteger(row,2);
1216
1217     source = MSI_RecordGetString(row,3);
1218     target = MSI_RecordGetString(row,4);
1219
1220     TRACE("Handling custom action %s (%x %s %s)\n",debugstr_w(action),type,
1221           debugstr_w(source), debugstr_w(target));
1222
1223     /* handle some of the deferred actions */
1224     if (type & msidbCustomActionTypeTSAware)
1225         FIXME("msidbCustomActionTypeTSAware not handled\n");
1226
1227     if (type & msidbCustomActionTypeInScript)
1228     {
1229         if (type & msidbCustomActionTypeNoImpersonate)
1230             WARN("msidbCustomActionTypeNoImpersonate not handled\n");
1231
1232         if (!execute)
1233         {
1234             LPWSTR actiondata = msi_dup_property(package->db, action);
1235             LPWSTR usersid = msi_dup_property(package->db, szUserSID);
1236             LPWSTR prodcode = msi_dup_property(package->db, szProductCode);
1237             LPWSTR deferred = msi_get_deferred_action(action, actiondata, usersid, prodcode);
1238
1239             if (type & msidbCustomActionTypeCommit)
1240             {
1241                 TRACE("Deferring commit action\n");
1242                 msi_schedule_action(package, COMMIT_SCRIPT, deferred);
1243             }
1244             else if (type & msidbCustomActionTypeRollback)
1245             {
1246                 TRACE("Deferring rollback action\n");
1247                 msi_schedule_action(package, ROLLBACK_SCRIPT, deferred);
1248             }
1249             else
1250             {
1251                 TRACE("Deferring action\n");
1252                 msi_schedule_action(package, INSTALL_SCRIPT, deferred);
1253             }
1254
1255             rc = ERROR_SUCCESS;
1256             msi_free(actiondata);
1257             msi_free(usersid);
1258             msi_free(prodcode);
1259             msi_free(deferred);
1260             goto end;
1261         }
1262         else
1263         {
1264             LPWSTR actiondata = msi_dup_property( package->db, action );
1265
1266             if (type & msidbCustomActionTypeInScript)
1267                 package->scheduled_action_running = TRUE;
1268
1269             if (type & msidbCustomActionTypeCommit)
1270                 package->commit_action_running = TRUE;
1271
1272             if (type & msidbCustomActionTypeRollback)
1273                 package->rollback_action_running = TRUE;
1274
1275             if (deferred_data)
1276                 set_deferred_action_props(package, deferred_data);
1277             else if (actiondata)
1278                 msi_set_property(package->db, szCustomActionData, actiondata);
1279             else
1280                 msi_set_property(package->db, szCustomActionData, szEmpty);
1281
1282             msi_free(actiondata);
1283         }
1284     }
1285     else if (!check_execution_scheduling_options(package,action,type))
1286     {
1287         rc = ERROR_SUCCESS;
1288         goto end;
1289     }
1290
1291     switch (type & CUSTOM_ACTION_TYPE_MASK)
1292     {
1293         case 1: /* DLL file stored in a Binary table stream */
1294             rc = HANDLE_CustomType1(package,source,target,type,action);
1295             break;
1296         case 2: /* EXE file stored in a Binary table stream */
1297             rc = HANDLE_CustomType2(package,source,target,type,action);
1298             break;
1299         case 18: /*EXE file installed with package */
1300             rc = HANDLE_CustomType18(package,source,target,type,action);
1301             break;
1302         case 19: /* Error that halts install */
1303             rc = HANDLE_CustomType19(package,source,target,type,action);
1304             break;
1305         case 17:
1306             rc = HANDLE_CustomType17(package,source,target,type,action);
1307             break;
1308         case 23: /* installs another package in the source tree */
1309             deformat_string(package,target,&deformated);
1310             rc = HANDLE_CustomType23(package,source,deformated,type,action);
1311             msi_free(deformated);
1312             break;
1313         case 50: /*EXE file specified by a property value */
1314             rc = HANDLE_CustomType50(package,source,target,type,action);
1315             break;
1316         case 34: /*EXE to be run in specified directory */
1317             rc = HANDLE_CustomType34(package,source,target,type,action);
1318             break;
1319         case 35: /* Directory set with formatted text. */
1320             deformat_string(package,target,&deformated);
1321             MSI_SetTargetPathW(package, source, deformated);
1322             msi_free(deformated);
1323             break;
1324         case 51: /* Property set with formatted text. */
1325             if (!source)
1326                 break;
1327
1328             deformat_string(package,target,&deformated);
1329             rc = msi_set_property( package->db, source, deformated );
1330             if (rc == ERROR_SUCCESS && !strcmpW( source, szSourceDir ))
1331                 msi_reset_folders( package, TRUE );
1332             msi_free(deformated);
1333             break;
1334     case 37: /* JScript/VBScript text stored in target column. */
1335     case 38:
1336         rc = HANDLE_CustomType37_38(package,source,target,type,action);
1337         break;
1338     case 5:
1339     case 6: /* JScript/VBScript file stored in a Binary table stream. */
1340         rc = HANDLE_CustomType5_6(package,source,target,type,action);
1341         break;
1342     case 21: /* JScript/VBScript file installed with the product. */
1343     case 22:
1344         rc = HANDLE_CustomType21_22(package,source,target,type,action);
1345         break;
1346     case 53: /* JScript/VBScript text specified by a property value. */
1347     case 54:
1348         rc = HANDLE_CustomType53_54(package,source,target,type,action);
1349         break;
1350     default:
1351         FIXME("unhandled action type %u (%s %s)\n", type & CUSTOM_ACTION_TYPE_MASK,
1352               debugstr_w(source), debugstr_w(target));
1353     }
1354
1355 end:
1356     package->scheduled_action_running = FALSE;
1357     package->commit_action_running = FALSE;
1358     package->rollback_action_running = FALSE;
1359     msi_free(action_copy);
1360     msiobj_release(&row->hdr);
1361     return rc;
1362 }
1363
1364 void ACTION_FinishCustomActions(const MSIPACKAGE* package)
1365 {
1366     struct list *item;
1367     HANDLE *wait_handles;
1368     unsigned int handle_count, i;
1369     msi_custom_action_info *info, *cursor;
1370
1371     while ((item = list_head( &package->RunningActions )))
1372     {
1373         MSIRUNNINGACTION *action = LIST_ENTRY( item, MSIRUNNINGACTION, entry );
1374
1375         list_remove( &action->entry );
1376
1377         TRACE("waiting for %s\n", debugstr_w( action->name ) );
1378         msi_dialog_check_messages( action->handle );
1379
1380         CloseHandle( action->handle );
1381         msi_free( action->name );
1382         msi_free( action );
1383     }
1384
1385     EnterCriticalSection( &msi_custom_action_cs );
1386
1387     handle_count = list_count( &msi_pending_custom_actions );
1388     wait_handles = msi_alloc( handle_count * sizeof(HANDLE) );
1389
1390     handle_count = 0;
1391     LIST_FOR_EACH_ENTRY_SAFE( info, cursor, &msi_pending_custom_actions, msi_custom_action_info, entry )
1392     {
1393         if (info->package == package )
1394         {
1395             if (DuplicateHandle(GetCurrentProcess(), info->handle, GetCurrentProcess(), &wait_handles[handle_count], SYNCHRONIZE, FALSE, 0))
1396                 handle_count++;
1397         }
1398     }
1399
1400     LeaveCriticalSection( &msi_custom_action_cs );
1401
1402     for (i = 0; i < handle_count; i++)
1403     {
1404         msi_dialog_check_messages( wait_handles[i] );
1405         CloseHandle( wait_handles[i] );
1406     }
1407
1408     msi_free( wait_handles );
1409 }
1410
1411 typedef struct _msi_custom_remote_impl {
1412     IWineMsiRemoteCustomAction IWineMsiRemoteCustomAction_iface;
1413     LONG refs;
1414 } msi_custom_remote_impl;
1415
1416 static inline msi_custom_remote_impl *impl_from_IWineMsiRemoteCustomAction( IWineMsiRemoteCustomAction *iface )
1417 {
1418     return CONTAINING_RECORD(iface, msi_custom_remote_impl, IWineMsiRemoteCustomAction_iface);
1419 }
1420
1421 static HRESULT WINAPI mcr_QueryInterface( IWineMsiRemoteCustomAction *iface,
1422                 REFIID riid,LPVOID *ppobj)
1423 {
1424     if( IsEqualCLSID( riid, &IID_IUnknown ) ||
1425         IsEqualCLSID( riid, &IID_IWineMsiRemoteCustomAction ) )
1426     {
1427         IUnknown_AddRef( iface );
1428         *ppobj = iface;
1429         return S_OK;
1430     }
1431
1432     return E_NOINTERFACE;
1433 }
1434
1435 static ULONG WINAPI mcr_AddRef( IWineMsiRemoteCustomAction *iface )
1436 {
1437     msi_custom_remote_impl* This = impl_from_IWineMsiRemoteCustomAction( iface );
1438
1439     return InterlockedIncrement( &This->refs );
1440 }
1441
1442 static ULONG WINAPI mcr_Release( IWineMsiRemoteCustomAction *iface )
1443 {
1444     msi_custom_remote_impl* This = impl_from_IWineMsiRemoteCustomAction( iface );
1445     ULONG r;
1446
1447     r = InterlockedDecrement( &This->refs );
1448     if (r == 0)
1449         msi_free( This );
1450     return r;
1451 }
1452
1453 static HRESULT WINAPI mcr_GetActionInfo( IWineMsiRemoteCustomAction *iface, LPCGUID custom_action_guid,
1454          INT *type, MSIHANDLE *handle, BSTR *dll, BSTR *func, IWineMsiRemotePackage **remote_package )
1455 {
1456     msi_custom_action_info *info;
1457
1458     info = find_action_by_guid( custom_action_guid );
1459     if (!info)
1460         return E_FAIL;
1461
1462     *type = info->type;
1463     *handle = alloc_msihandle( &info->package->hdr );
1464     *dll = SysAllocString( info->source );
1465     *func = SysAllocString( info->target );
1466
1467     release_custom_action_data( info );
1468     return create_msi_remote_package( NULL, (LPVOID *)remote_package );
1469 }
1470
1471 static const IWineMsiRemoteCustomActionVtbl msi_custom_remote_vtbl =
1472 {
1473     mcr_QueryInterface,
1474     mcr_AddRef,
1475     mcr_Release,
1476     mcr_GetActionInfo,
1477 };
1478
1479 HRESULT create_msi_custom_remote( IUnknown *pOuter, LPVOID *ppObj )
1480 {
1481     msi_custom_remote_impl* This;
1482
1483     This = msi_alloc( sizeof *This );
1484     if (!This)
1485         return E_OUTOFMEMORY;
1486
1487     This->IWineMsiRemoteCustomAction_iface.lpVtbl = &msi_custom_remote_vtbl;
1488     This->refs = 1;
1489
1490     *ppObj = This;
1491
1492     return S_OK;
1493 }