summaryrefslogtreecommitdiff
path: root/src/libs/dutil/WixToolset.DUtil/envutil.cpp
blob: aa9da233babf119451c7cce8738c642dcc1c4b58 (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
// 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 EnvExitOnLastError(x, s, ...) ExitOnLastErrorSource(DUTIL_SOURCE_ENVUTIL, x, s, __VA_ARGS__)
#define EnvExitOnLastErrorDebugTrace(x, s, ...) ExitOnLastErrorDebugTraceSource(DUTIL_SOURCE_ENVUTIL, x, s, __VA_ARGS__)
#define EnvExitWithLastError(x, s, ...) ExitWithLastErrorSource(DUTIL_SOURCE_ENVUTIL, x, s, __VA_ARGS__)
#define EnvExitOnFailure(x, s, ...) ExitOnFailureSource(DUTIL_SOURCE_ENVUTIL, x, s, __VA_ARGS__)
#define EnvExitOnRootFailure(x, s, ...) ExitOnRootFailureSource(DUTIL_SOURCE_ENVUTIL, x, s, __VA_ARGS__)
#define EnvExitWithRootFailure(x, e, s, ...) ExitWithRootFailureSource(DUTIL_SOURCE_ENVUTIL, x, e, s, __VA_ARGS__)
#define EnvExitOnFailureDebugTrace(x, s, ...) ExitOnFailureDebugTraceSource(DUTIL_SOURCE_ENVUTIL, x, s, __VA_ARGS__)
#define EnvExitOnNull(p, x, e, s, ...) ExitOnNullSource(DUTIL_SOURCE_ENVUTIL, p, x, e, s, __VA_ARGS__)
#define EnvExitOnNullWithLastError(p, x, s, ...) ExitOnNullWithLastErrorSource(DUTIL_SOURCE_ENVUTIL, p, x, s, __VA_ARGS__)
#define EnvExitOnNullDebugTrace(p, x, e, s, ...)  ExitOnNullDebugTraceSource(DUTIL_SOURCE_ENVUTIL, p, x, e, s, __VA_ARGS__)
#define EnvExitOnInvalidHandleWithLastError(p, x, s, ...) ExitOnInvalidHandleWithLastErrorSource(DUTIL_SOURCE_ENVUTIL, p, x, s, __VA_ARGS__)
#define EnvExitOnWin32Error(e, x, s, ...) ExitOnWin32ErrorSource(DUTIL_SOURCE_ENVUTIL, e, x, s, __VA_ARGS__)
#define EnvExitOnGdipFailure(g, x, s, ...) ExitOnGdipFailureSource(DUTIL_SOURCE_ENVUTIL, g, x, s, __VA_ARGS__)

#define ENV_GOOD_ENOUGH 64

DAPI_(HRESULT) EnvExpandEnvironmentStrings(
    __in LPCWSTR wzSource,
    __out LPWSTR* psczExpanded,
    __out_opt SIZE_T* pcchExpanded
    )
{
    HRESULT hr = S_OK;
    DWORD cch = 0;
    DWORD cchExpanded = 0;
    SIZE_T cchMax = 0;

    if (*psczExpanded)
    {
        hr = StrMaxLength(*psczExpanded, &cchMax);
        EnvExitOnFailure(hr, "Failed to get max length of input buffer.");

        cchExpanded = (DWORD)min(DWORD_MAX, cchMax);
    }
    else
    {
        cchExpanded = ENV_GOOD_ENOUGH;

        hr = StrAlloc(psczExpanded, cchExpanded);
        EnvExitOnFailure(hr, "Failed to allocate space for expanded path.");
    }

    cch = ::ExpandEnvironmentStringsW(wzSource, *psczExpanded, cchExpanded);
    if (!cch)
    {
        EnvExitWithLastError(hr, "Failed to expand environment variables in string: %ls", wzSource);
    }
    else if (cchExpanded < cch)
    {
        cchExpanded = cch;
        hr = StrAlloc(psczExpanded, cchExpanded);
        EnvExitOnFailure(hr, "Failed to re-allocate more space for expanded path.");

        cch = ::ExpandEnvironmentStringsW(wzSource, *psczExpanded, cchExpanded);
        if (!cch)
        {
            EnvExitWithLastError(hr, "Failed to expand environment variables in string: %ls", wzSource);
        }
        else if (cchExpanded < cch)
        {
            EnvExitWithRootFailure(hr, HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER), "Failed to allocate buffer for expanded string.");
        }
    }

    if (pcchExpanded)
    {
        *pcchExpanded = cch;
    }

LExit:
    return hr;
}