Knowledge Base

How To Create a Child Console Process from a CGI Process

Article ID: 194948

Article Last Modified on 6/23/2005


APPLIES TO


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:

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("<H1>I'm the Parent...</H1>");

      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("<H1>Hello, I'm the child.</H1>");
      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.

REFERENCES

IIS 3.0 Product Documentation

IIS 4.0 On Line Documentation

Keywords: kbhowto kbwebserver kbcgi KB194948