Find Window

  • Thread starter Thread starter James Alba
  • Start date Start date
J

James Alba

A quick question.

In C++ I always do the following to stop a user from loading two or more
instances of my application:

HWND hWndByTitle = FindWindow(NULL, "Hello World");
if (hWndByTitle != NULL)
{
/* MessageBox(NULL, "The Application is Already Open.", "Warning", MB_OK |
MB_ICONWARNING); */
SetForegroundWindow(hWndByTitle);
return 0;
}

....

I am now working on a C# project and I now want to convert this into C#. Is
it possible, and if yes, then how?

Thanks all!
 
James,

Looking for a window by the title of the window is not a good idea.
Rather, you should specify a named mutex (with a rather unique name) which
you then check the existence of in your program.

If you are using .NET 2.0, there is a class called
WindowsApplicationBase which you can use to assist you with this.

For more info, check my response to an earlier post, which you can find
here (watch for line wrap):

http://groups.google.com/group/micr...+author:Paldino&rnum=1&hl=en#8d2d86fcc6e071fe

Hope this helps.
 
Back
Top