获取计算机硬件信息

获取计算机硬件信息

cpu

HKEY hKey;
	long lResult;
	lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0"), 0, KEY_READ, &hKey);

	DWORD dwSize = 200;
	TCHAR CpuString[200];
	DWORD dwType = REG_SZ;
	if (lResult == ERROR_SUCCESS)
	{
		RegQueryValueEx(hKey, _T("ProcessorNameString"), NULL, &dwType, (LPBYTE)CpuString, &dwSize);
	}

	char CPUInfo[200];
	WideCharToMultiByte(CP_UTF8, 0, CpuString, dwSize, CPUInfo, 100, NULL, NULL);
	RegCloseKey(hKey);

CPU的信息是最简单获取到,就在系统的注册表之中。这里我有24个核心,所有CentralProcessor下面有24个项目,每个里面都有这个字段,一般最少有一个核心,所以取0的数据就可以了,如果电脑的cpu有多个且不同型号,是可能读出多个不同cpu型号的,看个人需要了。
在这里插入图片描述

显卡

DISPLAY_DEVICE disp;
	FMemory::Memset(&disp, 0, sizeof(DISPLAY_DEVICE));
	disp.cb = sizeof(DISPLAY_DEVICE);

	int nVgaCnt = 0;
	while (EnumDisplayDevices(NULL, nVgaCnt, &disp, 0))
	{
		if (disp.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE)
		{
			char DeviceInfo[100];
			WideCharToMultiByte(CP_UTF8, 0, disp.DeviceString, 100, DeviceInfo, 100, NULL, NULL);
			FMemory::Memcpy(g_PLOSI.xM3, DeviceInfo, 100);
		}

		nVgaCnt++;
	}

硬盘

char* flipAndCodeBytes(const char* str,
    int pos,
    int flip,
    char* buf)
{
    int i;
    int j = 0;
    int k = 0;

    buf[0] = '\0';
    if (pos <= 0)
        return buf;

    if (!j)
    {
        char p = 0;

        // First try to gather all characters representing hex digits only.
        j = 1;
        k = 0;
        buf[k] = 0;
        for (i = pos; j && str[i] != '\0'; ++i)
        {
            char c = tolower(str[i]);

            if (isspace(c))
                c = '0';

            ++p;
            buf[k] <<= 4;

            if (c >= '0' && c <= '9')
                buf[k] |= (unsigned char)(c - '0');
            else if (c >= 'a' && c <= 'f')
                buf[k] |= (unsigned char)(c - 'a' + 10);
            else
            {
                j = 0;
                break;
            }

            if (p == 2)
            {
                if (buf[k] != '\0' && !isprint(buf[k]))
                {
                    j = 0;
                    break;
                }
                ++k;
                p = 0;
                buf[k] = 0;
            }

        }
    }

    if (!j)
    {
        // There are non-digit characters, gather them as is.
        j = 1;
        k = 0;
        for (i = pos; j && str[i] != '\0'; ++i)
        {
            char c = str[i];

            if (!isprint(c))
            {
                j = 0;
                break;
            }

            buf[k++] = c;
        }
    }

    if (!j)
    {
        // The characters are not there or are not printable.
        k = 0;
    }

    buf[k] = '\0';

    if (flip)
        // Flip adjacent characters
        for (j = 0; j < k; j += 2)
        {
            char t = buf[j];
            buf[j] = buf[j + 1];
            buf[j + 1] = t;
        }

    // Trim any beginning and end space
    i = j = -1;
    for (k = 0; buf[k] != '\0'; ++k)
    {
        if (!isspace(buf[k]))
        {
            if (i < 0)
                i = k;
            j = k;
        }
    }

    if ((i >= 0) && (j >= 0))
    {
        for (k = i; (k <= j) && (buf[k] != '\0'); ++k)
            buf[k - i] = buf[k];
        buf[k - i] = '\0';
    }

    return buf;
}


for (int nDriveID = 0; nDriveID < 16; nDriveID++)
    {
        HANDLE hPhysicalDrive = INVALID_HANDLE_VALUE;

        TCHAR szDriveName[32];
        wsprintf(szDriveName, TEXT("\\\\.\\PhysicalDrive%d"), nDriveID);

        //  Windows NT, Windows 2000, Windows XP - admin rights not required
        hPhysicalDrive = CreateFile(szDriveName, 0,
            FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
            OPEN_EXISTING, 0, NULL);
        if (hPhysicalDrive == INVALID_HANDLE_VALUE)
        {
            continue;
        }

        STORAGE_PROPERTY_QUERY query;
        DWORD cbBytesReturned = 0;
        static char local_buffer[10000];

        memset((void*)&query, 0, sizeof(query));
        query.PropertyId = StorageDeviceProperty;
        query.QueryType = PropertyStandardQuery;

        memset(local_buffer, 0, sizeof(local_buffer));

        if (DeviceIoControl(hPhysicalDrive, IOCTL_STORAGE_QUERY_PROPERTY,
            &query,
            sizeof(query),
            &local_buffer[0],
            sizeof(local_buffer),
            &cbBytesReturned, NULL))
        {
            STORAGE_DEVICE_DESCRIPTOR* descrip = (STORAGE_DEVICE_DESCRIPTOR*)&local_buffer;
            char serialNumber[1000];
            char* aaa = (char*)descrip + descrip->SerialNumberOffset;
            flipAndCodeBytes(local_buffer,
                descrip->SerialNumberOffset,
                0, serialNumber);

            if (isalnum(serialNumber[0]))
            {
                ULONG ulSerialLenTemp = strnlen(serialNumber, 1023);
                char pszIDBuff[1024];
                memcpy(pszIDBuff, serialNumber, ulSerialLenTemp);
                pszIDBuff[ulSerialLenTemp] = NULL;
                cout <<"serialNumber"<<nDriveID<<":"<< pszIDBuff << endl;

                //return 0;
            }
        }
    }

可以看到这里硬盘序列号是一样的。
在这里插入图片描述

网卡

IP_ADAPTER_INFO AdapterInfo[16];			// Allocate information for up to 16 NICs
	DWORD size = sizeof(AdapterInfo);
	DWORD dwStatus = GetAdaptersInfo(AdapterInfo, &size);
	if (dwStatus != ERROR_SUCCESS)
	{
		return;
	}

操作系统

typedef INT_PTR (WINAPI* PGNSI)(LPSYSTEM_INFO);
typedef INT_PTR (WINAPI* PGPI)(DWORD, DWORD, DWORD, DWORD, PDWORD);


OSVERSIONINFOEX osvi;
	SYSTEM_INFO si;
	PGNSI pGNSI;
	PGPI pGPI;

	FMemory::Memset(&si, 0, sizeof(SYSTEM_INFO));
	FMemory::Memset(&osvi, 0, sizeof(OSVERSIONINFOEX));
	osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);

#pragma warning(disable:4996)
	if (GetVersionEx((OSVERSIONINFO*)&osvi) == 0)
	{
		return false;
	}

	pGNSI = (PGNSI)GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetNativeSystemInfo");
	if (0 != pGNSI)
	{
		pGNSI(&si);
	}
	else
	{
		GetSystemInfo(&si);
	}

	if (VER_PLATFORM_WIN32_NT == osvi.dwPlatformId && osvi.dwMajorVersion > 4)
	{
		StringCchCopy(pszOS, MAX_LEN1, TEXT("Microsoft "));

		if (osvi.dwMajorVersion == 6)
		{
			if (osvi.dwMinorVersion == 0)
			{
				if (osvi.wProductType == VER_NT_WORKSTATION)
					StringCchCat(pszOS, MAX_LEN1, TEXT("Windows Vista "));
				else StringCchCat(pszOS, MAX_LEN1, TEXT("Windows Server 2008 "));
			}

			if (osvi.dwMinorVersion == 1)
			{
				if (osvi.wProductType == VER_NT_WORKSTATION)
					StringCchCat(pszOS, MAX_LEN1, TEXT("Windows 7 "));
				else StringCchCat(pszOS, MAX_LEN1, TEXT("Windows Server 2008 R2 "));
			}

			pGPI = (PGPI)GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetProductInfo");

			DWORD dwType;
			pGPI(osvi.dwMajorVersion, osvi.dwMinorVersion, 0, 0, &dwType);
			switch (dwType)
			{
			case PRODUCT_ULTIMATE:
				StringCchCat(pszOS, MAX_LEN1, TEXT("Ultimate Edition"));
				break;
			case PRODUCT_PROFESSIONAL:
				StringCchCat(pszOS, MAX_LEN1, TEXT("Professional"));
				break;
			case PRODUCT_HOME_PREMIUM:
				StringCchCat(pszOS, MAX_LEN1, TEXT("Home Premium Edition"));
				break;
			case PRODUCT_HOME_BASIC:
				StringCchCat(pszOS, MAX_LEN1, TEXT("Home Basic Edition"));
				break;
			default:
				StringCchCat(pszOS, MAX_LEN1, TEXT("etc Edition"));
				break;
			}
		}

		if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2)
		{

			if (osvi.wSuiteMask & VER_SUITE_STORAGE_SERVER)
				StringCchCat(pszOS, MAX_LEN1, TEXT("Windows Storage Server 2003"));
			else if (osvi.wProductType == VER_NT_WORKSTATION &&
				si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
			{
				StringCchCat(pszOS, MAX_LEN1, TEXT("Windows XP Professional x64 Edition"));
			}
			else StringCchCat(pszOS, MAX_LEN1, TEXT("Windows Server 2003, "));

			// Test for the server type.
			if (osvi.wProductType != VER_NT_WORKSTATION)
			{
				if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64)
				{
					if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
						StringCchCat(pszOS, MAX_LEN1, TEXT("Datacenter Edition for Itanium-based Systems"));
					else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
						StringCchCat(pszOS, MAX_LEN1, TEXT("Enterprise Edition for Itanium-based Systems"));
				}

				else if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
				{
					if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
						StringCchCat(pszOS, MAX_LEN1, TEXT("Datacenter x64 Edition"));
					else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
						StringCchCat(pszOS, MAX_LEN1, TEXT("Enterprise x64 Edition"));
					else StringCchCat(pszOS, MAX_LEN1, TEXT("Standard x64 Edition"));
				}

				else
				{
					if (osvi.wSuiteMask & VER_SUITE_COMPUTE_SERVER)
						StringCchCat(pszOS, MAX_LEN1, TEXT("Compute Cluster Edition"));
					else if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
						StringCchCat(pszOS, MAX_LEN1, TEXT("Datacenter Edition"));
					else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
						StringCchCat(pszOS, MAX_LEN1, TEXT("Enterprise Edition"));
					else if (osvi.wSuiteMask & VER_SUITE_BLADE)
						StringCchCat(pszOS, MAX_LEN1, TEXT("Web Edition"));
					else StringCchCat(pszOS, MAX_LEN1, TEXT("Standard Edition"));
				}
			}
		}

		if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1)
		{
			StringCchCat(pszOS, MAX_LEN1, TEXT("Windows XP "));
			if (osvi.wSuiteMask & VER_SUITE_PERSONAL)
				StringCchCat(pszOS, MAX_LEN1, TEXT("Home Edition"));
			else StringCchCat(pszOS, MAX_LEN1, TEXT("Professional"));
		}

		if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0)
		{
			StringCchCat(pszOS, MAX_LEN1, TEXT("Windows 2000 "));

			if (osvi.wProductType == VER_NT_WORKSTATION)
			{
				StringCchCat(pszOS, MAX_LEN1, TEXT("Professional"));
			}
			else
			{
				if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
					StringCchCat(pszOS, MAX_LEN1, TEXT("Datacenter Server"));
				else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
					StringCchCat(pszOS, MAX_LEN1, TEXT("Advanced Server"));
				else StringCchCat(pszOS, MAX_LEN1, TEXT("Server"));
			}
		}

		if (_tcslen(osvi.szCSDVersion) > 0)
		{
			StringCchCat(pszOS, MAX_LEN1, TEXT(" "));
			StringCchCat(pszOS, MAX_LEN1, osvi.szCSDVersion);
		}

		TCHAR buf[80];

		StringCchPrintf(buf, 80, TEXT(" (build %d)"), osvi.dwBuildNumber);
		StringCchCat(pszOS, MAX_LEN1, buf);

		if (osvi.dwMajorVersion >= 6)
		{
			if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
				StringCchCat(pszOS, MAX_LEN1, TEXT(", 64-bit"));
			else if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL)
				StringCchCat(pszOS, MAX_LEN1, TEXT(", 32-bit"));
		}

		return true;
	}
	else
	{
		//printf("This sample does not support this version of Windows.\n");
		return false;
	}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Python中,可以使用多种方法获取电脑硬件信息。下面是一些常用的方法: 1. 使用`platform`模块获取系统相关信息: ```python import platform # 获取操作系统名称和版本号 os_name = platform.system() os_version = platform.release() # 获取计算机名称 computer_name = platform.node() # 获取处理器类型和名称 processor_type = platform.processor() # 打印以上信息 print("操作系统:", os_name) print("版本号:", os_version) print("计算机名称:", computer_name) print("处理器:", processor_type) ``` 2. 使用`psutil`模块获取更多详细信息: ```python import psutil # 获取CPU信息 cpu_info = psutil.cpu_info() print("CPU信息:", cpu_info) # 获取内存信息 memory_info = psutil.virtual_memory() print("内存信息:", memory_info) # 获取磁盘信息 disk_info = psutil.disk_partitions() print("磁盘信息:", disk_info) ``` 3. 使用`wmi`模块获取更多硬件信息(需要安装`pywin32`库): ```python import wmi # 创建wmi对象 c = wmi.WMI() # 获取CPU信息 cpu_info = c.Win32_Processor()[0] print("CPU信息:", cpu_info) # 获取主板信息 board_info = c.Win32_BaseBoard()[0] print("主板信息:", board_info) # 获取物理内存信息 memory_infos = c.Win32_PhysicalMemory() print("内存信息:") for memory_info in memory_infos: print(memory_info) # 获取磁盘信息 disk_infos = c.Win32_DiskDrive() print("磁盘信息:") for disk_info in disk_infos: print(disk_info) ``` 以上是获取电脑硬件信息的基本方法,根据需求可以进一步筛选和加工这些信息

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值