Unmanaged C code in C# Marshalling by ref string array!

3.5k views Asked by At

I am having a really hard time getting this marshalling down.

I have umanaged code that looks like this:

WORD HLP_GetDeviceNames (LPSTR *DevNames, WORD Max_Len, WORD Max_Num)

Just FYI I did not write this unmanaged code but must use it.

Returns: WORD indicating an error.

DevNames: Pointer to an array of char arrays. Basically an array of strings that will be modified and returned back to me!

Max_Len: Length of each string (I am told this must be 256)

Max_Num: Length of array. I am using another Invoke call that is working that tells me number of devices so i know exactly how many strings to send.

I have used P/Invoke interop signatureToolkit to figure alot of this out but also read a bunch to get even further. Where I am now is here:

[DllImport("UsbMeasLib.dll")]
public static extern ushort HLP_GetDeviceNames([MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPStr)] ref StringBuilder[] DevNames, ushort Max_Len, ushort Max_Num);

I call my code like this:

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())); 

I am using string builder array because I need the unmanaged code to modify the string builder so that it can return the new string since string is unmutable.

When I run the code, My array is unmodified!

I'm not really sure what is going on but I think it has something to do with CLR telling unmanaged code to not modify my array in place but instead creates a new reference(pointer). Even if this is the case, I dont know how to fix it.

Thanks for any insight anybody can offer!

2

There are 2 answers

0
Mike Bynum On BEST ANSWER

I figured this one out. Thanks to anybody who replied.

I found out how it works. I simply supply the memory space but I have to let the marshaling know that I expect in and out with this object so it allows the unmanaged code to modify the allocated space.

I did it like this:

[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);

I use string instead of string builder because the unmanaged code will simply replace the string which is ok. I am getting the array pointer back, not modified strings. the managed code is just changing an array of pointers to point to new string objects (I think).

int numDev = HLP_GetNumDevices();


string[] names = new string[numDev];

for (int i = 0; i < names.Length; i++)
{
    names[i] = new StringBuilder().Append(' ', 256).ToString();
}

ushort errorCode = HLP_GetDeviceNames(names, 256, Convert.ToUInt16(numDev));

I allocate memory for the unamanged code then let the unmanaged code chane the strings there.

This works but I dont know if I have any potential memory leaks or other potential problems.

1
Alex F On

Try to work on low level. Declare DevNames parameter as IntPtr[]. Prepare it by the following way:

IntPtr[] devNames = new IntPtr[deviceCount];

for(int i = 0; i < deviceCount; i++) 
{ 
    devNames[i] = Marshal.AllocHGlobal[256];
}

Pass this array to HLP_GetDeviceNames. To handle output data, apply Marshal.PtrToStringAnsi to every DevNames member. Don't forget to release DevNames[i] with Marshal.FreeHGlobal in the end.