求教C#调用C++DLL没有加extern "C"修饰的函数

螃蟹大人 2012-01-09 10:32:24
C++头文件如下:

class IQuoteApi
{
public:
//API函数
virtual bool __cdecl Open(TAddressField& addr) = 0;

virtual void __cdecl Close() = 0;

virtual bool __cdecl IsOpen() = 0;

};


extern "C"
{
//认证函数 认证码 针对每个开发商 一个
bool __cdecl CertApi(TCertInfoType cert, TLogPathType path);
}


C#代码如下:

[DllImport("Api.Dll", CallingConvention = CallingConvention.Cdecl)]
public static extern bool CertApi(string cert,string path);

[DllImport("Api.Dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int Close();

bool certInfo;
certInfo = CertApi("sdfsasfwer", "asdfdf");
int c;
c = Close();


运行报错:
无法在 DLL“Api.Dll”中找到名为“Close”的入口点。

使用反编译工具PE Explorer查看入口点没有仅仅存在CertApi,未找到Open、Close、IsOpen,
由此是否可以得到结论未声明extern "C"的C++将不会生成函数入口?

请问使用C#调用C++DLL是不是非得声明extern "C"才能被C#调用到?若未申明怎样才能调用到?
...全文
263 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
Saleayas 2012-01-09
  • 打赏
  • 举报
回复
接口函数不是 API 。
当然更加不是 dllimport 了。

我一般都是用 托管 C++ 封装一下。
因为不仅可以封装函数,还可以封装数据类型。
螃蟹大人 2012-01-09
  • 打赏
  • 举报
回复
如果没有高手解答 希望版主转到C#版 小弟实在没有积分再开贴了
螃蟹大人 2012-01-09
  • 打赏
  • 举报
回复
跪求大大回答 在线等
螃蟹大人 2012-01-09
  • 打赏
  • 举报
回复
没有人知道吗?请高手解答 送分
螃蟹大人 2012-01-09
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 saleayas 的回复:]

是否能够用 Dllimport 和 extern "C" 没有任何联系。

extern "C" 只是把函数修饰为 C 语言的函数。

只有在 .def 里面导出的或者使用 __declspec(dllexport) 的才能使用 DllImport 来导入。
[/Quote]

哦哦 学到了 谢谢Saleayas 结贴送分~~
Saleayas 2012-01-09
  • 打赏
  • 举报
回复
是否能够用 Dllimport 和 extern "C" 没有任何联系。

extern "C" 只是把函数修饰为 C 语言的函数。

只有在 .def 里面导出的或者使用 __declspec(dllexport) 的才能使用 DllImport 来导入。
螃蟹大人 2012-01-09
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 saleayas 的回复:]

C/C++ code

class IQuoteApi
{
public:
//API函数
virtual bool __cdecl Open(TAddressField& addr) = 0;

virtual void __cdecl Close() = 0;

virtual bool __cdecl IsOpen() = 0;

};

……
[/Quote]

哦哦 我知道了 谢谢 就是还需要我自己封装一次,然后用extern "C"修饰一次重新生成一个能使用DllImport方法的DLL是吧!
Saleayas 2012-01-09
  • 打赏
  • 举报
回复

class IQuoteApi
{
public:
//API函数
virtual bool __cdecl Open(TAddressField& addr) = 0;

virtual void __cdecl Close() = 0;

virtual bool __cdecl IsOpen() = 0;

};

open,close and IsOpen 不是 API 函数,是接口 IQuoteApi 的接口函数。
这些函数在 DLL 里面是没有导出的,所以 DllImport 是导入不了的。
必须在创建 IQuoteApi 接口实例后后引用。
赵4老师 2012-01-09
  • 打赏
  • 举报
回复
DUMPBIN /ALL libname.lib

The output of the above command gives you the decorated names for functions, which is very useful in solving linker problems like LNK2001 error (Unresolved external). You also get the information of the default libraries used, which is useful in solving LNK2005 error (Symbol already defined in another library). Run the above command on the library just created and find out what default libraries are used and how the functions and variables are listed (or decorated).
螃蟹大人 2012-01-09
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 saleayas 的回复:]

接口函数不是 API 。
当然更加不是 dllimport 了。

我一般都是用 托管 C++ 封装一下。
因为不仅可以封装函数,还可以封装数据类型。
[/Quote]

大侠的意思是没有用extern "C"修饰的接口函数是不能用dllimport方式导出吗?

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧