|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Find WindowIn 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! Hi James,
Well, if you just want to prevent multiple instances, check out: http://odetocode.com/Blogs/scott/archive/2004/08/20/401.aspx 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! > > > 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. -- Show quoteHide quote- Nicholas Paldino [.NET/C# MVP] - mvp@spam.guard.caspershouse.com "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! > > 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! > >
Other interesting topics
Enums I thought they were safe to use
Can we Write a device Driver using CSharp XML deserialization C# 3.0 ADO.NET and SQL DataGrid Events - How to mark all row when user uses the keyboard can PDA apps be made in VS2005 C# Newbie: Thread question Get Auto id when inserting record into ms Access [Repost] MergeMenu and CloneMenu not working in VS 2005 beta 1 |
|||||||||||||||||||||||