【发布时间】:2014-07-24 10:02:46
【问题描述】:
我在我的 C# 项目中包含了一个 C++ 库,我正在调用它的方法之一。
早些时候我遇到了修改问题,然后阅读了有关 extern c 并将其应用于 C++ 方法。
然后尝试如下调用它:
[DllImport(@"F:\bin\APIClient.dll")]
public static extern IntPtr logIn2(IntPtr a, IntPtr b, IntPtr c, IntPtr d, IntPtr e, IntPtr f, int g);
但我仍然收到入口点异常。
C++:
APICLIENT_API char* logIn2(const char* a, const char* b,const char* c,const char* d,const char* e,const char* f, int g);
如果我在 DLLImport 中使用 entryPoint,那么它工作正常:
[DllImport(@"F:\bin\APIClient.dll", EntryPoint = "?logIn2@CAPIClient@API@@QAEPADPBD00000H@Z", CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr logIn2(IntPtr a, IntPtr b, IntPtr c, IntPtr d, IntPtr e, IntPtr f, int g);
为什么即使在使用 extern c 之后,我也必须提供这个入口点才能使事情正常工作。
【问题讨论】:
-
你能展示一下 C++ 方法声明吗?
-
您是否只在头文件中使用 extern c?从您所说的看来,您似乎在标头中声明为 extern C,但该对象仍然在 .cpp 中编译为 cpp (因此被破坏)。尝试打开 dll,看看你的 C 原型是否暴露。
-
@MichaelCMS:: 可以在类方法中使用外部“C”吗?如果我们在标头中使用 extern C 则会出错。所以我们在实现中使用它。
-
@jeroenh 我已经更新了问题。
-
你说的是类方法吗?类方法名称总是错位的。