您的位置:首页 > Web前端

如何判断是比笔记本还是台式机?

2009-11-05 15:14 645 查看
可以通过WMI读出机器的类型信息加以判断,不过类型太多,什么mini tower。这里介绍一种简单的办法。

 

笔记本必然有电池设备,即使你把电池卸掉了,也会有直流适配器的设备,我们只需要枚举出所有的hardware id,在其中找有没有"ACPI/ACPI0003"这个ID就行。

 

具体代码如下

#include <stdio.h>
#include <windows.h>
#include <setupapi.h>
#include <devguid.h>
#include <regstr.h>
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.

TCHAR buffer[4096]={0};
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for (i=0;SetupDiEnumDeviceInfo(hDevInfo,i,
&DeviceInfoData);i++)
{
DWORD DataT;
DWORD buffersize = 4096;

//
// 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.
//
SetupDiGetDeviceRegistryProperty(
hDevInfo,
&DeviceInfoData,
SPDRP_HARDWAREID,
&DataT,
(PBYTE)buffer,
buffersize,
&buffersize);

if( wcscmp(buffer, L"ACPI//PNP0000")==0 )
{

SetupDiDestroyDeviceInfoList(hDevInfo);
printf("It is a laptop/n");
return 1;
}
}

if ( GetLastError()!=NO_ERROR &&
GetLastError()!=ERROR_NO_MORE_ITEMS )
{
// Insert error handling here.
return -1;
}

//  Cleanup
SetupDiDestroyDeviceInfoList(hDevInfo);
return 0;
}


 

主要参考了 http://support.microsoft.com/kb/259695/的MS的知识库文章。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  buffer insert null function