Gil*_*ili 41 c# interop c++-cli
我需要能够从C++调用任意C#函数.http://www.infoq.com/articles/in-process-java-net-integration建议使用ICLRRuntimeHost :: ExecuteInDefaultAppDomain(),但这只允许我调用具有以下格式的方法:int method(string arg)
调用任意C#函数的最佳方法是什么?
cdi*_*ins 48
C++应用程序有几种方法可以调用C#DLL中的函数.
ICLRRuntimeHost::ExecuteInDefaultAppDomain())
Ðаn*_*Ðаn 24
使用/ clr标志编译C++代码.有了它,您可以相对轻松地调用任何.NET代码.
例如:
#include <tchar.h>
#include <stdio.h>
int _tmain(int argc, _TCHAR* argv[])
{
System::DateTime now = System::DateTime::Now;
printf("%d:%d:%d\n", now.Hour, now.Minute, now.Second);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这算是"C++"吗?好吧,它显然不是标准C++ ......
Con*_*ngo 11
请参阅DllExport。
IOW:与DllImport工作方式完全相反。
https://github.com/3F/DllExport
它支持 Windows,跨平台支持正在开发中。
C# 代码(我们从 C++ 调用):
[DllExport]
public static int _add(int a, int b)
{
return a + b;
}
[DllExport]
public static bool saySomething()
{
DialogResult dlgres = MessageBox.Show(
"Hello from managed environment !",
".NET clr",
MessageBoxButtons.OKCancel
);
return dlgres == DialogResult.OK;
}
Run Code Online (Sandbox Code Playgroud)
C++ 代码(调用以前的 C# 代码):
typedef int(__cdecl *_add)(int a, int b);
typedef bool(__cdecl *saySomething)();
auto pAdd = (_add)GetProcAddress(lib, "_add");
int c = pAdd(5, 7);
auto pSaySomething = (saySomething)GetProcAddress(lib, "saySomething");
bool dlgres = pSaySomething();
Run Code Online (Sandbox Code Playgroud)
以及在托管和非托管上带有演示的 YouTube 视频;调用;[Conari 与 DllExport]。老实说,文档是完美的,但不要让这让你失望:YouTube 视频非常好。
该项目的灵感来自 Robert Giesecke的另一个项目,该项目在 NuGet 上有220,000 次下载。
有趣的事实:一些 Python 库使用它来实现 C++ 和 C# 混合的功能。
最后,感谢 Robert Giesecke 和 Denis Kuzmin,出色的工作!
| 归档时间: |
|
| 查看次数: |
72890 次 |
| 最近记录: |