Solved

Not more than one instance of an application

Posted on 2004-03-25
8
336 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 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

691 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