3
我有了这个在其H文件中的DLL:后期绑定C++ DLL到C# - 功能总是返回true
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,但问题仍然存在。
任何想法?
这样做。谢谢! – Nitay