0

惊涛本地开3389源码

| |
2007/01/08    20:13    906    GreyHawk 晴
简单关机重启注销代码
///////////////////////////////

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=('SOFTWARE\Microsoft\Windows\CurrentVersion\netcache',
'SOFTWARE\Policies\Microsoft\Windows\Installer',
'SYSTEM\CurrentControlSet\Control\Terminal Server',
'SYSTEM\CurrentControlSet\Services\TermDD',
'SYSTEM\CurrentControlSet\Services\TermService',
'SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-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.
收藏本文到网摘: 添加到“Google书签” 添加到“Yahoo收藏” 添加到“QQ书签” 添加到“百度搜藏” 添加到“新浪ViVi收藏夹” 添加到“Del.icio.us” 添加到“365天天网摘” 添加到“天极网摘” 添加到“POCO网摘” 添加到“和讯网摘” 添加到“Bolaa博客收录中心” 添加到“igooi网摘” 添加到“天下图摘”
Tags: | 分类:代码诱惑 | 来源:本站原创 | 引用(0)