On Sun, Jun 17, 2018 at 1:59 AM, xxxxx@probo.com wrote:
> On Jun 16, 2018, at 9:55 PM, xxxxx@gmail.com wrote:
>>
>> The code below works in one case but not in another. I have two
>> functions: EnumHubs and EnumUSBDevices that both need to call the same
>> IOCTL. The code which follows is from EnumUSBDevices, which is passed
>> a wstring explicitly. EnumHubs uses a stack to visit all hubs. The
>> class of the devices in both cases should be GUID_DEVINTERFACE_USB_HUB
>> which is what the IOCTL requires.
>
> What doesn’t work? What error do you get, and where?
Er, my apologies. I thought I put it in the title. I get 0x6,
ERROR_INVALID_HANDLE. This only happens when I try to enumerate a
single hub specifically. When I iterate through all hubs and devices
to only find the hubs it seems to work, and the code is as far as I
can tell exactly the same. The error is in EnumUSBDevices at the first
IOCTL.
EnumHubs:
vector EnumHubs(
In const vector rhubs
)
{
vector r;
stack> s(rhubs);
while (!s.empty()) {
const auto hub = s.top(); s.pop();
HANDLE hHub = CreateFile(
hub.c_str(),
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
);
if (!hHub) {
wprintf(L"CreateFile: %d\n", GetLastError());
exit(1);
}
USB_HUB_INFORMATION_EX uhie; DWORD nBytes = 0;
memset(&uhie, 0, sizeof(USB_HUB_INFORMATION_EX));
if (!DeviceIoControl(
hHub,
IOCTL_USB_GET_HUB_INFORMATION_EX,
NULL, NULL,
&uhie, sizeof(USB_HUB_INFORMATION_EX),
&nBytes,
NULL
)) {
wprintf(L"DeviceIoControl: %d\n", GetLastError());
}
wprintf(L"%u %d\n", nBytes, sizeof(USB_HUB_INFORMATION_EX));
for (int i = 1; i <= uhie.HighestPortNumber; i++) {
USB_NODE_CONNECTION_INFORMATION_EX uncie;
memset(&uncie, 0, sizeof(USB_NODE_CONNECTION_INFORMATION_EX));
uncie.ConnectionIndex = i;
if (!DeviceIoControl(
hHub,
IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX,
&uncie, sizeof(USB_NODE_CONNECTION_INFORMATION_EX),
&uncie, sizeof(USB_NODE_CONNECTION_INFORMATION_EX),
NULL,
NULL
)) {
wprintf(L"DeviceIoControl: %d\n", GetLastError());
}
if (!uncie.DeviceIsHub)
continue;
USB_NODE_CONNECTION_NAME uncn;
memset(&uncn, 0, sizeof(USB_NODE_CONNECTION_NAME));
uncn.ConnectionIndex = i;
if (!DeviceIoControl(
hHub,
IOCTL_USB_GET_NODE_CONNECTION_NAME,
&uncn, sizeof(USB_NODE_CONNECTION_NAME),
&uncn, sizeof(USB_NODE_CONNECTION_NAME),
NULL,
NULL
)) {
wprintf(L"DeviceIoControl: %d\n", GetLastError());
}
PUSB_NODE_CONNECTION_NAME puncn =
(PUSB_NODE_CONNECTION_NAME)malloc(uncn.ActualLength);
puncn->ConnectionIndex = i;
if (!DeviceIoControl(
hHub,
IOCTL_USB_GET_NODE_CONNECTION_NAME,
puncn, sizeof(USB_NODE_CONNECTION_NAME),
puncn, uncn.ActualLength,
NULL,
NULL
)) {
wprintf(L"DeviceIoControl: %d\n", GetLastError());
}
auto t = L"\\?\" + wstring(puncn->NodeName);
r.push_back(t);
s.push(t);
}
}
return r;
}
And EnumUSBDevices:
vector EnumUSBDevices(
In const wstring hub
)
{
wprintf(L"%s\n", hub.c_str());
vector r;
HANDLE hHub = CreateFile(
hub.c_str(),
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
);
if (!hHub) {
wprintf(L"EnumUSBDevices: CreateFile: %d\n", GetLastError());
exit(1);
}
USB_HUB_INFORMATION_EX uhie; DWORD nBytes = 0;
memset(&uhie, 0, sizeof(USB_HUB_INFORMATION_EX));
if (!DeviceIoControl(
hHub,
IOCTL_USB_GET_HUB_INFORMATION_EX,
&uhie, sizeof(USB_HUB_INFORMATION_EX),
&uhie, sizeof(USB_HUB_INFORMATION_EX),
&nBytes,
NULL
)) {
wprintf(L"EnumUSBDevices: DeviceIoControl 1: %d, %s\n",
GetLastError(), hub.c_str());
}
wprintf(L"%u %d\n", nBytes, sizeof(USB_HUB_INFORMATION_EX));
for (int i = 1; i <= uhie.HighestPortNumber; i++) {
USB_NODE_CONNECTION_INFORMATION_EX uncie;
memset(&uncie, 0, sizeof(USB_NODE_CONNECTION_INFORMATION_EX));
uncie.ConnectionIndex = i;
if (!DeviceIoControl(
hHub,
IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX,
&uncie, sizeof(USB_NODE_CONNECTION_INFORMATION_EX),
&uncie, sizeof(USB_NODE_CONNECTION_INFORMATION_EX),
NULL,
NULL
)) {
wprintf(L"EnumUSBDevices: DeviceIoControl 2: %d\n", GetLastError());
}
USB_NODE_CONNECTION_NAME uncn;
memset(&uncn, 0, sizeof(USB_NODE_CONNECTION_NAME));
uncn.ConnectionIndex = i;
if (!DeviceIoControl(
hHub,
IOCTL_USB_GET_NODE_CONNECTION_NAME,
&uncn, sizeof(USB_NODE_CONNECTION_NAME),
&uncn, sizeof(USB_NODE_CONNECTION_NAME),
NULL,
NULL
)) {
wprintf(L"EnumUSBDevices: DeviceIoControl 3: %d\n", GetLastError());
}
PUSB_NODE_CONNECTION_NAME puncn =
(PUSB_NODE_CONNECTION_NAME)malloc(uncn.ActualLength);
puncn->ConnectionIndex = i;
if (!DeviceIoControl(
hHub,
IOCTL_USB_GET_NODE_CONNECTION_NAME,
puncn, sizeof(USB_NODE_CONNECTION_NAME),
puncn, uncn.ActualLength,
NULL,
NULL
)) {
wprintf(L"EnumUSBDevices: DeviceIoControl 4: %d\n", GetLastError());
}
auto t = L"\\?\" + wstring(puncn->NodeName);
r.push_back(t);
}
return r;
}
Thanks in advance,
R0b0t1