Windows Mobile process viewer development (1) -- obtain information about the currently running process

Source: Internet
Author: User

The development of process viewer in Windows Mobile is not too difficult. There are also many examples on the Internet for reference. I am too lazy to write too many explanations. In fact, I cannot explain it clearly, because I am still not very clear about it myself, let's take a look at my ideas by writing a blog. The current process Viewer interface is as follows:

 

Stay a little bit, cable performance running.

 

First, process viewer of course, the most important thing is to obtain information about all processes currently running. Here I list the process names, PIDs, and the number of threads in the process, I also wanted to give the CPU and memory used by the process. Later I found that there was no ready-made API function to call, that is, I had to calculate it myself, and the calculation results were not very accurate, so I will study this part of content later ~

 

Let's take a look at the specific code for getting the current process information table:

 

What is important here is:

 

Createconlhelp32snapshot: The function used to obtain a snapshot of the current process.

Process32first: function for obtaining the handle of the first process

Process32next: function for obtaining the handle of the next process

Closetoolhelp32snapshot: Disable snapshots

Processentry32: a struct used to store snapshot Process Information

 

For the specific usage of the above functions, refer to msdn

 

Class Process <br/>{< br/> private string processname; <br/> private uint processid; <br/> private int threadcount; <br/> public process () <br/>{</P> <p >}</P> <p> private process (uint32 processid, string processname, int threadcount) <br/>{< br/> This. processid = processid; <br/> This. processname = processname; <br/> This. threadcount = threadcount; <br/>}</P> <p> Public override string tostring () <br/>{< br/> return processname; <br/>}</P> <p> Public uint processid <br/>{< br/> get {return processid ;} <br/>}</P> <p> Public String processname <br/>{< br/> get {return processname ;} <br/>}</P> <p> Public int threadcount <br/>{< br/> get {return threadcount ;} <br/>}</P> <p> Public static process [] getprocesses () // obtain the current process information table <br/>{< br/> arraylist proclist = new arraylist (); <br/> intptr handle = createmedilhelp32snapshot (th32cs_snapprocess | th32cs_snapnoheaps, 0 ); <br/> int n = 0; <br/> If (INT) handle> 0) <br/>{< br/> try <br/> {<br/> processentry32 pecurrent; <br/> processentry32 pe32 = new processentry32 (); <br/> byte [] pebytes = pe32.tobytearray (); <br/> int retval = process32first (handle, pebytes ); </P> <p> while (retval = 1) <br/> {<br/> pecurrent = new processentry32 (pebytes); <br/> pecurrent. szexefile = pecurrent. szexefile. replace ("/0", ""); </P> <p> process proc = new process (uint32) pecurrent. PID), pecurrent. szexefile, (INT) pecurrent. threadcount); <br/> proclist. add (Proc); </P> <p> retval = process32next (handle, pebytes); <br/>}< br/> catch (exception ex) <br/>{< br/> throw new exception ("exception:" + ex. message); <br/>}< br/> closetoolhelp32snapshot (handle); <br/> return (process []) proclist. toarray (typeof (process); <br/>}< br/> else <br/>{< br/> throw new exception ("unable to get Process "); <br/>}</P> <p> public class processentry32 <br/> {<br/> private const int sizeoffset = 0; <br/> private const int usageoffset = 4; <br/> private const int processidoffset = 8; <br/> private const int defaultheapidoffset = 12; <br/> private const int moduleidoffset = 16; <br/> private const int threadsoffset = 20; <br/> private const int parentprocessidoffset = 24; <br/> private const int priclassbaseoffset = 28; <br/> private const int dwflagsoffset = 32; <br/> private const int exefileoffset = 36; <br/> private const int memorybaseoffset = 556; <br/> private const int accesskeyoffset = 560; <br/> private const int size = 564; <br/> private const int max_path = 260; </P> <p> Public uint dwsize; <br/> Public uint cntusage; <br/> Public uint th32processid; <br/> Public uint th32defaultheapid; <br/> Public uint th32moduleid; <br/> Public uint cntthreads; <br/> Public uint th32parentprocessid; <br/> Public long pcpriclassbase; <br/> Public uint dwflags; <br/> Public String szexefile; <br/> Public uint th32memorybase; <br/> Public uint th32accesskey; </P> <p> Public processentry32 () <br/>{< br/>}</P> <p> Public processentry32 (byte [] aData) <br/>{< br/> dwsize = getuint (aData, sizeoffset); <br/> cntusage = getuint (aData, usageoffset ); <br/> th32processid = getuint (aData, processidoffset); <br/> response = getuint (aData, defaultheapidoffset); <br/> th32moduleid = getuint (aData, moduleidoffset ); <br/> cntthreads = getuint (aData, threadsoffset); <br/> th32parentprocessid = getuint (aData, parentprocessidoffset); <br/> pcpriclassbase = (long) getuint (aData, priclassbaseoffset); <br/> dwflags = getuint (aData, dwflagsoffset); <br/> szexefile = getstring (aData, exefileoffset, max_path ); <br/> th32memorybase = getuint (aData, memorybaseoffset); <br/> th32accesskey = getuint (aData, accesskeyoffset ); <br/>}</P> <p> # region conversion functions <br/> Private Static uint getuint (byte [] aData, int offset) <br/>{< br/> return bitconverter. touint32 (aData, offset); <br/>}</P> <p> Private Static void setuint (byte [] aData, int offset, int value) <br/> {<br/> byte [] buint = bitconverter. getbytes (value); <br/> buffer. blockcopy (buint, 0, aData, offset, buint. length); <br/>}</P> <p> Private Static ushort getushort (byte [] aData, int offset) <br/>{< br/> return bitconverter. touint16 (aData, offset); <br/>}</P> <p> Private Static void setushort (byte [] aData, int offset, int value) <br/> {<br/> byte [] bushort = bitconverter. getbytes (short) value); <br/> buffer. blockcopy (bushort, 0, aData, offset, bushort. length); <br/>}</P> <p> Private Static string getstring (byte [] aData, int offset, int length) <br/>{< br/> string sreturn = encoding. unicode. getstring (aData, offset, length); <br/> return sreturn; <br/>}</P> <p> Private Static void setstring (byte [] aData, int offset, string value) <br/>{< br/> byte [] arr = encoding. ASCII. getbytes (value); <br/> buffer. blockcopy (ARR, 0, aData, offset, arr. length); <br/>}< br/> # endregion </P> <p> Public byte [] tobytearray () <br/>{< br/> byte [] aData; <br/> aData = new byte [size]; <br/> setuint (aData, sizeoffset, size ); <br/> return aData; <br/>}</P> <p> Public string name <br/>{< br/> get {return szexefile. substring (0, szexefile. indexof ('/0') ;}< br/>}</P> <p> Public ulong PID <br/>{< br/> get {return th32processid ;} <br/>}</P> <p> Public ulong baseaddress <br/>{< br/> get {return th32memorybase ;} <br/>}</P> <p> Public ulong threadcount <br/>{< br/> get {return cntthreads ;} <br/>}</P> <p> private const int th32cs_snapprocess = 0x00000002; <br/> private const int th32cs_snapnoheaps = 0x40000000; </P> <p> [dllimport ("toolhelp. DLL ")] <br/> Public static extern intptr createconlhelp32snapshot (uint flags, uint processid); <br/> [dllimport (" toolhelp. DLL ")] <br/> Public static extern int closetoolhelp32snapshot (intptr handle); <br/> [dllimport (" toolhelp. DLL ")] <br/> Public static extern int process32first (intptr handle, byte [] PE); <br/> [dllimport (" toolhelp. DLL ")] <br/> Public static extern int process32next (intptr handle, byte [] PE); </P> <p>}

 

Do not forget dllimport, because the above functions are not APIs in C #.

 

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.