I’m writing an application with shared memory and am creating named mutexes with the appropriate "Local\" prefix on the name. However, every time I call the CreateMutex function to create the handles, I get a NULL return value. I even try calling OpenMutex after that and get a NULL return.
The GetLastError() function returns 6 which means ERROR_INVALID_HANDLE. I believe that this happens on the first attempt to create this named mutex in any process. I included windows.h after including some MFC components and am using CMutex elsewhere in the application; so I don’t know if this is a problem or not. I am passing NULL and FALSE for the first two parameters always and am using Windows XP.
This is a summary of what my code does:
char to_name[16] = "Local\\to_1";
d_mutex_to_h = CreateMutex(NULL, FALSE, to_name);
if (d_mutex_to_h == NULL)
{
d_mutex_to_h = OpenMutex(NULL, FALSE, to_name);
}
From the MSDN documentation for CreateMutex:
Try using WinObj to see if there’s a non-mutex object with the same name.