Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将C字符[][]数组封送到C#_C#_C++_C_Interop_Marshalling - Fatal编程技术网

将C字符[][]数组封送到C#

将C字符[][]数组封送到C#,c#,c++,c,interop,marshalling,C#,C++,C,Interop,Marshalling,我看了又看,试了我能想到的或发现的所有建议。我仍然没有得到我需要的数据 我正在使用第三方DLL,我相信它是用C编写的。我需要用C#从这个DLL访问函数。在大多数情况下,除了一个函数外,我都可以使用它。我遇到问题的函数具有以下标题: uint queryNumOfServers(USHORT *NumOfServers, char ServerNames[8][16]); 我在我的C#申请表中声明了以下内容 [DllImport("client.dll", CharSet=CharSet.Ans

我看了又看,试了我能想到的或发现的所有建议。我仍然没有得到我需要的数据

我正在使用第三方DLL,我相信它是用C编写的。我需要用C#从这个DLL访问函数。在大多数情况下,除了一个函数外,我都可以使用它。我遇到问题的函数具有以下标题:

uint queryNumOfServers(USHORT *NumOfServers, char ServerNames[8][16]);
我在我的C#申请表中声明了以下内容

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

StringBuilder serverNames = new StringBuilder(128);
short numServers = -1;
queryNumberOfServers(ref numServers, serverNames);
byte[,] serverNames = new byte[8,16];
short numServers = -1;
queryNumberOfServers(ref numServers, serverNames);
问题是,虽然numberOfServers返回等于4,但我在serverNames中只得到一个名称。我已经在一个小C程序中测试了上述C函数。我得到numberOfServer=4,但我也得到了4个名字


我尝试了以下方法,但没有成功:

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

string serverNames = new string('\0', 128);
queryNumOfServers(ref numServers, serverNames);
使用此选项不会更改服务器名称



开发环境是VisualStudio2008

我会使用
[StructLayout(LayoutKind.Sequential…][/code>和
[Marshallas(UnmanagedType.ByValTStr…][/code>

以下是两个很好的例子:


经过多次搜索和梳理,这是一个对我有效的解决方案

将我的C#应用程序中的函数头声明为:

[DllImport("client.dll", CharSet = CharSet.Ansi)]
public static extern uint queryNumOfServers(ref short numberOfServers, [MarshalAs(UnmanagedType.LPArray)] byte[,] serverNames);
当时的情况如下:

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

这将返回一个双索引字节数组将字节数组转换为字符串数组。

我尝试按照指令进行操作,但得到的只是TypeLoadException。鉴于我尝试调用的函数正在查找双字符数组,我不清楚这将如何工作。