-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
156 lines (126 loc) · 4.52 KB
/
main.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
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
#include <windows.h>
#include <MSCorEE.h>
#include <MetaHost.h>
#include <assert.h>
#include <string>
#include <iostream>
#pragma comment(lib, "mscoree.lib")
using namespace std;
class ProfilingCLRHost : public IHostControl, public IHostGCManager
{
private:
LONG m_refCount;
LARGE_INTEGER m_lastGCStart;
LARGE_INTEGER m_frequency;
public:
ProfilingCLRHost() { QueryPerformanceFrequency(&m_frequency); }
// IHostControl
HRESULT __stdcall GetHostManager(REFIID riid, void** ppObject)
{
if(riid == IID_IHostGCManager)
{
*ppObject = static_cast<IHostGCManager*>(this);
return S_OK;
}
*ppObject = NULL;
return E_NOINTERFACE;
}
// IUnknown
HRESULT __stdcall QueryInterface(REFIID riid, void** ppvObject)
{
if (riid == IID_IHostGCManager)
{
*ppvObject = static_cast<IHostGCManager*>(this);
return S_OK;
}
*ppvObject = NULL;
return E_NOINTERFACE;
}
HRESULT __stdcall SetAppDomainManager(DWORD appDomain, IUnknown* domainManager)
{
return S_OK;
}
ULONG __stdcall AddRef() { return InterlockedIncrement(&m_refCount); }
ULONG __stdcall Release() { return InterlockedDecrement(&m_refCount); }
// IHostGCManager
HRESULT __stdcall ThreadIsBlockingForSuspension() { return S_OK; }
HRESULT __stdcall SuspensionStarting()
{
m_lastGCStart;
QueryPerformanceCounter(&m_lastGCStart);
return S_OK;
}
HRESULT __stdcall SuspensionEnding(DWORD gen)
{
LARGE_INTEGER gcEnd;
QueryPerformanceCounter(&gcEnd);
double duration = ((gcEnd.QuadPart - m_lastGCStart.QuadPart))
* 1000.0 / (double)m_frequency.QuadPart;
if (gen != UINT_MAX)
cout << "GC generation " << gen << " ended: " << duration << "ms" << endl;
else
cout << "CLR suspension ended: " << gen << " ms" << duration << endl;
return S_OK;
}
};
int wmain(int argc, wchar_t* argv[])
{
if (argc < 5)
cout << "Provide assembly_name entry_type_name enty_method_name." << endl;
assert(argv[1] && argv[2] && argv[3]);
wstring assembly(argv[1]), type(argv[2]), method(argv[3]);
//DWORD startupFlags = STARTUP_CONCURRENT_GC;
ICLRMetaHost *pMetaHost = NULL;
HRESULT hr = CLRCreateInstance(CLSID_CLRMetaHost, IID_ICLRMetaHost, (LPVOID*)&pMetaHost);
assert(SUCCEEDED(hr) && pMetaHost);
wchar_t runtimeVersion[MAX_PATH];
DWORD versionStringLength = _countof(runtimeVersion);
hr = pMetaHost->GetVersionFromFile(assembly.c_str(), runtimeVersion, &versionStringLength);
assert(SUCCEEDED(hr));
ICLRRuntimeInfo *pRuntimeInfo = NULL;
hr = pMetaHost->GetRuntime(runtimeVersion, IID_ICLRRuntimeInfo, (LPVOID*)&pRuntimeInfo);
assert(SUCCEEDED(hr) && pRuntimeInfo);
ICLRRuntimeHost* pClrRuntimeHost = NULL;
pRuntimeInfo->GetInterface(CLSID_CLRRuntimeHost, IID_ICLRRuntimeHost, (LPVOID*)&pClrRuntimeHost);
assert(SUCCEEDED(hr) && pClrRuntimeHost);
ProfilingCLRHost customTimingHost;
hr = pClrRuntimeHost->SetHostControl(&customTimingHost);
assert(SUCCEEDED(hr));
hr = pClrRuntimeHost->Start();
assert(SUCCEEDED(hr));
DWORD retcode;
hr = pClrRuntimeHost->ExecuteInDefaultAppDomain(assembly.c_str(), type.c_str(), method.c_str(), L"" , &retcode);
assert(SUCCEEDED(hr));
return 0;
};
ICLRRuntimeHost* getRuntimeHostForSpecificRuntimeVersion(ICLRMetaHost* pMetaHost, wstring runtimeVersion)
{
IEnumUnknown *pInstalledRuntimes = NULL;
HRESULT hr = pMetaHost->EnumerateInstalledRuntimes(&pInstalledRuntimes);
assert(SUCCEEDED(hr) && pInstalledRuntimes);
ICLRRuntimeHost* pCLR = NULL;
ICLRRuntimeInfo* pRuntimesArray[2] = { NULL };
const ULONG maxFetch = _countof(pRuntimesArray);
ULONG count = 0;
for (hr = pInstalledRuntimes->Next(maxFetch, (IUnknown**)pRuntimesArray, &count); SUCCEEDED(hr) && count != 0;
hr = pInstalledRuntimes->Next(maxFetch, (IUnknown**)pRuntimesArray, &count))
{
while (count)
{
--count;
wchar_t version[MAX_PATH] = { 0 };
DWORD numChars = _countof(version);
hr = pRuntimesArray[count]->GetVersionString(version, &numChars);
assert(SUCCEEDED(hr));
wcout << version << endl;
if (!pCLR && !runtimeVersion.compare(version))
{
hr = pRuntimesArray[count]->GetInterface(CLSID_CLRRuntimeHost, IID_ICLRRuntimeHost, (LPVOID*)&pCLR);
assert(SUCCEEDED(hr) && pCLR);
}
else
pRuntimesArray[count]->Release();
}
}
return pCLR;
}