UtilProcess.cpp
上传用户:lbr_007
上传日期:2019-05-31
资源大小:282k
文件大小:2k
- #include "stdafx.h"
- #include <UtilProcess.h>
- UtilProcess::UtilProcess(void)
- {
- InitProcess();
- }
- UtilProcess::~UtilProcess(void)
- {
- CloseProcessHandle();
- CloseThreadHandle();
- }
- void UtilProcess::Initialize(void)
- {
- CloseProcessHandle();
- CloseThreadHandle();
- InitProcess();
- }
- void UtilProcess::CloseProcessHandle(void)
- {
- CloseHandle(m_piProcessB.hProcess);
- }
- void UtilProcess::CloseThreadHandle(void)
- {
- CloseHandle(m_piProcessB.hThread);
- }
- bool UtilProcess::CreateProcess(const char * app_path, const char * cmdline,
- bool showWindow)
- {
- STARTUPINFO si;
- SECURITY_ATTRIBUTES saProcess, saThread;
- ZeroMemory(&si, sizeof(si));
- si.cb = sizeof(si);
- saProcess.nLength = sizeof(saProcess);
- saProcess.lpSecurityDescriptor = NULL;
- saProcess.bInheritHandle = TRUE;
- saThread.nLength = sizeof(saThread);
- saThread.lpSecurityDescriptor = NULL;
- saThread.bInheritHandle = FALSE;
- //
- // set the flag that says that we want to use the show/hide setting
- //
- si.dwFlags |= STARTF_USESHOWWINDOW;
- si.wShowWindow = showWindow ? SW_SHOW : SW_HIDE;
- m_app_path = app_path ? app_path : "";
- m_cmdline = cmdline ? cmdline : "";
- bool result = ::CreateProcess(m_app_path.c_str(),
- (char *)m_cmdline.c_str(),&saProcess, &saThread, FALSE, 0,
- NULL, NULL, &si, &m_piProcessB);
- return result;
- }
- bool UtilProcess::IsCreated(void)const
- {
- return (m_piProcessB.hProcess ? true : false);
- }
- bool UtilProcess::IsRunning(void)
- {
- int result = IsCreated();
- if (result)
- {
- DWORD status;
- int code = GetExitCodeProcess(m_piProcessB.hProcess,&status);
- if (status != STILL_ACTIVE)
- {
- result = 0;
- }
- }
- return result;
- }
- INT32 UtilProcess::Terminate(void)
- {
- int result = IsRunning();
- if (result)
- {
- DWORD status = 1;
- int code = TerminateProcess(m_piProcessB.hProcess, status);
- if (code == 0)
- {
- result = 0;
- }
- }
- return result;
- }