Adlandırılmış Nesneleri Kullanma
Aşağıdaki örnek, adı verilen nesne adlarının kullanımını, adlandırılmış bir mutex oluşturup açarak göstermektedir.
İlk İşlem
İlk işlem, mutex nesnesini oluşturmak için CreateMutex işlevini kullanır. Aynı ada sahip mevcut bir nesne olsa bile bu işlevin başarılı olduğunu unutmayın.
#include <windows.h>
#include <stdio.h>
#include <conio.h>
// This process creates the mutex object.
int main(void)
{
HANDLE hMutex;
hMutex = CreateMutex(
NULL, // default security descriptor
FALSE, // mutex not owned
TEXT("NameOfMutexObject")); // object name
if (hMutex == NULL)
printf("CreateMutex error: %d\n", GetLastError() );
else
if ( GetLastError() == ERROR_ALREADY_EXISTS )
printf("CreateMutex opened an existing mutex\n");
else printf("CreateMutex created a new mutex.\n");
// Keep this process around until the second process is run
_getch();
CloseHandle(hMutex);
return 0;
}
İkinci İşlem
İkinci işlem, mevcut mutex'e bir tanıtıcı açmak için OpenMutex işlevini kullanır. Belirtilen ada sahip bir mutex nesnesi yoksa bu işlev başarısız olur. Erişim parametresi, tanıtıcının herhangi bir bekleme işlevinde kullanılması için gerekli olan mutex nesnesine tam erişim talep eder.
#include <windows.h>
#include <stdio.h>
// This process opens a handle to a mutex created by another process.
int main(void)
{
HANDLE hMutex;
hMutex = OpenMutex(
MUTEX_ALL_ACCESS, // request full access
FALSE, // handle not inheritable
TEXT("NameOfMutexObject")); // object name
if (hMutex == NULL)
printf("OpenMutex error: %d\n", GetLastError() );
else printf("OpenMutex successfully opened the mutex.\n");
CloseHandle(hMutex);
return 0;
}
İlgili konular