System() call - setting environment in a command prompt and then executing an exe

This is a discussion on System() call - setting environment in a command prompt and then executing an exe within the C Programming forums, part of the General Programming Boards category; I need to call an exe through C code but it requires some environment to be set before calling the ...

  1. #1
    Apprentice
    Join Date
    Oct 2006
    Location
    LA
    Posts
    53

    System() call - setting environment in a command prompt and then executing an exe

    I need to call an exe through C code but it requires some environment to be set before calling the exe.

    The environment is set using a bat file.

    Is there any way i can set the environment and then call the exe in the same command prompt using the system() call in C.

    Thanks.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There are several options (in order of difficulty):
    1. Create a batch file that combines the "set environment batch file" and the application you want to run.
    2. Create your own envirionment.
    3. Run cmd.exe and redirect stdin to a pipe created by your application, and send the relevant commands down the pipe.

    You can use "_putenv" in Windows to set a new environment, but it requires a bit of string management to introduce a new env variable.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    4. Create your own OS :P
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by QuantumPete View Post
    4. Create your own OS :P
    Yeah, and on the "difficulty scale", that would end up several orders of magnitude higher than the three I suggested.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Apprentice
    Join Date
    Oct 2006
    Location
    LA
    Posts
    53
    ^^lol

    I used the first option as the "difficulty scale" was minimum.

    Thanks matsp.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by matsp View Post
    There are several options (in order of difficulty):
    1. Create a batch file that combines the "set environment batch file" and the application you want to run.
    2. Create your own envirionment.
    3. Run cmd.exe and redirect stdin to a pipe created by your application, and send the relevant commands down the pipe.

    You can use "_putenv" in Windows to set a new environment, but it requires a bit of string management to introduce a new env variable.

    --
    Mats
    Couldn't you also pass all the commands to system() in one long string separated by \n's?
    Although #1 would definitely be the easiest.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by cpjust View Post
    Couldn't you also pass all the commands to system() in one long string separated by \n's?
    Although #1 would definitely be the easiest.
    I don't know if that works - I always thought it was "one command". You can of course use other methods to combine commands, e.g "a && b" will execute a and then b (but only if a is successfull).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21