WIN32编程 -- 通过使用SetupDi 调用函数枚举硬件设备
2007-05-21 14:58
369 查看
要得到 Windows 2000 中安装的硬件设备列表,应用程序可以调用 SetupDi 类 API 函数。
loadTOCNode(1, 'moreinformation');
以下代码段演示如何显示已经安装的所有硬件设备的列表:
#include <stdio.h>
#include <windows.h>
#include <setupapi.h>
#include <devguid.h>
#include <regstr.h>
#pragma comment(lib,"Setupapi.lib")
int main( int argc, char *argv[ ], char *envp[ ] )
{
HDEVINFO hDevInfo;
SP_DEVINFO_DATA DeviceInfoData;
DWORD i;
// Create a HDEVINFO with all present devices.
hDevInfo = SetupDiGetClassDevs(NULL,
0, // Enumerator
0,
DIGCF_PRESENT | DIGCF_ALLCLASSES );
if (hDevInfo == INVALID_HANDLE_VALUE)
{
// Insert error handling here.
return 1;
}
// Enumerate through all devices in Set.
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for (i=0;SetupDiEnumDeviceInfo(hDevInfo,i,
&DeviceInfoData);i++)
{
DWORD DataT;
LPTSTR buffer = NULL;
DWORD buffersize = 0;
//
// Call function with null to begin with,
// then use the returned buffer size
// to Alloc the buffer. Keep calling until
// success or an unknown failure.
//
while (!SetupDiGetDeviceRegistryProperty(
hDevInfo,
&DeviceInfoData,
SPDRP_DEVICEDESC,
&DataT,
(PBYTE)buffer,
buffersize,
&buffersize))
{
if (GetLastError() ==
ERROR_INSUFFICIENT_BUFFER)
{
// Change the buffer size.
if (buffer) LocalFree(buffer);
buffer = (LPTSTR)LocalAlloc(LPTR,buffersize);
}
else
{
// Insert error handling here.
break;
}
}
printf("Result:[%s]\n",buffer);
if (buffer) LocalFree(buffer);
}
if ( GetLastError()!=NO_ERROR &&
GetLastError()!=ERROR_NO_MORE_ITEMS )
{
// Insert error handling here.
return 1;
}
// Cleanup
SetupDiDestroyDeviceInfoList(hDevInfo);
return 0;
}
以下代码段演示如何检索系统上的所有显示适配器组: hDevInfo = SetupDiGetClassDevs(
(LPGUID) &GUID_DEVCLASS_DISPLAY,
0,
0,
DIGCF_PRESENT);
以下代码段演示如何检索外围组件互连 (PCI) 总线上的所有设备组: hDevInfo = SetupDiGetClassDevs(NULL,
REGSTR_KEY_PCIENUM, // Enumerator
0,
DIGCF_PRESENT | DIGCF_ALLCLASSES );
loadTOCNode(1, 'moreinformation');
以下代码段演示如何显示已经安装的所有硬件设备的列表:
#include <stdio.h>
#include <windows.h>
#include <setupapi.h>
#include <devguid.h>
#include <regstr.h>
#pragma comment(lib,"Setupapi.lib")
int main( int argc, char *argv[ ], char *envp[ ] )
{
HDEVINFO hDevInfo;
SP_DEVINFO_DATA DeviceInfoData;
DWORD i;
// Create a HDEVINFO with all present devices.
hDevInfo = SetupDiGetClassDevs(NULL,
0, // Enumerator
0,
DIGCF_PRESENT | DIGCF_ALLCLASSES );
if (hDevInfo == INVALID_HANDLE_VALUE)
{
// Insert error handling here.
return 1;
}
// Enumerate through all devices in Set.
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for (i=0;SetupDiEnumDeviceInfo(hDevInfo,i,
&DeviceInfoData);i++)
{
DWORD DataT;
LPTSTR buffer = NULL;
DWORD buffersize = 0;
//
// Call function with null to begin with,
// then use the returned buffer size
// to Alloc the buffer. Keep calling until
// success or an unknown failure.
//
while (!SetupDiGetDeviceRegistryProperty(
hDevInfo,
&DeviceInfoData,
SPDRP_DEVICEDESC,
&DataT,
(PBYTE)buffer,
buffersize,
&buffersize))
{
if (GetLastError() ==
ERROR_INSUFFICIENT_BUFFER)
{
// Change the buffer size.
if (buffer) LocalFree(buffer);
buffer = (LPTSTR)LocalAlloc(LPTR,buffersize);
}
else
{
// Insert error handling here.
break;
}
}
printf("Result:[%s]\n",buffer);
if (buffer) LocalFree(buffer);
}
if ( GetLastError()!=NO_ERROR &&
GetLastError()!=ERROR_NO_MORE_ITEMS )
{
// Insert error handling here.
return 1;
}
// Cleanup
SetupDiDestroyDeviceInfoList(hDevInfo);
return 0;
}
以下代码段演示如何检索系统上的所有显示适配器组: hDevInfo = SetupDiGetClassDevs(
(LPGUID) &GUID_DEVCLASS_DISPLAY,
0,
0,
DIGCF_PRESENT);
以下代码段演示如何检索外围组件互连 (PCI) 总线上的所有设备组: hDevInfo = SetupDiGetClassDevs(NULL,
REGSTR_KEY_PCIENUM, // Enumerator
0,
DIGCF_PRESENT | DIGCF_ALLCLASSES );
相关文章推荐
- HOWTO: 通过使用 SetupDi 调用函数枚举硬件设备
- 【转贴】DXUT编程指南(四):通过DXUT使用设备
- 【转贴】DXUT编程指南(四):通过DXUT使用设备
- DXUT编程指南(四):通过DXUT使用设备
- 【转贴】DXUT编程指南(四):通过DXUT使用设备
- 【转贴】DXUT编程指南(四):通过DXUT使用设备
- 【转贴】DXUT编程指南(四):通过DXUT使用设备
- VISTA或WIN7下使用立体声混音作为录音来源时,如何通过编程方式去除或加入麦克风的输入
- 使用EPROCESS下Win32Process枚举进程
- C#编程:枚举(enum)与结构(struct)的定义和使用方法
- Win32编程 使用自定义的ICON资源
- 枚举硬件设备
- Win32编程点滴3 - 简单ActiveX控件的使用
- 1月14日苹果在线商店促销,冲动之下购买了IMAC。以前一直在旧电脑windows下使用itunes,现在有了新的也想借此机会将旧电脑系统重装。于是我需要把现有设备中的所有资料通过itunes全部导
- 【越狱开发】通过数据线连接使用ssh登陆到iOS 设备
- Linux硬件资源管理与外设设备使用、系统运行机制及用户管理
- sysfs 文件系统 通过设备属性访问硬件(DEVICE_ATTR, LINUX)
- Win32编程API 基础篇 -- 5.使用资源
- iOS编程——通过UUID和KeyChain来代替Mac地址实现iOS设备的唯一标示(Swift 优化版)
- [Winhttp]_[C/C++]_[使用win32 SDK的Windows HTTP Services(WinHTTP)通过代理下载网页]