简体   繁体   English

将C char [] []数组元素编组为C#

[英]Marshal a C char[][] array to C#

I have looked and looked and tried everything I can think of or have found suggested. 我看了看,尝试了一切我能想到或已经找到的建议。 I'm still not having any luck getting the data I need. 我仍然没有运气获得我需要的数据。

I'm using a third party DLL, which I believe is written in C. I need to access the functions from this DLL in C#. 我正在使用第三方DLL,我认为它是用C语言编写的。我需要在C#中访问此DLL中的函数。 For the most part, I have this working, except for one function. 在大多数情况下,我有这个工作,除了一个功能。 The function I'm having problems with has the following header: 我遇到问题的函数有以下标题:

uint queryNumOfServers(USHORT *NumOfServers, char ServerNames[8][16]);

I have the following declared in my C# application 我在C#应用程序中声明了以下内容

[DllImport("client.dll", CharSet=CharSet.Ansi]
public static extern uint queryNumOfServers(ref short numberofServer, StringBuilder serverNames);

I call this as follows: 我这称之为:

StringBuilder serverNames = new StringBuilder(128);
short numServers = -1;
queryNumberOfServers(ref numServers, serverNames);

The problem is, while numberOfServers come back equal to 4, I only get one name in serverNames. 问题是,虽然numberOfServers返回等于4,但我只在serverNames中获得一个名称。 I have tested the above C function in a small C program. 我在一个小C程序中测试了上面的C函数。 I get back numberOfServer = 4 but I also get 4 names back. 我得到了numberOfServer = 4,但我还得到了4个名字。


I have tried the following with no success: 我试过以下但没有成功:

[DllImport("client.dll", CharSet=charSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern uint queryNumOfServers(ref short numberOfServer,[MarshalAs(UnmanagedType.LPStr)] string serverNames);

Called this with 称之为

string serverNames = new string('\0', 128);
queryNumOfServers(ref numServers, serverNames);

There is no change to serverNames with this option. 使用此选项对serverNames没有任何更改。


The development environment is Visual Studio 2008. 开发环境是Visual Studio 2008。

After much searching and hair pulling, this was the solution that worked for me. 经过多次搜索和拔毛,这是对我有用的解决方案。

Declared the function header in my C# application as: 在我的C#应用​​程序中声明了函数头:

[DllImport("client.dll", CharSet = CharSet.Ansi)]
public static extern uint queryNumOfServers(ref short numberOfServers, [MarshalAs(UnmanagedType.LPArray)] byte[,] serverNames);

Then called as follows: 然后调用如下:

byte[,] serverNames = new byte[8,16];
short numServers = -1;
queryNumberOfServers(ref numServers, serverNames);

This returns a double index byte array. 这将返回一个双索引字节数组。 From here I used Buffer.BlockCopy() and Encoding.UTF8.GetString() to convert my byte array into a array of strings. 从这里我使用Buffer.BlockCopy()和Encoding.UTF8.GetString()将我的字节数组转换为字符串数组。

I would use [StructLayout(LayoutKind.Sequential...] and [MarshalAs(UnmanagedType.ByValTStr...] . 我会使用[StructLayout(LayoutKind.Sequential...][MarshalAs(UnmanagedType.ByValTStr...]

Here are two good examples: 这是两个很好的例子:

Marshalling a C 2-Dimensional fixed length char array 编组C二维固定长度字符数组

Marshaling a C++ two-dimensional fixed length char array as a structure member 将C ++二维固定长度char数组编组为结构成员

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

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