我正在尝试为Google's WebP encoder编写一个C#包装器。
我试图调用的方法是:
// Returns the size of the compressed data (pointed to by *output), or 0 if
// an error occurred. The compressed data must be released by the caller
// using the call 'free(*output)'.
WEBP_EXTERN(size_t) WebPEncodeRGB(const uint8_t* rgb,
int width, int height, int stride,
float quality_factor, uint8_t** output);
借鉴mc-kay's decoder wrapper我提出以下建议:
[DllImport("libwebp", CharSet = CharSet.Auto)]
public static extern IntPtr WebPEncodeRGB(IntPtr data, int width, int height, int stride, float quality, ref IntPtr output);
不幸的是,每当我尝试运行时,我都会收到以下错误:
对PInvoke函数'WebPSharpLib!LibwebpSharp.Native.WebPEncoder :: WebPEncodeRGB'的调用使堆栈失衡。这很可能是因为托管PInvoke签名与非托管目标签名不匹配。检查PInvoke签名的调用约定和参数是否与目标非托管签名匹配。
我在签名上尝试了很多变化无济于事。
任何人都有线索?
干杯, 麦克
答案 0 :(得分:2)
错误的最可能原因是C ++代码使用cdecl
调用约定,但您的pinvoke使用stdcall
调用约定。按如下方式更改pinvoke:
[DllImport("libwebp", CallingConvention=CallingConvention.Cdecl)]
public static extern UIntPtr WebPEncodeRGB(IntPtr data, int width, int height,
int stride, float quality, ref IntPtr output);
没有必要为没有文本参数的函数指定CharSet
。我也会使用UIntPtr
作为返回类型,因为size_t
是无符号的。
您的代码可能存在更多问题,因为我们无法看到您如何调用该函数,我们也不知道调用它的协议是什么。您需要了解更多功能签名才能知道如何调用函数。但是,我怀疑调用约会问题会让你超越目前的障碍。