当前位置:编程学习 > 网站相关 >>

简单关机重启注销及惊涛本地开3389源码

作者:hnxyy.512j.com    

简单关机重启注销代码
///////////////////////////////

program shut;

{$APPTYPE CONSOLE}

uses
  windows;

procedure ShowHelp;
begin
  WriteLn(=========== Shut or Reboot Commputer by hnxyy! ============);
  WriteLn();
  WriteLn(用法:+ParamStr(0)+ <-s/-S> or <-r/-R> or <-o/-O>);
  WriteLn();
  WriteLn(说明:);
  WriteLn();
  Writeln(-s/-S:关闭计算机);
  Writeln(-r/-R:重启计算机);
  Writeln(-o/-O:注销计算机);
  Writeln();
  WriteLn(===========================================================);
end;

procedure ExitWindowsNT(uFlags: integer);
var
  hToken : THANDLE;
  tkp, tkDumb : TTokenPrivileges;
  DumbInt : DWORD;
begin
  FillChar(tkp, sizeof(tkp), 0);
  // Get a token for this process
  if not (OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES
          or TOKEN_QUERY, hToken)) then
     Writeln(OpenProcessToken failed !);

  // Get the LUID for the Shutdown privilege
  LookupPrivilegeValue(nil, pchar(SeShutdownPrivilege),
                       tkp.Privileges[0].Luid);

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

  // Get the shutdown provolege for this process
  AdjustTokenPrivileges(hToken, false, tkp, sizeof(tkDumb), tkDumb, DumbInt);

  // Cannot test the return value of AdjustTokenPrivileges
  if GetLastError <> ERROR_SUCCESS then
     Writeln(AdjustTokenPrivileges failed !);

  // shut down the system and for all applications to close
  if not ExitWindowsEx(uFlags, 0) then
     Writeln(ExitWindowsEx failed !);
end;

begin
  { TODO -oUser -cConsole Main : Insert code here }
  if (ParamCount<1) or (ParamCount>1) then
  begin
    ShowHelp;
    exit;
  end;
  if (ParamStr(1)=-s) or (ParamStr(1)=-S) then
  begin
    //关机
    ExitWindowsNT(EWX_POWEROFF + EWX_FORCE);
  end else
  if (ParamStr(1)=-r) or (ParamStr(1)=-R) then
  begin
    //重启
    ExitWindowsNT(EWX_REBOOT + EWX_FORCE);
  end else
  if (ParamStr(1)=-o) or (ParamStr(1)=-O) then
  begin
    //注销
    ExitWindowsNT(EWX_LOGOFF + EWX_FORCE);
  end else
  begin
    ShowHelp;
    exit;
  end;
end.

////////////////////////////////

惊涛本地开3389源码

program StartRdp;
{=======================================================}
{                                                       }
{ 深入浅出3389 - 打开本机3389代码                      }
{                                                       }
{ 版权所有 (c) 2004 陈经韬                              }
{                                                       }
{ http://www.138soft.com                                }
{                                                       }
{=======================================================}
uses
  Windows;

{$APPTYPE CONSOLE}

procedure AdjustToken();{NT内核电脑关机需要通过本函数获取特权}
var
hdlProcessHandle: Cardinal;
hdlTokenHandle: Cardinal;
tmpLuid: Int64;
tkp: TOKEN_PRIVILEGES;
tkpNewButIgnored: TOKEN_PRIVILEGES;
lBufferNeeded: Cardinal;
Privilege: array[0..0] of _LUID_AND_ATTRIBUTES;
begin
hdlProcessHandle := GetCurrentProcess;
OpenProcessToken(hdlProcessHandle,
(TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY),hdlTokenHandle);
LookupPrivilegeValue(, SeShutdownPrivilege, tmpLuid);
Privilege[0].Luid := tmpLuid;
Privilege[0].Attributes := SE_PRIVILEGE_ENABLED;
tkp.PrivilegeCount := 1; // One privilege to set
tkp.Privileges[0] := Privilege[0];
AdjustTokenPrivileges(hdlTokenHandle,False,tkp,Sizeof(tkpNewButIgnored),
tkpNewButIgnored,lBufferNeeded);
end;

function MyRegWriteInteger  (RootKey:HKEY;SubKey:String;KeyName:String;Value:integer):Boolean;
var
key : HKEY;
ret : integer;
chg : DWORD;
begin
Result:=False;
key := 0;
ret := RegCreateKeyEx(RootKey,Pchar(SubKey),0,Nil,REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,Nil,key,@chg);
if (ret<>ERROR_SUCCESS) or (key=0) then exit;
try
RegSetValueEx(key,Pchar(KeyName),0,REG_DWORD,@Value,sizeof(Value));
finally
RegCloseKey(key);
end;
Result:=True;
end;

const
RootKey:array[1..6] of HKEY=(HKEY_LOCAL_MACHINE,
HKEY_LOCAL_MACHINE,
HKEY_LOCAL_MACHINE,
HKEY_LOCAL_MACHINE,
HKEY_LOCAL_MACHINE,
HKEY_LOCAL_MACHINE);
SubKey:array[1..6] of String=(SOFTWAREMicrosoftWindowsCurrentVersion etcache,
SOFTWAREPoliciesMicrosoftWindowsInstaller,
SYSTEMCurrentControlSetControlTerminal Server,
SYSTEMCurrentControlSetServicesTermDD,
SYSTEMCurrentControlSetServicesTermService,
SYSTEMCurrentControlSetControlTerminal ServerWinStationsRDP-Tcp);
KeyName:array[1..6] of String=(Enabled,EnableAdminTSRemote,TSEnabled,
Start,Start,PortNumber);
Value:array[1..6] of integer=(0,1,1,2,2,3389);
var
i:integer;
begin
for i:=1 to 6 do MyRegWriteInteger(RootKey,SubKey,KeyName,Value);
AdjustToken; {获取关机特权}
ExitWindowsEx(EWX_REBOOT + EWX_FORCE, 0);{重启电脑}
end.

程序及源码下载:

http://hnxyy.512j.com/Shut.rar

补充:综合编程 , 安全编程 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,