代码中存在错误,仅作自己的备忘之用。
参考一:
CreatePipe(@fInputRead, @fInputWrite, nil, 0);
...
aStartupInfo.hStdInput := fInputRead;
aStartupInfo.hStdOutput := INVALID_HANDLE_VALUE;
aStartupInfo.hStdError := INVALID_HANDLE_VALUE;
CreateProcess('php-cgi.exe', ... , aStartupInfo);
...
WriteFile(fInputWrite, ...);
...
//------------------------------------
CreatePipe(@fInputRead, @fInputWrite, nil, 0);
CreatePipe(@fOutputRead, @fOutputWrite, nil, 0);
...
aStartupInfo.hStdInput := fInputRead;
aStartupInfo.hStdOutput := fOutputWrite;
aStartupInfo.hStdError := fOutputWrite;
CreateProcess('php-cgi.exe', ... , aStartupInfo);
...
WriteFile(fInputWrite, ...);
ReadFile(fOutputRead, ...);
...
//-----------------------------------
参考二:
/*
* Determine if this library is being used to listen for FastCGI
* connections. This is communicated by STDIN containing a
* valid handle to a listener object. In this case, both the
* "stdout" and "stderr" handles will be INVALID (ie. closed) by
* the starting process.
*/
if((GetStdHandle(STD_OUTPUT_HANDLE) == INVALID_HANDLE_VALUE) &&
(GetStdHandle(STD_ERROR_HANDLE) == INVALID_HANDLE_VALUE) &&
(GetStdHandle(STD_INPUT_HANDLE) != INVALID_HANDLE_VALUE) )
{
DWORD pipeMode = PIPE_READMODE_BYTE | PIPE_WAIT;
HANDLE oldStdIn = GetStdHandle(STD_INPUT_HANDLE);
// Move the handle to a "low" number
if (! DuplicateHandle(GetCurrentProcess(), oldStdIn,
GetCurrentProcess(), &hListen,
0, TRUE, DUPLICATE_SAME_ACCESS))
{
return -1;
}
if (! SetStdHandle(STD_INPUT_HANDLE, hListen))
{
return -1;
}
CloseHandle(oldStdIn);
/*
* Set the pipe handle state so that it operates in wait mode.
*/
if (SetNamedPipeHandleState(hListen, &pipeMode, NULL, NULL))
{
listenType = FD_PIPE_SYNC;
}
else
{
listenType = FD_SOCKET_SYNC;
}
}
参考三:
-------------------------------------------------------------------
# include
# include
# define close closesocket
term() {
printf("err:%d", (GetLastError()));
exit(1);
}
struct sockaddr_in saddr;
main() {
SOCKET listen_handle;
SOCKADDR_IN saServer;
char *cmd = "php-cgi.exe";
STARTUPINFO su_info;
SECURITY_ATTRIBUTES sa = { 0 };
WORD wVersionRequested = MAKEWORD(1,1);
WSADATA wsaData;
int n = WSAStartup(wVersionRequested, &wsaData);
if (wsaData.wVersion != wVersionRequested) term();
listen_handle = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
saServer.sin_family = AF_INET;
saServer.sin_addr.s_addr = INADDR_ANY;
saServer.sin_port = htons(9667);
n = bind(listen_handle, (LPSOCKADDR)&saServer, sizeof(struct sockaddr));
if (n == SOCKET_ERROR) term();
n = listen(listen_handle, 20);
if (n == SOCKET_ERROR) term();
ZeroMemory(&su_info, sizeof(STARTUPINFO));
su_info.cb = sizeof(STARTUPINFO);
su_info.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
su_info.wShowWindow = SW_HIDE;
su_info.hStdInput = listen_handle;
su_info.hStdError = INVALID_HANDLE_VALUE;
su_info.hStdOutput = INVALID_HANDLE_VALUE;
SetHandleInformation(su_info.hStdInput, HANDLE_FLAG_INHERIT, TRUE);
PROCESS_INFORMATION p;
if(CreateProcess(NULL, cmd, NULL, NULL, 1, 0, NULL, NULL, &su_info, &p)) {
CloseHandle(p.hThread); closesocket(listen_handle);
} else {
term();
}
if( WaitForSingleObject(p.hProcess, INFINITE) == WAIT_FAILED ){
term();
}
WSACleanup();
}
----------------------------------
参考四:
Test script: (D:\test.php)
---------------------------------
---------------------------------
C++ source:
---------------------------------
#include
#include
#include
int main()
{
SECURITY_ATTRIBUTES sa = {sizeof(SECURITY_ATTRIBUTES)};
sa.bInheritHandle = 1;
sa.lpSecurityDescriptor = NULL;
HANDLE hStdoutR, hStdoutW, hStdinR, hStdinW;
CreatePipe(&hStdoutR,&hStdoutW,&sa,0);
SetHandleInformation(hStdoutR,HANDLE_FLAG_INHERIT,0);
CreatePipe(&hStdinR,&hStdinW,&sa,0);
SetHandleInformation(hStdinW,HANDLE_FLAG_INHERIT,0);
STARTUPINFO si = {sizeof(STARTUPINFO)};
PROCESS_INFORMATION pi;
si.dwFlags = STARTF_USESTDHANDLES;
si.hStdOutput = hStdoutW;
si.hStdInput = hStdinR;
char env[255] =
"REQUEST_METHOD=POST\0CONTENT_LENGTH=17\0CONTENT_TYPE=application/x-www-form-urlencoded\0SCRIPT_FILENAME=D:\\test.php";
if(!CreateProcess(NULL,"php-5.2.9-1-Win32\\php-cgi.exe D:\\test.php",NULL,NULL,1,NORMAL_PRIORITY_CLASS,env,NULL,&si,&pi))
return 0;
CloseHandle(hStdoutW);
CloseHandle(hStdinR);
DWORD dwWritten = 0;
//Write post data here?
if(!WriteFile(hStdinW,"var=post+variable",20,&dwWritten,NULL))
return 0;
CloseHandle(hStdinW);
char buf[1000] = {0};
DWORD dwRead = 0;
while(ReadFile(hStdoutR,buf,sizeof(buf),&dwRead,NULL) && dwRead != 0){
printf(buf);
}
printf("|\n\nEND");
CloseHandle(hStdoutR);
getch();
return 0;
}