Windows client Development-must be clear about the differences between HWND, HANDLE, hmodule, hinstance

Source: Internet
Author: User
Tags see definition

If I don't talk first, I'll take a look at some code snippets

1 Using HWND
Find window:

HWND h_wnd = ::FindWindow(_T("MainForm"), NULL);

To create a window:

HWND hwnd = WindowEx::Create(NULL, L"", WS_POPUP, WS_EX_TOOLWINDOW);

2 Using handle
Singleton run:

BOOL Singletonrun (const wchar_t*application) {assert (application);if(Application== NULL)return false;HANDLEHmutex= :: Createmutexw(NULL,TRUE, application);if(Hmutex== NULL)return false;if(:: GetLastError()==Error_already_exists) {:: CloseHandle(Hmutex);return false; }/ * This mutex is opened without control until the process exits * /    return true;}

Get File Size:

int64_t GetFileSize(const PathString &filepath){    WIN32_FIND_DATAW file_data;    HANDLE file = FindFirstFileW(filepath.c_str(), &file_data);    if (file == INVALID_HANDLE_VALUE)        return -1;    LARGE_INTEGER li = { file_data.nFileSizeLow, file_data.nFileSizeHigh };    FindClose(file);    return li.QuadPart;}

3 using Hmodule
To load DLLs dynamically:

HMODULE moudle_handle = ::LoadLibraryW(L"shell32.dll");

Get Module directory:

std::wstring GetModuleDirectory(HMODULE module_handle){    //DCHECK(IsModuleHandleValid(module_handle));    std::wstring module_directory;    if (FilePathApartDirectory(GetModulePathName(module_handle), module_directory))        return module_directory;    return L"";}

4 using HInstance
Open Directory:

HINSTANCE inst = ::ShellExecute(NULL, L"open", dir.c_str(), NULL, NULL, SW_SHOW);
What is defined on Windows

1 HWND
A handle to a window

typedef HANDLE HWND;

2 HANDLE
A handle to an object

typedef PVOID HANDLE;

3 hmodule
A handle to a module. The the base address of the module in memory.

typedef HINSTANCE HMODULE;

See the definition, the answer is clear:
Hmodule and hinstance only differ on 16-bit Windows systems, otherwise there is no difference.

4 hinstance
A handle to an instance. The the base address of the module in memory.

typedef HANDLE HINSTANCE;

At this moment, do not know that you have no, anyway I was ignorant, see definition, HWND, HANDLE, hmodule, hinstance these four things are not the same?!!!

That is to say, the difference between HWND, HANDLE, hmodule, HINSTANCE is not on the variable type, but on the semantics.

What difference does it really have?

The HWND is thread-dependent and you can find the process and thread that the window belongs to through the HWND

Handle is a kernel object that represents the system, such as a file handle, a thread handle, and a process handle.
The system manages the kernel objects in the form of a list, loaded into every memory in
The kernel object has a linear address, and relative to the system, there is an index position in the string column, which is the handle of the core object.

The essence of HINSTANCE is the base address of the module, which only makes sense in the same process, and the hinstance of the cross-process is meaningless.

Hmodule is the module that is loaded on behalf of the application, which is usually the linear address that is loaded into the module under the Win32 system.

HINSTANCE is the same thing as Hmodule under Win32 (only on 16-bit Windows, which is different).

Windows client Development-must be clear about the differences between HWND, HANDLE, hmodule, hinstance

Related Article

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.