诊断用户机器上出现的问题的时候,机器的硬件以及OS信息是非常有帮助的。 检查机器上各个硬件信息的代码工作量非常大, 这里介绍一个快速便捷的方法: 利用windows自带工具dxdiag.exe 来生成硬件检测的报告,然后读取报告文件即可。 示例代码如下:
view plaincopy to clipboardprint?
STARTUPINFO si = {sizeof(si), 0};
PROCESS_INFORMATION pi = {0};
TCHAR szCmdLine[MAX_PATH] = _T("dxdiag.exe /whql:off /t dxdiag_output.txt");
if (CreateProcess(
NULL,
szCmdLine,
NULL,
NULL,
TRUE,
NULL,
NULL,
NULL,
&si,
&pi))
{
WaitForSingleObject (pi.hProcess, INFINITE); // 等待dxdiag.exe执行结束
CloseHandle (pi.hProcess);
CloseHandle (pi.hThread);
//读取dxdiag_output.txt 文件,获取用户机器的硬件/OS信息
HANDLE hFile = CreateFile(_T("dxdiag_output.txt"), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
DWORD dwBytesRead = 0;
char szBuffer[BUFFER_SIZE];
ReadFile(hFile, szBuffer, BUFFER_SIZE-1, &dwBytesRead, NULL);
...
}
CloseHandle(hFile);
}
}
STARTUPINFO si = {sizeof(si), 0};
PROCESS_INFORMATION pi = {0};
TCHAR szCmdLine[MAX_PATH] = _T("dxdiag.exe /whql:off /t dxdiag_output.txt");
if (CreateProcess(
NULL,
szCmdLine,
NULL,
NULL,
TRUE,
NULL,
NULL,
NULL,
&si,
&pi))
{
WaitForSingleObject (pi.hProcess, INFINITE); // 等待dxdiag.exe执行结束
CloseHandle (pi.hProcess);
CloseHandle (pi.hThread);
//读取dxdiag_output.txt 文件,获取用户机器的硬件/OS信息
HANDLE hFile = CreateFile(_T("dxdiag_output.txt"), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
DWORD dwBytesRead = 0;
char szBuffer[BUFFER_SIZE];
ReadFile(hFile, szBuffer, BUFFER_SIZE-1, &dwBytesRead, NULL);
...
}
CloseHandle(hFile);
}
}
本文转自 陈本峰 51CTO博客,原文链接:http://blog.51cto.com/wingeek/274057,如需转载请自行联系原作者