REFACTOR: Invert 'if' statement to reduce nesting.

This commit is contained in:
2017-12-21 06:06:19 +00:00
parent 9cd1869d1d
commit 4d886dae25
138 changed files with 9447 additions and 9806 deletions

View File

@@ -494,57 +494,57 @@ namespace DiscImageChef.Devices.Windows
// We start at the "root" of the device tree and look for all
// devices that match the interface GUID of a Hub Controller
IntPtr h = SetupDiGetClassDevs(ref hostGuid, 0, IntPtr.Zero, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if(h.ToInt32() != INVALID_HANDLE_VALUE)
if(h.ToInt32() == INVALID_HANDLE_VALUE)
return new System.Collections.ObjectModel.ReadOnlyCollection<UsbController>(hostList);
IntPtr ptrBuf = Marshal.AllocHGlobal(BUFFER_SIZE);
bool success;
int i = 0;
do
{
IntPtr ptrBuf = Marshal.AllocHGlobal(BUFFER_SIZE);
bool success;
int i = 0;
do
UsbController host = new UsbController();
host.ControllerIndex = i;
// create a Device Interface Data structure
SpDeviceInterfaceData dia = new SpDeviceInterfaceData();
dia.cbSize = Marshal.SizeOf(dia);
// start the enumeration
success = SetupDiEnumDeviceInterfaces(h, IntPtr.Zero, ref hostGuid, i, ref dia);
if(success)
{
UsbController host = new UsbController();
host.ControllerIndex = i;
// build a DevInfo Data structure
SpDevinfoData da = new SpDevinfoData();
da.cbSize = Marshal.SizeOf(da);
// create a Device Interface Data structure
SpDeviceInterfaceData dia = new SpDeviceInterfaceData();
dia.cbSize = Marshal.SizeOf(dia);
// build a Device Interface Detail Data structure
SpDeviceInterfaceDetailData didd = new SpDeviceInterfaceDetailData();
didd.cbSize = 4 + Marshal.SystemDefaultCharSize; // trust me :)
// start the enumeration
success = SetupDiEnumDeviceInterfaces(h, IntPtr.Zero, ref hostGuid, i, ref dia);
if(success)
// now we can get some more detailed information
int nRequiredSize = 0;
int nBytes = BUFFER_SIZE;
if(SetupDiGetDeviceInterfaceDetail(h, ref dia, ref didd, nBytes, ref nRequiredSize, ref da))
{
// build a DevInfo Data structure
SpDevinfoData da = new SpDevinfoData();
da.cbSize = Marshal.SizeOf(da);
host.ControllerDevicePath = didd.DevicePath;
// build a Device Interface Detail Data structure
SpDeviceInterfaceDetailData didd = new SpDeviceInterfaceDetailData();
didd.cbSize = 4 + Marshal.SystemDefaultCharSize; // trust me :)
// get the Device Description and DriverKeyName
int requiredSize = 0;
int regType = REG_SZ;
// now we can get some more detailed information
int nRequiredSize = 0;
int nBytes = BUFFER_SIZE;
if(SetupDiGetDeviceInterfaceDetail(h, ref dia, ref didd, nBytes, ref nRequiredSize, ref da))
{
host.ControllerDevicePath = didd.DevicePath;
// get the Device Description and DriverKeyName
int requiredSize = 0;
int regType = REG_SZ;
if(SetupDiGetDeviceRegistryProperty(h, ref da, SPDRP_DEVICEDESC, ref regType, ptrBuf,
BUFFER_SIZE, ref requiredSize)) host.ControllerDeviceDesc = Marshal.PtrToStringAuto(ptrBuf);
if(SetupDiGetDeviceRegistryProperty(h, ref da, SPDRP_DRIVER, ref regType, ptrBuf,
BUFFER_SIZE, ref requiredSize)) host.ControllerDriverKeyName = Marshal.PtrToStringAuto(ptrBuf);
}
hostList.Add(host);
if(SetupDiGetDeviceRegistryProperty(h, ref da, SPDRP_DEVICEDESC, ref regType, ptrBuf,
BUFFER_SIZE, ref requiredSize)) host.ControllerDeviceDesc = Marshal.PtrToStringAuto(ptrBuf);
if(SetupDiGetDeviceRegistryProperty(h, ref da, SPDRP_DRIVER, ref regType, ptrBuf,
BUFFER_SIZE, ref requiredSize)) host.ControllerDriverKeyName = Marshal.PtrToStringAuto(ptrBuf);
}
i++;
hostList.Add(host);
}
while(success);
Marshal.FreeHGlobal(ptrBuf);
SetupDiDestroyDeviceInfoList(h);
i++;
}
while(success);
Marshal.FreeHGlobal(ptrBuf);
SetupDiDestroyDeviceInfoList(h);
// convert it into a Collection
return new System.Collections.ObjectModel.ReadOnlyCollection<UsbController>(hostList);
@@ -602,50 +602,49 @@ namespace DiscImageChef.Devices.Windows
// Open a handle to the Host Controller
h = CreateFile(ControllerDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0,
IntPtr.Zero);
if(h.ToInt32() != INVALID_HANDLE_VALUE)
{
int nBytesReturned;
UsbRootHubName hubName = new UsbRootHubName();
int nBytes = Marshal.SizeOf(hubName);
IntPtr ptrHubName = Marshal.AllocHGlobal(nBytes);
if(h.ToInt32() == INVALID_HANDLE_VALUE) return root;
// get the Hub Name
if(DeviceIoControl(h, IOCTL_USB_GET_ROOT_HUB_NAME, ptrHubName, nBytes, ptrHubName, nBytes,
int nBytesReturned;
UsbRootHubName hubName = new UsbRootHubName();
int nBytes = Marshal.SizeOf(hubName);
IntPtr ptrHubName = Marshal.AllocHGlobal(nBytes);
// get the Hub Name
if(DeviceIoControl(h, IOCTL_USB_GET_ROOT_HUB_NAME, ptrHubName, nBytes, ptrHubName, nBytes,
out nBytesReturned, IntPtr.Zero))
{
hubName = (UsbRootHubName)Marshal.PtrToStructure(ptrHubName, typeof(UsbRootHubName));
root.HubDevicePath = @"\\.\" + hubName.RootHubName;
}
// TODO: Get DriverKeyName for Root Hub
// Now let's open the Hub (based upon the HubName we got above)
h2 = CreateFile(root.HubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0,
IntPtr.Zero);
if(h2.ToInt32() != INVALID_HANDLE_VALUE)
{
UsbNodeInformation nodeInfo = new UsbNodeInformation();
nodeInfo.NodeType = (int)UsbHubNode.UsbHub;
nBytes = Marshal.SizeOf(nodeInfo);
IntPtr ptrNodeInfo = Marshal.AllocHGlobal(nBytes);
Marshal.StructureToPtr(nodeInfo, ptrNodeInfo, true);
// get the Hub Information
if(DeviceIoControl(h2, IOCTL_USB_GET_NODE_INFORMATION, ptrNodeInfo, nBytes, ptrNodeInfo, nBytes,
out nBytesReturned, IntPtr.Zero))
{
hubName = (UsbRootHubName)Marshal.PtrToStructure(ptrHubName, typeof(UsbRootHubName));
root.HubDevicePath = @"\\.\" + hubName.RootHubName;
nodeInfo = (UsbNodeInformation)Marshal.PtrToStructure(ptrNodeInfo,
typeof(UsbNodeInformation));
root.HubIsBusPowered = Convert.ToBoolean(nodeInfo.HubInformation.HubIsBusPowered);
root.HubPortCount = nodeInfo.HubInformation.HubDescriptor.bNumberOfPorts;
}
// TODO: Get DriverKeyName for Root Hub
// Now let's open the Hub (based upon the HubName we got above)
h2 = CreateFile(root.HubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0,
IntPtr.Zero);
if(h2.ToInt32() != INVALID_HANDLE_VALUE)
{
UsbNodeInformation nodeInfo = new UsbNodeInformation();
nodeInfo.NodeType = (int)UsbHubNode.UsbHub;
nBytes = Marshal.SizeOf(nodeInfo);
IntPtr ptrNodeInfo = Marshal.AllocHGlobal(nBytes);
Marshal.StructureToPtr(nodeInfo, ptrNodeInfo, true);
// get the Hub Information
if(DeviceIoControl(h2, IOCTL_USB_GET_NODE_INFORMATION, ptrNodeInfo, nBytes, ptrNodeInfo, nBytes,
out nBytesReturned, IntPtr.Zero))
{
nodeInfo = (UsbNodeInformation)Marshal.PtrToStructure(ptrNodeInfo,
typeof(UsbNodeInformation));
root.HubIsBusPowered = Convert.ToBoolean(nodeInfo.HubInformation.HubIsBusPowered);
root.HubPortCount = nodeInfo.HubInformation.HubDescriptor.bNumberOfPorts;
}
Marshal.FreeHGlobal(ptrNodeInfo);
CloseHandle(h2);
}
Marshal.FreeHGlobal(ptrHubName);
CloseHandle(h);
Marshal.FreeHGlobal(ptrNodeInfo);
CloseHandle(h2);
}
Marshal.FreeHGlobal(ptrHubName);
CloseHandle(h);
return root;
}
}
@@ -740,50 +739,49 @@ namespace DiscImageChef.Devices.Windows
// Open a handle to the Hub device
IntPtr h = CreateFile(HubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0,
IntPtr.Zero);
if(h.ToInt32() != INVALID_HANDLE_VALUE)
if(h.ToInt32() == INVALID_HANDLE_VALUE)
return new System.Collections.ObjectModel.ReadOnlyCollection<UsbPort>(portList);
int nBytes = Marshal.SizeOf(typeof(UsbNodeConnectionInformationEx));
IntPtr ptrNodeConnection = Marshal.AllocHGlobal(nBytes);
// loop thru all of the ports on the hub
// BTW: Ports are numbered starting at 1
for(int i = 1; i <= HubPortCount; i++)
{
int nBytes = Marshal.SizeOf(typeof(UsbNodeConnectionInformationEx));
IntPtr ptrNodeConnection = Marshal.AllocHGlobal(nBytes);
int nBytesReturned;
UsbNodeConnectionInformationEx nodeConnection = new UsbNodeConnectionInformationEx();
nodeConnection.ConnectionIndex = i;
Marshal.StructureToPtr(nodeConnection, ptrNodeConnection, true);
// loop thru all of the ports on the hub
// BTW: Ports are numbered starting at 1
for(int i = 1; i <= HubPortCount; i++)
{
int nBytesReturned;
UsbNodeConnectionInformationEx nodeConnection = new UsbNodeConnectionInformationEx();
nodeConnection.ConnectionIndex = i;
Marshal.StructureToPtr(nodeConnection, ptrNodeConnection, true);
if(!DeviceIoControl(h, IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX, ptrNodeConnection, nBytes,
ptrNodeConnection, nBytes, out nBytesReturned, IntPtr.Zero)) continue;
if(DeviceIoControl(h, IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX, ptrNodeConnection, nBytes,
ptrNodeConnection, nBytes, out nBytesReturned, IntPtr.Zero))
{
nodeConnection =
(UsbNodeConnectionInformationEx)Marshal.PtrToStructure(ptrNodeConnection,
typeof(
UsbNodeConnectionInformationEx
));
nodeConnection =
(UsbNodeConnectionInformationEx)Marshal.PtrToStructure(ptrNodeConnection,
typeof(
UsbNodeConnectionInformationEx
));
// load up the USBPort class
UsbPort port = new UsbPort();
port.PortPortNumber = i;
port.PortHubDevicePath = HubDevicePath;
UsbConnectionStatus status = (UsbConnectionStatus)nodeConnection.ConnectionStatus;
port.PortStatus = status.ToString();
UsbDeviceSpeed speed = (UsbDeviceSpeed)nodeConnection.Speed;
port.PortSpeed = speed.ToString();
port.PortIsDeviceConnected =
nodeConnection.ConnectionStatus == (int)UsbConnectionStatus.DeviceConnected;
port.PortIsHub = Convert.ToBoolean(nodeConnection.DeviceIsHub);
port.PortDeviceDescriptor = nodeConnection.DeviceDescriptor;
// load up the USBPort class
UsbPort port = new UsbPort();
port.PortPortNumber = i;
port.PortHubDevicePath = HubDevicePath;
UsbConnectionStatus status = (UsbConnectionStatus)nodeConnection.ConnectionStatus;
port.PortStatus = status.ToString();
UsbDeviceSpeed speed = (UsbDeviceSpeed)nodeConnection.Speed;
port.PortSpeed = speed.ToString();
port.PortIsDeviceConnected =
nodeConnection.ConnectionStatus == (int)UsbConnectionStatus.DeviceConnected;
port.PortIsHub = Convert.ToBoolean(nodeConnection.DeviceIsHub);
port.PortDeviceDescriptor = nodeConnection.DeviceDescriptor;
// add it to the list
portList.Add(port);
}
}
Marshal.FreeHGlobal(ptrNodeConnection);
CloseHandle(h);
// add it to the list
portList.Add(port);
}
Marshal.FreeHGlobal(ptrNodeConnection);
CloseHandle(h);
// convert it into a Collection
return new System.Collections.ObjectModel.ReadOnlyCollection<UsbPort>(portList);
}
@@ -862,143 +860,142 @@ namespace DiscImageChef.Devices.Windows
// Open a handle to the Hub device
IntPtr h = CreateFile(PortHubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0,
IntPtr.Zero);
if(h.ToInt32() != INVALID_HANDLE_VALUE)
if(h.ToInt32() == INVALID_HANDLE_VALUE) return device;
int nBytesReturned;
int nBytes = BUFFER_SIZE;
// We use this to zero fill a buffer
string nullString = new string((char)0, BUFFER_SIZE / Marshal.SystemDefaultCharSize);
// The iManufacturer, iProduct and iSerialNumber entries in the
// Device Descriptor are really just indexes. So, we have to
// request a String Descriptor to get the values for those strings.
if(PortDeviceDescriptor.iManufacturer > 0)
{
int nBytesReturned;
int nBytes = BUFFER_SIZE;
// We use this to zero fill a buffer
string nullString = new string((char)0, BUFFER_SIZE / Marshal.SystemDefaultCharSize);
// The iManufacturer, iProduct and iSerialNumber entries in the
// Device Descriptor are really just indexes. So, we have to
// request a String Descriptor to get the values for those strings.
if(PortDeviceDescriptor.iManufacturer > 0)
{
// build a request for string descriptor
UsbDescriptorRequest request = new UsbDescriptorRequest();
request.ConnectionIndex = PortPortNumber;
request.SetupPacket.wValue =
(short)((USB_STRING_DESCRIPTOR_TYPE << 8) + PortDeviceDescriptor.iManufacturer);
request.SetupPacket.wLength = (short)(nBytes - Marshal.SizeOf(request));
request.SetupPacket.wIndex = 0x409; // Language Code
// Geez, I wish C# had a Marshal.MemSet() method
IntPtr ptrRequest = Marshal.StringToHGlobalAuto(nullString);
Marshal.StructureToPtr(request, ptrRequest, true);
// Use an IOCTL call to request the String Descriptor
if(DeviceIoControl(h, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, ptrRequest, nBytes,
ptrRequest, nBytes, out nBytesReturned, IntPtr.Zero))
{
// The location of the string descriptor is immediately after
// the Request structure. Because this location is not "covered"
// by the structure allocation, we're forced to zero out this
// chunk of memory by using the StringToHGlobalAuto() hack above
IntPtr ptrStringDesc = new IntPtr(ptrRequest.ToInt32() + Marshal.SizeOf(request));
UsbStringDescriptor stringDesc =
(UsbStringDescriptor)Marshal.PtrToStructure(ptrStringDesc,
typeof(UsbStringDescriptor));
device.DeviceManufacturer = stringDesc.bString;
}
Marshal.FreeHGlobal(ptrRequest);
}
if(PortDeviceDescriptor.iProduct > 0)
{
// build a request for string descriptor
UsbDescriptorRequest request = new UsbDescriptorRequest();
request.ConnectionIndex = PortPortNumber;
request.SetupPacket.wValue =
(short)((USB_STRING_DESCRIPTOR_TYPE << 8) + PortDeviceDescriptor.iProduct);
request.SetupPacket.wLength = (short)(nBytes - Marshal.SizeOf(request));
request.SetupPacket.wIndex = 0x409; // Language Code
// Geez, I wish C# had a Marshal.MemSet() method
IntPtr ptrRequest = Marshal.StringToHGlobalAuto(nullString);
Marshal.StructureToPtr(request, ptrRequest, true);
// Use an IOCTL call to request the String Descriptor
if(DeviceIoControl(h, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, ptrRequest, nBytes,
ptrRequest, nBytes, out nBytesReturned, IntPtr.Zero))
{
// the location of the string descriptor is immediately after the Request structure
IntPtr ptrStringDesc = new IntPtr(ptrRequest.ToInt32() + Marshal.SizeOf(request));
UsbStringDescriptor stringDesc =
(UsbStringDescriptor)Marshal.PtrToStructure(ptrStringDesc,
typeof(UsbStringDescriptor));
device.DeviceProduct = stringDesc.bString;
}
Marshal.FreeHGlobal(ptrRequest);
}
if(PortDeviceDescriptor.iSerialNumber > 0)
{
// build a request for string descriptor
UsbDescriptorRequest request = new UsbDescriptorRequest();
request.ConnectionIndex = PortPortNumber;
request.SetupPacket.wValue =
(short)((USB_STRING_DESCRIPTOR_TYPE << 8) + PortDeviceDescriptor.iSerialNumber);
request.SetupPacket.wLength = (short)(nBytes - Marshal.SizeOf(request));
request.SetupPacket.wIndex = 0x409; // Language Code
// Geez, I wish C# had a Marshal.MemSet() method
IntPtr ptrRequest = Marshal.StringToHGlobalAuto(nullString);
Marshal.StructureToPtr(request, ptrRequest, true);
// Use an IOCTL call to request the String Descriptor
if(DeviceIoControl(h, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, ptrRequest, nBytes,
ptrRequest, nBytes, out nBytesReturned, IntPtr.Zero))
{
// the location of the string descriptor is immediately after the Request structure
IntPtr ptrStringDesc = new IntPtr(ptrRequest.ToInt32() + Marshal.SizeOf(request));
UsbStringDescriptor stringDesc =
(UsbStringDescriptor)Marshal.PtrToStructure(ptrStringDesc,
typeof(UsbStringDescriptor));
device.DeviceSerialNumber = stringDesc.bString;
}
Marshal.FreeHGlobal(ptrRequest);
}
// build a request for configuration descriptor
UsbDescriptorRequest dcrRequest = new UsbDescriptorRequest();
dcrRequest.ConnectionIndex = PortPortNumber;
dcrRequest.SetupPacket.wValue = (short)(USB_CONFIGURATION_DESCRIPTOR_TYPE << 8);
dcrRequest.SetupPacket.wLength = (short)(nBytes - Marshal.SizeOf(dcrRequest));
dcrRequest.SetupPacket.wIndex = 0;
// build a request for string descriptor
UsbDescriptorRequest request = new UsbDescriptorRequest();
request.ConnectionIndex = PortPortNumber;
request.SetupPacket.wValue =
(short)((USB_STRING_DESCRIPTOR_TYPE << 8) + PortDeviceDescriptor.iManufacturer);
request.SetupPacket.wLength = (short)(nBytes - Marshal.SizeOf(request));
request.SetupPacket.wIndex = 0x409; // Language Code
// Geez, I wish C# had a Marshal.MemSet() method
IntPtr dcrPtrRequest = Marshal.StringToHGlobalAuto(nullString);
Marshal.StructureToPtr(dcrRequest, dcrPtrRequest, true);
IntPtr ptrRequest = Marshal.StringToHGlobalAuto(nullString);
Marshal.StructureToPtr(request, ptrRequest, true);
// Use an IOCTL call to request the String Descriptor
if(DeviceIoControl(h, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, dcrPtrRequest, nBytes,
dcrPtrRequest, nBytes, out nBytesReturned, IntPtr.Zero))
if(DeviceIoControl(h, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, ptrRequest, nBytes,
ptrRequest, nBytes, out nBytesReturned, IntPtr.Zero))
{
IntPtr ptrStringDesc = new IntPtr(dcrPtrRequest.ToInt32() + Marshal.SizeOf(dcrRequest));
device.BinaryDeviceDescriptors = new byte[nBytesReturned];
Marshal.Copy(ptrStringDesc, device.BinaryDeviceDescriptors, 0, nBytesReturned);
// The location of the string descriptor is immediately after
// the Request structure. Because this location is not "covered"
// by the structure allocation, we're forced to zero out this
// chunk of memory by using the StringToHGlobalAuto() hack above
IntPtr ptrStringDesc = new IntPtr(ptrRequest.ToInt32() + Marshal.SizeOf(request));
UsbStringDescriptor stringDesc =
(UsbStringDescriptor)Marshal.PtrToStructure(ptrStringDesc,
typeof(UsbStringDescriptor));
device.DeviceManufacturer = stringDesc.bString;
}
Marshal.FreeHGlobal(dcrPtrRequest);
// Get the Driver Key Name (usefull in locating a device)
UsbNodeConnectionDriverkeyName driverKey = new UsbNodeConnectionDriverkeyName();
driverKey.ConnectionIndex = PortPortNumber;
nBytes = Marshal.SizeOf(driverKey);
IntPtr ptrDriverKey = Marshal.AllocHGlobal(nBytes);
Marshal.StructureToPtr(driverKey, ptrDriverKey, true);
// Use an IOCTL call to request the Driver Key Name
if(DeviceIoControl(h, IOCTL_USB_GET_NODE_CONNECTION_DRIVERKEY_NAME, ptrDriverKey, nBytes,
ptrDriverKey, nBytes, out nBytesReturned, IntPtr.Zero))
{
driverKey = (UsbNodeConnectionDriverkeyName)Marshal.PtrToStructure(ptrDriverKey,
typeof(
UsbNodeConnectionDriverkeyName
));
device.DeviceDriverKey = driverKey.DriverKeyName;
// use the DriverKeyName to get the Device Description and Instance ID
device.DeviceName = GetDescriptionByKeyName(device.DeviceDriverKey);
device.DeviceInstanceId = GetInstanceIdByKeyName(device.DeviceDriverKey);
}
Marshal.FreeHGlobal(ptrDriverKey);
CloseHandle(h);
Marshal.FreeHGlobal(ptrRequest);
}
if(PortDeviceDescriptor.iProduct > 0)
{
// build a request for string descriptor
UsbDescriptorRequest request = new UsbDescriptorRequest();
request.ConnectionIndex = PortPortNumber;
request.SetupPacket.wValue =
(short)((USB_STRING_DESCRIPTOR_TYPE << 8) + PortDeviceDescriptor.iProduct);
request.SetupPacket.wLength = (short)(nBytes - Marshal.SizeOf(request));
request.SetupPacket.wIndex = 0x409; // Language Code
// Geez, I wish C# had a Marshal.MemSet() method
IntPtr ptrRequest = Marshal.StringToHGlobalAuto(nullString);
Marshal.StructureToPtr(request, ptrRequest, true);
// Use an IOCTL call to request the String Descriptor
if(DeviceIoControl(h, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, ptrRequest, nBytes,
ptrRequest, nBytes, out nBytesReturned, IntPtr.Zero))
{
// the location of the string descriptor is immediately after the Request structure
IntPtr ptrStringDesc = new IntPtr(ptrRequest.ToInt32() + Marshal.SizeOf(request));
UsbStringDescriptor stringDesc =
(UsbStringDescriptor)Marshal.PtrToStructure(ptrStringDesc,
typeof(UsbStringDescriptor));
device.DeviceProduct = stringDesc.bString;
}
Marshal.FreeHGlobal(ptrRequest);
}
if(PortDeviceDescriptor.iSerialNumber > 0)
{
// build a request for string descriptor
UsbDescriptorRequest request = new UsbDescriptorRequest();
request.ConnectionIndex = PortPortNumber;
request.SetupPacket.wValue =
(short)((USB_STRING_DESCRIPTOR_TYPE << 8) + PortDeviceDescriptor.iSerialNumber);
request.SetupPacket.wLength = (short)(nBytes - Marshal.SizeOf(request));
request.SetupPacket.wIndex = 0x409; // Language Code
// Geez, I wish C# had a Marshal.MemSet() method
IntPtr ptrRequest = Marshal.StringToHGlobalAuto(nullString);
Marshal.StructureToPtr(request, ptrRequest, true);
// Use an IOCTL call to request the String Descriptor
if(DeviceIoControl(h, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, ptrRequest, nBytes,
ptrRequest, nBytes, out nBytesReturned, IntPtr.Zero))
{
// the location of the string descriptor is immediately after the Request structure
IntPtr ptrStringDesc = new IntPtr(ptrRequest.ToInt32() + Marshal.SizeOf(request));
UsbStringDescriptor stringDesc =
(UsbStringDescriptor)Marshal.PtrToStructure(ptrStringDesc,
typeof(UsbStringDescriptor));
device.DeviceSerialNumber = stringDesc.bString;
}
Marshal.FreeHGlobal(ptrRequest);
}
// build a request for configuration descriptor
UsbDescriptorRequest dcrRequest = new UsbDescriptorRequest();
dcrRequest.ConnectionIndex = PortPortNumber;
dcrRequest.SetupPacket.wValue = (short)(USB_CONFIGURATION_DESCRIPTOR_TYPE << 8);
dcrRequest.SetupPacket.wLength = (short)(nBytes - Marshal.SizeOf(dcrRequest));
dcrRequest.SetupPacket.wIndex = 0;
// Geez, I wish C# had a Marshal.MemSet() method
IntPtr dcrPtrRequest = Marshal.StringToHGlobalAuto(nullString);
Marshal.StructureToPtr(dcrRequest, dcrPtrRequest, true);
// Use an IOCTL call to request the String Descriptor
if(DeviceIoControl(h, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, dcrPtrRequest, nBytes,
dcrPtrRequest, nBytes, out nBytesReturned, IntPtr.Zero))
{
IntPtr ptrStringDesc = new IntPtr(dcrPtrRequest.ToInt32() + Marshal.SizeOf(dcrRequest));
device.BinaryDeviceDescriptors = new byte[nBytesReturned];
Marshal.Copy(ptrStringDesc, device.BinaryDeviceDescriptors, 0, nBytesReturned);
}
Marshal.FreeHGlobal(dcrPtrRequest);
// Get the Driver Key Name (usefull in locating a device)
UsbNodeConnectionDriverkeyName driverKey = new UsbNodeConnectionDriverkeyName();
driverKey.ConnectionIndex = PortPortNumber;
nBytes = Marshal.SizeOf(driverKey);
IntPtr ptrDriverKey = Marshal.AllocHGlobal(nBytes);
Marshal.StructureToPtr(driverKey, ptrDriverKey, true);
// Use an IOCTL call to request the Driver Key Name
if(DeviceIoControl(h, IOCTL_USB_GET_NODE_CONNECTION_DRIVERKEY_NAME, ptrDriverKey, nBytes,
ptrDriverKey, nBytes, out nBytesReturned, IntPtr.Zero))
{
driverKey = (UsbNodeConnectionDriverkeyName)Marshal.PtrToStructure(ptrDriverKey,
typeof(
UsbNodeConnectionDriverkeyName
));
device.DeviceDriverKey = driverKey.DriverKeyName;
// use the DriverKeyName to get the Device Description and Instance ID
device.DeviceName = GetDescriptionByKeyName(device.DeviceDriverKey);
device.DeviceInstanceId = GetInstanceIdByKeyName(device.DeviceDriverKey);
}
Marshal.FreeHGlobal(ptrDriverKey);
CloseHandle(h);
return device;
}
@@ -1015,61 +1012,59 @@ namespace DiscImageChef.Devices.Windows
// Open a handle to the Host Controller
h = CreateFile(PortHubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0,
IntPtr.Zero);
if(h.ToInt32() != INVALID_HANDLE_VALUE)
{
// Get the DevicePath for downstream hub
int nBytesReturned;
UsbNodeConnectionName nodeName = new UsbNodeConnectionName();
nodeName.ConnectionIndex = PortPortNumber;
int nBytes = Marshal.SizeOf(nodeName);
IntPtr ptrNodeName = Marshal.AllocHGlobal(nBytes);
Marshal.StructureToPtr(nodeName, ptrNodeName, true);
if(h.ToInt32() == INVALID_HANDLE_VALUE) return hub;
// Get the DevicePath for downstream hub
int nBytesReturned;
UsbNodeConnectionName nodeName = new UsbNodeConnectionName();
nodeName.ConnectionIndex = PortPortNumber;
int nBytes = Marshal.SizeOf(nodeName);
IntPtr ptrNodeName = Marshal.AllocHGlobal(nBytes);
Marshal.StructureToPtr(nodeName, ptrNodeName, true);
// Use an IOCTL call to request the Node Name
if(DeviceIoControl(h, IOCTL_USB_GET_NODE_CONNECTION_NAME, ptrNodeName, nBytes, ptrNodeName, nBytes,
// Use an IOCTL call to request the Node Name
if(DeviceIoControl(h, IOCTL_USB_GET_NODE_CONNECTION_NAME, ptrNodeName, nBytes, ptrNodeName, nBytes,
out nBytesReturned, IntPtr.Zero))
{
nodeName = (UsbNodeConnectionName)Marshal.PtrToStructure(ptrNodeName,
typeof(UsbNodeConnectionName));
hub.HubDevicePath = @"\\.\" + nodeName.NodeName;
}
// Now let's open the Hub (based upon the HubName we got above)
h2 = CreateFile(hub.HubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0,
IntPtr.Zero);
if(h2.ToInt32() != INVALID_HANDLE_VALUE)
{
UsbNodeInformation nodeInfo = new UsbNodeInformation();
nodeInfo.NodeType = (int)UsbHubNode.UsbHub;
nBytes = Marshal.SizeOf(nodeInfo);
IntPtr ptrNodeInfo = Marshal.AllocHGlobal(nBytes);
Marshal.StructureToPtr(nodeInfo, ptrNodeInfo, true);
// get the Hub Information
if(DeviceIoControl(h2, IOCTL_USB_GET_NODE_INFORMATION, ptrNodeInfo, nBytes, ptrNodeInfo, nBytes,
out nBytesReturned, IntPtr.Zero))
{
nodeName = (UsbNodeConnectionName)Marshal.PtrToStructure(ptrNodeName,
typeof(UsbNodeConnectionName));
hub.HubDevicePath = @"\\.\" + nodeName.NodeName;
nodeInfo = (UsbNodeInformation)Marshal.PtrToStructure(ptrNodeInfo,
typeof(UsbNodeInformation));
hub.HubIsBusPowered = Convert.ToBoolean(nodeInfo.HubInformation.HubIsBusPowered);
hub.HubPortCount = nodeInfo.HubInformation.HubDescriptor.bNumberOfPorts;
}
// Now let's open the Hub (based upon the HubName we got above)
h2 = CreateFile(hub.HubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0,
IntPtr.Zero);
if(h2.ToInt32() != INVALID_HANDLE_VALUE)
{
UsbNodeInformation nodeInfo = new UsbNodeInformation();
nodeInfo.NodeType = (int)UsbHubNode.UsbHub;
nBytes = Marshal.SizeOf(nodeInfo);
IntPtr ptrNodeInfo = Marshal.AllocHGlobal(nBytes);
Marshal.StructureToPtr(nodeInfo, ptrNodeInfo, true);
// get the Hub Information
if(DeviceIoControl(h2, IOCTL_USB_GET_NODE_INFORMATION, ptrNodeInfo, nBytes, ptrNodeInfo, nBytes,
out nBytesReturned, IntPtr.Zero))
{
nodeInfo = (UsbNodeInformation)Marshal.PtrToStructure(ptrNodeInfo,
typeof(UsbNodeInformation));
hub.HubIsBusPowered = Convert.ToBoolean(nodeInfo.HubInformation.HubIsBusPowered);
hub.HubPortCount = nodeInfo.HubInformation.HubDescriptor.bNumberOfPorts;
}
Marshal.FreeHGlobal(ptrNodeInfo);
CloseHandle(h2);
}
// Fill in the missing Manufacture, Product, and SerialNumber values
// values by just creating a Device instance and copying the values
UsbDevice device = GetDevice();
hub.HubInstanceId = device.DeviceInstanceId;
hub.HubManufacturer = device.Manufacturer;
hub.HubProduct = device.Product;
hub.HubSerialNumber = device.SerialNumber;
hub.HubDriverKey = device.DriverKey;
Marshal.FreeHGlobal(ptrNodeName);
CloseHandle(h);
Marshal.FreeHGlobal(ptrNodeInfo);
CloseHandle(h2);
}
// Fill in the missing Manufacture, Product, and SerialNumber values
// values by just creating a Device instance and copying the values
UsbDevice device = GetDevice();
hub.HubInstanceId = device.DeviceInstanceId;
hub.HubManufacturer = device.Manufacturer;
hub.HubProduct = device.Product;
hub.HubSerialNumber = device.SerialNumber;
hub.HubDriverKey = device.DriverKey;
Marshal.FreeHGlobal(ptrNodeName);
CloseHandle(h);
return hub;
}
}
@@ -1161,46 +1156,45 @@ namespace DiscImageChef.Devices.Windows
// Use the "enumerator form" of the SetupDiGetClassDevs API
// to generate a list of all USB devices
IntPtr h = SetupDiGetClassDevs(0, devEnum, IntPtr.Zero, DIGCF_PRESENT | DIGCF_ALLCLASSES);
if(h.ToInt32() != INVALID_HANDLE_VALUE)
if(h.ToInt32() == INVALID_HANDLE_VALUE) return ans;
IntPtr ptrBuf = Marshal.AllocHGlobal(BUFFER_SIZE);
string keyName;
bool success;
int i = 0;
do
{
IntPtr ptrBuf = Marshal.AllocHGlobal(BUFFER_SIZE);
string keyName;
// create a Device Interface Data structure
SpDevinfoData da = new SpDevinfoData();
da.cbSize = Marshal.SizeOf(da);
bool success;
int i = 0;
do
// start the enumeration
success = SetupDiEnumDeviceInfo(h, i, ref da);
if(success)
{
// create a Device Interface Data structure
SpDevinfoData da = new SpDevinfoData();
da.cbSize = Marshal.SizeOf(da);
int requiredSize = 0;
int regType = REG_SZ;
keyName = "";
// start the enumeration
success = SetupDiEnumDeviceInfo(h, i, ref da);
if(success)
if(SetupDiGetDeviceRegistryProperty(h, ref da, SPDRP_DRIVER, ref regType, ptrBuf, BUFFER_SIZE,
ref requiredSize)) keyName = Marshal.PtrToStringAuto(ptrBuf);
// is it a match?
if(keyName == driverKeyName)
{
int requiredSize = 0;
int regType = REG_SZ;
keyName = "";
if(SetupDiGetDeviceRegistryProperty(h, ref da, SPDRP_DRIVER, ref regType, ptrBuf, BUFFER_SIZE,
ref requiredSize)) keyName = Marshal.PtrToStringAuto(ptrBuf);
// is it a match?
if(keyName == driverKeyName)
{
if(SetupDiGetDeviceRegistryProperty(h, ref da, SPDRP_DEVICEDESC, ref regType, ptrBuf,
BUFFER_SIZE, ref requiredSize)) ans = Marshal.PtrToStringAuto(ptrBuf);
break;
}
if(SetupDiGetDeviceRegistryProperty(h, ref da, SPDRP_DEVICEDESC, ref regType, ptrBuf,
BUFFER_SIZE, ref requiredSize)) ans = Marshal.PtrToStringAuto(ptrBuf);
break;
}
i++;
}
while(success);
Marshal.FreeHGlobal(ptrBuf);
SetupDiDestroyDeviceInfoList(h);
i++;
}
while(success);
Marshal.FreeHGlobal(ptrBuf);
SetupDiDestroyDeviceInfoList(h);
return ans;
}
@@ -1216,48 +1210,47 @@ namespace DiscImageChef.Devices.Windows
// Use the "enumerator form" of the SetupDiGetClassDevs API
// to generate a list of all USB devices
IntPtr h = SetupDiGetClassDevs(0, devEnum, IntPtr.Zero, DIGCF_PRESENT | DIGCF_ALLCLASSES);
if(h.ToInt32() != INVALID_HANDLE_VALUE)
if(h.ToInt32() == INVALID_HANDLE_VALUE) return ans;
IntPtr ptrBuf = Marshal.AllocHGlobal(BUFFER_SIZE);
string keyName;
bool success;
int i = 0;
do
{
IntPtr ptrBuf = Marshal.AllocHGlobal(BUFFER_SIZE);
string keyName;
// create a Device Interface Data structure
SpDevinfoData da = new SpDevinfoData();
da.cbSize = Marshal.SizeOf(da);
bool success;
int i = 0;
do
// start the enumeration
success = SetupDiEnumDeviceInfo(h, i, ref da);
if(success)
{
// create a Device Interface Data structure
SpDevinfoData da = new SpDevinfoData();
da.cbSize = Marshal.SizeOf(da);
int requiredSize = 0;
int regType = REG_SZ;
// start the enumeration
success = SetupDiEnumDeviceInfo(h, i, ref da);
if(success)
keyName = "";
if(SetupDiGetDeviceRegistryProperty(h, ref da, SPDRP_DRIVER, ref regType, ptrBuf, BUFFER_SIZE,
ref requiredSize)) keyName = Marshal.PtrToStringAuto(ptrBuf);
// is it a match?
if(keyName == driverKeyName)
{
int requiredSize = 0;
int regType = REG_SZ;
keyName = "";
if(SetupDiGetDeviceRegistryProperty(h, ref da, SPDRP_DRIVER, ref regType, ptrBuf, BUFFER_SIZE,
ref requiredSize)) keyName = Marshal.PtrToStringAuto(ptrBuf);
// is it a match?
if(keyName == driverKeyName)
{
int nBytes = BUFFER_SIZE;
StringBuilder sb = new StringBuilder(nBytes);
SetupDiGetDeviceInstanceId(h, ref da, sb, nBytes, out requiredSize);
ans = sb.ToString();
break;
}
int nBytes = BUFFER_SIZE;
StringBuilder sb = new StringBuilder(nBytes);
SetupDiGetDeviceInstanceId(h, ref da, sb, nBytes, out requiredSize);
ans = sb.ToString();
break;
}
i++;
}
while(success);
Marshal.FreeHGlobal(ptrBuf);
SetupDiDestroyDeviceInfoList(h);
i++;
}
while(success);
Marshal.FreeHGlobal(ptrBuf);
SetupDiDestroyDeviceInfoList(h);
return ans;
}