SysTools.pas
上传用户:rickyhu
上传日期:2007-05-27
资源大小:842k
文件大小:4k
源码类别:

控制台编程

开发平台:

Delphi

  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // 2004 (C) Copyrights Reserved
  4. // Author:Aureala
  5. //
  6. ////////////////////////////////////////////////////////////////////////////////
  7. unit SysTools;
  8. interface
  9. uses
  10.   Windows, Messages, SysUtils, TlHelp32, PSApi, Classes;
  11. const
  12.   DISK='CDEFGHIJKLMNOPQRSTUVWXYZ';
  13.   function  TimeInfo():String;
  14.   function  ExtractStr(const StrOrg,SepChar:String):TStrings;
  15.   function  ListProcesses():String;
  16.   function  KillProcess(pId:Cardinal):Boolean;
  17.   function  SetPrivilege(sPrivilegeName:String;bEnabled:Boolean):Boolean;
  18.   function  WinExit(iFlags:integer):Boolean;
  19.   procedure ShutDownMachine();
  20.   procedure RebootMachine();
  21.   procedure Logout();  
  22. implementation
  23. function TimeInfo():String;
  24. var
  25.   StrTmp:String;
  26. begin
  27.   StrTmp:=FormatDateTime('YYYY-MM-DD HH:MM:SS',NOW);
  28.   result:=StrTmp;
  29. end;
  30. function  ExtractStr(const StrOrg,SepChar:String):TStrings;
  31. var
  32.   StrData:TStrings;
  33.   i,n,Len:integer;
  34. begin
  35.   i:=1;
  36.   n:=0;
  37.   Len:=Length(StrOrg)+1;
  38.   StrData:=TStringList.Create;
  39.   StrData.Text:='';
  40.   while(i<Len) do
  41.   begin
  42.     StrData.Add('');
  43.     while(StrOrg[i]<>SepChar) and (i<Len) do
  44.     begin
  45.       StrData.Strings[n]:=StrData.Strings[n]+StrOrg[i];
  46.       i:=i+1;
  47.     end;
  48.     i:=i+1;
  49.     n:=n+1;
  50.   end;
  51.   result:=StrData;
  52. end;
  53. function ListProcesses():String;
  54. var lppe:TProcessEntry32;
  55.     found:Boolean;
  56.     Hand:THandle;
  57.     FileName:array [0..MAX_PATH] of Char;
  58.     hProcess:THandle;
  59.     StrInfo:String;
  60. begin
  61.   StrInfo:='22';
  62.   lppe.dwSize:=Sizeof(TProcessEntry32);
  63.   Hand:=CreateToolhelp32Snapshot(TH32CS_SNAPALL,0);
  64.   found:=Process32First(Hand,lppe);
  65.   while found do
  66.   begin
  67.     StrPCopy(FileName,lppe.szExeFile);
  68.     hProcess:=OpenProcess(PROCESS_ALL_ACCESS or PROCESS_QUERY_INFORMATION,
  69.                            false,lppe.th32ProcessID);
  70.     if(hProcess>0) then
  71.     begin
  72.       GetModuleFileNameEx(hProcess,0,FileName,MAX_PATH);
  73.     end;
  74.     CloseHandle(hProcess);
  75.     StrInfo:=StrInfo+'|'+IntToHex(lppe.th32ProcessID,4)+'|'
  76.       +lppe.szExeFile+'|'+FileName;
  77.     found:=Process32Next(Hand,lppe);
  78.   end;
  79.   result:=StrInfo;
  80. end;
  81. function KillProcess(pId:Cardinal):Boolean;
  82. var
  83.   Found:BOOL;
  84.   HSnapshot,HProcess:THandle;
  85.   Lppe:TProcessEntry32;
  86. begin
  87.   HSnapshot:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
  88.   Lppe.dwSize:=Sizeof(Lppe);
  89.   Found:=Process32First(HSnapshot,Lppe);
  90.   while(Found) do
  91.   begin
  92.     try
  93.       if(Lppe.th32ProcessID=pId) then
  94.       begin
  95.         HProcess:=OpenProcess(PROCESS_ALL_ACCESS,FALSE,Lppe.th32ProcessID);
  96.         TerminateProcess(HProcess,0);
  97.         result:=true;
  98.         exit;
  99.       end;
  100.     except
  101.       result:=false;
  102.       exit;
  103.     end;
  104.     Found:=Process32Next(HSnapshot,Lppe);
  105.   end;
  106.   result:=false;
  107. end;
  108. function SetPrivilege(sPrivilegeName:String;bEnabled:Boolean):Boolean;
  109. var
  110.   TPPrev,TP:TTokenPrivileges;
  111.   Token:THandle;
  112.   dwRetLen:DWord;
  113. begin
  114.   Result:=False;
  115.   OpenProcessToken(GetCurrentProcess,TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,Token);
  116.   TP.PrivilegeCount:=1;
  117.   if(LookupPrivilegeValue(Nil,PChar(sPrivilegeName),TP.Privileges[0].LUID))then
  118.   begin
  119.     if(bEnabled)then 
  120.     begin
  121.       TP.Privileges[0].Attributes:=SE_PRIVILEGE_ENABLED;
  122.     end
  123.     else begin 
  124.       TP.Privileges[0].Attributes:=0;
  125.     end;
  126.     dwRetLen:=0;
  127.     Result:=AdjustTokenPrivileges(Token,False,TP,SizeOf(TPPrev),TPPrev,dwRetLen);
  128.   end;
  129.   CloseHandle(Token);
  130. end;
  131. function WinExit(iFlags:integer):Boolean;
  132. begin
  133.   Result:=True;
  134.   if(SetPrivilege('SeShutdownPrivilege',True))then
  135.   begin
  136.     if(not ExitWindowsEx(iFlags,0))then
  137.     begin
  138.       Result:=False;
  139.     end;
  140.     SetPrivilege('SeShutdownPrivilege',False);
  141.   end
  142.   else begin
  143.     Result:=False;
  144.   end;
  145. end;
  146. procedure ShutDownMachine();
  147. begin
  148.   if(Win32platform=VER_PLATFORM_WIN32_WINDOWS) then
  149.     ExitWindowsEx(EWX_FORCE+EWX_SHUTDOWN+EWX_POWEROFF,32);
  150.   if(Win32platform=VER_PLATFORM_WIN32_NT) then
  151.     WinExit(EWX_FORCE+EWX_SHUTDOWN+EWX_POWEROFF);
  152. end;
  153. procedure RebootMachine();
  154. begin
  155.   if(Win32platform=VER_PLATFORM_WIN32_WINDOWS) then
  156.     ExitWindowsEx(EWX_FORCE+EWX_REBOOT+EWX_POWEROFF,32);
  157.   if(Win32platform=VER_PLATFORM_WIN32_NT) then
  158.     WinExit(EWX_FORCE+EWX_REBOOT);
  159. end;
  160. procedure Logout();
  161. begin
  162.   Win32Check(ExitWindowsEx(EWX_LOGOFF,0));
  163. end;
  164. end.