aboutsummaryrefslogtreecommitdiff
path: root/src/ext/NetFx/be/detectnetcoresdk.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/ext/NetFx/be/detectnetcoresdk.cpp')
-rw-r--r--src/ext/NetFx/be/detectnetcoresdk.cpp99
1 files changed, 12 insertions, 87 deletions
diff --git a/src/ext/NetFx/be/detectnetcoresdk.cpp b/src/ext/NetFx/be/detectnetcoresdk.cpp
index 08b18334..332d8712 100644
--- a/src/ext/NetFx/be/detectnetcoresdk.cpp
+++ b/src/ext/NetFx/be/detectnetcoresdk.cpp
@@ -4,99 +4,22 @@
4 4
5HRESULT DetectNetCoreSdk( 5HRESULT DetectNetCoreSdk(
6 __in NETFX_NET_CORE_PLATFORM platform, 6 __in NETFX_NET_CORE_PLATFORM platform,
7 __in LPCWSTR wzVersion, 7 __in LPCWSTR wzMajorVersion,
8 __in LPCWSTR wzBaseDirectory, 8 __in LPCWSTR wzBaseDirectory,
9 __inout LPWSTR* psczLatestVersion 9 __inout LPWSTR* psczLatestVersion
10 ) 10 )
11{ 11{
12 HRESULT hr = S_OK; 12 HRESULT hr = S_OK;
13 LPCWSTR wzPlatformName = NULL; 13 LPWSTR sczArguments = NULL;
14 LPWSTR sczExePath = NULL;
15 LPWSTR sczCommandLine = NULL;
16 HANDLE hProcess = NULL;
17 HANDLE hStdOutErr = INVALID_HANDLE_VALUE;
18 BYTE* rgbOutput = NULL;
19 DWORD cbOutput = 0;
20 DWORD cbTotalRead = 0;
21 DWORD cbRead = 0;
22 DWORD dwExitCode = 0;
23 14
24 ReleaseNullStr(*psczLatestVersion); 15 hr = StrAllocFormatted(&sczArguments, L"sdk %ls", wzMajorVersion);
16 BextExitOnFailure(hr, "Failed to build sdk netcoresearch.exe arguments.");
25 17
26 switch (platform) 18 hr = RunNetCoreSearch(platform, wzBaseDirectory, sczArguments, psczLatestVersion);
27 { 19 BextExitOnFailure(hr, "Failed to run netcoresearch.exe for sdk.");
28 case NETFX_NET_CORE_PLATFORM_ARM64:
29 wzPlatformName = L"arm64";
30 break;
31 case NETFX_NET_CORE_PLATFORM_X64:
32 wzPlatformName = L"x64";
33 break;
34 case NETFX_NET_CORE_PLATFORM_X86:
35 wzPlatformName = L"x86";
36 break;
37 default:
38 BextExitWithRootFailure(hr, E_INVALIDARG, "Unknown platform: %u", platform);
39 break;
40 }
41
42 hr = StrAllocFormatted(&sczExePath, L"%ls%ls\\netcoresearch.exe", wzBaseDirectory, wzPlatformName);
43 BextExitOnFailure(hr, "Failed to build netcoresearch.exe path.");
44
45 hr = StrAllocFormatted(&sczCommandLine, L"\"%ls\" sdk %ls", sczExePath, wzVersion);
46 BextExitOnFailure(hr, "Failed to build netcoresearch.exe command line.");
47
48 hr = ProcExecute(sczExePath, sczCommandLine, &hProcess, NULL, &hStdOutErr);
49 if (HRESULT_FROM_WIN32(ERROR_EXE_MACHINE_TYPE_MISMATCH) == hr)
50 {
51 ExitFunction1(hr = S_FALSE);
52 }
53 BextExitOnFailure(hr, "Failed to run: %ls", sczCommandLine);
54
55 cbOutput = 64;
56
57 rgbOutput = static_cast<BYTE*>(MemAlloc(cbOutput, TRUE));
58 BextExitOnNull(rgbOutput, hr, E_OUTOFMEMORY, "Failed to alloc output string.");
59
60 while (::ReadFile(hStdOutErr, rgbOutput + cbTotalRead, cbOutput - cbTotalRead, &cbRead, NULL))
61 {
62 cbTotalRead += cbRead;
63
64 if (cbTotalRead == cbOutput)
65 {
66 cbOutput *= 2;
67
68 const LPVOID pvNew = MemReAlloc(rgbOutput, cbOutput, TRUE);
69 BextExitOnNull(pvNew, hr, E_OUTOFMEMORY, "Failed to realloc output string.");
70
71 rgbOutput = static_cast<BYTE*>(pvNew);
72 }
73 }
74
75 if (ERROR_BROKEN_PIPE != ::GetLastError())
76 {
77 BextExitWithLastError(hr, "Failed to read netcoresearch.exe output.");
78 }
79
80 hr = ProcWaitForCompletion(hProcess, INFINITE, &dwExitCode);
81 BextExitOnFailure(hr, "Failed to wait for netcoresearch.exe to exit.");
82
83 if (0 != dwExitCode)
84 {
85 BextExitWithRootFailure(hr, E_UNEXPECTED, "netcoresearch.exe failed with exit code: 0x%x\r\nOutput:\r\n%hs", dwExitCode, rgbOutput);
86 }
87
88 if (*rgbOutput)
89 {
90 hr = StrAllocStringAnsi(psczLatestVersion, reinterpret_cast<LPSTR>(rgbOutput), 0, CP_UTF8);
91 BextExitOnFailure(hr, "Failed to widen output string: %hs", rgbOutput);
92 }
93 20
94LExit: 21LExit:
95 ReleaseFileHandle(hStdOutErr); 22 ReleaseStr(sczArguments);
96 ReleaseHandle(hProcess);
97 ReleaseMem(rgbOutput);
98 ReleaseStr(sczCommandLine);
99 ReleaseStr(sczExePath);
100 23
101 return hr; 24 return hr;
102} 25}
@@ -109,14 +32,16 @@ HRESULT NetfxPerformDetectNetCoreSdk(
109 ) 32 )
110{ 33{
111 HRESULT hr = S_OK; 34 HRESULT hr = S_OK;
112 LPWSTR sczLatestVersion = nullptr; 35 LPWSTR sczLatestVersion = NULL;
113 const auto& searchParams = pSearch->NetCoreSdkSearch; 36
114 hr = DetectNetCoreSdk(searchParams.platform, searchParams.sczVersion, wzBaseDirectory, &sczLatestVersion); 37 hr = DetectNetCoreSdk(pSearch->NetCoreSdkSearch.platform, pSearch->NetCoreSdkSearch.sczMajorVersion, wzBaseDirectory, &sczLatestVersion);
115 BextExitOnFailure(hr, "DetectNetCoreSdk failed."); 38 BextExitOnFailure(hr, "DetectNetCoreSdk failed.");
116 39
117 hr = pEngine->SetVariableVersion(wzVariable, sczLatestVersion); 40 hr = pEngine->SetVariableVersion(wzVariable, sczLatestVersion);
118 BextExitOnFailure(hr, "Failed to set variable '%ls' to '%ls'", wzVariable, sczLatestVersion); 41 BextExitOnFailure(hr, "Failed to set variable '%ls' to '%ls'", wzVariable, sczLatestVersion);
119 42
120LExit: 43LExit:
44 ReleaseStr(sczLatestVersion);
45
121 return hr; 46 return hr;
122} 47}