I'm using a mutex to ensure only 1 instance of my app is created. It works
fine.

If an instance of my app IS already running, I want to use the following
code to bring the window to the foreground. BUT, the MainWindowHandle is
always 0 (I checked all processes and they are all 0). How can I get the
window handle back? Or, is there another way to popup my application?

Thanks,

Steve

if (!grantedInitialOwnership)
{
Process[] ps =
Process.GetProcessesByName("AppName");
IntPtr hWnd = ps[0].MainWindowHandle;

// Show existing instance
if (hWnd.ToInt64() != 0)
{
SetForegroundWindow(hWnd);
ShowWindow(hWnd, 5); // SW_SHOW
}
}

Re: Process -> MainWindowHandle always coming back as 0??? by Mattias

Mattias
Thu Sep 25 14:31:46 CDT 2003

Steven,

> Process[] ps =
>Process.GetProcessesByName("AppName");
> IntPtr hWnd = ps[0].MainWindowHandle;

You're only checking the first process found, which might be your
currently executing process that hasn't displayed its main window yet
(thus the null handle). You should loop through all processes returned
by GetProcessesByName().



Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.