NT下控制重新启动的编程问题(200分)

  • 主题发起人 主题发起人 mikecool
  • 开始时间 开始时间
M

mikecool

Unregistered / Unconfirmed
GUEST, unregistred user!
我原来写的一个程序是给我自己进行远程管理用的,不过现在转换成windows2000平台以后<br>重新启动不能使用了<br>我使用的是ExitWindowsEx函数,调用参数(2,0)<br>阅读SDK,需要使用AdjustTokenPrivileges函数取得权限<br>这个函数的调用我没看明白,望指点
 
好像不是你的问题,关机权限应当是用户的。
 
不是的,运行的是administrator,直接运行是没有效果
 
来自:温柔一刀 时间:2000-6-20 18:41:11 ID:268738 <br>procedure ExitWindowsNT(uFlags : integer);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;hToken : THANDLE;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tkp, tkDumb : TTokenPrivileges;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DumbInt : integer;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FillChar(tkp, sizeof(tkp), 0);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Get a token for this process<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if not (OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;or TOKEN_QUERY, hToken)) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; raise Exception.create('OpenProcessToken failed with code '<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + inttostr(GetLastError));<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Get the LUID for the Shutdown privilege<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;LookupPrivilegeValue(nil, pchar('SeShutdownPrivilege'),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tkp.Privileges[0].Luid);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tkp.PrivilegeCount := 1; // one privilege to set<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Get the shutdown provolege for this process<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AdjustTokenPrivileges(hToken, false, tkp, sizeof(tkDumb), tkDumb, DumbInt);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Cannot test the return value of AdjustTokenPrivileges<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if GetLastError &lt;&gt; ERROR_SUCCESS then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Raise Exception.create('AdjustTokenPrivileges failed with code ' <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + inttostr(GetLastError));<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// shut down the system and for all applications to close<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if not ExitWindowsEx(uFlags, 0) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Raise Exception.create('ExitWindowsEx failed with code '<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + inttostr(GetLastError));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br><br>调用方法:<br>ExitWindowsNT(EWX_SHUTDOWN or EWX_FORCE)<br>ExitWindowsNT(EWX_REBOOT)等等<br>&nbsp;<br>&nbsp;<br>
 
非常感谢wjiachun,我搜索不出来这个回答<br>我使用了这个函数,可以的,不过有一个小的问题出现,不知道是因为版本问题还是别的什么<br>我用delphi5编译出现了error<br>我试图阅读VCL windows.pas发现最后那个DumbInt是一个DWORD,我把DumbInt的类型改成<br>DWORD就一切OK<br>具体的原理,我也很长时间没用delphi了说不大上来,只能用了再说:)<br>大部分分数给wjiachun,少部分给参与者<br>感谢大家
 
多人接受答案了。
 
后退
顶部