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