Reverse PInvoke from native C++

后端 未结 1 2064
醉酒成梦
醉酒成梦 2021-02-08 16:18

I am currently trying to call a function from a C# DLL from an unmanaged C++ app.

After searching for hours on the web and SO, I found I have a few options.

I ca

相关标签:
1条回答
  • 2021-02-08 16:32

    Meh, just spin up your own CLR host and run what you need to:

    #include <mscoree.h>
    #include <stdio.h>
    #pragma comment(lib, "mscoree.lib") 
    
    void Bootstrap()
    {
        ICLRRuntimeHost *pHost = NULL;
        HRESULT hr = CorBindToRuntimeEx(L"v4.0.30319", L"wks", 0, CLSID_CLRRuntimeHost, IID_ICLRRuntimeHost, (PVOID*)&pHost);
        pHost->Start();
        printf("HRESULT:%x\n", hr);
    
        // target method MUST be static int method(string arg)
        DWORD dwRet = 0;
        hr = pHost->ExecuteInDefaultAppDomain(L"c:\\temp\\test.dll", L"Test.Hello", L"SayHello", L"Person!", &dwRet);
        printf("HRESULT:%x\n", hr);
    
        hr = pHost->Stop();
        printf("HRESULT:%x\n", hr);
    
        pHost->Release();
    }
    
    int main()
    {
        Bootstrap();
    }
    
    0 讨论(0)
提交回复
热议问题