简体   繁体   English

如何在C#中PInvoke另一个结构内的结构数组

[英]How to PInvoke in C# an array of structs inside another struct

From C#.NET I'm trying to PInvoke a method from a C++ assembly (no access to source) with this signature 从C#.NET,我正在尝试使用此签名从C ++程序集中调用方法(无法访问源代码)

result_code get_values(values_report_type *values_report)

Where values_report is a pointer to a struct of values_report_type containing the values I am querying which are returned by the method. 其中values_report是一个指向的一个结构values_report_type含有我查询其值是由该方法返回。 The struct looks like this: 该结构如下所示:

typedef struct{
    int countOfValues;
    values_type* values;
    const char* name;
} values_report_type;

Where values is an array of struct values_type . 其中values是struct values_type数组

Here's what I would assume to be the problem: the C++ assembly I am working against gives me no information about the content of the values_type struct other than the definition 这就是我可能会想到的问题:我正在使用的C ++程序集除了定义之外没有提供任何有关values_type结构内容的信息

typedef struct values_type_struct *values_type

According to documentation this privacy is intentional. 根据文档,此隐私是故意的。

So, my C# PInvoke looks like this right now: 因此,我的C#PInvoke现在看起来像这样:

[DllImport("MyLibrary.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
internal static extern result_code get_values(out values_report_type values_report);

internal struct values_report_type{
    int countOfValues;
    IntPtr values;
    string name;
}

That works fine and gives me a pointer to the values struct, but what I need is to access the array of values_type structs and get a pointer to each of the items in the array (a pointer to each item is all I need since I don't have a definition for the struct contents). 那工作正常,并为我提供了一个指向values结构的指针,但是我需要的是访问values_type结构的数组,并获得一个指向数组中每个项目的指针(指向每个项目的指针是我所需要的,因为我没有没有对struct内容的定义)。 But I can't think of a way to achieve this, especially because the library has limited me in what it says about that struct (not even giving length). 但是我想不出一种方法来实现这一点,特别是因为该库限制了我对结构的理解(甚至没有给出长度)。 In C++ it would just be something like values_report.values[0] and so on for each item as defined by countOfValues , but I don't know of a way to make it work when marshalling it to .NET. 在C ++中,由countOfValues定义的每个项目都将类似于values_report.values[0]等,但是当将其编组到.NET时,我不知道使它起作用的方法。

Is there a way to solve this in C# via marshalling? 有没有办法通过编组在C#中解决此问题?

values is not an array of struct. values不是struct数组。 It is an array of pointers to struct. 它是指向 struct的指针数组。

Based on the comments, and looking at the code, it seems that you don't have a definition of the struct itself. 根据注释并查看代码,似乎您没有结构本身的定义。 This is what is known as an opaque pointer. 这就是所谓的不透明指针。 You don't need to know the struct size, you just deal in the pointer to the struct, passing that back to the library. 您不需要知道结构的大小,只需处理指向结构的指针,然后将其传递回库即可。

Read the opaque pointer values from the array like this: 像这样从数组读取不透明指针值:

IntPtr Value = Marshal.ReadIntPtr(values_report.values, i*IntPtr.Size);

This obtains the i th value. 由此获得第i 值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM