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