In VC, enumerate all tasks and organize some documents of the task manager.

Source: Internet
Author: User

1. List all tasks

Call the enumwindows function.
Bool enumwindows (
Wndenumproc lpenumfunc, // callback function
Lparam // application-defined value
);
For example:
: Enumwindows (wndenumproc) enumproc, (lparam) This );

Enumproc is the callback function. All the real operations are implemented in this function.
The second parameter is user-defined transfer. For example, it can be a pointer of the dialog box.

The prototype of enumproc is
Bool callback cplistdlg: enumproc (hwnd, lparam)
If it is a class member function, it must be a static function. The declaration is as follows:
Bool static callback enumproc (hwnd, lparam );
Where, the hwnd is the window handle.

Bool callback cplistdlg: enumproc (hwnd, lparam)
{
Cplistdlg * pdlg = (cplistdlg *) lparam;

If (hwnd = NULL)
{
Return false;
}

If (hwnd = pdlg-> m_hwnd) // This is to avoid listing this program.
{
Return true;
}

If (: iswindow (hwnd) &: iswindowvisible (hwnd) & (getwindowlong (hwnd, gwl_exstyle) & ws_ex_toolwindow )!

= Ws_ex_toolwindow )&&
(Getwindowlong (hwnd, gwl_hwndparent) = 0 ))
{
Tchar szcap [255] = {0 };

:: Getwindowtext (hwnd, szcap, 255 );

If (strlen (szcap) = 0)
{
Return true;
}
If (lstrcmp (_ T ("program manager"), szcap) = 0)
{
Return true;
}

DWORD dwprocessid = 0;
: Getwindowthreadprocessid (hwnd, & dwprocessid );

Trace ("ID = % d, name = % s/n", dwprocessid, szcap );


}
Return true;

}

Getwindowthreadprocessid is used to obtain the ID of the worker.

If (lstrcmp (_ T ("program manager"), szcap) = 0)

This is to differentiate the Program Manager program. I don't know why, but I will list this item, dizzy.

 

2. Get the window icon

Hicon = NULL;

Hicon = (hicon): getclasslong (hwnd, gcl_hiconsm );

If (hicon = NULL)
{
Hicon = (hicon): getclasslong (hwnd, gcl_hicon );
}

If (hicon = NULL)
{
Hicon = (hicon): sendmessage (hwnd, wm_geticon, icon_small, 0 );
}

If (hicon = NULL)
{
Hicon = (hicon): sendmessage (hwnd, wm_geticon, icon_big, 0 );
}

I remember that when I was doing this, I had the code because some window icons were not available and I had to use the above method before I could get all of them.

3. Check whether the window is running normally or not.
First, define two function pointers,
Typedef bool (winapi * procishungappwindow) (hwnd );
Typedef bool (winapi * procishungthread) (DWORD );

Then define
Procishungappwindow m_pishungappwindow;
Procishungthread m_pishungthread;

Define a bool type to determine whether the current operating system is Windows NT/2000 or above
The methods for determining whether a program runs normally are different for different operating systems

Bool m_bisnt;

Get version information
Osversioninfo osver = {0 };

Osver. dwosversioninfosize = sizeof (osversioninfo );
If (! Getversionex (& osver ))
{
Bretval = false;
}

If (bretval = true)
{
If (osver. dwplatformid & ver_platform_win32_nt)
{
M_bisnt = true;
}
Else
{
M_bisnt = false;
}

}

Get the two function pointers
Hmodule huser32 =: getmodulehandle ("USER32 ");
 
If (! Huser32)
{
Bretval = false;
}

If (bretval = true)
{
M_pishungappwindow = (procishungappwindow)
Getprocaddress (huser32,
"Ishungappwindow ");

M_pishungthread = (procishungthread) getprocaddress (huser32,
"Ishungthread ");

If (! M_pishungappwindow &&! M_pishungthread)
{
Bretval = false;
}

}

The system determines whether the window is running normally or not.
The Code is as follows:

If (m_bisnt = true)
{
Bool bishung = m_pishungappwindow (hwnd );
If (bishung)
{
// No Response
}
Else
{
// Running
}
}
Else
{
Bool bishung = m_pishungthread (getwindowthreadprocessid (hwnd, null ));
If (bishung)
{
// No Response
}
Else
{
// Running
}
}

4. End the task
: Postmessage (hwnd, wm_close, 0, 0 );

However, you may not be able to close the window when you call this function. For example, if the window does not respond, it cannot be closed.
So we have to continue to find a solution.

: Postmessage (hwnd, wm_close, 0, 0 );
: Sleep (300 );
// If the window is not closed, continue to find a solution
If (: iswindow (hwnd ))
{
DWORD dwprocessid = 0;
: Getwindowthreadprocessid (hwnd, & dwprocessid );

If (terminateapp (dwprocessid, 500 )! = Ta_failed)
{
// The operation is successful.
}

}

Terminateapp is like this.

DWORD winapi cplistdlg: terminateapp (DWORD dwpid, DWORD dwtimeout)
{
Handle hproc;
DWORD dwret;
 
// If we can't open the process with process_terminate rights,
// Then we give up immediately.
Hproc =: OpenProcess (synchronize | process_terminate, false,
Dwpid );
 
If (hproc = NULL)
{
Return ta_failed;
}
 
// Terminateappenum () posts wm_close to all windows whose PID
// Matches your process's.
Enumwindows (wndenumproc) terminateappenum, (lparam) dwpid );
 
// Wait on the handle. If it signals, great. If it times out,
// Then you kill it.
If (waitforsingleobject (hproc, dwtimeout )! = Wait_object_0)
Dwret = (terminateprocess (hproc, 0 )? Ta_success_kill: ta_failed );
Else
Dwret = ta_success_clean;
 
Closehandle (hproc );
 
Return dwret;

}

Another callback function

Bool callback cplistdlg: terminateappenum (hwnd, lparam)
{
DWORD dwid;

: Getwindowthreadprocessid (hwnd, & dwid );

If (dwid = (DWORD) lparam)
{
: Postmessage (hwnd, wm_close, 0, 0 );
}

Return true;

}
Prototype is
Bool static callback terminateappenum (hwnd, lparam );

 

This is the Code related to a previous Task Manager, which has many shortcomings and has not been improved. This time, we sorted it out. If there is something wrong, hope you can get your corrections ~~~

For details about how to display the information, for example, in listctrl, I will not talk about it much. Refer to another article in the blog, "using listctrl in VC (1 )"

References
How to determine the state of a program ........................
Http://www.vckbase.com/bbs/prime/viewprime.asp? Id = 334

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.