The application programming interface of operating systems of the Windows family is represented by a set of functions. When they are called, various objects can be created (files, processes, threads, synchronization objects, etc.). To provide a sufficiently abstract and unified access to these objects, their identification is performed using descriptors - "impersonal" numerical values.
Necessary
- - a translator from a programming language that allows the use of Windows API;
- - possibly Windows Platform SDK.
Instructions
Step 1
Get window handles. There are many ways to do this. The exact method depends on the end goal.
Use the CreateWindow or CreateWindowEx APIs to create a window. They return a handle on success and NULL on failure.
Search for top-level windows and child windows by various parameters using the FindWindow and FindWindowEx functions, respectively. On successful search, the window handle will be obtained.
Enumerate windows with EnumWindows, EnumChildWindows, EnumThreadWindows functions. The handles of the found windows will be passed as a parameter to the callback function.
Find the handle to the window located at a specific position on the screen. Call one of the functions: WindowFromPoint, ChildWindowFromPoint, or ChildWindowFromPointEx.
Step 2
Get process handles. Create a new process by calling the CreateProcess, CreateProcessAsUser, CreateProcessWithTokenW, or CreateProcessWithLogonW API functions. They all return a handle to the new process in the hProcess field of the PROCESS_INFORMATION structure, the pointer to which should be passed to them as the last parameter.
Find the handle to the process by its known identifier. Use the OpenProcess call. The IDs of all running processes can be obtained, for example, using the CreateToolhelp32Snapshot, Process32First, and Process32Next functions of the Tool Help library.
Retrieve the pseudo handle of the current process using the GetCurrentProcess function.
Step 3
Get descriptors of threads. The CreateThread and CreateRemoteThread functions create threads in their own and someone else's process, respectively, returning their handles. You can open an existing thread using its identifier, having obtained the corresponding descriptor, using the OpenThread function. The pseudo-handle of the current flow is returned when GetCurrentThread is called.
Step 4
Descriptors for files, directories, physical disks, disk volumes, consoles, communication resources (I / O ports), mail slots, and named pipes can be obtained by calling a single function, CreateFile.
Step 5
File-to-memory mapping object descriptors are returned by calls to CreateFileMapping and OpenFileMapping.
Step 6
The CreateMutex, CreateSemaphore, and CreateEvent functions create, and the OpenMutex, OpenSemaphore, and OpenEvent functions open existing synchronization objects (mutexes, semaphores, and events). They all return descriptors.
Step 7
All GDI objects (such as device contexts, fonts, brushes, pencils, hardware dependent and independent bitmaps, DIB sections, etc.) are manipulated through their descriptors. The functions for creating GDI objects are numerous and should be consulted on the MSDN section for information on them.
Step 8
A descriptor obtained in one process, as a rule, cannot be used in another. However, in some cases it is possible to obtain a duplicate descriptor corresponding to the primary object. Call the DuplicateHandle API to duplicate the handle. This can be used, for example, to share unnamed synchronization objects or channels between multiple processes.