473,385 Members | 1,396 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Find Window

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!
Nov 17 '05 #1
3 7462
Hi James,

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

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

"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!

Nov 17 '05 #2
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/micro...2d86fcc6e071fe

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"James Alba" <a@a.com> wrote in message
news:OW**************@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!

Nov 17 '05 #3
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:
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!

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Yves-Alain NICOLLET | last post by:
I have a script that opens a small window containing a button which when clicked performs a find to the next occurence of a string. It works well when the string is made of only one word or of...
0
by: amit | last post by:
hi I have created a tool which does a find and replace thru DTE, now after it is done, it opens up a window, "FIND REACHED THE STARTING POINT OF SEARCH" I want to disbale this window...
3
by: Thief_ | last post by:
I need to use an existing IE window which already has a specific url and re-use that window for a new URL. Example: One open IE window has the following in its Address Bar: ...
17
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I find the size of a browser window?...
0
by: kloplop321 | last post by:
I found this code(vb only) and it does about the same thing(in vb, not vb .NET 2005) Attribute VB_Name = "Form1" Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = False Attribute...
1
by: wormsnout | last post by:
Hi all, I'm looking to write some Visual Basic .NET code to find a Window given its Caption and then kill it entirely (not just minimise it). I think I have found the correct Windows Handle...
2
by: emily224 | last post by:
Hello, I have been trying to understand this source code, which I retreived from my online course test. I would like to know how to find the answer for the question on the test. Im sure the answer...
4
by: emily224 | last post by:
Hello, I have been trying to understand this source code, which I retreived from my online course test. I would like to know how to find the answer for the question on the test. Im sure the answer...
10
by: Gary | last post by:
I'm trying to find a way to determin which window Within-Another-Program currently has the focus. THE SITUATION: (Skip down to "My Question" if you don't want the background...) * This is...
2
by: Alan Mailer | last post by:
Well this is a new one on me... Suddenly I can't get my "Find" and/or "Replace" window to appear on screen while I'm working on my VB.net code. These windows don't appear if I hit Ctrl-F,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.