/*
* Process.h
* Copyright (c) 2011 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED.
*/
// SOL++2000
#pragma once
#include <sol\Object.h>
#include <sol\Pipe.h>
namespace SOL {
class Process :public Object {
private:
STARTUPINFO startupInfo;
PROCESS_INFORMATION processInfo;
SECURITY_ATTRIBUTES securityAttributes;
public:
Process(const char* command, DWORD flag = DETACHED_PROCESS)
{
memset(&securityAttributes, 0, sizeof(SECURITY_ATTRIBUTES));
securityAttributes.nLength = sizeof(SECURITY_ATTRIBUTES);
securityAttributes.bInheritHandle = TRUE;
memset(&startupInfo, 0, sizeof(STARTUPINFO));
startupInfo.cb = sizeof(STARTUPINFO);
BOOL rc = CreateProcess(NULL, // pointer to name of executable module
(char*)command, // pointer to command line string
&securityAttributes, // pointer to process security attributes
NULL, // pointer to thread security attributes
TRUE, // inheritance flag
flag, // creation flags
NULL, // pointer to new environment block
NULL, // pointer to current directory name
&startupInfo, // pointer to STARTUPINFO
&processInfo); // pointer to PROCESS_INFORMATION
if (rc == False) {
memset(&processInfo, 0, sizeof(processInfo));
}
}
public:
Process(const char* command, Pipe& pipe, DWORD flag = DETACHED_PROCESS)
{
memset(&securityAttributes, 0, sizeof(SECURITY_ATTRIBUTES));
securityAttributes.nLength = sizeof(SECURITY_ATTRIBUTES);
securityAttributes.bInheritHandle = TRUE;
memset(&startupInfo, 0, sizeof(STARTUPINFO));
startupInfo.cb = sizeof(STARTUPINFO);
startupInfo.dwFlags = STARTF_USESTDHANDLES;
startupInfo.hStdOutput = pipe.getWriterHandle();
startupInfo.hStdInput = pipe.getReaderHandle();
BOOL rc = CreateProcess(NULL, // pointer to name of executable module
(char*)command, // pointer to command line string
&securityAttributes, // pointer to process security attributes
NULL, // pointer to thread security attributes
TRUE, // inheritance flag
flag, // creation flags
NULL, // pointer to new environment block
NULL, // pointer to current directory name
&startupInfo, // pointer to STARTUPINFO
&processInfo); // pointer to PROCESS_INFORMATION
if (rc == False) {
memset(&processInfo, 0, sizeof(processInfo));
}
}
public:
Process(const char* command, Pipe* pipe, DWORD flag = DETACHED_PROCESS)
{
memset(&securityAttributes, 0, sizeof(SECURITY_ATTRIBUTES));
securityAttributes.nLength = sizeof(SECURITY_ATTRIBUTES);
securityAttributes.bInheritHandle = TRUE;
memset(&startupInfo, 0, sizeof(STARTUPINFO));
startupInfo.cb = sizeof(STARTUPINFO);
if (pipe) {
startupInfo.dwFlags = STARTF_USESTDHANDLES;
startupInfo.hStdOutput = pipe -> getWriterHandle();
startupInfo.hStdInput = pipe -> getReaderHandle();
}
BOOL rc = CreateProcess(NULL, // pointer to name of executable module
(char*)command, // pointer to command line string
&securityAttributes, // pointer to process security attributes
NULL, // pointer to thread security attributes
TRUE, // inheritance flag
flag, // creation flags
NULL, // pointer to new environment block
NULL, // pointer to current directory name
&startupInfo, // pointer to STARTUPINFO
&processInfo); // pointer to PROCESS_INFORMATION
if (rc == False) {
memset(&processInfo, 0, sizeof(processInfo));
}
}
public:
Boolean destroy(int code=0)
{
Boolean rc = False;
if (processInfo.hProcess) {
rc = ::TerminateProcess(processInfo.hProcess, code);
}
return rc;
}
public:
HANDLE getProcessHandle()
{
return processInfo.hProcess;
}
public:
Boolean suspend()
{
Boolean rc = False;
if (processInfo.hThread) {
rc = ::SuspendThread(processInfo.hThread);
}
return rc;
}
public:
Boolean resume()
{
Boolean rc = False;
if (processInfo.hThread) {
rc = ::ResumeThread(processInfo.hThread);
}
return rc;
}
public:
void wait(int interval=INFINITE)
{
if (processInfo.hProcess) {
WaitForSingleObject(processInfo.hProcess, interval);
}
}
};
}
|