本文介绍了传递结构,其中包含C和C#之间的结构数组(DLL和P调用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的C dll具有一些复杂的结构,而我在C#中确实是一个新手:

I have C dll with some complicated struct and I ma really a newbie in C#:

typedef struct {
    int a;
    int b;
} simple_struct;

typedef struct {
    int d;
    int e;
    simple_struct f[20];
    short g;
    simple_struct h[20];
    short i;
} complex_struct;

问题是我无法使用这种结构连接C#应用程序!

The issue is that I am not able to interface my C# application with this structure!!

在DLL中有一个函数Ge​​tData(complex_struct * myStruct),我应该从C#中调用它,所以我创建了:

In the DLL there is a function GetData(complex_struct* myStruct) and I shoud call it from C#, so I created:

    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    unsafe struct simple_struct {
        public int a;
        public int b;
    } ;

    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    unsafe struct complex_struct {
        public int d;
        public int e;
        public simple_struct[] f;
        public short g;
        public simple_struct[] h;
        public short i;
    } ;

但是问题是当我将complex_struct作为GetData的参数传递时,所有字段都被填回表格我,但不是我的simple_struct这两个数组(我的意思是f和h)!他们的值是空的!

but the problem is that when I pass complex_struct as argument of GetData, all the fields are filled back form me, but not my two array of simple_struct (I mean f and h)!! Their values are null!!

有人可以帮我吗,谢谢

您好,谢谢您的答复,

我已经按照您说的做了,但是当我调用GetData时,我仍然遇到另一个问题,该过程在没有任何消息(一种异常):

I have done like what you said, but I still have another issue when I call GetData, the process crashes without any message (a kind of Exception):

这是我的C语言代码:
名称空间dll_test_import_c_sharp
{
class Program
{
[StructLayout(LayoutKind.Sequential,Pack = 1)]
struct simple_struct {
public int a;
public int b;
};

This is my C sharp code: namespace dll_test_import_c_sharp { class Program { [StructLayout(LayoutKind.Sequential, Pack = 1)] struct simple_struct { public int a; public int b; } ;

        [StructLayout(LayoutKind.Sequential, Pack = 1)]
        struct complex_struct {
            public int d;
            public int e;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
            public simple_struct[] f;
            public short g;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
            public simple_struct[] h;
            public short i;
        } ;




        [DllImport("test_dll.dll", CharSet = CharSet.Unicode)]
        static extern int GetData(ref complex_struct a);



        static void Main(string[] args)
        {
            complex_struct a = new complex_struct();
            GetData(ref a);
            return;
        }

    }
}

我有我在GetData上做了很多printf,并且它们都执行得很好,看来'return'指令崩溃了!!

I have done a lot of printf i GetData and all of them are well executed, it seems like the 'return' instruction crashes!!

我试图通过ref或out调用GetData,但它们都不起作用...

I tried to call GetData by ref or by out and both of them don't work...

您好,谢谢您的答复,

我的所作所为与您所说的一样,但是当我遇到其他问题时,调用GetData,进程崩溃而没有任何消息(一种异常):

I have done like what you said, but I still have another issue when I call GetData, the process crashes without any message (a kind of Exception):

这是我的C语言代码:
命名空间dll_test_import_c_sharp
{
类程序
{
[StructLayout(LayoutKind.Sequential,Pack = 1)]
struct simple_struct {
public int a;
public int b;
};

This is my C sharp code: namespace dll_test_import_c_sharp { class Program { [StructLayout(LayoutKind.Sequential, Pack = 1)] struct simple_struct { public int a; public int b; } ;

        [StructLayout(LayoutKind.Sequential, Pack = 1)]
        struct complex_struct {
            public int d;
            public int e;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
            public simple_struct[] f;
            public short g;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
            public simple_struct[] h;
            public short i;
        } ;




        [DllImport("test_dll.dll", CharSet = CharSet.Unicode)]
        static extern int GetData(ref complex_struct a);



        static void Main(string[] args)
        {
            complex_struct a = new complex_struct();
            GetData(ref a);
            return;
        }

    }
}

我有在GetData上做了很多printf,并且它们都执行得很好,看来'return'指令崩溃了!!

I have done a lot of printf i GetData and all of them are well executed, it seems like the 'return' instruction crashes!!

我尝试通过ref或out调用GetData,但它们都不起作用...

I tried to call GetData by ref or by out and both of them don't work...

推荐答案

您需要在 struct 上更改数组定义,以指定它是按值/内联数组

You need to change the array definition on the struct to specify that it's a by value / inline array

[StructLayout(LayoutKind.Sequential, Pack = 1)]
unsafe struct complex_struct {
    public int d;
    public int e;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
    public simple_struct[] f;
    public short g;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
    public simple_struct[] h;
    public short i;
} ;

这篇关于传递结构,其中包含C和C#之间的结构数组(DLL和P调用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 15:11