2010-10-24 41 views
4

这里是问题 - 我有一些C图像处理库,我需要从C#应用程序使用。缺乏DllImport的经验现在让我很难受。C#编组字符**和无符号字符**

我需要使用的功能是这样的:


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

所以,它接受原始图像数据,包含XML参数和图像width'height,然后返回处理后的图像和一些XML报告。

现在我试着接近它是这样的:

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

,但没有成功。

任何建议它应该怎么做?

编辑: 仍然没有运气(( 由杂乱C++ CLI完成它现在

+0

你能提供“没有成功”更多的细节? – Dialecticus 2010-10-24 15:06:52

+0

哦,它编译并运行,但遇到访问冲突,所以有些事情是错误的。我已经仔细检查了输入xml,所以我认为问题出在 – ALOR 2010-10-24 15:16:45

回答

3

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

由于原来的内存被分配的。非托管API,它仍然是你的责任,以释放它适当的。

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

+0

因为我通过System.Drawing.Bitmap.GetHBitamp()立即得到一个IntPtr; – ALOR 2010-10-24 19:35:39

+0

@ALOR - 我明白了,这不是一个C字符串,然后你就对了。那是'Bitmap'而不是'Bitamp'我认为? – 2010-10-24 20:09:03

+0

Yeap,sry拼写错误 – ALOR 2010-10-24 22:21:28

0

试试这个

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

好的,但我应该如何'将字节[]转换为字符串,反之亦然? – ALOR 2010-10-28 15:15:30

+0

好的意味着它的工作(如在 - 不崩溃)?你指的是什么字节[]?你发布的签名只有char数组/指针 – 2010-10-28 15:44:34