1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
| #include "CommonDLL.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
CMcrProcess::CMcrProcess(void)
: CMcrHandle(L"CMcrProcess")
{
ProcessSetNoIdentity();
ProcessResetCreationFlag();
m_thread_id = 0;
m_process_id = 0;
}
CMcrProcess::~CMcrProcess(void)
{
ProcessSetNoIdentity();
if(ProcessIsRunning() == true)
ProcessKill();
}
bool CMcrProcess::ProcessSetIdentity(MCR_CSTR Domain, MCR_CSTR Login, MCR_CSTR Password)
{
// not allowed to change identity when running
if(ProcessIsRunning() == true)
return false;
ProcessSetNoIdentity();
if(Domain != NULL)
{
m_run_as_domain = Domain;
}
else
{
// domain name is host name
CMcrNetwork::GetMyHostName(m_run_as_domain);
}
if(Login != NULL)
m_run_as_login = Login;
if(Password != NULL)
m_run_as_password = Password;
// all is correct
return true;
}
bool CMcrProcess::ProcessSetIdentity(MCR_CSTR Login, MCR_CSTR Password)
{
// check the login name
if(Login == NULL)
{
ERROR1(L"NULL login name");
return false;
}
// try to find the '\' separator character
MCR_STRING str = Login;
int pos = str.Find(C_BACKSLASH);
if(pos == -1)
{
// try to find the '@' separator character
pos = str.Find('@');
if(pos == -1)
{
return ProcessSetIdentity(NULL, Login, Password);
}
// extract domain name and login
MCR_STRING login = str.Left(pos);
MCR_STRING domain = str.Mid(pos + 1);
return ProcessSetIdentity(domain, login, Password);
}
// extract domain name and login
MCR_STRING domain = str.Left(pos);
MCR_STRING login = str.Mid(pos + 1);
return ProcessSetIdentity(domain, login, Password);
}
void CMcrProcess::ProcessSetNoIdentity(void)
{
m_run_as_domain = EMPTY_STR;
m_run_as_login = EMPTY_STR;
m_run_as_password = EMPTY_STR;
}
bool CMcrProcess::ProcessStart(MCR_CSTR ProgName, MCR_CSTR Param)
{
// test if running
if(ProcessIsRunning() == true)
{
INFO1(L"The process is already running");
return false;
}
// save the parameters
m_ProgName = ProgName;
m_Param = Param;
// create the command name
MCR_STRING command = m_ProgName;
if(m_Param.GetLength() != 0)
{
command += S_SPACE;
command += m_Param;
}
// Set up members of the PROCESS_INFORMATION structure.
PROCESS_INFORMATION pi;
::memset(&pi, 0, sizeof(pi));
// test if the change identity is on
BOOL ret;
if(m_run_as_login.GetLength() != 0)
{
STARTUPINFOW siw;
memset(&siw, 0, sizeof(siw));
siw.cb = sizeof(siw);
// start the process with the new identity
// WARNING1, this call will fail if running in debug mode
// it seems that only the user 'localsystem' is privilegied enougth
// to successfully execute this call
ret = ::CreateProcessWithLogonW(m_run_as_login,
m_run_as_domain,
m_run_as_password,
LOGON_WITH_PROFILE,
NULL, // name of executable module
(MCR_STR)(MCR_CSTR)command, // command line string
ProcessGetCreationFlag(),
NULL,
CMcrFile::GetWorkingDirectory(),
&siw, // startup information
&pi); // process information
if(ret == FALSE)
{
// a problem, this is probably an login problem
// restart the same process with no account
ERROR4(L"Error 0x%.8x when starting the process with the %s\\%s identity", GetLastError(), m_run_as_domain, m_run_as_login);
WARNING1(L"The same process will be restarted without credentials");
ProcessSetNoIdentity();
return ProcessStart(ProgName, Param);
}
//INFO3(L"Process is successfully started with the %s\\%s identity", m_run_as_domain, m_run_as_login);
}
else
{
// Set up members of the STARTUPINFO structure.
STARTUPINFO si;
memset(&si, 0, sizeof(si));
si.cb = sizeof(si);
ret = ::CreateProcess( NULL, // name of executable module
(MCR_STR)(MCR_CSTR)command, // command line string
NULL, // SD
NULL, // SD
FALSE, // handle inheritance option
ProcessGetCreationFlag(), // creation flags
NULL, // new environment block
CMcrFile::GetWorkingDirectory(),// current directory name
&si, // startup information
&pi); // process information
}
if(ret == FALSE)
{
ERROR2(L"%s", CMcr::McrGetErrorString());
INFO1(L"Error when starting the process");
return false;
}
// get the thread ID and process ID
m_process_id = pi.dwProcessId;
m_thread_id = pi.dwThreadId;
HandleSet(pi.hProcess);
::CloseHandle(pi.hThread);
// the process is running
//INFO2(L"Starting the process '%s'", command);
return true;
}
bool CMcrProcess::ProcessDetach(void)
{
// test if running
if(ProcessIsRunning() == false)
{
INFO1(L"The process is not running");
return false;
}
// No further communication with process required, so close the handle
INFO1(L"Detaching a process");
_reset_all();
return true;
}
bool CMcrProcess::ProcessKill(void)
{
// test if running
if(ProcessIsRunning() == false)
{
INFO1(L"The process is not running");
return false;
}
// kill the process
BOOL ret = ::TerminateProcess( HandleGet(), // handle to the process
(MCR_U4)-1); // exit code for the process
if(ret == FALSE)
{
INFO1(L"Error when killing the process");
}
// No further communication with process required, so close the handle
_reset_all();
// the process is killed
INFO1(L"Killing a process");
return true;
}
bool CMcrProcess::ProcessRestart(void)
{
// test if running
if(ProcessIsRunning() == true)
{
INFO1(L"The process is already running");
return false;
}
// restart the process
return ProcessStart(m_ProgName, m_Param);
}
bool CMcrProcess::ProcessIsRunning(void)
{
// test if started
if(HandleIsValid() == false)
return false;
// try to get the process exit code
MCR_U4 Exit;
BOOL ret = ::GetExitCodeProcess(HandleGet(), // handle to the process
&Exit); // termination status
if(ret == FALSE)
{
// unable to get information, the process has exited
_reset_all();
return false;
}
// test if still running
if(Exit != STILL_ACTIVE)
{
// the process has exited
_reset_all();
return false;
}
// the process is still running
return true;
}
bool CMcrProcess::ProcessWaitUntilInputIdle(void)
{
// test if running
if(ProcessIsRunning() == false)
{
INFO1(L"The process is not running");
return false;
}
// wait indefinitely until the input of the process idle
::WaitForInputIdle(HandleGet(), INFINITE);
return true;
}
bool CMcrProcess::ProcessWaitUntilEnd(void)
{
// test if running
if(ProcessIsRunning() == false)
{
INFO1(L"The process is not running");
return false;
}
//INFO1(L"Wait until the process exits");
HandleWait();
// No further communication with process required, so close the handle
_reset_all();
// the process is killed
return true;
}
void CMcrProcess::ProcessSetVisible(void)
{
m_creation_flag &= ~CREATE_NO_WINDOW;
m_creation_flag |= CREATE_UNICODE_ENVIRONMENT;
}
void CMcrProcess::ProcessSetNotVisible(void)
{
m_creation_flag |= CREATE_NO_WINDOW;
}
MCR_STRING CMcrProcess::ProcessQuote(MCR_CSTR String_In)
{
MCR_STRING ret = String_In;
ret.Replace(L"'", L"''");
ret.Replace(L"\"", L"'q");
return ret;
}
MCR_STRING CMcrProcess::ProcessUnquote(MCR_CSTR String_In)
{
MCR_STRING ret = String_In;
ret.Replace(L"'q", L"\"");
ret.Replace(L"''", L"'");
return ret;
}
void CMcrProcess::_reset_all(void)
{
HandleClose();
m_process_id = 0;
m_thread_id = 0;
} |