Using a config file and performing actions using entries

I'm making this program to start my windows services, the way I want to start them, not windows... Anyway, I want it to read a config file, like an INI, or something, and then parse the values into a string or array, then stick it in a system call... Also, is there something I can do besides a system call?

Basically the conf file will be like a list, with service names, and other values, like delay times, etc.

But I don't know where to start on that... I need a few tips to point me in the right direction, then I should be able to handle it from there.

Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
 * startservices.cpp
 *
 *  Created: Jun 18, 2010
 *      Author: Ryan Caywood
 */

#include <conio.h>
#include <prockill.cpp>
#include <iostream>
#include <windows.h>
using namespace std;

string choice,
done = "Done",
error = "The service is already started.",
startMsg1 = "\nStarting Shell Hardware Detection ... ",
startMsg2 = "\nStarting Removable Storage ... ",
startMsg3 = "\nStarting Themes ... ",
startMsg4 = "\nStarting MSI Server ... ",
startMsg5 = "\nStarting Network Connection Manager ... ",
startMsg6 = "\nStarting TimeSync ... ",
startMsg7 = "\nStarting UPnP Discovery ... ",
startMsg8 = "\nStarting UPnP Host ... ",
startMsg9 = "\nStarting Wireless Auto-Config ... ";

char
s1[50] = "net start ShellHWDetection >nul 2>nul",
s2[50] = "net start NtmsSvc >nul 2>nul",
s3[50] = "net start Themes >nul 2>nul",
s4[50] = "net start MSIServer >nul 2>nul",
s5[50] = "net start Netman >nul 2>nul",
s6[50] = "net start W32Time >nul 2>nul",
s7[50] = "net start SSDPSRV >nul 2>nul",
s8[50] = "net start upnphost >nul 2>nul",
s9[50] = "net start WZCSVC >nul 2>nul";

int main() {
	cout << "Starting services and programs ...\n";
	cout << startMsg1;
	if (system(s1) == 2) { cerr << error; }
	else {  cout << done;  }
	cout << startMsg2;
	if (system(s2) == 2) { cerr << error; }
	else {  cout << done;  }
	cout << startMsg3;
	if (system(s3) == 2) { cerr << error; }
	else {  cout << done;  }
	cout << startMsg4;
	if (system(s4) == 2) { cerr << error; }
	else {  cout << done;  }
	cout << startMsg5;
	if (system(s5) == 2) { cerr << error; }
	else {  cout << done;  }
	cout << startMsg6;
	if (system(s6) == 2) { cerr << error; }
	else {  cout << done;  }
	cout << startMsg7;
	if (system(s7) == 2) { cerr << error; }
	else {  cout << done;  }
	cout << startMsg8;
	if (system(s8) == 2) { cerr << error; }
	else {  cout << done;  }
	cout << startMsg9;
	if (system(s9) == 2) { cerr << error; }
	else {  cout << done;  }
	cout << "\nStarting PowerStrip ... ";
	if (ShellExecute(GetDesktopWindow(), "open",
			"C:\\Program Files\\PowerStrip\\PStrip.exe", NULL, NULL,
			SW_SHOWNORMAL)) cout << "Started.";
	else cerr<< "Error";
	cout << "\nKilling PowerStrip in 10 seconds (or less) ... ";
	Sleep(10000);
	if (KILL_PROC_BY_NAME("PStrip.exe") == 0) cout << "Killed";
	else cerr << "Error";
	cout << endl << "\nAll done!" << endl;
	Sleep(2000);
	return 0;
}
Windows has specific function calls to read ini files. Google for GetPrivateProfileString.
Why are you using char arrays as well as strings? Why not just use strings?
@RedX
Thanks, that's what I was looking for.

@Galik
I would perfer to use only strings, but I can't get system() to use them... it only accepts c-style strings, and I got nowhere trying to convert strings to char arrays then stick the converted array into the system call... I could have got it working but I would have had probably 30 or so more lines of code to deal with. I guess what I mean, is it was 'clunky'.

Although, the entire thing is clunky right now...

It'd be great if there was something besides system() I could use that would accept strings as args, or a decent way to get strings into system(). If there is a better alternative to system(), I'll go with it regardless of char or string args though.
Last edited on
RyanCaywood wrote:
I would perfer to use only strings, but I can't get system() to use them... it only accepts c-style strings, and I got nowhere trying to convert strings to char arrays then stick the converted array into the system call... I could have got it working but I would have had probably 30 or so more lines of code to deal with.


1
2
3
std::string strS1 = "net start ShellHWDetection >nul 2>nul";
[...]
system(strS1.c_str());
tadaaa...
What the.... That was the first thing I tried!

It works though... Thank you.

Maybe I didn't remember to type the () in strS1.c_str() or something... I dunno.

I should have wondered why it didn't work... Lol.

Thanks!
Topic archived. No new replies allowed.