Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Not more than one instance of an application

Posted on 2004-03-25
8
Medium Priority
?
341 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

610 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