Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 need to tell my helper class to attach to it. Prior to doing that, I'd like to know if the given hWnd is still valid, otherwise I'll spawn another instance.

How can I do this in C# & .NET 3.5?

like image 296
Ian P Avatar asked Apr 29 '10 16:04

Ian P


People also ask

What is HWND?

HWND is a "handle to a window" and is part of the Win32 API . HWNDs are essentially pointers (IntPtr) with values that make them (sort of) point to a window-structure data. In general HWNDs are part an example for applying the ADT model. If you want a Control's HWND see Control. Handle property.

How to Find windows handle?

The Win32 API provides no direct method for obtaining the window handle associated with a console application. However, you can obtain the window handle by calling FindWindow() . This function retrieves a window handle based on a class name or window name. Call GetConsoleTitle() to determine the current console title.


Video Answer


1 Answers

If it is a window handle, you can call isWindow(hWnd);

From msdn:

Return Value

BOOL

If the window handle identifies an existing window, the return value is nonzero.

If the window handle does not identify an existing window, the return value is zero. Remarks

A thread should not use IsWindow for a window that it did not create because the window could be destroyed after this function was called. Further, because window handles are recycled the handle could even point to a different window.

By the way since you are in .NET you'll have to do something like:

[DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] static extern bool IsWindow(IntPtr hWnd); 
like image 56
Francisco Soto Avatar answered Sep 21 '22 02:09

Francisco Soto