Originally posted by: Ben Key
I am just beginning my study of windows programming. I am using Visual C++ 6.0 and I am developing for Windows NT primarily. I had several problems figuring out how to use your example and to incorporate it into my own applications.
The first problem I had is your sample application will not compile. I got external link errors that I could not figure out.
The second, and largest problem is that you used that AppWizard (I prefer the term AppIdiot) to make your example and therefore your application has so many files and so much unnecessary code that it is a major chore just to navigate arround in the project to figure out what you are trying to do.
The third problem is that I can not find in any of the twenty or more files, how the variable
m_strClassName gets any other value than " Class." In the following code segement, I assume you
are assinging this variable a value, but I do not see how. I have commented sections that I do not
understand.
CString strFullString;
// Create our class name string
**
What does this line do? Is it a function that I do not recognize?
**
if ( strFullString.LoadString( nID ) ) {
// Extract the first sub-string
**
I could not find any documentation of the function AfxExtractSubString. What are the parameters,
especially the last parameter? I assume that it is extracting a portion of the string strFullString and
making m_strClassName equal to that portion of strFullString, however, I could not find anywhere where
strFullString gets its value. So in that case, you are making m_strClassName equal to a portion of a string
that has no value.
**
AfxExtractSubString( m_strClassName, strFullString, 0 );
}
// Add the word 'Class' to the end
**
Why add class to the end, unless as I suspect m_strClassName has no value up to this point so you are
actually making it equal to " Class"?
**
m_strClassName += _T(" Class");
The fourth problem is why use all that mess with CreateMutex. Why not simply just assign a value to
m_strClassName and use FindWindowEx (the only portion of your code I understood) to see if a window of the
class exists. It is definitely much simpler than CreateMutex (what exactly is a Mutex Object anyway, the
MSDN documentation was more than a little vague) and I believe it would do the same work with a whole lot
less code.
My last question about your sample is how do the following functions ever get called? I cannot find reference to them in any of the other .cpp files (which I believe you have way to many of).
CSingleInstance::CSingleInstance()
{
// Set our default values
m_hMutex = NULL;
}
CSingleInstance::~CSingleInstance()
{
if ( m_hMutex != NULL ) {
ReleaseMutex( m_hMutex );
}
}
I know the first is supposed to initialize the variable m_hMutex and the second is supposed to free it, but how do they get called?
ReplyOriginally posted by: Chris Brown
While this code works Ok for Win32 it seems to hang on a CE device(MIPS Palm PC) when not running under emulation. Do not know why but its related to the registering of the windows class.
My solution was not to register my own windows class but save the one given by MFC (something like Afx:4000:8483:8238c4) to the registry in when the first instance starts. Then when the second instance starts it gets this name to look for the mainframe to popup.
It's a slightly simplier implementation and less prone to window registration problems.
ReplyOriginally posted by: Colin Johnson
This essentially uses simillar method to my solution.
There is a second "Single Instance of App class" post on here.
Look @ my solution to fix these bugs.
Originally posted by: Ryan Schneider
I'm using this in a Dialog based app. It blocks the spawning of a second app just fine, but doesn't seem to find the window to bring it to the front.
Since it's a Dialog based app, i put the PreCreateWindow code in the main dialog. I also change IDR_MAINFRAME to the Dialogs ID (when I right-clicked on IDR_MAINFRAME and selected "find defintion of" it pointed to themain dialogs ID..)
Any ideas?
Originally posted by: Pedro
A cool piece of code. Using a mutex is neat, I tried it on my project and it works. However... in my particular case (a HUGE MDI app, the presence of the mutex, for some unknown reason, introduced a perceptible delay in the mainframe window refreshing (after a child window is closed), which creates an unpleasant flicker. So, reluctantly I had to drop it...
Reply