Instance handle, handle

Source: Internet
Author: User

Instance handle, handle

In Windows applications, application files are always loaded into the system memory. to identify the differences between each file in the memory, Windows uses a handle to identify these files. For example, if an application contains an executable file (EXE) and multiple dynamic connection library files (DLL), the executable file can be identified as 0x1D000000, the dynamic connection library file is 0x1D001000. The value here is just an example. When the system runs the application, the actual value is allocated to the application. Each executable or DLL file loaded into the process address space is assigned a unique instance handle. The instance handle of the executable file is the first hInstance parameter of the (w) WinMain function. Pass this parameter to the Windows application. In this way, you can get the Windows application instance handle from this parameter in the C language. This parameter is used in many functions. For example, to load an icon resource from an executable file:

HICON LoadIcon (HINSTANCE hInstance, PCTSTR pszIcdon)

The first hInstance parameter here is the Windows application instance handle. If you want to load the icon from the EXE file, you can pass in the EXE file handle. If you want to load the icon from the DLL file, the DLL file handle is passed in. The hInstance parameter is actually a memory base address. The system loads the executable file image to the location of the process address space. However, in a Python application, its entry function cannot directly obtain the instance handle parameter. You can only think of another method. There is no way to go back and forth. You can find two related API functions in the Win32 API: GetModuleHandle and GetModuleHandleEx. Both functions can obtain the instance handle of the file. The difference between the two functions is that GetModuleHandle can be used to obtain the handle of a single thread without increasing the reference count of the module. GetModuleHandleEx can be used to obtain the multi-thread handle, you can increase the reference count of the module. Therefore, each time this function is called, the reference count of the database is released. In Python applications, you can use these two functions to obtain the instance handle, as shown in the following example:

# Windows Applications

# Developer: Cai junsheng (QQ: 9073204) Shenzhen

From ctypes import *

From ctypes. wintypes import *

 

HInst = windll. kernel32.GetModuleHandleW (None)

Print ('hinst: 0x % x' % hInst)

In this example, you may find that the ctypes. wintypes module is imported. What is wintypes used? We have learned from the previous study that ctypes can use the types in ctypes, but remember that there are many types when converting to WIN32 APIs. Fortunately, wintypes has helped us to set basic common types, such as BYTE, WORD, DWORD, UINT, BOOL, LPCWSTR, COLORREF, HANDLE, HWND, and RECT.

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.