#include <mscoree.h>
#include <windows.h>
#include <assert.h>
#include <iostream>
using namespace std;
#define APP_STARTUP_EXE L"TestApplication.exe"
#define APP_ENTRY_TYPE L"SomeNamespace.Program"
#define APP_ENTRY_METHOD L"Main"
class MyCLRHost : public IHostControl, public IHostGCManager
{
private:
LONG m_refCount;
LARGE_INTEGER m_lastGCStart;
LARGE_INTEGER m_frequency;
public:
MyCLRHost() { 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: "<<duration<<" ms"<<endl;
return S_OK;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
ICLRRuntimeHost* pCLR;
DWORD startupFlags = STARTUP_CONCURRENT_GC;
HRESULT hr = CorBindToRuntimeEx(L"v2.0.50727", L"wks", startupFlags,
CLSID_CLRRuntimeHost, IID_ICLRRuntimeHost, (LPVOID*)&pCLR);
assert(SUCCEEDED(hr));
MyCLRHost customHost;
hr = pCLR->SetHostControl(&customHost);
assert(SUCCEEDED(hr));
hr = pCLR->Start();
assert(SUCCEEDED(hr));
DWORD retcode;
hr = pCLR->ExecuteInDefaultAppDomain(APP_STARTUP_EXE,
APP_ENTRY_TYPE, APP_ENTRY_METHOD, L"" , &retcode);
assert(SUCCEEDED(hr));
return 0;
};