// shutdown.cpp : main project file.執行後就會關機了,但是power不會off,會到..."現在可以自行關機了..."
#include "stdafx.h"
using namespace System;
using namespace System::Runtime::InteropServices;
[DllImport("kernel32.dll")]
extern "C" int* GetCurrentProcess(void);
[DllImport("advapi32.dll")]
extern "C" int OpenProcessToken(int* ProcessHandle,unsigned long DesireAccess, int** TokenHandle);
typedef struct _LUID {
unsigned long LowPart;
long HighPart;
} LUID, *PLUID;
[DllImport("advapi32.dll")]
extern "C" int LookupPrivilegeValue(char* lpSystemName, char* lpName, PLUID lpLuid);
typedef struct _LUID_AND_ATTRIBUTES {
LUID Luid;
unsigned long Attributes;
} LUID_AND_ATTRIBUTES, *PLUID_AND_ATTRIBUTES;
typedef struct _TOKEN_PRIVILEGES {
unsigned long PrivliegeCount;
LUID_AND_ATTRIBUTES Privileges[1];
} TOKEN_PRIVILEGES, *PTOKEN_PRIVILEGES;
[DllImport("advapi32.dll")]
extern "C" int AdjustTokenPrivileges(int* TokenHandle, int DisableAllPriviliges, PTOKEN_PRIVILEGES NewState,
unsigned long BufferLength, PTOKEN_PRIVILEGES PreviousState,
unsigned long ReturnLength);
[DllImport("kernel32.dll")]
extern "C" unsigned long GetLastError(void);
[DllImport("user32.dll")]
extern "C" int ExitWindowsEx(unsigned int uFlags, unsigned long dwReason);
const int SE_PRIVILEGE_ENABLED = 0x00000002;
const int TOKEN_QUERY = 0x00000008;
const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
char *SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
const int EWX_LOGOFF = 0x00000000;
const int EWX_SHUTDOWN = 0x00000001;
const int EWX_REBOOT = 0x00000002;
const int EWX_FORCE = 0x00000004;
const int EWX_POWEROFF = 0x00000008;
const int EWX_FORCEIFHUNG = 0x00000010;
int main(array<System::String ^> ^args)
{
int* hToken;
TOKEN_PRIVILEGES tkp;
if( !OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken) ) {
Console::WriteLine(L"Failed!!\n");
return 0;
}
LookupPrivilegeValue(0,SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
tkp.PrivliegeCount = 1;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken, 0, &tkp, 0, (PTOKEN_PRIVILEGES)0, 0);
if(GetLastError() != 0){
Console::WriteLine(L"Error!\n");
return 0;
}
if(!ExitWindowsEx(1,0)){
Console::WriteLine(L"ExitFail\n");
return 0;
}
Console::WriteLine(L"Hello World");
return 0;
}
不知道是甚麼問題....是不是argument 沒有加上 POWEROFF - 0x08 ?
的確,ExitWindowsEx 的argument使用
EWX_SHUTDOWN | EWX_FORCE | EWX_POWEROFF就會自動poweroff了
沒有留言:
張貼留言