Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Preventing For Openning Multiple Instances 1

Status
Not open for further replies.

dakkarin

Instructor
Nov 11, 2005
76
TR
Hi I wrote a program with borland c++ builder 5.0 but I want people not able to open more than one instance of my program how can i prevent this?
Thanks

Liars Do Not Fear The Truth If There Are Enough Liars
 
Let's assume you use the IDE to create a program named MyProg with a form titled, "My Program." The IDE creates two sets of C++ code. One is named MyProg.cpp and the other you name (the default is Unit1.cpp) Your program's C++ code probably looks something like this:
Code:
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
    try
    {
         Application->Initialize();
         Application->Title = "My Program";
         Application->CreateForm(__classid(TMyProgForm), &MyProgForm);
         Application->Run();
    }
    catch (Exception &exception)
    {
         Application->ShowException(&exception);
    }
    catch (...)
    {
         try
         {
             throw Exception("");
         }
         catch (Exception &exception)
         {
             Application->ShowException(&exception);
         }
    }
    return 0;
}

This is the MyProg.cpp code that is automatically created for you and not the code you change in your MyProgUnit.cpp.

There are several ways to do this. One of the easiest is to use Mutex's. To use a mutex, change the code as follows:
Code:
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
    HANDLE MyProgmtx; // Add this with whatever name you want. You could just use "Mutex" if you wanted.

    try
    {
        // Add the following code so only one instance of this program can be run

        // Step One -- Open a mutex with the same name
        MyProgmtx = OpenMutex (MUTEX_ALL_ACCESS, false, "MyProg");
        // For the string above, I make certain that it is unigue
        // For this reason, I use the program name

        // Step Two -- Check to see if the mutex exists
        if (CLMmtx == NULL)
            CLMmtx = CreateMutex (NULL, true, "MyProg"); // If not, create it
        else // If it does, display a warning and stop
        {
            ShowMessage("MyProg is already running!");
            return 0;
        }
        // Add the above code

         Application->Initialize();
         Application->Title = "My Program";
         Application->CreateForm(__classid(TMyProgForm), &MyProgForm);
         Application->Run();
    }
    catch (Exception &exception)
    {
         Application->ShowException(&exception);
    }
    catch (...)
    {
         try
         {
             throw Exception("");
         }
         catch (Exception &exception)
         {
             Application->ShowException(&exception);
         }
    }

    ReleaseMutex(MyProgmtx); // Add this line, too! 
    //Just make certain to use the same name as above

    return 0;
}

Clear as mud, now?



James P. Cottingham
-----------------------------------------
[sup]I'm number 1,229!
I'm number 1,229![/sup]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top