How to find running tasks on the System

Source: Internet
Author: User
How to find running tasks on the System

Note: This articleArticleI am from Netease, but it seems like I did not know what this guy entered in the <win95api developer guide>, but many netizens will appreciate it.
Problem
YesProgramThe member wants to list the currently running tasks to the user,
Don't be so sure. Can I list them at the same time? You still need to determine which one to display?

Method
Listing tasks and Windows is quite useful. The task is a running Windows application.
Whether the application displays a window. A task can also display several windows. The main window is the parent window.
Its window is the Child Window of the parent window. The child window is usually displayed in the parent window, but it can also be displayed in the parent window.
. Whether displayed in the parent window or outside the parent window, all child windows belong to the same
Tasks.

Procedure
Follow these steps to implement an example program. Run this subroutine and select a dish from the menu tasks.
Single View tasks. A dialog box is displayed. the dialog box contains a list box and four buttons. Select the button.
Process. The names of all currently running tasks are displayed in the list box.
Not all tasks are displayed in a window. Select the second button for Windows, which will be displayed in the list box
Names of all currently running Windows. Select the modules button, and the list box displays all
The loaded module. Click Close to close the dialog box.

The procedure for implementing the example program is as follows:
1. in Visual C ++, use Appwizard to create a new project file and name this project
Ld31.mak.
2. Go to appstudio and create a new dialog box. Add a list box and three buttons.
The titles are processes, windows, and modules, and the IDs are id_process_list and I
D_window_list and id_module_list. Change the title of the OK button to close and delete canc
El button. Change the title of the dialog box to Application List.
3. Enter classwizard to generate a new dialog box class named ctaskdlg. Slave
Select ctaskdlg from the object list, and select message wm_initdialog from the message list. Click
Add function, enter the following in method oninitdialogCode:

Bool ctaskdlg: oninitdialog ()
{
Cdialog: oninitdialog ();

Centerwindow ();

If (! Inittoolhelp32 ())
{
MessageBox ("unable to initialize toolhelp functions! ",
"Error", mb_ OK | mb_iconstop );
Enddicel (idcancel );
Return false;
}

Return true; // return true unless you set the focus to a control

}

4. Select id_process_list from the object list, and select message bn_clicke from the message list.
D. Name onprocesslist and add the following code to the method:

Void ctaskdlg: onprocesslist ()
{
Handle hsnapshot = pcreatetoolhelp32snapshot (th32cs_snapprocess, 0 );

Processentry32 PE;

If (! Hsnapshot)
Return;

Clistbox * List = (clistbox *) getdlgitem (idc_list1 );
List-> resetcontent ();

PE. dwsize = sizeof (PE );

For (INT I = pprocess32first (hsnapshot, & PE); I;
I = pprocess32next (hsnapshot, & PE ))
{
Handle hmodulesnap = NULL;
Moduleentry32 me;

Hmodulesnap = pcreatetoolhelp32snapshot (th32cs_snapmodule,
PE. th32processid );
If (hmodulesnap = (handle) (-1 ))
Return;
Me. dwsize = sizeof (moduleentry32 );
If (pmodule32first (hmodulesnap, & Me ))
{
Do
{
If (Me. th32moduleid = PE. th32moduleid)
{
List-> addstring (Me. szexepath );
Break;
}
} While (pmodule32next (hmodulesnap, & Me ));
}
}
Closehandle (hsnapshot );
}

5. Select id_window_list from the object list, and select message bn_clicked from the message list.
And add the following code to the onwindowlist method:

Void ctaskdlg: onwindowlist ()
{
Clistbox * List = (clistbox *) getdlgitem (idc_list1 );
List-> resetcontent ();

Farproc enumprocinstance = makeprocinstance (
(Farproc) enumwindowsproc, AfxGetInstanceHandle ());

Enumwindows (wndenumproc) enumprocinstance, (lparam) list );

Freeprocinstance (enumprocinstance );

Updatedata ();
}

6. Add the following code before the onwindowlist method:

Bool callback enumwindowsproc (hwnd, lparam)
{
Clistbox * List = (clistbox *) lparam;

Char Buf [256];
Getwindowtext (hwnd, Buf, 256 );

If (strlen (BUF ))
List-> addstring (BUF );

Return true;
}

7. Select id_module_list from the object list, and select message bn_clicked from the message list.
And add the following code to the onmodulelist method:

Void ctaskdlg: onmodulelist ()
{
Moduleentry32 me;

Clistbox * List = (clistbox *) getdlgitem (idc_list1 );
List-> resetcontent ();

Memset (& Me, 0, sizeof (me ));
Me. dwsize = sizeof (moduleentry32 );
Handle hsnapshot = pcreatetoolhelp32snapshot (th32cs_snapprocess, 0 );

If (! Hsnapshot)
Return;

For (INT I = pmodule32first (hsnapshot, & Me); I;
I = pmodule32next (hsnapshot, & Me ))
List-> addstring (Me. szexepath );

Closehandle (hsnapshot );
}

8. Add the following code at the top of the file taskdlg. cpp:

Typedef bool (winapi * modulewalk) (handle hsnapshot,
Lpmoduleentry32 lpme );
Typedef bool (winapi * threadwalk) (handle hsnapshot,
Lpthreadentry32 lpte );
Typedef bool (winapi * processwalk) (handle hsnapshot,
Lpprocessentry32 lppe );
Typedef handle (winapi * createsnapshot) (DWORD dwflags,
DWORD th32processid );

Static createsnapshot pcreatetoolhelp32snapshot = NULL;
Static modulewalk pmodule32first = NULL;
Static modulewalk pmodule32next = NULL;
Static processwalk pprocess32first = NULL;
Static processwalk pprocess32next = NULL;
Static threadwalk pthread32first = NULL;
Static threadwalk pthread32next = NULL;

Bool inittoolhelp32 (void)
{
Bool Bret = false;
Hinstance hkernel = NULL;

Hkernel = getmodulehandle ("kernel32.dll ");
If (hkernel)
{
Pcreatetoolhelp32snapshot =
(Createsnapshot) getprocaddress (hkernel,
"Createconlhelp32snapshot ");

Pmodule32first = (modulewalk) getprocaddress (hkernel,
"Module32first ");
Pmodule32next = (modulewalk) getprocaddress (hkernel,
"Module32next ");

Pprocess32first = (processwalk) getprocaddress (hkernel,
"Process32first ");
Pprocess32next = (processwalk) getprocaddress (hkernel,
"Process32next ");

Pthread32first = (threadwalk) getprocaddress (hkernel,
"Thread32first ");
Pthread32next = (threadwalk) getprocaddress (hkernel,
"Thread32next ");

Bret = pmodule32first & pmodule32next & pprocess32first &&
Pprocess32next & pthread32first & pthread32next &&
Pcreatetoolhelp32snapshot;
}
Else
Bret = false;

Return Bret;
}

9. Finally, add the following lines at the top of the file taskdlg. cpp:
# Include "tlhelp32.h"
10. Go to appstudio and add a new menu tasks in the menu idr_wainframe.
Add a new menu item "view tasks" to asks. The ID is named "id_view_tasks.
11. Start classwizard. Select the object cmainframe from the drop-down list, from the object list
Select id_view_tasks, select the message command, and click the Add function button. The method is named
Onviewtasks.
12. Enter the following code in the onviewtasks member function of cmainframe:

Void cmainframe: onviewtasks ()
{
Ctaskdlg DLG;

DLG. domodal ();
}

13. Add the following lines at the top of the mainfrm. cpp file:
# Include "taskdlg. H"
14. Compile and run this subprogram.

note
This section describes three different Windows API function sets, used to list the currently running tasks
, running Windows, and mounted DLL.
the Windows 9x API functions process32first and process32next are used to list the processes currently running.
. To use these functions, you must install these functions from kernel32.dll to
initialize the toolhelp function. The inittoolhelp32 function is used to complete the initialization of tollhelp
. These API functions traverse the process linked list and return the process identifier of each process.
call the module function (module32first and module32next) to traverse the module linked list, find the
process name.
the Windows API functions makeprocinstance and enumwindows are used to list the Windows currently running
. In the example program, the makeprocinstance function accepts the address of the function enumwindowspr
oC, returns the value of the farproc type, and then uses the function enumwindows to pass the returned value
to Windows, to list all the main windows. The enumwindowsproc function has two parameters.
the first parameter is the window handle, and the second parameter is the user-defined parameter, which is transferred by the function enumwindows
. In this example, this parameter is used by the clistbox instance to store data. In the function enumw
indowsproc (called by Windows), the title of the window is displayed in the
table box for users to view.

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.