在 WriteFile kernel32.dll 上检测到 PInvokeStackImbalance

发布于 2024-11-17 12:04:10 字数 796 浏览 4 评论 0原文

从框架 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故笙诉离歌 2024-11-24 12:04:10

lpOverlapped 是一个指针,您应该将其声明为 IntPtrref 参数。

您正在运行一个 32 位进程,并且之前在需要指针时传递了一个 64 位整数 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
);

lpOverlapped is a pointer and you should declare it as IntPtr, or as a ref 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 pass IntPtr.Zero.

[DllImport("kernel32.dll")]
static extern bool WriteFile(
    IntPtr hFile,
    byte[] lpBuffer,
    uint nNumberOfBytesToWrite, 
    out uint lpNumberOfBytesWritten,
    IntPtr lpOverlapped
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文