看了网上GOOGLE到的代码后修改了一下,使用互斥……
只需修改Project1.cpp中相应的地方即可。
//
---------------------------------------------------------------------------
#include < vcl.h >
#pragma hdrstop
// ---------------------------------------------------------------------------
USEFORM( " Main.cpp " , MainForm);
// ---------------------------------------------------------------------------
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int )
{
HANDLE hMutex = CreateMutex( 0 , false , Application -> Title.c_str() );
if ( hMutex )
{
if ( GetLastError() == ERROR_ALREADY_EXISTS )
{
CloseHandle(hMutex);
Application -> Terminate();
}
}
try
{
Application -> Initialize();
Application -> CreateForm(__classid(TMainForm), & MainForm);
Application -> Run();
}
catch (Exception & exception)
{
Application -> ShowException( & exception);
}
catch (...)
{
try
{
throw Exception( "" );
}
catch (Exception & exception)
{
Application -> ShowException( & exception);
}
}
CloseHandle(hMutex);
return 0 ;
}
// ---------------------------------------------------------------------------
#include < vcl.h >
#pragma hdrstop
// ---------------------------------------------------------------------------
USEFORM( " Main.cpp " , MainForm);
// ---------------------------------------------------------------------------
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int )
{
HANDLE hMutex = CreateMutex( 0 , false , Application -> Title.c_str() );
if ( hMutex )
{
if ( GetLastError() == ERROR_ALREADY_EXISTS )
{
CloseHandle(hMutex);
Application -> Terminate();
}
}
try
{
Application -> Initialize();
Application -> CreateForm(__classid(TMainForm), & MainForm);
Application -> Run();
}
catch (Exception & exception)
{
Application -> ShowException( & exception);
}
catch (...)
{
try
{
throw Exception( "" );
}
catch (Exception & exception)
{
Application -> ShowException( & exception);
}
}
CloseHandle(hMutex);
return 0 ;
}
// ---------------------------------------------------------------------------