WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
try
{
//这里创建一个互斥量(你就复制吧)!!!!!!!!!!!!!!!!!!!
HANDLE Mutex=OpenMutex(MUTEX_ALL_ACCESS,false,"OneInstanceAllowed");
if(Mutex==NULL) //this is the only instance
{
//Create the Mutex...
Mutex=CreateMutex(NULL,true,"OneInstanceAllowed");
}
else //this is not only instance
{
//Send a message to the previous instance of the application,and asking_
//it to restore and front itself.
SendMessage(HWND_BROADCAST,RegisterWindowMessage("OnlyOne"),0,0);
return 0;
}
//创建互斥结束
//---------------------------------------------------------------------------
#include <vcl\vcl.h>
#pragma hdrstop
//---------------------------------------------------------------------------
USEFORM("Unit1.cpp", Form1);
USERES("SingleInstance.res");
//---------------------------------------------------------------------------
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
HANDLE mutex ;
try
{
const char mutexname [] = "SingleInstanceProgram" ;
// See if the mutex already exists.
mutex = OpenMutex (0, false, mutexname) ;
if (mutex == NULL)
{
// The mutex is not there. This means another instance of the program is
// not already running. Create the mutex so any other instance of the
// program will know that this one is already running.
mutex = CreateMutex (NULL, true, mutexname) ;
}
else
{
// The mutex exists so another instance of the program is running
// OR some other program created a mutex called "SingleInstanceProgram".
ShowMessage ("Application Already Running") ;
// Returning from WinMain terminates the program.
return 0 ;
}