Tech Support > Microsoft Windows > Development Resources > Using hPrevInstance when registering window
Using hPrevInstance when registering window
Posted by gw7rib@aol.com on October 4th, 2006


I'm using Visual Express C++ and Windows XP, but the book I am using
("Programming Windows" by Charles Petzold) is very old, refering to C
and Windows 2. There are a few differences between what the book says
and what the situation is now, but I think I am top of most of them.
However, I have a query as regards registering window classes.

In the book, there is a line

if (!hPrevInstance)

before the code to register a window class with RegisterClass. The
theory is that, if another copy of the same program is already running,
the window class is already registered. Visual C++ will write a basic
windows program for me, but it does not include this line, running
RegisterClassEX whether or not hPrevInstance is NULL. I was wondering
if there was any significance to this - the rest of the windows program
looks similar to what is in my book and so I would have expected this
bit too to be the same unless there was some sort of problem.

So can you fill me in on this?

Thanks in advance.
Paul.

Posted by Michael on October 4th, 2006


I believe this used to be the case in dreadfully old versions of
Windows, but isn't any more. From the help on WinMain:

int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPWSTR lpCmdLine,
int nShowCmd
);

....

hPrevInstance
[in] Handle to the previous instance of the application. For a
Win32-based application, this parameter is always NULL.
If you need to detect whether another instance already exists, create a
uniquely named mutex using the CreateMutex function. CreateMutex will
succeed even if the mutex already exists, but the GetLastError function
will return ERROR_ALREADY_EXISTS. This indicates that another instance
of your application exists, because it created the mutex first.

Michael



Similar Posts