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