MOOS 0.2375
|
00001 00002 // 00003 // MOOS - Mission Oriented Operating Suite 00004 // 00005 // A suit of Applications and Libraries for Mobile Robotics Research 00006 // Copyright (C) 2001-2005 Massachusetts Institute of Technology and 00007 // Oxford University. 00008 // 00009 // This software was written by Paul Newman at MIT 2001-2002 and Oxford 00010 // University 2003-2005. email: pnewman@robots.ox.ac.uk. 00011 // 00012 // This file is part of a MOOS CORE Component. 00013 // 00014 // This program is free software; you can redistribute it and/or 00015 // modify it under the terms of the GNU General Public License as 00016 // published by the Free Software Foundation; either version 2 of the 00017 // License, or (at your option) any later version. 00018 // 00019 // This program is distributed in the hope that it will be useful, 00020 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00021 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00022 // General Public License for more details. 00023 // 00024 // You should have received a copy of the GNU General Public License 00025 // along with this program; if not, write to the Free Software 00026 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 00027 // 02111-1307, USA. 00028 // 00029 // The XPC classes in MOOS are modified versions of the source provided 00030 // in "Making UNIX and Windows NT Talk" by Mark Nadelson and Thomas Haga 00031 // 00033 00034 #include "XPCProcess.h" 00035 #include "XPCProcessAttrib.h" 00036 #include "XPCException.h" 00037 00039 // Construction/Destruction 00041 00042 XPCProcess::XPCProcess(XPCProcessAttrib& attrib): 00043 procAttrib (attrib) 00044 { 00045 00046 if (!CreateProcess (attrib.pApplicationName, 00047 attrib.pCommandLine, 00048 attrib.pProcessSA, 00049 attrib.pThreadSA, 00050 attrib.bInheritHandles, 00051 attrib.dwCreationFlags, 00052 attrib.pEnvironment, 00053 attrib.pCurrentDirectory, 00054 attrib.pSI, 00055 attrib.pProcessInfo)) 00056 { 00057 XPCException ex (ErrorString("CreateProcess failed")); 00058 throw ex; 00059 return; 00060 } 00061 } 00062 00063 XPCProcess::~XPCProcess() 00064 { 00065 00066 } 00067 00068 char * XPCProcess::ErrorString(char * sLeader) 00069 { 00070 static char buf[255]; 00071 LPVOID lpMsgBuf; 00072 UINT ErrNo; 00073 00074 FormatMessage ( 00075 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, 00076 NULL, 00077 ErrNo=GetLastError (), 00078 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language 00079 (LPTSTR) &lpMsgBuf, 00080 0, 00081 NULL); 00082 00083 wsprintf (buf, "%s: %s", sLeader, (LPTSTR)lpMsgBuf); 00084 00085 LocalFree (lpMsgBuf); 00086 return buf; 00087 } 00088 00089 DWORD XPCProcess::dwGetExitCode() 00090 { 00091 DWORD dwRet=0; 00092 00093 if (!GetExitCodeProcess (procAttrib.pGetProcessInformation()->hProcess, &dwRet)) 00094 { 00095 XPCException ex (ErrorString("GetExitCodeProcess failed")); 00096 throw ex; 00097 } 00098 return (dwRet); 00099 } 00100 00101 00102 void XPCProcess::vTerminate() 00103 { 00104 if (!TerminateProcess (procAttrib.pGetProcessInformation()->hProcess, 1)) 00105 { 00106 XPCException ex (ErrorString("TerminateProcess failed")); 00107 throw ex; 00108 return; 00109 } 00110 } 00111 00112 void XPCProcess::vWaitForTerminate(DWORD dwWaitTime) 00113 { 00114 if (WaitForSingleObject (procAttrib.pGetProcessInformation()->hProcess, 00115 dwWaitTime) == WAIT_FAILED) 00116 { 00117 XPCException ex (ErrorString("WaitForSingleObject failed")); 00118 throw ex; 00119 return; 00120 } 00121 } 00122 00123 void XPCProcess::vResume() 00124 { 00125 if (ResumeThread (procAttrib.pGetProcessInformation()->hThread) == 0xFFFFFFFF) 00126 { 00127 XPCException ex (ErrorString("ResumeThread failed")); 00128 throw ex; 00129 return; 00130 } 00131 }