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
|
// 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"
enum class NETCORESEARCHTYPE
{
None,
Runtime,
Sdk,
SdkFeatureBand,
};
struct NETCORESEARCH_STATE
{
NETCORESEARCHTYPE type;
HRESULT hrSearch;
VERUTIL_VERSION* pVersion;
struct
{
LPCWSTR wzTargetName;
DWORD dwMajorVersion;
} Runtime;
struct
{
DWORD dwMajorVersion;
} Sdk;
struct
{
DWORD dwMajorVersion;
DWORD dwMinorVersion;
DWORD dwPatchVersion;
} SdkFeatureBand;
};
static HRESULT GetSearchStateFromArguments(
__in int argc,
__in LPWSTR argv[],
__in NETCORESEARCH_STATE* pSearchState
);
static HRESULT GetDotnetEnvironmentInfo(
__in NETCORESEARCH_STATE* pSearchState
);
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;
NETCORESEARCH_STATE searchState = { };
ConsoleInitialize();
hr = GetSearchStateFromArguments(argc, argv, &searchState);
ConsoleExitOnFailure(hr, CONSOLE_COLOR_RED, "Failed to parse arguments.");
hr = GetDotnetEnvironmentInfo(&searchState);
ConsoleExitOnFailure(hr, CONSOLE_COLOR_RED, "Failed to search.");
if (searchState.pVersion)
{
ConsoleWriteW(CONSOLE_COLOR_NORMAL, searchState.pVersion->sczVersion);
}
LExit:
ReleaseVerutilVersion(searchState.pVersion);
ConsoleUninitialize();
return hr;
}
HRESULT GetSearchStateFromArguments(
__in int argc,
__in LPWSTR argv[],
__in NETCORESEARCH_STATE* pSearchState
)
{
HRESULT hr = S_OK;
LPCWSTR wzSearchKind = NULL;
if (argc < 2)
{
ExitFunction1(hr = E_INVALIDARG);
}
wzSearchKind = argv[1];
if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, NORM_IGNORECASE, wzSearchKind, -1, L"runtime", -1))
{
if (argc != 4)
{
ExitFunction1(hr = E_INVALIDARG);
}
LPCWSTR wzMajorVersion = argv[2];
LPCWSTR wzTargetName = argv[3];
pSearchState->type = NETCORESEARCHTYPE::Runtime;
hr = StrStringToUInt32(wzMajorVersion, 0, reinterpret_cast<UINT*>(&pSearchState->Runtime.dwMajorVersion));
ConsoleExitOnFailure(hr, CONSOLE_COLOR_RED, "Failed to get target version from: %ls", wzMajorVersion);
pSearchState->Runtime.wzTargetName = wzTargetName;
}
else if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, NORM_IGNORECASE, wzSearchKind, -1, L"sdk", -1))
{
if (argc != 3)
{
ExitFunction1(hr = E_INVALIDARG);
}
LPCWSTR wzMajorVersion = argv[2];
pSearchState->type = NETCORESEARCHTYPE::Sdk;
hr = StrStringToUInt32(wzMajorVersion, 0, reinterpret_cast<UINT*>(&pSearchState->Sdk.dwMajorVersion));
ConsoleExitOnFailure(hr, CONSOLE_COLOR_RED, "Failed to get sdk major version from: %ls", wzMajorVersion);
}
else if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, NORM_IGNORECASE, wzSearchKind, -1, L"sdkfeatureband", -1))
{
if (argc != 5)
{
ExitFunction1(hr = E_INVALIDARG);
}
LPCWSTR wzMajorVersion = argv[2];
LPCWSTR wzMinorVersion = argv[3];
LPCWSTR wzPatchVersion = argv[4];
pSearchState->type = NETCORESEARCHTYPE::SdkFeatureBand;
hr = StrStringToUInt32(wzMajorVersion, 0, reinterpret_cast<UINT*>(&pSearchState->SdkFeatureBand.dwMajorVersion));
ConsoleExitOnFailure(hr, CONSOLE_COLOR_RED, "Failed to get major version from: %ls", wzMajorVersion);
hr = StrStringToUInt32(wzMinorVersion, 0, reinterpret_cast<UINT*>(&pSearchState->SdkFeatureBand.dwMinorVersion));
ConsoleExitOnFailure(hr, CONSOLE_COLOR_RED, "Failed to get minor version from: %ls", wzMinorVersion);
hr = StrStringToUInt32(wzPatchVersion, 0, reinterpret_cast<UINT*>(&pSearchState->SdkFeatureBand.dwPatchVersion));
ConsoleExitOnFailure(hr, CONSOLE_COLOR_RED, "Failed to get patch version from: %ls", wzPatchVersion);
}
else
{
pSearchState->type = NETCORESEARCHTYPE::None;
ExitFunction1(hr = E_INVALIDARG);
}
LExit:
return hr;
}
static HRESULT GetDotnetEnvironmentInfo(
__in NETCORESEARCH_STATE* pState
)
{
HRESULT hr = S_OK;
LPWSTR sczProcessPath = NULL;
LPWSTR sczHostfxrPath = NULL;
HMODULE hModule = NULL;
hostfxr_get_dotnet_environment_info_fn pfnGetDotnetEnvironmentInfo = NULL;
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, pState);
ConsoleExitOnFailure(hr, CONSOLE_COLOR_RED, "Failed to get .NET Core environment info.");
hr = pState->hrSearch;
ConsoleExitOnFailure(hr, CONSOLE_COLOR_RED, "Failed to process .NET Core environment info.");
LExit:
ReleaseStr(sczHostfxrPath);
ReleaseStr(sczProcessPath);
if (hModule)
{
::FreeLibrary(hModule);
}
return hr;
}
static HRESULT PerformRuntimeSearch(
__in const hostfxr_dotnet_environment_info* pInfo,
__in DWORD dwMajorVersion,
__in LPCWSTR wzTargetName,
__inout VERUTIL_VERSION** ppVersion
)
{
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, 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 != dwMajorVersion)
{
continue;
}
if (*ppVersion)
{
hr = VerCompareParsedVersions(*ppVersion, pFrameworkVersion, &nCompare);
ConsoleExitOnFailure(hr, CONSOLE_COLOR_RED, "Failed to compare versions.");
if (nCompare > -1)
{
continue;
}
}
ReleaseVerutilVersion(*ppVersion);
*ppVersion = pFrameworkVersion;
pFrameworkVersion = NULL;
}
LExit:
ReleaseVerutilVersion(pFrameworkVersion);
return hr;
}
static HRESULT PerformSdkSearch(
__in const hostfxr_dotnet_environment_info* pInfo,
__in BOOL fFeatureBand,
__in DWORD dwMajorVersion,
__in DWORD dwMinorVersion,
__in DWORD dwPatchVersion,
__inout VERUTIL_VERSION** ppVersion
)
{
HRESULT hr = S_OK;
VERUTIL_VERSION* pSdkVersion = NULL;
int nCompare = 0;
DWORD dwRequestedBand = dwPatchVersion / 100;
for (size_t i = 0; i < pInfo->sdk_count; ++i)
{
const hostfxr_dotnet_environment_sdk_info* pSdkInfo = pInfo->sdks + i;
ReleaseVerutilVersion(pSdkVersion);
hr = VerParseVersion(pSdkInfo->version, 0, FALSE, &pSdkVersion);
ConsoleExitOnFailure(hr, CONSOLE_COLOR_RED, "Failed to parse sdk version: %ls", pSdkInfo->version);
if (pSdkVersion->dwMajor != dwMajorVersion)
{
continue;
}
if (fFeatureBand)
{
if (pSdkVersion->dwMinor != dwMinorVersion)
{
continue;
}
if ((pSdkVersion->dwPatch / 100) != dwRequestedBand)
{
continue;
}
}
if (*ppVersion)
{
hr = VerCompareParsedVersions(*ppVersion, pSdkVersion, &nCompare);
ConsoleExitOnFailure(hr, CONSOLE_COLOR_RED, "Failed to compare versions.");
if (nCompare > -1)
{
continue;
}
}
ReleaseVerutilVersion(*ppVersion);
*ppVersion = pSdkVersion;
pSdkVersion = NULL;
}
LExit:
ReleaseVerutilVersion(pSdkVersion);
return hr;
}
static void HOSTFXR_CALLTYPE GetDotnetEnvironmentInfoResult(
__in const hostfxr_dotnet_environment_info* pInfo,
__in LPVOID pvContext
)
{
NETCORESEARCH_STATE* pState = static_cast<NETCORESEARCH_STATE*>(pvContext);
HRESULT hr = S_OK;
if (pState->type == NETCORESEARCHTYPE::Sdk)
{
hr = PerformSdkSearch(pInfo, FALSE, pState->Sdk.dwMajorVersion, 0, 0, &pState->pVersion);
}
else if (pState->type == NETCORESEARCHTYPE::SdkFeatureBand)
{
hr = PerformSdkSearch(pInfo, TRUE, pState->SdkFeatureBand.dwMajorVersion, pState->SdkFeatureBand.dwMinorVersion, pState->SdkFeatureBand.dwPatchVersion, &pState->pVersion);
}
else if (pState->type == NETCORESEARCHTYPE::Runtime)
{
hr = PerformRuntimeSearch(pInfo, pState->Runtime.dwMajorVersion, pState->Runtime.wzTargetName, &pState->pVersion);
}
else
{
hr = E_INVALIDARG;
ConsoleExitOnFailure(hr, CONSOLE_COLOR_RED, "Invalid NETCORESEARCHTYPE.");
}
LExit:
pState->hrSearch = hr;
}
|