# hWnd functionality in Unity

**URL:** <https://discussions.unity.com/t/hwnd-functionality-in-unity/607747>\
**Category:** C# & .NET\
**Created:** [December 5, 2015, 12:16pm UTC](https://discussions.unity.com/t/hwnd-functionality-in-unity/607747 "2015-12-05T12:16:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Studio\_AKIBA](https://avatars.discourse-cdn.com/v4/letter/s/a9a28c/32.png) [@Studio\_AKIBA](https://discussions.unity.com/u/Studio_AKIBA)\
**Post date:** [December 5, 2015, 12:16pm UTC](https://discussions.unity.com/t/hwnd-functionality-in-unity/607747/1 "2015-12-05T12:16:29Z")

</div>

This is my second post on minimising windows today, and I have decided after Unity wouldn’t let me build with a DLL to try it without, which is causing more problems than it is quite frankly worth.

I am trying to get the window handle reference using hWnd, but nothing is working.

inside my class I have this:

```csharp
#if UNITY_STANDALONE_WIN && !UNITY_EDITOR
        private const int SW_SHOWNORMAL = 1;
        private const int SW_SHOWMINIMIZED = 2;
        private const int SW_SHOWMAXIMIZED = 3;

        [DllImport("user32.dll")]
        private static extern IntPtr GetActiveWindow();
        [DllImport("user32.dll")]
        private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
#endif

```

In my awake method:

```csharp
public void Awake()
    {
        windowName = UnityEngine.Application.productName.ToString();
        #if UNITY_STANDALONE_WIN && !UNITY_EDITOR
        Window window = Window.GetWindow(this);
        var wih = new WindowInteropHelper(window);
        IntPtr hWnd = wih.Handle;
        #endif
    }

```

And in my callable MinimiseWindow method:

```csharp
public void MinimiseWindow()
    {
        #if UNITY_STANDALONE_WIN && !UNITY_EDITOR
        if (!hWnd.Equals(IntPtr.Zero))
        {
            ShowWindowAsync(hWnd, SW_SHOWMINIMIZED);
        }
        #endif
    }

```

I keep getting these kinds of errors:

 ![2408957--164605--error.png](https://europe1.discourse-cdn.com/unity/original/3X/c/8/c8cdd2b31957eced16e75c2ac380ee2ee8b61e4b.png)

---

<div class="post-metadata">

**Author:** ![Studio\_AKIBA](https://avatars.discourse-cdn.com/v4/letter/s/a9a28c/32.png) [@Studio\_AKIBA](https://discussions.unity.com/u/Studio_AKIBA)\
**Post date:** [December 5, 2015, 12:42pm UTC](https://discussions.unity.com/t/hwnd-functionality-in-unity/607747/2 "2015-12-05T12:42:24Z")

</div>

Anyone?

---

<div class="post-metadata">

**Author:** ![greg-harding](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/greg-harding/32/570613_2.png) [@greg-harding](https://discussions.unity.com/u/greg-harding)\
**Post date:** [December 5, 2015, 11:50pm UTC](https://discussions.unity.com/t/hwnd-functionality-in-unity/607747/3 "2015-12-05T23:50:18Z")

</div>

hi,

we had problems getting the hWnd as well - often it was 0. Our original post: [Unity 5 Windows 7 application focus/fullscreen problem [Workaround] - Unity Engine - Unity Discussions](http://forum.unity3d.com/threads/unity-5-windows-7-application-focus-fullscreen-problem-workaround.327240/)

Here’s a snippet that might help;

```csharp
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string className, string windowName);

[DllImport("user32.dll")]
internal static extern IntPtr SetForegroundWindow(IntPtr hWnd);

[DllImport("user32.dll")]
internal static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

private void GetFocusWin() {
    // get hWnd, nb. hWnd always seems to return 0 with this method
    // http://stackoverflow.com/questions/7357675/how-can-i-set-the-focus-to-application-which-is-allready-in-running-state?rq=1
    //System.Diagnostics.Process currentProcess = System.Diagnostics.Process.GetCurrentProcess();
    //IntPtr hWnd = currentProcess.MainWindowHandle;

    // get hWnd, nb. this works ok on Windows 7 x64
    // http://ronniediaz.com/2011/05/03/start-a-process-in-the-foreground-in-c-net-without-appactivate/
    IntPtr hWnd = FindWindow(null, "Your App Name");

    if (hWnd != IntPtr.Zero) {
        Debug.Log("GetFocusWin() setting foreground window, showing window SW_SHOWDEFAULT");

        SetForegroundWindow(hWnd);
        // enum values from https://msdn.microsoft.com/en-us/library/windows/desktop/ms633548(v=vs.85).aspx
        ShowWindow(hWnd, 10); // SW_SHOWDEFAULT = 10, SW_MAXIMIZE = 3, SW_SHOW = 5

    } else {
        Debug.LogWarning("GetFocusWin() failed, hWnd is 0!");
    }
}

```

cheers,

// greg

---

<div class="post-metadata">

**Author:** ![Studio\_AKIBA](https://avatars.discourse-cdn.com/v4/letter/s/a9a28c/32.png) [@Studio\_AKIBA](https://discussions.unity.com/u/Studio_AKIBA)\
**Post date:** [December 6, 2015, 9:28am UTC](https://discussions.unity.com/t/hwnd-functionality-in-unity/607747/4 "2015-12-06T09:28:26Z")

</div>

> [@greg harding](#):
>
> hi,
> 
> we had problems getting the hWnd as well - often it was 0. Our original post: [Unity 5 Windows 7 application focus/fullscreen problem [Workaround] - Unity Engine - Unity Discussions](http://forum.unity3d.com/threads/unity-5-windows-7-application-focus-fullscreen-problem-workaround.327240/)
> 
> Here’s a snippet that might help;
> 
> ```csharp
> [DllImport("user32.dll")]
> public static extern IntPtr FindWindow(string className, string windowName);
> 
> [DllImport("user32.dll")]
> internal static extern IntPtr SetForegroundWindow(IntPtr hWnd);
> 
> [DllImport("user32.dll")]
> internal static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
> 
> private void GetFocusWin() {
> // get hWnd, nb. hWnd always seems to return 0 with this method
> // http://stackoverflow.com/questions/7357675/how-can-i-set-the-focus-to-application-which-is-allready-in-running-state?rq=1
> //System.Diagnostics.Process currentProcess = System.Diagnostics.Process.GetCurrentProcess();
> //IntPtr hWnd = currentProcess.MainWindowHandle;
> 
> // get hWnd, nb. this works ok on Windows 7 x64
> // http://ronniediaz.com/2011/05/03/start-a-process-in-the-foreground-in-c-net-without-appactivate/
> IntPtr hWnd = FindWindow(null, "Your App Name");
> 
> if (hWnd != IntPtr.Zero) {
> Debug.Log("GetFocusWin() setting foreground window, showing window SW_SHOWDEFAULT");
> 
> SetForegroundWindow(hWnd);
> // enum values from https://msdn.microsoft.com/en-us/library/windows/desktop/ms633548(v=vs.85).aspx
> ShowWindow(hWnd, 10); // SW_SHOWDEFAULT = 10, SW_MAXIMIZE = 3, SW_SHOW = 5
> 
> } else {
> Debug.LogWarning("GetFocusWin() failed, hWnd is 0!");
> }
> }
> 
> ```
> 
> cheers,
> 
> // greg

Tried plugging some of this stuff in, doesn’t seem to have made any difference.

I think the main issue is that Unity simply can’t get the reference for hWnd because it is in `!UNITY_EDITOR` tags, but knowing this still doesn’t help me in the slightest.

All I am trying to do is minimise the windows, which is borderless, maybe this is simply too much to ask of Unity.

---

<div class="post-metadata">

**Author:** ![greg-harding](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/greg-harding/32/570613_2.png) [@greg-harding](https://discussions.unity.com/u/greg-harding)\
**Post date:** [December 6, 2015, 8:44pm UTC](https://discussions.unity.com/t/hwnd-functionality-in-unity/607747/5 "2015-12-06T20:44:34Z")

</div>

There are issues with your code causing the errors you pasted above like not declaring variables in the right place - I was just trying to help with the hWnd = 0 problem which we had as well.

If you want to test hWnd stuff in the editor, maybe try doing something like;

```csharp
#if UNITY_EDITOR
IntPtr hWnd = FindWindow(null, "Unity"); // or whatever the Unity Editor window is called
#else
IntPtr hWnd = FindWindow(null, "Your App Name");
#endif

if(hWnd != IntPtr.Zero){
  Debug.Log("hWnd = " + hWnd);
} else {
  Debug.LogWarning("hWnd problem.");
}

```

cheers,

// greg

---

<div class="post-metadata">

**Author:** ![darkhog](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/darkhog/32/220656_2.png) [@darkhog](https://discussions.unity.com/u/darkhog)\
**Post date:** [December 7, 2015, 12:36am UTC](https://discussions.unity.com/t/hwnd-functionality-in-unity/607747/6 "2015-12-07T00:36:58Z")

</div>

First of all, type Window is missing. Did you forget appropriate using directive? Secondly, hWnd variable isn’t declared (most likely in MinimizeWindow). Thirdly, you’re missing a cast in #1 to System.IntPtr or an appropriate conversion routine.

---

<div class="post-metadata">

**Author:** ![Gold\_Rush](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/gold_rush/32/286297_2.png) [@Gold\_Rush](https://discussions.unity.com/u/Gold_Rush)\
**Post date:** [March 17, 2018, 9:34am UTC](https://discussions.unity.com/t/hwnd-functionality-in-unity/607747/7 "2018-03-17T09:34:03Z")

</div>

Executed app can get his hwnd independently, from himself by use winapi GetActiveWindow external function, but that works only if focus setted on runned window. Otherwise function returns System.IntPtr.Zero.

If you run app usually focus automatically moved to runned app, and in this moment you can save hwnd, and use it after.

For example:

```csharp
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;

// links to external dll functions
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
private static extern bool SetWindowPos(System.IntPtr hwnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);
[DllImport("user32.dll")]
static extern System.IntPtr GetActiveWindow();

public static void SetPosition(System.IntPtr ptr, int x, int y, int resX = 0, int resY = 0) {
   SetWindowPos( ptr , 0, x, y, resX, resY, resX * resY == 0 ? 1 : 0);
}

System.IntPtr ptr=System.IntPtr.Zero; // varibale for save hwnd

void Update() {
   if(ptr==System.IntPtr.Zero){
      // if ptr==zero try to catch it.
      ptr=GetActiveWindow();
   }else{
      // if we catch it try to move and resize window
      SetPosition(ptr,0,0,100,100);
  }
}

```
