Get the cell for the list of processes

Source: Internet
Author: User

Refer to some online information, and then change their own ... Mainly for their own writing of a monitoring activity process tools used, there is a need to refer to the reference

UnitProcessinfos;InterfaceusesWindows, TLHelp32, Psapi, sysutils;typeTprocessinfo=Recordpid:cardinal; PathName:string; CMDName:string; End; Pprocessinfo=^tprocessinfo; Tprocessinfos=Array  ofTprocessinfo;procedureGetprocessinfos (varApilist:tprocessinfos);functionFindprocess (apathname:string;varapi:tprocessinfo): Boolean;functionGetprocessinfo (apid:cardinal; Api:pprocessinfo): Boolean;Implementationtypeunicode_string=packed RecordLength:word;    Maximumlength:word;  Buffer:pwidechar; End; Punicode_string=unicode_string; Process_parameters=packed RecordAllocationsize:ulong;    Actualsize:ulong;    Flags:ulong;    Unknown1:ulong;    unknown2:unicode_string;    Inputhandle:thandle;    Outputhandle:thandle;    Errorhandle:thandle;    currentdirectory:unicode_string;    Currentdirectoryhandle:thandle;    searchpaths:unicode_string;    applicationname:unicode_string;    commandline:unicode_string;    Environmentblock:pointer; Unknown:Array[0..9-1] ofULONG;    unknown3:unicode_string;    unknown4:unicode_string;    unknown5:unicode_string;  unknown6:unicode_string; End; Pprocess_parameters=^process_parameters; PEB=packed RecordAllocationsize:ulong;    Unknown1:ulong;    Processhinstance:longword;    Listdlls:pointer;    Processparameters:pprocess_parameters;    Unknown2:ulong;  Heap:thandle; End; Ppeb=^peb; _process_basic_information=packed RecordReserved1:pointer;    PEBBASEADDRESS:PPEB; RESERVED2:Array[0..1] ofPointer;    Uniqueprocessid:pulong;  Reserved3:pointer; End; Process_basic_information=_process_basic_information; Pprocess_basic_information=^process_basic_information; Processinfoclass=(Processbasicinformation=0, Processwow64information= -  ); NTSTATUS=DWORD;functionntqueryinformationprocess (Processhandle:thandle;  Processinformationclass:processinfoclass;  Processinformation:pointer;  Processinformationlength:ulong; Returnlength:pulong): NTSTATUS; stdcall;External 'Ntdll.dll'Name'ntqueryinformationprocess';var_systemroot:string;procedureGetprocessinfos (varApilist:tprocessinfos);varNcontinueloop:bool;  Nsnapshothandle:thandle;  nprocessentry32:tprocessentry32; Ncount:word;beginNsnapshothandle:= CreateToolhelp32Snapshot (th32cs_snapprocess,0); Trynprocessentry32.dwsize:=SizeOf (NPROCESSENTRY32); Ncontinueloop:=Process32First (Nsnapshothandle, nProcessEntry32); SetLength (Apilist,65536); Ncount:=0;  whileNcontinueloop Do    begin      ifGetprocessinfo (Nprocessentry32.th32processid, @APIList [ncount]) ThenINC (ncount); Ncontinueloop:=Process32Next (Nsnapshothandle, nProcessEntry32); End;  SetLength (Apilist, ncount); finallyCloseHandle (Nsnapshothandle); End;End;functionFindprocess (apathname:string;varapi:tprocessinfo): Boolean;varNcontinueloop:bool;  Nsnapshothandle:thandle;  nprocessentry32:tprocessentry32; Nr:tprocessinfo;beginResult:=False; Nsnapshothandle:= CreateToolhelp32Snapshot (th32cs_snapprocess,0); Trynprocessentry32.dwsize:=SizeOf (NPROCESSENTRY32); Ncontinueloop:=Process32First (Nsnapshothandle, nProcessEntry32);  whileNcontinueloop Do    begin      ifGetprocessinfo (Nprocessentry32.th32processid, @nR) Then      begin        ifSametext (Nr.pathname, Apathname) Then        beginAPI:=NR; Result:=True;        break; End; End; Ncontinueloop:=Process32Next (Nsnapshothandle, nProcessEntry32); End; finallyCloseHandle (Nsnapshothandle); End;End;functionGetprocessinfo (apid:cardinal;  Api:pprocessinfo): Boolean; procedure_curepath (varApath:string); beginApath:=Trim (Apath); ifApath ="'  ThenExit; ifCopy (Apath,1,4) ='\?? \'  Then      {like \?? Path of the \c:\windows\system32\winlogon.exe}Apath:= Copy (Apath,5, Length (Apath))Else ifCopy (Apath,1, A) ='\systemroot\'  Then      {paths similar to \systemroot\system32\smss.exe}Apath:= Includetrailingbackslash (_systemroot) +Extractfilename (Apath); End;varNphandle:thandle;Nstr:string;  nstrc:widestring;  Npbi:process_basic_information;  nr:cardinal;  NPEB:PEB;  Npp:process_parameters;  Ncount:word; Nbuff:Array[0.. MAX_PATH] ofChar;beginResult:=False; ifAPI =Nil  ThenExit; Nphandle:= OpenProcess (process_query_informationorProcess_vm_read, False, APID); ifNphandle =0  ThenExit; Try    ifGetmodulefilenameex (Nphandle,0, @nBuff [0], SizeOf (nbuff)) >0  Thenapi^. PathName:=NbuffElseapi^. PathName:="'; Try      ifNtqueryinformationprocess (Nphandle, Processbasicinformation, @nPBI, SizeOf (NPBI), @nR) <>0  ThenAbort; if  notReadProcessMemory (Nphandle, npbi.pebbaseaddress, @nPEB, SizeOf (NPEB), NR) ThenAbort; if  notReadProcessMemory (Nphandle, Npeb.processparameters, @nPP, SizeOf (NPP), NR) ThenAbort; ifNPP.CommandLine.Length =0  ThenAbort; SetLength (NSTRC, NPP.CommandLine.LengthDiv 2); if  notReadProcessMemory (Nphandle, NPP.CommandLine.Buffer, @nStrC [1], nPP.CommandLine.Length, NR) ThenAbort; api^. CMDName:=nstrc; exceptapi^. CMDName:="'; End; finallyCloseHandle (Nphandle); End; _curepath (api^.  PathName); _curepath (api^.  CMDName); api^. PID:=APID; Result:=True;End;varNbuff:Array[0.. MAX_PATH] ofChar;initializationgetsystemdirectory (Nbuff, SizeOf (Nbuff)); _systemroot:=Nbuff;End.

Get the cell for the list of processes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.