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