SysTools.pas
资源名称:计算机远程监控.rar [点击查看]
上传用户:rickyhu
上传日期:2007-05-27
资源大小:842k
文件大小:4k
源码类别:
控制台编程
开发平台:
Delphi
- ////////////////////////////////////////////////////////////////////////////////
- //
- // 2004 (C) Copyrights Reserved
- // Author:Aureala
- //
- ////////////////////////////////////////////////////////////////////////////////
- unit SysTools;
- interface
- uses
- Windows, Messages, SysUtils, TlHelp32, PSApi, Classes;
- const
- DISK='CDEFGHIJKLMNOPQRSTUVWXYZ';
- function TimeInfo():String;
- function ExtractStr(const StrOrg,SepChar:String):TStrings;
- function ListProcesses():String;
- function KillProcess(pId:Cardinal):Boolean;
- function SetPrivilege(sPrivilegeName:String;bEnabled:Boolean):Boolean;
- function WinExit(iFlags:integer):Boolean;
- procedure ShutDownMachine();
- procedure RebootMachine();
- procedure Logout();
- implementation
- function TimeInfo():String;
- var
- StrTmp:String;
- begin
- StrTmp:=FormatDateTime('YYYY-MM-DD HH:MM:SS',NOW);
- result:=StrTmp;
- end;
- function ExtractStr(const StrOrg,SepChar:String):TStrings;
- var
- StrData:TStrings;
- i,n,Len:integer;
- begin
- i:=1;
- n:=0;
- Len:=Length(StrOrg)+1;
- StrData:=TStringList.Create;
- StrData.Text:='';
- while(i<Len) do
- begin
- StrData.Add('');
- while(StrOrg[i]<>SepChar) and (i<Len) do
- begin
- StrData.Strings[n]:=StrData.Strings[n]+StrOrg[i];
- i:=i+1;
- end;
- i:=i+1;
- n:=n+1;
- end;
- result:=StrData;
- end;
- function ListProcesses():String;
- var lppe:TProcessEntry32;
- found:Boolean;
- Hand:THandle;
- FileName:array [0..MAX_PATH] of Char;
- hProcess:THandle;
- StrInfo:String;
- begin
- StrInfo:='22';
- lppe.dwSize:=Sizeof(TProcessEntry32);
- Hand:=CreateToolhelp32Snapshot(TH32CS_SNAPALL,0);
- found:=Process32First(Hand,lppe);
- while found do
- begin
- StrPCopy(FileName,lppe.szExeFile);
- hProcess:=OpenProcess(PROCESS_ALL_ACCESS or PROCESS_QUERY_INFORMATION,
- false,lppe.th32ProcessID);
- if(hProcess>0) then
- begin
- GetModuleFileNameEx(hProcess,0,FileName,MAX_PATH);
- end;
- CloseHandle(hProcess);
- StrInfo:=StrInfo+'|'+IntToHex(lppe.th32ProcessID,4)+'|'
- +lppe.szExeFile+'|'+FileName;
- found:=Process32Next(Hand,lppe);
- end;
- result:=StrInfo;
- end;
- function KillProcess(pId:Cardinal):Boolean;
- var
- Found:BOOL;
- HSnapshot,HProcess:THandle;
- Lppe:TProcessEntry32;
- begin
- HSnapshot:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
- Lppe.dwSize:=Sizeof(Lppe);
- Found:=Process32First(HSnapshot,Lppe);
- while(Found) do
- begin
- try
- if(Lppe.th32ProcessID=pId) then
- begin
- HProcess:=OpenProcess(PROCESS_ALL_ACCESS,FALSE,Lppe.th32ProcessID);
- TerminateProcess(HProcess,0);
- result:=true;
- exit;
- end;
- except
- result:=false;
- exit;
- end;
- Found:=Process32Next(HSnapshot,Lppe);
- end;
- result:=false;
- end;
- function SetPrivilege(sPrivilegeName:String;bEnabled:Boolean):Boolean;
- var
- TPPrev,TP:TTokenPrivileges;
- Token:THandle;
- dwRetLen:DWord;
- begin
- Result:=False;
- OpenProcessToken(GetCurrentProcess,TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,Token);
- TP.PrivilegeCount:=1;
- if(LookupPrivilegeValue(Nil,PChar(sPrivilegeName),TP.Privileges[0].LUID))then
- begin
- if(bEnabled)then
- begin
- TP.Privileges[0].Attributes:=SE_PRIVILEGE_ENABLED;
- end
- else begin
- TP.Privileges[0].Attributes:=0;
- end;
- dwRetLen:=0;
- Result:=AdjustTokenPrivileges(Token,False,TP,SizeOf(TPPrev),TPPrev,dwRetLen);
- end;
- CloseHandle(Token);
- end;
- function WinExit(iFlags:integer):Boolean;
- begin
- Result:=True;
- if(SetPrivilege('SeShutdownPrivilege',True))then
- begin
- if(not ExitWindowsEx(iFlags,0))then
- begin
- Result:=False;
- end;
- SetPrivilege('SeShutdownPrivilege',False);
- end
- else begin
- Result:=False;
- end;
- end;
- procedure ShutDownMachine();
- begin
- if(Win32platform=VER_PLATFORM_WIN32_WINDOWS) then
- ExitWindowsEx(EWX_FORCE+EWX_SHUTDOWN+EWX_POWEROFF,32);
- if(Win32platform=VER_PLATFORM_WIN32_NT) then
- WinExit(EWX_FORCE+EWX_SHUTDOWN+EWX_POWEROFF);
- end;
- procedure RebootMachine();
- begin
- if(Win32platform=VER_PLATFORM_WIN32_WINDOWS) then
- ExitWindowsEx(EWX_FORCE+EWX_REBOOT+EWX_POWEROFF,32);
- if(Win32platform=VER_PLATFORM_WIN32_NT) then
- WinExit(EWX_FORCE+EWX_REBOOT);
- end;
- procedure Logout();
- begin
- Win32Check(ExitWindowsEx(EWX_LOGOFF,0));
- end;
- end.