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