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