使用C#P/Invoke的方法对Struct内部的数组进行封送处理 [英] Marshaling an array inside a Struct with methods using C# P/Invoke

查看:48
本文介绍了使用C#P/Invoke的方法对Struct内部的数组进行封送处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
我用C编写了这样的结构

Hi there,
I wrote in C a struct like that


struct IMAGE {
    unsigned int    x, y;
    unsigned char   **data;
};



有人可以告诉我如何编组此结构以在C#中使用吗?

我的解决方案不起作用.



Could anybody please tell me how to marshall this struct to use in C#?

my solution does not work.

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public class IMAGE
{
            public UInt32 x;
            public UInt32 y;

            public byte[][] data;
};

推荐答案

将struct的声明更改为:

Hi, change declaration of struct to follow:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public class IMAGE
{
            public UInt32 x;
            public UInt32 y;

            public IntPtr data;
};



并让IntPtr使用字符串方法:



and for IntPtr to string use methods:

public static string[] PtrToStringArray (IntPtr stringArray)
         {
            if (stringArray == IntPtr.Zero)
              return new string[]{};

            int argc = CountStrings (stringArray);
            return PtrToStringArray (argc, stringArray);
         }


这篇关于使用C#P/Invoke的方法对Struct内部的数组进行封送处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆