|
使用FindWindow和Mutex都可以,不过FindWindow有可能会失败:
给你个Mutex的例子!
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
HANDLE hMutex = CreateMutex(NULL,false,L"myApp.exe");
if(GetLastError()==ERROR_ALREADY_EXISTS)
{
RETAILMSG(true,(L"myApp.exe already exist --------\r\n"));
CloseHandle(hMutex);
return false;
}
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
if(hMutex)
{
ReleaseMutex(hMutex);
CloseHandle(hMutex);
}
} |
|