Use MFC to obtain system processes and list controls

Source: Internet
Author: User

Source code: http://download.csdn.net/detail/nuptboyzhb/4156430

Steps for obtaining a process:

The main functions are as follows:

Createconlhelp32snapshot()

Process32first

Process32next

Closehandle

1. Add the header file # include <tlhelp32.h>

2. Add the following code to the function body:

M_listctrl.deleteallitems ();

Handle hsnapshot; // defines a handle.

Hsnapshot =Createconlhelp32snapshot(Th32cs_snapprocess, 0); // create a System Snapshot

Processentry32 PE;

Process32first(Hsnapshot, & PE );

Cstring STR;

Int I = 0;

Do

{// Display the PE Value

M_listctrl.insertitem (I, PE. szexefile); // Insert Process name

Str. Format ("% d", PE. th32processid );

M_listctrl.setitemtext (I, 1, STR );

Str. Format ("% d", PE. cntusage );

M_listctrl.setitemtext (I, 2, STR );

Str. Format ("% d", PE. th32defaultheapid );

M_listctrl.setitemtext (I, 3, STR );

Str. Format ("% d", PE. th32moduleid );

M_listctrl.setitemtext (I, 4, STR );

Str. Format ("% d", PE. cntthreads );

M_listctrl.setitemtext (I, 5, STR );

Str. Format ("% d", PE. th32parentprocessid );

M_listctrl.setitemtext (I, 6, STR );

Str. Format ("% d", PE. pcpriclassbase );

M_listctrl.setitemtext (I, 7, STR );

I ++;

} While (Process32next(Hsnapshot, & PE); // obtain the next

Closehandle(Hsnapshot); // close

Str. Format ("% d", I );

Setdlgitemtext (idc_cnt, STR );

Problems displayed in the list box:

1. Create a list control and associate it with a clistctrl m_listctrl; member variable. Set the properties of the list control. On the "style" tab, set the view mode to "report arrangement", "top order", and "NONE ".

2. initialize the list control in the oninitdialog () function: insert eight columns to record the information of process entities;

Char * tab [8] = {"process", "PID", "cntusage", "th32defaultheapid ",

"Th32moduleid", "cntthreads", "th32parentprocessid ",

"Pcpriclassbase "};

For (INT I = 0; I <8; I ++)

{

M_listctrl.insertcolumn (I, Tab [I], lvcfmt_left, 80 );

}

3. record the information in the response function of the get button. See the above Code;

4. Obtain the value of the selected item.

Add the onitemchanged message response function to the list control. In the message response function, edit the Code as follows:

Nm_listview * pnmlistview = (nm_listview *) pnmhdr;

// Todo: add your control notification handler code here

For (INT I = 0; I <m_listctrl.getitemcount (); I ++) // traverses the entire list view

{

If (m_listctrl.getitemstate (I, lvis_selected) = lvis_selected) // obtain the selected row

{

Cstring STR = "";

STR = m_listctrl.getitemtext (I, 1); // obtain the second data PID selpid = (DWORD) atoi (STR) of the selected row; // convert the data into numbers

Updatedata (false );

}

}

* Presult = 0;

Note: selpid is a member variable of the custom DWORD type. It is used to record the ID of the selected process and pass the ID number to the end process;

End Process

Handle hprocess;

// Open the process

Hprocess =OpenProcess(Process_terminate, false, selpid );

If (hprocess)

{

If (!Terminateprocess(Hprocess, 0 ))

{

Cstring strerror;

Strerror. Format ("error code: % d", getlasterror ());

Afxmessagebox (strerror, mb_ OK | mb_iconinformation, null );

}

}

Else

{

Cstring strerror;

Strerror. Format ("error code: % d", getlasterror ());

If (getlasterror () = error_access_denied)

Strerror = _ T ("Access Denied! ") + Strerror;

Afxmessagebox (strerror, mb_ OK | mb_iconinformation, null );

}

Sleep (300 );

Onrefresh ();

 

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.