I'm looking for some C++ code or direction in writing some to close an app if it detects another instance running.

Our little update utility writes a log file, which I'm thinking of using as the clue. I was thinking that before the second instance attempts to write to the .log file, if the MyApp.log is opened, I could just skip out.

How can I get that done with C++? I'm not a C++ guys, as you may have gathered from other posts, so if there is anything special I have to do to close an app (something needed other than a Me.Close type statement), please let me know.

Any help GREATLY appreciated!!

This is highly platform dependent. What OS are you targeting?

Use a mutex so that only one instance can run?
Check processes to see if a process with that name already is running (Not fool proof).

I'll be targeting Windows.

If you are targetting Windows platform, using a mutex, as suggested by triumphost, is, as my personal experience, the best way to do this. I have here a little sample here:

UINT g_uSyncMsg;

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int)
{
    g_uSyncMsg = RegisterWindowMessage("any msg string you prefer");
    HANDLE hMutex = CreateMutex(NULL, TRUE, "unique mutex name you prefer");

    if( ERROR_ALREADY_EXISTS==GetLastError() )
    {
        PostMessage(HWND_BROADCAST, g_uSyncMsg, 0, 0);
        CloseHandle(hMutex);
        return 0;
    }
    ...
    ...
    ...
}

You can also do the same (with the necesary modification) in a console app.

Hope this can help you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.