SetUpDi系列函数获取系统设备信息
2015-12-01 20:13
288 查看
1 SetupDiGetClassDevs函数
The SetupDiGetClassDevs function returns a handle to a device information set that contains requested device information elements for a local machine.
我的理解也就是这个函数能返回一个包含某个设备集合信息的一个指针。这里的handle暂且这么理解。
用SetupDiGetClassDevs得到的这个handle,最后记得要调用SetupDiDestroyDeviceInfoList来删除。
2 SetupDiEnumDeviceInfo函数
The SetupDiEnumDeviceInfo function returns a
SP_DEVINFO_DATA
structure that specifies a device information element in a device information set.
我的理解:用第一个函数得到了一个设备集合的handle,然后根据此handle用SetupDiEnumDeviceInfo可以枚举出来该设备集合中某一个设备的信息。而这些信息保存在SP_DEVINFO_DATA这样一个结构体中。实际上这里得到的还不是某一个设备的具体信息,这里得到的只是一个指针,这个指针指向了一个包含该设备所有信息的集合。一个设备可能有很多信息,比如设备描述,设备名称,设备占有资源等等。要得到该设备具体的某一项信息需要用到下面要说的第3个函数:
3 SetupDiGetDeviceRegistryProperty
The SetupDiGetDeviceRegistryProperty function retrieves a specified Plug and Play device property.
用这个函数可以在前面得到的指向某一个具体设备信息集合的指针中取出某一项信息。
其实上面三个函数是从不同的层面上来一步一步的得到某一个设备的具体的信息的。
就拿网卡来说吧,这个三个层次如下:
一台机器上的网卡GUID->具体到某一个网卡的信息集合->一个集合中的一个具体的信息。
[cpp]
view plaincopyprint?
BOOL CollectNetCardInfo::GetAllPhysicsNetCardName()
{
BOOL bRet = TRUE;
GUID NetCardClassGuid =
{0X4D36E972, 0XE325, 0X11CE, {0XBF, 0XC1, 0X08, 0X00, 0X2B, 0XE1, 0X03, 0X18}};
//得到一个Class下信息集句柄
m_NewDeviceInfoSet = SetupDiGetClassDevs(&NetCardClassGuid, NULL, NULL, DIGCF_PRESENT);
if(m_NewDeviceInfoSet == INVALID_HANDLE_VALUE)
{
wprintf(_T("NewDeviceInfoSet == INVALID_HANDLE_VALUE"));
bRet = FALSE;
return bRet;
}
//对得到的此class信息集进行枚举
for(int Device_Index=0; ; Device_Index++)
{
memset((void*)&(m_NetCardDevInfoData), 0,
sizeof(SP_DEVINFO_DATA));
m_NetCardDevInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
BOOL bResult = SetupDiEnumDeviceInfo(m_NewDeviceInfoSet, Device_Index, &m_NetCardDevInfoData);
if(bResult == 0)
{
if(GetLastError() == ERROR_NO_MORE_ITEMS)
{
break;
}
}
DWORD dwBufSize = 0;
BYTE *pbReuslt = MySetupDiGetDeviceRegistryProperty(
SPDRP_ENUMERATOR_NAME ,
NULL,&dwBufSize);
if(pbReuslt)
{
TCHAR *tcName = (TCHAR*)pbReuslt;
_wcsupr_s(tcName,dwBufSize);
if(wcscmp(tcName,_T("ROOT")) != 0)
{
m_NetCardNum++;
GetNetCardDes();
}
free(pbReuslt);
}
}
//销毁一个Class的device information set
SetupDiDestroyDeviceInfoList(m_NewDeviceInfoSet);
return bRet;
}
void CollectNetCardInfo::GetNetCardDes()
{
DWORD dwBufSize = 0;
BYTE *pbTemp = MySetupDiGetDeviceRegistryProperty(
SPDRP_DEVICEDESC,
NULL,&dwBufSize);
if(pbTemp)
{
BYTE *pbNetDes =
new BYTE[dwBufSize];
memset(pbNetDes,0x00,dwBufSize);
memcpy(pbNetDes,pbTemp,dwBufSize);
m_NetCardDesMap.insert(make_pair(m_NetCardNum,pbNetDes));
free(pbTemp);
}
}
BYTE* CollectNetCardInfo::MySetupDiGetDeviceRegistryProperty(
IN DWORD Property,
OUT PDWORD PropertyRegDataType ,
OUT PDWORD pdwBufSize)
{
DWORD Required_Size = 0;
BOOL bResult = SetupDiGetDeviceRegistryProperty(
m_NewDeviceInfoSet,
&m_NetCardDevInfoData,
Property ,
PropertyRegDataType,
NULL, 0,
&Required_Size);
if(!bResult)
{
if(ERROR_INSUFFICIENT_BUFFER == GetLastError())
{
*pdwBufSize = Required_Size;
BYTE *PropertyBuffer = (BYTE*)malloc(Required_Size);
bResult = SetupDiGetDeviceRegistryProperty(
m_NewDeviceInfoSet,
&m_NetCardDevInfoData,
Property ,
PropertyRegDataType,
PropertyBuffer,
Required_Size, NULL);
if(bResult)
return PropertyBuffer;
else
{
free(PropertyBuffer);
return NULL;
}
}
else
return NULL;
}
else
return NULL;
}
The SetupDiGetClassDevs function returns a handle to a device information set that contains requested device information elements for a local machine.
我的理解也就是这个函数能返回一个包含某个设备集合信息的一个指针。这里的handle暂且这么理解。
用SetupDiGetClassDevs得到的这个handle,最后记得要调用SetupDiDestroyDeviceInfoList来删除。
2 SetupDiEnumDeviceInfo函数
The SetupDiEnumDeviceInfo function returns a
SP_DEVINFO_DATA
structure that specifies a device information element in a device information set.
我的理解:用第一个函数得到了一个设备集合的handle,然后根据此handle用SetupDiEnumDeviceInfo可以枚举出来该设备集合中某一个设备的信息。而这些信息保存在SP_DEVINFO_DATA这样一个结构体中。实际上这里得到的还不是某一个设备的具体信息,这里得到的只是一个指针,这个指针指向了一个包含该设备所有信息的集合。一个设备可能有很多信息,比如设备描述,设备名称,设备占有资源等等。要得到该设备具体的某一项信息需要用到下面要说的第3个函数:
3 SetupDiGetDeviceRegistryProperty
The SetupDiGetDeviceRegistryProperty function retrieves a specified Plug and Play device property.
用这个函数可以在前面得到的指向某一个具体设备信息集合的指针中取出某一项信息。
其实上面三个函数是从不同的层面上来一步一步的得到某一个设备的具体的信息的。
就拿网卡来说吧,这个三个层次如下:
一台机器上的网卡GUID->具体到某一个网卡的信息集合->一个集合中的一个具体的信息。
[cpp]
view plaincopyprint?
BOOL CollectNetCardInfo::GetAllPhysicsNetCardName()
{
BOOL bRet = TRUE;
GUID NetCardClassGuid =
{0X4D36E972, 0XE325, 0X11CE, {0XBF, 0XC1, 0X08, 0X00, 0X2B, 0XE1, 0X03, 0X18}};
//得到一个Class下信息集句柄
m_NewDeviceInfoSet = SetupDiGetClassDevs(&NetCardClassGuid, NULL, NULL, DIGCF_PRESENT);
if(m_NewDeviceInfoSet == INVALID_HANDLE_VALUE)
{
wprintf(_T("NewDeviceInfoSet == INVALID_HANDLE_VALUE"));
bRet = FALSE;
return bRet;
}
//对得到的此class信息集进行枚举
for(int Device_Index=0; ; Device_Index++)
{
memset((void*)&(m_NetCardDevInfoData), 0,
sizeof(SP_DEVINFO_DATA));
m_NetCardDevInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
BOOL bResult = SetupDiEnumDeviceInfo(m_NewDeviceInfoSet, Device_Index, &m_NetCardDevInfoData);
if(bResult == 0)
{
if(GetLastError() == ERROR_NO_MORE_ITEMS)
{
break;
}
}
DWORD dwBufSize = 0;
BYTE *pbReuslt = MySetupDiGetDeviceRegistryProperty(
SPDRP_ENUMERATOR_NAME ,
NULL,&dwBufSize);
if(pbReuslt)
{
TCHAR *tcName = (TCHAR*)pbReuslt;
_wcsupr_s(tcName,dwBufSize);
if(wcscmp(tcName,_T("ROOT")) != 0)
{
m_NetCardNum++;
GetNetCardDes();
}
free(pbReuslt);
}
}
//销毁一个Class的device information set
SetupDiDestroyDeviceInfoList(m_NewDeviceInfoSet);
return bRet;
}
void CollectNetCardInfo::GetNetCardDes()
{
DWORD dwBufSize = 0;
BYTE *pbTemp = MySetupDiGetDeviceRegistryProperty(
SPDRP_DEVICEDESC,
NULL,&dwBufSize);
if(pbTemp)
{
BYTE *pbNetDes =
new BYTE[dwBufSize];
memset(pbNetDes,0x00,dwBufSize);
memcpy(pbNetDes,pbTemp,dwBufSize);
m_NetCardDesMap.insert(make_pair(m_NetCardNum,pbNetDes));
free(pbTemp);
}
}
BYTE* CollectNetCardInfo::MySetupDiGetDeviceRegistryProperty(
IN DWORD Property,
OUT PDWORD PropertyRegDataType ,
OUT PDWORD pdwBufSize)
{
DWORD Required_Size = 0;
BOOL bResult = SetupDiGetDeviceRegistryProperty(
m_NewDeviceInfoSet,
&m_NetCardDevInfoData,
Property ,
PropertyRegDataType,
NULL, 0,
&Required_Size);
if(!bResult)
{
if(ERROR_INSUFFICIENT_BUFFER == GetLastError())
{
*pdwBufSize = Required_Size;
BYTE *PropertyBuffer = (BYTE*)malloc(Required_Size);
bResult = SetupDiGetDeviceRegistryProperty(
m_NewDeviceInfoSet,
&m_NetCardDevInfoData,
Property ,
PropertyRegDataType,
PropertyBuffer,
Required_Size, NULL);
if(bResult)
return PropertyBuffer;
else
{
free(PropertyBuffer);
return NULL;
}
}
else
return NULL;
}
else
return NULL;
}
相关文章推荐
- HDU 5538 house building
- C++类的静态成员
- bzoj:1675 [Usaco2005 Feb]Rigging the Bovine Election 竞选划区
- Ugly Number II
- C++ vector resize 和 reserve的区别
- HDU2191 多重背包或者01背包 xingxing在努力
- xe10 up1和谐成功
- 【BZOJ 1012】[JSOI2008]最大数maxnumber
- JS异步编程
- Python-sublime构建Python运行,调试环境
- spring boot + velocity中文乱码解决方式
- 最小生成树Prim算法理解
- PAT Basic 1010
- iOS程序启动与运转
- poj1149--PIGS(最大流)
- centos升级内核
- BestCoder Round #64 1002
- AWS-CDH5.5安装-安装
- maven 中的内置变量
- 建立 IT 持续交付的能力