如何在程序中控制WINDOWS2000关机或者重新启动?(100分)

  • 主题发起人 荷塘新月
  • 开始时间

荷塘新月

Unregistered / Unconfirmed
GUEST, unregistred user!
&nbsp; &nbsp;服务器开的久了,需要重新启动,但是机房的钥匙我没有,想通过程序让服务器重新启动,<br>调用哪个函数才能让WIN2000重新启动呢?
 
const //[EXIT CODE]<br>&nbsp; EI_SYSEXIT_LOGOFF &nbsp; &nbsp; &nbsp;=$00;<br>&nbsp; EI_SYSEXIT_SHUTDOWN &nbsp; &nbsp;=$01;<br>&nbsp; EI_SYSEXIT_REBOOT &nbsp; &nbsp; &nbsp;=$02;<br>&nbsp; EI_SYSEXIT_FORCE &nbsp; &nbsp; &nbsp; =$04;<br>&nbsp; EI_SYSEXIT_POWEROFF &nbsp; &nbsp;=$08;<br>&nbsp; EI_SYSEXIT_FORCEIFHUNG =$10;<br>//---------[退出系统]---------------------------------------------<br>FUNCTION FkSystemExit(vType:INTEGER;vForce:BOOL):BOOL;<br>VAR<br>&nbsp; hToken: THANDLE;<br>&nbsp; &nbsp; hProc: THANDLE;<br>&nbsp; &nbsp; iLUID: INT64;<br>&nbsp; &nbsp; mPriv, mNewPriv: TOKEN_PRIVILEGES;<br>&nbsp; &nbsp; dLength: DWORD;<br>&nbsp; &nbsp; dExit:DWORD;<br>BEGIN<br>&nbsp; IF vForce THEN dExit:=vType+EWX_FORCE ELSE dExit:=vType;<br>&nbsp; //[WIN9X]<br>&nbsp; IF Win32Platform=VER_PLATFORM_WIN32_WINDOWS THEN<br>&nbsp; &nbsp; BEGIN Result:=ExitWindowsEx(dExit,0); END;<br>&nbsp; //[WINNT]<br>&nbsp; IF Win32Platform=VER_PLATFORM_WIN32_NT &nbsp;THEN<br>&nbsp; &nbsp; BEGIN<br>&nbsp; &nbsp; hProc:=GetCurrentProcess() ;<br>&nbsp; &nbsp; OpenProcessToken(hProc,TOKEN_ADJUST_PRIVILEGES +TOKEN_QUERY,hToken);<br>&nbsp; &nbsp; LookupPrivilegeValue('','SeShutdownPrivilege',iLUID);<br>&nbsp; &nbsp; mPriv.PrivilegeCount:=1;<br>&nbsp; &nbsp; mPriv.Privileges[0].Attributes:=SE_PRIVILEGE_ENABLED;<br>&nbsp; &nbsp; mPriv.Privileges[0].Luid :=iLUID;<br>&nbsp; &nbsp; AdjustTokenPrivileges(hToken,False,mPriv,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (4+(12*mPriv.PrivilegeCount)),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mNewPriv,dLength);<br>&nbsp; &nbsp; Result:=ExitWindowsEx(dExit,0);<br>&nbsp; &nbsp; END;<br>&nbsp; //[END;]<br>END;<br>//-------------------------------------------------------------------------------<br>
 
-----以下代码在D5+WIN2000通过。<br><br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; procedure FormCreate(Sender: TObject);<br>&nbsp; private<br>function SetPrivilege (sPrivilegeName: string; bEnabled: Boolean) : Boolean;<br><br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>function WinExit(iFlags: integer):Boolean;<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>{$R *.DFM}<br><br><br>function TForm1.SetPrivilege(sPrivilegeName: string;<br>&nbsp; bEnabled: Boolean): Boolean;<br><br>var<br>&nbsp; TPPrev,<br>&nbsp; TP &nbsp; &nbsp; &nbsp; : TTokenPrivileges;<br>&nbsp; Token :THandle;<br>&nbsp; dwRetLen : DWORD;<br>begin<br>&nbsp; result := False;<br>&nbsp; OpenProcessToken (GetCurrentProcess,<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; Token);<br>&nbsp; TP.PrivilegeCount := 1;<br>&nbsp; if LookupPrivilegeValue (nil, PChar (sPrivilegeName), TP.Privileges[0].LUID) then<br>&nbsp; begin<br>&nbsp; &nbsp; if bEnabled then<br>&nbsp; &nbsp; &nbsp; TP.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; TP.Privileges[0].Attributes := 0;<br>&nbsp; &nbsp; dwRetLen := 0;<br>&nbsp; &nbsp; result := AdjustTokenPrivileges (<br>&nbsp; &nbsp; Token,<br>&nbsp; &nbsp; False,<br>&nbsp; &nbsp; TP,<br>&nbsp; &nbsp; SizeOf (TPPrev),<br>&nbsp; &nbsp; TPPrev,<br>&nbsp; &nbsp; dwRetLen);<br>&nbsp; end;<br>&nbsp; CloseHandle (Token);<br>end;<br>function Tform1.WinExit (iFlags: integer) : Boolean;<br><br>begin<br>&nbsp; result := true;<br>&nbsp; if SetPrivilege ('SeShutdownPrivilege', true) then<br>&nbsp; begin<br>&nbsp; &nbsp; if (not ExitWindowsEx (iFlags, 0)) then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; // handle errors...<br>&nbsp; &nbsp; &nbsp; result := False<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; SetPrivilege ('SeShutdownPrivilege', False)<br>&nbsp; end<br>&nbsp; else<br>&nbsp; begin<br>&nbsp; &nbsp; // handle errors...<br>&nbsp; &nbsp; result := False<br>&nbsp; end;<br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br>&nbsp;WinExit(EWX_POWEROFF + EWX_FORCE);//在这里控制是关机、重启等。<br>end;<br><br>end.
 
前提条件:<br>1、有那台服务器的管理员帐号、密码<br>2、有一台连接到那台服务器的机器<br>然后就可以通过下面的步骤进行远程关机、重启:<br>1、取得关机权限<br>2、用InitSystemShutdown指定服务器名关机或重启。
 
TO:xianjun<br>用telnet!<br>木馬也行!<br>還有winxp的遠程關機等!
 
权限和密码肯定都有,因为我是系统管理员!所没有的是机房的钥匙!谢谢各位了!
 
还是要源码? 给你发一段以前写的可用代码:[:)]<br>const<br>&nbsp; ADJUST_PRIV = TOKEN_QUERY or TOKEN_ADJUST_PRIVILEGES;<br>&nbsp; SHTDWN_PRIV = 'SeShutdownPrivilege';<br>&nbsp; PRIV_SIZE = SizeOf(TTokenPrivileges);<br><br>//发出关机指令<br>procedure TForm1.Button5Click(Sender: TObject);<br>var<br>&nbsp; Len: DWORD;<br>&nbsp; TokenPriv, Dummy: TTokenPrivileges;<br>&nbsp; Token: THandle;<br>&nbsp; lpNetRes: TNetResourceA;<br>begin<br>&nbsp; //登录到目标计算机(要有关机权限的用户:如管理员)<br>&nbsp; FillChar(lpNetRes, SizeOf(lpNetRes), 0);<br>&nbsp; lpNetRes.dwScope := RESOURCE_GLOBALNET;<br>&nbsp; lpNetRes.dwType := RESOURCETYPE_ANY;<br>&nbsp; lpNetRes.dwDisplayType := RESOURCEDISPLAYTYPE_SERVER;<br>&nbsp; lpNetRes.lpRemoteName := PChar('//' + edtMachine.Text);<br>&nbsp; if not (WNetAddConnection2(lpNetRes, PChar(edtPass.Text), PChar(edtUser.Text),<br>&nbsp; &nbsp; 0) in [NO_ERROR, ERROR_ALREADY_ASSIGNED]) then<br>&nbsp; &nbsp; RaiseLastOSError;<br><br>&nbsp; //取得关机权限<br>&nbsp; if not OpenProcessToken(GetCurrentProcess(), ADJUST_PRIV, Token) then<br>&nbsp; &nbsp; RaiseLastOSError;<br>&nbsp; if not LookupPrivilegeValue(nil, SHTDWN_PRIV, TokenPriv.Privileges[0].Luid) then<br>&nbsp; &nbsp; RaiseLastOSError;<br>&nbsp; TokenPriv.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;<br>&nbsp; TokenPriv.PrivilegeCount := 1;<br>&nbsp; if not AdjustTokenPrivileges(Token, False, TokenPriv, PRIV_SIZE, Dummy, Len) then<br>&nbsp; &nbsp; RaiseLastOSError;<br><br>&nbsp; if not InitiateSystemShutdown(PChar(edtMachine.Text), PChar(edtMsg.Text),<br>&nbsp; &nbsp; StrToIntDef(edtTimeOut.Text, 1800), False, True) then<br>&nbsp; &nbsp; //参数: 要关的机器名 显示的信息 倒计时秒数 强制结束程序 重新启动<br>&nbsp; &nbsp; RaiseLastOSError;<br>end;<br><br>//取消关机指令<br>procedure TForm1.Button6Click(Sender: TObject);<br>begin<br>&nbsp; AbortSystemShutdown(PChar(edtMachine.Text));<br>end;<br>
 
另外,你要确保你的服务器在启动过程不用人为干涉即能正常启动<br>否则关掉以后就不能启动了!!!<br>比如设置了BIOS密码的机器就不能这么用了。
 
多人接受答案了。
 
顶部