# GetNativeHandle(GameWindow)

> Obtains the native Win32 window handle of the specified window. This is an extension method for GameWindow.

## Definition

* **Type:** Method
* **Namespace:** [UnityEngine.Windowing.Windows](/engine/6000.7/script-reference/unityengine/windowing/windows.md)
* **Assembly:** UnityEngine.WindowsWindowingModule

```csharp
public static nint GetNativeHandle(this GameWindow window)
```

### Parameters

**** (\[GameWindow]\(/engine/6000.7/script-reference/unityengine/windowing/gamewindow)): The [GameWindow](/engine/6000.7/script-reference/unityengine/windowing/gamewindow.md) instance this extension method is implicitly called on.

### Returns

| Type                                                           | Description                                                                                                                                                            |
| -------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [IntPtr](https://learn.microsoft.com/dotnet/api/system.intptr) | The native `HWND` of the window, represented as an `IntPtr`. Returns `IntPtr.Zero` if the window has no native handle, for example because Unity already destroyed it. |

### Remarks

Windows identifies each top-level window with a window handle, or `HWND`. Use this method to obtain the `HWND` that corresponds to a [GameWindow](/engine/6000.7/script-reference/unityengine/windowing/gamewindow.md) so that you can pass it to a native plugin, or to a Win32 function that operates on a window, such as `SetWindowPos`.

**Notes**:

* This method is synchronous and returns the handle immediately.
* The handle is valid only for as long as the window exists. Query the handle each time you need it rather than caching the returned value, because Unity invalidates the handle when it destroys the window.
* Modifying the window directly through the Win32 API can put the window out of sync with the state that Unity tracks. Where an equivalent [GameWindow](/engine/6000.7/script-reference/unityengine/windowing/gamewindow.md) method exists, such as [GameWindow.SetTitle](/engine/6000.7/script-reference/unityengine/windowing/gamewindow/settitle.md) or [GameWindow.MoveTo(Vector2Int)](/engine/6000.7/script-reference/unityengine/windowing/gamewindow/moveto.md#moveto\(vector2int\)), use that method instead.
* In Development builds, this method throws an `InvalidOperationException` if the [GameWindow](/engine/6000.7/script-reference/unityengine/windowing/gamewindow.md) is no longer valid. Use [GameWindow.IsValid](/engine/6000.7/script-reference/unityengine/windowing/gamewindow/isvalid.md) to check the window before you call this method.
* This method is available only on Windows.

### Examples

```csharp
using System;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.Windowing;
using UnityEngine.Windowing.Windows;

// Attach this script to a GameObject in the scene. It runs only in a Windows player.
public class GetNativeHandleExample : MonoBehaviour
{
    [DllImport("user32.dll")]
    static extern bool FlashWindow(IntPtr hWnd, bool bInvert);

    void Start()
    {
        IntPtr hwnd = GameWindow.Main.GetNativeHandle();

        if (hwnd == IntPtr.Zero)
        {
            Debug.LogWarning("The main window has no native handle.");
            return;
        }

        // Pass the handle to a Win32 function to flash the window in the taskbar
        FlashWindow(hwnd, true);
    }
}
```
