Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Running (invoking) a windows cmd command within C code

2 views
Skip to first unread message

thomas...@gmail.com

unread,
Jul 8, 2008, 3:29:44 PM7/8/08
to
Hello all,


I searched both the web and a couple usenet groups before posting this
here.

I am not proficient in C, I am just fooling around. I need to find a
way to invoke a windows cmd command, namely shutdown, from within a C
program. I could only find stuff regarding C# or other languages..

In case you 're wondering, I frequently used to leave internet radio
on before going to sleep, setting my old XP PC to shutdown usually
after half an hour.. The command was something like shutdown /s /t
1800 (in sec).

Now I got a vista PC, and bloody microsoft decided to limit the
allowed time to 10 mins, or 600 seconds. Why the hell they bothered to
change that of all things is very weird, but anyway, I was thinking of
writing a simple C program that would take input from the user, for
example x seconds, 'sleep' for x-600 seconds, and then invoke
'shutdown /s /t 600'.

It will make my day, so any help is appreciated..
--
comp.lang.c.moderated - moderation address: cl...@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.

Barry Schwarz

unread,
Jul 10, 2008, 2:01:58 PM7/10/08
to
On Tue, 8 Jul 2008 14:29:44 -0500 (CDT), thomas...@gmail.com wrote:

>Hello all,
>
>
>I searched both the web and a couple usenet groups before posting this
>here.
>
>I am not proficient in C, I am just fooling around. I need to find a
>way to invoke a windows cmd command, namely shutdown, from within a C
>program. I could only find stuff regarding C# or other languages..
>
>In case you 're wondering, I frequently used to leave internet radio
>on before going to sleep, setting my old XP PC to shutdown usually
>after half an hour.. The command was something like shutdown /s /t
>1800 (in sec).
>
>Now I got a vista PC, and bloody microsoft decided to limit the
>allowed time to 10 mins, or 600 seconds. Why the hell they bothered to
>change that of all things is very weird, but anyway, I was thinking of
>writing a simple C program that would take input from the user, for
>example x seconds, 'sleep' for x-600 seconds, and then invoke
>'shutdown /s /t 600'.
>
>It will make my day, so any help is appreciated..

Look up the system() function in your C reference.


Remove del for email

Jack Klein

unread,
Jul 10, 2008, 2:02:01 PM7/10/08
to
On Tue, 8 Jul 2008 14:29:44 -0500 (CDT), thomas...@gmail.com wrote
in comp.lang.c.moderated:

> Hello all,
>
>
> I searched both the web and a couple usenet groups before posting this
> here.
>
> I am not proficient in C, I am just fooling around. I need to find a
> way to invoke a windows cmd command, namely shutdown, from within a C
> program. I could only find stuff regarding C# or other languages..
>
> In case you 're wondering, I frequently used to leave internet radio
> on before going to sleep, setting my old XP PC to shutdown usually
> after half an hour.. The command was something like shutdown /s /t
> 1800 (in sec).
>
> Now I got a vista PC, and bloody microsoft decided to limit the
> allowed time to 10 mins, or 600 seconds. Why the hell they bothered to
> change that of all things is very weird, but anyway, I was thinking of
> writing a simple C program that would take input from the user, for
> example x seconds, 'sleep' for x-600 seconds, and then invoke
> 'shutdown /s /t 600'.
>
> It will make my day, so any help is appreciated..

The part about sleeping is not provided by standard C, and requires a
Windows API call. You need to ask about that in a Windows programming
group, news:comp.os.ms-windows.programmer.win32 is a good one. Or you
could try searching Microsoft's MSDN site.

As for passing a string to the command interpreter, there is a
standard C function for that.

#include <stdlib.h>

int main(void)
{
system("shutdown /s /t 600");
return 0;
}

....should generally do the same thing as typing the command on a
command line.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html

Francis Glassborow

unread,
Jul 10, 2008, 2:02:20 PM7/10/08
to
thomas...@gmail.com wrote:
> Hello all,
>
>
> I searched both the web and a couple usenet groups before posting this
> here.
>
> I am not proficient in C, I am just fooling around. I need to find a
> way to invoke a windows cmd command, namely shutdown, from within a C
> program. I could only find stuff regarding C# or other languages..
>
> In case you 're wondering, I frequently used to leave internet radio
> on before going to sleep, setting my old XP PC to shutdown usually
> after half an hour.. The command was something like shutdown /s /t
> 1800 (in sec).
>
> Now I got a vista PC, and bloody microsoft decided to limit the
> allowed time to 10 mins, or 600 seconds. Why the hell they bothered to
> change that of all things is very weird, but anyway, I was thinking of
> writing a simple C program that would take input from the user, for
> example x seconds, 'sleep' for x-600 seconds, and then invoke
> 'shutdown /s /t 600'.
>
> It will make my day, so any help is appreciated..

check out 'system()' I think that will meet your requirements.

cervello_in_vacanza

unread,
Jul 10, 2008, 2:02:23 PM7/10/08
to
thomas...@gmail.com wrote:

> Hello all,
>
>
> I searched both the web and a couple usenet groups before posting this
> here.
>
> I am not proficient in C, I am just fooling around. I need to find a
> way to invoke a windows cmd command, namely shutdown, from within a C
> program. I could only find stuff regarding C# or other languages..
>
> In case you 're wondering, I frequently used to leave internet radio
> on before going to sleep, setting my old XP PC to shutdown usually
> after half an hour.. The command was something like shutdown /s /t
> 1800 (in sec).
>
> Now I got a vista PC, and bloody microsoft decided to limit the
> allowed time to 10 mins, or 600 seconds. Why the hell they bothered to
> change that of all things is very weird, but anyway, I was thinking of
> writing a simple C program that would take input from the user, for
> example x seconds, 'sleep' for x-600 seconds, and then invoke
> 'shutdown /s /t 600'.
>
> It will make my day, so any help is appreciated..

In your program #include the <stdlib.h> header file and call any system
command from within the system() function:

int system(const char *command);

The value returned from system() is -1 on error or the return status
of the command otherwise.

the character string command is the name of the system command you whish
to call.

To make an example if you want to blank the screen of the console you
should call:

system("cls");

and if you want to shut down after 60 seconds your computer call:

system("shutdown /s /t 60");

As far as I know, that's the sole way to make an OS
call without being forced to implement any Assembly code.

remember well that doing so, you bound your program to the OS it
was implemented for, because "cmd" or "shutdwn" are MS Windows
commands.

regards,

cervello_in_vacanza

Jasen Betts

unread,
Jul 10, 2008, 2:02:46 PM7/10/08
to
On 2008-07-08, thomas...@gmail.com <thomas...@gmail.com> wrote:
> Hello all,
>
> I searched both the web and a couple usenet groups before posting this
> here.
>
> I am not proficient in C, I am just fooling around. I need to find a
> way to invoke a windows cmd command, namely shutdown, from within a C
> program. I could only find stuff regarding C# or other languages..

system("shutdown");

or ask in a windows programming newsgroup.

> Now I got a vista PC, and bloody microsoft decided to limit the
> allowed time to 10 mins, or 600 seconds. Why the hell they bothered to
> change that of all things is very weird, but anyway, I was thinking of
> writing a simple C program that would take input from the user, for
> example x seconds, 'sleep' for x-600 seconds, and then invoke
> 'shutdown /s /t 600'.

something like that could be done in batch, ping can be abused to
provide a timer....

Bye.
Jasen

Kenneth Brody

unread,
Jul 10, 2008, 2:02:59 PM7/10/08
to
thomas...@gmail.com wrote:
[...]

> In case you 're wondering, I frequently used to leave internet radio
> on before going to sleep, setting my old XP PC to shutdown usually
> after half an hour.. The command was something like shutdown /s /t
> 1800 (in sec).
[...]

Use the system() function:

int system(const char *string);

For example:

system("shutdown /s /t 1800");

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:ThisIsA...@gmail.com>

0 new messages