Seems pretty strightforward to marshal, but the larger question is what
problem are you trying to solve? There may be a better, faster,
already-done way to get the info you want.
This code is not working correctly because it can't find the method
"FindFirstDevice", but this is what i have now.
code snippet:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct DEVMGR_DEVICE_INFORMATION
{
public UInt32 dwSize;
public IntPtr hDevice;
public IntPtr hParentDevice;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 6)]
public string szLegacyName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDeviceKey;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDeviceName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szBusName;
}
public enum DeviceSearchType : int
{
DeviceSearchByLegacyName = 0,
DeviceSearchByDeviceName = 1,
DeviceSearchByBusName = 2,
DeviceSearchByGuid = 3,
DeviceSearchByParent = 4
}
[DllImport("coredll.dll", SetLastError = true)]
public static extern IntPtr FindFirstDevice(DeviceSearchType
searchType, IntPtr searchParam, ref DEVMGR_DEVICE_INFORMATION pdi);
In my c# class I try to test it.
void testCode()
{
try
{
DeviceSearchType searchType =
DeviceSearchType.DeviceSearchByDeviceName;
DEVMGR_DEVICE_INFORMATION pdi =
new DEVMGR_DEVICE_INFORMATION();
string searchParamString = "COM*";
IntPtr searchParam = Marshal.AllocHGlobal(searchParamString.Length);
IntPtr searchParam = Marshal.StringToBSTR(searchParamString);
IntPtr deviceHandle = FindFirstDevice(searchType,searchParam, ref pdi);
}
catch (Exception err)
{
}
}
I get the exception "Can't find an Entry Point 'FindFirstDevice' in a
PInvoke DLL 'coredll.dll'."
The "FindFirstDevice" is not found in the "coredll.dll", so I am not sure
on
how to proceed since I do not know which dll to use.