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 "XPCProcessAttrib.h" 00035 00037 // Construction/Destruction 00039 00040 XPCProcessAttrib::XPCProcessAttrib(LPTSTR appName, 00041 LPSTR commandLine, 00042 BOOL processInheritable, 00043 BOOL threadInheritable, 00044 BOOL inheritHandles, 00045 DWORD creationFlags, 00046 LPVOID environment, 00047 LPTSTR currentDirectory) 00048 { 00049 pSI= (LPSTARTUPINFO)HeapAlloc (GetProcessHeap(), 00050 HEAP_ZERO_MEMORY, 00051 sizeof (STARTUPINFO)); 00052 00053 pProcessSA= (LPSECURITY_ATTRIBUTES) HeapAlloc (GetProcessHeap(), 00054 HEAP_ZERO_MEMORY, 00055 sizeof (SECURITY_ATTRIBUTES)); 00056 00057 pThreadSA= (LPSECURITY_ATTRIBUTES) HeapAlloc (GetProcessHeap(), 00058 HEAP_ZERO_MEMORY, 00059 sizeof (SECURITY_ATTRIBUTES)); 00060 00061 pProcessInfo= (LPPROCESS_INFORMATION) HeapAlloc (GetProcessHeap(), 00062 HEAP_ZERO_MEMORY, 00063 sizeof (PROCESS_INFORMATION)); 00064 00065 if (!pSI || !pProcessSA || !pThreadSA || !pProcessInfo) 00066 { 00067 XPCException ex (ErrorString("HeapAlloc failed")); 00068 throw ex; 00069 return; 00070 } 00071 00072 pCurrentDirectory= pApplicationName= pCommandLine= NULL; 00073 pEnvironment= NULL; 00074 00075 GetStartupInfo (pSI); 00076 00077 pProcessSA->nLength= sizeof(SECURITY_ATTRIBUTES); 00078 pProcessSA->lpSecurityDescriptor= NULL; //no restrictions 00079 pProcessSA->bInheritHandle= processInheritable; 00080 00081 pThreadSA->nLength= sizeof(SECURITY_ATTRIBUTES); 00082 pThreadSA->lpSecurityDescriptor= NULL; //no restrictions 00083 pThreadSA->bInheritHandle= threadInheritable; 00084 00085 bInheritHandles= inheritHandles; 00086 dwCreationFlags= creationFlags; 00087 00088 vSetCurrentDirectory (currentDirectory); 00089 vSetCommandLine (commandLine); 00090 vSetApplicationName (appName); 00091 vSetEnvironment (environment); 00092 } 00093 00094 XPCProcessAttrib::~XPCProcessAttrib() 00095 { 00096 HeapFree (GetProcessHeap(), 0, pSI); 00097 HeapFree (GetProcessHeap(), 0, pProcessSA); 00098 HeapFree (GetProcessHeap(), 0, pThreadSA); 00099 HeapFree (GetProcessHeap(), 0, pProcessInfo); 00100 HeapFree (GetProcessHeap(), 0, pEnvironment); 00101 00102 if (pApplicationName) 00103 HeapFree (GetProcessHeap(), 0, pApplicationName); 00104 00105 if (pCommandLine) 00106 HeapFree (GetProcessHeap(), 0, pCommandLine); 00107 00108 if (pCurrentDirectory) 00109 HeapFree (GetProcessHeap(), 0, pCurrentDirectory); 00110 00111 if (pEnvironment) 00112 HeapFree (GetProcessHeap(), 0, pEnvironment); 00113 } 00114 00115 00116 void XPCProcessAttrib::AddEnvironmentVariable(LPCTSTR param, LPCTSTR val) 00117 { 00118 DWORD dwI= 0; 00119 00120 if (dwEnvSize > 0) 00121 dwI= dwEnvSize-1; 00122 00123 if (pEnvironment) 00124 { 00125 pEnvironment= HeapReAlloc (GetProcessHeap(), 00126 HEAP_ZERO_MEMORY, 00127 pEnvironment, 00128 dwEnvSize+= strlen (param) + strlen(val) + 2); 00129 } 00130 else 00131 { 00132 pEnvironment= HeapAlloc (GetProcessHeap(), 00133 HEAP_ZERO_MEMORY, 00134 dwEnvSize= strlen (param) + strlen(val) + 3); 00135 } 00136 00137 if (!pEnvironment) 00138 { 00139 XPCException ex (ErrorString("HeapReAlloc failed")); 00140 throw ex; 00141 return; 00142 } 00143 00144 char* pBuf= (char*) pEnvironment; 00145 strcpy (&pBuf[dwI], param); 00146 dwI+= strlen (param); 00147 strcpy (&pBuf[dwI++], "="); 00148 strcpy (&pBuf[dwI], val); 00149 pBuf[dwEnvSize-1]= '\0'; 00150 } 00151 00152 void XPCProcessAttrib::vSetEnvironment (LPVOID _pEnv) 00153 { 00154 char *pEnv= (char *) _pEnv; 00155 00156 if (pEnvironment) 00157 HeapFree (GetProcessHeap(), 0, pEnvironment); 00158 00159 if (pEnv) 00160 { 00161 for (dwEnvSize= 1; ; dwEnvSize++, pEnv++) 00162 { 00163 if (!*pEnv && !*(pEnv+1)) 00164 break; 00165 } 00166 pEnvironment= HeapAlloc (GetProcessHeap(), HEAP_ZERO_MEMORY, dwEnvSize+1); 00167 if (!pEnvironment) 00168 { 00169 XPCException ex (ErrorString("HeapAlloc failed")); 00170 throw ex; 00171 return; 00172 } 00173 memmove (pEnvironment, _pEnv, dwEnvSize+1); 00174 } 00175 else 00176 { 00177 pEnvironment= NULL; 00178 dwEnvSize= 0; 00179 } 00180 } 00181 00182 00183 void XPCProcessAttrib::vSetCommandLine (LPTSTR commandLine) 00184 { 00185 if (pCommandLine) 00186 { 00187 HeapFree (GetProcessHeap(), 0, pEnvironment); 00188 pCommandLine= NULL; 00189 } 00190 00191 if (commandLine) 00192 { 00193 pCommandLine= (LPTSTR) HeapAlloc (GetProcessHeap(), 0, strlen (commandLine)+1); 00194 if (!pCommandLine) 00195 { 00196 XPCException ex (ErrorString("HeapAlloc failed")); 00197 throw ex; 00198 return; 00199 } 00200 strcpy (pCommandLine, commandLine); 00201 } 00202 } 00203 00204 00205 void XPCProcessAttrib::vSetApplicationName (LPTSTR appName) 00206 { 00207 if (pApplicationName) 00208 { 00209 HeapFree (GetProcessHeap(), 0, pApplicationName); 00210 pApplicationName= NULL; 00211 } 00212 00213 if (appName) 00214 { 00215 pApplicationName= (LPTSTR) HeapAlloc (GetProcessHeap(), 0, strlen (appName)+1); 00216 if (!pApplicationName) 00217 { 00218 XPCException ex (ErrorString("HeapAlloc failed")); 00219 throw ex; 00220 return; 00221 } 00222 strcpy (pApplicationName, appName); 00223 } 00224 } 00225 00226 00227 void XPCProcessAttrib::vSetCurrentDirectory (LPTSTR currentDirectory) 00228 { 00229 if (pCurrentDirectory) 00230 { 00231 HeapFree (GetProcessHeap(), 0, pCurrentDirectory); 00232 pCurrentDirectory= NULL; 00233 } 00234 00235 if (currentDirectory) 00236 { 00237 pCurrentDirectory= (LPTSTR) HeapAlloc (GetProcessHeap(), 0, 00238 strlen (currentDirectory)+1); 00239 if (!pCurrentDirectory) 00240 { 00241 XPCException ex (ErrorString("HeapAlloc failed")); 00242 throw ex; 00243 return; 00244 } 00245 strcpy (pCurrentDirectory, currentDirectory); 00246 } 00247 } 00248 00249 char * XPCProcessAttrib::ErrorString(char * sLeader) 00250 { 00251 static char buf[255]; 00252 LPVOID lpMsgBuf; 00253 UINT ErrNo; 00254 00255 FormatMessage ( 00256 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, 00257 NULL, 00258 ErrNo=GetLastError (), 00259 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language 00260 (LPTSTR) &lpMsgBuf, 00261 0, 00262 NULL); 00263 00264 wsprintf (buf, "%s: %s", sLeader, (LPTSTR)lpMsgBuf); 00265 00266 LocalFree (lpMsgBuf); 00267 return buf; 00268 }