本文整理汇总了C#中DeviceInterfaceData类的典型用法代码示例。如果您正苦于以下问题:C# DeviceInterfaceData类的具体用法?C# DeviceInterfaceData怎么用?C# DeviceInterfaceData使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
DeviceInterfaceData类属于命名空间,在下文中一共展示了DeviceInterfaceData类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FindDevice
public static HidDevice FindDevice(int nVid, int nPid, Type oType)
{
string strPath = string.Empty;
string strSearch = string.Format("vid_{0:x4}&pid_{1:x4}", nVid, nPid);
Guid gHid = HIDGuid;
IntPtr hInfoSet = SetupDiGetClassDevs(ref gHid, null, IntPtr.Zero, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);
try
{
DeviceInterfaceData oInterface = new DeviceInterfaceData();
oInterface.Size = Marshal.SizeOf(oInterface);
int nIndex = 0;
while (SetupDiEnumDeviceInterfaces(hInfoSet, 0, ref gHid, (uint)nIndex, ref oInterface))
{
string strDevicePath = GetDevicePath(hInfoSet, ref oInterface);
if (strDevicePath.IndexOf(strSearch) >= 0)
{
HidDevice oNewDevice = (HidDevice)Activator.CreateInstance(oType);
oNewDevice.Initialise(strDevicePath);
return oNewDevice;
}
nIndex++;
}
}
catch(Exception ex)
{
throw HidDeviceException.GenerateError(ex.ToString());
}
finally
{
SetupDiDestroyDeviceInfoList(hInfoSet);
}
// No device found.
return null;
}
示例2: FindDevice
/// <summary>
/// Finds a device given its PID and VID
/// </summary>
/// <param name="nVid">Vendor id for device (VID)</param>
/// <param name="nPid">Product id for device (PID)</param>
/// <param name="oType">Type of device class to create</param>
/// <returns>A new device class of the given type or null</returns>
public static HIDDevice FindDevice(int nVid, int nPid, Type oType)
{
var searchPath = GetSearchPath(nVid, nPid); // first, build the path search string
Guid gHid;
HidD_GetHidGuid(out gHid); // next, get the GUID from Windows that it uses to represent the HID USB interface
var hInfoSet = SetupDiGetClassDevs(ref gHid, null, IntPtr.Zero, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT); // this gets a list of all HID devices currently connected to the computer (InfoSet)
try
{
var oInterface = new DeviceInterfaceData(); // build up a device interface data block
oInterface.Size = Marshal.SizeOf(oInterface);
// Now iterate through the InfoSet memory block assigned within Windows in the call to SetupDiGetClassDevs
// to get device details for each device connected
for (var i = 0; SetupDiEnumDeviceInterfaces(hInfoSet, 0, ref gHid, (uint)i, ref oInterface); i++) // this gets the device interface information for a device at index 'i' in the memory block
{
var strDevicePath = GetDevicePath(hInfoSet, ref oInterface); // get the device path (see helper method 'GetDevicePath')
if (strDevicePath.IndexOf(searchPath) < 0)
continue;
var oNewDevice = (HIDDevice)Activator.CreateInstance(oType); // create an instance of the class for this device
oNewDevice.Initialise(strDevicePath); // initialise it with the device path
return oNewDevice; // and return it
}
}
finally
{
// Before we go, we have to free up the InfoSet memory reserved by SetupDiGetClassDevs
SetupDiDestroyDeviceInfoList(hInfoSet);
}
return null; // oops, didn't find our device
}
示例3: Find
public static string[] Find(int nVid, int nPid)
{
Guid guid;
List<string> list = new List<string>();
string str = string.Format("vid_{0:x4}&pid_{1:x4}", nVid, nPid);
HidD_GetHidGuid(out guid);
IntPtr lpDeviceInfoSet = SetupDiGetClassDevs(ref guid, null, IntPtr.Zero, 0x12);
try
{
DeviceInterfaceData structure = new DeviceInterfaceData();
structure.Size = Marshal.SizeOf(structure);
for (int i = 0; SetupDiEnumDeviceInterfaces(lpDeviceInfoSet, 0, ref guid, (uint)i, ref structure); i++)
{
string devicePath = GetDevicePath(lpDeviceInfoSet, ref structure);
if (devicePath.IndexOf(str) >= 0)
{
list.Add(devicePath);
}
}
}
finally
{
SetupDiDestroyDeviceInfoList(lpDeviceInfoSet);
}
return list.ToArray();
}
示例4: FindDevice
/// <summary>
/// Finds a device given its PID and VID
/// </summary>
/// <param name="VendorID">Vendor id for device (VID)</param>
/// <param name="ProductID">Product id for device (PID)</param>
/// <param name="oType">Type of device class to create</param>
/// <returns>A new device class of the given type or null</returns>
public static HIDDevice FindDevice(int VendorID, int ProductID, Type oType)
{
string strPath = string.Empty;
string strSearch = string.Format("vid_{0:x4}&pid_{1:x4}", VendorID, ProductID); // first, build the path search string
Guid gHid;
HidD_GetHidGuid(out gHid); // next, get the GUID from Windows that it uses to represent the HID USB interface
IntPtr hInfoSet = SetupDiGetClassDevs(ref gHid, null, IntPtr.Zero, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT); // this gets a list of all HID devices currently connected to the computer (InfoSet)
try
{
DeviceInterfaceData oInterface = new DeviceInterfaceData(); // build up a device interface data block
oInterface.Size = Marshal.SizeOf(oInterface);
// Now iterate through the InfoSet memory block assigned within Windows in the call to SetupDiGetClassDevs
// to get device details for each device connected
int nIndex = 0;
while (SetupDiEnumDeviceInterfaces(hInfoSet, 0, ref gHid, (uint)nIndex, ref oInterface)) // this gets the device interface information for a device at index 'nIndex' in the memory block
{
string strDevicePath = GetDevicePath(hInfoSet, ref oInterface); // get the device path (see helper method 'GetDevicePath')
if (strDevicePath.IndexOf(strSearch) >= 0) // do a string search, if we find the VID/PID string then we found our device!
{
HIDDevice oNewDevice = (HIDDevice)Activator.CreateInstance(oType); // create an instance of the class for this device
oNewDevice.Initialize(strDevicePath); // initialise it with the device path
return oNewDevice; // and return it
}
nIndex++; // if we get here, we didn't find our device. So move on to the next one.
}
}
finally
{
// Before we go, we have to free up the InfoSet memory reserved by SetupDiGetClassDevs
SetupDiDestroyDeviceInfoList(hInfoSet);
}
return null; // oops, didn't find our device
}
示例5: SetupDiGetDeviceInterfaceDetail
public static string SetupDiGetDeviceInterfaceDetail(
SafeDeviceInfoSetHandle lpDeviceInfoSet,
DeviceInterfaceData oInterfaceData,
IntPtr lpDeviceInfoData)
{
var requiredSize = new NullableUInt32();
// First call to get the size to allocate
SetupDiGetDeviceInterfaceDetail(
lpDeviceInfoSet,
ref oInterfaceData,
IntPtr.Zero,
0,
requiredSize,
null);
// As we passed an empty buffer we know that the function will fail, not need to check the result.
var lastError = GetLastError();
if (lastError != Win32ErrorCode.ERROR_INSUFFICIENT_BUFFER)
{
throw new Win32Exception(lastError);
}
var buffer = Marshal.AllocHGlobal((int)requiredSize.Value);
try
{
Marshal.WriteInt32(buffer, DeviceInterfaceDetailDataSize.Value);
// Second call to get the value
var success = SetupDiGetDeviceInterfaceDetail(
lpDeviceInfoSet,
ref oInterfaceData,
buffer,
(uint)requiredSize,
null,
null);
if (!success)
{
Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
return null;
}
var strPtr = new IntPtr(buffer.ToInt64() + 4);
return Marshal.PtrToStringAuto(strPtr);
}
finally
{
Marshal.FreeHGlobal(buffer);
}
}
示例6: GetDevicePath
private static string GetDevicePath(IntPtr hInfoSet, ref DeviceInterfaceData oInterface)
{
uint nRequiredSize = 0;
if (!SetupDiGetDeviceInterfaceDetail(hInfoSet, ref oInterface, IntPtr.Zero, 0, ref nRequiredSize, IntPtr.Zero))
{
DeviceInterfaceDetailData oDetailData = new DeviceInterfaceDetailData();
oDetailData.Size = 5;
if (SetupDiGetDeviceInterfaceDetail(hInfoSet, ref oInterface, ref oDetailData, nRequiredSize, ref nRequiredSize, IntPtr.Zero))
{
return oDetailData.DevicePath;
}
}
return null;
}
示例7: FindDevice
/// <summary>
/// Finds a device given its PID and VID
/// </summary>
/// <param name="nVid">Vendor id for device (VID)</param>
/// <param name="nPid">Product id for device (PID)</param>
/// <param name="oType">Type of device class to create</param>
/// <returns>A new device class of the given type or null</returns>
public static USBDevice FindDevice(int nVid, int nPid, Type oType)
{
string strPath = string.Empty;
string strSearch = string.Format("vid_{0:x4}&pid_{1:x4}", nVid, nPid); // first, build the path search string
Guid gHid = new Guid("{0x28d78fad,0x5a12,0x11D1,{0xae,0x5b,0x00,0x00,0xf8,0x03,0xa8,0xc2}}");
//HidD_GetHidGuid(out gHid); // next, get the GUID from Windows that it uses to represent the HID USB interface
IntPtr hInfoSet = SetupDiGetClassDevs(ref gHid, null, IntPtr.Zero, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);// this gets a list of all HID devices currently connected to the computer (InfoSet)
try
{
DeviceInterfaceData oInterface = new DeviceInterfaceData(); // build up a device interface data block
oInterface.Size = Marshal.SizeOf(oInterface);
// Now iterate through the InfoSet memory block assigned within Windows in the call to SetupDiGetClassDevs
// to get device details for each device connected
int nIndex = 0;
System.Console.WriteLine(gHid.ToString());
System.Console.WriteLine(hInfoSet.ToString());
Boolean Result = SetupDiEnumDeviceInterfaces(hInfoSet, 0, ref gHid, (uint)nIndex, ref oInterface);
System.Console.WriteLine(Result.ToString());
while (SetupDiEnumDeviceInterfaces(hInfoSet, 0, ref gHid, (uint)nIndex, ref oInterface)) // this gets the device interface information for a device at index 'nIndex' in the memory block
{
string strDevicePath = GetDevicePath(hInfoSet, ref oInterface); // get the device path (see helper method 'GetDevicePath')
if (strDevicePath.IndexOf(strSearch) >= 0) // do a string search, if we find the VID/PID string then we found our device!
{
USBDevice oNewDevice = (USBDevice)Activator.CreateInstance(oType); // create an instance of the class for this device
//USBPRINT\VID_088c&PID_2030
oNewDevice.Initialise(strDevicePath); // strDevicePath initialise it with the device path
return oNewDevice; // and return it
}
nIndex++; // if we get here, we didn't find our device. So move on to the next one.
}
System.Console.WriteLine(Marshal.GetLastWin32Error().ToString());
}
finally
{
// Before we go, we have to free up the InfoSet memory reserved by SetupDiGetClassDevs
SetupDiDestroyDeviceInfoList(hInfoSet);
}
return null; // oops, didn't find our device
}
示例8: SetupDiGetDeviceInterfaceDetail
[DllImport("setupapi.dll", SetLastError = true)] protected static extern bool SetupDiGetDeviceInterfaceDetail(IntPtr lpDeviceInfoSet, ref DeviceInterfaceData oInterfaceData, ref DeviceInterfaceDetailData oDetailData, uint nDeviceInterfaceDetailDataSize, ref uint nRequiredSize, IntPtr lpDeviceInfoData);
示例9: SetupDiEnumDeviceInterfaces
/// <summary>
/// Gets the DeviceInterfaceData for a device from an InfoSet.
/// </summary>
/// <param name="lpDeviceInfoSet">InfoSet to access</param>
/// <param name="nDeviceInfoData">Not used</param>
/// <param name="gClass">Device class guid</param>
/// <param name="nIndex">Index into InfoSet for device</param>
/// <param name="oInterfaceData">DeviceInterfaceData to fill with data</param>
/// <returns>True if successful, false if not (e.g. when index is passed end of InfoSet)</returns>
[DllImport("setupapi.dll", SetLastError = true)] protected static extern bool SetupDiEnumDeviceInterfaces(IntPtr lpDeviceInfoSet, uint nDeviceInfoData, ref Guid gClass, uint nIndex, ref DeviceInterfaceData oInterfaceData);
示例10: SetupDiEnumDeviceInterfaces
protected static extern bool SetupDiEnumDeviceInterfaces(IntPtr lpDeviceInfoSet, uint nDeviceInfoData, ref Guid gClass, uint nIndex, ref DeviceInterfaceData oInterfaceData);
示例11: SetupDiEnumDeviceInterfaces
/// <summary>
/// Gets the DeviceInterfaceData for a device from an InfoSet.
/// </summary>
/// <param name="lpDeviceInfoSet">InfoSet to access</param>
/// <param name="nDeviceInfoData">Not used</param>
/// <param name="gClass">Device class guid</param>
/// <param name="nIndex">Index into InfoSet for device</param>
/// <param name="oInterfaceData">DeviceInterfaceData to fill with data</param>
/// <returns>True if successful, false if not (e.g. when index is passed end of InfoSet)</returns>
[DllImport("setupapi.dll", SetLastError = true)] internal static extern bool SetupDiEnumDeviceInterfaces(IntPtr lpDeviceInfoSet, IntPtr nDeviceInfoData, ref Guid gClass, Int32 nIndex, ref DeviceInterfaceData oInterfaceData);
示例12: GetDevicePath
/// <summary>
/// Helper method to return the device path given a DeviceInterfaceData structure and an InfoSet handle.
/// Used in 'FindDevice' so check that method out to see how to get an InfoSet handle and a DeviceInterfaceData.
/// </summary>
/// <param name="hInfoSet">Handle to the InfoSet</param>
/// <param name="oInterface">DeviceInterfaceData structure</param>
/// <returns>The device path or null if there was some problem</returns>
static string GetDevicePath(IntPtr hInfoSet, ref DeviceInterfaceData oInterface)
{
uint nRequiredSize = 0;
// Get the device interface details
if (!SetupDiGetDeviceInterfaceDetail(hInfoSet, ref oInterface, IntPtr.Zero, 0, ref nRequiredSize, IntPtr.Zero))
{
DeviceInterfaceDetailData oDetail = new DeviceInterfaceDetailData();
oDetail.Size = 5; // hardcoded to 5! Sorry, but this works and trying more future proof versions by setting the size to the struct sizeof failed miserably. If you manage to sort it, mail me! Thx
if (SetupDiGetDeviceInterfaceDetail(hInfoSet, ref oInterface, ref oDetail, nRequiredSize, ref nRequiredSize, IntPtr.Zero))
{
return oDetail.DevicePath;
}
}
return null;
}
示例13: SetupDiGetDeviceInterfaceDetail
public bool SetupDiGetDeviceInterfaceDetail(
SafeDeviceInfoSetHandle deviceInfoSet,
ref DeviceInterfaceData deviceInterfaceData,
IntPtr deviceInterfaceDetailData,
uint deviceInterfaceDetailDataSize,
NullableUInt32 requiredSize,
DeviceInfoData deviceInfoData)
=> SetupDiGetDeviceInterfaceDetail(deviceInfoSet, ref deviceInterfaceData, deviceInterfaceDetailData, deviceInterfaceDetailDataSize, requiredSize, deviceInfoData);
示例14: GetDevicePath
private static string GetDevicePath(IntPtr hInfoSet, ref DeviceInterfaceData oInterface)
{
uint nRequiredSize = 0;
if (!SetupDiGetDeviceInterfaceDetail(hInfoSet, ref oInterface, IntPtr.Zero, 0, ref nRequiredSize, IntPtr.Zero))
{
DeviceInterfaceDetailData oDetail = new DeviceInterfaceDetailData();
if (Marshal.SizeOf(typeof(IntPtr)) == 8)
{
oDetail.Size = 8;
}
else
{
oDetail.Size = 5;
}
if (SetupDiGetDeviceInterfaceDetail(hInfoSet, ref oInterface, ref oDetail, nRequiredSize, ref nRequiredSize, IntPtr.Zero))
{
return oDetail.DevicePath;
}
}
return null;
}
示例15: FindDevice
/// <summary>
/// Finds a device given its PID and VID
/// </summary>
/// <param name="nVid">Vendor id for device (VID)</param>
/// <param name="nPid">Product id for device (PID)</param>
/// <param name="oType">Type of device class to create</param>
/// <returns>A new device class of the given type or null</returns>
public static HIDDevice FindDevice(int nVid, int nPid, Type oType)
{
string strPath = string.Empty;
string strSearch = @"#vid_" + nVid.ToString("x4") + "&pid_" + nPid.ToString("x4") + "#";
//string strSearch = string.Format("VID_{0:X4}&PID_{1:X4}", nVid, nPid); // first, build the path search string
Guid gHid = HIDGuid;
HidD_GetHidGuid(ref gHid); // next, get the GUID from Windows that it uses to represent the HID USB interface
IntPtr hInfoSet = SetupDiGetClassDevs(ref gHid, IntPtr.Zero, IntPtr.Zero, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT); // this gets a list of all HID devices currently connected to the computer (InfoSet)
try
{
var oInterface = new DeviceInterfaceData(); // build up a device interface data block
oInterface.Size = Marshal.SizeOf(oInterface);
//oInterface.Size = (IntPtr.Size == 4) ? (4 + Marshal.SystemDefaultCharSize) : 8;
// Now iterate through the InfoSet memory block assigned within Windows in the call to SetupDiGetClassDevs
// to get device details for each device connected
int nIndex = 0;
while (SetupDiEnumDeviceInterfaces(hInfoSet, IntPtr.Zero, ref gHid, nIndex, ref oInterface)) // this gets the device interface information for a device at index 'nIndex' in the memory block
{
string strDevicePath = GetDevicePath(hInfoSet, ref oInterface); // get the device path (see helper method 'GetDevicePath')
if (strDevicePath.IndexOf(strSearch) >= 0) // do a string search, if we find the VID/PID string then we found our device!
{
HIDDevice oNewDevice = (HIDDevice)Activator.CreateInstance(oType); // create an instance of the class for this device
oNewDevice.Initialise(strDevicePath); // initialise it with the device path
return oNewDevice; // and return it
}
nIndex++; // if we get here, we didn't find our device. So move on to the next one.
}
}
catch(Exception ex)
{
throw HIDDeviceException.GenerateError(ex.ToString());
//Console.WriteLine(ex.ToString());
}
finally
{
// Before we go, we have to free up the InfoSet memory reserved by SetupDiGetClassDevs
SetupDiDestroyDeviceInfoList(hInfoSet);
}
return null; // oops, didn't find our device
}