【发布时间】:2011-02-06 01:35:09
【问题描述】:
所以我有一个包含在 *.dll 中的 C++ API,我想使用 C# 应用程序来调用 API 中的方法。
到目前为止,我已经创建了一个包含本机 C++ API 的 C++/CLR 项目,并设法创建了一个类似于以下内容的“桥”类:
// ManagedBridge.h
#include <CoreAPI.h>
using namespace __CORE_API;
namespace ManagedAPIWrapper
{
public ref class Bridge
{
public:
int bridge_test(void);
int bridge_test2(api_struct* temp);
}
}
.
// ManagedBridge.cpp
#include <ManagedBridge.h>
int Bridge::bridge_test(void)
{
return test();
}
int Bridge::bridge_test2(api_struct* temp)
{
return test2(temp);
}
我还有一个 C# 应用程序,它引用了 C++/CLR“Bridge.dll”,然后使用其中包含的方法。我对此有很多问题:
- 我不知道如何在 C# 程序中调用 bridge_test2,因为它不知道 api_struct 究竟是什么。我知道我需要在某处编组对象,但我是在 C# 程序还是在 C++/CLR 桥中进行?
- 这似乎是一种非常冗长的公开 API 中所有方法的方法,难道没有更简单的方法让我错过了吗? (这不使用 P/Invoke!)
编辑:好的,由于下面的回复,我现在已经掌握了基础知识,但是我的结构(在本例中称为“api_struct2”)在C++ 原生代码,如下所示:
typedef struct
{
enum_type1 eEnumExample;
union
{
long lData;
int iData;
unsigned char ucArray[128];
char *cString;
void *pvoid;
} uData;
} api_struct2;
我想我已经知道如何让枚举工作了;我已经在托管代码中重新声明了它,并且正在执行“native_enum test = static_cast(eEnumExample)”以将托管版本切换为本地版本。
然而工会让我难住了,我真的不知道如何攻击它.. 有什么想法吗?
【问题讨论】:
标签: c# c++ interop clr marshalling