从本机C反向的PInvoke ++ [英] Reverse PInvoke from native C++
问题描述
目前我正在试图从一个非托管C ++应用程序调用从C#DLL函数。
网页等搜索了几个小时之后,我发现我有几个选项。
我可以使用COM, DLLEXPORT
,也可以使用反向的PInvoke与代表。最后响起最吸引我的,所以SO搜索后,我结束了的这里。
据指出,文章介绍了如何使用反向PInvoke的,但它看起来像C#代码必须先导入C ++ DLL,然后才能使用它。
我需要能够使用C ++调用我的C#DLL函数,无需先运行一个C#应用程序。
也许扭转的PInvoke是不这样做的方式,但我很没有经验,当涉及到低层次的东西,所以就如何做到这一点的任何指针或提示将是巨大的。
在链接的代码是
的 C#
使用System.Runtime.InteropServices;
公共类Foo
{
公众委托无效回调(字符串str);
公共静态无效的被叫方(字符串str)
{
的System.Console.WriteLine(管理:+ STR);
}
公共静态INT的Main()
{
调用者(的Hello World!,10,新的回调(foo.callee));
返回0;
}
函数[DllImport(nat.dll,CallingConvention = CallingConvention.StdCall)]
公共静态外部无效调用者(字符串str,诠释计数,回拨电话);
}
的 C ++ 的
的#include<&stdio.h中GT;
&#包括LT;文件string.h>
无效的typedef(__stdcall *回调)(wchar_t的* STR);
的externC__declspec(dllexport)的无效__stdcall调用者(wchar_t的*输入,诠释计数,回拨电话)
{
的for(int i = 0; I<计数;我++)
{
调用(输入);
}
}
咩,只是旋转起来自己的CLR的主机并运行你所需要的:
的#include< mscoree.h>
&#包括LT; stdio.h中>
的#pragma评论(LIB,mscoree.lib)
无效引导()
{
ICLRRuntimeHost * pHost = NULL;
HRESULT小时= CorBindToRuntimeEx(Lv4.0.30319L周,0,CLSID_CLRRuntimeHost,IID_ICLRRuntimeHost,(PVOID *)及pHost);
pHost->启动();
的printf(HRESULT:%x\\\
,小时);
//目标方法必须是静态的INT方法(String ARG)
DWORD dwRet = 0;
HR = pHost-> ExecuteInDefaultAppDomain(LC:\\temp\\test.dllLTest.HelloLSayHello的L!人,&安培; dwRet);
的printf(HRESULT:%x\\\
,小时);
HR = pHost->停止();
的printf(HRESULT:%x\\\
,小时);
pHost->发行();
}
INT主要()
{
引导();
}
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 can use COM, DllExport
, or use reverse PInvoke with delegates. The last sounded most appealing to me, so after searching SO I ended up here.
It states that the article shows how to use reverse PInvoke, but it looks like the C# code has to first import the C++ Dll, before it can be used.
I need to be able to use C++ to call my C# Dll functions, without running a C# app first.
Maybe reverse PInvoke isn't the way to do it, but I am quite inexperienced when it comes to low level stuff, so any pointers or tips on how to do this would be great.
The code in the link is
C#
using System.Runtime.InteropServices;
public class foo
{
public delegate void callback(string str);
public static void callee(string str)
{
System.Console.WriteLine("Managed: " +str);
}
public static int Main()
{
caller("Hello World!", 10, new callback(foo.callee));
return 0;
}
[DllImport("nat.dll",CallingConvention=CallingConvention.StdCall)]
public static extern void caller(string str, int count, callback call);
}
C++
#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);
}
}
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();
}
这篇关于从本机C反向的PInvoke ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!