UE4 外部打开exe文件和虚拟键盘

一、外部文件的打开

   主要通过这四个函数来打开外部文件,这里的const FString& AppPath是你们要打开的exe文件的路径。

//打开外部exe文件
UFUNCTION(BlueprintCallable)
	void OpenExternalApp(const FString& AppPath);
//关闭外部exe文件
UFUNCTION(BlueprintCallable)
	void CloseExternalApp();
//打开外部exe文件
UFUNCTION(BlueprintCallable)
	void ExecuteExternalApp(const FString& AppPath);
//关闭外部exe文件
UFUNCTION(BlueprintCallable)
	void KillProcess(const FString& ProcessName);
//打开外部文件
void AMyGameMode::OpenExternalApp(const FString& AppPath)
{
pHandle = FPlatformProcess::CreateProc(*AppPath, nullptr, true, false, false, nullptr, 0, nullptr, nullptr);
UE_LOG(LogTemp, Warning, TEXT(FUNCTION"Create App"))
}
//关闭外部文件
void AMyGameMode::CloseExternalApp()
{
if (pHandle.IsValid())
{
FPlatformProcess::TerminateProc(pHandle);
pHandle.Reset();
UE_LOG(LogTemp, Warning, TEXT(FUNCTION"Close App"))
}
else
{
//输出日志
UE_LOG(LogTemp, Warning, TEXT(FUNCTION"Close None"))
}
}
//打开外部exe文件
void AMyGameMode::ExecuteExternalApp(const FString& AppPath)
{
std::string str_path = TCHAR_TO_UTF8(*AppPath);
std::wstring wstr_path;
wstr_path.assign(str_path.begin(), str_path.end());
ShellExecute(NULL, L"open", wstr_path.c_str(), NULL, NULL, SW_SHOWDEFAULT);
}
//关闭外部exe文件
void AMyGameMode::KillProcess(const FString& ProcessName)
{
std::string process = std::string("TASKKILL /F /IM ") + TCHAR_TO_UTF8(*ProcessName);
system(process.c_str());
UE_LOG(LogTemp, Warning, TEXT(FUNCTION"Kill App Process"));
}

二、打开虚拟键盘

UFUNCTION(BlueprintCallable)
   void ShowVirtualKeyboard();
void AMyGameMode::ShowVirtualKeyboard()
{
ShellExecute(NULL, L"open", L"osk.exe", NULL, NULL, SW_SHOWNORMAL);
}

三、注意在使用这些函数的时候需要加上一些头文件,才能够在C++中编译通过,否则无法编译成功

#include"Runtime/Core/Public/HAL/PlatformFilemanager.h"
#include"Runtime/Core/Public/Misc/FileHelper.h"
#include"Runtime/Core/Public/Misc/Paths.h"
#include "Windows/AllowWindowsPlatformTypes.h"
#include "Windows/PreWindowsApi.h"
#include <windows.h>
#include<string>
#include "Windows/PostWindowsApi.h"
#include "Windows/HideWindowsPlatformTypes.h"
#include <ShlDisp.h>
#include <shellapi.h>

四、也可以使用.bat文件操作,这也是打开DS服务器的一种方式

  找到你打包的文件位置,我这里是H:\OpenWorld\WindowsServer\OpenWorld\Binaries\Win64

   新建一个文本,输入命令,我这里加的 -log是测试ds服务器时用的,大家可以不加

点击运行bat文件即可

实际上UE4在打包之后的exe文件是放在Binaries,我们打开EXE文件时,实际是打开Binaries文件里面的exe文件。

五、注意事项

       我们在引用到第三方库时,尝尝会无法打包成功,原因是我们没有把编译好的第三方库的dll文件放到我们Binaries文件夹中导致我们打包失败,同样,打包成功后也是需要把第三方库的dll文件放到打包后的Binaries文件夹中,才可以打开exe文件。

猜你喜欢

转载自blog.csdn.net/qq_43021038/article/details/126498044