Skip to content

Commit 6d017b0

Browse files
committed
Changes relating to post 5
1 parent 303d9cd commit 6d017b0

16 files changed

+833
-181
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup Label="ProjectConfigurations">
4+
<ProjectConfiguration Include="Debug|Win32">
5+
<Configuration>Debug</Configuration>
6+
<Platform>Win32</Platform>
7+
</ProjectConfiguration>
8+
<ProjectConfiguration Include="Release|Win32">
9+
<Configuration>Release</Configuration>
10+
<Platform>Win32</Platform>
11+
</ProjectConfiguration>
12+
</ItemGroup>
13+
<PropertyGroup Label="Globals">
14+
<ProjectGuid>{5B3B7E22-740F-4E87-B9C8-14BBB2ACEA88}</ProjectGuid>
15+
<Keyword>Win32Proj</Keyword>
16+
<RootNamespace>NativeUnitTests</RootNamespace>
17+
</PropertyGroup>
18+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
19+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
20+
<ConfigurationType>DynamicLibrary</ConfigurationType>
21+
<UseDebugLibraries>true</UseDebugLibraries>
22+
<PlatformToolset>v120</PlatformToolset>
23+
<CharacterSet>Unicode</CharacterSet>
24+
<UseOfMfc>false</UseOfMfc>
25+
</PropertyGroup>
26+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
27+
<ConfigurationType>DynamicLibrary</ConfigurationType>
28+
<UseDebugLibraries>false</UseDebugLibraries>
29+
<PlatformToolset>v120</PlatformToolset>
30+
<WholeProgramOptimization>true</WholeProgramOptimization>
31+
<CharacterSet>Unicode</CharacterSet>
32+
<UseOfMfc>false</UseOfMfc>
33+
</PropertyGroup>
34+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
35+
<ImportGroup Label="ExtensionSettings">
36+
</ImportGroup>
37+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
38+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
39+
</ImportGroup>
40+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
41+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
42+
</ImportGroup>
43+
<PropertyGroup Label="UserMacros" />
44+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
45+
<LinkIncremental>true</LinkIncremental>
46+
<LibraryPath>$(OutDir);$(LibraryPath)</LibraryPath>
47+
</PropertyGroup>
48+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
49+
<LinkIncremental>true</LinkIncremental>
50+
</PropertyGroup>
51+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
52+
<ClCompile>
53+
<PrecompiledHeader>Use</PrecompiledHeader>
54+
<WarningLevel>Level3</WarningLevel>
55+
<Optimization>Disabled</Optimization>
56+
<AdditionalIncludeDirectories>$(VCInstallDir)UnitTest\include;$(OutDir);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
57+
<PreprocessorDefinitions>WIN32;_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
58+
<UseFullPaths>true</UseFullPaths>
59+
</ClCompile>
60+
<Link>
61+
<SubSystem>Windows</SubSystem>
62+
<GenerateDebugInformation>true</GenerateDebugInformation>
63+
<AdditionalLibraryDirectories>$(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
64+
<AdditionalDependencies>clr_host.lib;%(AdditionalDependencies)</AdditionalDependencies>
65+
</Link>
66+
</ItemDefinitionGroup>
67+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
68+
<ClCompile>
69+
<WarningLevel>Level3</WarningLevel>
70+
<PrecompiledHeader>Use</PrecompiledHeader>
71+
<Optimization>MaxSpeed</Optimization>
72+
<FunctionLevelLinking>true</FunctionLevelLinking>
73+
<IntrinsicFunctions>true</IntrinsicFunctions>
74+
<AdditionalIncludeDirectories>$(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
75+
<PreprocessorDefinitions>WIN32;NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
76+
<UseFullPaths>true</UseFullPaths>
77+
</ClCompile>
78+
<Link>
79+
<SubSystem>Windows</SubSystem>
80+
<GenerateDebugInformation>true</GenerateDebugInformation>
81+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
82+
<OptimizeReferences>true</OptimizeReferences>
83+
<AdditionalLibraryDirectories>$(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
84+
</Link>
85+
</ItemDefinitionGroup>
86+
<ItemGroup>
87+
<ClInclude Include="stdafx.h" />
88+
<ClInclude Include="targetver.h" />
89+
</ItemGroup>
90+
<ItemGroup>
91+
<ClCompile Include="stdafx.cpp">
92+
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
93+
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
94+
</ClCompile>
95+
<ClCompile Include="unittest1.cpp" />
96+
</ItemGroup>
97+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
98+
<ImportGroup Label="ExtensionTargets">
99+
</ImportGroup>
100+
</Project>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup>
4+
<Filter Include="Source Files">
5+
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
6+
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
7+
</Filter>
8+
<Filter Include="Header Files">
9+
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
10+
<Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions>
11+
</Filter>
12+
<Filter Include="Resource Files">
13+
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
14+
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
15+
</Filter>
16+
</ItemGroup>
17+
<ItemGroup>
18+
<ClInclude Include="stdafx.h">
19+
<Filter>Header Files</Filter>
20+
</ClInclude>
21+
<ClInclude Include="targetver.h">
22+
<Filter>Header Files</Filter>
23+
</ClInclude>
24+
</ItemGroup>
25+
<ItemGroup>
26+
<ClCompile Include="stdafx.cpp">
27+
<Filter>Source Files</Filter>
28+
</ClCompile>
29+
<ClCompile Include="unittest1.cpp">
30+
<Filter>Source Files</Filter>
31+
</ClCompile>
32+
</ItemGroup>
33+
</Project>

NativeUnitTests/stdafx.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// stdafx.cpp : source file that includes just the standard includes
2+
// NativeUnitTests.pch will be the pre-compiled header
3+
// stdafx.obj will contain the pre-compiled type information
4+
5+
#include "stdafx.h"
6+
7+
// TODO: reference any additional headers you need in STDAFX.H
8+
// and not in this file

NativeUnitTests/stdafx.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// stdafx.h : include file for standard system include files,
2+
// or project specific include files that are used frequently, but
3+
// are changed infrequently
4+
//
5+
6+
#pragma once
7+
8+
#include "targetver.h"
9+
10+
// Headers for CppUnitTest
11+
#include "CppUnitTest.h"
12+
13+
// TODO: reference additional headers your program requires here

NativeUnitTests/targetver.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#pragma once
2+
3+
// Including SDKDDKVer.h defines the highest available Windows platform.
4+
5+
// If you wish to build your application for a previous Windows platform, include WinSDKVer.h and
6+
// set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h.
7+
8+
#include <SDKDDKVer.h>

NativeUnitTests/unittest1.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "stdafx.h"
2+
#include "CppUnitTest.h"
3+
#include "..\clr_host\ClrHost.h"
4+
#pragma comment(lib, "clr_host.lib")
5+
6+
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
7+
8+
namespace NativeUnitTests
9+
{
10+
TEST_CLASS(UnitTest1)
11+
{
12+
public:
13+
14+
TEST_METHOD(TestMethod1)
15+
{
16+
// TODO: Your test code here
17+
IUnmanagedHostPtr pClrHost;
18+
HRESULT hrBind = CClrHost::BindToRuntime(&pClrHost.GetInterfacePtr());
19+
Assert::AreSame<HRESULT>(S_OK, hrBind);
20+
}
21+
22+
};
23+
}

clr_host/ClrHost.cpp

Lines changed: 68 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,16 @@ CClrHost::~CClrHost()
4242
#pragma warning( disable : 4996 )
4343
HRESULT CClrHost::FinalConstruct()
4444
{
45-
//load the CLR into the process
46-
/*return CorBindToRuntimeEx(NULL,
47-
NULL,
48-
0,
49-
CLSID_CLRRuntimeHost,
50-
IID_ICLRRuntimeHost,
51-
reinterpret_cast<LPVOID *>(&m_pClr));*/
5245

5346
//load the CLR into the process
47+
// First we get an instance of the MetaHost
5448
ICLRMetaHost *pMetaHost = NULL;
5549
ICLRMetaHostPolicy *pMetaHostPolicy = NULL;
5650
ICLRDebugging *pCLRDebugging = NULL;
5751
HRESULT hr;
5852
hr = CLRCreateInstance(CLSID_CLRMetaHost, IID_ICLRMetaHost,
5953
(LPVOID*)&pMetaHost);
54+
6055
/*hr = CLRCreateInstance(CLSID_CLRMetaHostPolicy, IID_ICLRMetaHostPolicy,
6156
(LPVOID*)&pMetaHostPolicy);*/
6257
/*hr = CLRCreateInstance(CLSID_CLRDebugging, IID_ICLRDebugging,
@@ -72,6 +67,10 @@ HRESULT CClrHost::FinalConstruct()
7267
WCHAR strName[128];
7368
DWORD len = 128;
7469

70+
// If for whatever reason we end up here, we need to check to see what Runtimes are loaded
71+
// This will set the default runtime as the last CLR to be loaded.
72+
// At the time of writing this application it is 4.5 (4.0)
73+
7574
while ((hr = pRtEnum->Next(1, (IUnknown **)&info, &fetched)) == S_OK && fetched > 0)
7675
{
7776
ZeroMemory(strName, sizeof(strName));
@@ -81,12 +80,41 @@ HRESULT CClrHost::FinalConstruct()
8180
reinterpret_cast<LPVOID *>(&m_pClr));
8281
if (!SUCCEEDED(hr))
8382
printf("hr failed....");
84-
runtimesLoaded = true;
83+
84+
HRESULT hrClrControl = m_pClr->GetCLRControl(&m_pClrControl);
85+
if (FAILED(hrClrControl))
86+
return hrClrControl;
87+
88+
// set ourselves up as the host control
89+
HRESULT hrHostControl = m_pClr->SetHostControl(static_cast<IHostControl *>(this));
90+
if (FAILED(hrHostControl))
91+
return hrHostControl;
92+
93+
// get the host protection manager
94+
ICLRHostProtectionManager *pHostProtectionManager = NULL;
95+
HRESULT hrGetProtectionManager = m_pClrControl->GetCLRManager(
96+
IID_ICLRHostProtectionManager,
97+
reinterpret_cast<void **>(&pHostProtectionManager));
98+
if (FAILED(hrGetProtectionManager))
99+
return hrGetProtectionManager;
100+
101+
// setup host proctection
102+
HRESULT hrHostProtection = pHostProtectionManager->SetProtectedCategories(
103+
(EApiCategories)(eSynchronization | eSelfAffectingThreading));
104+
pHostProtectionManager->Release();
105+
106+
if (FAILED(hrHostProtection))
107+
return hrHostProtection;
108+
85109
this->m_lastCLR.assign(strName);
110+
runtimesLoaded = true;
86111
}
87112
pRtEnum->Release();
88113
pRtEnum = NULL;
89114

115+
// If no runtimes are loaded we will make sure to load them all.
116+
// This will set the default runtime as the last CLR to be loaded.
117+
// At the time of writing this application it is 4.5 (4.0)
90118
if (!runtimesLoaded)
91119
{
92120
pMetaHost->EnumerateInstalledRuntimes(&pRtEnum);
@@ -101,6 +129,36 @@ HRESULT CClrHost::FinalConstruct()
101129
if (!SUCCEEDED(hr))
102130
printf("hr failed....");
103131
m_CLRRuntimeMap[std::wstring(strName)] = m_pClr;
132+
133+
134+
HRESULT hrClrControl = m_pClr->GetCLRControl(&m_pClrControl);
135+
if (FAILED(hrClrControl))
136+
return hrClrControl;
137+
138+
// set ourselves up as the host control
139+
HRESULT hrHostControl = m_pClr->SetHostControl(static_cast<IHostControl *>(this));
140+
if (FAILED(hrHostControl))
141+
142+
return hrHostControl;
143+
// get the host protection manager
144+
ICLRHostProtectionManager *pHostProtectionManager = NULL;
145+
HRESULT hrGetProtectionManager = m_pClrControl->GetCLRManager(
146+
IID_ICLRHostProtectionManager,
147+
reinterpret_cast<void **>(&pHostProtectionManager));
148+
if (FAILED(hrGetProtectionManager))
149+
return hrGetProtectionManager;
150+
151+
// setup host proctection to disallow any threading from partially trusted code.
152+
// Why? well, if a thread is allowed to hang indefinitely the command could get stuck.
153+
HRESULT hrHostProtection = pHostProtectionManager->SetProtectedCategories(
154+
(EApiCategories)(eSynchronization | eSelfAffectingThreading | eSelfAffectingProcessMgmt
155+
| eExternalProcessMgmt | eExternalThreading | eUI));
156+
pHostProtectionManager->Release();
157+
158+
if (FAILED(hrHostProtection))
159+
return hrHostProtection;
160+
161+
104162
this->m_lastCLR.assign(strName);
105163
}
106164
pRtEnum->Release();
@@ -139,13 +197,6 @@ STDMETHODIMP CClrHost::GetHostManager(const IID &RIID, __deref_opt_out_opt void
139197
if (ppvObject == NULL)
140198
return E_POINTER;
141199

142-
if (RIID == IID_IHostGCManager)
143-
{
144-
*ppvObject = (IHostGCManager*)this;
145-
return S_OK;
146-
}
147-
148-
149200
*ppvObject = NULL;
150201
return E_NOINTERFACE;
151202
}
@@ -319,6 +370,8 @@ STDMETHODIMP CClrHost::ThreadIsBlockingForSuspension(){ return S_OK; }
319370
STDMETHODIMP CClrHost::raw_CreateAppDomainForQuery(BSTR FnName, BSTR *pRetVal)
320371
{
321372
IManagedHostPtr pAppMgr = this->GetDefaultManagedHost();
373+
auto clrVersion = pAppMgr->GetAssemblyCLRVersion(FnName);
374+
322375
IManagedHostPtr pNewDomain = pAppMgr->CreateAppDomain(FnName);
323376
*pRetVal = (BSTR)pNewDomain->GetAppDomainName;
324377
this->m_NewlyCreatedAppDomains[std::wstring(*pRetVal)] = pNewDomain;

clr_host/ClrHost.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class ATL_NO_VTABLE CClrHost : public CComObjectRootEx<CComSingleThreadModel>,
1818
COM_INTERFACE_ENTRY(IHostGCManager)
1919
COM_INTERFACE_ENTRY(IUnmanagedHost)
2020
END_COM_MAP()
21+
DECLARE_PROTECT_FINAL_CONSTRUCT()
2122

2223
private:
2324
typedef map<const std::wstring, IManagedHost *> AppDomainManagerMap;

clr_host/stdafx.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <iostream>
2828
#include <map>
2929
#include <string>
30+
#include <sstream>
3031
#include <atlbase.h>
3132
#include <atlcom.h>
3233
#include <comdef.h>

0 commit comments

Comments
 (0)