有关win2000下关机的问题(300分)

  • 主题发起人 主题发起人 www
  • 开始时间 开始时间
写写登陆的程序吧!
 
ExitWindowsEx(EWX_LOGOFF , 0); &nbsp; &nbsp; &nbsp; //注销<br>ExitWindowsEx(EWX_SHUTDOWN, 0); &nbsp; &nbsp; &nbsp;//关机,但是,不关闭电源<br>ExitWindowsEx(EWX_REBOOT , 0); &nbsp; &nbsp; &nbsp; // 重启<br>ExitWindowsEx(EWX_FORCE , 0); &nbsp; &nbsp; &nbsp; &nbsp;//强制关机<br>ExitWindowsEx(EWX_POWEROFF, 0); &nbsp; &nbsp; &nbsp;//关机,并且关闭电源<br>ExitWindowsEx(EWX_FORCEIFHUNG , 0); &nbsp;//<br>
 
to helpu:<br>&nbsp; WIN2000的关机机制和98完全不同,那些函数不顶用吧?
 
procedure CloseWin(CloseType:Byte = 1); &nbsp; &nbsp; //关机程序<br>&nbsp;const<br>&nbsp; &nbsp; EWX_FORCE=4; //关闭所有程序并以其他用户身份登录<br>&nbsp; &nbsp; EWX_LOGOFF=0; //重新启动计算机并切换到MS-DOS方式<br>&nbsp; &nbsp; EWX_REBOOT=2; //重新启动计算机<br>&nbsp; &nbsp; EWX_SHUTDOWN=1;//关闭计算机<br>begin<br>&nbsp; if GetisWind2K then AdjustToken();<br>&nbsp; case CloseType of<br>&nbsp; &nbsp; EWX_LOGOFF:ExitWindowsEx(EWX_FORCE or EWX_LOGOFF,0);<br>&nbsp; &nbsp; EWX_REBOOT:ExitWindowsEx(EWX_REBOOT,0);<br>&nbsp; else<br>&nbsp; &nbsp;ExitWindowsEx(EWX_FORCE or EWX_SHUTDOWN,0); &nbsp;//<br>&nbsp; end;<br>end;<br><br>&nbsp;procedure AdjustToken(); &nbsp; //取得关机权限<br>&nbsp;var<br>&nbsp; hdlProcessHandle:Cardinal; hdlTokenHandle:Cardinal;<br>&nbsp; tmpLuid:Int64;// tkpPrivilegeCount:Int64;<br>&nbsp; tkp:TOKEN_PRIVILEGES; &nbsp;tkpNewButIgnored:TOKEN_PRIVILEGES;<br>&nbsp; lBufferNeeded:Cardinal;Privilege : array[0..0] of _LUID_AND_ATTRIBUTES;<br>&nbsp;begin<br>&nbsp; &nbsp;hdlProcessHandle := GetCurrentProcess;<br>&nbsp; &nbsp;OpenProcessToken(hdlProcessHandle,(TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;hdlTokenHandle);<br>&nbsp; &nbsp;// Get the LUID for shutdown privilege.<br>&nbsp; &nbsp;LookupPrivilegeValue('', 'SeShutdownPrivilege', tmpLuid);<br>&nbsp; &nbsp;Privilege[0].Luid := tmpLuid;<br>&nbsp; &nbsp;Privilege[0].Attributes := SE_PRIVILEGE_ENABLED;<br>&nbsp; &nbsp;tkp.PrivilegeCount := 1; &nbsp;// One privilege to set<br>&nbsp; &nbsp;tkp.Privileges[0] := Privilege[0];<br>&nbsp; &nbsp;// Enable the shutdown privilege in the access token of this<br>&nbsp; &nbsp;// process.<br>&nbsp; &nbsp;AdjustTokenPrivileges(hdlTokenHandle,False,tkp,Sizeof(tkpNewButIgnored),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tkpNewButIgnored,lBufferNeeded);<br>&nbsp;end;<br><br>function GetisWind2K:Boolean; &nbsp;//判断是否为Nt,2k<br>var ver:integer;//版本号  <br>&nbsp; &nbsp; major:integer;//主版本号<br>&nbsp; // &nbsp;minor:integer;//次版本号<br>begin<br>&nbsp;Result:=False;<br>&nbsp;ver:=getversion();//获得版本号<br>&nbsp;major:=ver and 255;//获得主版本号<br>// minor:=(ver and 255*256)div 256; //获得次版本号<br>&nbsp; if major&gt;=5 then Result:=True;<br>end;
 
如果是在WIN2000下不能软件关机,请检查CMOS中有关POWER MANAGMENT SETUP 中的<br>ACPI FUNCTION 是否设为允许.<br><br>function ShutDown(uFlags: Cardinal):boolean;<br>const<br>&nbsp; ADJUST_PRIV = TOKEN_QUERY or TOKEN_ADJUST_PRIVILEGES;<br>&nbsp; SHTDWN_PRIV = 'SeShutdownPrivilege';<br>&nbsp; PRIV_SIZE &nbsp; = sizeOf(TTokenPrivileges);<br><br>var<br>&nbsp; Len: DWORD;<br>&nbsp; TokenPriv, Dummy: TTokenPrivileges;<br>&nbsp; Token: THandle;<br>&nbsp; Error:integer;<br>begin<br>&nbsp; error:=0;<br>&nbsp; // 设置特权<br>&nbsp; // Delphi2:<br>&nbsp; //if not OpenProcessToken(GetCurrentProcess(), ADJUST_PRIV, @Token) then<br>&nbsp; if not OpenProcessToken(GetCurrentProcess(), ADJUST_PRIV, Token) then<br>&nbsp; &nbsp; &nbsp; Error := Error or 4;<br>&nbsp; if not LookupPrivilegeValue(nil, SHTDWN_PRIV,TokenPriv.Privileges[0].Luid) then<br>&nbsp; &nbsp; Error := Error or 8;<br>&nbsp; TokenPriv.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;<br>&nbsp; TokenPriv.PrivilegeCount := 1; &nbsp;// One privilege to set<br>&nbsp; if not AdjustTokenPrivileges(Token, false, TokenPriv, PRIV_SIZE,Dummy, Len) then<br>&nbsp; &nbsp; Error:=Error or 16;<br>&nbsp; ExitWindowsEx(uFlags, 0);<br>&nbsp; Result := (Error=0);<br>end;<br><br><br><br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>ShutDown(EWX_POWEROFF + EWX_FORCE);//<br>end;
 
To: kingdeezj<br><br>当然,前提是你已经获得了权限,例如:<br><br>procedure AdjustToken();<br>var<br>&nbsp; hdlProcessHandle : Cardinal;<br>&nbsp; hdlTokenHandle &nbsp; : Cardinal;<br>&nbsp; tmpLuid &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: Int64;<br>&nbsp; tkp &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: TOKEN_PRIVILEGES;<br>&nbsp; tkpNewButIgnored : TOKEN_PRIVILEGES;<br>&nbsp; lBufferNeeded &nbsp; &nbsp;: Cardinal;<br>&nbsp; Privilege &nbsp; &nbsp; &nbsp; &nbsp;: array[0..0] of _LUID_AND_ATTRIBUTES;<br>begin<br>&nbsp; hdlProcessHandle := GetCurrentProcess;<br>&nbsp; OpenProcessToken(hdlProcessHandle,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;hdlTokenHandle);<br>&nbsp; // Get the LUID for shutdown privilege.<br>&nbsp; LookupPrivilegeValue('', 'SeShutdownPrivilege', tmpLuid);<br>&nbsp; Privilege[0].Luid := tmpLuid;<br>&nbsp; Privilege[0].Attributes := SE_PRIVILEGE_ENABLED;<br>&nbsp; tkp.PrivilegeCount := 1; &nbsp; // One privilege to set<br>&nbsp; tkp.Privileges[0] := Privilege[0];<br>&nbsp; // Enable the shutdown privilege in the access token of this process.<br>&nbsp; AdjustTokenPrivileges(hdlTokenHandle,False,tkp,Sizeof(tkpNewButIgnored),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tkpNewButIgnored,lBufferNeeded);<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>&nbsp; AdjustToken;<br>&nbsp; ExitWindowsEx(EWX_LOGOFF , 0);<br>&nbsp; //ExitWindowsEx(EWX_SHUTDOWN, 0);<br>&nbsp; //ExitWindowsEx(EWX_REBOOT , 0);<br>&nbsp; //ExitWindowsEx(EWX_FORCE , 0);<br>&nbsp; //ExitWindowsEx(EWX_POWEROFF, 0);<br>&nbsp; //ExitWindowsEx(EWX_FORCEIFHUNG , 0);<br>end;
 
如果在2000下可以自动关机,在程序中不能,那么修改你的代码。<br>如果在2000下不能,程序中也不能,那么修改你的电源设置。
 
只要再加上 &nbsp; &nbsp; EWX_POWEROFF 就可以了~
 
后退
顶部