Big*_*gmo 4 .net c++ reflection clr .net-assembly
由于文档丰富,我设法在非托管程序中托管 CLR。但是,当托管 CLR 时,似乎只能从硬盘驱动器加载程序集 - 当运行托管应用程序时,可以通过调用 Assembly.Load() 从内存加载程序集。
有没有办法从内存中执行托管 CLR 中的程序集?喜欢:
我已经在网络和 MSDN 上搜索了几个小时,但找不到解决此问题的方法!我想出的解决方法将涉及另一个调用 Assembly.Load() 的程序集 - 但是我担心这可能有点矫枉过正。
在此先感谢您的任何提示或提示!
我建议您从这里的示例开始:C++ 应用程序托管 CLR 4 并调用 .NET 程序集 (CppHostCLR),它似乎几乎可以满足您的需求。唯一缺少的部分是它不是从内存加载程序集,而是使用文件。
所以你需要做的只是替换以下几行(在 RuntimeHostV4.cpp 中):
// Load the .NET assembly.
wprintf(L"Load the assembly %s\n", pszAssemblyName);
hr = spDefaultAppDomain->Load_2(bstrAssemblyName, &spAssembly);
if (FAILED(hr))
{
wprintf(L"Failed to load the assembly w/hr 0x%08lx\n", hr);
goto Cleanup;
}
Run Code Online (Sandbox Code Playgroud)
通过以下使用此方法的行:_AppDomain.Load Method (Byte[])
// let's suppose I have a LPBYTE (pointer to byte array) and an ULONG (int32) value
// that describe the buffer that contains an assembly bytes.
LPBYTE buffer = <my buffer>;
ULONG size = <my buffer size>;
// let's create an OLEAUT's SAFEARRAY of BYTEs and copy the buffer into it
// TODO: add some error checking here (mostly for out of memory errors)
SAFEARRAYBOUND bounds = { size, 0 };
SAFEARRAY *psa = SafeArrayCreate(VT_UI1, 1, &bounds);
void* data;
SafeArrayAccessData(psa, &data);
CopyMemory(data, buffer, size);
SafeArrayUnaccessData(psa);
hr = spDefaultAppDomain->Load_3(psa, &spAssembly);
if (FAILED(hr))
{
wprintf(L"Failed to load the assembly w/hr 0x%08lx\n", hr);
goto Cleanup;
}
SafeArrayDestroy(psa); // don't forget to destroy
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1904 次 |
| 最近记录: |