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