aboutsummaryrefslogtreecommitdiff
path: root/src/ext/NetFx/netcoresearch
diff options
context:
space:
mode:
Diffstat (limited to 'src/ext/NetFx/netcoresearch')
-rw-r--r--src/ext/NetFx/netcoresearch/netcoresearch.cpp158
-rw-r--r--src/ext/NetFx/netcoresearch/netcoresearch.h10
-rw-r--r--src/ext/NetFx/netcoresearch/netcoresearch.vcxproj73
-rw-r--r--src/ext/NetFx/netcoresearch/packages.config9
-rw-r--r--src/ext/NetFx/netcoresearch/precomp.cpp3
-rw-r--r--src/ext/NetFx/netcoresearch/precomp.h18
6 files changed, 271 insertions, 0 deletions
diff --git a/src/ext/NetFx/netcoresearch/netcoresearch.cpp b/src/ext/NetFx/netcoresearch/netcoresearch.cpp
new file mode 100644
index 00000000..b2dad9a8
--- /dev/null
+++ b/src/ext/NetFx/netcoresearch/netcoresearch.cpp
@@ -0,0 +1,158 @@
1// 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.
2
3#include "precomp.h"
4
5struct NETCORESEARCH_STATE
6{
7 LPCWSTR wzTargetName;
8 DWORD dwMajorVersion;
9 VERUTIL_VERSION* pVersion;
10};
11
12static HRESULT GetDotnetEnvironmentInfo(
13 __in DWORD dwMajorVersion,
14 __in_z LPCWSTR wzTargetName,
15 __inout VERUTIL_VERSION** ppVersion
16 );
17static void HOSTFXR_CALLTYPE GetDotnetEnvironmentInfoResult(
18 __in const hostfxr_dotnet_environment_info* pInfo,
19 __in LPVOID pvContext
20 );
21
22int __cdecl wmain(int argc, LPWSTR argv[])
23{
24 HRESULT hr = S_OK;
25 DWORD dwMajorVersion = 0;
26 VERUTIL_VERSION* pVersion = NULL;
27 LPSTR pszVersion = NULL;
28
29 ::SetConsoleCP(CP_UTF8);
30
31 ConsoleInitialize();
32
33 if (argc != 3)
34 {
35 ExitFunction1(hr = E_INVALIDARG);
36 }
37
38 hr = StrStringToUInt32(argv[1], 0, reinterpret_cast<UINT*>(&dwMajorVersion));
39 ConsoleExitOnFailure(hr, CONSOLE_COLOR_RED, "Failed to get target version from: %ls", argv[1]);
40
41 hr = GetDotnetEnvironmentInfo(dwMajorVersion, argv[2], &pVersion);
42 ConsoleExitOnFailure(hr, CONSOLE_COLOR_RED, "Failed to search for .NET Core.");
43
44 if (pVersion)
45 {
46 hr = StrAnsiAllocString(&pszVersion, pVersion->sczVersion, 0, CP_UTF8);
47 ConsoleExitOnFailure(hr, CONSOLE_COLOR_RED, "Failed to convert version to UTF-8.");
48
49 ConsoleWrite(CONSOLE_COLOR_NORMAL, "%hs", pszVersion);
50 }
51
52LExit:
53 ReleaseStr(pszVersion);
54 ReleaseVerutilVersion(pVersion);
55 ConsoleUninitialize();
56 return hr;
57}
58
59static HRESULT GetDotnetEnvironmentInfo(
60 __in DWORD dwMajorVersion,
61 __in_z LPCWSTR wzTargetName,
62 __inout VERUTIL_VERSION** ppVersion
63 )
64{
65 HRESULT hr = S_OK;
66 LPWSTR sczProcessPath = NULL;
67 LPWSTR sczHostfxrPath = NULL;
68 HMODULE hModule = NULL;
69 hostfxr_get_dotnet_environment_info_fn pfnGetDotnetEnvironmentInfo = NULL;
70 NETCORESEARCH_STATE state = { };
71
72 state.dwMajorVersion = dwMajorVersion;
73 state.wzTargetName = wzTargetName;
74
75 hr = PathForCurrentProcess(&sczProcessPath, NULL);
76 ConsoleExitOnFailure(hr, CONSOLE_COLOR_RED, "Failed to get process path.");
77
78 hr = PathGetDirectory(sczProcessPath, &sczHostfxrPath);
79 ConsoleExitOnFailure(hr, CONSOLE_COLOR_RED, "Failed to get process directory.");
80
81 hr = StrAllocConcat(&sczHostfxrPath, L"hostfxr.dll", 0);
82 ConsoleExitOnFailure(hr, CONSOLE_COLOR_RED, "Failed to build hostfxr path.");
83
84 hModule = ::LoadLibraryExW(sczHostfxrPath, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
85 ConsoleExitOnNullWithLastError(hModule, hr, CONSOLE_COLOR_RED, "Failed to load hostfxr.");
86
87 pfnGetDotnetEnvironmentInfo = (hostfxr_get_dotnet_environment_info_fn)::GetProcAddress(hModule, "hostfxr_get_dotnet_environment_info");
88 ConsoleExitOnNullWithLastError(pfnGetDotnetEnvironmentInfo, hr, CONSOLE_COLOR_RED, "Failed to get address for hostfxr_get_dotnet_environment_info.");
89
90 hr = pfnGetDotnetEnvironmentInfo(NULL, NULL, GetDotnetEnvironmentInfoResult, &state);
91 ConsoleExitOnFailure(hr, CONSOLE_COLOR_RED, "Failed to get .NET Core environment info.");
92
93 if (state.pVersion)
94 {
95 *ppVersion = state.pVersion;
96 state.pVersion = NULL;
97 }
98
99LExit:
100 ReleaseVerutilVersion(state.pVersion);
101 ReleaseStr(sczHostfxrPath);
102 ReleaseStr(sczProcessPath);
103
104 if (hModule)
105 {
106 ::FreeLibrary(hModule);
107 }
108
109 return hr;
110}
111
112static void HOSTFXR_CALLTYPE GetDotnetEnvironmentInfoResult(
113 __in const hostfxr_dotnet_environment_info* pInfo,
114 __in LPVOID pvContext
115 )
116{
117 NETCORESEARCH_STATE* pState = reinterpret_cast<NETCORESEARCH_STATE*>(pvContext);
118 HRESULT hr = S_OK;
119 VERUTIL_VERSION* pFrameworkVersion = NULL;
120 int nCompare = 0;
121
122 for (size_t i = 0; i < pInfo->framework_count; ++i)
123 {
124 const hostfxr_dotnet_environment_framework_info* pFrameworkInfo = pInfo->frameworks + i;
125 ReleaseVerutilVersion(pFrameworkVersion);
126
127 if (CSTR_EQUAL != ::CompareStringW(LOCALE_INVARIANT, NORM_IGNORECASE, pState->wzTargetName, -1, pFrameworkInfo->name, -1))
128 {
129 continue;
130 }
131
132 hr = VerParseVersion(pFrameworkInfo->version, 0, FALSE, &pFrameworkVersion);
133 ConsoleExitOnFailure(hr, CONSOLE_COLOR_RED, "Failed to parse framework version: %ls", pFrameworkInfo->version);
134
135 if (pFrameworkVersion->dwMajor != pState->dwMajorVersion)
136 {
137 continue;
138 }
139
140 if (pState->pVersion)
141 {
142 hr = VerCompareParsedVersions(pState->pVersion, pFrameworkVersion, &nCompare);
143 ConsoleExitOnFailure(hr, CONSOLE_COLOR_RED, "Failed to compare versions.");
144
145 if (nCompare > -1)
146 {
147 continue;
148 }
149 }
150
151 ReleaseVerutilVersion(pState->pVersion);
152 pState->pVersion = pFrameworkVersion;
153 pFrameworkVersion = NULL;
154 }
155
156LExit:
157 ReleaseVerutilVersion(pFrameworkVersion);
158}
diff --git a/src/ext/NetFx/netcoresearch/netcoresearch.h b/src/ext/NetFx/netcoresearch/netcoresearch.h
new file mode 100644
index 00000000..d25b13f9
--- /dev/null
+++ b/src/ext/NetFx/netcoresearch/netcoresearch.h
@@ -0,0 +1,10 @@
1#pragma once
2// 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.
3
4// TODO: remove when definition is provided in hostfxr.h.
5typedef int32_t(HOSTFXR_CALLTYPE* hostfxr_get_dotnet_environment_info_fn)(
6 const char_t* dotnet_root,
7 void* reserved,
8 hostfxr_get_dotnet_environment_info_result_fn result,
9 void* result_context
10 );
diff --git a/src/ext/NetFx/netcoresearch/netcoresearch.vcxproj b/src/ext/NetFx/netcoresearch/netcoresearch.vcxproj
new file mode 100644
index 00000000..b5f12f07
--- /dev/null
+++ b/src/ext/NetFx/netcoresearch/netcoresearch.vcxproj
@@ -0,0 +1,73 @@
1<?xml version="1.0" encoding="utf-8"?>
2<!-- 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. -->
3<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
4 <ItemGroup Label="ProjectConfigurations">
5 <ProjectConfiguration Include="Debug|ARM64">
6 <Configuration>Debug</Configuration>
7 <Platform>ARM64</Platform>
8 </ProjectConfiguration>
9 <ProjectConfiguration Include="Release|ARM64">
10 <Configuration>Release</Configuration>
11 <Platform>ARM64</Platform>
12 </ProjectConfiguration>
13 <ProjectConfiguration Include="Debug|Win32">
14 <Configuration>Debug</Configuration>
15 <Platform>Win32</Platform>
16 </ProjectConfiguration>
17 <ProjectConfiguration Include="Release|Win32">
18 <Configuration>Release</Configuration>
19 <Platform>Win32</Platform>
20 </ProjectConfiguration>
21 <ProjectConfiguration Include="Debug|x64">
22 <Configuration>Debug</Configuration>
23 <Platform>x64</Platform>
24 </ProjectConfiguration>
25 <ProjectConfiguration Include="Release|x64">
26 <Configuration>Release</Configuration>
27 <Platform>x64</Platform>
28 </ProjectConfiguration>
29 </ItemGroup>
30
31 <PropertyGroup Label="Globals">
32 <ProjectGuid>{A7FD9EF2-68CF-4C8E-AD81-3E8A6C7E1937}</ProjectGuid>
33 <ConfigurationType>Application</ConfigurationType>
34 <CharacterSet>Unicode</CharacterSet>
35 <ProjectSubSystem>Console</ProjectSubSystem>
36 <TargetName>netcoresearch</TargetName>
37 </PropertyGroup>
38
39 <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
40 <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
41
42 <PropertyGroup>
43 <NetHostPlatform>$(Platform)</NetHostPlatform>
44 <NetHostPlatform Condition=" '$(NetHostPlatform)'=='Win32' ">x86</NetHostPlatform>
45 <NetHostPath>..\..\..\..\packages\runtime.win-$(NetHostPlatform).Microsoft.NETCore.DotNetAppHost.6.0.4\runtimes\win-$(NetHostPlatform)\native\</NetHostPath>
46 <HostfxrPath>..\..\..\..\packages\runtime.win-$(NetHostPlatform).Microsoft.NETCore.DotNetHostResolver.6.0.4\runtimes\win-$(NetHostPlatform)\native\</HostfxrPath>
47 <ProjectAdditionalIncludeDirectories>$(NetHostPath)</ProjectAdditionalIncludeDirectories>
48 </PropertyGroup>
49
50 <ItemGroup>
51 <ClCompile Include="netcoresearch.cpp" />
52 <ClCompile Include="precomp.cpp">
53 <PrecompiledHeader>Create</PrecompiledHeader>
54 </ClCompile>
55 </ItemGroup>
56
57 <ItemGroup>
58 <ClInclude Include="netcoresearch.h" />
59 <ClInclude Include="precomp.h" />
60 </ItemGroup>
61
62 <ItemGroup>
63 <PackageReference Include="WixToolset.Dutil" />
64 <PackageReference Include="Microsoft.SourceLink.GitHub" PrivateAssets="All" />
65 <PackageReference Include="GitInfo" PrivateAssets="All" />
66 </ItemGroup>
67
68 <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
69
70 <Target Name="CopyHostfxr" AfterTargets="Build">
71 <Copy SourceFiles="$(HostfxrPath)hostfxr.dll" DestinationFolder="$(OutputPath)" />
72 </Target>
73</Project>
diff --git a/src/ext/NetFx/netcoresearch/packages.config b/src/ext/NetFx/netcoresearch/packages.config
new file mode 100644
index 00000000..ad26db1c
--- /dev/null
+++ b/src/ext/NetFx/netcoresearch/packages.config
@@ -0,0 +1,9 @@
1<?xml version="1.0" encoding="utf-8"?>
2<packages>
3 <package id="runtime.win-arm64.Microsoft.NETCore.DotNetAppHost" version="6.0.4" targetFramework="native" />
4 <package id="runtime.win-x64.Microsoft.NETCore.DotNetAppHost" version="6.0.4" targetFramework="native" />
5 <package id="runtime.win-x86.Microsoft.NETCore.DotNetAppHost" version="6.0.4" targetFramework="native" />
6 <package id="runtime.win-arm64.Microsoft.NETCore.DotNetHostResolver" version="6.0.4" targetFramework="native" />
7 <package id="runtime.win-x64.Microsoft.NETCore.DotNetHostResolver" version="6.0.4" targetFramework="native" />
8 <package id="runtime.win-x86.Microsoft.NETCore.DotNetHostResolver" version="6.0.4" targetFramework="native" />
9</packages> \ No newline at end of file
diff --git a/src/ext/NetFx/netcoresearch/precomp.cpp b/src/ext/NetFx/netcoresearch/precomp.cpp
new file mode 100644
index 00000000..37664a1c
--- /dev/null
+++ b/src/ext/NetFx/netcoresearch/precomp.cpp
@@ -0,0 +1,3 @@
1// 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.
2
3#include "precomp.h"
diff --git a/src/ext/NetFx/netcoresearch/precomp.h b/src/ext/NetFx/netcoresearch/precomp.h
new file mode 100644
index 00000000..24023ec2
--- /dev/null
+++ b/src/ext/NetFx/netcoresearch/precomp.h
@@ -0,0 +1,18 @@
1#pragma once
2// 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.
3
4#include <windows.h>
5#include <msiquery.h>
6#include <corerror.h>
7
8#include <dutil.h>
9#include <conutil.h>
10#include <memutil.h>
11#include <pathutil.h>
12#include <strutil.h>
13#include <verutil.h>
14#include <xmlutil.h>
15
16#include <hostfxr.h>
17
18#include "netcoresearch.h"