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