C#按引用字符串数组编组的非托管C代码!

C#按引用字符串数组编组的非托管C代码!,c#,clr,marshalling,unmanagedresources,C#,Clr,Marshalling,Unmanagedresources,我真的很难把这个编组下来 我的代码如下所示: WORD HLP_GetDeviceNames (LPSTR *DevNames, WORD Max_Len, WORD Max_Num) StringBuilder[] DevNames = new StringBuilder[deviceCount]; for(int i = 0; i< deviceCount; i++) { DevNames[i] = new StringBuilder().A

我真的很难把这个编组下来

我的代码如下所示:

WORD HLP_GetDeviceNames (LPSTR *DevNames, WORD Max_Len, WORD Max_Num)
StringBuilder[] DevNames = new StringBuilder[deviceCount];
     for(int i = 0; i< deviceCount; i++)
     {
           DevNames[i] = new StringBuilder().Append(' ', 256);
     }

     HachUsbMeasLib.HLP_GetDeviceNames(ref DevNames, 256, Convert.ToUInt16(DevNames.Count())); 
[DllImport("UsbMeasLib.dll")]
private static extern ushort HLP_GetDeviceNames([In, Out, MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPStr)] string[] DevNames, ushort Max_Len, ushort Max_Num);
仅供参考,我没有编写此非托管代码,但必须使用它

返回:表示错误的单词

DevNames:指向字符数组数组的指针。基本上是一个字符串数组,将被修改并返回给我

Max_Len:每个字符串的长度(我被告知这必须是256)

Max_Num:数组的长度。我正在使用另一个正在工作的调用,它告诉我设备的数量,以便我确切地知道要发送多少字符串

我已经使用p/Invoke interop signatureToolkit解决了很多问题,但也阅读了一些文章以进一步了解。我现在在这里:

[DllImport("UsbMeasLib.dll")]
public static extern ushort HLP_GetDeviceNames([MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPStr)] ref StringBuilder[] DevNames, ushort Max_Len, ushort Max_Num);
我这样称呼我的代码:

WORD HLP_GetDeviceNames (LPSTR *DevNames, WORD Max_Len, WORD Max_Num)
StringBuilder[] DevNames = new StringBuilder[deviceCount];
     for(int i = 0; i< deviceCount; i++)
     {
           DevNames[i] = new StringBuilder().Append(' ', 256);
     }

     HachUsbMeasLib.HLP_GetDeviceNames(ref DevNames, 256, Convert.ToUInt16(DevNames.Count())); 
[DllImport("UsbMeasLib.dll")]
private static extern ushort HLP_GetDeviceNames([In, Out, MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPStr)] string[] DevNames, ushort Max_Len, ushort Max_Num);
StringBuilder[]DevNames=新StringBuilder[deviceCount];
for(int i=0;i
我使用字符串生成器数组是因为我需要非托管代码来修改字符串生成器,以便它可以返回新字符串,因为字符串是不可变的

当我运行代码时,我的数组没有被修改

我不确定到底发生了什么,但我认为这与CLR告诉非托管代码不要就地修改数组,而是创建一个新引用(指针)有关。即使是这样,我也不知道如何解决它


感谢任何人提供的任何见解

试着在低水平上工作。将DevNames参数声明为IntPtr[]。按以下方式准备:

IntPtr[] devNames = new IntPtr[deviceCount]; for(int i = 0; i < deviceCount; i++) { devNames[i] = Marshal.AllocHGlobal[256]; } IntPtr[]devNames=新的IntPtr[设备计数]; for(int i=0;i将此数组传递给HLP_GetDeviceNames。要处理输出数据,请对每个DevNames成员应用Marshal.PtrToStringAnsi。最后,别忘了用Marshal.FreeHGlobal发布DevNames[i]。

我想出了这个办法。感谢所有回复的人

我发现了它的工作原理。我只是提供内存空间,但我必须让封送处理知道我希望使用该对象进行输入和输出,以便允许非托管代码修改分配的空间

我是这样做的:

WORD HLP_GetDeviceNames (LPSTR *DevNames, WORD Max_Len, WORD Max_Num)
StringBuilder[] DevNames = new StringBuilder[deviceCount];
     for(int i = 0; i< deviceCount; i++)
     {
           DevNames[i] = new StringBuilder().Append(' ', 256);
     }

     HachUsbMeasLib.HLP_GetDeviceNames(ref DevNames, 256, Convert.ToUInt16(DevNames.Count())); 
[DllImport("UsbMeasLib.dll")]
private static extern ushort HLP_GetDeviceNames([In, Out, MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPStr)] string[] DevNames, ushort Max_Len, ushort Max_Num);
我使用字符串而不是字符串生成器,因为非托管代码将简单地替换字符串,这是正常的。我得到的是数组指针,而不是修改过的字符串。托管代码只是更改指针数组以指向新的字符串对象(我认为)

int numDev=HLP_GetNumDevices();
字符串[]名称=新字符串[numDev];
for(int i=0;i
我为未更改的代码分配内存,然后让非托管代码更改那里的字符串


这是可行的,但我不知道我是否有任何潜在的内存泄漏或其他潜在问题。

谢谢您的回复!我做了一件非常类似的事情,并让它发挥作用。