0

我在 C++ 方面不是那么好,但是,.. 例如我有这个

#define GETSomthing_API  __declspec(dllexport)
extern GETSomthing_API int nGetSomthing;

我想像这样导入的方法

GETSomthing_API  int GetSomthing(const char* szConfigPath, char *A, int B)

我如何从 C# 调用它?

此外,我认为我的问题在于 C++ 端的参数类型(const char*),C# 中的相等类型是什么!常量字符*

谢谢,

4

3 回答 3

2

如何从 C# 调用 C++ 代码

或者

从 C# 调用 C++ 函数

使用: [DllImport("xxx.dll")] xxx.dll 由 C++ 编译

希望这有帮助。

于 2014-01-09T08:13:46.230 回答
1

有几种方法可以从 C# 调用本机代码。最简单的可能是使用P/Invoke. 说你的功能是这样的:

extern "C" int nGetSomeThing(void);

并将其编译为YourDllName.dll文件,您可以使用P/Invoke直接从C#代码中调用非托管代码,方法如下:

public static class interop
{
    [DllImport("YourDllName.dll")]
    static extern public int nGetSomeThing(void);
}
...
interop.nGetSomething() // call to the native function

参考:http: //msdn.microsoft.com/en-us/library/aa288468%28VS.71%29.aspx

如果您有很多具有复杂签名的函数,您可能应该在 C++/CLI 中使用互操作层。参考: http: //www.codeproject.com/Articles/19354/Quick-C-CLI-Learn-C-CLI-in-less-than-10-minutes

于 2014-01-09T09:13:13.703 回答
0

我刚刚添加了 __cplusplus 检查,它起作用了!

#ifdef __cplusplus

外部“C”{

万一

GETSomthing_API char* GetAgentId(const char* szConfigPath, char *szA, int ASize);

ifdef __cplusplus

}

于 2014-01-12T14:11:59.340 回答