解決:對 PInvoke 函數的調用導致堆棧不對稱問題

問題描述:

       在使用託管代碼調用非託管代碼時,發生“對 PInvoke 函數“UseTwiHikVisionDllTest!UseTwiHikVisionDllTest.TwiHikVision::GetFirstPic”的調用導致堆棧不對稱。原因可能是託管的 PInvoke 簽名與非託管的目標籤名不匹配。請檢查 PInvoke 簽名的調用約定和參數與非託管的目標籤名是否匹配。

解決方法:

     添加屬性:CallingConvention=CallingConvention.Cdecl
如:
        [DllImport("TwiHikVision.dll", EntryPoint="GetFirstPic",CallingConvention=CallingConvention.Cdecl)]
        public static extern string GetFirstPic(string videoFileName, string exportFilePath);
 
解決:對 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.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章