我有一个 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,但问题仍然存在。
有任何想法吗?