积极答复者
C#调用C++ dll

问题
-
从网上找了一个C版本的des加密实现,需要在C#调用。
c++ 的头文件片段:
class _declspec(dllexport) Des { public: Des(); ~Des(); bool Des_Go(char *Out,char *In,long datalen,const char *Key,int keylen,bool Type = ENCRYPT);
C#中调用代码:
现在运行C#程序总是提示“无法在 DLL“test.dll”中找到名为“Des_Go”的入口点。”[DllImportAttribute("test.dll", EntryPoint = "Des_Go")]
[return: MarshalAsAttribute(UnmanagedType.I1)]
public static extern bool Des_Go(StringBuilder Out, StringBuilder In, int datalen,
[InAttribute()] [MarshalAsAttribute(UnmanagedType.LPStr)] string Key, int keylen,
[MarshalAsAttribute(UnmanagedType.I1)] bool Type);
- 已编辑 MatrixZero 2012年3月30日 5:29
- 已移动 ThankfulHeartModerator 2012年3月30日 6:12 C#问题 (发件人:.NET Framework 一般性问题讨论区)
答案
-
用depends看看你的DLL的入口点,如果你的C++代码正确的话,入口点应该为_Des_Go
The following is signature, not part of post
Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
Visual C++ MVP- 已标记为答案 Jason Dot WangModerator 2012年4月11日 5:16
-
Hi,
你确定你的CallingConvention正确吗!还有你的这个c++的dll加密算法是否已经用extern "C" __declspec(dllexport)来输出这个方法签名了吗?还有一个你要考虑的是如果你想得到调用这个c++返回参数的ExceptionCode的代码的话,你还要设置GetLastError是否为true 或者false,具体的如何COM互操作,你可以参考这个帖子: http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/b0637998-dc89-4ff9-b59d-5ad262a48bc6
其中包括的那本《COM and .NET interoperability》讲到这个主题的方方面面。
Jason Wang [MSFT]
MSDN Community Support | Feedback to us
- 已编辑 Jason Dot WangModerator 2012年4月2日 6:17
- 已标记为答案 Jason Dot WangModerator 2012年4月11日 5:16
全部回复
-
用depends看看你的DLL的入口点,如果你的C++代码正确的话,入口点应该为_Des_Go
The following is signature, not part of post
Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
Visual C++ MVP- 已标记为答案 Jason Dot WangModerator 2012年4月11日 5:16
-
Hi,
你确定你的CallingConvention正确吗!还有你的这个c++的dll加密算法是否已经用extern "C" __declspec(dllexport)来输出这个方法签名了吗?还有一个你要考虑的是如果你想得到调用这个c++返回参数的ExceptionCode的代码的话,你还要设置GetLastError是否为true 或者false,具体的如何COM互操作,你可以参考这个帖子: http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/b0637998-dc89-4ff9-b59d-5ad262a48bc6
其中包括的那本《COM and .NET interoperability》讲到这个主题的方方面面。
Jason Wang [MSFT]
MSDN Community Support | Feedback to us
- 已编辑 Jason Dot WangModerator 2012年4月2日 6:17
- 已标记为答案 Jason Dot WangModerator 2012年4月11日 5:16