How can I bring to front a window called by an script in unity?

Hello,

I'm trying to bring to front the unity game when it is not the active window.

I've tried implenting a dll and later importing de "user32.dll" but is not working.

The code is something like this:

using System; using System.Collections.Generic; using System.Threading; using System.Runtime.InteropServices; using System.Diagnostics; using UnityEngine; using System.Collections;

public class AccesoDLL : MonoBehaviour { 

    [DllImport("SimpleDLL")] 
    private static extern byte cSum(byte b1, byte b2);
    [DllImport("SimpleDLL")] 
    private static extern int MinimizarVentana(int u);
    [DllImport("SimpleDLL")]
    private static extern int MaximizarVentana(int u);
    [DllImport("SimpleDLL")]
    private static extern int GetWindowHandler();

    [DllImport("user32.dll")]
    [return: MarshalAs (UnmanagedType.Bool)]
    private static extern bool SetForegroundWindow(int hWnd);

    public int u;

    void Awake() {

        u = GetWindowHandler();
    }

    public void Minimizar () {

        MinimizarVentana(u);
    }

    public void Maximizar () {

        MaximizarVentana(u);
    }

    public void SetForegroundWindows(){

        SetForegroundWindow(u);

    }

}

The functions Maximizar and Minimizar are working on my own dll, but when I try yo use de API functions like SetForegroundWindow not work.

Does anybody know how to do this?

Thanks!

Here’s my solution that works with Unity 5.0.1 and Windows 8.1:

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

public class ForeGrounder : MonoBehaviour {

    private const uint LOCK = 1;
    private const uint UNLOCK = 2;

    private IntPtr window;

	void Start() {
        LockSetForegroundWindow(LOCK);
        window = GetActiveWindow();
        StartCoroutine(Checker());
	}

    IEnumerator Checker() {
        while (true) {

            yield return new WaitForSeconds(1);
            IntPtr newWindow = GetActiveWindow();

            if (window != newWindow) {
                Debug.Log("Set to foreground");
                SwitchToThisWindow(window, true);
            }
        }
    }

    [DllImport("user32.dll")]
    static extern IntPtr GetActiveWindow();
    [DllImport("user32.dll")]
    static extern bool LockSetForegroundWindow(uint uLockCode);
    [DllImport("user32.dll")]
    static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);
}