note 62162 added to features.commandline

From: Date: Tue, 21 Feb 2006 18:27:31 +0000
Subject: note 62162 added to features.commandline
Groups: php.notes 
Request: Send a blank email to php-notes+get-104143@lists.php.net to get a copy of this message
Spawning php-win.exe as a child process to handle scripting in Windows applications has a few quirks (all having to do with pipes between Windows apps and console apps). To do this in C++: // We will run php.exe as a child process after creating // two pipes and attaching them to stdin and stdout // of the child process // Define sa struct such that child inherits our handles SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES) }; sa.bInheritHandle = TRUE; sa.lpSecurityDescriptor = NULL; // Create the handles for our two pipes (two handles per pipe, one for each end) // We will have one pipe for stdin, and one for stdout, each with a READ and WRITE end HANDLE hStdoutRd, hStdoutWr, hStdinRd, hStdinWr; // Now create the pipes, and make them inheritable CreatePipe (&hStdoutRd, &hStdoutWr, &sa, 0)) SetHandleInformation(hStdoutRd, HANDLE_FLAG_INHERIT, 0); CreatePipe (&hStdinRd, &hStdinWr, &sa, 0) SetHandleInformation(hStdinWr, HANDLE_FLAG_INHERIT, 0); // Now we have two pipes, we can create the process // First, fill out the usage structs STARTUPINFO si = { sizeof(STARTUPINFO) }; PROCESS_INFORMATION pi; si.dwFlags = STARTF_USESTDHANDLES; si.hStdOutput = hStdoutWr; si.hStdInput = hStdinRd; // And finally, create the process CreateProcess (NULL, "c:\\php\\php-win.exe", NULL, NULL, TRUE, NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi); // Close the handles we aren't using CloseHandle(hStdoutWr); CloseHandle(hStdinRd); // Now that we have the process running, we can start pushing PHP at it WriteFile(hStdinWr, "<?php echo 'test'; ?>", 9, &dwWritten, NULL); // When we're done writing to stdin, we close that pipe CloseHandle(hStdinWr); // Reading from stdout is only slightly more complicated int i; std::string processed(""); char buf[128]; while ( (ReadFile(hStdoutRd, buf, 128, &dwRead, NULL) && (dwRead != 0)) ) { for (i = 0; i < dwRead; i++) processed += buf[i]; } // Done reading, so close this handle too CloseHandle(hStdoutRd); A full implementation (implemented as a C++ class) is available at http://www.stromcode.com ---- Server IP: 64.71.164.2 Probable Submitter: 68.171.55.65 ---- Manual Page -- http://www.php.net/manual/en/features.commandline.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+62162 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+62162&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+62162&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+62162&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+62162&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+62162&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+62162&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+62162&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+62162&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#104143) next »