Shutdown program

Hello
This is my first program. It works by using the windows command:
shutdown -s -t *variable in seconds

What would you do different and why?
Tiny improvements I can do?
And what does it matter if i have [100] or [50] after char buffer?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

using namespace std;

int main()
{
	char buffer [100];
	const char *shutdown = "shutdown -s -t";
	int a;
	cout << "Shutdown PC" << endl;
	cout << endl;
	cout << "Minutes: ";
	cin >> a;
	cout << endl;
	int b = a*60;
	cout << "Shutting down in: " << a << " Minutes" << endl;
	cout << "Seconds: " << b << endl;
	snprintf(buffer, sizeof(buffer), "%s %d", shutdown, b);
	system(buffer);
	return 0;
}
closed account (18hRX9L8)
ehe wrote:
What would you do different and why?

Since this is a Windows program, instead of using system(), I would suggest using ExitWindowsEx from windows.h. See https://msdn.microsoft.com/en-us/library/windows/desktop/aa376868(v=vs.85).aspx .

Some reasons as to why not to use system(): http://www.cplusplus.com/forum/articles/11153/ .

ehe wrote:
And what does it matter if i have [100] or [50] after char buffer?
char buffer [100]; means that buffer can safely hold 100 chars while char buffer [50]; means that buffer can only safely hold 50 chars. It matters because you may have buffer overflow if you do not specify a buffer large enough, which can ruin program functionality/output silently. So why not specify a fail-safe buffer length like 10000000...? Because then your program will take up the unneeded memory of 10000000... chars or whatever type you specified. It is important to manage your memory, especially in environments where available memory amounts are small like those of embedded/peripheral devices.
Last edited on
I'll try this other method you mention.
Got some reading to do.
Great and thorough description of the char buffer [*].
Thank you very much.
I'll probably post my work here once it's done for others interested.
There's actually an MSDN example showing how to use ShutdownWindowsEx

How to Shut Down the System
https://msdn.microsoft.com/en-us/library/windows/desktop/aa376871%28v=vs.85%29.aspx

You'll see it takes a bit more work than making the function call itself; you also have to acquire shutdown privilege. (This is mentioned right at the end of the Remarks section in the MSDN entry for ExitWindowsEx.)

Andy

PS Regarding

And what does it matter if i have [100] or [50] after char buffer?

There is a constant MAX_PATH defined somewhere in the Windows headers, which has a value of 260. This is the longest file path supported by most Windows API functions (there are exceptions.)

See Maximum Path Length Limitation section here for further info:

Naming Files, Paths, and Namespaces
https://msdn.microsoft.com/en-gb/library/windows/desktop/aa365247%28v=vs.85%29.aspx
Last edited on
What would you do different and why?

Check for errors. Otherwise a typo could cause the system to shutdown instantly.
Topic archived. No new replies allowed.