AUI Framework  master
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
AProcess Class Referenceabstract

Retrieves information about processes. More...

#include <AUI/Platform/AProcess.h>

Classes#

struct  ArgSingleString
 Process arguments represented as a single string. More...
 
struct  ArgStringList
 Process arguments represented as array of strings. More...
 
struct  ProcessCreationInfo
 Process creation info. More...
 

Public Member Functions#

virtual APath getModuleName ()=0
 
virtual APath getPathToExecutable ()=0
 
virtual uint32_t getPid () const noexcept=0
 
virtual int waitForExitCode ()=0
 Wait for process to be finished and returns exit code.
 
virtual size_t processMemory () const =0
 Obtain process memory usage.
 
void kill () const noexcept
 
- Public Member Functions inherited from aui::noncopyable
 noncopyable (const noncopyable &)=delete
 
noncopyableoperator= (const noncopyable &)=delete
 

Static Public Member Functions#

static _< AChildProcesscreate (ProcessCreationInfo args)
 Launches an executable.
 
static _< AChildProcessmake (AString applicationFile, AString args={}, APath workingDirectory={})
 Launches an executable.
 
static int executeWaitForExit (AString applicationFile, AString args={}, APath workingDirectory={}, ASubProcessExecutionFlags flags=ASubProcessExecutionFlags::DEFAULT)
 Launches executable.
 
static void executeAsAdministrator (const AString &applicationFile, const AString &args={}, const APath &workingDirectory={})
 Launches executable with administrator rights. (Windows only)
 
static AVector< _< AProcess > > all ()
 
static _< AProcessself ()
 
static _< AProcessfindAnotherSelfInstance (const AString &yourProjectName)
 tempFileName file name which will be used as lock
 
static _< AProcessfromPid (uint32_t pid)
 

Detailed Description#

Process model that facilitates process creation, management, and interaction with other processes.

Note
In a sandboxed environment (especially in iOS and Android) this functionality is mostly irrelevant (except AProcess::self()).

The AProcess class is typically used for creating, controlling, and monitoring subprocesses (including self) in a platform-independent manner. It provides a way to run external applications from within the application itself, which can be useful for tasks like running scripts, launching other programs, or automating system operations through commands.

Launching executable#

To start a process, pass the name of application you want to run and optionally provide arguments and working dir for that application. In this code snippet, we are starting another instance of the current executable with specific arguments and capturing its standard output (stdOut).

auto self = AProcess::self()->getPathToExecutable();
args.list << "--help";
args.list << "-a";
auto p = AProcess::create({
  .executable = self,
  .args = std::move(args),
  .workDir = self.parent(),
});
AString accumulator;
AObject::connect(p->stdOut, p, [&](const AByteBuffer& buffer) { accumulator += AString::fromUtf8(buffer); });
p->run();
EXPECT_EQ(p->waitForExitCode(), 0);
EXPECT_TRUE(accumulator.contains("This program contains tests written using Google Test.")) << accumulator;
std::vector-like growing array for byte storage.
Definition AByteBuffer.h:31
static _< AProcess > self()
static _< AChildProcess > create(ProcessCreationInfo args)
Launches an executable.
Represents a Unicode character string.
Definition AString.h:38
static decltype(auto) connect(const Signal &signal, Object *object, Function &&function)
Connects signal to the slot of the specified object.
Definition AObject.h:86
Process arguments represented as array of strings.
Definition AProcess.h:131

We define an empty string accumulator to collect the output from the process. Then, we connect a lambda function to the stdOut signal of the process. This lambda function converts the received buffer (a byte array) to a UTF-8 string and appends it to accumulator.

We start the new process by calling its AProcess::run() method, which will execute the specified application with the provided arguments in the given working directory.

We wait for the process to finish by calling waitForExitCode(), which blocks until the process exits and returns its exit code. If the exit code is 0, it means the process completed successfully.

Member Function Documentation#

◆ all()#

static AVector< _< AProcess > > AProcess::all ( )
static
Returns
data about all other running processes.

◆ create()#

static _< AChildProcess > AProcess::create ( ProcessCreationInfo args)
static
Parameters
argsdesignated-initializer-style args. See ProcessCreationInfo
Returns
AChildProcess instance. Use AChildProcess::run to execute.

◆ executeAsAdministrator()#

static void AProcess::executeAsAdministrator ( const AString & applicationFile,
const AString & args = {},
const APath & workingDirectory = {} )
static
Parameters
applicationFileexecutable file
argsarguments
workingDirectorypro
Note
This function could not determine exit code because of MS Windows restrictions
Windows-specific
This API is available on Windows (operating system) only.

◆ executeWaitForExit()#

static int AProcess::executeWaitForExit ( AString applicationFile,
AString args = {},
APath workingDirectory = {},
ASubProcessExecutionFlags flags = ASubProcessExecutionFlags::DEFAULT )
static
Parameters
applicationFileexecutable file.
argsarguments.
workingDirectoryworking directory.
flagsprocess execution flags. see ASubProcessExecutionFlags.
Returns
exit code

◆ findAnotherSelfInstance()#

static _< AProcess > AProcess::findAnotherSelfInstance ( const AString & yourProjectName)
static
Returns
another instance of this application; nullptr, if not found

◆ fromPid()#

static _< AProcess > AProcess::fromPid ( uint32_t pid)
static

This function might cause race condition if process is about to die. If process is not found, nullptr is returned so you must check for nullptr before proceeding. However, if non-nullptr is returned, the process handle is "acquired" and guaranteed to be valid during lifetime of AProcess instance.

Returns
process by id

◆ getModuleName()#

virtual APath AProcess::getModuleName ( )
pure virtual
Returns
process' executable file name.

Implemented in AChildProcess.

◆ getPathToExecutable()#

virtual APath AProcess::getPathToExecutable ( )
pure virtual
Returns
path to the process' executable.

Implemented in AChildProcess.

◆ getPid()#

virtual uint32_t AProcess::getPid ( ) const
pure virtualnoexcept
Returns
process' ID.

Implemented in AChildProcess.

◆ make()#

static _< AChildProcess > AProcess::make ( AString applicationFile,
AString args = {},
APath workingDirectory = {} )
inlinestatic
Parameters
applicationFileexecutable file
argsarguments
workingDirectoryworking directory
Returns
AChildProcess instance. Use AChildProcess::run to execute.

◆ processMemory()#

virtual size_t AProcess::processMemory ( ) const
pure virtual

Implemented in AChildProcess.

◆ self()#

static _< AProcess > AProcess::self ( )
static
Returns
data about this process.

◆ waitForExitCode()#

virtual int AProcess::waitForExitCode ( )
pure virtual
Returns
exit code

Implemented in AChildProcess.