to eknight(小廷): HANDLE m_mutex语句应该放在InitInstance()函数外边的
同意eknight(小廷) 的其它方法我不提倡
Add these code to your InitInstance(). { ATOM atom=GlobalFindAtom("YourAppName"); if(!atom) { AfxMessageBox("You aready run it"); return false; } m_atom=GlobalAddAtom("YourAppName"); //add your other code //before exit your process,be sure to call GlobalDeleteAtom(m_atom);
if((m_mutex=CreateMutex(NULL,TRUE,"MyFlag"))==0) //MyFlag:你的程序标识,任意值
{
AfxMessageBox("创建互斥量失败,程序不能运行!");
return FALSE;
}
if(GetLastError()==ERROR_ALREADY_EXISTS)
{
AfxMessageBox("本程序已在运行,不允许再一次运行!");
return FALSE;
}
HANDLE m_mutex语句应该放在InitInstance()函数外边的
{
ATOM atom=GlobalFindAtom("YourAppName");
if(!atom)
{
AfxMessageBox("You aready run it");
return false;
}
m_atom=GlobalAddAtom("YourAppName");
//add your other code
//before exit your process,be sure to call GlobalDeleteAtom(m_atom);
Modify to: if(atom)