0

I need to eventually set the game screen to the topmost (foreground) window.

But my function does nothing (other user32.dll functions do work however)

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

public class DatabaseManager : MonoBehaviour
{



    //DLL imports
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool SetForegroundWindow(IntPtr hWnd);

    static Process proc = Process.GetProcessesByName("firefox")[0];
    IntPtr ptrFF = proc.Handle;
    //------------------------------------------------------------------------


    void Awake()
    {              
        SetForegroundWindow(ptrFF);
    }

   //Other code functions like Update etc.
}

Nothing happens, no errors. Is there a way to force a window to the front

PaffAttack
  • 49
  • 6
  • A process handle is not a HWND, and SetForegroundWindow expects a HWND (a window handle). `Process.GetProcessesByName` does not return a HWND. And how do you know there are *no errors*? You don't check the return value of `SetForegroundWindow` to know whether there are errors or not. – Ken White Jun 26 '19 at 20:13
  • Proc.handle is a process (firefox or notepad in tskmanager for example) so its a running process. Then I use proc.Handle to get an IntPtr which I thought was an HWND? Maybe not? Is it even possible to get focus on windows 10? Yes in .NET an hwnd is an IntPtr (handle) – PaffAttack Jun 26 '19 at 20:15
  • No, an `IntPtr` is not magically a window handle. An `IntPtr` is a data type that represents a pointer that is the size of an integer. In .Net, an `IntPtr` can **store** a HWND, but it's not magically an HWND when you assign a different kind of handle to it. Just because an `IntPtr` **can hold** a window handle does not automagically convert anything you stuff into it into a window handle. – Ken White Jun 26 '19 at 20:17
  • okay it comes back false. – PaffAttack Jun 26 '19 at 20:18
  • Which means it failed, which is because you're passing it a process handle instead of a window handle. – Ken White Jun 26 '19 at 20:19
  • How do I get an HWND then? Thank you – PaffAttack Jun 26 '19 at 20:19
  • https://stackoverflow.com/q/1888863/62576, which I found with a very quick search for `winapi window handle from process handle`, which you could also have done based on the information I gave you above. :-) – Ken White Jun 26 '19 at 20:21
  • @KenWhite I have no idea how to use this... I'm guessing you're talking about the one with three functions? Where do I pass it? What do I pass it. Please be more specific, thank you Ken. – PaffAttack Jun 26 '19 at 20:23
  • Process.MainWindowHandle (returns IntPtr) should do it, no? – PaffAttack Jun 26 '19 at 20:27
  • It still returns false :/ – PaffAttack Jun 26 '19 at 20:29
  • proc (process) is returning as zero, I have tried proc.Refresh() but it didn't fix it being zero. – PaffAttack Jun 26 '19 at 20:31
  • I'm not sure why you're unable to search this site yourself. I've given you the information needed, and you have access to the same search box at the top of this page that I do. And if `proc` is zero, then you need to solve that problem first anyway, because you can't convert a non-existent process handle to an HWND. – Ken White Jun 26 '19 at 20:34
  • 1
    well proc isn't ever going to stop being zero, and winapi is not exactly the best for me when I am doing stuff in c#... Is this even possible on windows 10 still? Don't be a jerk either. But I appreciate the help. I've been querying all day and inbetween every comment I send to you. – PaffAttack Jun 26 '19 at 20:35
  • 1
    Proc will stop being zero when you correctly get a process ID, and if you want to get a window handle based on that process ID you'll need to resolve that issue first. I'd guess that `GetProcessesByName("firefox")[0]` is the issue, because it's returning an invalid process handle. And name-calling when you've got someone that's been trying to help you is somewhat rude and isn't conducive to getting continued help; you may want to reconsider that approach. – Ken White Jun 26 '19 at 20:50
  • I read somewhere that accurately trying to get window handle proccess ID's is basically VERY finicky and not going to be accurrate at all. It is returning the firefox process when I debug the Proc, but the proc.mainwindowhandle is still zero. – PaffAttack Jun 26 '19 at 20:51

0 Answers0