Hi to all,
I've a question to ask you.
I've do a function which interface PHP.EXE and redirect the stdin / stdout
so I can pass it a TMemoryStream object with stdin and I receive a
TMemoryStream
with stdout. All is done with a console program in which I launch the
PHP.EXE
between the CreateProcess API. The process is hidden so you don't see
never...
At this point I've a problem.
The Pipe don't pass the EOF (CTRL + Z) char to PHP.EXE stdin so I don't able
to inform PHP.EXE of end of my input stdin sequence.
What I must do to do that ?
Follow a snapshot of my C++ code....
Thank you very much....
Silverio Diquigiovanni
bool __fastcall RunPHP(TStream *Input, TStream *Output)
{
bool Result = false;
// sets security informations
SECURITY_DESCRIPTOR sd;
SECURITY_ATTRIBUTES sa;
LPSECURITY_ATTRIBUTES lpsa = NULL;
if (IsWindowsNT())
{
InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);
SetSecurityDescriptorDacl(&sd, true, NULL, false);
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle = true;
sa.lpSecurityDescriptor = &sd;
lpsa = &sa;
}
// creates & checks stdin pipe
HANDLE h_IN_ReadPipe;
HANDLE h_IN_WritePipe;
assert(CreatePipe(&h_IN_ReadPipe, &h_IN_WritePipe, lpsa, 0));
// creates & checks stdout pie
HANDLE h_OUT_ReadPipe;
HANDLE h_OUT_WritePipe;
assert(CreatePipe(&h_OUT_ReadPipe, &h_OUT_WritePipe, lpsa, 0));
// sets process informations
STARTUPINFO si;
PROCESS_INFORMATION pi;
memset(&si, 0, sizeof (STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
si.dwFlags = STARTF_USESHOWWINDOW |STARTF_USESTDHANDLES;
si.wShowWindow = SW_SHOW;
si.wShowWindow = SW_HIDE;
si.hStdInput = h_IN_ReadPipe;
si.hStdOutput = h_OUT_WritePipe;
si.hStdError = h_OUT_WritePipe;
// creates process
bool process_run = CreateProcess
(
NULL,
"PHP.EXE",
NULL,
NULL,
TRUE,
0,
0,
0,
&si,
&pi
);
// checks process running
if (process_run)
{
THandleStream *InStream = new THandleStream((int) h_IN_WritePipe);
Input->Position = 0;
InStream->CopyFrom(Input, Input->Size);
//
// Here I must send the CTRL + Z to the PHP.EXE but I don't know how....
//
/* tentative n.1: no good :(
TerminateProcess(pi.hProcess, 0x1a);
*/
/* tentative n.2: no good :(
HWND CalledHandle = FindWindow(NULL, "PHP");
SendMessage(CalledHandle, 0x0501, 0x1, 0x010d86a4);
*/
/* tentative n.3: no good :(
HWND CalledHandle = FindWindow(NULL, "PHP");
bool b_ok = BringWindowToTop(CalledHandle);
keybd_event(VK_CONTROL, MapVirtualKey(VK_CONTROL, 0), 0, 0);
keybd_event('Z', MapVirtualKey('Z', 0), 0, 0);
keybd_event('Z', MapVirtualKey('Z', 0), KEYEVENTF_KEYUP, 0);
keybd_event(VK_CONTROL, MapVirtualKey(VK_CONTROL, 0), KEYEVENTF_KEYUP,
0);
// Xxx...
WaitForSingleObject(pi.hProcess, INFINITE);
// Xxx...
DWORD BytesRead;
char Buffer[1024];
while (1)
{
memset(Buffer, 0, sizeof (Buffer));
assert
(
ReadFile
(
h_OUT_ReadPipe,
&Buffer,
sizeof (Buffer),
&BytesRead,
NULL
)
);
Output->Write(Buffer, BytesRead);
if (BytesRead < 1024)
break;
}
Result = true;
// frees objects
delete InStream;
CloseHandle(pi.hProcess);
}
// frees objects
CloseHandle(h_IN_ReadPipe);
CloseHandle(h_IN_WritePipe);
CloseHandle(h_OUT_ReadPipe);
CloseHandle(h_OUT_WritePipe);
return Result;
}
/***************************************************************************
****
* Event handler method
****************************************************************************
***/
void __fastcall TDesktop::RunPHPClick(TObject *Sender)
{
// creates PHPCode & HTMLCode
TMemoryStream *PHPCode = new TMemoryStream;
TMemoryStream *HTMLCode = new TMemoryStream;
// creates a little PHP script flow
AnsiString s =
(
"<?php" + CRLF +
"echo (\"<br>\n\");" + CRLF +
"for ($a = 1; $a < 6; $a++)" + CRLF +
"{" + CRLF +
"echo (\"Ciao per la $a volta <br>\n\");" + CRLF +
"}" + CRLF +
"exit;" + CRLF +
"?>" + CRLF
);
PHPCode->Write(s.c_str(), s.Length());
// calls & checks PHP html generator
if(::RunPHP(PHPCode, HTMLCode) == true)
{
HTMLCode->SaveToFile("C:\\index.html");
WebBrowser->Navigate((WideString) "C:\\index.html");
}
// frees objects
delete PHPCode;
delete HTMLCode;
}
--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]