后期绑定 C++ DLL 到 C# - 函数始终返回 true
我有一个 DLL,它的 h 文件中包含此内容:
extern "C" __declspec(dllexport) bool Connect();
在 c 文件中:
extern "C" __declspec(dllexport) bool Connect()
{
return false;
}
在 c# 中,我有以下代码:
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate bool ConnectDelegate();
private ConnectDelegate DLLConnect;
public bool Connect()
{
bool l_bResult = DLLConnect();
return l_bResult;
}
public bool LoadPlugin(string a_sFilename)
{
string l_sDLLPath = AppDomain.CurrentDomain.BaseDirectory;
m_pDLLHandle = LoadLibrary(a_sFilename);
DLLConnect = (ConnectDelegate)GetDelegate("Connect", typeof(ConnectDelegate));
return false;
}
private Delegate GetDelegate(string a_sProcName, Type a_oDelegateType)
{
IntPtr l_ProcAddress = GetProcAddress(m_pDLLHandle, a_sProcName);
if (l_ProcAddress == IntPtr.Zero)
throw new EntryPointNotFoundException("Function: " + a_sProcName);
return Marshal.GetDelegateForFunctionPointer(l_ProcAddress, a_oDelegateType);
}
由于某些奇怪的原因,无论 C++ 中的返回值是什么,connect 函数总是返回 true。 我尝试将 C# 中的调用约定更改为 StdCall,但问题仍然存在。
有什么想法吗?
I have an DLL that has this in its h file:
extern "C" __declspec(dllexport) bool Connect();
and in the c file:
extern "C" __declspec(dllexport) bool Connect()
{
return false;
}
In c# i have the following code:
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate bool ConnectDelegate();
private ConnectDelegate DLLConnect;
public bool Connect()
{
bool l_bResult = DLLConnect();
return l_bResult;
}
public bool LoadPlugin(string a_sFilename)
{
string l_sDLLPath = AppDomain.CurrentDomain.BaseDirectory;
m_pDLLHandle = LoadLibrary(a_sFilename);
DLLConnect = (ConnectDelegate)GetDelegate("Connect", typeof(ConnectDelegate));
return false;
}
private Delegate GetDelegate(string a_sProcName, Type a_oDelegateType)
{
IntPtr l_ProcAddress = GetProcAddress(m_pDLLHandle, a_sProcName);
if (l_ProcAddress == IntPtr.Zero)
throw new EntryPointNotFoundException("Function: " + a_sProcName);
return Marshal.GetDelegateForFunctionPointer(l_ProcAddress, a_oDelegateType);
}
For some weird reason the connect function always returns true no matter what the return value is in the C++.
I've tried changing the calling convention to StdCall in C#, but the problem stays.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题可能出在“bool”上。
在 MSVC 中,sizeof(bool) 是 1,而 sizeof(BOOL) 是 4!
BOOL是Windows API用来表达布尔值的类型,是一个32位整数。
因此,C# 需要 32 位值,但您提供的是 1 字节值,因此您得到的是“垃圾”。
有两种解决方案:
1) u 更改 C 代码以返回 BOOL 或 int。
2) 您更改 C# 代码,将
[return:MarshalAs(UnmanagementType.I1)]
属性添加到 dll 导入函数中。The problem propably is in the "bool".
In MSVC sizeof(bool) is 1 while sizeof(BOOL) is 4!
BOOL is the type used by windows API to express a boolean value, and is a 32 bit integer.
So C# expets a 32 bit value but u are prividing a 1 byte value, so u are getting "garbage".
There are two solutions:
1) u change your C code to return BOOL or int.
2) You change the C# code adding
[return:MarshalAs(UnmanagedType.I1)]
attribute to your dll import functions.