資源簡介
c++ 實現枚舉USB設備接口
涉及函數:
SetupDiGetClassDevs
SetupDiEnumDeviceInterfaces
SetupDiGetDeviceInterfaceDetail
涉及函數:
SetupDiGetClassDevs
SetupDiEnumDeviceInterfaces
SetupDiGetDeviceInterfaceDetail
代碼片段和文件信息
//?EnumDevice.cpp?:?Defines?the?entry?point?for?the?console?application.
//
#include?“stdafx.h“
#include??
#include?
#include?
#include?
#include?
using?namespace?std;
//U盤?interface?class?GUID?
GUID?IID_CLASS_WCEUSBS?={0xa5dcbf10?0x6530?0x11d2?0x90?0x1f?0x00?0xc0?0x4f?0xb9?0x51?0xed};
BOOL?SearchDevice(vector?&vDevicePath)
{
BOOL?bRes?=?FALSE;
LPGUID?pInterfaceGuid?=?&IID_CLASS_WCEUSBS;
HDEVINFO?hDeviceInfo?=?SetupDiGetClassDevs(?pInterfaceGuid
NULL
NULL
DIGCF_PRESENT?|?DIGCF_DEVICEINTERFACE);
if?(INVALID_HANDLE_VALUE?==?hDeviceInfo)
{
goto?Exit;
}
//?enum?device?interface
SP_DEVICE_INTERFACE_DATA?spDevInterData; //a?structure?of?device?interface?data
memset(&spDevInterData?0x00?sizeof(SP_DEVICE_INTERFACE_DATA));
spDevInterData.cbSize?=?sizeof(SP_DEVICE_INTERFACE_DATA);
DWORD?dwIndex?=?0;
while?(TRUE)
{
if?(!SetupDiEnumDeviceInterfaces(?hDeviceInfo
NULL
pInterfaceGuid
dwIndex
&spDevInterData))
{
if?(ERROR_NO_MORE_ITEMS?==?GetLastError())
{
OutputDebugStringW(L“No?more?interface“);
}
else
{
OutputDebugStringW(L“SetupDiEnumDeviceInterfaces?Error“);
}
goto?Exit;
}
//?get?length?of?interface?detail?info
DWORD?dwRequiredLength?=?0; //for?getting?length?of?inter?face?detail?data
if?(!SetupDiGetDeviceInterfaceDetail(?hDeviceInfo
&spDevInterData
NULL?
0
&dwRequiredLength
NULL))
{
if?(ERROR_INSUFFICIENT_BUFFER?!=?GetLastError())
{
OutputDebugStringW(L“calculate?require?length“);
//goto?Exit;
}
}
//?get?interface?detail?info
PSP_DEVICE_INTERFACE_DETAIL_DATA?pSpDIDetailData; //a?pointer?to?interface?detail?data
pSpDIDetailData?=?NULL;
pSpDIDetailData?=?(PSP_DEVICE_INTERFACE_DETAIL_DATA)HeapAlloc(GetProcessHeap()?HEAP_ZERO_MEMORY?dwRequiredLength);
if(NULL?==?pSpDIDetailData)
{
OutputDebugStringW(L“HeapAlloc?Memory?Failed“);
if?(!SetupDiDestroyDeviceInfoList(hDeviceInfo))
{
OutputDebugStringW(L“SetupDiDestroyDeviceInfoList?Error“);
}
goto?Exit;
}
pSpDIDetailData->cbSize?=?sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
if?(!SetupDiGetDeviceInterfaceDetail(?hDeviceInfo
&spDevInterData
pSpDIDetailData
dwRequiredLength
&dwRequiredLength
NULL))
{
OutputDebugStringW(L“SetupDiGetDeviceInterfaceDetail?Error“);
goto?Exit;
}
wstring?wcsDevicePath?=?pSpDIDetailData->DevicePath;
vDevicePath.push_back(wcsDevicePath);?
if?(NULL?!=?pSpDIDetailData)
{
HeapFree(GetProcessHeap()?0?pSpDIDetailData);
pSpDIDetailData?=?NULL;
}
dwIndex++;
}
if?(!SetupDiDestroyDeviceInfoList(hDeviceInfo))
{
OutputDebugStringW(L“SetupDiDestroyDeviceInfoList?Error“);
}
bRes?=?TRUE;
Exit:
return?bRes;
}
int?_tmain(int?argc?_TCHAR*?argv[])
{
vector
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????3241??2008-11-12?15:06??EnumDevice\EnumDevice\EnumDevice.cpp
?????文件???????4597??2008-11-05?19:39??EnumDevice\EnumDevice\EnumDevice.vcproj
?????文件????????297??2008-11-05?19:30??EnumDevice\EnumDevice\stdafx.cpp
?????文件????????516??2008-11-05?19:30??EnumDevice\EnumDevice\stdafx.h
?????文件????????895??2008-11-05?19:30??EnumDevice\EnumDevice.sln
?????目錄??????????0??2008-11-12?15:07??EnumDevice\EnumDevice
?????目錄??????????0??2008-11-12?15:07??EnumDevice
-----------?---------??----------?-----??----
?????????????????9546????????????????????7
- 上一篇:2006年湖北工業大學409數據結構試題
- 下一篇:編譯原理中間代碼生成程序
評論
共有 條評論