前言 本文记录UE4获取硬件信息和地址等常用方法
Mac地址 GetMacAddressString()
方法已经在4.15版本后被彻底废弃, 所以UE目前(4.26)版本无法获取Mac地址
我们之前UE4获取第三方库 文章中有用到使用自己用原生cpp获取到mac地址, 然后导出成动态或者静态库到UE4再封装成插件的形式来获取mac地址, 此方法也是目前比较快捷的一个方法
下面贴DLL/Lib的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 #pragma once #define DLL_EXPORT __declspec(dllexport) #ifdef __cplusplus extern "C" { #endif int DLL_EXPORT GetMac (char * mac) ; #ifdef __cplusplus } #endif
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 #pragma once #include "string.h" #include "ThirdPartyDLL.h" #include <windows.h> #include <wincon.h> #include <stdlib.h> #include <stdio.h> #include <time.h> #include <Nb30.h> #pragma comment(lib,"netapi32.lib" ) int GetMac (char * mac) { NCB ncb; typedef struct _ASTAT_ { ADAPTER_STATUS adapt; NAME_BUFFER NameBuff[30 ]; }ASTAT, *PASTAT; ASTAT Adapter; typedef struct _LANA_ENUM { UCHAR length; UCHAR lana[MAX_LANA]; }LANA_ENUM; LANA_ENUM lana_enum; UCHAR uRetCode; memset (&ncb, 0 , sizeof (ncb)); memset (&lana_enum, 0 , sizeof (lana_enum)); ncb.ncb_command = NCBENUM; ncb.ncb_buffer = (unsigned char *)&lana_enum; ncb.ncb_length = sizeof (LANA_ENUM); uRetCode = Netbios (&ncb); if (uRetCode != NRC_GOODRET) return uRetCode; for (int lana = 0 ; lana < lana_enum.length; lana++) { ncb.ncb_command = NCBRESET; ncb.ncb_lana_num = lana_enum.lana[lana]; uRetCode = Netbios (&ncb); if (uRetCode == NRC_GOODRET) break ; } if (uRetCode != NRC_GOODRET) return uRetCode; memset (&ncb, 0 , sizeof (ncb)); ncb.ncb_command = NCBASTAT; ncb.ncb_lana_num = lana_enum.lana[0 ]; strcpy ((char *)ncb.ncb_callname, "*" ); ncb.ncb_buffer = (unsigned char *)&Adapter; ncb.ncb_length = sizeof (Adapter); uRetCode = Netbios (&ncb); if (uRetCode != NRC_GOODRET) return uRetCode; sprintf (mac, "%02X-%02X-%02X-%02X-%02X-%02X" , Adapter.adapt.adapter_address[0 ], Adapter.adapt.adapter_address[1 ], Adapter.adapt.adapter_address[2 ], Adapter.adapt.adapter_address[3 ], Adapter.adapt.adapter_address[4 ], Adapter.adapt.adapter_address[5 ]); return 0 ; }
CPU信息 最常用的方法是获取CPU的品牌信息, 即用如下方法即可获取
1 Brand = FPlatformMisc::GetCPUBrand ();
对比cmd中的信息
另外UE中还封装了其他一些CPU信息, 比如供应商等, 分别可以用如下方法去获取
1 2 3 Chipset = FPlatformMisc::GetCPUChipset (); Info = FPlatformMisc::GetCPUInfo (); Vendor = FPlatformMisc::GetCPUVendor ();
GPU信息 GPU信息相比就多一些, 通过两个方法来获取
1 2 3 4 5 6 7 8 9 Brand = FPlatformMisc::GetPrimaryGPUBrand (); FGPUDriverInfo info = FPlatformMisc::GetGPUDriverInfo (Brand); ProviderName = info.ProviderName; VendorID = info.VendorId; InternalDriverVersion = info.InternalDriverVersion; UserDriverVersion = info.UserDriverVersion; DriverDate = info.DriverDate; RHIName = info.RHIName;
统统打印出来是如下
IP地址 需要包含库Sockets
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include "SocketSubsystem.h" FString UFlib_DeviceInfo::GetIpAddress (bool bHasPort ) { FString IpAddr ("NONE" ) ; bool canBind = false ; TSharedRef<FInternetAddr>LocalIp = ISocketSubsystem::Get (PLATFORM_SOCKETSUBSYSTEM)->GetLocalHostAddr (*GLog, canBind); if (LocalIp->IsValid ()) { IpAddr = LocalIp->ToString (bHasPort); } return IpAddr; }