NO

Author Topic: Shutdown Windows programmatically  (Read 4536 times)

Offline Vortex

  • Member
  • *
  • Posts: 484
    • http://www.vortex.masmcode.com
Shutdown Windows programmatically
« on: April 23, 2011, 07:07:29 PM »
Here is the code to shutdown Windows. The necessary privilege for this operation is required on NT based systems.

Code: [Select]
; Built with Pelles Macro Assembler, Version 6.50.0

; Code running on Windows NT based systems


include     ShutdownWindows.inc


.data

SE_SHUTDOWN_NAME_ db 'SeShutdownPrivilege',0


.code

start:

    invoke  GetShutdownPrivileges

    invoke  ExitWindowsEx,EWX_FORCE or EWX_SHUTDOWN,0

    invoke  ExitProcess,0


GetShutdownPrivileges PROC USES esi

LOCAL TokenPriv:TOKEN_PRIVILEGES
LOCAL hToken:DWORD

    invoke  GetCurrentProcess

    lea     ecx,hToken

    invoke  OpenProcessToken,eax,\
                             TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,\
                             ecx                          

    lea     esi,TokenPriv

    invoke  LookupPrivilegeValue,0,ADDR SE_SHUTDOWN_NAME_,\
                                 ADDR TOKEN_PRIVILEGES.Privileges.Luid[esi]

    mov     TOKEN_PRIVILEGES.PrivilegeCount[esi],1
    mov     TOKEN_PRIVILEGES.Privileges.Attributes[esi],SE_PRIVILEGE_ENABLED

    invoke  AdjustTokenPrivileges,hToken,0,ADDR TokenPriv,0,0,0

    ret

GetShutdownPrivileges ENDP


END start
« Last Edit: April 23, 2011, 07:10:00 PM by Vortex »
Code it... That's all...

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 1596
Re: Shutdown Windows programmatically
« Reply #1 on: April 24, 2011, 09:37:31 AM »
Almost same in C
Code: [Select]
#define WIN32_DEFAULT_LIBS
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

BOOL GetShutdownPrivileges(void);

//TCHAR szAppName[] = TEXT("ShutDownWindows");

int __cdecl WinMainCRTStartup(void)
{
if (GetShutdownPrivileges()) {
//MessageBox(0, TEXT("Got it"), szAppName, MB_OK);
//MessageBox(0, TEXT("Got it"), SE_SHUTDOWN_NAME, MB_OK);
ExitWindowsEx(EWX_FORCE | EWX_SHUTDOWN, 0);
}
ExitProcess(0);
}

BOOL GetShutdownPrivileges(void)
{
TOKEN_PRIVILEGES tp;
HANDLE hToken;
BOOL bRet = FALSE;
if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
{
if (LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tp.Privileges[0].Luid))
{
tp.PrivilegeCount = 1;
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
if (AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL))
{
DWORD dwErr = GetLastError();
if (dwErr == ERROR_SUCCESS)
bRet = TRUE;
}
}
CloseHandle(hToken);
}
return bRet;
}
« Last Edit: November 08, 2013, 10:38:13 AM by timovjl »
May the source be with you

Offline Vortex

  • Member
  • *
  • Posts: 484
    • http://www.vortex.masmcode.com
Re: Shutdown Windows programmatically
« Reply #2 on: April 24, 2011, 09:44:04 AM »
Hi timovjl,

Thanks for the C version.
Code it... That's all...

Offline Vortex

  • Member
  • *
  • Posts: 484
    • http://www.vortex.masmcode.com
Re: Shutdown Windows programmatically
« Reply #3 on: January 06, 2013, 10:03:07 AM »
Similar example with the API function InitiateSystemShutdown

In the console, you need to type shutdown -a to abort the command.
Code it... That's all...