all groups > dotnet interop > july 2006 >
You're in the

dotnet interop

group:

Marshaling for setupapi.dll


Marshaling for setupapi.dll bullshark NO[at]SPAM gmail.com
7/27/2006 12:23:13 PM
dotnet interop:
We have:
[StructLayout(LayoutKind.Sequential, CharSet= CharSet.Ansi)]
public unsafe struct SP_DEVICE_INTERFACE_DETAIL_DATA {
public int cbSize;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=84)]
public char[] DevicePath;
}

The setupapi.dll method SetupDiGetDeviceInterfaceDetail requires us to
call it once to find the size of the output buffer "DevicePath", and
then to call it again, after allocating the char[] to fetch the
details.

The code above is kludged to marshal a fixed size. It works fine. What
I can't seem to figure is how to accomplish this without the kludging
to a pre-determined size.

Is there no way to do this without a C library as a go-between?

This idiom occurs so often in Win32 that I find it hard to believe that
theres not a simple solution. I tried manual marshalling with
Marshal.AllocHGlobal, which was successfully used to retrieve the
details, but then extracting "DevicePath" from an IntPtr was
problematic. Marshal.PtrToStructure doesn't work on valueTypes and I
couldn't figure a way to get from (coerce) the IntPtr into a char[] for
Marshal.copy.

Thanks in advance for any ideas,

bullshark
Re: Marshaling for setupapi.dll Winista
8/2/2006 11:43:44 AM
See if this helps..

http://www.netomatix.com/Development/EmbedStruct.aspx

[quoted text, click to view]

AddThis Social Bookmark Button