 |
|
 |
I've got a program which is allowed to be started just once, but sometimes more instances of the program are allowed.
If I've never instantiated the CSingleInstanceObject in the code, the program crashed on exit, because m_data of CSingleInstanceImpl never got initialized.
I guess this is a bug, because all the other data members of CSingleInstanceImpl are initialized in the constructor.
Here is the code to fix this bug:
CSingleInstanceImpl
CSingleInstanceImpl::CSingleInstanceImpl ( const CSingleInstance* aOwner )
: mOwner ( aOwner ),
mEvent ( NULL ),
mSignal ( NULL ),
mData (NULL) {
}
Otherwise great code !
All the label says is that this stuff contains chemicals "... known to the State of California to cause cancer in rats and low-income test subjects." Roger Wright http://www.codeproject.com/lounge.asp?select=965687&exp=5&fr=1#xx965687xx
|
|
|
|
 |
|
 |
Hi there,
I was tring to use your code in a case where I need to have an application running, if someone else would try to load it again, it would refuse to work... I don't need the restore thing.
Now why it doesn't work: if someone log into the machine via Terminal Service or remote desktop, it will not validate the instance... In fact, your implementation won't work, and nither the solutions posted elsewere...
Besides using a kind of Windows service is there any other way to limit the instances running?
Regards,
ALMC
|
|
|
|
 |
|
 |
When I first used this code in my MDI application under Win2K, it crashed trying to set the foreground window in ActivatePreviousInstance(). I had to add a test:
if ( pWndChild )
pWndChild->SetForegroundWindow();
else
wndPrev.SetForegroundWindow();
|
|
|
|
 |
|
 |
Make sure you have the latest version from my web site as there have been a number of updates since I posted to codeproject
|
|
|
|
 |
|
 |
Now, I execute my dialog-based app and minimize into system tray,after that I execute my app again,it can't restore from system tray,how can I do it? can anyone help me? thankx.
|
|
|
|
 |
|
 |
just broad cast a mess using post message and capture it in the systray class and reactive the dialog from there.
|
|
|
|
 |
|
 |
I included the files and put the example code into my InitInstance() function, and it just doesn't work. Specifically, when I call TrackFirstInstanceRunning(), it fails an assertion in afxwin2.inl (line 261):
_AFXWIN_INLINE CWnd* CWnd::GetLastActivePopup() const
{ ASSERT(::IsWindow(m_hWnd)); return CWnd::FromHandle(::GetLastActivePopup(m_hWnd)); }
I've included the example code as demonstrated. It doesn't even work if you put it at the end of InitInstance. Blah.
|
|
|
|
 |
|
 |
Make sure you have the latest version from my web site as there have been a number of updates since I posted to codeproject
|
|
|
|
 |
|
 |
Hello
Is it possbile that a similar class can be used in WTL SDI Application.
If yes Could anyone direct me to such a URL/Article/etc.
Thank in advance
|
|
|
|
 |
|
 |
I have inserted the required code and everything is working fine except that it does not restore the previous instance of the app.
Any ideas why this is happening?
Deb
|
|
|
|
 |
|
 |
Make sure you have the latest version from my web site as there have been a number of updates since I posted to codeproject
|
|
|
|
 |
|
 |
What if the program is a dialog application?
|
|
|
|
 |
|
|
 |
|
 |
The latest version on my web site includes an example on how to use it with a dialog app
|
|
|
|
 |
|
 |
What if i want to kill the previos instance instead of restoring it....?
|
|
|
|
 |
|
 |
Just post a WM_CLOSE message to it.
|
|
|
|
 |
|
 |
What if i want to kill the previos instance instead of restoring it....?
|
|
|
|
 |
|
 |
It seems the second check
if (!instanceChecker.PreviousInstanceRunning())
is needless (see comments /// below)
Thanks, Eugene
CMyApp::InitInstance()
{
CInstanceChecker instanceChecker;
if (instanceChecker.PreviousInstanceRunning())
{
AfxMessageBox(_T("Previous version detected, will now restore it"), MB_OK);
instanceChecker.ActivatePreviousInstance();
return FALSE;
}
....
// create main MDI Frame window
CMainFrame* pMainFrame = new CMainFrame;
m_pMainWnd = pMainFrame;
if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
return FALSE;
// If this is the first instance of our App then track it so any other instances can find it.
/// ===> if (!instanceChecker.PreviousInstanceRunning())
/// this line is needless,you already checked it above,
/// so we are sure it's the first instance
instanceChecker.TrackFirstInstanceRunning();
.....
}
|
|
|
|
 |
|
 |
Hi
How to use this class in a dialog based application?
akra
|
|
|
|
 |
|
 |
sent instanceChecker object into that dialog and call code below in InitDialog
// If this is the first instance of our App then
// track it so any other instances can find it.
if (!instanceChecker.PreviousInstanceRunning())
instanceChecker.TrackFirstInstanceRunning();
|
|
|
|
 |
|
 |
The code was a great help but I have a Logon screen which is displayed before the MainFrame is created and would like to know if it is possible to check whether an instance exists before this
|
|
|
|
 |
|
 |
Hi,
I looked at the code and wondered, what happens if the app crash?
Will the next instance work?
Thanks,
El
|
|
|
|
 |
|
 |
Hi !
If that should happen Windows deletes all Memory Objects which belong to that App.
And so the next Instance will work !
Note : You have to close the MessageBox caused by the crash for Example Unhandled Exception first !
HtH
|
|
|
|
 |
|
 |
TrackFirstInstanceRunning() is not in the source code
|
|
|
|
 |
|
 |
It is on line 126 of sinstance.cpp Maybe your download was't quite right
|
|
|
|
 |