-
I'm not sure how to use it with |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
That struct is incorrectly generated at the moment. You can track #387 for resolution of that. |
Beta Was this translation helpful? Give feedback.
-
As a workaround I'm using the following definition to just have a fixed size array that should be big enough and then cast it as I pass it to the function. public partial struct SP_DEVICE_INTERFACE_DETAIL_DATA
{
public uint cbSize;
public __char_1 DevicePath;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public unsafe partial struct __char_1
{
public fixed char _0[512];
/// <summary>Always <c>1</c>.</summary>
public readonly int Length => 512;
/// <summary>
/// Copies the fixed array to a new string up to the specified length regardless of whether there are null terminating characters.
/// </summary>
/// <exception cref="ArgumentOutOfRangeException">
/// Thrown when <paramref name="length"/> is less than <c>0</c> or greater than <see cref="Length"/>.
/// </exception>
public unsafe readonly string ToString(int length)
{
if (length < 0 || length > Length) throw new ArgumentOutOfRangeException(nameof(length), length, "Length must be between 0 and the fixed array length.");
fixed (char* p0 = _0)
return new string(p0, 0, length);
}
/// <summary>
/// Copies the fixed array to a new string, stopping before the first null terminator character or at the end of the fixed array (whichever is shorter).
/// </summary>
public override readonly unsafe string ToString()
{
int length;
fixed (char* p = _0)
{
char* pLastExclusive = p + Length;
char* pCh = p;
for (;
pCh < pLastExclusive && *pCh != '\0';
pCh++) ;
length = checked((int)(pCh - p));
}
return ToString(length);
}
}
} |
Beta Was this translation helpful? Give feedback.
-
My workaround uses dynamic size with existing definition: int DevicePathOffset = (int)(nint)Marshal.OffsetOf<SP_DEVICE_INTERFACE_DETAIL_DATA_W>(nameof(SP_DEVICE_INTERFACE_DETAIL_DATA_W.DevicePath));
var buffer = new byte[requiredSize];
fixed (byte* pBuffer = buffer)
{
var deviceInterfaceDetailData = (SP_DEVICE_INTERFACE_DETAIL_DATA_W*)pBuffer;
deviceInterfaceDetailData->cbSize = (uint)sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_W);
if (!SetupDiGetDeviceInterfaceDetail(
diskClassDevices,
deviceInterfaceData,
deviceInterfaceDetailData,
requiredSize,
null,
null))
{
// Error
return;
}
}
var devicePath = System.Text.Encoding.Unicode.GetString(buffer[DevicePathOffset..]); |
Beta Was this translation helpful? Give feedback.
That struct is incorrectly generated at the moment. You can track #387 for resolution of that.