Microsoft KB Archive/194948

= How To Create a Child Console Process from a CGI Process =

Article ID: 194948

Article Last Modified on 6/23/2005

-

APPLIES TO


 * Microsoft Internet Information Server 3.0
 * Microsoft Internet Information Server 4.0

-



This article was previously published under Q194948



We strongly recommend that all users upgrade to Microsoft Internet Information Services (IIS) version 6.0 running on Microsoft Windows Server 2003. IIS 6.0 significantly increases Web infrastructure security. For more information about IIS security-related topics, visit the following Microsoft Web site:

http://www.microsoft.com/technet/security/prodtech/IIS.mspx



SUMMARY
When using the C-runtime routine _popen to create a child process from within a CGI process, there are several issues regarding the mapping of STDIN and STDOUT (also STDERR) handles. By default Internet Information Server (IIS) 3.0 and 4.0 will not create a console when a CGI process is created. The STDIN and STDOUT of the CGI process and that of the child process (created from the CGI process) will not be mapped correctly.



MORE INFORMATION
When you have code such as below:

/* Parent.c -- This is the CGI process created by IIS */

int main(int argc, char *argv[]) {     FILE  *pFile = _popen("c:\\inetput\\scripts\\child.exe", "rt"); char cBuf[256];

printf("HTTP/1.0 200 Ok\r\nContent-Type: test/html\r\n\r\n"); printf("I'm the Parent...");

while(!feof(pFile)) {        fgets(cBuf, 256, pFile); printf("%s", cBuf); }     _pclose (pFile); return 0; }

/* Child.c -- This is the child process created by Parent */

int main(int argc, char *argv[]) {     printf("Hello, I'm the child."); return 0; }

The browser will not receive the message from the child process. This is because at the time that _popen is called, the parent process does not have a console. Therefore, the STDIN, STDOUT, and STDERR are invalid (or more precisely are pseudo standard handles provided to the parent process by IIS). Therefore, the FILE pointer is created with STDIN/STDOUT, which do not map the child process.

There are two ways to work around this default behavior.


 * Force IIS to launch all CGI application with their own console. Under IIS 3.0 this is done through the CreateProcessWithNewConsole DWORD registry entry under HKLM\CurrentControlSet\Services\W3SVC\Parameters to 1. Under IIS 4.0, this is done by setting the MD_CREATE_PROC_NEW_CONSOLE entry in the metabase with IIS Admin Base Objects or corresponding CreateCGIWithNewConsole property with IIS Admin Objects to TRUE (see IIS 4.0 online documentation).
 * If altering the configuration of IIS is not an option, you can programmatically guarantee that the parent process will have a console by issuing the AllocConsole call. This call will fail if the parent already has a console. But if the console does not exist, it will allocate one and attach the parent process to this console. NOTE: AllocConsole call must be issued before the _popen call is made; Otherwise, the resulting allocated console's standard I/O handles will not have been picked up by the _popen routine.

