問題描述:
在使用託管代碼調用非託管代碼時,發生“對 PInvoke 函數“UseTwiHikVisionDllTest!UseTwiHikVisionDllTest.TwiHikVision::GetFirstPic”的調用導致堆棧不對稱。原因可能是託管的 PInvoke 簽名與非託管的目標籤名不匹配。請檢查 PInvoke 簽名的調用約定和參數與非託管的目標籤名是否匹配。”
解決方法:
You need to set the calling convention. The default convention (stdcall) is not correct, which will cause P/Invoke stack imbalances.
For example, your first call should look like:
[DllImport("Service.dll", CallingConvention=CallingConvention.Cdecl)] static private extern IntPtr CallCreateClass();
By default, C and C++ use cdecl - but marshalling uses stdcall to match the Windows API.