Click here to Skip to main content
16,012,611 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionRe: Problem with CreateMutex() and CloseHandle() Pin
Kiran Pinjala19-Dec-07 22:31
Kiran Pinjala19-Dec-07 22:31 
GeneralRe: Problem with CreateMutex() and CloseHandle() Pin
Sarath C19-Dec-07 23:03
Sarath C19-Dec-07 23:03 
GeneralRe: Problem with CreateMutex() and CloseHandle() Pin
Kiran Pinjala19-Dec-07 23:11
Kiran Pinjala19-Dec-07 23:11 
GeneralRe: Problem with CreateMutex() and CloseHandle() Pin
Sarath C19-Dec-07 19:41
Sarath C19-Dec-07 19:41 
GeneralRe: Problem with CreateMutex() and CloseHandle() Pin
ashishbhatt19-Dec-07 20:56
ashishbhatt19-Dec-07 20:56 
AnswerRe: Problem with CreateMutex() and CloseHandle() Pin
Member 75496020-Dec-07 3:37
Member 75496020-Dec-07 3:37 
GeneralRe: Problem with CreateMutex() and CloseHandle() Pin
ashishbhatt24-Dec-07 18:18
ashishbhatt24-Dec-07 18:18 
GeneralRe: Problem with CreateMutex() and CloseHandle() Pin
Member 75496026-Dec-07 4:51
Member 75496026-Dec-07 4:51 
It is not clear to me which instance of the process you are trying to terminate. I have been assuming you want to terminate the second instance. I'll address that.

First what are the results we are looking for? The test program I use is the MFC program generated by MSVC App Wizard. The produces a program that will run two instances. When you do the Windows task manager will show both instances in the process list. To make the test results easy to see, sort the Task Manager process list by name in order to see the two processes listed together. If you then terminate one instance it will be removed from the process list after it has terminated. This is the result we are seeking. What we want is a way for the second instance of the program to detect the first instance of the program. When the first instance is detected the second program instance should terminate itself, just as if we had done it from the application. As before, the task manager process list will reflect that the second instance of the program terminated and remove it from the process list, leaving just the first instance running.

The way we detect the second instance of the program is to use a mutex object. Each instance of the program tries to create the mutex. This is doen when the application starts during InitInstance().

BOOL CMfctestApp::InitInstance()
{
	//...

	m_hMutex = ::CreateMutex(0, FALSE, "TestApp");
	if (::GetLastError() == ERROR_ALREADY_EXISTS)
	{
		TRACE("TestApp: Already running");
		// explicitly close the mutex handle
		::CloseHandle(m_hMutex), m_hMutex = 0;
		return FALSE;	// do not run second time
	}
	
	// ...
}

Note the use comma sequence operator and the explicit assignment of the mutex handle. A second instance of the application will get a valid handle to the existing mutex. Once we have closed the handle we do not want to use it again. We declare the m_hMutex handle as a private member variable in the application class,

class CMfctestApp : public CWinApp
{
	// ...
private:
	HANDLE m_hMutex;
};

Finally, we insure that the first instance of the application will properly release the mutex when the application terminates. This is done in ExitInstance()

int CMfctestApp::ExitInstance() 
{
	if (m_hMutex)
		::CloseHandle(m_hMutex);
	
	return CWinApp::ExitInstance();
}

The first instance of the program will have a valid, non null mutex handle and will close it. All other instances will have handle of 0.

If now run the program outside of the debugger and look at the Task Manager process list and you will see the first instance running. In the MSVC IDE place a breakpoint in the InitInstance member function and run the program under the debugger. When the breakpoint is hit look at the process list in the Task Manager and you will see both instances running. Let the second instance of the program, in the debugger, continue and you should see the process get removed from the task list.
GeneralRe: Problem with CreateMutex() and CloseHandle() Pin
DevelopmentNoob16-Dec-09 19:56
DevelopmentNoob16-Dec-09 19:56 
GeneralRe: Problem with CreateMutex() and CloseHandle() Pin
NehaMishra286846-Dec-10 19:31
NehaMishra286846-Dec-10 19:31 
GeneralDrag Drop Pin
john563219-Dec-07 18:01
john563219-Dec-07 18:01 
GeneralRe: Drag Drop Pin
Hamid_RT19-Dec-07 18:17
Hamid_RT19-Dec-07 18:17 
GeneralHelp Needed for getting record from database Pin
Y_Kaushik19-Dec-07 17:53
Y_Kaushik19-Dec-07 17:53 
GeneralRe: Help Needed for getting record from database Pin
Roger Broomfield19-Dec-07 18:57
Roger Broomfield19-Dec-07 18:57 
GeneralRe: Help Needed for getting record from database Pin
Y_Kaushik19-Dec-07 19:22
Y_Kaushik19-Dec-07 19:22 
GeneralRe: Help Needed for getting record from database Pin
Roger Broomfield19-Dec-07 19:41
Roger Broomfield19-Dec-07 19:41 
GeneralRe: Help Needed for getting record from database Pin
Y_Kaushik19-Dec-07 19:53
Y_Kaushik19-Dec-07 19:53 
GeneralRe: Help Needed for getting record from database Pin
Y_Kaushik19-Dec-07 20:01
Y_Kaushik19-Dec-07 20:01 
GeneralRe: Help Needed for getting record from database Pin
Y_Kaushik19-Dec-07 20:36
Y_Kaushik19-Dec-07 20:36 
QuestionNeed help with restricting window movement to the screen... Pin
demxine19-Dec-07 16:35
demxine19-Dec-07 16:35 
GeneralRe: Need help with restricting window movement to the screen... Pin
Maxwell Chen19-Dec-07 16:45
Maxwell Chen19-Dec-07 16:45 
QuestionRe: Need help with restricting window movement to the screen... [modified] Pin
demxine19-Dec-07 19:47
demxine19-Dec-07 19:47 
GeneralFake Memory Leak!! Pin
Lea Hayes19-Dec-07 16:06
Lea Hayes19-Dec-07 16:06 
GeneralRe: Fake Memory Leak!! [modified] Pin
Maxwell Chen19-Dec-07 16:34
Maxwell Chen19-Dec-07 16:34 
GeneralRe: Fake Memory Leak!! Pin
Lea Hayes19-Dec-07 21:14
Lea Hayes19-Dec-07 21:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.