C#位图比较(PinvokeStackimbalance异常)

4

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

       [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个回答

5

PInvoke.net认为签名应该是

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

编辑:pinvoke.net将声明的原始版本标记为仅限x64,但只需添加 CallingConvention=CallingConvention.Cdecl,似乎也可以在x32上正常运行。


帮助:[DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)] private static extern int memcmp(IntPtr b1, IntPtr b2, long count); - Alex
我刚刚根据你的解决方案修改了[import dll]的后半部分.. ;) 感谢你的提示。 - Alex
@Alex 啊,Cdecl 就够了,省去了一些类型转换的魔法 :) - Joachim Isaksson
@JoachimIsaksson你所说的“unless you run x64 only”是什么意思?UIntPtr在x86和x64上都与size_t匹配。 - David Heffernan
pinvoke.net在我的经验中非常不准确。有很多错误。因为size_t与UIntPtr大小相同,所以该签名在x86和x64上都可以正常使用。 - David Heffernan
显示剩余5条评论

2
签名应该是:
[DllImport("msvcrt.dll", CallingConvention=CallingConvention.Cdecl)]
static extern int memcmp(IntPtr b1, IntPtr b2, UIntPtr count);

该问题的代码存在以下问题:

  1. 调用约定不匹配:msvcrt.dll将其函数导出为cdecl
  2. 本地代码中的计数参数是size_t,在.net中等同于指针大小的无符号整数,即UIntPtr

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接