flat assembler
Message board for the users of flat assembler.

Index > Heap > [C++] Variables in system calls? How?

Author
Thread Post new topic Reply to topic
DataHunter2009



Joined: 10 Jun 2005
Posts: 144
DataHunter2009
I'm learning C++ and it's really cool. So i decided to write a simple command line program to help me learn different aspects of the language. Once that's done, I plan to move into Windows programming with it.

I've already finished one part, it asks if you wish to preform a quick format and then you can type y or n and it formats a floppy.

But now, I want to make a function that adds a path to the system PATH variable. I know the system call I have to make, but I can't seem to figure out how to put a variable in it.... here's my code:

Code:
    if (choice == 1) {
        string path;
        system("CLS");
        cout << "Please type the full path that you wish to add to the PATH variable:\n\n";
        cin >> path;
        cout << "\n\n";
        system("echo" + path + ";%PATH%");
        system("PAUSE");
        system("CLS");
        showmenu();
    }    


I already have the showmenu function defined and everything, but the problem lies in the system("echo" + path + ";%PATH%");. Here's the error I get in Dev-C++:

Quote:
20 C:\Dev-Cpp\EasyDosToolkit\functions.h cannot convert `std::basic_string<char, std::char_traits<char>, std::allocator<char> >' to `const char*' for argument `1' to `int system(const char*)'


Hopefully someone can help me out. Very Happy

P.S. Yes, I know I used the wrong system call for adding a path, but i wanted to test it first before I actually did any of the path editing.
Post 13 Aug 2005, 15:35
View user's profile Send private message Reply with quote
flaith



Joined: 07 Feb 2005
Posts: 122
Location: $300:20 58 FC 60 N 300G => Vietnam
flaith
you can use the strcat function:

Code:
char *cmdLine = malloc(255);

strcpy(cmdLine,"echo ");
strcat(cmdLine,path);
strcat(cmdLine,";%PATH%");

system(cmdLine);
    


Smile

_________________
Je suis sur de 'rien', mais je ne suis pas sur du 'tout'.
Post 13 Aug 2005, 17:36
View user's profile Send private message Visit poster's website Reply with quote
DataHunter2009



Joined: 10 Jun 2005
Posts: 144
DataHunter2009
That looks like it would work... and to me, it makes sense, but when I compile it, I get this:

Quote:
functions.h:20: error: invalid conversion from `void*' to `char*'
functions.h:23: error: no matching function for call to `strcat(char*&, std::string&)'


Sad
Post 13 Aug 2005, 21:14
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
Did you included strings.h?
Post 13 Aug 2005, 21:30
View user's profile Send private message Reply with quote
DataHunter2009



Joined: 10 Jun 2005
Posts: 144
DataHunter2009
The tutorial I read told me to use just "#include <strings>"
Post 13 Aug 2005, 21:56
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
That's OK in C++ but strcat is a C function and needs "#include <strings.h>" instead.

[edit]Note that std::string (C++ class for strings) is not a "char *" (which is only a char pointer)[/edit]
Post 13 Aug 2005, 22:15
View user's profile Send private message Reply with quote
DataHunter2009



Joined: 10 Jun 2005
Posts: 144
DataHunter2009
I changed it to string.h, but that didn't help. Sad
Post 13 Aug 2005, 23:34
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
Sorry, also change
Code:
char *cmdLine = malloc(255);     

with this
Code:
char *cmdLine = (char *)malloc(255);    


"path" is "string" or "char *"? Remember, srtcat only supports "char *"

[edit] Change "system("echo" + path + ";%PATH%");" of your original code to "system(("echo " + path + ";%PATH%").c_str());" and tell me if it works[/edit]
Post 13 Aug 2005, 23:43
View user's profile Send private message Reply with quote
DataHunter2009



Joined: 10 Jun 2005
Posts: 144
DataHunter2009
Heh... that last part worked. Very Happy (The edit)

Thanks! Very Happy

EDIT: I've got another problem now... my system call to "set PATH=" doesn't work. Sad When i type a path to be added, it doesn't add it to the environment variable... here's what I am using now:

Code:
system(("set PATH=" + path + ";%PATH%").c_str());    
Post 14 Aug 2005, 00:41
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
Try opening a console first and then call your program from that console. Note the same happen when you type "set path=foo" and then "exit", when you open a new console the path doesn't contain "foo".

[edit]Opening a console first doesn't work Sad. I guess "system" executes the command you give in a separated enviroment. Anyway there is an API to change enviroment variables but I don't remember how to do Sad[/edit]
Post 14 Aug 2005, 01:08
View user's profile Send private message Reply with quote
flaith



Joined: 07 Feb 2005
Posts: 122
Location: $300:20 58 FC 60 N 300G => Vietnam
flaith
locodelassembly wrote:
Opening a console first doesn't work Sad. I guess "system" executes the command you give in a separated enviroment. Anyway there is an API to change enviroment variables but I don't remember how to do Sad


Must use GetEnvironmentVariable and SetEnvironmentVariable : check MSDN here http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/getenvironmentvariable.asp

_________________
Je suis sur de 'rien', mais je ne suis pas sur du 'tout'.
Post 14 Aug 2005, 02:08
View user's profile Send private message Visit poster's website Reply with quote
DataHunter2009



Joined: 10 Jun 2005
Posts: 144
DataHunter2009
Thanks! That document was real helpful! Very Happy
Post 14 Aug 2005, 02:27
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar.

Powered by rwasa.