at the beginning of your cpp file#pragma data_seg("YourSec") LONG g_Global=0; #pragma data_seg()#pragma comment(linker,"/section:YourSec,RWS")at the beginning of your entry point function:if(InterLockedIncrement(g_Global)!=1) InterLockedDecrement(g_Global); return 0;
Oh I am sorry, for a problems:the right code is below:at the beginning of your entry point function:if(InterlockedIncrement(&g_Global)!=1) { InterlockedDecrement(&g_Global); return 0; }
HANDLE hd=::CreateMutex(NULL,TRUE,*****);
if(GetLastError()==ERROR_ALREADY_EXISTS)
{
return FALSE;
}
*****为你编译后的EXE文件的的文件名
LONG g_Global=0;
#pragma data_seg()#pragma comment(linker,"/section:YourSec,RWS")at the beginning of your entry point function:if(InterLockedIncrement(g_Global)!=1)
InterLockedDecrement(g_Global);
return 0;
{
InterlockedDecrement(&g_Global);
return 0;
}
//======================================================================
// 函 数 名: RunAlready
// 功能描述: 保证程序只运行一次
// 输入参数:
// 输出类型: bool
// 创建日期:
// 修改日期:
// 作 者:
// 附加说明:
//======================================================================
const char *myAtonstring = "hello";
ATOM myAtom = 0;
bool RunAlready()
{
myAtom = GlobalFindAtom(myAtonstring);
if (myAtom != 0) return true; //已运行
myAtom = GlobalAddAtom(myAtonstring);
return false; //没有运行
}程序结束时:GlobalDeleteAtom(myAtom);GlobalDeleteAtom(myAtom);
BOOL C???App::InitInstance()
{
.....
if(RunAlready())//找到原子
return false;
.....
GlobalDeleteAtom(GlobalAtom);
return FALSE;
}
不知houjzs()