Click to See Complete Forum and Search --> : CreateProcess for perl scripts ?


thooomy
June 8th, 2005, 04:08 AM
Hi there Gurus !


I want to execute perl scripts from within my application an redirect the output into it (and write to their stdin) by using anonymous pipes

If found a part of a sourcecode in the web and modifed it:


static char pBuffer[STD_BUFF_SIZE];
memset(pBuffer,0,STD_BUFF_SIZE);
bool WinNt = false;

STARTUPINFO si;
SECURITY_ATTRIBUTES sa;
SECURITY_DESCRIPTOR sd; //security information for pipes
PROCESS_INFORMATION pi;
HANDLE newstdin,newstdout,read_stdout,write_stdin; //pipe handles

//check if we're running NT
OSVERSIONINFO osv;
osv.dwOSVersionInfoSize = sizeof(osv);
GetVersionEx(&osv);
WinNt = (osv.dwPlatformId == VER_PLATFORM_WIN32_NT);

if (WinNt) { //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
{
//ErrorMessage("CreatePipe");
//getch();
return("");
}
if (!CreatePipe(&read_stdout,&newstdout,&sa,0)) //create stdout pipe
{
//ErrorMessage("CreatePipe");
//getch();
CloseHandle(newstdin);
CloseHandle(write_stdin);
return("");
}

GetStartupInfo(&si); //set startupinfo for the spawned process

si.dwFlags = STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
si.hStdOutput = newstdout;
si.hStdError = newstdout; //set the new handles for the child process
si.hStdInput = newstdin;

//spawn the child process <<<--- THIS IS THE PROBLEM !

if (!CreateProcess( sExecuteCommandA, NULL, , NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi))
{
CloseHandle(newstdin);
CloseHandle(newstdout);
CloseHandle(read_stdout);
CloseHandle(write_stdin);
return("");
}
(...)


my Problem is that CreateProcess always fails.
in sExecuteCommandA is a path and a name of a perl script (for example "C:\\Folder\\guestbook.pl"). A valid perl interpreter is runnig on my system...
Is is possible to execute those scripts with this call ?

thank you in advance !

j0nas
June 8th, 2005, 04:21 AM
You need to call perl.exe with the argument to your script (C:\Folder\guestbook.pl"). Check the CreateProcess help for more info on how to pass program arguments.

Andreas Masur
June 8th, 2005, 04:22 AM
You most likely need to provide the path to the application...the perl interpreter. 'CreateProcess()' does not do any lookups...opposed to 'ShellExecute()' for example...

thooomy
June 8th, 2005, 05:32 AM
okay thank you ! your answers pointed me in the right direction...

mrkotni
June 13th, 2005, 08:55 AM
Hi,

I am novice to MSDN programming.

In the code snippet shown for this question, pipes were created but not duplicated. Can you please throw some light how to read the out put of perl script execution using the code snippet shown?

Thanks
Mohan