api关机

mingcsharp 2011-10-07 11:31:56
网上都说这两个函数可以实现快速关机,可是我用了连一点反应都没有,不知道为什么,请高手指教.谢谢!
RtlAdjustPrivilege(SE_DEBUG_PRIVILEGE,true,false,out i);
NtShutdownSystem(ShutdownPowerOff);
...全文
372 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
星羽 2011-10-08
  • 打赏
  • 举报
回复
没必要用到Ntxxxxx的函数吧
用这个试试

BOOL WINAPI ExitWindowsEx(
__in UINT uFlags,
__in DWORD dwReason
);
赵4老师 2011-10-08
  • 打赏
  • 举报
回复
liuwu19301 2011-10-08
  • 打赏
  • 举报
回复

void root()
{
HANDLE hToken;
LUID sedebugnameValue;
TOKEN_PRIVILEGES tkp;
int fResult;
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;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,(PTOKEN_PRIVILEGES) NULL, 0); //调整获得的权限

ExitWindowsEx(EWX_SHUTDOWN,0);
}
mingcsharp 2011-10-08
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 only_lonely 的回复:]
引用 10 楼 wmingcsharp 的回复:

怎么让程序运行的时候就在管理员的模式下?

.... ....
这...很难...,让操作电脑的人提供授权是最简单的了.. ...
[/Quote]
我就怕你这样说,结果你还是这样说了。。。。。。。。。唉。。。。
only_lonely 2011-10-08
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 wmingcsharp 的回复:]

怎么让程序运行的时候就在管理员的模式下?
[/Quote]
.... ....
这...很难...,让操作电脑的人提供授权是最简单的了.. ...
mingcsharp 2011-10-08
  • 打赏
  • 举报
回复
怎么让程序运行的时候就在管理员的模式下?
only_lonely 2011-10-08
  • 打赏
  • 举报
回复
关机需要的权限挺大的,在vista 之后没有管理员权限的程序怕是不能。LZ可以尝试一下LS的解决方案。
辰岡墨竹 2011-10-08
  • 打赏
  • 举报
回复
微软推荐的正规关机代码是这个:

#include <windows.h>

#pragma comment( lib, "advapi32.lib" )

BOOL MySystemShutdown( LPTSTR lpMsg )
{
HANDLE hToken; // handle to process token
TOKEN_PRIVILEGES tkp; // pointer to token structure

BOOL fResult; // system shutdown flag

// Get the current process token handle so we can get shutdown
// privilege.

if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
return FALSE;

// Get the LUID for shutdown privilege.

LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
&tkp.Privileges[0].Luid);

tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

// Get shutdown privilege for this process.

AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES) NULL, 0);

// Cannot test the return value of AdjustTokenPrivileges.

if (GetLastError() != ERROR_SUCCESS)
return FALSE;

// Display the shutdown dialog box and start the countdown.

fResult = InitiateSystemShutdown(
NULL, // shut down local computer
lpMsg, // message for user
30, // time-out period, in seconds
FALSE, // ask user to close apps
TRUE); // reboot after shutdown

if (!fResult)
return FALSE;

// Disable shutdown privilege.

tkp.Privileges[0].Attributes = 0;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES) NULL, 0);

return TRUE;
}


注意在Vista/7上没有以管理员方式运行的程序无法得到关机的权限令牌会关机失败。
辰岡墨竹 2011-10-08
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wmingcsharp 的回复:]

是内核态的,不是什么系统态的,
难道内核态的有什么不一样吗??
[/Quote]

这些内核函数在ntdll.dll里,所以你必须LoadLibrary加载它。而且在比较新的系统里比如Vista/7应该是不允许普通程序使用这些函数的,这些没用公开的机制没人能保证一定向后兼容。


HINSTANCE h=LoadLibrary("ntdll.dll");
if(!h)
{
MessageBox("加载'ntdll.dll'失败!");
return;
}

typedef int (__stdcall *AdjustPrivilege)(int,int,int,int *);
AdjustPrivilege SetPrivilege=NULL;
SetPrivilege=(AdjustPrivilege)GetProcAddress(h,"RtlAdjustPrivilege");
if(!SetPrivilege)
{
MessageBox("加载'RtlAdjustPrivilege'失败!");
return;
}

typedef int (__stdcall *ShutdownSystem)(int);
ShutdownSystem Shutdown=NULL;
Shutdown=(ShutdownSystem)GetProcAddress(h,"NtShutdownSystem");
if(!Shutdown)
{
MessageBox("加载'NtShutdownSystem'失败!");
return;
}

const int SE_SHUTDOWN_PRIVILEGE = 19;
const int shutdown = 0;
const int RESTART = 1;
const int POWEROFF = 2;
int *a;
(*SetPrivilege)(SE_SHUTDOWN_PRIVILEGE,true,false,a);
(*Shutdown)(POWEROFF);


你可以试试,在任务管理器里按住Ctrl键点关机,效果就是立即关机。但是这其实是一种违规操作。
因为系统会跳过轮询关闭所有应用程序,保存注册表和用户配置,I/O缓冲回写,设备驱动关闭操作等等常规操作而直接调用ACPI关闭电源。
这样其实和直接切断电源差不了多少,会导致文件和配置丢失。所以一般情况下都是绝对不能这样关机的。

ExitWindowsEx在Win9x上可以直接退出系统。但是要在WinNT内核的系统上必须提权。
mingcsharp 2011-10-08
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 chiyer 的回复:]
没必要用到Ntxxxxx的函数吧
用这个试试

BOOL WINAPI ExitWindowsEx(
__in UINT uFlags,
__in DWORD dwReason
);
[/Quote]
问题是只用这一个无用啊?
Athenacle_ 2011-10-07
  • 打赏
  • 举报
回复
当然,你可能没有执行的权限

而且你使用了Native API,MS可能在未做任何通知的情况下更改API的行为
mingcsharp 2011-10-07
  • 打赏
  • 举报
回复
是内核态的,不是什么系统态的,
难道内核态的有什么不一样吗??
Athenacle_ 2011-10-07
  • 打赏
  • 举报
回复
大概这两个API是系统态的把,,你GetLastError()试试看

64,685

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧