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