線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:5486
推到 Plurk!
推到 Facebook!

about USBview(IOCTL_USB_GET_NODE_INFORMATION)

尚未結案
jorping
一般會員


發表:3
回覆:5
積分:1
註冊:2006-11-15

發送簡訊給我
#1 引用回覆 回覆 發表時間:2006-12-07 09:42:25 IP:220.112.xxx.xxx 訂閱
各位大大,小弟在轉換USBVIEW的程式時遇到個問題,在利用DEVICEIOCONTROLIOCTL_USB_GET_NODE_INFORMATION時,總是返回FALSE,下面是我的CODE,我已在出現問題的地方標出, 希望大大們 指點下:謝謝!
//---------------------CODE------------------------
void TForm1::ShowInformation(String str)
{
//TODO: Add your source code here
this->Memo1->Text = this->Memo1->Text str;
this->Memo1->Lines->Add("\n");
}

void TForm1::NumUsbAll(void)
{
//TODO: Add your source code here
char strShow[64];
String ShowInfo;
char HCName[16];
int HCNum;
HANDLE hHCDev;
PCHAR rootHubName;
PCHAR leafName;

ULONG index;
bool success;
PUSB_NODE_CONNECTION_INFORMATION connectionInfo;
HANDLE hHubDevice;
PCHAR driverKeyName ,deviceDesc;
ULONG nBytes;
PUSB_NODE_INFORMATION HubInfo;
PCHAR deviceName = NULL;
int port;
for(HCNum = 0;HCNum < NUM_HCS_TO_CHECK;HCNum )
{
wsprintf(HCName,"\\\\.\\HCD%d",HCNum);
hHCDev = CreateFile(HCName,
GENERIC_WRITE,
FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
if(hHCDev != INVALID_HANDLE_VALUE)
{
wsprintf(strShow,"找到USB控制器%d\n",HCNum 1);
ShowInfo = strShow;
ShowInformation(ShowInfo);
}
else
break;
driverKeyName = GetHCDDriverKeyName(hHCDev);
if(driverKeyName)
{
;//;
}
else
{
ShowInformation("錯誤!\r\n");
goto end;
}
wsprintf(strShow,"USB控制器%s\r\n",driverKeyName);
ShowInfo = strShow;
ShowInformation(ShowInfo);

leafName = HCName sizeof("\\\\.\\") - sizeof("");
wsprintf(strShow,"USB控制器名稱:%s\r\n",leafName);
ShowInfo = strShow;
ShowInformation(ShowInfo);

rootHubName = GetRootHubName(hHCDev);
if(rootHubName == NULL)
{
ShowInformation("錯誤!\r\n");
goto end;
}
wsprintf(strShow,"USB控制器:%s",rootHubName);
ShowInfo = strShow;
ShowInformation(ShowInfo);

HubInfo = (PUSB_NODE_INFORMATION)malloc
(sizeof(USB_NODE_INFORMATION));
deviceName = (PCHAR)malloc(strlen(rootHubName)
sizeof("\\\\.\\"));
if(rootHubName!=NULL)
{
strcpy(deviceName,"\\\\.\\");
strcpy(deviceName sizeof("\\\\.\\")-1,rootHubName);
hHubDevice = CreateFile(deviceName,
GENERIC_WRITE,
FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
free(deviceName);
if(hHubDevice == INVALID_HANDLE_VALUE)
{
ShowInformation("錯誤!");
goto end;
}
success = DeviceIoControl(hHubDevice,
IOCTL_USB_GET_NODE_INFORMATION,
HubInfo,
sizeof(USB_NODE_INFORMATION),
HubInfo,
sizeof(USB_NODE_INFORMATION),
&nBytes,
NULL);
if(!success) //這裏返回的總是false,VC下就是TRUE為什麼啊
{
ShowInformation("錯誤!");
goto end;
}
} //if(rootHubName!=Null)結束
port = HubInfo->u.HubInformation.HubDescriptor.bNumberOfPorts;
for(index = 1;index <= port;index )
{
ULONG nBytes;
nBytes = sizeof(USB_NODE_CONNECTION_INFORMATION)
sizeof(USB_PIPE_INFO)*30;
connectionInfo = (PUSB_NODE_CONNECTION_INFORMATION)
malloc(nBytes);
if(connectionInfo == NULL)
{
ShowInformation("錯誤!");
goto end;
}
connectionInfo->ConnectionIndex = index;
success = DeviceIoControl(hHubDevice,
IOCTL_USB_GET_NODE_CONNECTION_INFORMATION,
connectionInfo,
nBytes,
connectionInfo,
nBytes,
&nBytes,
NULL);
if(!success)
{
free(connectionInfo);
ShowInformation("錯誤!");
goto end;
}
if(connectionInfo)
{
if(connectionInfo->ConnectionStatus == DeviceConnected)
{
wsprintf(strShow,"%d埠有usb設備連接.",index);
ShowInfo = strShow;
ShowInformation(ShowInfo);
}
else
{
wsprintf(strShow,"%d埠沒有usb設備連接.",index);
ShowInfo = strShow;
ShowInformation(ShowInfo);
}
}//if(connection)結束

}//for-index結束

ShowInformation("--------------------------------------------");
}
end:
}
//輔助函數

PCHAR TForm1::GetHCDDriverKeyName(HANDLE HCD)
{
//TODO: Add your source code here
bool success;
ULONG nBytes;
USB_HCD_DRIVERKEY_NAME driverKeyName;
PUSB_HCD_DRIVERKEY_NAME driverKeyNameW;
PCHAR driverKeyNameA;
driverKeyNameW = NULL;
driverKeyNameA = NULL;
success = DeviceIoControl(HCD,
IOCTL_GET_HCD_DRIVERKEY_NAME,
&driverKeyName,
sizeof(driverKeyName),
&driverKeyName,
sizeof(driverKeyName),
&nBytes,
NULL);
if(!success)
{
goto GetHCDDriverKeyNameError;
}
nBytes = driverKeyName.ActualLength;
if(nBytes <= sizeof(driverKeyName))
{
goto GetHCDDriverKeyNameError;
}
driverKeyNameW = (PUSB_HCD_DRIVERKEY_NAME)malloc(nBytes);
if(driverKeyNameW == NULL)
{
goto GetHCDDriverKeyNameError;
}
success = DeviceIoControl(HCD,
IOCTL_GET_HCD_DRIVERKEY_NAME,
driverKeyNameW,
nBytes,
driverKeyNameW,
nBytes,
&nBytes,
NULL);
if(!success)
{
goto GetHCDDriverKeyNameError;
}
driverKeyNameA = WideStrToMultiStr(driverKeyNameW->DriverKeyName );
free(driverKeyNameW);
return driverKeyNameA;

GetHCDDriverKeyNameError:
if (driverKeyNameW != NULL)
{
ShowInformation("出錯!\r\n");
free(driverKeyNameW);
driverKeyNameW = NULL;
}
return NULL;

}

//資料處理函數
PCHAR TForm1::WideStrToMultiStr(PWCHAR WideStr)
{
//TODO: Add your source code here
ULONG nBytes;
PCHAR MultiStr;
nBytes = WideCharToMultiByte(
CP_ACP,
0,
WideStr,
-1,
NULL,
0,
NULL,
NULL);

if (nBytes == 0)
{
return NULL;
}
MultiStr =(PCHAR) malloc(nBytes);

if (MultiStr == NULL)
{
return NULL;
}
nBytes = WideCharToMultiByte(
CP_ACP,
0,
WideStr,
-1,
MultiStr,
nBytes,
NULL,
NULL);

if (nBytes == 0)
{
free(MultiStr);
return NULL;
}

return MultiStr;
}
//輔助函數
PCHAR TForm1::GetRootHubName(HANDLE HostController)
{
//TODO: Add your source code here
bool success;
ULONG nBytes;
USB_ROOT_HUB_NAME rootHubName;
PUSB_ROOT_HUB_NAME rootHubNameW;
PCHAR rootHubNameA;

rootHubNameW = NULL;
rootHubNameA =NULL;

success = DeviceIoControl(HostController,
IOCTL_USB_GET_ROOT_HUB_NAME,
0,
0,
&rootHubName,
sizeof(rootHubName),
&nBytes,
NULL);
if(!success)
{
goto GetRootHubNameError;
}
nBytes = rootHubName.ActualLength;
rootHubNameW = (PUSB_ROOT_HUB_NAME)malloc(nBytes);
success = false;
success = DeviceIoControl(HostController,
IOCTL_USB_GET_ROOT_HUB_NAME,
NULL,
0,
rootHubNameW,
nBytes,
&nBytes,
NULL);
if(!success)
{
goto GetRootHubNameError;
}
rootHubNameA = WideStrToMultiStr(rootHubNameW->RootHubName);
free(rootHubNameW);
return(rootHubNameA);
GetRootHubNameError:
if (rootHubNameW != NULL)
{
free(rootHubNameW);
rootHubNameW = NULL;
ShowInformation("出錯!\r\n");
}
return NULL;
}

------
Jordan.Jeng
編輯記錄
dllee 重新編輯於 2007-04-19 23:55:03, 註解 如果是提問,請記得將文章分類設為 [問題] 喔,謝謝您的配合。‧
stnovel
一般會員


發表:4
回覆:4
積分:1
註冊:2007-01-10

發送簡訊給我
#2 引用回覆 回覆 發表時間:2007-01-10 13:03:09 IP:61.229.xxx.xxx 訂閱
PUSB_NODE_INFORMATION HubInfo;
不是這樣宣告,改成
PUSBDEVICEINFO info;

info = NULL;
info = (PUSBDEVICEINFO) alloca(sizeof(USBDEVICEINFO));
info->HubInfo = (PUSB_NODE_INFORMATION)alloca(sizeof(USB_NODE_INFORMATION));

然後這樣就可
// Root hub is open. Collect the node information
Success = DeviceIoControl(RootHubHandle, IOCTL_USB_GET_NODE_INFORMATION, info->HubInfo,
sizeof(USB_NODE_INFORMATION), info->HubInfo, sizeof(USB_NODE_INFORMATION), &BytesReturned, NULL);

if (!Success)
return GetLastError();
hap0702
一般會員


發表:0
回覆:5
積分:1
註冊:2007-04-04

發送簡訊給我
#3 引用回覆 回覆 發表時間:2007-04-09 11:05:46 IP:220.128.xxx.xxx 訂閱
各位大大:
能否將source code寄給我參考作範例
謝謝


===================引 用 文 章===================
PUSB_NODE_INFORMATION HubInfo;
不是這樣宣告,改成
PUSBDEVICEINFO info;

info = NULL;
info = (PUSBDEVICEINFO) alloca(sizeof(USBDEVICEINFO));
info->HubInfo = (PUSB_NODE_INFORMATION)alloca(sizeof(USB_NODE_INFORMATION));

然後這樣就可
// Root hub is open. Collect the node information
Success = DeviceIoControl(RootHubHandle, IOCTL_USB_GET_NODE_INFORMATION, info->HubInfo,
sizeof(USB_NODE_INFORMATION), info->HubInfo, sizeof(USB_NODE_INFORMATION), &BytesReturned, NULL);

if (!Success)
return GetLastError();
stnovel
一般會員


發表:4
回覆:4
積分:1
註冊:2007-01-10

發送簡訊給我
#4 引用回覆 回覆 發表時間:2007-04-12 13:55:45 IP:61.229.xxx.xxx 訂閱
hap0702
一般會員


發表:0
回覆:5
積分:1
註冊:2007-04-04

發送簡訊給我
#5 引用回覆 回覆 發表時間:2007-04-13 09:43:25 IP:220.128.xxx.xxx 訂閱
您好
感謝回覆

===================引 用 文 章===================
Please see this
http://delphi.ktop.com.tw/board.php?cid=31&fid=79&tid=87849
系統時間:2017-12-15 6:30:20
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!