?
Solved

Not more than one instance of an application

Posted on 2004-03-25
8
Medium Priority
?
338 Views
Last Modified: 2008-02-01
Hi there all,

I don't want more than one instance of the application running.

The code that I have sofar works, but I can't minimize the application after I try to open the application again.
(It restore the application but then I can't minimize it again)

----------------------------------------------------------------
if(GetLastError() == ERROR_ALREADY_EXISTS)
   {
       if(hInstanceMutex)
           CloseHandle(hInstanceMutex);
       Application->Title = "";
       HWND hPrevApp = ::FindWindow(NULL, "Medisoft");

       if(hPrevApp)
       {
               PostMessage(hPrevApp, WM_SYSCOMMAND,  SC_RESTORE, 0);
           SetForegroundWindow(hPrevApp);
       }
       return 0;
   }
----------------------------------------------------------------

I've tried to use IsIconic(hPrevApp)), but it is always false.

I'm using Borland C++ Builder 5.0 on Windows 2000
Please help...
0
Comment
Question by:morees
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
8 Comments
 
LVL 13

Accepted Solution

by:
SteH earned 300 total points
ID: 10675597
Have a look at the following web site. It gives details on several aspects concerning different ways to limit the number of program instances.

http://www.flounder.com/nomultiples.htm
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 10675621
Try this:

if(hPrevApp)
{
    hWndChild = GetLastActivePopup(hPrevApp);

    if ( hWndChild )
    {
         if ( IsIconic(hWndChild) )
               ShowWindow(hWndChild, SW_RESTORE);

         SetForegroundWindow(hWndChild);
    }
    return 0;
}
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 10675633
Correction: remove line return 0;
This code replaces your lines:

       if(hPrevApp)
       {
               PostMessage(hPrevApp, WM_SYSCOMMAND,  SC_RESTORE, 0);
               SetForegroundWindow(hPrevApp);
       }
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Expert Comment

by:de9989
ID: 10684742
Try to put this in yor ctor.

  // Prevent multiple instances of this application.
  HANDLE hMutex = CreateMutex(NULL, TRUE, "__UNIQUE_NAME__");
  if(GetLastError() == ERROR_ALREADY_EXISTS || GetLastError() == ERROR_ACCESS_DENIED)
  {
    Application->Terminate();
    return;
  }
0
 

Author Comment

by:morees
ID: 10738631
Thanks for the responses,
Tried all the above mentioned tips but,

if I use if (IsIconic(hWndChild)), IsIconic is always false,
so it can't restore the program
and if I don't use IsIconic , the program can't minimize anymore.

Please help...
any more suggestions.
0
 
LVL 13

Expert Comment

by:SteH
ID: 10946885
I think in the link I showed is a working example. And the article is explaning that using FindWindow is not a safe way to do what morees wants. But from the sentence

>>Tried all the above mentioned tips but,
>>if I use if (IsIconic(hWndChild)), IsIconic is always false,

I don't see if he really take the time to read the article. It looks more that the solutions posted as code where tried. Using the method given there I have no problem getting an app back to normal size and it can be minimized later on as well.
0
 

Author Comment

by:morees
ID: 10947362
Sorry, I forgot about this post, until today tinchos did a post.
I'm working on another project at the moment, so I havn't finished the code related to this post on my previous project.

I did read the article, when I first posted this message.
The article did explain what to do quite well, but yes,
I was lazy and tried the FindWindow way and as you expected, it did not work.

The article gave me the most info, so the points goes to.....


0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question