Home > Article > Computer Tutorials > What does a screen window handle mean?
It will be easier to understand if you know what a handle is
Is to get the identification code of this window
The handle is a unique identifier used to distinguish various memory objects. It is a 32-bit integer.
Some are unique to the entire system (such as window handles), and some are unique to the current process or thread
(such as thread handle, the global one has another identifier).
It can be divided into many types in detail, all starting with H. When using it in VB, all use Long.
Common ones include window handle (HWND), device description table handle (HDC), memory handle (HMEM),
File handle, process handle, thread handle, pen type handle (HPEN), font handle (HFONT),
Region handle (HRGN) and so on.
When applying for a handle, resources are occupied, which are divided into three categories: SYSTEM, USER, and GDI.
The resources of WINDOWS are fixed and do not expand with the expansion of memory, so you must release them after use
put.
>
If you only use VB's own code, you will generally not use handles, but if you use API functions,
Most will use it.
In the Windows system, handles (I have always felt that this word is particularly awkward to translate) are divided into three categories: Kernel
Handle, UserHandle and application-defined Handle.
KernelHandle is actually the pointer table index of the Kernel object in the process. The Kernel object includes the process and file
Pieces, signals, etc. However, in order to hide the fact, MS generated a so-called Obsfucator value when the system started.
(actually it should be Obfuscator, MicrosoftBugs(R):), after generating the Handle, differentiate the Handle from this value
or is returned to the application, so the Handles you see are all large and meaningless numbers. These
Handle and index objects are jointly managed by KRNL32.DLL and VMM32.VXD, so they are called Kernel
Handle.
UserHandle is used to mark objects such as windows and DCs. They are real pointers, but they do not point to objects
At the beginning of, there is an offset. Again, these objects are managed by USER32.DLL.
The third type of Handle is just some indexes customized by the application. The specific meaning is related to the application
1. Call GetConsoleTitle() to save the current console window title.
2. Call SetConsoleTitle() to change the console title to a unique title.
3. Call Sleep(40) to ensure that the window title has been updated.
4. Call FindWindow(NULL, uniquetitle) to obtain HWND. This call will return HWND. If the operation fails, NULL will be returned.
5. From step 1, to restore the original window title retrieve the value call SetConsoleTitle().
The HWND of the test result should be tested. For example you can test whether the returned HWND corresponds to the current process calling GetWindowText() on the HWND and compare the result to GetConsoleTitle().
Sample code
The following function retrieves the current console application window handle (HWND). If this function succeeds, the return value is a handle to the console window. If this function fails, the return value is NULL. Some error checking is omitted for brevity.
HWND GetConsoleHwnd(void)
{
#define MY_BUFSIZE 1024 // Buffer size for console window titles.
HWND hwndFound; // This is what is returned to the caller.
char pszNewWindowTitle[MY_BUFSIZE]; // Contains fabricated
// WindowTitle.
char pszOldWindowTitle[MY_BUFSIZE]; // Contains original
// WindowTitle.
// Fetch current window title.
GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE);
// Format a "unique" NewWindowTitle.
wsprintf(pszNewWindowTitle,"%d/%d",
GetTickCount(),
GetCurrentProcessId());
// Change current window title.
SetConsoleTitle(pszNewWindowTitle);
// Ensure window title has been updated.
Sleep(40);
// Look for NewWindowTitle.
hwndFound=FindWindow(NULL, pszNewWindowTitle);
// Restore original window title.
SetConsoleTitle(pszOldWindowTitle);
return(hwndFound);
}
#include
#include
using namespace std;
//Callback function for EnumWindows
BOOL CALLBACK EnumProc(HWND hwnd,LPARAM lparam)
{
vector
pvec->push_back(hwnd);
return TRUE;
}
void main()
{
vector
EnumWindows(EnumProc,(LPARAM)&vec);
}
This is written in a win32 console program project. If you want to use it under MFC, you only need to include
#include
EnumProc is defined before, just add the two sentences in the main function to the button response function.
I tested here and got 407 handles
The above is the detailed content of What does a screen window handle mean?. For more information, please follow other related articles on the PHP Chinese website!