C++から簡単にPHP、CGIなどを呼び出せます。 方法は以下のように:
1)まず、pipeを作成
SECURITY_ATTRIBUTES saSample = {sizeof(SECURITY_ATTRIBUTES)}; saSample.bInheritHandle = 1; saSample.lpSecurityDescriptor = NULL; HANDLE hStdoutR, hStdoutW, hStdinR, hStdinW; CreatePipe(&hStdoutR, &hStdoutW, &saSample, 0); SetHandleInformation(hStdoutR,HANDLE_FLAG_INHERIT, 0); CreatePipe(&hStdinR, &hStdinW, &saSample, 0); SetHandleInformation(hStdinW, HANDLE_FLAG_INHERIT, 0); |
2)そして、PHP-CGIを呼び出す
STARTUPINFO siSample = {sizeof(STARTUPINFO)}; PROCESS_INFORMATION pi; siSample.dwFlags = STARTF_USESTDHANDLES; siSample.hStdOutput = hStdoutW; siSample.hStdInput = hStdinR; char env[255] = "REQUEST_METHOD=POST\0CONTENT_LENGTH=18\0CONTENT_TYPE= application/x-www-form-urlencoded\0SCRIPT_FILENAME=D:\\test.php"; if(!CreateProcess(NULL, "d:\\php5\\php-cgi.exe D:\\test.php", NULL, NULL, 1, NORMAL_PRIORITY_CLASS, env, NULL, &siSample, π)) return 0;
CloseHandle(hStdoutW); CloseHandle(hStdinR); |
3)データをPHP−CGIに渡す
if(!WriteFile(hStdinW, "v1=Hello World!",15, &dwWritten, NULL)) return 0; CloseHandle(hStdinW); |
4)戻り値を獲得
char buf[1000] = {0}; DWORD dwRead = 0; while(ReadFile(hStdoutR, buf, sizeof(buf), &dwRead, NULL) && dwRead != 0){ printf(buf); }
CloseHandle(hStdoutR); |
●test.phpの内容:
<? echo $_REQUEST["v1"]; ?> |
●実行結果:
X-Powered-By: PHP/5.X.X Content-type: text/html
Hello World! |
|