Windows 2000 breaking my codes?(Mutex problem)

Started by
3 comments, last by _dot_ 24 years ago
I just found out windows 2000 breaking a small part of my codes. It used to work in Windows 98. Here is the snipper of the codes that didn''t work char strFileName[255]; ::ZeroMemory( strFileName, sizeof( strFileName ) ); GetModuleFileName( NULL, strFileName, sizeof( strFileName ) ); // create mutex, if succeeded means only instance HANDLE hMutex = ::CreateMutex( NULL, TRUE, strFileName ); if ( ::GetLastError() == ERROR_ALREADY_EXISTS ) { ::CloseHandle( hMutex ); // jump to previous game instance HWND hWnd = ::FindWindowEx( NULL, NULL, GFC_GameUtils::getWindowClassName(), NULL ); if ( hWnd ) { ::ShowWindow( hWnd, SW_RESTORE ); ::BringWindowToTop( hWnd ); ::SetForegroundWindow( hWnd ); } return false; } return true; When I ran this application in Windows 98, it worked(I had only 1 instance running max, and second instances sets focus to the first instance. However, in Windows 2000, i can get multiple application instances. Anyway can explain why?
Advertisement
The mutex name is invalid. (i.e. C:\MyProgram\MyProgram.exe)

Check the documentation for ::CreateMutex. It supports new prefixes "Global\" and "Local\" on 2000. Also, it specifically states that backslashes are not valid characters for the NT and 9x platforms. (Why it worked, I don''t know.)

Tim
This isn''t an answer to your question, since I think that that will work, but actually a question. I do pretty much the same thing in my game, but when I recognize that I''ve already got another instance of the game running, I simply quit the current one. I was wondering how you bring the other instance to the front and make it active.

Thanks,

Pythius
"The object of war is not to die for your country, but to make the other bastard die for his"
The following code snipper hops to the previous game instance
// jump to previous game instance
HWND hWnd = ::FindWindowEx( NULL, NULL, GFC_GameUtils::getWindowClassName(), NULL );
if ( hWnd ) {
::ShowWindow( hWnd, SW_RESTORE );
::BringWindowToTop( hWnd );
::SetForegroundWindow( hWnd );
}


Thanks, that works perfectly.

Pythius
"The object of war is not to die for your country, but to make the other bastard die for his"

This topic is closed to new replies.

Advertisement