summaryrefslogtreecommitdiff
path: root/src/libs/dutil/WixToolset.DUtil/procutil.cpp
blob: 6cd3214ca22bceaf5460887615d1edc5bb27c6b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.

#include "precomp.h"


// Exit macros
#define ProcExitOnLastError(x, s, ...) ExitOnLastErrorSource(DUTIL_SOURCE_PROCUTIL, x, s, __VA_ARGS__)
#define ProcExitOnLastErrorDebugTrace(x, s, ...) ExitOnLastErrorDebugTraceSource(DUTIL_SOURCE_PROCUTIL, x, s, __VA_ARGS__)
#define ProcExitWithLastError(x, s, ...) ExitWithLastErrorSource(DUTIL_SOURCE_PROCUTIL, x, s, __VA_ARGS__)
#define ProcExitOnFailure(x, s, ...) ExitOnFailureSource(DUTIL_SOURCE_PROCUTIL, x, s, __VA_ARGS__)
#define ProcExitOnRootFailure(x, s, ...) ExitOnRootFailureSource(DUTIL_SOURCE_PROCUTIL, x, s, __VA_ARGS__)
#define ProcExitWithRootFailure(x, e, s, ...) ExitWithRootFailureSource(DUTIL_SOURCE_PROCUTIL, x, e, s, __VA_ARGS__)
#define ProcExitOnFailureDebugTrace(x, s, ...) ExitOnFailureDebugTraceSource(DUTIL_SOURCE_PROCUTIL, x, s, __VA_ARGS__)
#define ProcExitOnNull(p, x, e, s, ...) ExitOnNullSource(DUTIL_SOURCE_PROCUTIL, p, x, e, s, __VA_ARGS__)
#define ProcExitOnNullWithLastError(p, x, s, ...) ExitOnNullWithLastErrorSource(DUTIL_SOURCE_PROCUTIL, p, x, s, __VA_ARGS__)
#define ProcExitOnNullDebugTrace(p, x, e, s, ...)  ExitOnNullDebugTraceSource(DUTIL_SOURCE_PROCUTIL, p, x, e, s, __VA_ARGS__)
#define ProcExitOnInvalidHandleWithLastError(p, x, s, ...) ExitOnInvalidHandleWithLastErrorSource(DUTIL_SOURCE_PROCUTIL, p, x, s, __VA_ARGS__)
#define ProcExitOnWin32Error(e, x, s, ...) ExitOnWin32ErrorSource(DUTIL_SOURCE_PROCUTIL, e, x, s, __VA_ARGS__)
#define ProcExitOnGdipFailure(g, x, s, ...) ExitOnGdipFailureSource(DUTIL_SOURCE_PROCUTIL, g, x, s, __VA_ARGS__)
#define ProcExitOnWaitObjectFailure(x, b, s, ...) ExitOnWaitObjectFailureSource(DUTIL_SOURCE_PROCUTIL, x, b, s, __VA_ARGS__)


// private functions
static HRESULT CreatePipes(
    __out HANDLE *phOutRead,
    __out HANDLE *phOutWrite,
    __out HANDLE *phErrWrite,
    __out HANDLE *phInRead,
    __out HANDLE *phInWrite
    );

static BOOL CALLBACK CloseWindowEnumCallback(
    __in HWND hWnd,
    __in LPARAM lParam
    );


extern "C" HRESULT DAPI ProcElevated(
    __in HANDLE hProcess,
    __out BOOL* pfElevated
    )
{
    HRESULT hr = S_OK;
    HANDLE hToken = NULL;
    TOKEN_ELEVATION tokenElevated = { };
    DWORD cbToken = 0;

    if (!::OpenProcessToken(hProcess, TOKEN_QUERY, &hToken))
    {
        ProcExitWithLastError(hr, "Failed to open process token.");
    }

    if (::GetTokenInformation(hToken, TokenElevation, &tokenElevated, sizeof(TOKEN_ELEVATION), &cbToken))
    {
        *pfElevated = (0 != tokenElevated.TokenIsElevated);
    }
    else
    {
        DWORD er = ::GetLastError();
        hr = HRESULT_FROM_WIN32(er);

        // If it's invalid argument, this means the OS doesn't support TokenElevation, so we're not elevated.
        if (E_INVALIDARG == hr)
        {
            *pfElevated = FALSE;
            hr = S_OK;
        }
        else
        {
            ProcExitOnRootFailure(hr, "Failed to get elevation token from process.");
        }
    }

LExit:
    ReleaseHandle(hToken);

    return hr;
}

extern "C" HRESULT DAPI ProcSystem(
    __in HANDLE hProcess,
    __out BOOL* pfSystem
    )
{
    HRESULT hr = S_OK;
    TOKEN_USER* pTokenUser = NULL;

    hr = ProcGetTokenInformation(hProcess, TokenUser, reinterpret_cast<LPVOID*>(&pTokenUser));
    ProcExitOnFailure(hr, "Failed to get TokenUser from process token.");

    *pfSystem = ::IsWellKnownSid(pTokenUser->User.Sid, WinLocalSystemSid);

LExit:
    ReleaseMem(pTokenUser);

    return hr;
}

extern "C" HRESULT DAPI ProcGetTokenInformation(
    __in HANDLE hProcess,
    __in TOKEN_INFORMATION_CLASS tokenInformationClass,
    __out LPVOID* ppvTokenInformation
    )
{
    HRESULT hr = S_OK;
    DWORD er = ERROR_SUCCESS;
    HANDLE hToken = NULL;
    LPVOID pvTokenInformation = NULL;
    DWORD cbToken = 0;

    if (!::OpenProcessToken(hProcess, TOKEN_QUERY, &hToken))
    {
        ProcExitWithLastError(hr, "Failed to open process token.");
    }

    if (!::GetTokenInformation(hToken, tokenInformationClass, pvTokenInformation, 0, &cbToken))
    {
        er = ::GetLastError();
    }

    if (er != ERROR_INSUFFICIENT_BUFFER)
    {
        ProcExitOnWin32Error(er, hr, "Failed to get information from process token size.");
    }

    pvTokenInformation = MemAlloc(cbToken, TRUE);
    ProcExitOnNull(pvTokenInformation, hr, E_OUTOFMEMORY, "Failed to allocate token information.");

    if (!::GetTokenInformation(hToken, tokenInformationClass, pvTokenInformation, cbToken, &cbToken))
    {
        ProcExitWithLastError(hr, "Failed to get information from process token.");
    }

    *ppvTokenInformation = pvTokenInformation;
    pvTokenInformation = NULL;

LExit:
    ReleaseMem(pvTokenInformation);
    ReleaseHandle(hToken);

    return hr;
}

extern "C" HRESULT DAPI ProcHasPrivilege(
    __in HANDLE hProcess,
    __in LPCWSTR wzPrivilegeName,
    __out BOOL* pfHasPrivilege
    )
{
    HRESULT hr = S_OK;
    TOKEN_PRIVILEGES* pTokenPrivileges = NULL;
    LUID luidPrivilege = { };

    *pfHasPrivilege = FALSE;

    if (!::LookupPrivilegeValueW(NULL, wzPrivilegeName, &luidPrivilege))
    {
        ProcExitWithLastError(hr, "Failed to get privilege LUID: %ls", wzPrivilegeName);
    }

    hr = ProcGetTokenInformation(hProcess, TokenPrivileges, reinterpret_cast<LPVOID*>(&pTokenPrivileges));
    ProcExitOnFailure(hr, "Failed to get token privilege information.");

    for (DWORD i = 0; i < pTokenPrivileges->PrivilegeCount; ++i)
    {
        LUID* pTokenLuid = &pTokenPrivileges->Privileges[i].Luid;

        if (luidPrivilege.LowPart == pTokenLuid->LowPart && luidPrivilege.HighPart == pTokenLuid->HighPart)
        {
            *pfHasPrivilege = TRUE;
            break;
        }
    }

LExit:
    ReleaseMem(pTokenPrivileges);

    return hr;
}

extern "C" HRESULT DAPI ProcEnablePrivilege(
    __in HANDLE hProcess,
    __in LPCWSTR wzPrivilegeName
    )
{
    HRESULT hr = S_OK;
    HANDLE hToken = NULL;
    TOKEN_PRIVILEGES priv = { };

    priv.PrivilegeCount = 1;
    priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

    if (!::LookupPrivilegeValueW(NULL, wzPrivilegeName, &priv.Privileges[0].Luid))
    {
        ProcExitWithLastError(hr, "Failed to get privilege LUID: %ls", wzPrivilegeName);
    }

    if (!::OpenProcessToken(hProcess, TOKEN_ADJUST_PRIVILEGES, &hToken))
    {
        ProcExitWithLastError(hr, "Failed to get process token to adjust privileges.");
    }

    if (!::AdjustTokenPrivileges(hToken, FALSE, &priv, sizeof(TOKEN_PRIVILEGES), NULL, 0))
    {
        ProcExitWithLastError(hr, "Failed to adjust token to add privilege: %ls", wzPrivilegeName);
    }

    if (ERROR_NOT_ALL_ASSIGNED == ::GetLastError())
    {
        hr = S_FALSE;
    }

LExit:
    ReleaseHandle(hToken);

    return hr;
}

extern "C" HRESULT DAPI ProcWow64(
    __in HANDLE hProcess,
    __out BOOL* pfWow64
    )
{
    HRESULT hr = S_OK;
    BOOL fIsWow64 = FALSE;

    typedef BOOL(WINAPI* LPFN_ISWOW64PROCESS2)(HANDLE, USHORT *, USHORT *);
    LPFN_ISWOW64PROCESS2 pfnIsWow64Process2 = (LPFN_ISWOW64PROCESS2)::GetProcAddress(::GetModuleHandleW(L"kernel32"), "IsWow64Process2");

    if (pfnIsWow64Process2)
    {
        USHORT usProcessMachine = IMAGE_FILE_MACHINE_UNKNOWN;
        if (!pfnIsWow64Process2(hProcess, &usProcessMachine, nullptr))
        {
            ProcExitWithLastError(hr, "Failed to check WOW64 process - IsWow64Process2.");
        }

        if (usProcessMachine != IMAGE_FILE_MACHINE_UNKNOWN)
        {
            fIsWow64 = TRUE;
        }
    }
    else
    {
        typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS)(HANDLE, PBOOL);
        LPFN_ISWOW64PROCESS pfnIsWow64Process = (LPFN_ISWOW64PROCESS)::GetProcAddress(::GetModuleHandleW(L"kernel32"), "IsWow64Process");

        if (pfnIsWow64Process)
        {
            if (!pfnIsWow64Process(hProcess, &fIsWow64))
            {
                ProcExitWithLastError(hr, "Failed to check WOW64 process - IsWow64Process.");
            }
        }
    }

    *pfWow64 = fIsWow64;

LExit:
    return hr;
}

extern "C" HRESULT DAPI ProcNativeMachine(
    __in HANDLE hProcess,
    __out USHORT* pusNativeMachine
    )
{
    // S_FALSE will indicate that the method is not supported.
    HRESULT hr = S_FALSE;

    typedef BOOL(WINAPI* LPFN_ISWOW64PROCESS2)(HANDLE, USHORT *, USHORT *);
    LPFN_ISWOW64PROCESS2 pfnIsWow64Process2 = (LPFN_ISWOW64PROCESS2)::GetProcAddress(::GetModuleHandleW(L"kernel32"), "IsWow64Process2");

    if (pfnIsWow64Process2)
    {
        USHORT usProcessMachineUnused = IMAGE_FILE_MACHINE_UNKNOWN;
        if (!pfnIsWow64Process2(hProcess, &usProcessMachineUnused, pusNativeMachine))
        {
            ExitWithLastError(hr, "Failed to check WOW64 process - IsWow64Process2.");
        }
        hr = S_OK;
    }

LExit:
    return hr;
}

extern "C" HRESULT DAPI ProcDisableWowFileSystemRedirection(
    __in PROC_FILESYSTEMREDIRECTION* pfsr
    )
{
    AssertSz(!pfsr->fDisabled, "File system redirection was already disabled.");
    HRESULT hr = S_OK;

    typedef BOOL (WINAPI *LPFN_Wow64DisableWow64FsRedirection)(PVOID *);
    LPFN_Wow64DisableWow64FsRedirection pfnWow64DisableWow64FsRedirection = (LPFN_Wow64DisableWow64FsRedirection)::GetProcAddress(::GetModuleHandleW(L"kernel32"), "Wow64DisableWow64FsRedirection");

    if (!pfnWow64DisableWow64FsRedirection)
    {
        ExitFunction1(hr = E_NOTIMPL);
    }

    if (!pfnWow64DisableWow64FsRedirection(&pfsr->pvRevertState))
    {
        ProcExitWithLastError(hr, "Failed to disable file system redirection.");
    }

    pfsr->fDisabled = TRUE;

LExit:
    return hr;
}

extern "C" HRESULT DAPI ProcRevertWowFileSystemRedirection(
    __in PROC_FILESYSTEMREDIRECTION* pfsr
    )
{
    HRESULT hr = S_OK;

    if (pfsr->fDisabled)
    {
        typedef BOOL (WINAPI *LPFN_Wow64RevertWow64FsRedirection)(PVOID);
        LPFN_Wow64RevertWow64FsRedirection pfnWow64RevertWow64FsRedirection = (LPFN_Wow64RevertWow64FsRedirection)::GetProcAddress(::GetModuleHandleW(L"kernel32"), "Wow64RevertWow64FsRedirection");

        if (!pfnWow64RevertWow64FsRedirection(pfsr->pvRevertState))
        {
            ProcExitWithLastError(hr, "Failed to revert file system redirection.");
        }

        pfsr->fDisabled = FALSE;
        pfsr->pvRevertState = NULL;
    }

LExit:
    return hr;
}


extern "C" HRESULT DAPI ProcExec(
    __in_z LPCWSTR wzExecutablePath,
    __in_z_opt LPCWSTR wzCommandLine,
    __in int nCmdShow,
    __out HANDLE *phProcess
    )
{
    HRESULT hr = S_OK;
    LPWSTR sczFullCommandLine = NULL;
    STARTUPINFOW si = { };
    PROCESS_INFORMATION pi = { };

    hr = StrAllocFormatted(&sczFullCommandLine, L"\"%ls\" %ls", wzExecutablePath, wzCommandLine ? wzCommandLine : L"");
    ProcExitOnFailure(hr, "Failed to allocate full command-line.");

    si.cb = sizeof(si);
    si.dwFlags = STARTF_USESHOWWINDOW;
    si.wShowWindow = static_cast<WORD>(nCmdShow);
    if (!::CreateProcessW(wzExecutablePath, sczFullCommandLine, NULL, NULL, FALSE, 0, 0, NULL, &si, &pi))
    {
        ProcExitWithLastError(hr, "Failed to create process: %ls", sczFullCommandLine);
    }

    *phProcess = pi.hProcess;
    pi.hProcess = NULL;

LExit:
    ReleaseHandle(pi.hThread);
    ReleaseHandle(pi.hProcess);
    ReleaseStr(sczFullCommandLine);

    return hr;
}


/********************************************************************
 ProcExecute() - executes a command-line.

*******************************************************************/
extern "C" HRESULT DAPI ProcExecute(
    __in_z_opt LPCWSTR wzApplicationName,
    __in_z LPWSTR wzCommand,
    __out HANDLE *phProcess,
    __out_opt HANDLE *phChildStdIn,
    __out_opt HANDLE *phChildStdOutErr
    )
{
    HRESULT hr = S_OK;

    PROCESS_INFORMATION pi = { };
    STARTUPINFOW si = { };

    HANDLE hOutRead = INVALID_HANDLE_VALUE;
    HANDLE hOutWrite = INVALID_HANDLE_VALUE;
    HANDLE hErrWrite = INVALID_HANDLE_VALUE;
    HANDLE hInRead = INVALID_HANDLE_VALUE;
    HANDLE hInWrite = INVALID_HANDLE_VALUE;

    // Create redirect pipes.
    hr = CreatePipes(&hOutRead, &hOutWrite, &hErrWrite, &hInRead, &hInWrite);
    ProcExitOnFailure(hr, "failed to create output pipes");

    // Set up startup structure.
    si.cb = sizeof(STARTUPINFOW);
    si.dwFlags = STARTF_USESTDHANDLES;
    si.hStdInput = hInRead;
    si.hStdOutput = hOutWrite;
    si.hStdError = hErrWrite;

#pragma prefast(push)
#pragma prefast(disable:25028)
    if (::CreateProcessW(wzApplicationName,
        wzCommand, // command line
        NULL, // security info
        NULL, // thread info
        TRUE, // inherit handles
        ::GetPriorityClass(::GetCurrentProcess()) | CREATE_NO_WINDOW, // creation flags
        NULL, // environment
        NULL, // cur dir
        &si,
        &pi))
#pragma prefast(pop)
    {
        // Close child process output/input handles so child doesn't hang
        // while waiting for input from parent process.
        ::CloseHandle(hOutWrite);
        hOutWrite = INVALID_HANDLE_VALUE;

        ::CloseHandle(hErrWrite);
        hErrWrite = INVALID_HANDLE_VALUE;

        ::CloseHandle(hInRead);
        hInRead = INVALID_HANDLE_VALUE;
    }
    else
    {
        ProcExitWithLastError(hr, "Process failed to execute.");
    }

    *phProcess = pi.hProcess;
    pi.hProcess = 0;

    if (phChildStdIn)
    {
        *phChildStdIn = hInWrite;
        hInWrite = INVALID_HANDLE_VALUE;
    }

    if (phChildStdOutErr)
    {
        *phChildStdOutErr = hOutRead;
        hOutRead = INVALID_HANDLE_VALUE;
    }

LExit:
    if (pi.hThread)
    {
        ::CloseHandle(pi.hThread);
    }

    if (pi.hProcess)
    {
        ::CloseHandle(pi.hProcess);
    }

    ReleaseFileHandle(hOutRead);
    ReleaseFileHandle(hOutWrite);
    ReleaseFileHandle(hErrWrite);
    ReleaseFileHandle(hInRead);
    ReleaseFileHandle(hInWrite);

    return hr;
}


/********************************************************************
 ProcWaitForCompletion() - waits for process to complete and gets return code.

*******************************************************************/
extern "C" HRESULT DAPI ProcWaitForCompletion(
    __in HANDLE hProcess,
    __in DWORD dwTimeout,
    __out_opt DWORD* pdwReturnCode
    )
{
    HRESULT hr = S_OK;
    BOOL fTimedOut = FALSE;

    // Wait for everything to finish.
    hr = AppWaitForSingleObject(hProcess, dwTimeout);
    ProcExitOnWaitObjectFailure(hr, fTimedOut, "Failed to wait for process to complete.");

    if (fTimedOut)
    {
        hr = HRESULT_FROM_WIN32(WAIT_TIMEOUT);
    }
    else if (pdwReturnCode && !::GetExitCodeProcess(hProcess, pdwReturnCode))
    {
        ProcExitWithLastError(hr, "Failed to get process return code.");
    }

LExit:
    return hr;
}

/********************************************************************
 ProcWaitForIds() - waits for multiple processes to complete.

*******************************************************************/
extern "C" HRESULT DAPI ProcWaitForIds(
    __in_ecount(cProcessIds) const DWORD rgdwProcessIds[],
    __in DWORD cProcessIds,
    __in DWORD dwMilliseconds
    )
{
    HRESULT hr = S_OK;
    HANDLE hProcess = NULL;
    HANDLE* rghProcesses = NULL;
    DWORD cProcesses = 0;
    BOOL fTimedOut = FALSE;

    rghProcesses =  static_cast<HANDLE*>(MemAlloc(sizeof(HANDLE) * cProcessIds, TRUE));
    ProcExitOnNull(rgdwProcessIds, hr, E_OUTOFMEMORY, "Failed to allocate array for process ID Handles.");

    for (DWORD i = 0; i < cProcessIds; ++i)
    {
        hProcess = ::OpenProcess(SYNCHRONIZE, FALSE, rgdwProcessIds[i]);
        if (hProcess != NULL)
        {
            rghProcesses[cProcesses++] = hProcess;
        }
    }

    hr = AppWaitForMultipleObjects(cProcesses, rghProcesses, TRUE, dwMilliseconds, NULL);
    ProcExitOnWaitObjectFailure(hr, fTimedOut, "Failed to wait for processes to complete.");

    if (fTimedOut)
    {
        ProcExitWithRootFailure(hr, HRESULT_FROM_WIN32(WAIT_TIMEOUT), "Timed out while waiting for processes to complete.");
    }

LExit:
    if (rghProcesses)
    {
        for (DWORD i = 0; i < cProcesses; ++i)
        {
            if (NULL != rghProcesses[i])
            {
                ::CloseHandle(rghProcesses[i]);
            }
        }
        
        MemFree(rghProcesses);
    }

    return hr;
}

/********************************************************************
 ProcCloseIds() - sends WM_CLOSE messages to all process ids.

*******************************************************************/
extern "C" HRESULT DAPI ProcCloseIds(
    __in_ecount(cProcessIds) const DWORD* pdwProcessIds,
    __in DWORD cProcessIds
    )
{
    HRESULT hr = S_OK;

    for (DWORD i = 0; i < cProcessIds; ++i)
    {
        if (!::EnumWindows(&CloseWindowEnumCallback, pdwProcessIds[i]))
        {
            ProcExitWithLastError(hr, "Failed to enumerate windows.");
        }
    }

LExit:
    return hr;
}


static HRESULT CreatePipes(
    __out HANDLE *phOutRead,
    __out HANDLE *phOutWrite,
    __out HANDLE *phErrWrite,
    __out HANDLE *phInRead,
    __out HANDLE *phInWrite
    )
{
    HRESULT hr = S_OK;
    SECURITY_ATTRIBUTES sa;
    HANDLE hOutTemp = INVALID_HANDLE_VALUE;
    HANDLE hInTemp = INVALID_HANDLE_VALUE;

    HANDLE hOutRead = INVALID_HANDLE_VALUE;
    HANDLE hOutWrite = INVALID_HANDLE_VALUE;
    HANDLE hErrWrite = INVALID_HANDLE_VALUE;
    HANDLE hInRead = INVALID_HANDLE_VALUE;
    HANDLE hInWrite = INVALID_HANDLE_VALUE;

    // Fill out security structure so we can inherit handles
    ZeroMemory(&sa, sizeof(SECURITY_ATTRIBUTES));
    sa.nLength = sizeof(SECURITY_ATTRIBUTES);
    sa.bInheritHandle = TRUE;
    sa.lpSecurityDescriptor = NULL;

    // Create pipes
    if (!::CreatePipe(&hOutTemp, &hOutWrite, &sa, 0))
    {
        ProcExitWithLastError(hr, "failed to create output pipe");
    }

    if (!::CreatePipe(&hInRead, &hInTemp, &sa, 0))
    {
        ProcExitWithLastError(hr, "failed to create input pipe");
    }

    // Duplicate output pipe so standard error and standard output write to the same pipe.
    if (!::DuplicateHandle(::GetCurrentProcess(), hOutWrite, ::GetCurrentProcess(), &hErrWrite, 0, TRUE, DUPLICATE_SAME_ACCESS))
    {
        ProcExitWithLastError(hr, "failed to duplicate write handle");
    }

    // We need to create new "output read" and "input write" handles that are non inheritable.  Otherwise CreateProcess will creates handles in 
    // the child process that can't be closed.
    if (!::DuplicateHandle(::GetCurrentProcess(), hOutTemp, ::GetCurrentProcess(), &hOutRead, 0, FALSE, DUPLICATE_SAME_ACCESS))
    {
        ProcExitWithLastError(hr, "failed to duplicate output pipe");
    }

    if (!::DuplicateHandle(::GetCurrentProcess(), hInTemp, ::GetCurrentProcess(), &hInWrite, 0, FALSE, DUPLICATE_SAME_ACCESS))
    {
        ProcExitWithLastError(hr, "failed to duplicate input pipe");
    }

    // now that everything has succeeded, assign to the outputs
    *phOutRead = hOutRead;
    hOutRead = INVALID_HANDLE_VALUE;

    *phOutWrite = hOutWrite;
    hOutWrite = INVALID_HANDLE_VALUE;

    *phErrWrite = hErrWrite;
    hErrWrite = INVALID_HANDLE_VALUE;

    *phInRead = hInRead;
    hInRead = INVALID_HANDLE_VALUE;

    *phInWrite = hInWrite;
    hInWrite = INVALID_HANDLE_VALUE;

LExit:
    ReleaseFileHandle(hOutRead);
    ReleaseFileHandle(hOutWrite);
    ReleaseFileHandle(hErrWrite);
    ReleaseFileHandle(hInRead);
    ReleaseFileHandle(hInWrite);
    ReleaseFileHandle(hOutTemp);
    ReleaseFileHandle(hInTemp);

    return hr;
}


/********************************************************************
 CloseWindowEnumCallback() - outputs trace and log info

*******************************************************************/
static BOOL CALLBACK CloseWindowEnumCallback(
    __in HWND hWnd,
    __in LPARAM lParam
    )
{
    DWORD dwPid = static_cast<DWORD>(lParam);
    DWORD dwProcessId = 0;

    ::GetWindowThreadProcessId(hWnd, &dwProcessId);
    if (dwPid == dwProcessId)
    {
        ::SendMessageW(hWnd, WM_CLOSE, 0, 0);
    }

    return TRUE;
}