-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
#include "Process.h" | ||
#include <tlhelp32.h> | ||
|
||
using namespace xop; | ||
|
||
Process::Process() | ||
: pid_(0) | ||
{ | ||
|
||
} | ||
|
||
Process::~Process() | ||
{ | ||
|
||
} | ||
|
||
bool Process::Start(std::string app_path, std::string cmd, std::string log_path) | ||
{ | ||
if (pid_ > 0) { | ||
return false; | ||
} | ||
|
||
#if defined(WIN32) || defined(_WIN32) | ||
PROCESS_INFORMATION pi; | ||
STARTUPINFO si = { sizeof(STARTUPINFO) }; | ||
SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL,TRUE }; | ||
si.wShowWindow = SW_HIDE; | ||
|
||
if (!log_path.empty()) { | ||
file_ = CreateFile(log_path.c_str(), GENERIC_WRITE, | ||
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, | ||
&sa, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); | ||
if (file_ != INVALID_HANDLE_VALUE) { | ||
si.dwFlags = STARTF_USESTDHANDLES; | ||
si.hStdOutput = file_; | ||
si.hStdError = file_; | ||
} | ||
} | ||
|
||
if (CreateProcess(app_path.c_str(), (LPTSTR)cmd.c_str(), NULL, NULL, true, 0, NULL, NULL, &si, &pi)) { | ||
CloseHandle(pi.hProcess); | ||
CloseHandle(pi.hThread); | ||
pid_ = static_cast<int64_t>(pi.dwProcessId); | ||
} | ||
else { | ||
return false; | ||
} | ||
|
||
#elif defined(__linux) || defined(__linux__) | ||
|
||
|
||
|
||
#endif | ||
return false; | ||
} | ||
|
||
void Process::Stop() | ||
{ | ||
if (pid_ <= 0) { | ||
return ; | ||
} | ||
#if defined(WIN32) || defined(_WIN32) | ||
HANDLE hProcess = NULL; | ||
hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, (DWORD)pid_); | ||
if (hProcess != NULL) { | ||
TerminateProcess(hProcess, 0); | ||
CloseHandle(hProcess); | ||
} | ||
|
||
if (file_ != INVALID_HANDLE_VALUE) { | ||
CloseHandle(file_); | ||
file_ = INVALID_HANDLE_VALUE; | ||
} | ||
#elif defined(__linux) || defined(__linux__) | ||
|
||
#endif | ||
} | ||
|
||
bool Process::IsAlive() | ||
{ | ||
if (pid_ <= 0) { | ||
return false; | ||
} | ||
#if defined(WIN32) || defined(_WIN32) | ||
HANDLE hProcess = NULL; | ||
hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid_); | ||
//hProcess = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, (DWORD)pid_); | ||
if (hProcess != NULL) { | ||
CloseHandle(hProcess); | ||
return true; | ||
} | ||
#elif defined(__linux) || defined(__linux__) | ||
|
||
#endif | ||
return false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#ifndef XOP_PROCESS_H | ||
#define XOP_PROCESS_H | ||
|
||
#include <string> | ||
#include <cstdio> | ||
#if defined(WIN32) || defined(_WIN32) | ||
#include <Windows.h> | ||
#endif | ||
|
||
namespace xop { | ||
|
||
class Process | ||
{ | ||
public: | ||
Process(); | ||
virtual ~Process(); | ||
|
||
bool Start(std::string app_path, std::string cmd, std::string log_path =""); | ||
void Stop(); | ||
bool IsAlive(); | ||
|
||
private: | ||
|
||
std::string command_; | ||
int64_t pid_; | ||
#if defined(WIN32) || defined(_WIN32) | ||
HANDLE file_ = INVALID_HANDLE_VALUE; | ||
#endif | ||
}; | ||
|
||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters