Solved

Not more than one instance of an application

Posted on 2004-03-25
8
325 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
Comment Utility
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
Comment Utility
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
Comment Utility
Correction: remove line return 0;
This code replaces your lines:

       if(hPrevApp)
       {
               PostMessage(hPrevApp, WM_SYSCOMMAND,  SC_RESTORE, 0);
               SetForegroundWindow(hPrevApp);
       }
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Expert Comment

by:de9989
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
PDF library for Delphi 2 89
Arduino EDI - Programming language 3 60
Dialogbox API leak? 18 58
FMX TCameraComponent Problem 2 9
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. …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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.
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.

762 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

6 Experts available now in Live!

Get 1:1 Help Now