【问题标题】:C# Marshalling char** and unsigned char**C# 编组 char** 和 unsigned char**
【发布时间】:2011-04-29 20:23:40
【问题描述】:

这就是问题所在 - 我有一些 C 图像处理库需要从 C# 应用程序中使用。缺乏使用 DllImport 的经验让我现在很难受。

我需要使用的函数如下:


    IMAGEPROCESS_API const int importImage
        (
        const unsigned char* image,
        const char* xmlInput,
        unsigned char** resultImage,
        char** xmlOutput
        );

因此,它接受原始图像数据,包含参数的xml和图像宽度'高度,然后返回处理后的图像和一些xml报告。

现在我试图这样处理它:

[DllImport("imageprocess.dll",CallingConvention = CallingConvention.StdCall,EntryPoint = "importImage",CharSet=CharSet.Ansi)] private static extern int ImportImageNative(IntPtr imageData, String xmlDescriptor, out IntPtr modifiedImage, out IntPtr xmlOut);

但没有任何成功。

有什么建议应该怎么做?

编辑: 仍然没有运气(( 现在是由凌乱的 C++ CLI 完成的

【问题讨论】:

  • 您能否提供有关“没有任何成功”的更多详细信息?
  • 哦,编译运行了,但是遇到了访问冲突,确实是有问题。我已经仔细检查了输入 xml,所以我认为问题出在输出

标签: c# marshalling dllimport


【解决方案1】:

对于输出参数,您应该使用Marshal.PtrToStringAnsi 访问返回的数据。

由于原始内存是在非托管 API 中分配的,因此您仍有责任酌情释放它。

我也认为你应该在前两个参数上都使用String,不知道为什么第一个是IntPtr

【讨论】:

  • 因为我通过 System.Drawing.Bitmap.GetHBitamp() 立即获得了 IntPtr;
  • @ALOR - 我明白了,这不是 C 字符串,你说得对。我想那是Bitmap 不是Bitamp
【解决方案2】:

试试这个

    [DllImport("imageprocess.dll", CharSet = CharSet.Auto, SetLastError = true)]
    static extern int importImage(string imageData, string xmlDescriptor, out string processedImage, out string xmlOut);

【讨论】:

  • 好的,但是我应该如何将字节[]“转换”为字符串,反之亦然?
  • OK 意味着它工作(如 - 没有崩溃)?你指的是什么字节[]?您发布的签名只有字符数组/指针
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-15
  • 1970-01-01
  • 2020-10-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多