【VS2015】 C++实现硬件ID的查询
生活随笔
收集整理的這篇文章主要介紹了
【VS2015】 C++实现硬件ID的查询
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1.VS2015 新建項目->win32 控制臺應用程序
以下操作實現(xiàn)跨平臺 (1).配置屬性-常規(guī)-MFC的使用->使用標準Windows庫或者在靜態(tài)庫中使用MFC (2).配置屬性-c/c++-代碼生成-運行庫->多線程(/MT)2.頭文件stdafx.h
// stdafx.h : 標準系統(tǒng)包含文件的包含文件, // 或是經常使用但不常更改的 // 特定于項目的包含文件 //#pragma once //#define _AFXDLL #include "targetver.h"#include <stdio.h> #include <tchar.h>// TODO: 在此處引用程序需要的其他頭文件#include <afxwin.h> // MFC core and standard components #include <SetupAPI.h> #pragma comment(lib, "Setupapi.lib")
3.主程序ConsoleApplication5.cpp
// ConsoleApplication5.cpp : 定義控制臺應用程序的入口點。
#include "stdafx.h"
int main()
{HDEVINFO hDevInfo;SP_DEVINFO_DATA DeviceInfoData;DWORD i;// Create a HDEVINFO with all present devices.hDevInfo = SetupDiGetClassDevs(NULL,0, // Enumerator0,DIGCF_PRESENT | DIGCF_ALLCLASSES);if (hDevInfo == INVALID_HANDLE_VALUE){// Insert error handling here./*return 1;*/exit(0);}// 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 (doubled)// to Alloc the buffer. Keep calling until// success or an unknown failure.//// Double the returned buffersize to correct// for underlying legacy CM functions that // return an incorrect buffersize value on // DBCS/MBCS systems.// while (!SetupDiGetDeviceRegistryProperty(hDevInfo,&DeviceInfoData,//SPDRP_DEVICEDESC,SPDRP_HARDWAREID,&DataT,(PBYTE)buffer,buffersize,&buffersize)){if (GetLastError() ==ERROR_INSUFFICIENT_BUFFER){// Change the buffer size.if (buffer) LocalFree(buffer);// Double the size to avoid problems on // W2k MBCS systems per KB 888609. buffer = (LPTSTR)LocalAlloc(LPTR, buffersize * 2);}else{// Insert error handling here.break;}}//CString str;//str.Format(_T("%d"), num);//AfxMessageBox(buffer);printf("dev_ID=%S \n" ,buffer);}return 0;}
4.效果
總結
以上是生活随笔為你收集整理的【VS2015】 C++实现硬件ID的查询的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: TraceView issue
- 下一篇: ctfmon是什么启动项_Win7启动项