在 WriteFile kernel32.dll 上检测到 PInvokeStackImbalance
从框架 2 迁移到框架 4 后,运行 WriteFile 函数时出现错误。
[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,它是一个unsigned long in 非托管 C++。
原始错误:
检测到 PInvokeStackImbalance 消息:对 PInvoke 函数“”的调用使堆栈不平衡。这可能是因为托管 PInvoke 签名与非托管目标签名不匹配。检查 PInvoke 签名的调用约定和参数是否与目标非托管签名匹配。
After i migrated from Framework 2 to framework 4, i get an error when i run the WriteFile function.
[DllImport("kernel32.dll")]
public static extern bool WriteFile(SafeHandle hFile,
byte[] lpBuffer,
uint nNumberOfBytesToWrite,
out uint lpNumberOfBytesWritten,
long lpOverlapped);
Solution:
[DllImport("kernel32.dll")]
public static extern bool WriteFile(SafeHandle hFile,
byte[] lpBuffer,
uint nNumberOfBytesToWrite,
out uint lpNumberOfBytesWritten,
**Int32** lpOverlapped);
The lpOverlapped parameter should be a int32, which is an unsigned long in
umanaged C++.
Original Error:
PInvokeStackImbalance was detected
Message: A call to PInvoke function '' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
lpOverlapped
是一个指针,您应该将其声明为IntPtr
或ref
参数。您正在运行一个 32 位进程,并且之前在需要指针时传递了一个 64 位整数
long
。较新版本的 .net 运行时检测到该错误。解决方案绝对不是将参数声明为
Int32
。如果您编译为 64 位目标,那么这将是错误的。由于您似乎没有使用重叠 I/OI,因此只需使用
IntPtr
并传递IntPtr.Zero
。lpOverlapped
is a pointer and you should declare it asIntPtr
, or as aref
parameter.You are running a 32 bit process and were formerly passing a 64 bit integer,
long
, when a pointer was expected. The newer version of the .net runtime detects the error.The solution is most definitely not to declare the parameter as
Int32
. That will then be wrong if you ever compile to a 64 bit target.Since you appear not to be using overlapped I/O I would just use
IntPtr
and passIntPtr.Zero
.