The extracticonex function is used to extract the process icon.
Add several functions to the process class:
Public String getfullpath () // obtain the path of the Process file <br/>{< br/> stringbuilder S = new stringbuilder (); <br/> S. capacity = 100; <br/> process. getmodulefilename (this. processid, S, S. capacity); <br/> string Path = path. getfullpath (S. tostring (); <br/> return path; <br/>}</P> <p> Public icon geticonfromexe () <br/>{< br/> intptr hlargeicon = intptr. zero; <br/> intptr hsmallicon = intptr. zero; <br/> string Path = This. getfullpath (); <br/> try <br/>{< br/> extracticonex (path, 0, ref hlargeicon, ref hsmallicon, 1 ); <br/>}< br/> catch (exception ex) <br/>{< br/> throw new exception (ex. message); <br/>}< br/> icon Ic = NULL; <br/> try <br/> {<br/> Ic = (icon) icon. fromhandle (hsmallicon ). clone (); <br/>}< br/> catch (exception e) <br/>{< br/> return NULL; <br/>}< br/> return IC; <br/>}</P> <p> [dllimport ("coredll. DLL ", charset = charset. auto)] <br/> Public extern static int getmodulefilename (uint hmodule, stringbuilder strfullpath, int nsize); </P> <p> [dllimport ("coredll. DLL ", setlasterror = true)] <br/> Private Static extern intptr extracticonex (string filename, int index, ref intptr hiconlarge, ref intptr hiconsmall, uint nicons); <br/>
Let's take a look at the displayed icon. You need to add imagelist to the listview control of the interface.
Private void refreshlist () <br/>{< br/> processes = process. getprocesses (); </P> <p> listviewitem LVI; </P> <p> listprocess. beginupdate (); <br/> listprocess. items. clear (); <br/> imagelist imagelistsmall = new imagelist (); <br/> int I = 0; </P> <p> foreach (PROCESS p in processes) <br/>{< br/> LVI = new listviewitem (P. processname); <br/> icon IC; <br/> Ic = P. geticonfromexe (); <br/> If (IC! = NULL) <br/>{< br/> imagelistsmall. images. add (IC); <br/> LVI. imageindex = I ++; <br/>}</P> <p> string pid = string. format ("{0: x}", p. processid); <br/> LVI. subitems. add (PID); <br/> LVI. subitems. add (P. threadcount. tostring (); <br/> listprocess. items. add (LVI); <br/>}< br/> listprocess. smallimagelist = imagelistsmall; </P> <p> listprocess. endupdate (); <br/>}</P> <p>
Listprocess is an instance of the listview control.