Home All Groups Group Topic Archive Search About
Author
5 Oct 2005 12:37 PM
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!

Author
5 Oct 2005 1:10 PM
Rakesh Rajan
Hi James,

Well, if you just want to prevent multiple instances, check out:
http://odetocode.com/Blogs/scott/archive/2004/08/20/401.aspx

--
HTH,
Rakesh Rajan
MVP, MCSD
http://www.msmvps.com/rakeshrajan/



Show quoteHide quote
"James Alba" wrote:

> 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!
>
>
>
Are all your drivers up to date? click for free checkup

Author
5 Oct 2005 2:31 PM
Nicholas Paldino [.NET/C# MVP]
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/microsoft.public.dotnet.languages.csharp/browse_thread/thread/95e715a151e403c4/8d2d86fcc6e071fe?lnk=st&q=WindowsApplicationBase+group:microsoft.public.dotnet.languages.csharp+author:Nicholas+author:Paldino&rnum=1&hl=en#8d2d86fcc6e071fe

    Hope this helps.


--
          - Nicholas Paldino [.NET/C# MVP]
          - mvp@spam.guard.caspershouse.com

Show quoteHide quote
"James Alba" <a@a.com> wrote in message
news:OWVLLmayFHA.2008@TK2MSFTNGP10.phx.gbl...
>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!
>
>
Author
5 Oct 2005 2:53 PM
Jason Newell
And for future reference, this is called PInvoke in .NET.  There is a
great website for PInvoke signatures here:  www.pinvoke.net.

Jason Newell


James Alba wrote:
Show quoteHide quote
> 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!
>
>

Bookmark and Share