일반적인 유틸리티 DLL에있는 아래 함수를 사용합니다. 이 방법은 WIN32에서 여러 인스턴스를 방지하는 방법을 설명하는 Microsoft 문서에서 파생됩니다.
#define STRICT
#include <stdheaders.h>
HANDLE ghSem;
BOOL IExist(LPSTR lpszWindowClass)
{
HWND hWndMe;
int attempt;
for(attempt=0; attempt<2; attempt++)
{
// Create or open a named semaphore.
ghSem = CreateSemaphore(NULL, 0, 1, lpszWindowClass);
// Close handle and return NULL if existing semaphore was opened.
if((ghSem != NULL) &&
(GetLastError() == ERROR_ALREADY_EXISTS))
{ // Someone has this semaphore open...
CloseHandle(ghSem);
ghSem = NULL;
hWndMe = FindWindow(lpszWindowClass, NULL);
if(hWndMe && IsWindow(hWndMe))
{ // I found the guy, try to wake him up
if(SetForegroundWindow(hWndMe))
{ // Windows says we woke the other guy up
return TRUE;
}
}
Sleep(100); // Maybe the semaphore will go away like the window did...
}
else
{ // If new semaphore was created, return FALSE.
return FALSE;
}
}
// We never got the semaphore, so we must
// behave as if a previous instance exists
return TRUE;
}
은 당신의 WinMain에서 같은 것을 할 :
물론
if(IExist("MyWindowClass"))
{
return 1;
}
, 당신은 활성화로 (첫 번째 인스턴스가 아닌 경우에 할 당신이 필요로하는 무엇으로 수익을 대체 할 수있는 기존의 예).