通过 C++ 调用时如何拆分 Dot Net Hosting 功能 动态链接库

发布于 2024-07-17 18:16:59 字数 1392 浏览 8 评论 0原文

我正在探索从非托管 C++ 代码调用 .net 方法,并在 如何将托管 .NET 程序集 (DLL) 注入另一个进程

void StartTheDotNetRuntime()
{
    // Bind to the CLR runtime..
    ICLRRuntimeHost *pClrHost = NULL;
    HRESULT hr = CorBindToRuntimeEx(
        NULL, L"wks", 0, CLSID_CLRRuntimeHost,
        IID_ICLRRuntimeHost, (PVOID*)&pClrHost);

    // Push the CLR start button
    hr = pClrHost->Start();

    // Okay, the CLR is up and running in this (previously native) process.
    // Now call a method on our managed class library.
    DWORD dwRet = 0;
    hr = pClrHost->ExecuteInDefaultAppDomain(
        L"c:\\PathToYourManagedAssembly\\MyManagedAssembly.dll",
        L"MyNamespace.MyClass", L"MyMethod", L"MyParameter", &dwRet);

    // Stop the CLR runtime
    hr = pClrHost->Stop();

    // Don't forget to clean up.
    pClrHost->Release();
}

在控制台应用程序中调用一次时,这不会出现任何问题。

我现在想拆分此函数以在 dll 中使用,从逻辑上讲,这应该分为三个部分

Method - DLLMain
    DLL_PROCESS_ATTACH
         Bind to the CLR runtime
         Push the CLR start button

    DLL_PROCESS_DETACH
         Stop the CLR runtime
         Do not forget to clean up.

Method - CallDotNetToDoSomething

我如何以及在哪里声明 ICLRRuntimeHost pClrHost/HRESULT hr 才能实现此目的?

I am exploring calling .net methods from unmanaged C++ code and have found the function below in How To Inject a Managed .NET Assembly (DLL) Into Another Process

void StartTheDotNetRuntime()
{
    // Bind to the CLR runtime..
    ICLRRuntimeHost *pClrHost = NULL;
    HRESULT hr = CorBindToRuntimeEx(
        NULL, L"wks", 0, CLSID_CLRRuntimeHost,
        IID_ICLRRuntimeHost, (PVOID*)&pClrHost);

    // Push the CLR start button
    hr = pClrHost->Start();

    // Okay, the CLR is up and running in this (previously native) process.
    // Now call a method on our managed class library.
    DWORD dwRet = 0;
    hr = pClrHost->ExecuteInDefaultAppDomain(
        L"c:\\PathToYourManagedAssembly\\MyManagedAssembly.dll",
        L"MyNamespace.MyClass", L"MyMethod", L"MyParameter", &dwRet);

    // Stop the CLR runtime
    hr = pClrHost->Stop();

    // Don't forget to clean up.
    pClrHost->Release();
}

This works with no problem when called once in a console application.

I now want to split this function for use within a dll, logically this should be in three parts

Method - DLLMain
    DLL_PROCESS_ATTACH
         Bind to the CLR runtime
         Push the CLR start button

    DLL_PROCESS_DETACH
         Stop the CLR runtime
         Do not forget to clean up.

Method - CallDotNetToDoSomething

How and where do I declare the ICLRRuntimeHost pClrHost/HRESULT hr in order to achieve this?

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

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

发布评论

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

评论(1

静赏你的温柔 2024-07-24 18:17:00

它们可能应该是全局(静态)变量,或者某种单例变量。 每个进程只允许有一个 .NET 运行时(至少现在是这样),因此尝试变得更聪明是没有意义的。 在 DLL 加载期间填充全局变量,然后在 DLL 卸载期间取消填充它们。

对于我所做的 .NET/Mono 嵌入项目,我创建了一个对象,其构造函数启动运行时(即绑定/按下启动按钮),其析构函数将其关闭(停止/释放)。 这样主应用程序可以选择如何操作,即在main()中将其放入堆栈,或者在DLL加载期间执行new(),并在DLL卸载时删除。 在这种情况下,您提到的指针将是您创建的新对象的实例变量,例如 ClrEmbedManager。 如果您的库不需要在具有不同行为的不同类型的应用程序中重用,那就太过分了。

They should likely be global (static) variables, or in a singleton of some sort. There is only one .NET runtime allowed per process (at least these days), so there is little sense in trying to be a whole lot more clever than that. Populate the globals in DLL load, then depopulate them during DLL unload.

For a .NET/Mono embedding project I did, I created an object whose constructor booted up the runtime (i.e. bind/push start button) and whose destructor shut it down (stop/release). This way the main application could choose how to operate, i.e. put it on the stack in main(), or do a new() during DLL load, and delete in DLL unload. In that case, the pointers you mention would be instance variables of a new object you create, e.g. ClrEmbedManager. That's overkill if your library doesn't need to be reused in different kinds of applications with different behaviors.

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