Click here to Skip to main content
15,900,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an VC++ 6.0 application.What I need is if application is already running and again If I run another instance of same application I want the previous instance to be opend
Posted

First create a unique mutex (with some guid)in your InitInstance() method.Then check GetLastError value.if you get the error saying mutex already exist then use FindWindow API with your class name and your window name to window the existing window handle.The use ShowWindow API with that handle,and return from it.
Bored of explanation then look this algorithm,

C#
::CreateMutex(NULL, false, TEXT("UniqueMutexName"));
if (::GetLastError() == ERROR_ALREADY_EXISTS)
{
    HWND hMyPrevWnd = ::FindWindow(YourClass, YourWindoName); //Check Msdn for more info
    if(hMyPrevWnd != NULL)
    {
        ::ShowWindow(hMyPrevWnd, SW_SHOW);
    }
}
 
Share this answer
 
one quick solution is that create a mutext in your applications InitInstance() function. refer http://www.codeguru.com/forum/showthread.php?t=312467[^]
after this within
case ERROR_ALREADY_EXISTS:
call the function FindWindow() by passing the window name.
u get the handle to the window.
then send the message to show the window by calling the function SendMessage() with proper parameters to maximize the window. You can somehow maximize the window since you have the handle.

Not sure whether its a good solution.
 
Share this answer
 
Search CodeProject for 'only open one instance'[^]

Choose Articles, Tech Blogs and Tips and Tricks

Be aware, if you try the mutex approach, you need to test behaviour with concurrent windows stations, concurrent logins (on a server, for instance) and and Terminal Services/mstsc (similar problems to windows stations)
 
Share this answer
 
v2
Comments
Albert Holguin 1-Jun-11 21:04pm    
um... your search doesn't bring up anything relevant unless you select "Everything"... that's probably why you're getting the downvotes... you should consider revising your post... I'll upvote you if you come up with a better search query! :)
barneyman 1-Jun-11 21:14pm    
granted - i think the point i was trying to make was that there were a myriad of solutions already on here for that common problem
Albert Holguin 1-Jun-11 21:15pm    
I know... I didn't downvote you! ..I knew what you were going for! :D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900