Multi windows lock

How to lock launching few windows of the game on windows and mac?
When user try to launch game one more time, I have to show him already launched window or show message that game already launched (first variant much better)
Any ideas?

2 Likes

The easiest thing that comes to mind is to modify a file on disk to say that the game is running, and change it back when you quit. Check it on startup to see if there’s another instance already running. I don’t know about switching windows though, you would need a Native Extension for that, no?

If you go this way you will want to allow the user a dialog which says “Game is already running if you run multiple copies bad things could happen. Do you wish to continue?” in case of bad exit.

Better way is Interprocess Communications which is supported on OSX and Windows. We could add it to DefOS.

I think way with lock file is bad way.

Now I’m reading winapi documentation (mutex and so on) and think how to make this NE.

1 Like

On Mac it’s already works. When I try to open my app one more time a macOS just make opened window active. I love my mac.
But I need this behavior for windows =(

1 Like

https://www.codeproject.com/Articles/538/Avoiding-Multiple-Instances-of-an-Application

Could test some of these to see what works best for you?

I already try many variants, and main problem is : I can’t make first window active.

What do you mean by can’t make first window active? Do you mean selecting the first window?

What I want to do:

  1. Open first window
  2. Roll up first window
  3. Double tap to exe
  4. First window activate and bring to top

Open EXE, minimize running game, open EXE again, the first copy of the game is maximized?

1 Like

right! (and second window should close, of course)

I can’t find way to activate first window. Current variant (Maybe it will be useful for someone) :

#define EXTENSION_NAME DefOneWindow
#define LIB_NAME "DefOneWindow"

#define DLIB_LOG_DOMAIN LIB_NAME
#include <dmsdk/sdk.h>

#if defined(DM_PLATFORM_WINDOWS)

#include <windows.h>

dmExtension::Result AppInitialize(dmExtension::AppParams* params)
{
 const char* name = dmConfigFile::GetString(params->m_ConfigFile, "project.title", 0);
 HANDLE m_singleInstanceMutex = CreateMutex(NULL, TRUE, name);
 if (m_singleInstanceMutex == NULL || GetLastError() == ERROR_ALREADY_EXISTS) 
 {
	MessageBox( nullptr, TEXT( "The game is already launched." ), TEXT( "Message" ), MB_OK );
	ExitProcess(0);
 }
    return dmExtension::RESULT_OK;
}

dmExtension::Result Initialize(dmExtension::Params* params)
{
    return dmExtension::RESULT_OK;
}

dmExtension::Result AppFinalize(dmExtension::AppParams* params)
{
    return dmExtension::RESULT_OK;
}

dmExtension::Result Finalize(dmExtension::Params* params)
{
    return dmExtension::RESULT_OK;
}

#else 

dmExtension::Result AppInitialize(dmExtension::AppParams* params)
{
    return dmExtension::RESULT_OK;
}

dmExtension::Result Initialize(dmExtension::Params* params)
{
    return dmExtension::RESULT_OK;
}

dmExtension::Result AppFinalize(dmExtension::AppParams* params)
{
    return dmExtension::RESULT_OK;
}

dmExtension::Result Finalize(dmExtension::Params* params)
{
    return dmExtension::RESULT_OK;
}

#endif

DM_DECLARE_EXTENSION(EXTENSION_NAME, LIB_NAME, AppInitialize, AppFinalize, Initialize, 0, 0, Finalize)
1 Like