summaryrefslogtreecommitdiffstats
path: root/Source/cmWin32ProcessExecution.cxx
diff options
context:
space:
mode:
authorAndy Cedilnik <andy.cedilnik@kitware.com>2002-10-04 14:38:14 (GMT)
committerAndy Cedilnik <andy.cedilnik@kitware.com>2002-10-04 14:38:14 (GMT)
commit0f3661562c396f0d58c09eec54844bbd4d48656d (patch)
tree93f87969e5afc19c9856c7d9d8eea7380848eafd /Source/cmWin32ProcessExecution.cxx
parent38d1ea9b4c6d79fff36cc19e3fb81a5253b6665f (diff)
downloadCMake-0f3661562c396f0d58c09eec54844bbd4d48656d.zip
CMake-0f3661562c396f0d58c09eec54844bbd4d48656d.tar.gz
CMake-0f3661562c396f0d58c09eec54844bbd4d48656d.tar.bz2
Cleanup RunCOmmand code and move borland one to vtkWin32ProcessExecution, so that it is all in one place... Add timeout option whihc does not work yet, but it should not produce warning any more
Diffstat (limited to 'Source/cmWin32ProcessExecution.cxx')
-rw-r--r--Source/cmWin32ProcessExecution.cxx212
1 files changed, 211 insertions, 1 deletions
diff --git a/Source/cmWin32ProcessExecution.cxx b/Source/cmWin32ProcessExecution.cxx
index db509ab..96f0fd5 100644
--- a/Source/cmWin32ProcessExecution.cxx
+++ b/Source/cmWin32ProcessExecution.cxx
@@ -48,6 +48,216 @@
#define win32_error(x,y) std::cout << "Win32_Error(" << x << ", " << y << ")" << std::endl, false
+void DisplayErrorMessage()
+{
+ LPVOID lpMsgBuf;
+ FormatMessage(
+ FORMAT_MESSAGE_ALLOCATE_BUFFER |
+ FORMAT_MESSAGE_FROM_SYSTEM |
+ FORMAT_MESSAGE_IGNORE_INSERTS,
+ NULL,
+ GetLastError(),
+ MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
+ (LPTSTR) &lpMsgBuf,
+ 0,
+ NULL
+ );
+ // Process any inserts in lpMsgBuf.
+ // ...
+ // Display the string.
+ MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION );
+ // Free the buffer.
+ LocalFree( lpMsgBuf );
+}
+
+// Code from a Borland web site with the following explaination :
+/* In this article, I will explain how to spawn a console application
+ * and redirect its standard input/output using anonymous pipes. An
+ * anonymous pipe is a pipe that goes only in one direction (read
+ * pipe, write pipe, etc.). Maybe you are asking, "why would I ever
+ * need to do this sort of thing?" One example would be a Windows
+ * telnet server, where you spawn a shell and listen on a port and
+ * send and receive data between the shell and the socket
+ * client. (Windows does not really have a built-in remote
+ * shell). First, we should talk about pipes. A pipe in Windows is
+ * simply a method of communication, often between process. The SDK
+ * defines a pipe as "a communication conduit with two ends;
+ a process
+ * with a handle to one end can communicate with a process having a
+ * handle to the other end." In our case, we are using "anonymous"
+ * pipes, one-way pipes that "transfer data between a parent process
+ * and a child process or between two child processes of the same
+ * parent process." It's easiest to imagine a pipe as its namesake. An
+ * actual pipe running between processes that can carry data. We are
+ * using anonymous pipes because the console app we are spawning is a
+ * child process. We use the CreatePipe function which will create an
+ * anonymous pipe and return a read handle and a write handle. We will
+ * create two pipes, on for stdin and one for stdout. We will then
+ * monitor the read end of the stdout pipe to check for display on our
+ * child process. Every time there is something availabe for reading,
+ * we will display it in our app. Consequently, we check for input in
+ * our app and send it off to the write end of the stdin pipe. */
+
+inline bool IsWinNT()
+//check if we're running NT
+{
+ OSVERSIONINFO osv;
+ osv.dwOSVersionInfoSize = sizeof(osv);
+ GetVersionEx(&osv);
+ return (osv.dwPlatformId == VER_PLATFORM_WIN32_NT);
+}
+
+//---------------------------------------------------------------------------
+bool cmWin32ProcessExecution::BorlandRunCommand(
+ const char* command, const char* dir,
+ std::string& output, int& retVal, bool verbose, int /* timeout */)
+{
+ //verbose = true;
+ //std::cerr << std::endl
+ // << "WindowsRunCommand(" << command << ")" << std::endl
+ // << std::flush;
+ const int BUFFER_SIZE = 4096;
+ char buf[BUFFER_SIZE];
+
+//i/o buffer
+ STARTUPINFO si;
+ SECURITY_ATTRIBUTES sa;
+ SECURITY_DESCRIPTOR sd;
+
+//security information for pipes
+ PROCESS_INFORMATION pi;
+ HANDLE newstdin,newstdout,read_stdout,write_stdin;
+
+//pipe handles
+ if (IsWinNT())
+//initialize security descriptor (Windows NT)
+ {
+ InitializeSecurityDescriptor(&sd,SECURITY_DESCRIPTOR_REVISION);
+ SetSecurityDescriptorDacl(&sd, true, NULL, false);
+ sa.lpSecurityDescriptor = &sd;
+
+ }
+ else sa.lpSecurityDescriptor = NULL;
+ sa.nLength = sizeof(SECURITY_ATTRIBUTES);
+ sa.bInheritHandle = true;
+
+//allow inheritable handles
+ if (!CreatePipe(&newstdin,&write_stdin,&sa,0))
+//create stdin pipe
+ {
+ std::cerr << "CreatePipe" << std::endl;
+ return false;
+
+ }
+ if (!CreatePipe(&read_stdout,&newstdout,&sa,0))
+//create stdout pipe
+ {
+ std::cerr << "CreatePipe" << std::endl;
+ CloseHandle(newstdin);
+ CloseHandle(write_stdin);
+ return false;
+
+ }
+ GetStartupInfo(&si);
+
+//set startupinfo for the spawned process
+ /* The dwFlags member tells CreateProcess how to make the
+ * process. STARTF_USESTDHANDLES validates the hStd*
+ * members. STARTF_USESHOWWINDOW validates the wShowWindow
+ * member. */
+
+ si.cb = sizeof(STARTUPINFO);
+ si.dwFlags = STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW;
+ si.hStdOutput = newstdout;
+ si.hStdError = newstdout;
+ si.wShowWindow = SW_HIDE;
+
+//set the new handles for the child process si.hStdInput = newstdin;
+ char* commandAndArgs = strcpy(new char[strlen(command)+1], command);
+ if (!CreateProcess(NULL,commandAndArgs,NULL,NULL,TRUE,CREATE_NEW_CONSOLE,
+ NULL,dir,&si,&pi))
+ {
+ std::cerr << "CreateProcess failed " << commandAndArgs << std::endl;
+ CloseHandle(newstdin);
+ CloseHandle(newstdout);
+ CloseHandle(read_stdout);
+ CloseHandle(write_stdin);
+ delete [] commandAndArgs;
+ return false;
+
+ }
+ delete [] commandAndArgs;
+ unsigned long exit=0;
+
+//process exit code unsigned
+ unsigned long bread;
+
+//bytes read unsigned
+ unsigned long avail;
+
+//bytes available
+ memset(buf, 0, sizeof(buf));
+ for(;;)
+//main program loop
+ {
+ Sleep(10);
+//check to see if there is any data to read from stdout
+ //std::cout << "Peek for data..." << std::endl;
+ PeekNamedPipe(read_stdout,buf,1023,&bread,&avail,NULL);
+ if (bread != 0)
+ {
+ memset(buf, 0, sizeof(buf));
+ if (avail > 1023)
+ {
+ while (bread >= 1023)
+ {
+ //std::cout << "Read data..." << std::endl;
+ ReadFile(read_stdout,buf,1023,&bread,NULL);
+
+ //read the stdout pipe
+ memset(buf, 0, sizeof(buf));
+ output += buf;
+ if (verbose)
+ {
+ std::cout << buf << std::flush;
+ }
+ }
+ }
+ else
+ {
+ ReadFile(read_stdout,buf,1023,&bread,NULL);
+ output += buf;
+ if(verbose)
+ {
+ std::cout << buf << std::flush;
+ }
+
+ }
+
+ }
+
+ //std::cout << "Check for process..." << std::endl;
+ GetExitCodeProcess(pi.hProcess,&exit);
+
+//while the process is running
+ if (exit != STILL_ACTIVE) break;
+
+ }
+ WaitForSingleObject(pi.hProcess, INFINITE);
+ GetExitCodeProcess(pi.hProcess,&exit);
+ CloseHandle(pi.hThread);
+ CloseHandle(pi.hProcess);
+ CloseHandle(newstdin);
+
+//clean stuff up
+ CloseHandle(newstdout);
+ CloseHandle(read_stdout);
+ CloseHandle(write_stdin);
+ retVal = exit;
+ return true;
+
+}
+
bool cmWin32ProcessExecution::StartProcess(
const char* cmd, const char* path, bool verbose)
{
@@ -503,7 +713,7 @@ bool cmWin32ProcessExecution::PrivateOpen(const char *cmdstring,
* holding the global lock.
*/
-bool cmWin32ProcessExecution::PrivateClose(int timeout)
+bool cmWin32ProcessExecution::PrivateClose(int /* timeout */)
{
HANDLE hProcess = this->m_ProcessHandle;