在 C++ 中加载 CLR,Start() 问题

发布于 2024-11-18 20:14:49 字数 1320 浏览 2 评论 0原文

因此,我尝试加载 .NET 4 运行时并运行我自己的 C# DLL。 Start() 方法引发 HRESULT=0x1 错误。如果我注释掉启动代码,C# DLL 将加载并执行,然后 Stop() 方法将引发 HRESULT=0x8000ffff 错误。我已经查找了几个小时,所有代码看起来都像下面的代码(我省略了所有调试/错误处理)。非常感谢您提前提供任何提示! =)

    void DotNetLoad()
    {
        ICLRRuntimeHost *pClrHost = NULL;
        ICLRMetaHost *lpMetaHost = NULL;
        MessageBox(0, L"Creating CLR instance.", L"Bootstrap Message", 0);
        HRESULT hr = CLRCreateInstance(
            CLSID_CLRMetaHost,
            IID_PPV_ARGS(&lpMetaHost));
        ICLRRuntimeInfo *lpRuntimeInfo = NULL;
        hr = lpMetaHost->GetRuntime(L"v4.0.30319",
            IID_PPV_ARGS(&lpRuntimeInfo));
        hr = lpRuntimeInfo->GetInterface(
            CLSID_CLRRuntimeHost,
            IID_ICLRRuntimeHost,
            (LPVOID *)&pClrHost);
        hr = pClrHost->Start();
        DWORD dwRet = 0;
        hr = pClrHost->ExecuteInDefaultAppDomain(
            pwzTargetDll,
            pwzNamespaceClass, pwzFunction, L"pwzArgument", &dwRet);
        hr = pClrHost->Stop();
        hr = pClrHost->Release();

    }

我了解有关解耦 init、.NET 调用和 deinit 的知识,但是应用程序启动和关闭是什么意思?现在,我从注入远程进程的 DLL 方法中调用 DotNetLoad。基本上:

extern "C" __Declspec(dllexport) void Initialize()
{
    DotNetLoad(params); //ex.
}

So I'm trying to load up the .NET 4 runtime and run my own C# DLL. The Start() method is throwing a HRESULT=0x1 error. If I comment out the start code, the C# DLL loads and executes, then the Stop() method throws a HRESULT=0x8000ffff error. I've looked for hours and all the code looks like what I have below (I left out all my debugging/error handling). Thank you very much for any tips in advance! =)

    void DotNetLoad()
    {
        ICLRRuntimeHost *pClrHost = NULL;
        ICLRMetaHost *lpMetaHost = NULL;
        MessageBox(0, L"Creating CLR instance.", L"Bootstrap Message", 0);
        HRESULT hr = CLRCreateInstance(
            CLSID_CLRMetaHost,
            IID_PPV_ARGS(&lpMetaHost));
        ICLRRuntimeInfo *lpRuntimeInfo = NULL;
        hr = lpMetaHost->GetRuntime(L"v4.0.30319",
            IID_PPV_ARGS(&lpRuntimeInfo));
        hr = lpRuntimeInfo->GetInterface(
            CLSID_CLRRuntimeHost,
            IID_ICLRRuntimeHost,
            (LPVOID *)&pClrHost);
        hr = pClrHost->Start();
        DWORD dwRet = 0;
        hr = pClrHost->ExecuteInDefaultAppDomain(
            pwzTargetDll,
            pwzNamespaceClass, pwzFunction, L"pwzArgument", &dwRet);
        hr = pClrHost->Stop();
        hr = pClrHost->Release();

    }

I understand the bit about decoupling the init, .NET call, and deinit, but what do you mean by app startup and shutdown? Right now I have DotNetLoad being called from a DLL method that is injected into a remote process. Basically:

extern "C" __Declspec(dllexport) void Initialize()
{
    DotNetLoad(params); //ex.
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木緿 2024-11-25 20:14:49

通过将运行时 init 与程序集方法调用相结合,然后进行运行时 deinit,您将在每次调用 DotNetLoad() 时执行此代码。

请参阅此处的重要部分。这让我相信,一旦将运行时加载到进程中,您就不想再这样做了。

将初始化/取消初始化从用于调用 .NET 程序集的方法中分离出来。仅执行一次初始化(在应用程序启动时和进行调用之前),并且仅执行一次去初始化(在应用程序关闭时)。我对此进行了测试,它工作正常,没有错误。

By combining runtime init with the assembly method call, followed by runtime deinit, you are executing this code on every call to DotNetLoad().

See the important block here. This leads me to believe that once you load the runtime into your process you don't want to do it again.

Split your initialization / deinitialization out of the method used to call the .NET assembly. Do the initialization only once (at app startup and prior to making the call), and do the deinitialization only once (at app shutdown). I tested this and it worked without error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文