-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
ComActivation.cpp
96 lines (74 loc) · 2.61 KB
/
ComActivation.cpp
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#include "CoreShim.h"
#include <vector>
namespace
{
HRESULT InitializeCoreClr(_In_ coreclr* inst)
{
assert(inst != nullptr);
HRESULT hr;
std::string tpaList;
RETURN_IF_FAILED(coreclr::CreateTpaList(tpaList));
const char *keys[] =
{
"APP_PATHS",
"TRUSTED_PLATFORM_ASSEMBLIES",
};
std::string assemblyPath;
RETURN_IF_FAILED(Utility::GetCoreShimDirectory(assemblyPath));
const char *values[] =
{
assemblyPath.c_str(),
tpaList.c_str(),
};
static_assert(ARRAY_SIZE(keys) == ARRAY_SIZE(values), "key/values pairs should match in length");
return inst->Initialize(ARRAY_SIZE(keys), keys, values, "COMAct");
}
}
STDAPI DllGetClassObject(
_In_ REFCLSID rclsid,
_In_ REFIID riid,
_Outptr_ LPVOID FAR* ppv)
{
HRESULT hr;
coreclr *inst;
RETURN_IF_FAILED(coreclr::GetCoreClrInstance(&inst));
if (hr == S_OK)
RETURN_IF_FAILED(InitializeCoreClr(inst));
using GetClassFactoryForTypeInternal_ptr = HRESULT(*)(void *);
GetClassFactoryForTypeInternal_ptr GetClassFactoryForTypeInternal;
RETURN_IF_FAILED(inst->CreateDelegate(
"System.Private.CoreLib",
"Internal.Runtime.InteropServices.ComActivator",
"GetClassFactoryForTypeInternal", (void**)&GetClassFactoryForTypeInternal));
// Get assembly and type for activation
std::wstring assemblyName;
RETURN_IF_FAILED(Utility::TryGetEnvVar(COMACT_ASSEMBLYNAME_ENVVAR, assemblyName));
std::wstring typeName;
RETURN_IF_FAILED(Utility::TryGetEnvVar(COMACT_TYPENAME_ENVVAR, typeName));
// Compute the path to the assembly. This should be adjacent to CoreShim (i.e. this library).
std::wstring assemblyPath;
RETURN_IF_FAILED(Utility::GetCoreShimDirectory(assemblyPath));
assemblyPath.append(assemblyName);
assemblyPath.append(W(".dll"));
IUnknown *ccw = nullptr;
struct ComActivationContext
{
GUID ClassId;
GUID InterfaceId;
const WCHAR *AssemblyPath;
const WCHAR *AssemblyName;
const WCHAR *TypeName;
void **ClassFactoryDest;
} comCxt{ rclsid, riid, assemblyPath.data(), assemblyName.data(), typeName.data(), (void**)&ccw };
RETURN_IF_FAILED(GetClassFactoryForTypeInternal(&comCxt));
assert(ccw != nullptr);
hr = ccw->QueryInterface(riid, ppv);
ccw->Release();
return hr;
}
STDAPI DllCanUnloadNow(void)
{
return S_FALSE;
}