Thread: Detecting the current instance of exe file

  1. #1
    Unregistered
    Guest

    Detecting the current instance of exe file

    How can we detect that the currnet instance of an exe file is running already or not?

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    there is an easy way and a correct way....

    1) the correct way is to use mutexes..... not too easy.

    2) the easy way is to get your program to open a dummy file and check beforehand whether that file is already open.If it is you have a previous instance running and can exit or whatever!
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Why are mutexes hard? Just call CreateMutex() with a specific name then call GetLastError() if the mutex alredy existed, then GetLastError() returns ERROR_ALREADY_EXISTS, so you know another copy is running and you can exit. Easy.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Try,

    hMapping = CreateFileMapping((HANDLE)0xffffffff, NULL, PAGE_READONLY, 0, 32, "MyExeMAP");

    if (hMapping)// Check to see if app is already running, if so exit
    {
    if (GetLastError() == ERROR_ALREADY_EXISTS)
    {
    MessageBox(NULL, "My Exe is already running.", "Error", MB_OK);
    ExitProcess(1);
    }
    }
    else
    {
    MessageBox(NULL, "Error creating file mapping.", "Error", MB_OK);
    ExitProcess(1);

    These are the first lines of code in your WINMAIN function.
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM