Solved

Not more than one instance of an application

Posted on 2004-03-25
8
328 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
  • 2
  • 2
  • 2
  • +1
8 Comments
 
LVL 13

Accepted Solution

by:
SteH earned 100 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

864 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now