C++同一时刻仅允许一个实例,包含多用户的场景。

···

BOOL CtestDialogApp::InitInstance()
{

char exeFullPath[MAX_PATH] = {0}; // MAX_PATH在API中有定义

int len=GetModuleFileNameA(NULL,

        exeFullPath, //应用程序的全路径存放地址

        MAX_PATH);

std::string path = exeFullPath; //配置文件名

path = path.substr(path.rfind("\\")+1);



HANDLE hMutex = ::CreateMutex(NULL,TRUE,("Global\\"+ path).c_str());



if(GetLastError() == ERROR_ALREADY_EXISTS || ERROR_ACCESS_DENIED == GetLastError())

{

    MessageBox(NULL,"Has been running!","MT4 Manager Interface", MB_ICONEXCLAMATION );

    return FALSE;

}

return FALSE;
}
···

posted @ 2019-08-05 14:52  hostid  阅读(165)  评论(0编辑  收藏  举报