Single Application Instance

Classes limiting an application to a single instance; includes command line passing.


Change the applications _Module to be of type CSaiAppModule, constructing it with CSaiAppModule _Module(_T("<uniqueAppId/Name>"));. The overriden CAppModule::Init() returns APP_S_INSTANCE_RUNNING when a running instance of the application is found.

int nRet = 0;
hRes = _Module.Init(NULL, hInstance);
    nRet = Run(lpstrCmdLine, nCmdShow);

The applications CMainFrame needs to derive from CSaiFrameWindowImpl when the build-in command line passing should be used. The implementation calls void OnReceivedCmdLine(LPCTSTR pCmdLine) when a new instance passes its command line. The receiving application should consider the pCmdLine read-only. The pCmdLine parameter is valid only during the processing of the function. If the receiving application must access the data after OnReceivedCmdLine returns, it must copy the data into a local buffer.

About the Implementation

CSharedCmdLine CSaiAppModule CSaiFrameWindowImpl