custom.c 24.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Custom Action processing for the Microsoft Installer (msi.dll)
 *
 * Copyright 2005 Aric Stewart for CodeWeavers
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 22
 */

#define COBJMACROS

23
#include <stdarg.h>
24 25 26
#include "windef.h"
#include "winbase.h"
#include "winerror.h"
27
#include "msidefs.h"
28 29
#include "msipriv.h"
#include "winuser.h"
30
#include "wine/debug.h"
31 32 33 34 35 36 37 38 39 40
#include "wine/unicode.h"

WINE_DEFAULT_DEBUG_CHANNEL(msi);

#define CUSTOM_ACTION_TYPE_MASK 0x3F
static const WCHAR c_collen[] = {'C',':','\\',0};
static const WCHAR cszTempFolder[]= {'T','e','m','p','F','o','l','d','e','r',0};

typedef struct tagMSIRUNNINGACTION
{
41
    struct list entry;
42 43 44 45 46 47 48 49 50
    HANDLE handle;
    BOOL   process;
    LPWSTR name;
} MSIRUNNINGACTION;

static UINT HANDLE_CustomType1(MSIPACKAGE *package, LPCWSTR source,
                               LPCWSTR target, const INT type, LPCWSTR action);
static UINT HANDLE_CustomType2(MSIPACKAGE *package, LPCWSTR source,
                               LPCWSTR target, const INT type, LPCWSTR action);
51 52
static UINT HANDLE_CustomType17(MSIPACKAGE *package, LPCWSTR source,
                                LPCWSTR target, const INT type, LPCWSTR action);
53 54
static UINT HANDLE_CustomType18(MSIPACKAGE *package, LPCWSTR source,
                                LPCWSTR target, const INT type, LPCWSTR action);
55 56
static UINT HANDLE_CustomType19(MSIPACKAGE *package, LPCWSTR source,
                                LPCWSTR target, const INT type, LPCWSTR action);
57 58 59 60 61
static UINT HANDLE_CustomType50(MSIPACKAGE *package, LPCWSTR source,
                                LPCWSTR target, const INT type, LPCWSTR action);
static UINT HANDLE_CustomType34(MSIPACKAGE *package, LPCWSTR source,
                                LPCWSTR target, const INT type, LPCWSTR action);

62
typedef UINT (WINAPI *MsiCustomActionEntryPoint)( MSIHANDLE );
63

64 65 66 67 68 69 70 71 72 73 74 75
static CRITICAL_SECTION msi_custom_action_cs;
static CRITICAL_SECTION_DEBUG msi_custom_action_cs_debug =
{
    0, 0, &msi_custom_action_cs,
    { &msi_custom_action_cs_debug.ProcessLocksList,
      &msi_custom_action_cs_debug.ProcessLocksList },
      0, 0, { (DWORD_PTR)(__FILE__ ": msi_custom_action_cs") }
};
static CRITICAL_SECTION msi_custom_action_cs = { &msi_custom_action_cs_debug, -1, 0, 0, 0, 0 };

static struct list msi_pending_custom_actions = LIST_INIT( msi_pending_custom_actions );

76 77 78 79 80
static BOOL check_execution_scheduling_options(MSIPACKAGE *package, LPCWSTR action, UINT options)
{
    if (!package->script)
        return TRUE;

81
    if ((options & msidbCustomActionTypeClientRepeat) ==
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
            msidbCustomActionTypeClientRepeat)
    {
        if (!(package->script->InWhatSequence & SEQUENCE_UI &&
            package->script->InWhatSequence & SEQUENCE_EXEC))
        {
            TRACE("Skipping action due to dbCustomActionTypeClientRepeat option.\n");
            return FALSE;
        }
    }
    else if (options & msidbCustomActionTypeFirstSequence)
    {
        if (package->script->InWhatSequence & SEQUENCE_UI &&
            package->script->InWhatSequence & SEQUENCE_EXEC )
        {
            TRACE("Skipping action due to msidbCustomActionTypeFirstSequence option.\n");
            return FALSE;
        }
    }
    else if (options & msidbCustomActionTypeOncePerProcess)
    {
        if (check_unique_action(package,action))
        {
            TRACE("Skipping action due to msidbCustomActionTypeOncePerProcess option.\n");
            return FALSE;
        }
        else
            register_unique_action(package,action);
    }

    return TRUE;
}

114 115 116
/* stores the CustomActionData before the action:
 *     [CustomActionData]Action
 */
117
static LPWSTR msi_get_deferred_action(LPCWSTR action, LPWSTR actiondata)
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
{
    LPWSTR deferred;
    DWORD len;

    static const WCHAR begin[] = {'[',0};
    static const WCHAR end[] = {']',0};

    if (!actiondata)
        return strdupW(action);

    len = lstrlenW(action) + lstrlenW(actiondata) + 3;
    deferred = msi_alloc(len * sizeof(WCHAR));

    lstrcpyW(deferred, begin);
    lstrcatW(deferred, actiondata);
    lstrcatW(deferred, end);
    lstrcatW(deferred, action);

    return deferred;
}

139 140 141 142 143
UINT ACTION_CustomAction(MSIPACKAGE *package,LPCWSTR action, BOOL execute)
{
    UINT rc = ERROR_SUCCESS;
    MSIRECORD * row = 0;
    static const WCHAR ExecSeqQuery[] =
144 145 146 147
    {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
     '`','C','u','s','t','o' ,'m','A','c','t','i','o','n','`',
     ' ','W','H','E','R','E',' ','`','A','c','t','i' ,'o','n','`',' ',
     '=',' ','\'','%','s','\'',0};
148
    UINT type;
149
    LPCWSTR source, target;
150 151
    LPWSTR ptr, deferred_data = NULL;
    LPWSTR action_copy = strdupW(action);
152 153
    WCHAR *deformated=NULL;

154 155 156 157 158 159 160 161
    /* deferred action: [CustomActionData]Action */
    if ((ptr = strchrW(action_copy, ']')))
    {
        deferred_data = action_copy + 1;
        *ptr = '\0';
        action = ptr + 1;
    }

162 163
    row = MSI_QueryGetRecord( package->db, ExecSeqQuery, action );
    if (!row)
164 165
    {
        msi_free(action_copy);
166
        return ERROR_CALL_NOT_IMPLEMENTED;
167
    }
168 169 170

    type = MSI_RecordGetInteger(row,2);

171 172
    source = MSI_RecordGetString(row,3);
    target = MSI_RecordGetString(row,4);
173 174 175 176 177

    TRACE("Handling custom action %s (%x %s %s)\n",debugstr_w(action),type,
          debugstr_w(source), debugstr_w(target));

    /* handle some of the deferred actions */
178 179 180 181
    if (type & msidbCustomActionTypeTSAware)
        FIXME("msidbCustomActionTypeTSAware not handled\n");

    if (type & msidbCustomActionTypeInScript)
182
    {
183 184 185 186
        if (type & msidbCustomActionTypeNoImpersonate)
            FIXME("msidbCustomActionTypeNoImpersonate not handled\n");

        if (type & msidbCustomActionTypeRollback)
187 188
        {
            FIXME("Rollback only action... rollbacks not supported yet\n");
189
            schedule_action(package, ROLLBACK_SCRIPT, action);
Mike McCormack's avatar
Mike McCormack committed
190 191
            rc = ERROR_SUCCESS;
            goto end;
192 193 194
        }
        if (!execute)
        {
195 196 197
            LPWSTR actiondata = msi_dup_property(package, action);
            LPWSTR deferred = msi_get_deferred_action(action, actiondata);

198
            if (type & msidbCustomActionTypeCommit)
199 200
            {
                TRACE("Deferring Commit Action!\n");
201
                schedule_action(package, COMMIT_SCRIPT, deferred);
202 203 204 205
            }
            else
            {
                TRACE("Deferring Action!\n");
206
                schedule_action(package, INSTALL_SCRIPT, deferred);
207 208
            }

Mike McCormack's avatar
Mike McCormack committed
209
            rc = ERROR_SUCCESS;
210
            msi_free(deferred);
Mike McCormack's avatar
Mike McCormack committed
211
            goto end;
212 213 214 215 216 217 218
        }
        else
        {
            /*Set ActionData*/

            static const WCHAR szActionData[] = {
            'C','u','s','t','o','m','A','c','t','i','o','n','D','a','t','a',0};
219
            static const WCHAR szBlank[] = {0};
220
            LPWSTR actiondata = msi_dup_property( package, action );
221 222 223
            if (deferred_data)
                MSI_SetPropertyW(package,szActionData,deferred_data);
            else if (actiondata)
224
                MSI_SetPropertyW(package,szActionData,actiondata);
225 226
            else
                MSI_SetPropertyW(package,szActionData,szBlank);
227
            msi_free(actiondata);
228 229
        }
    }
230
    else if (!check_execution_scheduling_options(package,action,type))
231
    {
Mike McCormack's avatar
Mike McCormack committed
232 233
        rc = ERROR_SUCCESS;
        goto end;
234
    }
235 236 237 238 239 240

    switch (type & CUSTOM_ACTION_TYPE_MASK)
    {
        case 1: /* DLL file stored in a Binary table stream */
            rc = HANDLE_CustomType1(package,source,target,type,action);
            break;
241
        case 2: /* EXE file stored in a Binary table stream */
242 243 244 245 246
            rc = HANDLE_CustomType2(package,source,target,type,action);
            break;
        case 18: /*EXE file installed with package */
            rc = HANDLE_CustomType18(package,source,target,type,action);
            break;
247
        case 19: /* Error that halts install */
248
            rc = HANDLE_CustomType19(package,source,target,type,action);
249
            break;
250 251 252
        case 17:
            rc = HANDLE_CustomType17(package,source,target,type,action);
            break;
253 254 255 256 257 258 259 260 261
        case 50: /*EXE file specified by a property value */
            rc = HANDLE_CustomType50(package,source,target,type,action);
            break;
        case 34: /*EXE to be run in specified directory */
            rc = HANDLE_CustomType34(package,source,target,type,action);
            break;
        case 35: /* Directory set with formatted text. */
            deformat_string(package,target,&deformated);
            MSI_SetTargetPathW(package, source, deformated);
262
            msi_free(deformated);
263 264 265 266
            break;
        case 51: /* Property set with formatted text. */
            deformat_string(package,target,&deformated);
            rc = MSI_SetPropertyW(package,source,deformated);
267
            msi_free(deformated);
268 269 270 271 272 273 274
            break;
        default:
            FIXME("UNHANDLED ACTION TYPE %i (%s %s)\n",
             type & CUSTOM_ACTION_TYPE_MASK, debugstr_w(source),
             debugstr_w(target));
    }

Mike McCormack's avatar
Mike McCormack committed
275
end:
276
    msi_free(action_copy);
277 278 279 280 281
    msiobj_release(&row->hdr);
    return rc;
}


282
static UINT store_binary_to_temp(MSIPACKAGE *package, LPCWSTR source,
283 284
                                LPWSTR tmp_file)
{
285 286 287 288 289 290 291
    static const WCHAR query[] = {
        'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
        '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',' ',
        '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
    MSIRECORD *row = 0;
    HANDLE file;
    CHAR buffer[1024];
292 293
    static const WCHAR f1[] = {'m','s','i',0};
    WCHAR fmt[MAX_PATH];
294
    DWORD sz = MAX_PATH;
295
    UINT r;
296

Mike McCormack's avatar
Mike McCormack committed
297
    if (MSI_GetPropertyW(package, cszTempFolder, fmt, &sz) != ERROR_SUCCESS)
298
        GetTempPathW(MAX_PATH, fmt);
299

300
    if (GetTempFileNameW(fmt, f1, 0, tmp_file) == 0)
301
    {
302 303
        TRACE("Unable to create file\n");
        return ERROR_FUNCTION_FAILED;
304
    }
305
    track_tempfile(package, tmp_file);
306

307 308 309
    row = MSI_QueryGetRecord(package->db, query, source);
    if (!row)
        return ERROR_FUNCTION_FAILED;
310

311 312 313 314 315 316
    /* write out the file */
    file = CreateFileW(tmp_file, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
                       FILE_ATTRIBUTE_NORMAL, NULL);
    if (file == INVALID_HANDLE_VALUE)
        r = ERROR_FUNCTION_FAILED;
    else
317
    {
318
        do
319
        {
320 321 322 323 324 325 326 327 328 329 330 331
            DWORD write;
            sz = sizeof buffer;
            r = MSI_RecordReadStream(row, 2, buffer, &sz);
            if (r != ERROR_SUCCESS)
            {
                ERR("Failed to get stream\n");
                break;
            }
            WriteFile(file, buffer, sz, &write, NULL);
        } while (sz == sizeof buffer);
        CloseHandle(file);
    }
332

333
    msiobj_release(&row->hdr);
334

335
    return r;
336 337
}

338
static void file_running_action(MSIPACKAGE* package, HANDLE Handle,
339 340
                                BOOL process, LPCWSTR name)
{
341 342
    MSIRUNNINGACTION *action;

343
    action = msi_alloc( sizeof(MSIRUNNINGACTION) );
344

345 346 347
    action->handle = Handle;
    action->process = process;
    action->name = strdupW(name);
348

349
    list_add_tail( &package->RunningActions, &action->entry );
350 351
}

352
static UINT custom_get_process_return( HANDLE process )
353
{
354
    DWORD rc = 0;
355

356 357 358 359 360 361 362 363 364 365 366
    GetExitCodeProcess( process, &rc );
    if (rc != 0)
        return ERROR_FUNCTION_FAILED;
    return ERROR_SUCCESS;
}

static UINT custom_get_thread_return( HANDLE thread )
{
    DWORD rc = 0;

    GetExitCodeThread( thread, &rc );
367 368 369

    switch (rc)
    {
370 371 372 373 374 375 376 377
    case ERROR_FUNCTION_NOT_CALLED:
    case ERROR_SUCCESS:
    case ERROR_INSTALL_USEREXIT:
    case ERROR_INSTALL_FAILURE:
        return rc;
    case ERROR_NO_MORE_ITEMS:
        return ERROR_SUCCESS;
    default:
378
        ERR("Invalid Return Code %d\n",rc);
379
        return ERROR_INSTALL_FAILURE;
380 381 382
    }
}

383 384
static UINT wait_process_handle(MSIPACKAGE* package, UINT type,
                           HANDLE ProcessHandle, LPCWSTR name)
385 386 387
{
    UINT rc = ERROR_SUCCESS;

388
    if (!(type & msidbCustomActionTypeAsync))
389
    {
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
        TRACE("waiting for %s\n", debugstr_w(name));

        msi_dialog_check_messages(ProcessHandle);

        if (!(type & msidbCustomActionTypeContinue))
            rc = custom_get_process_return(ProcessHandle);

        CloseHandle(ProcessHandle);
    }
    else
    {
        TRACE("%s running in background\n", debugstr_w(name));

        if (!(type & msidbCustomActionTypeContinue))
            file_running_action(package, ProcessHandle, TRUE, name);
405
        else
406 407 408 409 410 411
            CloseHandle(ProcessHandle);
    }

    return rc;
}

412 413 414 415 416 417 418 419
typedef struct _msi_custom_action_info {
    struct list entry;
    MSIPACKAGE *package;
    LPWSTR dllname;
    LPWSTR function;
    HANDLE handle;
    LPWSTR action;
    INT type;
420
    GUID guid;
421 422 423 424
} msi_custom_action_info;

static void free_custom_action_data( msi_custom_action_info *info )
{
425
    EnterCriticalSection( &msi_custom_action_cs );
426
    list_remove( &info->entry );
427
    LeaveCriticalSection( &msi_custom_action_cs );
428 429 430 431 432 433 434 435 436 437
    if (info->handle)
        CloseHandle( info->handle );
    msi_free( info->action );
    msi_free( info->dllname );
    msi_free( info->function );
    msiobj_release( &info->package->hdr );
    msi_free( info );
}

static UINT wait_thread_handle( msi_custom_action_info *info )
438 439 440
{
    UINT rc = ERROR_SUCCESS;

441
    if (!(info->type & msidbCustomActionTypeAsync))
442
    {
443
        TRACE("waiting for %s\n", debugstr_w( info->action ));
444

445
        msi_dialog_check_messages( info->handle );
446

447 448
        if (!(info->type & msidbCustomActionTypeContinue))
            rc = custom_get_thread_return( info->handle );
449

450
        free_custom_action_data( info );
451
    }
452
    else
453
    {
454
        TRACE("%s running in background\n", debugstr_w( info->action ));
455

456 457
        if (info->type & msidbCustomActionTypeContinue)
            free_custom_action_data( info );
458 459 460 461 462
    }

    return rc;
}

463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
static msi_custom_action_info *find_action_by_guid( const LPGUID guid )
{
    msi_custom_action_info *info;
    BOOL found = FALSE;

    EnterCriticalSection( &msi_custom_action_cs );

    LIST_FOR_EACH_ENTRY( info, &msi_pending_custom_actions, msi_custom_action_info, entry )
    {
        if (IsEqualGUID( &info->guid, guid ))
        {
            found = TRUE;
            break;
        }
    }

    LeaveCriticalSection( &msi_custom_action_cs );

    if (!found)
        return NULL;

    return info;
}
486

487
static DWORD WINAPI ACTION_CallDllFunction( const LPGUID guid )
488
{
489
    msi_custom_action_info *info;
490 491
    MsiCustomActionEntryPoint fn;
    MSIHANDLE hPackage;
492 493
    HANDLE hModule;
    LPSTR proc;
494
    UINT r = ERROR_FUNCTION_FAILED;
495

496 497 498 499 500 501 502
    info = find_action_by_guid( guid );
    if (!info)
    {
        ERR("failed to find action %s\n", debugstr_guid( guid) );
        return r;
    }

503
    TRACE("%s %s\n", debugstr_w( info->dllname ), debugstr_w( info->function ) );
504

505 506
    hModule = LoadLibraryW( info->dllname );
    if (!hModule)
507
    {
508 509 510
        ERR("failed to load dll %s\n", debugstr_w( info->dllname ) );
        return r;
    }
511

512 513 514 515 516 517 518 519 520 521 522
    proc = strdupWtoA( info->function );
    fn = (MsiCustomActionEntryPoint) GetProcAddress( hModule, proc );
    msi_free( proc );
    if (fn)
    {
        hPackage = alloc_msihandle( &info->package->hdr );
        if (hPackage)
        {
            TRACE("calling %s\n", debugstr_w( info->function ) );
            r = fn( hPackage );
            MsiCloseHandle( hPackage );
523 524
        }
        else
525
            ERR("failed to create handle for %p\n", info->package );
526 527
    }
    else
528 529 530 531 532
        ERR("GetProcAddress(%s) failed\n", debugstr_w( info->function ) );

    FreeLibrary(hModule);

    return r;
533 534
}

535
static DWORD WINAPI DllThread( LPVOID arg )
536
{
537
    LPGUID guid = arg;
538
    DWORD rc = 0;
539

540
    TRACE("custom action (%x) started\n", GetCurrentThreadId() );
541

542
    rc = ACTION_CallDllFunction( guid );
543 544

    TRACE("custom action (%x) returned %i\n", GetCurrentThreadId(), rc );
545 546 547 548 549

    MsiCloseAllHandles();
    return rc;
}

550 551
static msi_custom_action_info *do_msidbCustomActionTypeDll(
    MSIPACKAGE *package, INT type, LPCWSTR dllname, LPCWSTR function, LPCWSTR action )
552
{
553 554 555 556 557
    msi_custom_action_info *info;

    info = msi_alloc( sizeof *info );
    if (!info)
        return NULL;
558 559 560

    msiobj_addref( &package->hdr );
    info->package = package;
561 562 563 564
    info->type = type;
    info->function = strdupW( function );
    info->dllname = strdupW( dllname );
    info->action = strdupW( action );
565 566 567 568 569
    CoCreateGuid( &info->guid );

    EnterCriticalSection( &msi_custom_action_cs );
    list_add_tail( &msi_pending_custom_actions, &info->entry );
    LeaveCriticalSection( &msi_custom_action_cs );
570

571
    info->handle = CreateThread( NULL, 0, DllThread, &info->guid, 0, NULL );
572 573 574 575 576
    if (!info->handle)
    {
        free_custom_action_data( info );
        return NULL;
    }
577

578
    return info;
579 580
}

581
static UINT HANDLE_CustomType1(MSIPACKAGE *package, LPCWSTR source,
582 583
                               LPCWSTR target, const INT type, LPCWSTR action)
{
584
    msi_custom_action_info *info;
585
    WCHAR tmp_file[MAX_PATH];
586
    UINT r;
587

588 589 590
    r = store_binary_to_temp(package, source, tmp_file);
    if (r != ERROR_SUCCESS)
        return r;
591 592 593 594 595 596 597 598

    TRACE("Calling function %s from %s\n",debugstr_w(target),
          debugstr_w(tmp_file));

    if (!strchrW(tmp_file,'.'))
    {
        static const WCHAR dot[]={'.',0};
        strcatW(tmp_file,dot);
599
    }
600

601
    info = do_msidbCustomActionTypeDll( package, type, tmp_file, target, action );
602

603
    return wait_thread_handle( info );
604 605
}

606
static UINT HANDLE_CustomType2(MSIPACKAGE *package, LPCWSTR source,
607 608 609 610 611 612 613
                               LPCWSTR target, const INT type, LPCWSTR action)
{
    WCHAR tmp_file[MAX_PATH];
    STARTUPINFOW si;
    PROCESS_INFORMATION info;
    BOOL rc;
    INT len;
614
    WCHAR *deformated = NULL;
615 616
    WCHAR *cmd;
    static const WCHAR spc[] = {' ',0};
617
    UINT r;
618 619 620

    memset(&si,0,sizeof(STARTUPINFOW));

621 622 623
    r = store_binary_to_temp(package, source, tmp_file);
    if (r != ERROR_SUCCESS)
        return r;
624 625 626 627 628 629 630

    deformat_string(package,target,&deformated);

    len = strlenW(tmp_file)+2;

    if (deformated)
        len += strlenW(deformated);
631

632
    cmd = msi_alloc(sizeof(WCHAR)*len);
633 634 635 636 637 638 639

    strcpyW(cmd,tmp_file);
    if (deformated)
    {
        strcatW(cmd,spc);
        strcatW(cmd,deformated);

640
        msi_free(deformated);
641 642
    }

643
    TRACE("executing exe %s\n", debugstr_w(cmd));
644 645 646

    rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
                  c_collen, &si, &info);
647
    msi_free(cmd);
648 649 650

    if ( !rc )
    {
651
        ERR("Unable to execute command %s\n", debugstr_w(cmd));
652 653
        return ERROR_SUCCESS;
    }
654
    CloseHandle( info.hThread );
655

656
    r = wait_process_handle(package, type, info.hProcess, action);
657

658
    return r;
659 660
}

661 662 663
static UINT HANDLE_CustomType17(MSIPACKAGE *package, LPCWSTR source,
                                LPCWSTR target, const INT type, LPCWSTR action)
{
664
    msi_custom_action_info *info;
665 666 667 668 669 670 671 672 673 674 675
    MSIFILE *file;

    TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));

    file = get_loaded_file( package, source );
    if (!file)
    {
        ERR("invalid file key %s\n", debugstr_w( source ));
        return ERROR_FUNCTION_FAILED;
    }

676
    info = do_msidbCustomActionTypeDll( package, type, file->TargetPath, target, action );
677

678
    return wait_thread_handle( info );
679 680
}

681 682 683 684 685 686 687 688 689 690
static UINT HANDLE_CustomType18(MSIPACKAGE *package, LPCWSTR source,
                                LPCWSTR target, const INT type, LPCWSTR action)
{
    STARTUPINFOW si;
    PROCESS_INFORMATION info;
    BOOL rc;
    WCHAR *deformated;
    WCHAR *cmd;
    INT len;
    static const WCHAR spc[] = {' ',0};
691
    MSIFILE *file;
692 693 694

    memset(&si,0,sizeof(STARTUPINFOW));

695 696 697
    file = get_loaded_file(package,source);
    if( !file )
        return ERROR_FUNCTION_FAILED;
698

699
    len = lstrlenW( file->TargetPath );
700 701 702 703 704 705

    deformat_string(package,target,&deformated);
    if (deformated)
        len += strlenW(deformated);
    len += 2;

706
    cmd = msi_alloc(len * sizeof(WCHAR));
707

708
    lstrcpyW( cmd, file->TargetPath);
709 710 711 712 713
    if (deformated)
    {
        strcatW(cmd, spc);
        strcatW(cmd, deformated);

714
        msi_free(deformated);
715 716
    }

717
    TRACE("executing exe %s\n", debugstr_w(cmd));
718 719 720 721 722 723

    rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
                  c_collen, &si, &info);

    if ( !rc )
    {
724 725
        ERR("Unable to execute command %s\n", debugstr_w(cmd));
        msi_free(cmd);
726 727
        return ERROR_SUCCESS;
    }
728
    msi_free(cmd);
729
    CloseHandle( info.hThread );
730

731
    return wait_process_handle(package, type, info.hProcess, action);
732 733
}

734 735 736 737 738 739
static UINT HANDLE_CustomType19(MSIPACKAGE *package, LPCWSTR source,
                                LPCWSTR target, const INT type, LPCWSTR action)
{
    static const WCHAR query[] = {
      'S','E','L','E','C','T',' ','`','M','e','s','s','a','g','e','`',' ',
      'F','R','O','M',' ','`','E','r','r','o','r','`',' ',
740
      'W','H','E','R','E',' ','`','E','r','r','o','r','`',' ','=',' ',
741
      '%','s',0
742 743 744 745 746 747 748
    };
    MSIRECORD *row = 0;
    LPWSTR deformated = NULL;

    deformat_string( package, target, &deformated );

    /* first try treat the error as a number */
749 750
    row = MSI_QueryGetRecord( package->db, query, deformated );
    if( row )
751
    {
752 753 754
        LPCWSTR error = MSI_RecordGetString( row, 1 );
        MessageBoxW( NULL, error, NULL, MB_OK );
        msiobj_release( &row->hdr );
755
    }
756
    else
757
        MessageBoxW( NULL, deformated, NULL, MB_OK );
758

759
    msi_free( deformated );
760 761 762 763

    return ERROR_FUNCTION_FAILED;
}

764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
static UINT HANDLE_CustomType50(MSIPACKAGE *package, LPCWSTR source,
                                LPCWSTR target, const INT type, LPCWSTR action)
{
    STARTUPINFOW si;
    PROCESS_INFORMATION info;
    WCHAR *prop;
    BOOL rc;
    WCHAR *deformated;
    WCHAR *cmd;
    INT len;
    static const WCHAR spc[] = {' ',0};

    memset(&si,0,sizeof(STARTUPINFOW));
    memset(&info,0,sizeof(PROCESS_INFORMATION));

779
    prop = msi_dup_property( package, source );
780
    if (!prop)
781
        return ERROR_SUCCESS;
782 783 784 785 786 787

    deformat_string(package,target,&deformated);
    len = strlenW(prop) + 2;
    if (deformated)
         len += strlenW(deformated);

788
    cmd = msi_alloc(sizeof(WCHAR)*len);
789 790 791 792 793 794 795

    strcpyW(cmd,prop);
    if (deformated)
    {
        strcatW(cmd,spc);
        strcatW(cmd,deformated);

796
        msi_free(deformated);
797
    }
798
    msi_free(prop);
799

800
    TRACE("executing exe %s\n", debugstr_w(cmd));
801 802 803 804 805 806

    rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
                  c_collen, &si, &info);

    if ( !rc )
    {
807 808
        ERR("Unable to execute command %s\n", debugstr_w(cmd));
        msi_free(cmd);
809 810
        return ERROR_SUCCESS;
    }
811
    msi_free(cmd);
812

813 814 815
    CloseHandle( info.hThread );

    return wait_process_handle(package, type, info.hProcess, action);
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833
}

static UINT HANDLE_CustomType34(MSIPACKAGE *package, LPCWSTR source,
                                LPCWSTR target, const INT type, LPCWSTR action)
{
    LPWSTR filename, deformated;
    STARTUPINFOW si;
    PROCESS_INFORMATION info;
    BOOL rc;

    memset(&si,0,sizeof(STARTUPINFOW));

    filename = resolve_folder(package, source, FALSE, FALSE, NULL);

    if (!filename)
        return ERROR_FUNCTION_FAILED;

    SetCurrentDirectoryW(filename);
834
    msi_free(filename);
835 836 837 838 839 840

    deformat_string(package,target,&deformated);

    if (!deformated)
        return ERROR_FUNCTION_FAILED;

841
    TRACE("executing exe %s\n", debugstr_w(deformated));
842 843 844 845 846 847

    rc = CreateProcessW(NULL, deformated, NULL, NULL, FALSE, 0, NULL,
                  c_collen, &si, &info);

    if ( !rc )
    {
848 849
        ERR("Unable to execute command %s\n", debugstr_w(deformated));
        msi_free(deformated);
850 851
        return ERROR_SUCCESS;
    }
852
    msi_free(deformated);
853
    CloseHandle( info.hThread );
854

855
    return wait_process_handle(package, type, info.hProcess, action);
856 857 858 859
}

void ACTION_FinishCustomActions(MSIPACKAGE* package)
{
860
    struct list *item;
861

862
    while ((item = list_head( &package->RunningActions )))
863
    {
864 865 866 867
        MSIRUNNINGACTION *action = LIST_ENTRY( item, MSIRUNNINGACTION, entry );

        list_remove( &action->entry );

868 869
        TRACE("waiting for %s\n", debugstr_w( action->name ) );
        msi_dialog_check_messages( action->handle );
870

871
        CloseHandle( action->handle );
872 873
        msi_free( action->name );
        msi_free( action );
874
    }
875

876
    while (1)
877
    {
878
        HANDLE handle;
879 880
        msi_custom_action_info *info;

881 882
        EnterCriticalSection( &msi_custom_action_cs );
        item = list_head( &msi_pending_custom_actions );
883
        info = LIST_ENTRY( item, msi_custom_action_info, entry );
884 885 886 887 888 889 890 891 892 893 894 895 896 897

        if (item && info->package == package )
        {
            handle = info->handle;
            free_custom_action_data( info );
        }
        else
            handle = NULL;
        LeaveCriticalSection( &msi_custom_action_cs );

        if (!item)
            break;

        msi_dialog_check_messages( handle );
898
    }
899
}