C# 从本机C+反转PInvoke+;

C# 从本机C+反转PInvoke+;,c#,c++,pinvoke,C#,C++,Pinvoke,我现在试图从一个非托管C++应用程序调用一个c*dll函数。< /p> 在网上搜索了几个小时后,我发现我有一些选择 我可以使用COM、DllExport,也可以对代理使用反向PInvoke。最后一个听起来最吸引我,所以在搜索之后,我就结束了 它说明了如何使用反向PInvoke,但是看起来C代码必须先导入C++ DLL,然后才可以使用。 我需要能够用C++调用我的C.Y.DLL函数,而不首先运行C语言应用程序。p> 也许反向PInvoke不是这样做的,但我对低级的东西缺乏经验,所以任何关于如何做到

我现在试图从一个非托管C++应用程序调用一个c*dll函数。< /p> 在网上搜索了几个小时后,我发现我有一些选择

我可以使用COM、
DllExport
,也可以对代理使用反向PInvoke。最后一个听起来最吸引我,所以在搜索之后,我就结束了

它说明了如何使用反向PInvoke,但是看起来C代码必须先导入C++ DLL,然后才可以使用。

我需要能够用C++调用我的C.Y.DLL函数,而不首先运行C语言应用程序。p> 也许反向PInvoke不是这样做的,但我对低级的东西缺乏经验,所以任何关于如何做到这一点的建议都是很好的

链接中的代码是

C#

C++

#包括
#包括
typedef void(u stdcall*回调)(wchar_t*str);
外部“C”\uuuu declspec(dllexport)void\uuu stdcall调用者(wchar\u t*输入、int计数、回调调用)
{    
for(int i=0;i
Meh,只需启动您自己的CLR主机并运行您需要的:

#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();
}
#包括
#包括
#pragma注释(lib,“mscoree.lib”)
void Bootstrap()
{
ICLRRuntimeHost*pHost=NULL;
HRESULT hr=CorBindToRuntimeEx(L“v4.0.30319”,L“wks”,0,CLSID_CLRUNTIMEOST,IID_ICLRRUNTIMEOST,(PVOID*)和pHost);
pHost->Start();
printf(“HRESULT:%x\n”,hr);
//目标方法必须是静态int方法(字符串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();
}

最简单的是UnMeaveExtExt:如果你想做的是从C++中托管一个C语言的DLL,你看了这里[1 ]吗?[1] :相关链接无效非常感谢,我非常感谢代码示例。唯一的问题是它找不到mscoree.h,并且它不在程序文件中我的sdk路径中,你知道这个头应该在哪里或者我可以从哪里得到它吗?再次感谢。@bali-c不是100%确定,但我认为它是frameworksdk的一部分。您可以从msdn中获取它。谢谢,我找到了它,但是在复制了所有它抱怨的文件之后,在标题中出现了大量错误。我使用的是Code::Blocks,可以吗?还是应该使用VS?谢谢@BaliC我对Code::Blocks还不太熟悉,不能说-我用VS编译了这个,但我想网上有一些指南会有所帮助。
#include <stdio.h>    
#include <string.h>

typedef void (__stdcall *callback)(wchar_t * str);    
extern "C" __declspec(dllexport) void __stdcall caller(wchar_t * input, int count, callback call)    
{    
    for(int i = 0; i < count; i++)    
    {    
        call(input);    
    }    
}
#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();
}