Questions tagged [hwnd]

The Microsoft Windows operating environment identifies each form in an application by assigning it a handle, or hWnd. The hWnd property is used with Windows API calls. Many Windows operating environment functions require the hWnd of the active window as an argument.

In computer programming, a handle is an abstract reference to a resource. Handles are used when application software references blocks of memory or objects managed by another system, such as a database or an operating system.

While a pointer literally contains the address of the item to which it refers, a handle is an abstraction of a reference which is managed externally; its opacity allows the referent to be relocated in memory by the system without invalidating the handle, which is impossible with pointers. The extra layer of indirection also increases the control the managing system has over operations performed on the referent. Typically the handle is an index or a pointer into a global array of tombstones.

Handles were a popular solution to memory management in operating systems of the 1980s, such as Mac OS and Windows. Unix file descriptors are essentially handles. Like other desktop environments, the Windows API heavily uses handles to represent objects in the system and to provide a communication pathway between the operating system and user space. For example, a window on the desktop is represented by a handle of type HWND (handle, window).

Doubly indirect handles have fallen out of favour in recent times, as increases in available memory and improved virtual memory algorithms have made the use of the simpler pointer more attractive. However, many operating systems still apply the term to pointers to opaque, "private" data structures—opaque pointers—or to indexes into internal arrays passed from one process to its client.

A handle leak is a type of software bug that occurs when a computer program asks for a handle to a resource but does not free the handle when it is no longer used.

Wikipedia Article

397 questions
58
votes
12 answers

Loading a WPF Window without showing it

I create a global hot key to show a window by PInvoking RegisterHotKey(). But to do this I need that window's HWND, which doesn't exist until the window is loaded, that means shown for the first time. But I don't want to show the window before I can…
svick
  • 236,525
  • 50
  • 385
  • 514
54
votes
7 answers

How do I get the handle of a console application's window

Can someone tell me how to get the handle of a Windows console application in C#? In a Windows Forms application, I would normally try this.Handle.
Grant
  • 11,138
  • 32
  • 94
  • 140
32
votes
5 answers

How can I tell if a window has focus? (Win32 API)

Using the Win32 API (in C, but that's inconsequential), how can I tell if a given window (identified by HWND) has focus? I'm hooking an application watching for an event, and when that event occurs I want to check if the application already has…
Daniel Jennings
  • 6,304
  • 3
  • 32
  • 42
29
votes
6 answers

In Java Swing how do you get a Win32 window handle (hwnd) reference to a window?

In Java 1.4 you could use ((SunToolkit) Toolkit.getDefaultToolkit()).getNativeWindowHandleFromComponent() but that was removed. It looks like you have to use JNI to do this now. Do you have the JNI code and sample Java code to do this? I need this…
Sarel Botha
  • 12,419
  • 7
  • 54
  • 59
28
votes
3 answers

Difference between HANDLE and HWND in Windows API?

I'm trying to use function SetForegroundWindow(HWND hWnD). I have some handles but it's not working as parameter of above function. My handle is a thread and I want to run it in foreground. What are the differences between a HWND and a HANDLE?
Thangnv
  • 805
  • 3
  • 11
  • 22
23
votes
1 answer

How can I tell if a given hWnd is still valid?

I'm using a third-party class that spawns an instance of Internet Explorer. This class has a property, hWnd, that returns the hWnd of the process. Later on down the line, I may want to reuse the instance of the application if it still exists, so I…
Ian P
  • 12,840
  • 6
  • 48
  • 70
21
votes
5 answers

Is the order in which handles are returned by EnumWindows meaningful?

From a couple of preliminary tests it seems that EnumWindows always returns windows in reverse instantiation order, i.e. most recently instantiated window first. Is that a valid observation? If so, is it true across all versions of Windows? And is…
Oliver Giesen
  • 9,129
  • 6
  • 46
  • 82
17
votes
4 answers

Global hotkey with WIN32 API?

I've been able to set local hotkeys like this RegisterHotKey(hwndDlg, 100, MOD_ALT | MOD_CONTROL, 'S'); How can I set the hotkey to be global? I want it to be there even when my window is hidden.
Mars
  • 4,197
  • 11
  • 39
  • 63
17
votes
1 answer

Custom Messages in Non-Windowed Classes - need a default handler?

With a class (TObject) I have : private FHwnd : HWND; procedure HandleMyMessage(var Message : TMessage); message TH_MYMESSAGE; where TH_MYMESSAGE = WM_USER + 1 In the class constructor: FHwnd := AllocateHWND(HandleMyMessage); The only object…
J...
  • 30,968
  • 6
  • 66
  • 143
16
votes
4 answers

QT5 QPlatformNativeInterface and HWND

In one of the answers to Get HWND on windows with Qt5 (from WId) it is suggested to employ QPlatformNativeInterface in order to recover the native window handler. To access the QT header though the example uses its full path: #include…
Pierluigi
  • 2,212
  • 1
  • 25
  • 39
15
votes
1 answer

"The operation completed successfully" exception

I have a custom method that finds the largest size to use for a given string and font to fill a given box without cutting off the text. To test it, I created a service that cycles through a few different strings and a few different fonts and does…
user253667
15
votes
4 answers

Get hwnd by process id c++

How can I get the HWND of application, if I know the process ID? Anyone could post a sample please? I'm using MSV C++ 2010. I found Process::MainWindowHandle but I don't know how to use it.
Luke
  • 2,350
  • 6
  • 26
  • 41
15
votes
1 answer

What is the purpose of hPrevInstance in WinMain

The definition of WinMain is: int CALLBACK WinMain( _In_ HINSTANCE hInstance, _In_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nCmdShow ); What I understand is: hInstance is a handle to the application's…
ApprenticeHacker
  • 21,351
  • 27
  • 103
  • 153
13
votes
4 answers

What is the lifetime of a CWnd obtained from CWnd::FromHandle?

According to msdn, when I get a CWnd* with CWnd::FromHandle, The pointer may be temporary and should not be stored for later use. What is meant by "later use" is not clear to me. Is it only the scope of the current method? As far as I know, there…
remio
  • 1,242
  • 2
  • 15
  • 36
12
votes
4 answers

Insert text into the textbox of another application

How do I, using C# or C++, insert text into the textbox of another application? I did this a long time ago and seemed to remember something about using the applications HWND. But since that change for every instance of the application I feel that I…
inquam
  • 12,664
  • 15
  • 61
  • 101
1
2 3
26 27