Sign in to follow this  
dbzprogrammer

WinMain function

Recommended Posts

They say hPrevInstance doesn't do anything in the WinMain function and that it's always NULL. But I've been thinking... If you have a previence instance of that application running, and you start another, will the hPrevInstance not be NULL, but instead point to your previous application instance?

Share this post


Link to post
Share on other sites
hPrevInstance is a throw back to the days of Windows 3.11. If you want to check if a previous instance of your program is running a better approach is to use CreateMutex to create a named mutex followed by a call to GetLastError. If GetLastError returns ERROR_ALREADY_EXISTS then there is a previous instance of the program running.

// edit - Here the method is laid out more explicitly: How do I prevent a the user from starting a second instance of my app?.

Share this post


Link to post
Share on other sites
You can just try it out and see. hehe, I always try it this way (in your main program) do:




char test_string [50] = "" ;
sprintf ( test_string, "%d", hInstance) ;
MessageBox ( NULL, test_string, "hInstance", MB_OK ) ;

if ( hPrevInstance != NULL )
{
sprintf ( test_string, "%d", hPrevInstance ) ;
MessageBox ( NULL, test_string, "hPrevInstance ", MB_OK ) ;
}




Then you can check it out and see what happen

Share this post


Link to post
Share on other sites
Quote:
Original post by shermy101
i always thought it was a handle to the process that spawned hInstance.. :S


Then it would be a HANDLE not a HINSTANCE. Under 32 bit windows (95,98,Me,Nt,2k,XP), every process has it's own address space. The HINSTANCE of a process usually (99.9% of the time) has a value of 0x00400000. This is the address where the actual program - the exe file itself - is mapped into the process address space. If the hPrevInstance was passed along from the previous process to the current process it would have the value 0x00400000 but in the current process that would point to the HINSTANCE of the current process and not the previous process.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this