Making system configuration changes with C++

Started by
9 comments, last by v0dKA 17 years, 8 months ago
I have been learning C++ for a small bit of time now, but it has been in the form of writing independant programs that all it's instructions are referenced to the program itself. What I was wanting to ask in this forum, was where I can find the classes or other info on how to tap into the OS (windows xp and 2000 in this case) and run other programs, add and manipulate pre-existing registry keys, manipulate files and directories and stuff of the sort. Manipulating files and directories probably won't be hard to find out as I haven't looked into that. The big question is how to use C++ to run programs just as if I was to run them from the run command with any additional switches. I would assume I would probably create console program as I won't need any user interaction. Any suggestions would be greatly appreciated.
Advertisement
I would check out the std::system() and see if this is what your looking for.


Hope this helps :)
Adamhttp://www.allgamedevelopment.com
In answer to your "big question", you are correct in thinking you need to write console programs. Thankfully these are the easiest C++ programs to write. In terms of passing switches to your program:

#include <iostream>int main(int,const char **av){    while(*av)        {        std::cout << *av++ << std::endl;        }    return 0; // current debate about whether this line is good form or not :)}


will create a console app that, if run from the command line with switches, will print it's own executable path (the first command line arg) then any switches that you pass to it.

In terms of manipulating the registry, I believe you will need to look into the Win32 API functions for this but I know nothing about them. Try MSDN.

One thing to bear in mind for clarity is that if you write a console application with a Win32 compiler, you can still include any <windows.h> etc files and use the Win32 API (within reason - you can't use GDI obviously).

HTH
If you want your programs to interact with windows, then you will need Platform SDK (you will find some information about setting it up with VC++ 2005 Express Edition here). Also, a nice link is MSDN - you will find all the documentation to all windows functions, libraries and programs.

If you want to run other programs from inside your app - std::system will probably work for you, but i'd suggest you to look at CreateProcess or ShellExecute function at MSDN.
Thanks all for the tips.
To through some additional information, I have already setup the SDK for VC2005 so I should be good to go in that department.

I basically want to run my console app, and when I do, it will execute another program, as well apply any switches that I need. In addition, I need my application to wait until the child app that my application initializes is finished running. As and example, the WMI wscript function RUN in VBScript takes a parameter that tells it to wait until execution of the child program is finished before carrying out any further lines of code.

I will look into the items you all have already mentioned. Thanks again.
Misunderstood you slightly. If you get yourself linked to the Win32 API okay, you will want to have a look at ShellExecute in order to run another program. The second and third parameters allow you to pass the name of the program and any switches respectivley.

HTH
Quote:Original post by EasilyConfused
If you get yourself linked to the Win32 API okay, you will want to have a look at ShellExecute in order to run another program.


Actually, if you want to run another program, the real function you want is CreateProcess, or one of the spawn family.

ShellExecute is more useful to open/print documents using whatever program has been registered to handle them.

To the OP: you're looking for a book like this one
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
"Running other programs" is normally better done in a scripting language (e.g. Windows batch scripting, although it's a horrible scripting language - any .*sh on Linux is much prettier - or Python via the 'os' module).
yes, thats what I have found having scripted all my programs for the last year. However, in my position, i can use whatever language or script that i want to get the programming done.

So, I am taking the advantage of using C++ as a means to learn it.
Well, one simple way to run a program in a console application:

std::system("start /wait someprogram.exe");

This will start whatever program in a new window, and the original program will wait for the new program to finish.

This is the simplest way I know of. It's also somewhat crude though.

This topic is closed to new replies.

Advertisement