渐渐地开始认识到C++不只是C++,站在它后面的是强大的各种API和类库的支持,还有STL。这就是好的project应该具备的地方——大家愿意去使用它并乐意为其贡献代码。
闲话不多说,easyCraft进行到这里遇到的问题是获取硬件信息、修改Windows配置。首先需要的是SetupDiGetClassDevs函数。原形如下:
HDEVINFO SetupDiGetClassDevs(
IN PGUID ClassGuid, /* optional */
IN PCTSTR Enumerator, /* optional */
IN HWND hwndParent, /* optional */
IN DWORD Flags
);
作用是枚举设备信息。通过参数 ClassGuid的修改就可以获取各种类型的设备,实例函数如下:
void GetGraphics(std::string devices[10])
{
DWORD dwIdx = 0;
SP_DEVINFO_DATA adapterData;
DWORD dwType = 0;
DWORD dwBuflen = 0;
char* pBuf = NULL;
HDEVINFO hScsi = INVALID_HANDLE_VALUE;
//GUID设为显卡GUID
GUID guidScsiRaid = {0x4d36e968,0xe325,0x11CE, {0xBF, 0xC1, 0x08, 0x00, 0x2B, 0xE1, 0x03, 0x18} };
BOOL bret = FALSE;
hScsi = SetupDiGetClassDevs(&guidScsiRaid, NULL, NULL, DIGCF_PRESENT);
adapterData.cbSize = sizeof(SP_DEVINFO_DATA);
//开始枚举显卡
for(dwIdx = 0; SetupDiEnumDeviceInfo(hScsi, dwIdx, &adapterData); dwIdx++)
{
SetupDiGetDeviceRegistryProperty(hScsi, &adapterData, SPDRP_DEVICEDESC,&dwType,NULL,0,&dwBuflen);
if(ERROR_INSUFFICIENT_BUFFER == GetLastError())
{
pBuf = (char*)malloc(dwBuflen);
if(SetupDiGetDeviceRegistryProperty(hScsi, &adapterData, SPDRP_DEVICEDESC,&dwType,(unsigned char *)pBuf, dwBuflen, NULL))
{
devices[dwIdx]=pBuf;
}
free(pBuf);
pBuf = NULL;
}
}
SetupDiDestroyDeviceInfoList(hScsi);
}
如上方函数所示,把每个获取的 pBuf 赋给字符串变量 devices ,但是不能将 pBuf 赋给 char* 型变量,否则会出现内存错误和乱码。
关于这个ClassGuid是可以在MSDN上找到的,如这个页面:http://msdn.microsoft.com/en-us/library/windows/hardware/ff553426%28v=vs.85%29.aspx
几个常用设备的ClassGuid:
-
CD-ROM Drives(光盘驱动器)
-
-
Class = CDROM
ClassGuid = {4d36e965-e325-11ce-bfc1-08002be10318}
This class includes CD-ROM drives, including SCSI CD-ROM drives. By default, the system's CD-ROM class installer also installs a system-supplied CD audio driver and CD-ROM changer driver as Plug and Play filters.
-
Display Adapters(显示卡)
-
-
Class = Display
ClassGuid = {4d36e968-e325-11ce-bfc1-08002be10318}
This class includes video adapters. Drivers for this class include display drivers and video miniport drivers.
-
Hard Disk Controllers(硬盘驱动器)
-
-
Class = HDC
ClassGuid = {4d36e96a-e325-11ce-bfc1-08002be10318}
This class includes hard disk controllers, including ATA/ATAPI controllers but not SCSI and RAID disk controllers.
-
Network Adapter(网络适配器)
-
-
Class = Net
ClassGuid = {4d36e972-e325-11ce-bfc1-08002be10318}
This class includes NDIS miniport drivers excluding Fast-IR miniport drivers, NDIS intermediate drivers (of virtual adapters), and CoNDIS MCM miniport drivers.
-
Processors(处理器)
-
-
Class = Processor
ClassGuid = {50127dc3-0f36-415e-a6cc-4cb3be910b65}
This class includes processor types.
-
Storage Volumes(磁盘分卷)
-
-
Class = Volume
ClassGuid = {71a27cdd-812a-11d0-bec7-08002be2092f}
This class includes storage volumes as defined by the system-supplied logical volume manager and class drivers that create device objects to represent storage volumes, such as the system disk class driver.