Sign in to follow this  

Making system configuration changes with C++

This topic is 4161 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
"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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Just to address some other things you mentioned:

Dealing with files is done using streams. You can use std::ifstream for reading from files, and std::ofstream for writing to files.

As for dealing with files and directories, you should have a look into the WIN32_FIND_DATA structure.

Also, you can have a look at this page for dealing with specific files, with functions that do things such as renaming and deleting. However, the functions I gave look like they're from the C runtime library. I'm not sure if there's a better way to do this in C++. I guess you could always use the shell (system( "del file.txt" );).

Good luck!

Share this post


Link to post
Share on other sites

This topic is 4161 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this