【问题标题】:A call to PInvoke function has unbalanced the stack调用 PInvoke 函数使堆栈不平衡
【发布时间】:2013-09-29 20:27:57
【问题描述】:

对本机代码调用 .NET 4 的函数会导致以下异常:

对 PInvoke 函数的调用使堆栈不平衡。这可能是因为托管 PInvoke 签名与非托管目标签名不匹配。检查 PInvoke 签名的调用约定和参数是否与目标非托管签名匹配。

这是我在 Native Code 中的定义:

typedef void ( CALLBACK* CB_DOWNLOADING )
(
      ULONG,    // secs elapsed
      LPARAM,   // custom callback param
      LPBOOL    // abort?
);


FETCH_API HttpFetchW
(
      LPCWSTR         url,
      IStream**       retval,
      LPCWSTR         usrname  = NULL,
      LPCWSTR         pwd      = NULL,
      BOOL            unzip    = TRUE,
      CB_DOWNLOADING  cb       = NULL,
      LPARAM          cb_param = 0,
      LPWSTR          ctype    = NULL,
      ULONG           ctypelen = 0
);

这是 .NET 对应的:

[DllImport(dllPath, CharSet = CharSet.Unicode, SetLastError = true, CallingConvention =     CallingConvention.Cdecl)]
        internal static extern FETCH HttpFetchW
        (
            [MarshalAs(UnmanagedType.LPWStr)]
            string url,
            out System.Runtime.InteropServices.ComTypes.IStream retval,
            [MarshalAs(UnmanagedType.LPWStr)] 
            string usrname,
            [MarshalAs(UnmanagedType.LPWStr)] 
            string pwd,
            bool unzip,
            dlgDownloadingCB cb,
            IntPtr cb_param,
            [MarshalAs(UnmanagedType.LPWStr)] 
            string ctype,
            ulong ctypelen
        );

其中 FETCH 是一个枚举。

[UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl)]
    internal delegate void dlgDownloadingCB(ulong elapsedSec, IntPtr lParam, bool abort);


internal static void DownloadingCB(ulong elapsedSec, IntPtr lParam, bool abort)
        {
           // Console.WriteLine("elapsedSec = " + elapsedSec.ToString());
        }

任何人都可以在 .NET 4 中提出替代方案吗?

非常感谢您的帮助。

【问题讨论】:

  • 我认为LPBOOL 匹配ref bool

标签: c# pinvoke


【解决方案1】:

我可以看到HttpFetchW 中唯一明显的错误是 C# ulong 是 64 位宽,而 C++ ULONG 是 32 位宽。最后一个参数的 C# 代码应该是uint

要检查的其他事项包括调用约定。你确定是cdecl?你确定SetLastError 应该是true

至于回调,您对ulong 犯了同样的错误。而abort 参数是LPBOOL。那是指向BOOL 的指针。因此,您应该将 C# 参数声明为 ref 参数。并且使用CALLBACK 声明回调,即stdcall

所以代表应该是:

[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate void dlgDownloadingCB(
    uint elapsedSec, 
    IntPtr lParam, 
    ref bool abort
);

或者干脆删除UnmanagedFunctionPointer属性,因为默认是stdcall

我认为极有可能因为回调是stdcall,所以函数也是如此。我希望 FETCH_API 宏扩展为返回枚举和调用约定。由于您没有提供这些细节,我只能猜测。你需要检查一下。

我们无法检查的另一件事是CB_DOWNLOADING

无论如何,有了这些假设,函数变为:

[DllImport(dllPath, CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern FETCH HttpFetchW(
    string url,
    out System.Runtime.InteropServices.ComTypes.IStream retval,
    string usrname,
    string pwd,
    bool unzip,
    dlgDownloadingCB cb,
    IntPtr cb_param,
    string ctype,
    uint ctypelen
);

【讨论】:

    【解决方案2】:

    来源:MSDN

    在 .NET Framework 3.5 版中,默认情况下禁用 pInvokeStackImbalance MDA。当您将 .NET Framework 3.5 版与 Visual Studio 2005 一起使用时,pInvokeStackImbalance MDA 将出现在“异常”对话框(当您单击“调试”菜单上的“异常”时显示)的“托管调试助手”列表中。但是,选择或清除 pInvokeStackImbalance 的 Throw 复选框不会启用或禁用 MDA;它只控制在激活 MDA 时 Visual Studio 是否抛出异常。

    https://msdn.microsoft.com/en-us/library/0htdy0k3(v=vs.110).aspx

    【讨论】:

      猜你喜欢
      • 2011-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多