Archived

This topic is now archived and is closed to further replies.

_dot_

Windows 2000 breaking my codes?(Mutex problem)

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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 );
}


Share this post


Link to post
Share on other sites