muntasir2000
muntasir2000

Reputation: 204

Writing a small C program to shutdown windows in entered time

I want to write a program to shut down windows in N seconds. The easiest method I know to shutdown windows is to call system() with

shutdown -s -t XXXX

where XXXX is the given time. However system() only accepts string as a parameter. How can I call system("shutdown -s -t 7200") where 7200 is inputed by the user?

Upvotes: 3

Views: 5720

Answers (2)

Jerry Coffin
Jerry Coffin

Reputation: 490338

I'd use InitiateSystemShutdown instead. You could use ExitWindows or ExitWindowsEx, but neither of those directly supports the delay being asked about in the original question, so you'd have to add code to do that delaying (e.g., using SetTimer). That's certainly possible, but incurs extra work without accomplishing anything extra in return.

If you insist on using system, you can use sprintf (or something similar) to create the string you pass to system:

char buffer[256];

sprintf(buffer, "shutdown -s -t %d", seconds);
system(buffer);

Upvotes: 1

Alex Reynolds
Alex Reynolds

Reputation: 96966

Take a look at scanf() and sprintf(), e.g.:

#define MAX_LENGTH 50
/* ... */
int shutdownTime;
char shutdownCall[MAX_LENGTH];

scanf("%d", &shutdownTime);
if (shutdownTime < 0) 
    return NEGATIVE_TIME_ERROR;
sprintf(shutdownCall, "shutdown -s -t %d", shutdownTime);
system(shutdownCall);

Upvotes: 1

Related Questions