[英]PInvokeStackImbalance was detected on WriteFile kernel32.dll
從框架 2 遷移到框架 4 后,運行 WriteFile function 時出現錯誤。
[DllImport("kernel32.dll")]
public static extern bool WriteFile(SafeHandle hFile,
byte[] lpBuffer,
uint nNumberOfBytesToWrite,
out uint lpNumberOfBytesWritten,
long lpOverlapped);
解決方案:
[DllImport("kernel32.dll")]
public static extern bool WriteFile(SafeHandle hFile,
byte[] lpBuffer,
uint nNumberOfBytesToWrite,
out uint lpNumberOfBytesWritten,
**Int32** lpOverlapped);
lpOverlapped 參數應該是一個 int32,它在 umanaged C++ 中是一個 unsigned long。
原始錯誤:
檢測到 PInvokeStackImbalance 消息:對 PInvoke function '' 的調用使堆棧不平衡。 這可能是因為托管 PInvoke 簽名與非托管目標簽名不匹配。 檢查 PInvoke 簽名的調用約定和參數是否與目標非托管簽名匹配。
lpOverlapped
是一個指針,您應該將其聲明為IntPtr
或ref
參數。
您正在運行 32 位進程,並且以前在預期指針時傳遞了 64 位 integer, long
。 較新版本的 .net 運行時檢測到錯誤。
解決方案是絕對不要將參數聲明為Int32
。 如果您曾經編譯為 64 位目標,那將是錯誤的。
由於您似乎沒有使用重疊 I/OI,因此只需使用IntPtr
並傳遞IntPtr.Zero
。
[DllImport("kernel32.dll")]
static extern bool WriteFile(
IntPtr hFile,
byte[] lpBuffer,
uint nNumberOfBytesToWrite,
out uint lpNumberOfBytesWritten,
IntPtr lpOverlapped
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.