Small Tip:Using FInteractiveProcess to communicate with your own process

Hello friends, I’d like to share this small tip and if anyone interest in communicating with another process (for me it is a 32-bit dll so I have to use a sub-process instead of linking it in directly) this may help.

So first you need to have a path to your .exe file.You can use FPath, for me it is :


FString Path = FPaths::Combine(*FPaths::GamePluginsDir(), TEXT("ShaderTest/ThirdParty"), TEXT("Win32LibraryWrapper.exe"));

Then you can use this to start a sub-process:



FInteractiveProcess* WrapperProcess;
WrapperProcess = new FInteractiveProcess(
			Path,
			TEXT(""),
			true,
			false
			);


Params are:

  1. The path to your exe file
  2. The startup parameters to send to your process
  3. Should the process run hidden
  4. Is this a long process?

And then you can use Launch function to make it run.


WrapperProcess->Launch();

So now let’s talk about how to communicate with it.From ue to sub-process,you can do like this:


 
WrapperProcess->SendWhenReady(TEXT("GetModule"));//Send message to Process


And if you want to get message back,you should use delegate OnOutput. My code is like this:



WrapperProcess->OnOutput().BindLambda(
		=](const FString& outputMessage)
		{	
			//Do anything you want
		}	
	);


But the big problem is in your sub-process.Because if you use cin ,you will find it don’t work.This blocks me for a long time.
After I look up msdn,I finally find how to get message from ue in sub-process:you must use ReadFile and Write File.For example:



#define BUFSIZE 4096 
HANDLE hStdin, hStdout;
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
hStdin = GetStdHandle(STD_INPUT_HANDLE);
CHAR* chBuf=new CHAR[BUFSIZE];
bSuccess = ReadFile(hStdin, chBuf, BUFSIZE, &dwRead, NULL);//Get Message from UE
bool bSuccess = WriteFile(hStdout, chBuf, BUFSIZE, NULL, NULL);//Send Message to UE


For output,you can use std::cout or printf. I don’t know why but that’s what I found on my computer;
I use automation tools to test ,that’s my result:


And there are some tips for you if you use multi-process:

  • You can use yield.hpp and coroutine.hpp from boost::asio. That will make your communicate work a lot more easier
4 Likes