title: What should COMSPEC and PATH environmental variables be to locate the command-interpreter, using system()?
body: <p>Are the OS (XP) environmental variables the same used in a process running from visual studio .NET C++?  </p>
  
  <p>It seems the command interpreter is not found:<br>
  When using NULL as the command, <a href="http://msdn.microsoft.com/en-us/library/277bwbdz%28VS.80%29.aspx" rel="nofollow noreferrer">system() returns 0</a>  and with command - <code>ENOENT Command interpreter cannot be found</code>.</p>
  
  <p>In windows (System->Environmental Variables), COMSPEC contains the path to cmd.exe<br>
  PATH does not.   </p>
  
  <p>What should PATH be?</p>
  
  <p>Other than this, not sure why it can not find the interpreter.</p>
  
  <p>Any suggestions are appreciated. Thanks.</p>
  
  <pre><code>  if( system("tail -500 log.txt") == -1)
    {
        //Error calling tail.exe on log 
        //errno is a system macro that expands int returning
        //the last error. strerror() converts the error to it's
        //corresponding error message. 
        printf("Error calling tail.exe with system(): %s",strerror( errno ));
  
    }
  </code></pre>
  
  <p><strong>EDIT1</strong><br>
  Stepping into system() <code>argv[0] = _tgetenv(_T("COMSPEC"));</code>returns a bad pointer. Being this is a cgi executable, the COMPSEC is not properly set or inherited from the OS.<br>
  I now set COMSPEC before the process is started and use CreateProcess() as in <a href="http://msdn.microsoft.com/en-us/library/ms682009%28VS.85%29.aspx" rel="nofollow noreferrer">example 2</a>  </p>
  
  <p>However, create process still returning 0? Getting closer. See any issues with this? Thanks.</p>
  
  <pre><code> if (! SetEnvironmentVariable("COMSPEC", "C:\\WINDOWS\\system32\\cmd.exe") )
   {
      printf("SetEnvironmentVariable failed (%d)\n", GetLastError());         
   }
  
   //r = system("dir c:\\");
   r = CreateProcess("dir.exe", NULL, NULL, NULL, TRUE, NULL, 
      NULL,     // inherit parent's environment 
      NULL, &amp;si, &amp;pi);
  </code></pre>
  
  <p><strong>EDIT 2</strong><br>
  SetEnvironmentVariable() did not work. However, putenv does.<br>
     <code>_putenv( "COMSPEC=C:\\WINDOWS\\system32\\cmd.exe" ); // C4996</code><br>
  Not sure what the difference is...?<br>
  Now that this env var is set, any request on the cgi app from the browser gives the option to save the cgi.exe instead of executing it.. Not sure why this has changed  based on this env var?</p>