3dffb8680a587ebcd20cc61c1a0f3fd348bf5d16
[reactos.git] / 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 #include "msipriv.h"
22
23 #include <wine/exception.h>
24
25 #ifdef _MSC_VER
26 #include "msvchelper.h"
27 #endif
28
29 WINE_DEFAULT_DEBUG_CHANNEL(msi);
30
31 #define CUSTOM_ACTION_TYPE_MASK 0x3F
32
33 typedef struct tagMSIRUNNINGACTION
34 {
35 struct list entry;
36 HANDLE handle;
37 BOOL process;
38 LPWSTR name;
39 } MSIRUNNINGACTION;
40
41 typedef UINT (WINAPI *MsiCustomActionEntryPoint)( MSIHANDLE );
42
43 static CRITICAL_SECTION msi_custom_action_cs;
44 static CRITICAL_SECTION_DEBUG msi_custom_action_cs_debug =
45 {
46 0, 0, &msi_custom_action_cs,
47 { &msi_custom_action_cs_debug.ProcessLocksList,
48 &msi_custom_action_cs_debug.ProcessLocksList },
49 0, 0, { (DWORD_PTR)(__FILE__ ": msi_custom_action_cs") }
50 };
51 static CRITICAL_SECTION msi_custom_action_cs = { &msi_custom_action_cs_debug, -1, 0, 0, 0, 0 };
52
53 static struct list msi_pending_custom_actions = LIST_INIT( msi_pending_custom_actions );
54
55 UINT msi_schedule_action( MSIPACKAGE *package, UINT script, const WCHAR *action )
56 {
57 UINT count;
58 WCHAR **newbuf = NULL;
59
60 if (script >= SCRIPT_MAX)
61 {
62 FIXME("Unknown script requested %u\n", script);
63 return ERROR_FUNCTION_FAILED;
64 }
65 TRACE("Scheduling action %s in script %u\n", debugstr_w(action), script);
66
67 count = package->script->ActionCount[script];
68 package->script->ActionCount[script]++;
69 if (count != 0) newbuf = msi_realloc( package->script->Actions[script],
70 package->script->ActionCount[script] * sizeof(WCHAR *) );
71 else newbuf = msi_alloc( sizeof(WCHAR *) );
72
73 newbuf[count] = strdupW( action );
74 package->script->Actions[script] = newbuf;
75 return ERROR_SUCCESS;
76 }
77
78 UINT msi_register_unique_action( MSIPACKAGE *package, const WCHAR *action )
79 {
80 UINT count;
81 WCHAR **newbuf = NULL;
82
83 if (!package->script) return FALSE;
84
85 TRACE("Registering %s as unique action\n", debugstr_w(action));
86
87 count = package->script->UniqueActionsCount;
88 package->script->UniqueActionsCount++;
89 if (count != 0) newbuf = msi_realloc( package->script->UniqueActions,
90 package->script->UniqueActionsCount * sizeof(WCHAR *) );
91 else newbuf = msi_alloc( sizeof(WCHAR *) );
92
93 newbuf[count] = strdupW( action );
94 package->script->UniqueActions = newbuf;
95 return ERROR_SUCCESS;
96 }
97
98 BOOL msi_action_is_unique( const MSIPACKAGE *package, const WCHAR *action )
99 {
100 UINT i;
101
102 if (!package->script) return FALSE;
103
104 for (i = 0; i < package->script->UniqueActionsCount; i++)
105 {
106 if (!strcmpW( package->script->UniqueActions[i], action )) return TRUE;
107 }
108 return FALSE;
109 }
110
111 static BOOL check_execution_scheduling_options(MSIPACKAGE *package, LPCWSTR action, UINT options)
112 {
113 if (!package->script)
114 return TRUE;
115
116 if ((options & msidbCustomActionTypeClientRepeat) ==
117 msidbCustomActionTypeClientRepeat)
118 {
119 if (!(package->script->InWhatSequence & SEQUENCE_UI &&
120 package->script->InWhatSequence & SEQUENCE_EXEC))
121 {
122 TRACE("Skipping action due to dbCustomActionTypeClientRepeat option.\n");
123 return FALSE;
124 }
125 }
126 else if (options & msidbCustomActionTypeFirstSequence)
127 {
128 if (package->script->InWhatSequence & SEQUENCE_UI &&
129 package->script->InWhatSequence & SEQUENCE_EXEC )
130 {
131 TRACE("Skipping action due to msidbCustomActionTypeFirstSequence option.\n");
132 return FALSE;
133 }
134 }
135 else if (options & msidbCustomActionTypeOncePerProcess)
136 {
137 if (msi_action_is_unique(package, action))
138 {
139 TRACE("Skipping action due to msidbCustomActionTypeOncePerProcess option.\n");
140 return FALSE;
141 }
142 else
143 msi_register_unique_action(package, action);
144 }
145
146 return TRUE;
147 }
148
149 /* stores the following properties before the action:
150 *
151 * [CustomActionData<=>UserSID<=>ProductCode]Action
152 */
153 static LPWSTR msi_get_deferred_action(LPCWSTR action, LPCWSTR actiondata,
154 LPCWSTR usersid, LPCWSTR prodcode)
155 {
156 LPWSTR deferred;
157 DWORD len;
158
159 static const WCHAR format[] = {
160 '[','%','s','<','=','>','%','s','<','=','>','%','s',']','%','s',0
161 };
162
163 if (!actiondata)
164 return strdupW(action);
165
166 len = lstrlenW(action) + lstrlenW(actiondata) +
167 lstrlenW(usersid) + lstrlenW(prodcode) +
168 lstrlenW(format) - 7;
169 deferred = msi_alloc(len * sizeof(WCHAR));
170
171 sprintfW(deferred, format, actiondata, usersid, prodcode, action);
172 return deferred;
173 }
174
175 static void set_deferred_action_props( MSIPACKAGE *package, const WCHAR *deferred_data )
176 {
177 static const WCHAR sep[] = {'<','=','>',0};
178 const WCHAR *end, *beg = deferred_data + 1;
179
180 end = strstrW(beg, sep);
181 msi_set_property( package->db, szCustomActionData, beg, end - beg );
182 beg = end + 3;
183
184 end = strstrW(beg, sep);
185 msi_set_property( package->db, szUserSID, beg, end - beg );
186 beg = end + 3;
187
188 end = strchrW(beg, ']');
189 msi_set_property( package->db, szProductCode, beg, end - beg );
190 }
191
192 WCHAR *msi_create_temp_file( MSIDATABASE *db )
193 {
194 WCHAR *ret;
195
196 if (!db->tempfolder)
197 {
198 WCHAR tmp[MAX_PATH];
199 UINT len = sizeof(tmp)/sizeof(tmp[0]);
200
201 if (msi_get_property( db, szTempFolder, tmp, &len ) ||
202 GetFileAttributesW( tmp ) != FILE_ATTRIBUTE_DIRECTORY)
203 {
204 GetTempPathW( MAX_PATH, tmp );
205 }
206 if (!(db->tempfolder = strdupW( tmp ))) return NULL;
207 }
208
209 if ((ret = msi_alloc( (strlenW( db->tempfolder ) + 20) * sizeof(WCHAR) )))
210 {
211 if (!GetTempFileNameW( db->tempfolder, szMsi, 0, ret ))
212 {
213 msi_free( ret );
214 return NULL;
215 }
216 }
217
218 return ret;
219 }
220
221 static MSIBINARY *create_temp_binary( MSIPACKAGE *package, LPCWSTR source, BOOL dll )
222 {
223 static const WCHAR query[] = {
224 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
225 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',' ',
226 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
227 MSIRECORD *row;
228 MSIBINARY *binary = NULL;
229 HANDLE file;
230 CHAR buffer[1024];
231 WCHAR *tmpfile;
232 DWORD sz, write;
233 UINT r;
234
235 if (!(tmpfile = msi_create_temp_file( package->db ))) return NULL;
236
237 if (!(row = MSI_QueryGetRecord( package->db, query, source ))) goto error;
238 if (!(binary = msi_alloc_zero( sizeof(MSIBINARY) ))) goto error;
239
240 file = CreateFileW( tmpfile, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
241 if (file == INVALID_HANDLE_VALUE) goto error;
242
243 do
244 {
245 sz = sizeof(buffer);
246 r = MSI_RecordReadStream( row, 2, buffer, &sz );
247 if (r != ERROR_SUCCESS)
248 {
249 ERR("Failed to get stream\n");
250 break;
251 }
252 WriteFile( file, buffer, sz, &write, NULL );
253 } while (sz == sizeof buffer);
254
255 CloseHandle( file );
256 if (r != ERROR_SUCCESS) goto error;
257
258 /* keep a reference to prevent the dll from being unloaded */
259 if (dll && !(binary->module = LoadLibraryW( tmpfile )))
260 {
261 WARN( "failed to load dll %s (%u)\n", debugstr_w( tmpfile ), GetLastError() );
262 }
263 binary->source = strdupW( source );
264 binary->tmpfile = tmpfile;
265 list_add_tail( &package->binaries, &binary->entry );
266
267 msiobj_release( &row->hdr );
268 return binary;
269
270 error:
271 if (row) msiobj_release( &row->hdr );
272 DeleteFileW( tmpfile );
273 msi_free( tmpfile );
274 msi_free( binary );
275 return NULL;
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 "subl $4,%esp\n\t"
531 "pushl 12(%ebp)\n\t"
532 "movl 8(%ebp),%eax\n\t"
533 "call *%eax\n\t"
534 "leave\n\t"
535 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
536 __ASM_CFI(".cfi_same_value %ebp\n\t")
537 "ret" )
538 #else
539 static inline UINT CUSTOMPROC_wrapper( MsiCustomActionEntryPoint proc, MSIHANDLE handle )
540 {
541 return proc(handle);
542 }
543 #endif
544
545 static DWORD ACTION_CallDllFunction( const GUID *guid )
546 {
547 MsiCustomActionEntryPoint fn;
548 MSIHANDLE hPackage, handle;
549 HANDLE hModule;
550 LPSTR proc;
551 UINT r = ERROR_FUNCTION_FAILED;
552 BSTR dll = NULL, function = NULL;
553 INT type;
554 IWineMsiRemotePackage *remote_package = NULL;
555
556 TRACE("%s\n", debugstr_guid( guid ));
557
558 r = get_action_info( guid, &type, &handle, &dll, &function, &remote_package );
559 if (r != ERROR_SUCCESS)
560 return r;
561
562 hModule = LoadLibraryW( dll );
563 if (!hModule)
564 {
565 WARN( "failed to load dll %s (%u)\n", debugstr_w( dll ), GetLastError() );
566 return ERROR_SUCCESS;
567 }
568
569 proc = strdupWtoA( function );
570 fn = (MsiCustomActionEntryPoint) GetProcAddress( hModule, proc );
571 msi_free( proc );
572 if (fn)
573 {
574 hPackage = alloc_msi_remote_handle( (IUnknown *)remote_package );
575 if (hPackage)
576 {
577 IWineMsiRemotePackage_SetMsiHandle( remote_package, handle );
578 TRACE("calling %s\n", debugstr_w( function ) );
579 handle_msi_break( function );
580
581 __TRY
582 {
583 r = CUSTOMPROC_wrapper( fn, hPackage );
584 }
585 __EXCEPT_PAGE_FAULT
586 {
587 ERR("Custom action (%s:%s) caused a page fault: %08x\n",
588 debugstr_w(dll), debugstr_w(function), GetExceptionCode());
589 r = ERROR_SUCCESS;
590 }
591 __ENDTRY;
592
593 MsiCloseHandle( hPackage );
594 }
595 else
596 ERR("failed to create handle for %p\n", remote_package );
597 }
598 else
599 ERR("GetProcAddress(%s) failed\n", debugstr_w( function ) );
600
601 FreeLibrary(hModule);
602
603 IWineMsiRemotePackage_Release( remote_package );
604 SysFreeString( dll );
605 SysFreeString( function );
606 MsiCloseHandle( handle );
607
608 return r;
609 }
610
611 static DWORD WINAPI DllThread( LPVOID arg )
612 {
613 LPGUID guid = arg;
614 DWORD rc = 0;
615
616 TRACE("custom action (%x) started\n", GetCurrentThreadId() );
617
618 rc = ACTION_CallDllFunction( guid );
619
620 TRACE("custom action (%x) returned %i\n", GetCurrentThreadId(), rc );
621
622 MsiCloseAllHandles();
623 return rc;
624 }
625
626 static msi_custom_action_info *do_msidbCustomActionTypeDll(
627 MSIPACKAGE *package, INT type, LPCWSTR source, LPCWSTR target, LPCWSTR action )
628 {
629 msi_custom_action_info *info;
630
631 info = msi_alloc( sizeof *info );
632 if (!info)
633 return NULL;
634
635 msiobj_addref( &package->hdr );
636 info->refs = 2; /* 1 for our caller and 1 for thread we created */
637 info->package = package;
638 info->type = type;
639 info->target = strdupW( target );
640 info->source = strdupW( source );
641 info->action = strdupW( action );
642 CoCreateGuid( &info->guid );
643
644 EnterCriticalSection( &msi_custom_action_cs );
645 list_add_tail( &msi_pending_custom_actions, &info->entry );
646 LeaveCriticalSection( &msi_custom_action_cs );
647
648 info->handle = CreateThread( NULL, 0, DllThread, &info->guid, 0, NULL );
649 if (!info->handle)
650 {
651 /* release both references */
652 release_custom_action_data( info );
653 release_custom_action_data( info );
654 return NULL;
655 }
656
657 return info;
658 }
659
660 static UINT HANDLE_CustomType1( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
661 INT type, const WCHAR *action )
662 {
663 msi_custom_action_info *info;
664 MSIBINARY *binary;
665
666 if (!(binary = get_temp_binary( package, source, TRUE )))
667 return ERROR_FUNCTION_FAILED;
668
669 TRACE("Calling function %s from %s\n", debugstr_w(target), debugstr_w(binary->tmpfile));
670
671 info = do_msidbCustomActionTypeDll( package, type, binary->tmpfile, target, action );
672 return wait_thread_handle( info );
673 }
674
675 static HANDLE execute_command( const WCHAR *app, WCHAR *arg, const WCHAR *dir )
676 {
677 static const WCHAR dotexeW[] = {'.','e','x','e',0};
678 STARTUPINFOW si;
679 PROCESS_INFORMATION info;
680 WCHAR *exe = NULL, *cmd = NULL, *p;
681 BOOL ret;
682
683 if (app)
684 {
685 int len_arg = 0;
686 DWORD len_exe;
687
688 if (!(exe = msi_alloc( MAX_PATH * sizeof(WCHAR) ))) return INVALID_HANDLE_VALUE;
689 len_exe = SearchPathW( NULL, app, dotexeW, MAX_PATH, exe, NULL );
690 if (len_exe >= MAX_PATH)
691 {
692 msi_free( exe );
693 if (!(exe = msi_alloc( len_exe * sizeof(WCHAR) ))) return INVALID_HANDLE_VALUE;
694 len_exe = SearchPathW( NULL, app, dotexeW, len_exe, exe, NULL );
695 }
696 if (!len_exe)
697 {
698 WARN("can't find executable %u\n", GetLastError());
699 msi_free( exe );
700 return INVALID_HANDLE_VALUE;
701 }
702
703 if (arg) len_arg = strlenW( arg );
704 if (!(cmd = msi_alloc( (len_exe + len_arg + 4) * sizeof(WCHAR) )))
705 {
706 msi_free( exe );
707 return INVALID_HANDLE_VALUE;
708 }
709 p = cmd;
710 if (strchrW( exe, ' ' ))
711 {
712 *p++ = '\"';
713 memcpy( p, exe, len_exe * sizeof(WCHAR) );
714 p += len_exe;
715 *p++ = '\"';
716 *p = 0;
717 }
718 else
719 {
720 strcpyW( p, exe );
721 p += len_exe;
722 }
723 if (arg)
724 {
725 *p++ = ' ';
726 memcpy( p, arg, len_arg * sizeof(WCHAR) );
727 p[len_arg] = 0;
728 }
729 }
730 memset( &si, 0, sizeof(STARTUPINFOW) );
731 ret = CreateProcessW( exe, exe ? cmd : arg, NULL, NULL, FALSE, 0, NULL, dir, &si, &info );
732 msi_free( cmd );
733 msi_free( exe );
734 if (!ret)
735 {
736 WARN("unable to execute command %u\n", GetLastError());
737 return INVALID_HANDLE_VALUE;
738 }
739 CloseHandle( info.hThread );
740 return info.hProcess;
741 }
742
743 static UINT HANDLE_CustomType2( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
744 INT type, const WCHAR *action )
745 {
746 MSIBINARY *binary;
747 HANDLE handle;
748 WCHAR *arg;
749
750 if (!(binary = get_temp_binary( package, source, FALSE ))) return ERROR_FUNCTION_FAILED;
751
752 deformat_string( package, target, &arg );
753 TRACE("exe %s arg %s\n", debugstr_w(binary->tmpfile), debugstr_w(arg));
754
755 handle = execute_command( binary->tmpfile, arg, szCRoot );
756 msi_free( arg );
757 if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
758 return wait_process_handle( package, type, handle, action );
759 }
760
761 static UINT HANDLE_CustomType17( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
762 INT type, const WCHAR *action )
763 {
764 msi_custom_action_info *info;
765 MSIFILE *file;
766
767 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
768
769 file = msi_get_loaded_file( package, source );
770 if (!file)
771 {
772 ERR("invalid file key %s\n", debugstr_w( source ));
773 return ERROR_FUNCTION_FAILED;
774 }
775
776 info = do_msidbCustomActionTypeDll( package, type, file->TargetPath, target, action );
777 return wait_thread_handle( info );
778 }
779
780 static UINT HANDLE_CustomType18( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
781 INT type, const WCHAR *action )
782 {
783 MSIFILE *file;
784 HANDLE handle;
785 WCHAR *arg;
786
787 if (!(file = msi_get_loaded_file( package, source ))) return ERROR_FUNCTION_FAILED;
788
789 deformat_string( package, target, &arg );
790 TRACE("exe %s arg %s\n", debugstr_w(file->TargetPath), debugstr_w(arg));
791
792 handle = execute_command( file->TargetPath, arg, szCRoot );
793 msi_free( arg );
794 if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
795 return wait_process_handle( package, type, handle, action );
796 }
797
798 static UINT HANDLE_CustomType19( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
799 INT type, const WCHAR *action )
800 {
801 static const WCHAR query[] = {
802 'S','E','L','E','C','T',' ','`','M','e','s','s','a','g','e','`',' ',
803 'F','R','O','M',' ','`','E','r','r','o','r','`',' ',
804 'W','H','E','R','E',' ','`','E','r','r','o','r','`',' ','=',' ',
805 '%','s',0
806 };
807 MSIRECORD *row = 0;
808 LPWSTR deformated = NULL;
809
810 deformat_string( package, target, &deformated );
811
812 /* first try treat the error as a number */
813 row = MSI_QueryGetRecord( package->db, query, deformated );
814 if( row )
815 {
816 LPCWSTR error = MSI_RecordGetString( row, 1 );
817 if ((package->ui_level & INSTALLUILEVEL_MASK) != INSTALLUILEVEL_NONE)
818 MessageBoxW( NULL, error, NULL, MB_OK );
819 msiobj_release( &row->hdr );
820 }
821 else if ((package->ui_level & INSTALLUILEVEL_MASK) != INSTALLUILEVEL_NONE)
822 MessageBoxW( NULL, deformated, NULL, MB_OK );
823
824 msi_free( deformated );
825
826 return ERROR_INSTALL_FAILURE;
827 }
828
829 static UINT HANDLE_CustomType23( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
830 INT type, const WCHAR *action )
831 {
832 static const WCHAR msiexecW[] = {'m','s','i','e','x','e','c',0};
833 static const WCHAR paramsW[] = {'/','q','b',' ','/','i',' '};
834 WCHAR *dir, *arg, *p;
835 UINT len_src, len_dir, len_tgt, len = sizeof(paramsW)/sizeof(paramsW[0]);
836 HANDLE handle;
837
838 if (!(dir = msi_dup_property( package->db, szOriginalDatabase ))) return ERROR_OUTOFMEMORY;
839 if (!(p = strrchrW( dir, '\\' )) && !(p = strrchrW( dir, '/' )))
840 {
841 msi_free( dir );
842 return ERROR_FUNCTION_FAILED;
843 }
844 *p = 0;
845 len_dir = p - dir;
846 len_src = strlenW( source );
847 len_tgt = strlenW( target );
848 if (!(arg = msi_alloc( (len + len_dir + len_src + len_tgt + 5) * sizeof(WCHAR) )))
849 {
850 msi_free( dir );
851 return ERROR_OUTOFMEMORY;
852 }
853 memcpy( arg, paramsW, sizeof(paramsW) );
854 arg[len++] = '"';
855 memcpy( arg + len, dir, len_dir * sizeof(WCHAR) );
856 len += len_dir;
857 arg[len++] = '\\';
858 memcpy( arg + len, source, len_src * sizeof(WCHAR) );
859 len += len_src;
860 arg[len++] = '"';
861 arg[len++] = ' ';
862 strcpyW( arg + len, target );
863
864 TRACE("installing %s concurrently\n", debugstr_w(source));
865
866 handle = execute_command( msiexecW, arg, dir );
867 msi_free( dir );
868 msi_free( arg );
869 if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
870 return wait_process_handle( package, type, handle, action );
871 }
872
873 static UINT HANDLE_CustomType50( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
874 INT type, const WCHAR *action )
875 {
876 WCHAR *exe, *arg;
877 HANDLE handle;
878
879 if (!(exe = msi_dup_property( package->db, source ))) return ERROR_SUCCESS;
880
881 deformat_string( package, target, &arg );
882 TRACE("exe %s arg %s\n", debugstr_w(exe), debugstr_w(arg));
883
884 handle = execute_command( exe, arg, szCRoot );
885 msi_free( exe );
886 msi_free( arg );
887 if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
888 return wait_process_handle( package, type, handle, action );
889 }
890
891 static UINT HANDLE_CustomType34( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
892 INT type, const WCHAR *action )
893 {
894 const WCHAR *workingdir = NULL;
895 HANDLE handle;
896 WCHAR *cmd;
897
898 if (source)
899 {
900 workingdir = msi_get_target_folder( package, source );
901 if (!workingdir) return ERROR_FUNCTION_FAILED;
902 }
903 deformat_string( package, target, &cmd );
904 if (!cmd) return ERROR_FUNCTION_FAILED;
905
906 TRACE("cmd %s dir %s\n", debugstr_w(cmd), debugstr_w(workingdir));
907
908 handle = execute_command( NULL, cmd, workingdir );
909 msi_free( cmd );
910 if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
911 return wait_process_handle( package, type, handle, action );
912 }
913
914 static DWORD ACTION_CallScript( const GUID *guid )
915 {
916 msi_custom_action_info *info;
917 MSIHANDLE hPackage;
918 UINT r = ERROR_FUNCTION_FAILED;
919
920 info = find_action_by_guid( guid );
921 if (!info)
922 {
923 ERR("failed to find action %s\n", debugstr_guid( guid) );
924 return ERROR_FUNCTION_FAILED;
925 }
926
927 TRACE("function %s, script %s\n", debugstr_w( info->target ), debugstr_w( info->source ) );
928
929 hPackage = alloc_msihandle( &info->package->hdr );
930 if (hPackage)
931 {
932 r = call_script( hPackage, info->type, info->source, info->target, info->action );
933 TRACE("script returned %u\n", r);
934 MsiCloseHandle( hPackage );
935 }
936 else
937 ERR("failed to create handle for %p\n", info->package );
938
939 release_custom_action_data( info );
940 return r;
941 }
942
943 static DWORD WINAPI ScriptThread( LPVOID arg )
944 {
945 LPGUID guid = arg;
946 DWORD rc;
947
948 TRACE("custom action (%x) started\n", GetCurrentThreadId() );
949
950 rc = ACTION_CallScript( guid );
951
952 TRACE("custom action (%x) returned %i\n", GetCurrentThreadId(), rc );
953
954 MsiCloseAllHandles();
955 return rc;
956 }
957
958 static msi_custom_action_info *do_msidbCustomActionTypeScript(
959 MSIPACKAGE *package, INT type, LPCWSTR script, LPCWSTR function, LPCWSTR action )
960 {
961 msi_custom_action_info *info;
962
963 info = msi_alloc( sizeof *info );
964 if (!info)
965 return NULL;
966
967 msiobj_addref( &package->hdr );
968 info->refs = 2; /* 1 for our caller and 1 for thread we created */
969 info->package = package;
970 info->type = type;
971 info->target = strdupW( function );
972 info->source = strdupW( script );
973 info->action = strdupW( action );
974 CoCreateGuid( &info->guid );
975
976 EnterCriticalSection( &msi_custom_action_cs );
977 list_add_tail( &msi_pending_custom_actions, &info->entry );
978 LeaveCriticalSection( &msi_custom_action_cs );
979
980 info->handle = CreateThread( NULL, 0, ScriptThread, &info->guid, 0, NULL );
981 if (!info->handle)
982 {
983 /* release both references */
984 release_custom_action_data( info );
985 release_custom_action_data( info );
986 return NULL;
987 }
988
989 return info;
990 }
991
992 static UINT HANDLE_CustomType37_38( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
993 INT type, const WCHAR *action )
994 {
995 msi_custom_action_info *info;
996
997 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
998
999 info = do_msidbCustomActionTypeScript( package, type, target, NULL, action );
1000 return wait_thread_handle( info );
1001 }
1002
1003 static UINT HANDLE_CustomType5_6( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
1004 INT type, const WCHAR *action )
1005 {
1006 static const WCHAR query[] = {
1007 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1008 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',' ',
1009 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
1010 MSIRECORD *row = NULL;
1011 msi_custom_action_info *info;
1012 CHAR *buffer = NULL;
1013 WCHAR *bufferw = NULL;
1014 DWORD sz = 0;
1015 UINT r;
1016
1017 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1018
1019 row = MSI_QueryGetRecord(package->db, query, source);
1020 if (!row)
1021 return ERROR_FUNCTION_FAILED;
1022
1023 r = MSI_RecordReadStream(row, 2, NULL, &sz);
1024 if (r != ERROR_SUCCESS) goto done;
1025
1026 buffer = msi_alloc( sz + 1 );
1027 if (!buffer)
1028 {
1029 r = ERROR_FUNCTION_FAILED;
1030 goto done;
1031 }
1032
1033 r = MSI_RecordReadStream(row, 2, buffer, &sz);
1034 if (r != ERROR_SUCCESS)
1035 goto done;
1036
1037 buffer[sz] = 0;
1038 bufferw = strdupAtoW(buffer);
1039 if (!bufferw)
1040 {
1041 r = ERROR_FUNCTION_FAILED;
1042 goto done;
1043 }
1044
1045 info = do_msidbCustomActionTypeScript( package, type, bufferw, target, action );
1046 r = wait_thread_handle( info );
1047
1048 done:
1049 msi_free(bufferw);
1050 msi_free(buffer);
1051 msiobj_release(&row->hdr);
1052 return r;
1053 }
1054
1055 static UINT HANDLE_CustomType21_22( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
1056 INT type, const WCHAR *action )
1057 {
1058 msi_custom_action_info *info;
1059 MSIFILE *file;
1060 HANDLE hFile;
1061 DWORD sz, szHighWord = 0, read;
1062 CHAR *buffer=NULL;
1063 WCHAR *bufferw=NULL;
1064 BOOL bRet;
1065 UINT r;
1066
1067 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1068
1069 file = msi_get_loaded_file(package, source);
1070 if (!file)
1071 {
1072 ERR("invalid file key %s\n", debugstr_w(source));
1073 return ERROR_FUNCTION_FAILED;
1074 }
1075
1076 hFile = CreateFileW(file->TargetPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
1077 if (hFile == INVALID_HANDLE_VALUE) return ERROR_FUNCTION_FAILED;
1078
1079 sz = GetFileSize(hFile, &szHighWord);
1080 if (sz == INVALID_FILE_SIZE || szHighWord != 0)
1081 {
1082 CloseHandle(hFile);
1083 return ERROR_FUNCTION_FAILED;
1084 }
1085 buffer = msi_alloc( sz + 1 );
1086 if (!buffer)
1087 {
1088 CloseHandle(hFile);
1089 return ERROR_FUNCTION_FAILED;
1090 }
1091 bRet = ReadFile(hFile, buffer, sz, &read, NULL);
1092 CloseHandle(hFile);
1093 if (!bRet)
1094 {
1095 r = ERROR_FUNCTION_FAILED;
1096 goto done;
1097 }
1098 buffer[read] = 0;
1099 bufferw = strdupAtoW(buffer);
1100 if (!bufferw)
1101 {
1102 r = ERROR_FUNCTION_FAILED;
1103 goto done;
1104 }
1105 info = do_msidbCustomActionTypeScript( package, type, bufferw, target, action );
1106 r = wait_thread_handle( info );
1107
1108 done:
1109 msi_free(bufferw);
1110 msi_free(buffer);
1111 return r;
1112 }
1113
1114 static UINT HANDLE_CustomType53_54( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
1115 INT type, const WCHAR *action )
1116 {
1117 msi_custom_action_info *info;
1118 WCHAR *prop;
1119
1120 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1121
1122 prop = msi_dup_property( package->db, source );
1123 if (!prop) return ERROR_SUCCESS;
1124
1125 info = do_msidbCustomActionTypeScript( package, type, prop, NULL, action );
1126 msi_free(prop);
1127 return wait_thread_handle( info );
1128 }
1129
1130 static BOOL action_type_matches_script( UINT type, UINT script )
1131 {
1132 switch (script)
1133 {
1134 case SCRIPT_NONE:
1135 case SCRIPT_INSTALL:
1136 return !(type & msidbCustomActionTypeCommit) && !(type & msidbCustomActionTypeRollback);
1137 case SCRIPT_COMMIT:
1138 return (type & msidbCustomActionTypeCommit);
1139 case SCRIPT_ROLLBACK:
1140 return (type & msidbCustomActionTypeRollback);
1141 default:
1142 ERR("unhandled script %u\n", script);
1143 }
1144 return FALSE;
1145 }
1146
1147 static UINT defer_custom_action( MSIPACKAGE *package, const WCHAR *action, UINT type )
1148 {
1149 WCHAR *actiondata = msi_dup_property( package->db, action );
1150 WCHAR *usersid = msi_dup_property( package->db, szUserSID );
1151 WCHAR *prodcode = msi_dup_property( package->db, szProductCode );
1152 WCHAR *deferred = msi_get_deferred_action( action, actiondata, usersid, prodcode );
1153
1154 if (!deferred)
1155 {
1156 msi_free( actiondata );
1157 msi_free( usersid );
1158 msi_free( prodcode );
1159 return ERROR_OUTOFMEMORY;
1160 }
1161 if (type & msidbCustomActionTypeCommit)
1162 {
1163 TRACE("deferring commit action\n");
1164 msi_schedule_action( package, SCRIPT_COMMIT, deferred );
1165 }
1166 else if (type & msidbCustomActionTypeRollback)
1167 {
1168 TRACE("deferring rollback action\n");
1169 msi_schedule_action( package, SCRIPT_ROLLBACK, deferred );
1170 }
1171 else
1172 {
1173 TRACE("deferring install action\n");
1174 msi_schedule_action( package, SCRIPT_INSTALL, deferred );
1175 }
1176
1177 msi_free( actiondata );
1178 msi_free( usersid );
1179 msi_free( prodcode );
1180 msi_free( deferred );
1181 return ERROR_SUCCESS;
1182 }
1183
1184 UINT ACTION_CustomAction( MSIPACKAGE *package, LPCWSTR action, UINT script )
1185 {
1186 static const WCHAR query[] = {
1187 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1188 '`','C','u','s','t','o','m','A','c','t','i','o','n','`',' ','W','H','E','R','E',' ',
1189 '`','A','c','t','i' ,'o','n','`',' ','=',' ','\'','%','s','\'',0};
1190 UINT rc = ERROR_SUCCESS;
1191 MSIRECORD *row;
1192 UINT type;
1193 const WCHAR *source, *target, *ptr, *deferred_data = NULL;
1194 WCHAR *deformated = NULL;
1195 int len;
1196
1197 /* deferred action: [properties]Action */
1198 if ((ptr = strrchrW(action, ']')))
1199 {
1200 deferred_data = action;
1201 action = ptr + 1;
1202 }
1203
1204 row = MSI_QueryGetRecord( package->db, query, action );
1205 if (!row)
1206 return ERROR_FUNCTION_NOT_CALLED;
1207
1208 type = MSI_RecordGetInteger(row,2);
1209 source = MSI_RecordGetString(row,3);
1210 target = MSI_RecordGetString(row,4);
1211
1212 TRACE("Handling custom action %s (%x %s %s)\n",debugstr_w(action),type,
1213 debugstr_w(source), debugstr_w(target));
1214
1215 /* handle some of the deferred actions */
1216 if (type & msidbCustomActionTypeTSAware)
1217 FIXME("msidbCustomActionTypeTSAware not handled\n");
1218
1219 if (type & msidbCustomActionTypeInScript)
1220 {
1221 if (type & msidbCustomActionTypeNoImpersonate)
1222 WARN("msidbCustomActionTypeNoImpersonate not handled\n");
1223
1224 if (!action_type_matches_script( type, script ))
1225 {
1226 rc = defer_custom_action( package, action, type );
1227 goto end;
1228 }
1229 else
1230 {
1231 LPWSTR actiondata = msi_dup_property( package->db, action );
1232
1233 if (type & msidbCustomActionTypeInScript)
1234 package->scheduled_action_running = TRUE;
1235
1236 if (type & msidbCustomActionTypeCommit)
1237 package->commit_action_running = TRUE;
1238
1239 if (type & msidbCustomActionTypeRollback)
1240 package->rollback_action_running = TRUE;
1241
1242 if (deferred_data)
1243 set_deferred_action_props(package, deferred_data);
1244 else if (actiondata)
1245 msi_set_property( package->db, szCustomActionData, actiondata, -1 );
1246 else
1247 msi_set_property( package->db, szCustomActionData, szEmpty, -1 );
1248
1249 msi_free(actiondata);
1250 }
1251 }
1252 else if (!check_execution_scheduling_options(package,action,type))
1253 {
1254 rc = ERROR_SUCCESS;
1255 goto end;
1256 }
1257
1258 switch (type & CUSTOM_ACTION_TYPE_MASK)
1259 {
1260 case 1: /* DLL file stored in a Binary table stream */
1261 rc = HANDLE_CustomType1(package,source,target,type,action);
1262 break;
1263 case 2: /* EXE file stored in a Binary table stream */
1264 rc = HANDLE_CustomType2(package,source,target,type,action);
1265 break;
1266 case 18: /*EXE file installed with package */
1267 rc = HANDLE_CustomType18(package,source,target,type,action);
1268 break;
1269 case 19: /* Error that halts install */
1270 rc = HANDLE_CustomType19(package,source,target,type,action);
1271 break;
1272 case 17:
1273 rc = HANDLE_CustomType17(package,source,target,type,action);
1274 break;
1275 case 23: /* installs another package in the source tree */
1276 deformat_string(package,target,&deformated);
1277 rc = HANDLE_CustomType23(package,source,deformated,type,action);
1278 msi_free(deformated);
1279 break;
1280 case 50: /*EXE file specified by a property value */
1281 rc = HANDLE_CustomType50(package,source,target,type,action);
1282 break;
1283 case 34: /*EXE to be run in specified directory */
1284 rc = HANDLE_CustomType34(package,source,target,type,action);
1285 break;
1286 case 35: /* Directory set with formatted text. */
1287 deformat_string(package,target,&deformated);
1288 MSI_SetTargetPathW(package, source, deformated);
1289 msi_free(deformated);
1290 break;
1291 case 51: /* Property set with formatted text. */
1292 if (!source)
1293 break;
1294
1295 len = deformat_string( package, target, &deformated );
1296 rc = msi_set_property( package->db, source, deformated, len );
1297 if (rc == ERROR_SUCCESS && !strcmpW( source, szSourceDir ))
1298 msi_reset_folders( package, TRUE );
1299 msi_free(deformated);
1300 break;
1301 case 37: /* JScript/VBScript text stored in target column. */
1302 case 38:
1303 rc = HANDLE_CustomType37_38(package,source,target,type,action);
1304 break;
1305 case 5:
1306 case 6: /* JScript/VBScript file stored in a Binary table stream. */
1307 rc = HANDLE_CustomType5_6(package,source,target,type,action);
1308 break;
1309 case 21: /* JScript/VBScript file installed with the product. */
1310 case 22:
1311 rc = HANDLE_CustomType21_22(package,source,target,type,action);
1312 break;
1313 case 53: /* JScript/VBScript text specified by a property value. */
1314 case 54:
1315 rc = HANDLE_CustomType53_54(package,source,target,type,action);
1316 break;
1317 default:
1318 FIXME("unhandled action type %u (%s %s)\n", type & CUSTOM_ACTION_TYPE_MASK,
1319 debugstr_w(source), debugstr_w(target));
1320 }
1321
1322 end:
1323 package->scheduled_action_running = FALSE;
1324 package->commit_action_running = FALSE;
1325 package->rollback_action_running = FALSE;
1326 msiobj_release(&row->hdr);
1327 return rc;
1328 }
1329
1330 void ACTION_FinishCustomActions(const MSIPACKAGE* package)
1331 {
1332 struct list *item;
1333 HANDLE *wait_handles;
1334 unsigned int handle_count, i;
1335 msi_custom_action_info *info, *cursor;
1336
1337 while ((item = list_head( &package->RunningActions )))
1338 {
1339 MSIRUNNINGACTION *action = LIST_ENTRY( item, MSIRUNNINGACTION, entry );
1340
1341 list_remove( &action->entry );
1342
1343 TRACE("waiting for %s\n", debugstr_w( action->name ) );
1344 msi_dialog_check_messages( action->handle );
1345
1346 CloseHandle( action->handle );
1347 msi_free( action->name );
1348 msi_free( action );
1349 }
1350
1351 EnterCriticalSection( &msi_custom_action_cs );
1352
1353 handle_count = list_count( &msi_pending_custom_actions );
1354 wait_handles = msi_alloc( handle_count * sizeof(HANDLE) );
1355
1356 handle_count = 0;
1357 LIST_FOR_EACH_ENTRY_SAFE( info, cursor, &msi_pending_custom_actions, msi_custom_action_info, entry )
1358 {
1359 if (info->package == package )
1360 {
1361 if (DuplicateHandle(GetCurrentProcess(), info->handle, GetCurrentProcess(), &wait_handles[handle_count], SYNCHRONIZE, FALSE, 0))
1362 handle_count++;
1363 }
1364 }
1365
1366 LeaveCriticalSection( &msi_custom_action_cs );
1367
1368 for (i = 0; i < handle_count; i++)
1369 {
1370 msi_dialog_check_messages( wait_handles[i] );
1371 CloseHandle( wait_handles[i] );
1372 }
1373 msi_free( wait_handles );
1374
1375 EnterCriticalSection( &msi_custom_action_cs );
1376 LIST_FOR_EACH_ENTRY_SAFE( info, cursor, &msi_pending_custom_actions, msi_custom_action_info, entry )
1377 {
1378 if (info->package == package) release_custom_action_data( info );
1379 }
1380 LeaveCriticalSection( &msi_custom_action_cs );
1381 }
1382
1383 typedef struct _msi_custom_remote_impl {
1384 IWineMsiRemoteCustomAction IWineMsiRemoteCustomAction_iface;
1385 LONG refs;
1386 } msi_custom_remote_impl;
1387
1388 static inline msi_custom_remote_impl *impl_from_IWineMsiRemoteCustomAction( IWineMsiRemoteCustomAction *iface )
1389 {
1390 return CONTAINING_RECORD(iface, msi_custom_remote_impl, IWineMsiRemoteCustomAction_iface);
1391 }
1392
1393 static HRESULT WINAPI mcr_QueryInterface( IWineMsiRemoteCustomAction *iface,
1394 REFIID riid,LPVOID *ppobj)
1395 {
1396 if( IsEqualCLSID( riid, &IID_IUnknown ) ||
1397 IsEqualCLSID( riid, &IID_IWineMsiRemoteCustomAction ) )
1398 {
1399 IWineMsiRemoteCustomAction_AddRef( iface );
1400 *ppobj = iface;
1401 return S_OK;
1402 }
1403
1404 return E_NOINTERFACE;
1405 }
1406
1407 static ULONG WINAPI mcr_AddRef( IWineMsiRemoteCustomAction *iface )
1408 {
1409 msi_custom_remote_impl* This = impl_from_IWineMsiRemoteCustomAction( iface );
1410
1411 return InterlockedIncrement( &This->refs );
1412 }
1413
1414 static ULONG WINAPI mcr_Release( IWineMsiRemoteCustomAction *iface )
1415 {
1416 msi_custom_remote_impl* This = impl_from_IWineMsiRemoteCustomAction( iface );
1417 ULONG r;
1418
1419 r = InterlockedDecrement( &This->refs );
1420 if (r == 0)
1421 msi_free( This );
1422 return r;
1423 }
1424
1425 static HRESULT WINAPI mcr_GetActionInfo( IWineMsiRemoteCustomAction *iface, LPCGUID custom_action_guid,
1426 INT *type, MSIHANDLE *handle, BSTR *dll, BSTR *func, IWineMsiRemotePackage **remote_package )
1427 {
1428 msi_custom_action_info *info;
1429
1430 info = find_action_by_guid( custom_action_guid );
1431 if (!info)
1432 return E_FAIL;
1433
1434 *type = info->type;
1435 *handle = alloc_msihandle( &info->package->hdr );
1436 *dll = SysAllocString( info->source );
1437 *func = SysAllocString( info->target );
1438
1439 release_custom_action_data( info );
1440 return create_msi_remote_package( NULL, (LPVOID *)remote_package );
1441 }
1442
1443 static const IWineMsiRemoteCustomActionVtbl msi_custom_remote_vtbl =
1444 {
1445 mcr_QueryInterface,
1446 mcr_AddRef,
1447 mcr_Release,
1448 mcr_GetActionInfo,
1449 };
1450
1451 HRESULT create_msi_custom_remote( IUnknown *pOuter, LPVOID *ppObj )
1452 {
1453 msi_custom_remote_impl* This;
1454
1455 This = msi_alloc( sizeof *This );
1456 if (!This)
1457 return E_OUTOFMEMORY;
1458
1459 This->IWineMsiRemoteCustomAction_iface.lpVtbl = &msi_custom_remote_vtbl;
1460 This->refs = 1;
1461
1462 *ppObj = &This->IWineMsiRemoteCustomAction_iface;
1463
1464 return S_OK;
1465 }