Do not assume pointers are 32-bit in several Windows device calls.

This commit is contained in:
2018-06-20 18:52:54 +01:00
parent e92b6c6715
commit dc1884f5d8
3 changed files with 17 additions and 17 deletions

View File

@@ -737,7 +737,7 @@ namespace DiscImageChef.Devices.Windows
commandData.deviceDataBufferSize]; commandData.deviceDataBufferSize];
IntPtr hBuf = Marshal.AllocHGlobal(commandB.Length); IntPtr hBuf = Marshal.AllocHGlobal(commandB.Length);
Marshal.StructureToPtr(commandData, hBuf, true); Marshal.StructureToPtr(commandData, hBuf, true);
IntPtr descriptorOffset = new IntPtr(hBuf.ToInt32() + commandData.size); IntPtr descriptorOffset = IntPtr.Add(hBuf, commandData.size);
Marshal.StructureToPtr(commandDescriptor, descriptorOffset, true); Marshal.StructureToPtr(commandDescriptor, descriptorOffset, true);
Marshal.Copy(hBuf, commandB, 0, commandB.Length); Marshal.Copy(hBuf, commandB, 0, commandB.Length);
Marshal.FreeHGlobal(hBuf); Marshal.FreeHGlobal(hBuf);

View File

@@ -58,7 +58,7 @@ namespace DiscImageChef.Devices.Windows
// We start at the "root" of the device tree and look for all // We start at the "root" of the device tree and look for all
// devices that match the interface GUID of a Hub Controller // devices that match the interface GUID of a Hub Controller
IntPtr h = SetupDiGetClassDevs(ref hostGuid, 0, IntPtr.Zero, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); IntPtr h = SetupDiGetClassDevs(ref hostGuid, 0, IntPtr.Zero, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if(h.ToInt32() == INVALID_HANDLE_VALUE) return new ReadOnlyCollection<UsbController>(hostList); if(h == INVALID_HANDLE_VALUE) return new ReadOnlyCollection<UsbController>(hostList);
IntPtr ptrBuf = Marshal.AllocHGlobal(BUFFER_SIZE); IntPtr ptrBuf = Marshal.AllocHGlobal(BUFFER_SIZE);
bool success; bool success;
@@ -128,7 +128,7 @@ namespace DiscImageChef.Devices.Windows
// Use the "enumerator form" of the SetupDiGetClassDevs API // Use the "enumerator form" of the SetupDiGetClassDevs API
// to generate a list of all USB devices // to generate a list of all USB devices
IntPtr h = SetupDiGetClassDevs(0, DEV_ENUM, IntPtr.Zero, DIGCF_PRESENT | DIGCF_ALLCLASSES); IntPtr h = SetupDiGetClassDevs(0, DEV_ENUM, IntPtr.Zero, DIGCF_PRESENT | DIGCF_ALLCLASSES);
if(h.ToInt32() == INVALID_HANDLE_VALUE) return ans; if(h == INVALID_HANDLE_VALUE) return ans;
IntPtr ptrBuf = Marshal.AllocHGlobal(BUFFER_SIZE); IntPtr ptrBuf = Marshal.AllocHGlobal(BUFFER_SIZE);
@@ -184,7 +184,7 @@ namespace DiscImageChef.Devices.Windows
// Use the "enumerator form" of the SetupDiGetClassDevs API // Use the "enumerator form" of the SetupDiGetClassDevs API
// to generate a list of all USB devices // to generate a list of all USB devices
IntPtr h = SetupDiGetClassDevs(0, DEV_ENUM, IntPtr.Zero, DIGCF_PRESENT | DIGCF_ALLCLASSES); IntPtr h = SetupDiGetClassDevs(0, DEV_ENUM, IntPtr.Zero, DIGCF_PRESENT | DIGCF_ALLCLASSES);
if(h.ToInt32() == INVALID_HANDLE_VALUE) return ans; if(h == INVALID_HANDLE_VALUE) return ans;
IntPtr ptrBuf = Marshal.AllocHGlobal(BUFFER_SIZE); IntPtr ptrBuf = Marshal.AllocHGlobal(BUFFER_SIZE);
@@ -271,7 +271,7 @@ namespace DiscImageChef.Devices.Windows
// Open a handle to the Host Controller // Open a handle to the Host Controller
h = CreateFile(ControllerDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, h = CreateFile(ControllerDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0,
IntPtr.Zero); IntPtr.Zero);
if(h.ToInt32() == INVALID_HANDLE_VALUE) return root; if(h == INVALID_HANDLE_VALUE) return root;
UsbRootHubName hubName = new UsbRootHubName(); UsbRootHubName hubName = new UsbRootHubName();
int nBytes = Marshal.SizeOf(hubName); int nBytes = Marshal.SizeOf(hubName);
@@ -290,7 +290,7 @@ namespace DiscImageChef.Devices.Windows
// Now let's open the Hub (based upon the HubName we got above) // 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, h2 = CreateFile(root.HubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0,
IntPtr.Zero); IntPtr.Zero);
if(h2.ToInt32() != INVALID_HANDLE_VALUE) if(h2 != INVALID_HANDLE_VALUE)
{ {
UsbNodeInformation nodeInfo = new UsbNodeInformation {NodeType = (int)UsbHubNode.UsbHub}; UsbNodeInformation nodeInfo = new UsbNodeInformation {NodeType = (int)UsbHubNode.UsbHub};
nBytes = Marshal.SizeOf(nodeInfo); nBytes = Marshal.SizeOf(nodeInfo);
@@ -379,7 +379,7 @@ namespace DiscImageChef.Devices.Windows
// Open a handle to the Hub device // Open a handle to the Hub device
IntPtr h = CreateFile(HubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr h = CreateFile(HubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0,
IntPtr.Zero); IntPtr.Zero);
if(h.ToInt32() == INVALID_HANDLE_VALUE) return new ReadOnlyCollection<UsbPort>(portList); if(h == INVALID_HANDLE_VALUE) return new ReadOnlyCollection<UsbPort>(portList);
int nBytes = Marshal.SizeOf(typeof(UsbNodeConnectionInformationEx)); int nBytes = Marshal.SizeOf(typeof(UsbNodeConnectionInformationEx));
IntPtr ptrNodeConnection = Marshal.AllocHGlobal(nBytes); IntPtr ptrNodeConnection = Marshal.AllocHGlobal(nBytes);
@@ -482,7 +482,7 @@ namespace DiscImageChef.Devices.Windows
// Open a handle to the Hub device // Open a handle to the Hub device
IntPtr h = CreateFile(PortHubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr h = CreateFile(PortHubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0,
IntPtr.Zero); IntPtr.Zero);
if(h.ToInt32() == INVALID_HANDLE_VALUE) return device; if(h == INVALID_HANDLE_VALUE) return device;
int nBytesReturned; int nBytesReturned;
int nBytes = BUFFER_SIZE; int nBytes = BUFFER_SIZE;
@@ -519,7 +519,7 @@ namespace DiscImageChef.Devices.Windows
// the Request structure. Because this location is not "covered" // the Request structure. Because this location is not "covered"
// by the structure allocation, we're forced to zero out this // by the structure allocation, we're forced to zero out this
// chunk of memory by using the StringToHGlobalAuto() hack above // chunk of memory by using the StringToHGlobalAuto() hack above
IntPtr ptrStringDesc = new IntPtr(ptrRequest.ToInt32() + Marshal.SizeOf(request)); IntPtr ptrStringDesc = IntPtr.Add(ptrRequest, Marshal.SizeOf(request));
UsbStringDescriptor stringDesc = UsbStringDescriptor stringDesc =
(UsbStringDescriptor)Marshal.PtrToStructure(ptrStringDesc, typeof(UsbStringDescriptor)); (UsbStringDescriptor)Marshal.PtrToStructure(ptrStringDesc, typeof(UsbStringDescriptor));
device.DeviceManufacturer = stringDesc.bString; device.DeviceManufacturer = stringDesc.bString;
@@ -549,7 +549,7 @@ namespace DiscImageChef.Devices.Windows
nBytes, out nBytesReturned, IntPtr.Zero)) nBytes, out nBytesReturned, IntPtr.Zero))
{ {
// the location of the string descriptor is immediately after the Request structure // the location of the string descriptor is immediately after the Request structure
IntPtr ptrStringDesc = new IntPtr(ptrRequest.ToInt32() + Marshal.SizeOf(request)); IntPtr ptrStringDesc = IntPtr.Add(ptrRequest, Marshal.SizeOf(request));
UsbStringDescriptor stringDesc = UsbStringDescriptor stringDesc =
(UsbStringDescriptor)Marshal.PtrToStructure(ptrStringDesc, typeof(UsbStringDescriptor)); (UsbStringDescriptor)Marshal.PtrToStructure(ptrStringDesc, typeof(UsbStringDescriptor));
device.DeviceProduct = stringDesc.bString; device.DeviceProduct = stringDesc.bString;
@@ -579,7 +579,7 @@ namespace DiscImageChef.Devices.Windows
nBytes, out nBytesReturned, IntPtr.Zero)) nBytes, out nBytesReturned, IntPtr.Zero))
{ {
// the location of the string descriptor is immediately after the Request structure // the location of the string descriptor is immediately after the Request structure
IntPtr ptrStringDesc = new IntPtr(ptrRequest.ToInt32() + Marshal.SizeOf(request)); IntPtr ptrStringDesc = IntPtr.Add(ptrRequest, Marshal.SizeOf(request));
UsbStringDescriptor stringDesc = UsbStringDescriptor stringDesc =
(UsbStringDescriptor)Marshal.PtrToStructure(ptrStringDesc, typeof(UsbStringDescriptor)); (UsbStringDescriptor)Marshal.PtrToStructure(ptrStringDesc, typeof(UsbStringDescriptor));
device.DeviceSerialNumber = stringDesc.bString; device.DeviceSerialNumber = stringDesc.bString;
@@ -602,7 +602,7 @@ namespace DiscImageChef.Devices.Windows
if(DeviceIoControl(h, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, dcrPtrRequest, nBytes, if(DeviceIoControl(h, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, dcrPtrRequest, nBytes,
dcrPtrRequest, nBytes, out nBytesReturned, IntPtr.Zero)) dcrPtrRequest, nBytes, out nBytesReturned, IntPtr.Zero))
{ {
IntPtr ptrStringDesc = new IntPtr(dcrPtrRequest.ToInt32() + Marshal.SizeOf(dcrRequest)); IntPtr ptrStringDesc = IntPtr.Add(dcrPtrRequest, Marshal.SizeOf(dcrRequest));
device.BinaryDeviceDescriptors = new byte[nBytesReturned]; device.BinaryDeviceDescriptors = new byte[nBytesReturned];
Marshal.Copy(ptrStringDesc, device.BinaryDeviceDescriptors, 0, nBytesReturned); Marshal.Copy(ptrStringDesc, device.BinaryDeviceDescriptors, 0, nBytesReturned);
} }
@@ -650,7 +650,7 @@ namespace DiscImageChef.Devices.Windows
// Open a handle to the Host Controller // Open a handle to the Host Controller
h = CreateFile(PortHubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, h = CreateFile(PortHubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0,
IntPtr.Zero); IntPtr.Zero);
if(h.ToInt32() == INVALID_HANDLE_VALUE) return hub; if(h == INVALID_HANDLE_VALUE) return hub;
// Get the DevicePath for downstream hub // Get the DevicePath for downstream hub
UsbNodeConnectionName nodeName = new UsbNodeConnectionName {ConnectionIndex = PortPortNumber}; UsbNodeConnectionName nodeName = new UsbNodeConnectionName {ConnectionIndex = PortPortNumber};
int nBytes = Marshal.SizeOf(nodeName); int nBytes = Marshal.SizeOf(nodeName);
@@ -669,7 +669,7 @@ namespace DiscImageChef.Devices.Windows
// Now let's open the Hub (based upon the HubName we got above) // 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, h2 = CreateFile(hub.HubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0,
IntPtr.Zero); IntPtr.Zero);
if(h2.ToInt32() != INVALID_HANDLE_VALUE) if(h2 != INVALID_HANDLE_VALUE)
{ {
UsbNodeInformation nodeInfo = new UsbNodeInformation {NodeType = (int)UsbHubNode.UsbHub}; UsbNodeInformation nodeInfo = new UsbNodeInformation {NodeType = (int)UsbHubNode.UsbHub};
nBytes = Marshal.SizeOf(nodeInfo); nBytes = Marshal.SizeOf(nodeInfo);
@@ -759,7 +759,7 @@ namespace DiscImageChef.Devices.Windows
const int FILE_SHARE_READ = 0x1; const int FILE_SHARE_READ = 0x1;
const int FILE_SHARE_WRITE = 0x2; const int FILE_SHARE_WRITE = 0x2;
const int OPEN_EXISTING = 0x3; const int OPEN_EXISTING = 0x3;
const int INVALID_HANDLE_VALUE = -1; static readonly IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
const int IOCTL_GET_HCD_DRIVERKEY_NAME = 0x220424; const int IOCTL_GET_HCD_DRIVERKEY_NAME = 0x220424;
const int IOCTL_USB_GET_ROOT_HUB_NAME = 0x220408; const int IOCTL_USB_GET_ROOT_HUB_NAME = 0x220408;

View File

@@ -209,7 +209,7 @@ namespace DiscImageChef.Devices.Windows
// We start at the "root" of the device tree and look for all // We start at the "root" of the device tree and look for all
// devices that match the interface GUID of a disk // devices that match the interface GUID of a disk
IntPtr h = SetupDiGetClassDevs(ref diskGuid, 0, IntPtr.Zero, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); IntPtr h = SetupDiGetClassDevs(ref diskGuid, 0, IntPtr.Zero, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if(h.ToInt32() != INVALID_HANDLE_VALUE) if(h != INVALID_HANDLE_VALUE)
{ {
bool success; bool success;
int i = 0; int i = 0;
@@ -274,7 +274,7 @@ namespace DiscImageChef.Devices.Windows
int ans = -1; int ans = -1;
IntPtr h = CreateFile(devicePath.TrimEnd('\\'), 0, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero); IntPtr h = CreateFile(devicePath.TrimEnd('\\'), 0, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
if(h.ToInt32() == INVALID_HANDLE_VALUE) return ans; if(h == INVALID_HANDLE_VALUE) return ans;
StorageDeviceNumber sdn = new StorageDeviceNumber(); StorageDeviceNumber sdn = new StorageDeviceNumber();
int nBytes = Marshal.SizeOf(sdn); int nBytes = Marshal.SizeOf(sdn);