Jump to content

How can I add %ProgramFiles% to a system() call?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
21 replies to this topic

#1
Panarchy

Panarchy

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 258 posts
Hello

How can I add %ProgramFiles% to a system() call?

Here is my current code (using Dev-C++);

#include <cstdlib>

#include <iostream>


using namespace std;


int main(int argc, char *argv[])

{

    system("diskmgmt.msc");

    system(""%ProgramFiles%"\\Symantec\\LiveUpdate\\LUALL.exe");

    system("PAUSE");

    return EXIT_SUCCESS;

}


Please tell me how to get this line: system(""%ProgramFiles%"\\Symantec\\LiveUpdate\\LUALL.exe"); to work.

Thanks in advance,

Panarchy

PS: Once I've gotten this to work, I'd also like to know how to stop the command prompt window from appearing.

#2
brownhead

brownhead

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts
char* pFiles = getenv("ProgramFiles");
pFiles now contains the value of the environmental variable ProgramFiles. Joining the two strings shouldn't be much of a problem for you.

#3
Panarchy

Panarchy

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 258 posts
Thanks for your reply. I think I did as you asked... here is a screenshot of my code and the result;

Posted Image

Please tell me what I need to do in order to get it to work.\

Thanks in advance,

Panarchy

#4
brownhead

brownhead

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts
Erm, it appears you don't need getenv. System() is evaluating the variable for you, you just didn't enclose the command in parenthesis.
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    system("\"%ProgramFiles%\\Symantec\\LiveUpdate\\LUALL.exe\"");
    system("PAUSE");
    return EXIT_SUCCESS;
}
Please try that code and tell me the result.

What I did was encase the entire command in quotations to tell the system that everything inside the quotes is a single path. The above code will send the system
"C:\Program Files\Symantec\LiveUpdate\LUALL.exe"
Notice the quotations. Before you were sending
C:\Program Files\Symantec\LiveUpdate\LUALL.exe
Notice the space after program. Thus the system thought that C:\Program was the command you wanted to execute and Files\Symantec\LiveUpdate\LUALL.exe was the arguments you wanted to pass to the command.

Hope this helped you!

#5
Panarchy

Panarchy

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 258 posts
Hi

Thanks once again for replying.

However, I tried the code, exactly as you have done, yet still got an error. Here is a screenshot;

Posted Image

Please tell me (once more) how to get this to work.

Thanks in advance,

Panarchy

#6
brownhead

brownhead

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts
Alright, I'm not really sure why this works, but using double quotes solved the problem, anyone feel free to step in here and explain why this works.
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    system("\"\"%ProgramFiles%\\Symantec\\LiveUpdate\\LUALL.exe\"\"");
    system("PAUSE");
    return EXIT_SUCCESS;
}
Tested it myself on my computer so I know it works.

#7
Panarchy

Panarchy

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 258 posts
Thanks

Perfect

We finally worked it out!!!

EDIT: One last question, do you know anyway I can stop the command prompt from appearing?

#8
carly

carly

    Learning Programmer

  • Members
  • PipPipPip
  • 34 posts
No, it doesn't work.
system() must never be called in Windows (repeated on usenet for 20 years)
Use SE or CP instead.

#9
shibbythestoner

shibbythestoner

    Programmer

  • Members
  • PipPipPipPip
  • 135 posts

carly said:

No, it doesn't work.
system() must never be called in Windows (repeated on usenet for 20 years)
Use SE or CP instead.
Tell that to my lecturer :(
Although it's painfully intricate/difficult/annoying to get the console to clear without system("cls")
Posted Image

#10
Panarchy

Panarchy

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 258 posts
Hi ShibbyTheStoner,

Is this what you're looking for;

ShellExecute(NULL, TEXT("open"), TEXT("cmd"), TEXT("cls"), NULL, SW_HIDE);

#11
Johhny

Johhny

    Newbie

  • Members
  • PipPip
  • 19 posts
nice, but why not just use "C:\Program Files\something" ?

#12
Panarchy

Panarchy

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 258 posts
Because "Program Files" might be (is) mapped to a different directory. Which is the advantage of environmental variables. Environmental variables exist for specifically this reason.

ALWAYS use Environmental Variables (wherever possible) in your programs & scripts.