-
-
Save SkyN9ne/4867410591d207fb039b26f8aed1528d to your computer and use it in GitHub Desktop.
2 Simple C and C++ example of showing the loading of am .NET CLR via unmanaged native code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "stdafx.h" | |
int main() | |
{ | |
ICLRMetaHost *metaHost = NULL; | |
IEnumUnknown *runtime = NULL; | |
ICLRRuntimeInfo *runtimeInfo = NULL; | |
ICLRRuntimeHost *runtimeHost = NULL; | |
IUnknown *enumRuntime = NULL; | |
LPWSTR frameworkName = NULL; | |
DWORD bytes = 2048, result = 0; | |
HRESULT hr; | |
printf("CLR via native code\n"); | |
if (CLRCreateInstance(CLSID_CLRMetaHost, IID_ICLRMetaHost, (LPVOID*)&metaHost) != S_OK) { | |
printf("[x] Error: CLRCreateInstance(..)\n"); | |
return 2; | |
} | |
if (metaHost->EnumerateInstalledRuntimes(&runtime) != S_OK) { | |
printf("[x] Error: EnumerateInstalledRuntimes(..)\n"); | |
return 2; | |
} | |
frameworkName = (LPWSTR)LocalAlloc(LPTR, 2048); | |
if (frameworkName == NULL) { | |
printf("[x] Error: malloc could not allocate\n"); | |
return 2; | |
} | |
// Enumerate through runtimes and show supported frameworks | |
while (runtime->Next(1, &enumRuntime, 0) == S_OK) { | |
if (enumRuntime->QueryInterface<ICLRRuntimeInfo>(&runtimeInfo) == S_OK) { | |
if (runtimeInfo != NULL) { | |
runtimeInfo->GetVersionString(frameworkName, &bytes); | |
wprintf(L"[*] Supported Framework: %s\n", frameworkName); | |
} | |
} | |
} | |
// For demo, we just use the last supported runtime | |
if (runtimeInfo->GetInterface(CLSID_CLRRuntimeHost, IID_ICLRRuntimeHost, (LPVOID*)&runtimeHost) != S_OK) { | |
printf("[x] ..GetInterface(CLSID_CLRRuntimeHost...) failed\n"); | |
return 2; | |
} | |
if (runtimeHost == NULL || bytes == 0) { | |
wprintf(L"[*] Using runtime: %s\n", frameworkName); | |
} | |
// Start runtime, and load our assembly | |
runtimeHost->Start(); | |
printf("[*] ======= Calling .NET Code =======\n\n"); | |
if (runtimeHost->ExecuteInDefaultAppDomain( | |
L"myassembly.dll", | |
L"myassembly.Program", | |
L"test", | |
L"argtest", | |
&result | |
) != S_OK) { | |
printf("[x] Error: ExecuteInDefaultAppDomain(..) failed\n"); | |
return 2; | |
} | |
printf("[*] ======= Done =======\n"); | |
return 0; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <metahost.h> | |
#pragma comment(lib, "mscoree.lib") | |
int main() | |
{ | |
ICLRMetaHost* metaHost = NULL; | |
ICLRRuntimeInfo* runtimeInfo = NULL; | |
ICLRRuntimeHost* runtimeHost = NULL; | |
if (CLRCreateInstance(CLSID_CLRMetaHost, IID_ICLRMetaHost, (LPVOID*)&metaHost) == S_OK) | |
if (metaHost->GetRuntime(L"v4.0.30319", IID_ICLRRuntimeInfo, (LPVOID*)&runtimeInfo) == S_OK) | |
if (runtimeInfo->GetInterface(CLSID_CLRRuntimeHost, IID_ICLRRuntimeHost, (LPVOID*)&runtimeHost) == S_OK) | |
if (runtimeHost->Start() == S_OK) | |
{ | |
DWORD pReturnValue; | |
runtimeHost->ExecuteInDefaultAppDomain(L"C:\\random.dll", L"dllNamespace.dllClass", L"ShowMsg", L"It works!!", &pReturnValue); | |
runtimeInfo->Release(); | |
metaHost->Release(); | |
runtimeHost->Release(); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment