使用Windows库的文件需要引入Windows的头文件
1.获取本机IP
.h
UFUNCTION(BlueprintCallable)
static FString ShowIP();
.cpp内容
#include "MyBlueprintFunctionLibrary.h"
#include "Windows/AllowWindowsPlatformTypes.h"
#include "Windows/PreWindowsApi.h"
#include <winsock2.h>
#include "Engine/GameEngine.h"
#include "Windows/PostWindowsApi.h"
#include "Windows/HideWindowsPlatformTypes.h"
FString UMyBlueprintFunctionLibrary::ShowIP()
{
WSADATA Wsadata;
char name[255];
if (!WSAStartup(MAKEWORD(2,0),&Wsadata))
{
if (!gethostname(name,sizeof(name)))
{
hostent*host=gethostbyname(name);
if (host!=nullptr)
{
FString a=inet_ntoa(*(struct in_addr*)*host->h_addr_list);
return (TEXT("%s"),a);
}
}
}
return TEXT("0.0.0.0");
}
蓝图展示
2.读取本地文件
.h
UFUNCTION(BlueprintCallable)
static FString GetSavedIP(FString Filename);
.cpp
FString UMyBlueprintFunctionLibrary::GetSavedIP(FString Filename)
{
FString ResultString;
FFileHelper::LoadFileToString(ResultString,*(FPaths::ProjectDir()+Filename));
return ResultString;
}
蓝图展示