Solved

Single Instance of Console App?

Posted on 2000-04-13
5
328 Views
Last Modified: 2013-11-20
Can anyone suggest me a fool proof way to limit a console app to single instance?

If somebody tries to start the console app in any or all the possible ways it should pop up th eprevious instance.

0
Comment
Question by:nv3prasad
  • 2
  • 2
5 Comments
 
LVL 1

Expert Comment

by:MT_MU
Comment Utility
Some good info can be found at...

http://www3.pgh.net/~newcomer/nomultiples.htm
0
 
LVL 11

Expert Comment

by:mikeblas
Comment Utility

You can just create a named, global object and check for it before continuing.

"popping up" another instance isn't allowable: what if the user is typing in some other window? Abruptly popping up another window without the user expecting it is rude. (And, Windows 2000 and newer are _finally_ actively preventing apps from performing this miserable behaviour!)

-- begin file only1.cpp --

// compile with
//   cl /W4 only1.cpp

#include <stdio.h>
#include <windows.h>

void main()
{
   HANDLE hPrev = CreateEvent(NULL, FALSE, FALSE, "SomeUniqueNameHere");
   if (GetLastError() == ERROR_ALREADY_EXISTS)
   {
      printf("Already running!\n");
      return;
   }

   printf("Not already running.\n");
   printf("Press a key to exit\n");
   getchar();
}

-- end of only1.cpp --

..B ekiM
0
 
LVL 1

Author Comment

by:nv3prasad
Comment Utility
I tried what you said but does not seem to work, can you post me a sample which can have only one instance of a console application.

My E-mail is pnarahar@hotmail.com
0
 
LVL 1

Author Comment

by:nv3prasad
Comment Utility
Hi Mike,

I figured out that you were not wrong and it was my mistake to reject your answer.

I will award you the points.

Can you help me to close the console window without it asking me "Press any key to continue" ( this is not your code though!)

Thank you

nv3prasad
0
 
LVL 11

Accepted Solution

by:
mikeblas earned 50 total points
Comment Utility
> Can you help me to close the console window without
 > it asking me "Press any key to continue" ( this is not
 > your code though!)

If it's not my code, then where is it coming from? Which OS are you using?

..B ekiM
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
only14 challenge 19 63
Line meaning 9 75
Can not remove SSL certificate on iPhone 6 - iOS10.2 12 84
Making an alias 7 51
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

743 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

17 Experts available now in Live!

Get 1:1 Help Now