public static String getDevicePath(IntPtr hardwareDeviceInfoPtr, ref DeviceInterfaceData did) { uint size = 0; if (!SetupApi.ClassDev.SetupDiGetDeviceInterfaceDetail(hardwareDeviceInfoPtr, ref did, IntPtr.Zero, 0, ref size, IntPtr.Zero)) { DeviceInterfaceData da = new DeviceInterfaceData(); da.size = (uint)Marshal.SizeOf(da); DeviceInterfaceDetailData detail = new DeviceInterfaceDetailData(); if (IntPtr.Size == 8) { detail.size = 8; } else { detail.size = 4 + (uint)Marshal.SystemDefaultCharSize; } if (SetupApi.ClassDev.SetupDiGetDeviceInterfaceDetail(hardwareDeviceInfoPtr, ref did, ref detail, size, out size, ref da)) { return(detail.DevicePath); } Error.GetLastError(); } return(null); }
/// <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> private 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(); if (Marshal.SizeOf(typeof(IntPtr)) == 8) { oDetail.Size = 8; // For 64 bit. } else { oDetail.Size = 5; // hardcoded to 5! (I think this correctly handles 32 bit systems, but not yet tested) } if (SetupDiGetDeviceInterfaceDetail(hInfoSet, ref oInterface, ref oDetail, nRequiredSize, ref nRequiredSize, IntPtr.Zero)) { return(oDetail.DevicePath); } } return(null); }
public static extern bool SetupDiGetDeviceInterfaceDetail( IntPtr lpDeviceInfoSet, ref DeviceInterfaceData oInterfaceData, ref DeviceInterfaceDetailData oDetailData, //We have the size -> send struct uint nDeviceInterfaceDetailDataSize, ref uint nRequiredSize, ref DevinfoData deviceInfoData);
private static extern bool SetupDiGetDeviceInterfaceDetail( IntPtr handle, ref DeviceInterfaceData deviceInterfaceData, ref DeviceInterfaceDetailData deviceInterfaceDetailData, uint detailSize, IntPtr unused1, IntPtr unused2);
/// <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> private static string GetDevicePath(IntPtr hInfoSet, ref DeviceInterfaceData oInterface) { DeviceInterfaceDetailData oDetail = new DeviceInterfaceDetailData(); // Size workaround if (IntPtr.Size == 8) { oDetail.Size = 8; } else { oDetail.Size = 5; } Console.WriteLine("Size of struct: {0}", Marshal.SizeOf(oDetail)); // 4 + 256 = 260 uint nRequiredSize = 0; // Error 0 if (!SetupDiGetDeviceInterfaceDetail(hInfoSet, ref oInterface, IntPtr.Zero, 0, ref nRequiredSize, IntPtr.Zero)) { // Error 122 - ERROR_INSUFFICIENT_BUFFER (not a problem, just used to set nRequiredSize) if (SetupDiGetDeviceInterfaceDetail(hInfoSet, ref oInterface, ref oDetail, nRequiredSize, ref nRequiredSize, IntPtr.Zero)) { return(oDetail.DevicePath); } } // Error 1784 - ERROR_INVALID_USER_BUFFER (unless size=5 on 32bit, size=8 on 64bit) return(null); }
public static extern Boolean SetupDiGetDeviceInterfaceDetail( IntPtr hDevInfo, ref DeviceInterfaceData deviceInterfaceData, ref DeviceInterfaceDetailData deviceInterfaceDetailData, UInt32 deviceInterfaceDetailDataSize, out UInt32 requiredSize, ref DeviceInterfaceData deviceInfoData );
public static extern Boolean SetupDiGetDeviceInterfaceDetail( IntPtr hDevInfo, ref DeviceInterfaceData deviceInterfaceData, ref DeviceInterfaceDetailData deviceInterfaceDetailData, uint deviceInterfaceDetailDataSize, out uint requiredSize, IntPtr deviceInfoData );
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; }
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(); oDetail.Size = (IntPtr.Size == 8) ? 8 : 5; if (SetupDiGetDeviceInterfaceDetail(hInfoSet, ref oInterface, ref oDetail, nRequiredSize, ref nRequiredSize, IntPtr.Zero)) { return(oDetail.DevicePath); } } return(null); }
/// <summary> /// The get device paths. /// </summary> /// <param name="pointer"> /// The pointer. /// </param> /// <param name="oInterface"> /// The o interface. /// </param> /// <returns> /// The <see cref="string"/>. /// </returns> internal static string GetDevicePaths(ref IntPtr pointer, ref SpDeviceInterfaceData oInterface) { uint requiredSize = 0; SetupDiGetDeviceInterfaceDetail(pointer, ref oInterface, IntPtr.Zero, 0, ref requiredSize, IntPtr.Zero); var detail = new DeviceInterfaceDetailData { Size = Marshal.SizeOf(typeof(IntPtr)) == 8 ? 8 : 5 }; SetupDiGetDeviceInterfaceDetail(pointer, ref oInterface, ref detail, requiredSize, ref requiredSize, IntPtr.Zero); return(detail.DevicePath.Clone().ToString()); }
/// <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> private 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); }
/// <summary> /// Get USB HID device path /// </summary> private static string GetDevicePath(IntPtr hInfoSet, ref DeviceInterfaceData oInterface) { uint nRequiredSize = 0; if (SetupDiGetDeviceInterfaceDetail(hInfoSet, ref oInterface, IntPtr.Zero, 0, ref nRequiredSize, IntPtr.Zero) == false) { // TODO: Find a solution // Tip: http://stackoverflow.com/questions/1054748/setupdigetdeviceinterfacedetail-unexplainable-error //throw new HIDDeviceException(); } var oDetail = new DeviceInterfaceDetailData(); oDetail.Size = Marshal.SizeOf(typeof(IntPtr)) == 8 ? 8 : 5; // x86/x64 magic... if (SetupDiGetDeviceInterfaceDetail(hInfoSet, ref oInterface, ref oDetail, nRequiredSize, ref nRequiredSize, IntPtr.Zero) == false) { throw new Win32Exception(); } return(oDetail.DevicePath); }
static string GetDevicePath(IntPtr infoSetPtr, ref DeviceInterfaceData interfaceData) { var interfaceDetailData = new DeviceInterfaceDetailData { Size = Environment.Is64BitProcess ? 8 : 5 }; // Query needed structure size uint requiredSize = 0; if (SetupDiGetDeviceInterfaceDetail(infoSetPtr, ref interfaceData, IntPtr.Zero, 0, ref requiredSize, IntPtr.Zero)) { return(string.Empty); // Device has no path (because request was successful without memory for the path). // Just skip it. Theoretically, there should be not such devices. } // Check we have enough space in details structure if (requiredSize > Marshal.SizeOf(interfaceDetailData)) { var errorMessage = string.Format(Errors.Insufficient_Path_Buffer_0, requiredSize - Marshal.SizeOf(interfaceDetailData.Size)); throw new InsufficientMemoryException(errorMessage) { Source = "WinUSB.SetupDiGetDeviceInterfaceDetail" }; } // Request device path if (SetupDiGetDeviceInterfaceDetail(infoSetPtr, ref interfaceData, ref interfaceDetailData, requiredSize, ref requiredSize, IntPtr.Zero)) { return(interfaceDetailData.DevicePath); } throw new Win32Exception(Marshal.GetLastWin32Error()) { Source = "WinUSB.SetupDiGetDeviceInterfaceDetail" }; }
private static string smethod_0(IntPtr intptr_0, ref DeviceInterfaceData deviceInterfaceData_0) { uint nDeviceInterfaceDetailDataSize = 0u; if (!Win32Usb.SetupDiGetDeviceInterfaceDetail(intptr_0, ref deviceInterfaceData_0, IntPtr.Zero, 0u, ref nDeviceInterfaceDetailDataSize, IntPtr.Zero)) { DeviceInterfaceDetailData deviceInterfaceDetailData = default(DeviceInterfaceDetailData); if (IntPtr.Size == 4) { deviceInterfaceDetailData.Size = 5; } else { deviceInterfaceDetailData.Size = 8; } if (Win32Usb.SetupDiGetDeviceInterfaceDetail(intptr_0, ref deviceInterfaceData_0, ref deviceInterfaceDetailData, nDeviceInterfaceDetailDataSize, ref nDeviceInterfaceDetailDataSize, IntPtr.Zero)) { return(deviceInterfaceDetailData.DevicePath); } } return(null); }
/// <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> private 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(); if (IntPtr.Size == 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); }
protected string FindDevice(Guid classGuid) { if (Transceivers != null) { _eHomeTransceivers = Transceivers.Select(t => t.DeviceID); } IntPtr handle = SetupDiGetClassDevs(ref classGuid, 0, 0, 0x12); string devicePath = null; if (handle.ToInt32() == -1) { throw new Exception(string.Format("Failed in call to SetupDiGetClassDevs ({0})", GetLastError())); } for (int deviceIndex = 0;; deviceIndex++) { DeviceInfoData deviceInfoData = new DeviceInfoData(); deviceInfoData.Size = Marshal.SizeOf(deviceInfoData); if (SetupDiEnumDeviceInfo(handle, deviceIndex, ref deviceInfoData) == false) { // out of devices or do we have an error? if (GetLastError() != 0x103 && GetLastError() != 0x7E) { SetupDiDestroyDeviceInfoList(handle); throw new Exception(string.Format("Failed in call to SetupDiEnumDeviceInfo ({0})", GetLastError())); } SetupDiDestroyDeviceInfoList(handle); break; } DeviceInterfaceData deviceInterfaceData = new DeviceInterfaceData(); deviceInterfaceData.Size = Marshal.SizeOf(deviceInterfaceData); if (SetupDiEnumDeviceInterfaces(handle, ref deviceInfoData, ref classGuid, 0, ref deviceInterfaceData) == false) { SetupDiDestroyDeviceInfoList(handle); throw new Exception(string.Format("Failed in call to SetupDiEnumDeviceInterfaces ({0})", GetLastError())); } uint cbData = 0; if (SetupDiGetDeviceInterfaceDetail(handle, ref deviceInterfaceData, 0, 0, ref cbData, 0) == false && cbData == 0) { SetupDiDestroyDeviceInfoList(handle); throw new Exception(string.Format("Failed in call to SetupDiGetDeviceInterfaceDetail ({0})", GetLastError())); } DeviceInterfaceDetailData deviceInterfaceDetailData = new DeviceInterfaceDetailData(); deviceInterfaceDetailData.Size = 5; if ( SetupDiGetDeviceInterfaceDetail(handle, ref deviceInterfaceData, ref deviceInterfaceDetailData, cbData, 0, 0) == false) { SetupDiDestroyDeviceInfoList(handle); throw new Exception(string.Format("Failed in call to SetupDiGetDeviceInterfaceDetail ({0})", GetLastError())); } if (LogVerbose) { MceRemoteReceiver.LogInfo("Found: {0}", deviceInterfaceDetailData.DevicePath); } foreach (string deviceId in _eHomeTransceivers) { if ((deviceInterfaceDetailData.DevicePath.IndexOf(deviceId) != -1) || (deviceInterfaceDetailData.DevicePath.StartsWith(@"\\?\hid#irdevice&col01#2")) || // eHome Infrared Transceiver List XP (deviceInterfaceDetailData.DevicePath.StartsWith(@"\\?\hid#irdevicev2&col01#2"))) // Microsoft/Philips 2005 (Vista) { SetupDiDestroyDeviceInfoList(handle); devicePath = deviceInterfaceDetailData.DevicePath; } } if (devicePath != null) { break; } } return(devicePath); }
internal static extern bool SetupDiGetDeviceInterfaceDetail(IntPtr deviceInfoSet, ref DeviceInterfaceData deviceInterfaceData, ref DeviceInterfaceDetailData deviceInterfaceDetailData, int deviceInterfaceDetailDataSize, ref int requiredSize, IntPtr deviceInfoData);
/// <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> private static string GetDevicePath(IntPtr hInfoSet, ref DeviceInterfaceData oInterface) { DeviceInterfaceDetailData oDetail = new DeviceInterfaceDetailData(); // Size workaround if (IntPtr.Size == 8) oDetail.Size = 8; else oDetail.Size = 5; Console.WriteLine("Size of struct: {0}", Marshal.SizeOf(oDetail)); // 4 + 256 = 260 uint nRequiredSize = 0; // Error 0 if (!SetupDiGetDeviceInterfaceDetail(hInfoSet, ref oInterface, IntPtr.Zero, 0, ref nRequiredSize, IntPtr.Zero)) // Error 122 - ERROR_INSUFFICIENT_BUFFER (not a problem, just used to set nRequiredSize) if (SetupDiGetDeviceInterfaceDetail(hInfoSet, ref oInterface, ref oDetail, nRequiredSize, ref nRequiredSize, IntPtr.Zero)) return oDetail.DevicePath; // Error 1784 - ERROR_INVALID_USER_BUFFER (unless size=5 on 32bit, size=8 on 64bit) return null; }
private static string GetDeviceName() { 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 { uint SPDRP_DEVICEDESC = 0x00000000; uint SPDRP_DRIVER = 0x00000009; int BUFFER_SIZE = 256; bool Success = true; int i = 0; while (Success) { // create a Device Interface Data structure DeviceInterfaceData oInterface = new DeviceInterfaceData(); // build up a device interface data block oInterface.Size = Marshal.SizeOf(oInterface); // start the enumeration Success = SetupDiEnumDeviceInterfaces(hInfoSet, IntPtr.Zero, ref gHid, (uint)i, ref oInterface); if (Success) { // build a Device Interface Detail Data structure DeviceInterfaceDetailData oDetail = new DeviceInterfaceDetailData(); oDetail.Size = (IntPtr.Size == 4) ? 5 : 8; // 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 DeviceInfoData da = new DeviceInfoData(); da.Size = (uint)Marshal.SizeOf(da); // now we can get some more detailed information uint nRequiredSize = 0; int nBytes = BUFFER_SIZE; //hInfoSet, ref oInterface, ref oDetail, nRequiredSize, ref nRequiredSize, IntPtr.Zero; if (SetupDiGetDeviceInterfaceDetail(hInfoSet, ref oInterface, ref oDetail, (uint)nBytes, ref nRequiredSize, ref da)) { // get the Device Description and DriverKeyName uint RequiredSize; uint RegType; byte[] ptrBuf = new byte[BUFFER_SIZE]; if (SetupDiGetDeviceRegistryProperty(hInfoSet, ref oInterface, SPDRP_DEVICEDESC, out RegType, ptrBuf, (uint)BUFFER_SIZE, out RequiredSize)) { string ControllerDeviceDesc = System.Text.Encoding.UTF8.GetString(ptrBuf);// Marshal.PtrToStringAuto(ptrBuf); } if (SetupDiGetDeviceRegistryProperty(hInfoSet, ref oInterface, SPDRP_DRIVER, out RegType, ptrBuf, (uint)BUFFER_SIZE, out RequiredSize)) { string ControllerDriverKeyName = System.Text.Encoding.UTF8.GetString(ptrBuf); } } } i++; } } finally { // Before we go, we have to free up the InfoSet memory reserved by SetupDiGetClassDevs SetupDiDestroyDeviceInfoList(hInfoSet); } return(""); }
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; }
/// <summary> /// Find the device path for the supplied Device Class Guid. /// </summary> /// <param name="classGuid">GUID to locate device with.</param> /// <returns>Device path.</returns> public static string Find(Guid classGuid) { IntPtr handle = SetupDiGetClassDevs(ref classGuid, null, IntPtr.Zero, Digcfs.DeviceInterface | Digcfs.Present); if (handle.ToInt32() == -1) return null; for (int deviceIndex = 0;; deviceIndex++) { DeviceInfoData deviceInfoData = new DeviceInfoData(); deviceInfoData.Size = Marshal.SizeOf(deviceInfoData); if (!SetupDiEnumDeviceInfo(handle, deviceIndex, ref deviceInfoData)) { int lastError = Marshal.GetLastWin32Error(); // out of devices or do we have an error? if (lastError != ErrorNoMoreItems && lastError != ErrorModNotFound) { SetupDiDestroyDeviceInfoList(handle); throw new Win32Exception(lastError); } SetupDiDestroyDeviceInfoList(handle); break; } DeviceInterfaceData deviceInterfaceData = new DeviceInterfaceData(); deviceInterfaceData.Size = Marshal.SizeOf(deviceInterfaceData); if (!SetupDiEnumDeviceInterfaces(handle, ref deviceInfoData, ref classGuid, 0, ref deviceInterfaceData)) { SetupDiDestroyDeviceInfoList(handle); throw new Win32Exception(Marshal.GetLastWin32Error()); } uint cbData = 0; if ( !SetupDiGetDeviceInterfaceDetail(handle, ref deviceInterfaceData, IntPtr.Zero, 0, ref cbData, IntPtr.Zero) && cbData == 0) { SetupDiDestroyDeviceInfoList(handle); throw new Win32Exception(Marshal.GetLastWin32Error()); } DeviceInterfaceDetailData deviceInterfaceDetailData = new DeviceInterfaceDetailData(); if (IntPtr.Size == 8) deviceInterfaceDetailData.Size = 8; else deviceInterfaceDetailData.Size = 5; if ( !SetupDiGetDeviceInterfaceDetail(handle, ref deviceInterfaceData, ref deviceInterfaceDetailData, cbData, IntPtr.Zero, IntPtr.Zero)) { SetupDiDestroyDeviceInfoList(handle); throw new Win32Exception(Marshal.GetLastWin32Error()); } if (!String.IsNullOrEmpty(deviceInterfaceDetailData.DevicePath)) { SetupDiDestroyDeviceInfoList(handle); return deviceInterfaceDetailData.DevicePath; } } return null; }
private static string GetDeviceName() { 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 { uint SPDRP_DEVICEDESC = 0x00000000; uint SPDRP_DRIVER = 0x00000009; int BUFFER_SIZE = 256; bool Success = true; int i = 0; while (Success) { // create a Device Interface Data structure DeviceInterfaceData oInterface = new DeviceInterfaceData(); // build up a device interface data block oInterface.Size = Marshal.SizeOf(oInterface); // start the enumeration Success = SetupDiEnumDeviceInterfaces(hInfoSet, IntPtr.Zero, ref gHid, (uint)i, ref oInterface); if (Success) { // build a Device Interface Detail Data structure DeviceInterfaceDetailData oDetail = new DeviceInterfaceDetailData(); oDetail.Size = (IntPtr.Size == 4) ? 5 : 8; // 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 DeviceInfoData da = new DeviceInfoData(); da.Size = (uint)Marshal.SizeOf(da); // now we can get some more detailed information uint nRequiredSize = 0; int nBytes = BUFFER_SIZE; //hInfoSet, ref oInterface, ref oDetail, nRequiredSize, ref nRequiredSize, IntPtr.Zero; if (SetupDiGetDeviceInterfaceDetail(hInfoSet, ref oInterface, ref oDetail, (uint)nBytes, ref nRequiredSize, ref da)) { // get the Device Description and DriverKeyName uint RequiredSize; uint RegType; byte[] ptrBuf = new byte[BUFFER_SIZE]; if (SetupDiGetDeviceRegistryProperty(hInfoSet, ref oInterface, SPDRP_DEVICEDESC, out RegType, ptrBuf, (uint)BUFFER_SIZE, out RequiredSize)) { string ControllerDeviceDesc = System.Text.Encoding.UTF8.GetString(ptrBuf);// Marshal.PtrToStringAuto(ptrBuf); } if (SetupDiGetDeviceRegistryProperty(hInfoSet, ref oInterface, SPDRP_DRIVER, out RegType, ptrBuf, (uint)BUFFER_SIZE, out RequiredSize)) { string ControllerDriverKeyName = System.Text.Encoding.UTF8.GetString(ptrBuf); } } } i++; } } finally { // Before we go, we have to free up the InfoSet memory reserved by SetupDiGetClassDevs SetupDiDestroyDeviceInfoList(hInfoSet); } return ""; }
private IntPtr OpenDFUDevice() { Guid GUID = GUID_DFU; DeviceInterfaceData ifData = new DeviceInterfaceData(); ifData.Size = Marshal.SizeOf(ifData); DeviceInterfaceDetailData ifDetail = new DeviceInterfaceDetailData(); UInt32 Size = 0; IntPtr hInfoSet = SetupDiGetClassDevs(ref GUID, null, IntPtr.Zero, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT); // this gets a list of all DFU devices currently connected to the computer (InfoSet) IntPtr hDevice = IntPtr.Zero; if (hInfoSet == INVALID_HANDLE_VALUE) { throw new Exception("Could not open DFU device!"); //throw new Exception("SetupDiGetClassDevs returned error=" + Marshal.GetLastWin32Error().ToString()); } // Loop ten times hoping to find exactly one DFU device int i = 10; uint Index = 0; while (i-- > 0) { Index = 0; while (SetupDiEnumDeviceInterfaces(hInfoSet, 0, ref GUID, Index, ref ifData)) { Index++; } if (0 == Index) { Thread.Sleep(500); } else { break; } } if (1 == Index) { SetupDiEnumDeviceInterfaces(hInfoSet, 0, ref GUID, 0, ref ifData); SetupDiGetDeviceInterfaceDetail(hInfoSet, ref ifData, IntPtr.Zero, 0, ref Size, IntPtr.Zero); if (IntPtr.Size == 8) // If we are compiled as 64bit { ifDetail.Size = 8; } else if (IntPtr.Size == 4) // If we are compiled as 32 bit { ifDetail.Size = 5; } if (Marshal.SizeOf(ifDetail) < Size) { throw new Exception("Could not open DFU device!"); //throw new Exception("ifDetail too small"); } if (true == SetupDiGetDeviceInterfaceDetail(hInfoSet, ref ifData, ref ifDetail, Size, ref Size, IntPtr.Zero)) { string DevicePath = ifDetail.DevicePath.ToUpper(); if (STDFU.STDFU_Open(DevicePath, out hDevice) != STDFU.STDFU_NOERROR) { throw new Exception("Could not open DFU device!"); } } } else { throw new Exception("There must be exactly one DFU device attached to the computer!"); } SetupDiDestroyDeviceInfoList(hInfoSet); return(hDevice); }
/// <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; }
//public static FileStream Open(string tSerial, string tMan) public static FileStream Open(UInt16 vid, UInt16 pid, int report_length) { FileStream devFile = null; Guid gHid; HidD_GetHidGuid(out gHid); // create list of HID devices present right now var hInfoSet = SetupDiGetClassDevs(ref gHid, null, IntPtr.Zero, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT); var iface = new DeviceInterfaceData(); // allocate mem for interface descriptor iface.Size = Marshal.SizeOf(iface); // set size field uint index = 0; // interface index // Enumerate all interfaces with HID GUID while (SetupDiEnumDeviceInterfaces(hInfoSet, 0, ref gHid, index, ref iface)) { var detIface = new DeviceInterfaceDetailData(); // detailed interface information uint reqSize = (uint)Marshal.SizeOf(detIface); // required size detIface.Size = Marshal.SizeOf(typeof(IntPtr)) == 8 ? 8 : 5; // Size depends on arch (32 / 64 bit), distinguish by IntPtr size // get device path SetupDiGetDeviceInterfaceDetail(hInfoSet, ref iface, ref detIface, reqSize, ref reqSize, IntPtr.Zero); var path = detIface.DevicePath; System.Console.WriteLine("Path: {0}", path); // Open filehandle to device var handle = CreateFile(path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, IntPtr.Zero); if (handle == INVALID_HANDLE_VALUE) { //System.Console.WriteLine("Invalid handle"); index++; continue; } IntPtr lpData; HidDAttributes pAttributes = new HidDAttributes(); if (HidD_GetPreparsedData(handle, out lpData)) { HidCaps oCaps; HidP_GetCaps(lpData, out oCaps); // extract the device capabilities from the internal buffer int inp = oCaps.InputReportByteLength; // get the input... int outp = oCaps.OutputReportByteLength; // ... and output report length HidD_FreePreparsedData(ref lpData); System.Console.WriteLine("Input: {0}, Output: {1}", inp, outp); // we have report length matching our input / output report, so we create a device file in each case if (inp == report_length && outp == report_length) { HidD_GetAttributes(handle, ref pAttributes); //Check PID&VID if (pAttributes.ProductID == pid && pAttributes.VendorID == vid) { var shandle = new SafeFileHandle(handle, false); devFile = new FileStream(shandle, FileAccess.Read | FileAccess.Write, 32, true); break; } } } index++; } SetupDiDestroyDeviceInfoList(hInfoSet); return(devFile); }
/// <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> private 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)) { var oDetail = new DeviceInterfaceDetailData {Size = Marshal.SizeOf(typeof (IntPtr)) == 8 ? 8 : 5}; if (SetupDiGetDeviceInterfaceDetail(hInfoSet, ref oInterface, ref oDetail, nRequiredSize, ref nRequiredSize, IntPtr.Zero)) { return oDetail.DevicePath; } } return null; }
protected static extern bool SetupDiGetDeviceInterfaceDetail(IntPtr handle, ref DeviceInterfaceData deviceInterfaceData, ref DeviceInterfaceDetailData deviceInterfaceDetailData, uint detailSize, int unused1, int unused2);
/// <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> private 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(); if (IntPtr.Size == 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; }
public static extern bool SetupDiGetDeviceInterfaceDetail( IntPtr lpDeviceInfoSet, ref DeviceInterfaceData oInterfaceData, ref DeviceInterfaceDetailData oDetailData, //We have the size -> send struct uint nDeviceInterfaceDetailDataSize, ref uint nRequiredSize, ref DevinfoData deviceInfoData);
protected static extern bool SetupDiGetDeviceInterfaceDetail(IntPtr handle, ref DeviceInterfaceData deviceInterfaceData, ref DeviceInterfaceDetailData deviceInterfaceDetailData, uint detailSize, int unused1, int unused2);
protected static string FindDevice(Guid classGuid) { IntPtr handle = SetupDiGetClassDevs(ref classGuid, 0, 0, 0x12); string devicePath = null; if (handle.ToInt32() == -1) { throw new Exception(string.Format("Failed in call to SetupDiGetClassDevs ({0})", GetLastError())); } for (int deviceIndex = 0;; deviceIndex++) { DeviceInfoData deviceInfoData = new DeviceInfoData(); deviceInfoData.Size = Marshal.SizeOf(deviceInfoData); if (SetupDiEnumDeviceInfo(handle, deviceIndex, ref deviceInfoData) == false) { // out of devices or do we have an error? if (GetLastError() != 0x103 && GetLastError() != 0x7E) { SetupDiDestroyDeviceInfoList(handle); throw new Exception(string.Format("Failed in call to SetupDiEnumDeviceInfo ({0})", GetLastError())); } SetupDiDestroyDeviceInfoList(handle); break; } DeviceInterfaceData deviceInterfaceData = new DeviceInterfaceData(); deviceInterfaceData.Size = Marshal.SizeOf(deviceInterfaceData); if (SetupDiEnumDeviceInterfaces(handle, ref deviceInfoData, ref classGuid, 0, ref deviceInterfaceData) == false) { SetupDiDestroyDeviceInfoList(handle); throw new Exception(string.Format("Failed in call to SetupDiEnumDeviceInterfaces ({0})", GetLastError())); } uint cbData = 0; if (SetupDiGetDeviceInterfaceDetail(handle, ref deviceInterfaceData, 0, 0, ref cbData, 0) == false && cbData == 0) { SetupDiDestroyDeviceInfoList(handle); throw new Exception(string.Format("Failed in call to SetupDiGetDeviceInterfaceDetail ({0})", GetLastError())); } DeviceInterfaceDetailData deviceInterfaceDetailData = new DeviceInterfaceDetailData(); deviceInterfaceDetailData.Size = 5; if ( SetupDiGetDeviceInterfaceDetail(handle, ref deviceInterfaceData, ref deviceInterfaceDetailData, cbData, 0, 0) == false) { SetupDiDestroyDeviceInfoList(handle); throw new Exception(string.Format("Failed in call to SetupDiGetDeviceInterfaceDetail ({0})", GetLastError())); } if (deviceInterfaceDetailData.DevicePath.IndexOf("#vid_0471&pid_0815") != -1) { SetupDiDestroyDeviceInfoList(handle); devicePath = deviceInterfaceDetailData.DevicePath; break; } if (deviceInterfaceDetailData.DevicePath.IndexOf("#vid_045e&pid_006d") != -1) { SetupDiDestroyDeviceInfoList(handle); devicePath = deviceInterfaceDetailData.DevicePath; break; } if (deviceInterfaceDetailData.DevicePath.IndexOf("#vid_1460&pid_9150") != -1) { SetupDiDestroyDeviceInfoList(handle); devicePath = deviceInterfaceDetailData.DevicePath; break; } if (deviceInterfaceDetailData.DevicePath.IndexOf("#vid_0609&pid_031d") != -1) { SetupDiDestroyDeviceInfoList(handle); devicePath = deviceInterfaceDetailData.DevicePath; break; } if (deviceInterfaceDetailData.DevicePath.IndexOf("#vid_03ee&pid_2501") != -1) { SetupDiDestroyDeviceInfoList(handle); devicePath = deviceInterfaceDetailData.DevicePath; break; } } return devicePath; }
/// <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> private 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(); bool is64BitProcess; IsWow64Process(Process.GetCurrentProcess().Handle, out is64BitProcess); oDetail.Size = (IntPtr.Size == 8 || (IntPtr.Size == 4 && is64BitProcess)) ? 8 : 5; if (SetupDiGetDeviceInterfaceDetail(hInfoSet, ref oInterface, ref oDetail, nRequiredSize, ref nRequiredSize, IntPtr.Zero)) { return oDetail.DevicePath; } } return null; }
public static extern bool SetupDiGetDeviceInterfaceDetail( IntPtr lpDeviceInfoSet, ref DeviceInterfaceData oInterfaceData, ref DeviceInterfaceDetailData oDetailData, uint nDeviceInterfaceDetailDataSize, ref uint nRequiredSize, IntPtr lpDeviceInfoData);
[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);
protected string FindDevice(Guid classGuid) { if (Transceivers != null) _eHomeTransceivers = Transceivers.Select(t => t.DeviceID); IntPtr handle = SetupDiGetClassDevs(ref classGuid, 0, 0, 0x12); string devicePath = null; if (handle.ToInt32() == -1) { throw new Exception(string.Format("Failed in call to SetupDiGetClassDevs ({0})", GetLastError())); } for (int deviceIndex = 0;; deviceIndex++) { DeviceInfoData deviceInfoData = new DeviceInfoData(); deviceInfoData.Size = Marshal.SizeOf(deviceInfoData); if (SetupDiEnumDeviceInfo(handle, deviceIndex, ref deviceInfoData) == false) { // out of devices or do we have an error? if (GetLastError() != 0x103 && GetLastError() != 0x7E) { SetupDiDestroyDeviceInfoList(handle); throw new Exception(string.Format("Failed in call to SetupDiEnumDeviceInfo ({0})", GetLastError())); } SetupDiDestroyDeviceInfoList(handle); break; } DeviceInterfaceData deviceInterfaceData = new DeviceInterfaceData(); deviceInterfaceData.Size = Marshal.SizeOf(deviceInterfaceData); if (SetupDiEnumDeviceInterfaces(handle, ref deviceInfoData, ref classGuid, 0, ref deviceInterfaceData) == false) { SetupDiDestroyDeviceInfoList(handle); throw new Exception(string.Format("Failed in call to SetupDiEnumDeviceInterfaces ({0})", GetLastError())); } uint cbData = 0; if (SetupDiGetDeviceInterfaceDetail(handle, ref deviceInterfaceData, 0, 0, ref cbData, 0) == false && cbData == 0) { SetupDiDestroyDeviceInfoList(handle); throw new Exception(string.Format("Failed in call to SetupDiGetDeviceInterfaceDetail ({0})", GetLastError())); } DeviceInterfaceDetailData deviceInterfaceDetailData = new DeviceInterfaceDetailData(); deviceInterfaceDetailData.Size = 5; if ( SetupDiGetDeviceInterfaceDetail(handle, ref deviceInterfaceData, ref deviceInterfaceDetailData, cbData, 0, 0) == false) { SetupDiDestroyDeviceInfoList(handle); throw new Exception(string.Format("Failed in call to SetupDiGetDeviceInterfaceDetail ({0})", GetLastError())); } if (LogVerbose) { MceRemoteReceiver.LogInfo("Found: {0}", deviceInterfaceDetailData.DevicePath); } foreach (string deviceId in _eHomeTransceivers) { if ((deviceInterfaceDetailData.DevicePath.IndexOf(deviceId) != -1) || (deviceInterfaceDetailData.DevicePath.StartsWith(@"\\?\hid#irdevice&col01#2")) || // eHome Infrared Transceiver List XP (deviceInterfaceDetailData.DevicePath.StartsWith(@"\\?\hid#irdevicev2&col01#2"))) // Microsoft/Philips 2005 (Vista) { SetupDiDestroyDeviceInfoList(handle); devicePath = deviceInterfaceDetailData.DevicePath; } } if (devicePath != null) { break; } } return devicePath; }
public static extern bool SetupDiGetDeviceInterfaceDetail( IntPtr handle, ref DeviceInterfaceData deviceInterfaceData, ref DeviceInterfaceDetailData deviceInterfaceDetailData, uint detailSize, IntPtr unused1, IntPtr unused2);
/// <summary> /// SetupDiGetDeviceInterfaceDetail /// Gets the interface detail from a DeviceInterfaceData. This is pretty much the device path. /// You call this twice, once to get the size of the struct you need to send (nDeviceInterfaceDetailDataSize=0) /// and once again when you've allocated the required space. /// </summary> /// <param name="lpDeviceInfoSet">InfoSet to access</param> /// <param name="oInterfaceData">DeviceInterfaceData to use</param> /// <param name="lpDeviceInterfaceDetailData">DeviceInterfaceDetailData to fill with data</param> /// <param name="nDeviceInterfaceDetailDataSize">The size of the above</param> /// <param name="nRequiredSize">The required size of the above when above is set as zero</param> /// <param name="lpDeviceInfoData">Not used</param> /// <returns></returns> [DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)] internal static extern bool SetupDiGetDeviceInterfaceDetail(IntPtr lpDeviceInfoSet, ref DeviceInterfaceData oInterfaceData, ref DeviceInterfaceDetailData lpDeviceInterfaceDetailData, Int32 nDeviceInterfaceDetailDataSize, ref Int32 nRequiredSize, IntPtr lpDeviceInfoData);
[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);
protected static string FindDevice(Guid classGuid) { IntPtr handle = SetupDiGetClassDevs(ref classGuid, 0, 0, 0x12); string devicePath = null; if (handle.ToInt32() == -1) { throw new Exception(string.Format("Failed in call to SetupDiGetClassDevs ({0})", GetLastError())); } for (int deviceIndex = 0;; deviceIndex++) { DeviceInfoData deviceInfoData = new DeviceInfoData(); deviceInfoData.Size = Marshal.SizeOf(deviceInfoData); if (SetupDiEnumDeviceInfo(handle, deviceIndex, ref deviceInfoData) == false) { // out of devices or do we have an error? if (GetLastError() != 0x103 && GetLastError() != 0x7E) { SetupDiDestroyDeviceInfoList(handle); throw new Exception(string.Format("Failed in call to SetupDiEnumDeviceInfo ({0})", GetLastError())); } SetupDiDestroyDeviceInfoList(handle); break; } DeviceInterfaceData deviceInterfaceData = new DeviceInterfaceData(); deviceInterfaceData.Size = Marshal.SizeOf(deviceInterfaceData); if (SetupDiEnumDeviceInterfaces(handle, ref deviceInfoData, ref classGuid, 0, ref deviceInterfaceData) == false) { SetupDiDestroyDeviceInfoList(handle); throw new Exception(string.Format("Failed in call to SetupDiEnumDeviceInterfaces ({0})", GetLastError())); } uint cbData = 0; if (SetupDiGetDeviceInterfaceDetail(handle, ref deviceInterfaceData, 0, 0, ref cbData, 0) == false && cbData == 0) { SetupDiDestroyDeviceInfoList(handle); throw new Exception(string.Format("Failed in call to SetupDiGetDeviceInterfaceDetail ({0})", GetLastError())); } DeviceInterfaceDetailData deviceInterfaceDetailData = new DeviceInterfaceDetailData(); deviceInterfaceDetailData.Size = 5; if ( SetupDiGetDeviceInterfaceDetail(handle, ref deviceInterfaceData, ref deviceInterfaceDetailData, cbData, 0, 0) == false) { SetupDiDestroyDeviceInfoList(handle); throw new Exception(string.Format("Failed in call to SetupDiGetDeviceInterfaceDetail ({0})", GetLastError())); } if (deviceInterfaceDetailData.DevicePath.IndexOf("#vid_0471&pid_0815") != -1) { SetupDiDestroyDeviceInfoList(handle); devicePath = deviceInterfaceDetailData.DevicePath; break; } if (deviceInterfaceDetailData.DevicePath.IndexOf("#vid_045e&pid_006d") != -1) { SetupDiDestroyDeviceInfoList(handle); devicePath = deviceInterfaceDetailData.DevicePath; break; } if (deviceInterfaceDetailData.DevicePath.IndexOf("#vid_1460&pid_9150") != -1) { SetupDiDestroyDeviceInfoList(handle); devicePath = deviceInterfaceDetailData.DevicePath; break; } if (deviceInterfaceDetailData.DevicePath.IndexOf("#vid_0609&pid_031d") != -1) { SetupDiDestroyDeviceInfoList(handle); devicePath = deviceInterfaceDetailData.DevicePath; break; } if (deviceInterfaceDetailData.DevicePath.IndexOf("#vid_03ee&pid_2501") != -1) { SetupDiDestroyDeviceInfoList(handle); devicePath = deviceInterfaceDetailData.DevicePath; break; } } return(devicePath); }
/// <summary> /// SetupDiGetDeviceInterfaceDetail /// Gets the interface detail from a DeviceInterfaceData. This is pretty much the device path. /// You call this twice, once to get the size of the struct you need to send (nDeviceInterfaceDetailDataSize=0) /// and once again when you've allocated the required space. /// </summary> /// <param name="lpDeviceInfoSet">InfoSet to access</param> /// <param name="oInterfaceData">DeviceInterfaceData to use</param> /// <param name="lpDeviceInterfaceDetailData">DeviceInterfaceDetailData to fill with data</param> /// <param name="nDeviceInterfaceDetailDataSize">The size of the above</param> /// <param name="nRequiredSize">The required size of the above when above is set as zero</param> /// <param name="lpDeviceInfoData">Not used</param> /// <returns></returns> [DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)] internal static extern bool SetupDiGetDeviceInterfaceDetail(IntPtr lpDeviceInfoSet, ref DeviceInterfaceData oInterfaceData, ref DeviceInterfaceDetailData lpDeviceInterfaceDetailData, Int32 nDeviceInterfaceDetailDataSize, ref Int32 nRequiredSize, IntPtr lpDeviceInfoData);
//public static FileStream Open(string tSerial, string tMan) public static FileStream Open(string tSerial, string tMan) { FileStream devFile = null; Guid gHid; HidD_GetHidGuid(out gHid); // create list of HID devices present right now var hInfoSet = SetupDiGetClassDevs(ref gHid, null, IntPtr.Zero, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT); var iface = new DeviceInterfaceData(); // allocate mem for interface descriptor iface.Size = Marshal.SizeOf(iface); // set size field uint index = 0; // interface index // Enumerate all interfaces with HID GUID while (SetupDiEnumDeviceInterfaces(hInfoSet, 0, ref gHid, index, ref iface)) { var detIface = new DeviceInterfaceDetailData(); // detailed interface information uint reqSize = (uint)Marshal.SizeOf(detIface); // required size detIface.Size = Marshal.SizeOf(typeof(IntPtr)) == 8 ? 8 : 5; // Size depends on arch (32 / 64 bit), distinguish by IntPtr size // get device path SetupDiGetDeviceInterfaceDetail(hInfoSet, ref iface, ref detIface, reqSize, ref reqSize, IntPtr.Zero); var path = detIface.DevicePath; // System.Console.WriteLine("Path: {0}", path); // Open filehandle to device var handle = CreateFile(path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, IntPtr.Zero); if (handle == INVALID_HANDLE_VALUE) { //System.Console.WriteLine("Invalid handle"); index++; continue; } IntPtr lpData; if (HidD_GetPreparsedData(handle, out lpData)) { HidCaps oCaps; HidP_GetCaps(lpData, out oCaps); // extract the device capabilities from the internal buffer int inp = oCaps.InputReportByteLength; // get the input... int outp = oCaps.OutputReportByteLength; // ... and output report length HidD_FreePreparsedData(ref lpData); // System.Console.WriteLine("Input: {0}, Output: {1}", inp, outp); // we have report length matching our input / output report, so we create a device file in each case if (inp == 65 || outp == 65) { // check if manufacturer and serial string are matching //Manufacturer var s = new StringBuilder(256); // returned string string man = String.Empty; // get string if (HidD_GetManufacturerString(handle, s, s.Capacity)) { man = s.ToString(); } //Serial string serial = String.Empty; // get string if (HidD_GetSerialNumberString(handle, s, s.Capacity)) { serial = s.ToString(); } if (tMan.Equals(man, StringComparison.Ordinal) && tSerial.Equals(serial, StringComparison.Ordinal)) { //Console.WriteLine("Device found: " + path); var shandle = new SafeFileHandle(handle, false); devFile = new FileStream(shandle, FileAccess.Read | FileAccess.Write, 32, true); break; } } } index++; } SetupDiDestroyDeviceInfoList(hInfoSet); return(devFile); }
protected static extern bool SetupDiGetDeviceInterfaceDetail(IntPtr lpDeviceInfoSet, ref DeviceInterfaceData oInterfaceData, ref DeviceInterfaceDetailData oDetailData, uint nDeviceInterfaceDetailDataSize, ref uint nRequiredSize, IntPtr lpDeviceInfoData);
/// <summary> /// Find the device path for the supplied Device Class Guid. /// </summary> /// <param name="classGuid">GUID to locate device with.</param> /// <returns>Device path.</returns> public static string Find(Guid classGuid) { IntPtr handle = SetupDiGetClassDevs(ref classGuid, null, IntPtr.Zero, Digcfs.DeviceInterface | Digcfs.Present); if (handle.ToInt32() == -1) { return(null); } for (int deviceIndex = 0;; deviceIndex++) { DeviceInfoData deviceInfoData = new DeviceInfoData(); deviceInfoData.Size = Marshal.SizeOf(deviceInfoData); if (!SetupDiEnumDeviceInfo(handle, deviceIndex, ref deviceInfoData)) { int lastError = Marshal.GetLastWin32Error(); // out of devices or do we have an error? if (lastError != ErrorNoMoreItems && lastError != ErrorModNotFound) { SetupDiDestroyDeviceInfoList(handle); throw new Win32Exception(lastError); } SetupDiDestroyDeviceInfoList(handle); break; } DeviceInterfaceData deviceInterfaceData = new DeviceInterfaceData(); deviceInterfaceData.Size = Marshal.SizeOf(deviceInterfaceData); if (!SetupDiEnumDeviceInterfaces(handle, ref deviceInfoData, ref classGuid, 0, ref deviceInterfaceData)) { SetupDiDestroyDeviceInfoList(handle); throw new Win32Exception(Marshal.GetLastWin32Error()); } uint cbData = 0; if ( !SetupDiGetDeviceInterfaceDetail(handle, ref deviceInterfaceData, IntPtr.Zero, 0, ref cbData, IntPtr.Zero) && cbData == 0) { SetupDiDestroyDeviceInfoList(handle); throw new Win32Exception(Marshal.GetLastWin32Error()); } DeviceInterfaceDetailData deviceInterfaceDetailData = new DeviceInterfaceDetailData(); if (IntPtr.Size == 8) { deviceInterfaceDetailData.Size = 8; } else { deviceInterfaceDetailData.Size = 5; } if ( !SetupDiGetDeviceInterfaceDetail(handle, ref deviceInterfaceData, ref deviceInterfaceDetailData, cbData, IntPtr.Zero, IntPtr.Zero)) { SetupDiDestroyDeviceInfoList(handle); throw new Win32Exception(Marshal.GetLastWin32Error()); } if (!String.IsNullOrEmpty(deviceInterfaceDetailData.DevicePath)) { SetupDiDestroyDeviceInfoList(handle); return(deviceInterfaceDetailData.DevicePath); } } return(null); }