C ++ Builder master advanced (7) Design System Environmental Monitoring Procedure

zhaozj2021-02-16  44

C builder master advanced

nxyc_twz@63.com

(7) How to Design System Environment Monitoring Procedures

Do you want to master system resources in real time? For example, current windows, disks and memory usage, devices and environment variable settings, running programs and boot launchers, etc. Through my series, I believe that you must understand this information in depth, and can control it by programming.

First, how to get the current window

Here you need an API function: enumwindows.

Function function: This function enumerates the top window on all screens, and the method is to pass the handle to each window and then transfer it to the application defined callback function. The EnumThreadWindows function continues until all top window enumeration complete or callback functions Returns FALSE Function prototype: BOOL EnumWindows (WndenumProc Lpenc, LParam LPARAM); Parameters: LpenumFunc: Point to an application-defined callback function pointer, please see EnumWindowsProc. LPARARM: Specifies a defined value that passes to the callback function. Return Value: If the function is successful, the return value is non-zero; if the function fails, the return value is zero. To get more error messages, call the getLastError function. Remarks: The EnumWindows function is not listed in the sub-window. Calling this function in the cyclic body is more reliable than calling the getWindow function. Calling an application that performing this task in a getWindow function may fall into a handle of a dead loop or point to a window that has been destroyed.

I wrote the callback function: BOOL __STDCALL ENUMPROC (HWND HWND, Long LP); // Detailed definitions are as follows:

BOOL __STDCALL ENUMPROC (HWND HWND, LONG LP)

{

If (hwnd == null) Return False; // If the specified window handle is empty, exit Char Title [60]; // Save Window Title Char HWndStr [10]; // Save Window Handle Char ClassName [60]; / / Save Class Name TListItem * mitem; // Used to store window information: window title, handle and class name getWindowText (hwnd, title, 60); // get the window title IF of the specified window handle (ANSISTRING (Title)! = "&& Ansistring (Title)! =" Default IME ")

{// If the window title is not empty and is not a input method title, then mitem = mainform-> listview1-> items-> add (); // Add this window information to the list Sprintf (HWndStr, "% 08X", HWnd ); // convert the window handle to 16 Enter MITEM-> CAPTION = Ansistring (Title); MITEM-> SubItems-> add (ansistring (hwndstr)); // Add window handle getClassName (hwnd, classname, 60); // Get the class name mitem-> Subitems-> add (ansistring (classname)); // Add class name} return true;}

Get the current window: Enumwindows ((WndenumProc) Enumproc, 0); "Not complete"

转载请注明原文地址:https://www.9cbs.com/read-26432.html

New Post(0)