how to make computer shutdown in C?which header do i have to include?

Recommended Answers

All 19 Replies

Member Avatar for iamthwee

You need the windows header.

int main()
{
    HANDLE hToken;
    TOKEN_PRIVILEGES tkp;
    
    if (OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
    {
        
        LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
        tkp.PrivilegeCount = 1;
        tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
        if(AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0))
        {
            ExitWindowsEx(EWX_SHUTDOWN|EWX_POWEROFF|EWX_FORCE,0);
        }
    }
}

I don't have a Unix system, but try this and tell me whether it works:

#include <stdlib.h>

int main( )
{
    system( "shutdown" ) ;
    return 0 ;
}

For references see here and here.

no actualy i want that for windows....

Member Avatar for iamthwee

Check my post again.

Is there anything written there, because I can't see anything at my side ?

lol....he was just pressing enter

So can you give me source?

You can try this. Haven't verified it but should work.

Just write the function, call it in main().

lol....he was just pressing enter

So can you give me source?

Don't you ever read the posts to your thread? :eek: I already gave you the win32 api function call for MS-Windows os. Just click the link I posted and you will get everything you need to know about that function. include <windows.h> to use it.

Member Avatar for iamthwee

Check my post again please.

Check my post again please.

It still looks blank in my browser window. Maybe someone needs to edit your post...

Member Avatar for iamthwee

Hint

You need to hold down the left mouse button over the blank parts then drag the mouse down.

commented: stop that silly madness -2

Heh..I knew that, just wanted to tell you that it is not a normal way of reading posts and many members will get confused...;)

BTW have fixed the code so that it can be read by each and every person.

Heh..I knew that, just wanted to tell you that it is not a normal way of reading posts and many members will get confused...;)

BTW have fixed the code so that it can be read by each and every person.

Appears we need to have Ms. Dani add a new category to the Rules thread -- do not make hidden posts. I don't know how he did it, but than you ~S.O.S~ for fixing it up. :)

I guess he used the [COLOUR=WHITE] tag.. I'm not sure why Daniweb even has this, since AFAIK, its not possible to change the background of posts (Probably a good thing...)

I imagine a few other colours on that chart would be pretty nasty to read aswell... Maybe that rule should be "No unreadable colours" :)

I guess he used the [COLOUR=WHITE] tag.. I'm not sure why Daniweb even has this, since AFAIK, its not possible to change the background of posts (Probably a good thing...)

Yeah, I think he used the color white for posting, but don't know why play this prank...:(

Appears we need to have Ms. Dani add a new category to the Rules thread -- do not make hidden posts. I don't know how he did it, but thank you ~S.O.S~ for fixing it up.

Yes it is a good idea to let Miss Dani know of it.

I guess he used the [COLOUR=WHITE] tag.. I'm not sure why Daniweb even has this, since AFAIK, its not possible to change the background of posts (Probably a good thing...)

I imagine a few other colours on that chart would be pretty nasty to read aswell... Maybe that rule should be "No unreadable colours" :)

Haha, this used to happen a lot at Cprogramming. We often hid little messages in our post while it contained useful information, so it didn't look suspecting. The background wasn't white, so we looked in the CSS to retrieve the HTML color, and then used this to make parts of the post invisible. Finally the mods got mad at us, and we had to stop. :D

The only time white is visible is when it's within another kind of tag:

this is some white

I don't have a Unix system, but try this and tell me whether it works:

Your code usually won't work. Most Unix-based systems require you to use shutdown now or shutdown -t [I]seconds till shutdown[/I] . And even with the correct syntax, shutdown is only made possible if the program is run with root privilages.

unless of course you #include "im133e7hax0r.h" :)

Your code usually won't work. Most Unix-based systems require you to use shutdown now or shutdown -t [I]seconds till shutdown[/I] . And even with the correct syntax, shutdown is only made possible if the program is run with root privilages.

I guess, you forgot to click on the links which I included with that post, which explains in detail the syntax of "shutdown" along with its usage. ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.