UE4 C++获取硬件码

需要用到iphlpapi库,加载第三方库的方法见https://blog.csdn.net/u014532636/article/details/72770741

.h

	static void AdjustString(const char * str, int pos, char * buf);

	static bool GetMacAddress(int nNetIndex, char *sAddress);

	static ULONG GetHDSerial(char* pszIDBuff, int nBuffLen, int nDriveID);

	//获取网卡地址
	UFUNCTION(BlueprintCallable, Category = "Registe|GetMacAddress")
		static TArray<FString> GetMacAddress();

	//获取CPU序列号
	UFUNCTION(BlueprintCallable, Category = "Registe|base64Encrypt")
		static FString GetCPUAddresss();

	//获取硬盘序列号
	UFUNCTION(BlueprintCallable, Category = "Registe|base64Encrypt")
		static TArray<FString>  GetHDSerial();

.cpp

#include <Windows.h>
#include<intrin.h>
#include <winioctl.h>
#include "Iphlpapi.h"

void URegisteLibrary::AdjustString(const char * str, int pos, char * buf)
{
	int i = 0, nStart, nEnd;

	nStart = pos;
	while ((str[nStart] == ' '))
		nStart++;

	nEnd = nStart;
	while (str[nEnd])
	{
		buf[i++] = str[nEnd];
		nEnd++;
	}
	buf[i] = '\0';
}

TArray<FString> URegisteLibrary::GetMacAddress()
{
	TArray<FString> MacAddress;
	MacAddress.Empty();
	char sBuf[50] = "";
	int nNetNum = 0;
	while (GetMacAddress(nNetNum, sBuf))
	{
		//printf("MAC Address: %s\n", sBuf);
		MacAddress.Add(sBuf);
		nNetNum++;
		strcpy(sBuf, "");
	}
	return MacAddress;
}

FString URegisteLibrary::GetCPUAddresss()
{
	FString cpuAddress;
	INT32 deBuf[4];
	__cpuid(deBuf, 01);
	//printf("CPUID:%.8X%.8X\n", deBuf[0], deBuf[3]);
	//cpuAddress = FString::FromInt(deBuf[0]);
	cpuAddress = FString::Printf(TEXT("%.8X%.8X"), deBuf[0], deBuf[3]);
	//cpuAddress.Append(FString::FromInt(deBuf[3]));
	return cpuAddress;
}

TArray<FString> URegisteLibrary::GetHDSerial()
{
	TArray<FString> HDSerial;
	HDSerial.Empty();
	char szBuff[256];
	const int MAX_IDE_DRIVES = 16;
	for (int nDriveNum = 0; nDriveNum < MAX_IDE_DRIVES; nDriveNum++)
	{
		ULONG ulLen = GetHDSerial(szBuff, sizeof(szBuff), nDriveNum);
		if (ulLen > 0)
		{
			//_tprintf(TEXT("第%d块硬盘的序列号为:%hs\n"), nDriveNum + 1, szBuff);
			//printf("Disk Serial No.%d: %s\n", nDriveNum + 1, szBuff);
			HDSerial.Add(szBuff);
		}
	}
	return HDSerial;
}

bool URegisteLibrary::GetMacAddress(int nNetIndex, char *sAddress)
{
	int nIndex = 0;
	char sTmp[10];
	//PIP_ADAPTER_INFO结构体指针存储本机网卡信息
	PIP_ADAPTER_INFO pIpAdapterInfo = new IP_ADAPTER_INFO();
	//得到结构体大小,用于GetAdaptersInfo参数
	unsigned long stSize = sizeof(IP_ADAPTER_INFO);
	//调用GetAdaptersInfo函数,填充pIpAdapterInfo指针变量;其中stSize参数既是一个输入量也是一个输出量
	int nRel = GetAdaptersInfo(pIpAdapterInfo, &stSize);
	if (ERROR_BUFFER_OVERFLOW == nRel)
	{
		//如果函数返回的是ERROR_BUFFER_OVERFLOW
		//则说明GetAdaptersInfo参数传递的内存空间不够,同时其传出stSize,表示需要的空间大小
		//这也是说明为什么stSize既是一个输入量也是一个输出量
		//释放原来的内存空间
		delete pIpAdapterInfo;
		//重新申请内存空间用来存储所有网卡信息
		pIpAdapterInfo = (PIP_ADAPTER_INFO)new BYTE[stSize];
		//再次调用GetAdaptersInfo函数,填充pIpAdapterInfo指针变量
		nRel = GetAdaptersInfo(pIpAdapterInfo, &stSize);
	}
	if (ERROR_SUCCESS == nRel)
	{
		//输出网卡信息
		while (pIpAdapterInfo)
		{
			if (nIndex == nNetIndex)
			{
				for (UINT i = 0; i < pIpAdapterInfo->AddressLength; i++)
				{
					sprintf(sTmp, "%02X", pIpAdapterInfo->Address[i]);
					strcat(sAddress, sTmp);
				}
				return TRUE;
			}

			nIndex++;
			pIpAdapterInfo = pIpAdapterInfo->Next;
		}
	}
	//释放内存空间
	if (pIpAdapterInfo)
	{
		delete pIpAdapterInfo;
	}

	return FALSE;
}

ULONG URegisteLibrary::GetHDSerial(char* pszIDBuff, int nBuffLen, int nDriveID)
{
	//char pszIDBuff[256];
	HANDLE hPhysicalDrive = INVALID_HANDLE_VALUE;
	ULONG ulSerialLen = 0;

	//  Try to get a handle to PhysicalDrive IOCTL, report failure
	//  and exit if can't.
	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)
	{
		return FALSE;
	}
	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];

		AdjustString(local_buffer, descrip->SerialNumberOffset, serialNumber);

		if (isalnum(serialNumber[0]))
		{
			ULONG ulSerialLenTemp = (ULONG)strnlen(serialNumber, nBuffLen - 1);
			memcpy(pszIDBuff, serialNumber, ulSerialLenTemp);
			pszIDBuff[ulSerialLenTemp] = NULL;
			ulSerialLen = ulSerialLenTemp;
		}
	}

	if (hPhysicalDrive != INVALID_HANDLE_VALUE)
	{
		CloseHandle(hPhysicalDrive);
	}
	return ulSerialLen;
}

 

评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符 “速评一下”
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页