user1944429
user1944429

Reputation:

What is the best way to block system shutdown in an application

I'm fairly new to the Windows API. I'm trying to figure out how to block system shutdown. I figured out about SERVICE_CONTROL_PRESHUTDOWN. Not exactly how to use it yet, but I was planning to do something along the following lines as HandlerEx:

DWORD WINAPI serviceHandler(DWORD dwControl, DWORD dwEventType, LPVOID lpEventData, LPVOID lpContext)
{
    switch (dwControl)
    {
    case SERVICE_CONTROL_PRESHUTDOWN:
        while (1)
            Sleep(1000);
    }
    return NO_ERROR;
}

But A.) I'm not sure if I'm reading MSDN right, and implementing this serviceHandler correctly, and B.) Is there a better way of doing this. I essentially need this application to not allow shutdown unless it was forcibly by holding down the powerbutton.

Upvotes: 0

Views: 1269

Answers (3)

AleXelton
AleXelton

Reputation: 772

There is a limited time available for service shutdown.
20 seconds will help you block system shutdown.

The PC will perform an action similar to "shutdown -t 20 -s", then quickly enter Windows Logo+ R \ "shutdown -a" and the above command will be canceled.

It can be easily implemented using WinExec () or CreateProcess ().

Upvotes: 0

Sergei Vorobiev
Sergei Vorobiev

Reputation: 374

Instead of doing it from code, you should deploy several group policies, as described here: http://www.sevenforums.com/tutorials/128920-shut-down-computer-allow-prevent-users-groups.html

Upvotes: 1

Jonathan Potter
Jonathan Potter

Reputation: 37122

You can't block system shutdown indefinitely like that. See the remarks for the HandlerEx callback function:

The SERVICE_CONTROL_SHUTDOWN control code should only be processed by services that must absolutely clean up during shutdown, because there is a limited time (about 20 seconds) available for service shutdown. After this time expires, system shutdown proceeds regardless of whether service shutdown is complete

After at most 20 seconds Windows will kill your service and continue the shutdown process.

Upvotes: 1

Related Questions