通过 C++/CLI 公开托管 C#
Exposing managed C# through C++/CLI
我用/clr 编译了这个 C++/CLI 代码。
// CppBridge.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#using <mscorlib.dll>
using namespace System;
using namespace EmulatorLibrary;
using namespace std;
#define EXTERN_DLL_EXPORT extern "C" __declspec(dllexport)
EXTERN_DLL_EXPORT const char* exportedCall()
{
return "exportedCall";
}
public ref class DelegateCLI
{
private:
EmulatorDelegate^ emulatorDelegate;
public:
DelegateCLI() {
emulatorDelegate = gcnew EmulatorDelegate();
}
String^ callTest() {
return emulatorDelegate->test();
}
};
能够呼叫
exportedCall()
来自 JNI 和 Java。我目前对Java没有问题。
但是现在我也需要通过公开它来调用callTest((。该方法仍然是 C++/CLI。不是吗?我已经看到了对gcroot的引用,但没有完全理解实现这一目标的过程。
如何在这个 C++/CLI 层中导出callTest((?
更新1:我找到了 https://msdn.microsoft.com/en-us/library/c320cx3h.aspx,我正在尝试破译它。
这不应该起作用。
extern "C" {
__declspec(dllexport)
String^ exportedCall1() {
EmulatorDelegate^ emulatorDelegate = gcnew EmulatorDelegate();
return emulatorDelegate->test();
}
}
更新 2 : https://msdn.microsoft.com/en-us/library/481fa11f(v=vs.80(.aspx 是我正在探索的。但是我需要导出一个返回托管函数返回的字符串的函数。
这是我最好的尝试。编译为 DLL。应该工作。右?必须测试。
class Unmanaged {
public:
gcroot<String^> interopstring;
Unmanaged() {}
};
EXTERN_DLL_EXPORT const char* exportedInteropCall()
{
EmulatorDelegate^ emulatorDelegate = gcnew EmulatorDelegate();
Unmanaged u;
u.interopstring = emulatorDelegate->test();
return (const char*)
(Marshal::StringToHGlobalAnsi(u.interopstring)).ToPointer(); // "exportedCall";
}
这段代码就是我所追求的。我已经在调试模式下执行了.exe并进行了测试。我能够从我的 C++/CLI 层调用 C# 代码。
class Unmanaged {
public:
gcroot<String^> interopstring;
Unmanaged() {}
};
EXTERN_DLL_EXPORT const char* exportedInteropCall()
{
EmulatorDelegate^ emulatorDelegate = gcnew EmulatorDelegate();
Unmanaged u;
u.interopstring = emulatorDelegate->test();
return (const char*)
(Marshal::StringToHGlobalAnsi(u.interopstring)).ToPointer(); // "exportedCall";
}
相关文章:
- 在 C++/CLI 中将 .NET 事件从一个 DLL 引发到另一个 DLL
- C++/CLI System.AccessViolation在托管类中调用非托管函数时出现异常
- 是否可以在Linux上使用.Net Core 3.1创建C++/CLI代码的C#DLL
- C++ DLL(不是 CLI)是否可以调用 .NET Core 3.0 委托?
- VS2010 C++ 调试托管 (C++/CLI) DLL
- 如何将 C++/CLI 库链接到C++应用程序
- C++/CLI targetting .NET Core 3.1
- 模板函数签名在 C++/CLI 中解包
- C++/CLI 混合托管/本机 DLL 不起作用
- C++/CLI 和 C#/VB 与不安全和外部有什么区别?
- 是否有可能构建面向Linux和Windows的.Net Core C++ / CLI应用程序?
- DirectX 12 和 C++ CLI :无法使用IID_PPV_ARGS
- 如何为要在 C# 中使用的静态库创建 C++ 或 C++/CLI 包装器
- 将 C# 对象(包含静态对象成员)作为参数传递给 C++/CLI 程序
- C++/CLI -- 访问结构成员
- 如何将字符串从 C++/CLI 方法返回到调用它的非托管C++
- 如何将CLI类的函数指针作为参数传递给C++方法
- 用于LabView中使用的本机c++的c++/cli dll包装器
- C#WPF和C++/CLI以及C++在C++/CLI中添加外部库错误
- 在.NET Core 3.1中运行托管C++/CLI程序集时,映像格式错误