在C ++中加载CLR,Start()问题 [英] Loading CLR in C++, Start() problem

查看:67
本文介绍了在C ++中加载CLR,Start()问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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


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.
}


推荐答案

通过将运行时初始化与组装方法调用,然后再运行时deinit,您将在每次对DotNetLoad()的调用上执行此代码。

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

在此处查看重要的块- http://msdn.microsoft.com/zh-cn/library/ms164416.aspx 我相信,一旦您将运行时加载到您的进程中,就不想再执行一次了。

See the important block here -- http://msdn.microsoft.com/en-us/library/ms164416.aspx This leads me to believe that once you load the runtime into your process you don't want to do it again.

拆分用于调用.NET程序集的方法的初始化/取消初始化。仅进行一次初始化(在应用程序启动时以及进行调用之前),并且仅进行一次初始化(在应用程序关闭时)。我对此进行了测试,并且没有错误。

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.

这篇关于在C ++中加载CLR,Start()问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆