Click to See Complete Forum and Search --> : prevInstance


October 4th, 1999, 01:32 AM
How can I define the previous instance of my application in VC6. The member m_hPrevInstance in 32 bit app is always set to NULL. The method FindWindow looks bad to me, because I don't want use window title to search.

JohnCz
October 4th, 1999, 09:50 PM
And you are right.
There is many other ways to check if your app is running. For example you can create memory file and check for it’s presence. If it exists refuse to run another instance.
Named pipes will also do.

Casper
November 2nd, 1999, 03:31 AM
You can use Mutex for that.
First, in InitInstance() you should create it:

HANDLE test=CreateMutex(NULL,TRUE,"MyMutex");

If then you run another app copy, this call will
return ERROR_INVALID_HANDLE and GetLastError() will return ERROR_ALREADY_EXISTS value.
Don't forget to close mutex after program termination in ExitInstance():

CloseHandle(test);