C#位图比较(PinvokeStackimbalance异常)

时间:2012-02-19 12:00:15

标签: c# exception bitmap pinvoke

我正在尝试使用以下方法比较两个图像:

       [DllImport("msvcrt.dll")]
private static extern int memcmp(IntPtr b1, IntPtr b2, long count);

public static bool CompareMemCmp(Bitmap b1, Bitmap b2)
{
    if ((b1 == null) != (b2 == null)) return false;
    if (b1.Size != b2.Size) return false;

    var bd1 = b1.LockBits(new Rectangle(new Point(0, 0), b1.Size), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
    var bd2 = b2.LockBits(new Rectangle(new Point(0, 0), b2.Size), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

    try
    {
        IntPtr bd1scan0 = bd1.Scan0;
        IntPtr bd2scan0 = bd2.Scan0;

        int stride = bd1.Stride;
        int len = stride * b1.Height;

        return memcmp(bd1scan0, bd2scan0, len) == 0;
    }
    finally
    {
        b1.UnlockBits(bd1);
        b2.UnlockBits(bd2);
    }
}

我正在使用这样的CompareMemCmp()(on_click事件):

        Bitmap img1 = new Bitmap(@"C:\1\1.png");
        Bitmap img2 = new Bitmap(@"C:\1\2.png");

        if (CompareMemCmp(img1, img2) == true)
        { textBox1.Text = "Same"; }
        else { textBox1.Text = "Different"; }

不幸的是抛出异常:

return memcmp(bd1scan0, bd2scan0, len) == 0;

PinvokeStackimbalance “调用PInvoke函数'TextRecognition!TextRecognition.Form1 :: memcmp'使堆栈失衡。这可能是因为托管PInvoke签名与非托管目标签名不匹配。请检查PInvoke签名的调用约定和参数是否与目标非托管签名。“

可能是什么问题?我已经尝试过不同的方法来解决这个问题。

2 个答案:

答案 0 :(得分:4)

pinvoke.net says the signature should be

[DllImport("msvcrt.dll", CallingConvention=CallingConvention.Cdecl)]
static extern int memcmp(byte[] b1, byte[] b2, UIntPtr count);

编辑:pinvoke.net已将声明的原始版本标记为x64,但它似乎在x32上运行正常,只添加了CallingConvention=CallingConvention.Cdecl

答案 1 :(得分:2)

签名应为:

[DllImport("msvcrt.dll", CallingConvention=CallingConvention.Cdecl)]
static extern int memcmp(IntPtr b1, IntPtr b2, UIntPtr count);

问题中代码的问题是

  1. 调用约定之间不匹配:msvcrt.dll将其函数导出为cdecl
  2. 本机代码中的count参数为size_t,相当于.net中指针大小的无符号整数,即UIntPtr