aboutsummaryrefslogtreecommitdiff
path: root/src/ext/NetFx/netcoresearch/netcoresearch.cpp
blob: 517f6ac4a05d6b13788fc14ec287c8a1f3d931f2 (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
// 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"

struct NETCORESEARCH_STATE
{
    LPCWSTR wzTargetName;
    DWORD dwMajorVersion;
    VERUTIL_VERSION* pVersion;
};

static HRESULT GetDotnetEnvironmentInfo(
    __in DWORD dwMajorVersion,
    __in_z LPCWSTR wzTargetName,
    __inout VERUTIL_VERSION** ppVersion
    );
static void HOSTFXR_CALLTYPE GetDotnetEnvironmentInfoResult(
    __in const hostfxr_dotnet_environment_info* pInfo,
    __in LPVOID pvContext
    );

int __cdecl wmain(int argc, LPWSTR argv[])
{
    HRESULT hr = S_OK;
    DWORD dwMajorVersion = 0;
    VERUTIL_VERSION* pVersion = NULL;

    ConsoleInitialize();

    if (argc != 3)
    {
        ExitFunction1(hr = E_INVALIDARG);
    }

    hr = StrStringToUInt32(argv[1], 0, reinterpret_cast<UINT*>(&dwMajorVersion));
    ConsoleExitOnFailure(hr, CONSOLE_COLOR_RED, "Failed to get target version from: %ls", argv[1]);

    hr = GetDotnetEnvironmentInfo(dwMajorVersion, argv[2], &pVersion);
    ConsoleExitOnFailure(hr, CONSOLE_COLOR_RED, "Failed to search for .NET Core.");

    if (pVersion)
    {
        ConsoleWriteW(CONSOLE_COLOR_NORMAL, pVersion->sczVersion);
    }

LExit:
    ReleaseVerutilVersion(pVersion);
    ConsoleUninitialize();
    return hr;
}

static HRESULT GetDotnetEnvironmentInfo(
    __in DWORD dwMajorVersion,
    __in_z LPCWSTR wzTargetName,
    __inout VERUTIL_VERSION** ppVersion
    )
{
    HRESULT hr = S_OK;
    LPWSTR sczProcessPath = NULL;
    LPWSTR sczHostfxrPath = NULL;
    HMODULE hModule = NULL;
    hostfxr_get_dotnet_environment_info_fn pfnGetDotnetEnvironmentInfo = NULL;
    NETCORESEARCH_STATE state = { };

    state.dwMajorVersion = dwMajorVersion;
    state.wzTargetName = wzTargetName;

    hr = PathForCurrentProcess(&sczProcessPath, NULL);
    ConsoleExitOnFailure(hr, CONSOLE_COLOR_RED, "Failed to get process path.");

    hr = PathGetDirectory(sczProcessPath, &sczHostfxrPath);
    ConsoleExitOnFailure(hr, CONSOLE_COLOR_RED, "Failed to get process directory.");

    hr = StrAllocConcat(&sczHostfxrPath, L"hostfxr.dll", 0);
    ConsoleExitOnFailure(hr, CONSOLE_COLOR_RED, "Failed to build hostfxr path.");

    hModule = ::LoadLibraryExW(sczHostfxrPath, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
    ConsoleExitOnNullWithLastError(hModule, hr, CONSOLE_COLOR_RED, "Failed to load hostfxr.");

    pfnGetDotnetEnvironmentInfo = (hostfxr_get_dotnet_environment_info_fn)::GetProcAddress(hModule, "hostfxr_get_dotnet_environment_info");
    ConsoleExitOnNullWithLastError(pfnGetDotnetEnvironmentInfo, hr, CONSOLE_COLOR_RED, "Failed to get address for hostfxr_get_dotnet_environment_info.");

    hr = pfnGetDotnetEnvironmentInfo(NULL, NULL, GetDotnetEnvironmentInfoResult, &state);
    ConsoleExitOnFailure(hr, CONSOLE_COLOR_RED, "Failed to get .NET Core environment info.");

    if (state.pVersion)
    {
        *ppVersion = state.pVersion;
        state.pVersion = NULL;
    }

LExit:
    ReleaseVerutilVersion(state.pVersion);
    ReleaseStr(sczHostfxrPath);
    ReleaseStr(sczProcessPath);

    if (hModule)
    {
        ::FreeLibrary(hModule);
    }

    return hr;
}

static void HOSTFXR_CALLTYPE GetDotnetEnvironmentInfoResult(
    __in const hostfxr_dotnet_environment_info* pInfo,
    __in LPVOID pvContext
    )
{
    NETCORESEARCH_STATE* pState = reinterpret_cast<NETCORESEARCH_STATE*>(pvContext);
    HRESULT hr = S_OK;
    VERUTIL_VERSION* pFrameworkVersion = NULL;
    int nCompare = 0;

    for (size_t i = 0; i < pInfo->framework_count; ++i)
    {
        const hostfxr_dotnet_environment_framework_info* pFrameworkInfo = pInfo->frameworks + i;
        ReleaseVerutilVersion(pFrameworkVersion);

        if (CSTR_EQUAL != ::CompareStringW(LOCALE_INVARIANT, NORM_IGNORECASE, pState->wzTargetName, -1, pFrameworkInfo->name, -1))
        {
            continue;
        }

        hr = VerParseVersion(pFrameworkInfo->version, 0, FALSE, &pFrameworkVersion);
        ConsoleExitOnFailure(hr, CONSOLE_COLOR_RED, "Failed to parse framework version: %ls", pFrameworkInfo->version);

        if (pFrameworkVersion->dwMajor != pState->dwMajorVersion)
        {
            continue;
        }

        if (pState->pVersion)
        {
            hr = VerCompareParsedVersions(pState->pVersion, pFrameworkVersion, &nCompare);
            ConsoleExitOnFailure(hr, CONSOLE_COLOR_RED, "Failed to compare versions.");

            if (nCompare > -1)
            {
                continue;
            }
        }

        ReleaseVerutilVersion(pState->pVersion);
        pState->pVersion = pFrameworkVersion;
        pFrameworkVersion = NULL;
    }

LExit:
    ReleaseVerutilVersion(pFrameworkVersion);
}