Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 622845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:01:42+00:00 2026-05-13T19:01:42+00:00

Are the OS (XP) environmental variables the same used in a process running from

  • 0

Are the OS (XP) environmental variables the same used in a process running from visual studio .NET C++?

It seems the command interpreter is not found:
When using NULL as the command, system() returns 0 and with command – ENOENT Command interpreter cannot be found.

In windows (System->Environmental Variables), COMSPEC contains the path to cmd.exe
PATH does not.

What should PATH be?

Other than this, not sure why it can not find the interpreter.

Any suggestions are appreciated. Thanks.

  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 ));

  }

EDIT1
Stepping into system() argv[0] = _tgetenv(_T("COMSPEC"));returns a bad pointer. Being this is a cgi executable, the COMPSEC is not properly set or inherited from the OS.
I now set COMSPEC before the process is started and use CreateProcess() as in example 2

However, create process still returning 0? Getting closer. See any issues with this? Thanks.

 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, &si, &pi);

EDIT 2
SetEnvironmentVariable() did not work. However, putenv does.
_putenv( "COMSPEC=C:\\WINDOWS\\system32\\cmd.exe" ); // C4996
Not sure what the difference is…?
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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T19:01:42+00:00Added an answer on May 13, 2026 at 7:01 pm

    The environment variables are inherited when running a process, including system(...) call. Unless there is something weird going on, usually running %windir%\system32\cmd.exe should do the trick, it should expand the environment variable, unless you can use the API to get the windows directory ‘GetWindowsDirectory‘. See here for an example from the MSDN.

    Edit: IIRC, COMSPEC environment variable, if done on the command line

    > echo %COMSPEC%
    C:\WINDOWS\system32\cmd.exe
    

    You got a bad pointer, because it is not probably set up, the above echo command should prove that, if you get no output, it is not set, right click on ‘My Computer’, left-click on ‘Properties’, a dialog with tab-pages appear, click on ‘Advanced’, look for ‘Environment Variables’…see the two screenshots here…

    Also I should point out that you are setting the environment variable temporarily, hence it will not see the ‘COMSPEC’….it is not permanent, the only permanent way to do it is follow the screenshots…

    I am trying to get the screenshots in place….

    alt text

    alt text

    Edit#2:
    Just to point out this, when you set the Environment variable here, that is temporary – not permanent!

    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, &si, &pi);
    

    When using the call CreateProcess, it is bound to fail, look at the comment "inherit parent’s environment", that cannot happen as the environment was set up temporarily. Have you tested the simple echo command here. Something is wrong as to why the COMSPEC variable is not set..after setting it permanently – you will need to reboot the machine for it to work. Then the echo command above should show the value for that environment variable, and in turn, this

    argv[0] = strdup(_tgetenv(_T("COMSPEC")));
    

    should return a proper pointer…by the way, I think that should be strdup‘d also…

    Edit#3: Whoops I noticed when I had ‘&pi’ used, it came up as a pi symbol instead!…duh, that’s amended now…also I have amended this ‘argv’ code here:

    argv[0] = _tcsdup(_tgetenv(_T("COMSPEC")));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.