Click here to Skip to main content
15,905,558 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionRe: Access Violation. Pin
David Crow18-Jul-07 2:41
David Crow18-Jul-07 2:41 
Questionhelp with files Pin
josip cagalj17-Jul-07 23:53
josip cagalj17-Jul-07 23:53 
AnswerRe: help with files Pin
PravinSingh18-Jul-07 1:45
PravinSingh18-Jul-07 1:45 
AnswerRe: help with files Pin
David Crow18-Jul-07 2:44
David Crow18-Jul-07 2:44 
AnswerRe: help with files Pin
Hamid_RT25-Jul-07 8:30
Hamid_RT25-Jul-07 8:30 
QuestionSubclassing Issue Pin
LoveCPlusplus17-Jul-07 23:42
LoveCPlusplus17-Jul-07 23:42 
AnswerRe: Subclassing Issue Pin
Mike Dimmick18-Jul-07 2:53
Mike Dimmick18-Jul-07 2:53 
QuestionNamedpipe Pin
charian092017-Jul-07 23:35
charian092017-Jul-07 23:35 
Hi!

Good day Smile | :)

I have this problem about synchronization on namedpipe ...
I have this namedpipe server that allows multiple instances.
The algorithm I have is I'll wait for a client to connect to the namedpipe then spawn a thread which will read the message from the namedpipe. My expectation on the spawned thread is that when the client has disconnected the spawned thread will end also. But when I tried testing my code, the thread count keeps on increasing due to the spawned thread, which i think means I was not able to detect that the client had disconnected or something else Frown | :(

Any help is greatly appreciated.

Below are my codes:
1. Createnamedpipe, wait for a client to connect, and spawned a thread.
<br />
UINT fnRcvMsgsFrmNamedpipeThread(LPVOID pParam)<br />
{<br />
<br />
	DWORD	dwResult = SUCCEED;	<br />
	CString strLog( _T( "" ) );<br />
<br />
	OVERLAPPED stClientConnectOverLap = {0};<br />
	stClientConnectOverLap.hEvent = CreateEvent(<br />
					NULL,    // no security attributes<br />
					TRUE,    // manual reset event<br />
					FALSE,   // not-signalled<br />
					NULL);   // no name<br />
<br />
	if( stClientConnectOverLap.hEvent == NULL )<br />
	{<br />
	 return 0;<br />
	}<br />
	<br />
	HANDLE	hEvents;<br />
	hEvents = stClientConnectOverLap.hEvent;<br />
<br />
	CString strFormatPipeName( _T( "" ) );<br />
	strFormatPipeName.Format( "\\\\%s\pipe\mypipename", STR_PERIOD );<br />
<br />
	HANDLE hClientReqPipe;<br />
	<br />
	while ( TRUE )<br />
	{<br />
	hClientReqPipe = CreateNamedPipe( strFormatPipeName,<br />
					 FILE_FLAG_OVERLAPPED | PIPE_ACCESS_DUPLEX,									          PIPE_TYPE_MESSAGE |  PIPE_READMODE_MESSAGE | PIPE_WAIT,<br />
				          PIPE_UNLIMITED_INSTANCES,	<br />
                                              0, 0, 10000, NULL );   <br />
	if( hClientReqPipe == INVALID_HANDLE_VALUE )<br />
	{	<br />
	dwResult = GetLastError();<br />
	break;<br />
	}<br />
<br />
	if( !ConnectNamedPipe( hClientReqPipe, &stClientConnectOverLap ) )<br />
	{<br />
	   if( GetLastError() != ERROR_PIPE_CONNECTED )<br />
	   {<br />
		DWORD dwRet = WaitForSingleObject( hEvents, INFINITE );<br />
		if( dwRet != WAIT_OBJECT_0 )<br />
		{<br />
		    dwResult = GetLastError();<br />
		    break;<br />
		}<br />
	   }<br />
<br />
	}<br />
	CWinThread *pReadMsgThread = AfxBeginThread( fnReadMsgFrmNamedpipe, (LPVOID) hClientReqPipe );<br />
	if( NULL == pReadMsgThread )<br />
	{<br />
	break;<br />
	}<br />
	<br />
        }<br />
<br />
	// Close handles<br />
	::CloseHandle( hClientReqPipe );<br />
	::CloseHandle( stClientConnectOverLap.hEvent );<br />
	<br />
	return dwResult;<br />
}<br />


2. Spawned thread
<br />
UINT fnReadMsgFrmNamedpipe(LPVOID pParam)<br />
{	<br />
	I32L	dwResult = SUCCEED;	<br />
	CString strLog( _T("") );<br />
	HANDLE hClientReqPipe = (HANDLE) pParam;<br />
<br />
	OVERLAPPED stClientRecOverLap = {0};<br />
<br />
	stClientRecOverLap.hEvent = CreateEvent( NULL,    // no security attributes<br />
											 TRUE,    // manual reset event<br />
											 FALSE,   // not-signalled<br />
											 NULL );   // no name<br />
<br />
	if( stClientRecOverLap.hEvent == NULL )<br />
	{<br />
		return 0;<br />
	}<br />
	<br />
	HANDLE		hEvents;<br />
	hEvents = stClientRecOverLap.hEvent;<br />
<br />
	DWORD				dwTotalByte, dwRead, dwRet;<br />
<br />
	while ( TRUE )<br />
	{	<br />
		dwTotalByte = 0;<br />
		dwRead = 0;<br />
<br />
		ReadFile( hClientReqPipe, NULL, 0, &dwRead, &stClientRecOverLap );<br />
		dwRet = WaitForSingleObject( hEvents, INFINITE );<br />
		<br />
		if( dwRet != WAIT_OBJECT_0 )<br />
		{<br />
			dwResult = GetLastError();<br />
			break;<br />
		}<br />
		if( !PeekNamedPipe( hClientReqPipe, NULL, 0, NULL, &dwTotalByte, NULL ) )<br />
		{<br />
			dwResult = GetLastError();<br />
			break;<br />
		}<br />
<br />
		// There are messages to read in namedpipe<br />
		if( dwTotalByte > 0 )<br />
		{<br />
			// Allocate memory for the message based on the size from PeekNamedPipe<br />
			char*	pBuffer = new char[ dwTotalByte + 1 ];<br />
			if( !pBuffer )<br />
			{<br />
				continue;				<br />
			}<br />
			::ZeroMemory( pBuffer, dwTotalByte + 1 );<br />
<br />
			ReadFile( hClientReqPipe, pBuffer, dwTotalByte, &dwRead, &stClientRecOverLap );<br />
			dwRet = WaitForSingleObject( stClientRecOverLap.hEvent, 10000 );<br />
			if( dwRet != WAIT_OBJECT_0 )<br />
			{<br />
				dwResult = GetLastError();<br />
				break;<br />
			}<br />
<br />
			delete pBuffer;<br />
			<br />
		}<br />
	}<br />
	<br />
	DisconnectNamedPipe( hClientReqPipe );<br />
	CloseHandle( stClientRecOverLap.hEvent );<br />
	return dwResult;<br />
}<br />


3. Namedpipe Client
<br />
BOOL fnNewConnectNamedPipe(LPCTSTR lpczPipeName)<br />
{<br />
	HANDLE hPipe; <br />
	<br />
	// Try to open a named pipe; wait for it, if necessary. <br />
	while (TRUE)<br />
	{<br />
		hPipe = CreateFile( <br />
				lpczPipeName,   // pipe name <br />
				GENERIC_READ |  // read and write access <br />
				GENERIC_WRITE, <br />
				0,              // no sharing <br />
				NULL,           // default security attributes<br />
				OPEN_EXISTING,  // opens existing pipe <br />
				0,              // default attributes <br />
				NULL);          // no template file <br />
		<br />
		// Break if the pipe handle is valid. <br />
		<br />
		if (hPipe != INVALID_HANDLE_VALUE) <br />
			break; <br />
		<br />
		// Exit if an error other than ERROR_PIPE_BUSY occurs. <br />
		<br />
		if (GetLastError() != ERROR_PIPE_BUSY) <br />
		{<br />
			printf("Could not open pipe"); <br />
			return FALSE;<br />
		}<br />
		<br />
		// All pipe instances are busy, so wait for 20 seconds. 		<br />
		if ( !WaitNamedPipe(lpczPipeName, 20000) ) <br />
		{ <br />
			printf("Could not open pipe"); <br />
			return FALSE;<br />
		} <br />
	} <br />
	<br />
 	DWORD dwBytesWritten = 0;	<br />
	DWORD dwBytesWrite = m_strMsgText.GetLength();<br />
	DWORD dwRet;<br />
<br />
	OVERLAPPED WriteOverLap = {0};<br />
	WriteOverLap.hEvent = CreateEvent(<br />
								NULL,    // no security attributes<br />
								TRUE,    // manual reset event<br />
								FALSE,   // not-signalled<br />
								NULL);   // no name<br />
<br />
	HANDLE	hEvent;<br />
	hEvent = WriteOverLap.hEvent;<br />
	if( !WriteFile(hPipe, m_strMsgText, dwBytesWrite + 1, &dwBytesWritten, &WriteOverLap) )<br />
	{<br />
		if (GetLastError() == ERROR_IO_PENDING)<br />
		{<br />
			dwRet = WaitForSingleObject(WriteOverLap.hEvent, 10000);<br />
			if (dwRet != WAIT_OBJECT_0) // Something not correct happens<br />
			{<br />
				CloseHandle(WriteOverLap.hEvent);<br />
				CloseHandle(hPipe);<br />
				hPipe = NULL;<br />
				return FALSE;<br />
			}<br />
		}<br />
		else <br />
		{<br />
			CloseHandle(WriteOverLap.hEvent); <br />
			CloseHandle(hPipe);<br />
			hPipe = NULL;<br />
			return FALSE;<br />
		}<br />
	}<br />
	<br />
	CloseHandle(WriteOverLap.hEvent); <br />
	CloseHandle(hPipe);<br />
	hPipe = NULL;<br />
<br />
    return TRUE; <br />
}<br />

AnswerRe: Namedpipe Pin
Mark Salsbery18-Jul-07 6:19
Mark Salsbery18-Jul-07 6:19 
GeneralRe: Namedpipe Pin
charian092018-Jul-07 14:57
charian092018-Jul-07 14:57 
QuestionWhat happens after OnInitDialog()? Pin
rp_suman17-Jul-07 22:41
rp_suman17-Jul-07 22:41 
AnswerRe: What happens after OnInitDialog()? Pin
Cedric Moonen17-Jul-07 22:49
Cedric Moonen17-Jul-07 22:49 
AnswerRe: What happens after OnInitDialog()? Pin
Nibu babu thomas18-Jul-07 1:15
Nibu babu thomas18-Jul-07 1:15 
QuestionModifying the Windows Explorer Statusbar Pin
Toadflakz_UK17-Jul-07 22:14
Toadflakz_UK17-Jul-07 22:14 
QuestionHow to make first dialog iitself in a dialog based application Modeless Pin
robshere17-Jul-07 20:52
robshere17-Jul-07 20:52 
AnswerRe: How to make first dialog iitself in a dialog based application Modeless Pin
Arman S.17-Jul-07 21:35
Arman S.17-Jul-07 21:35 
GeneralRe: How to make first dialog iitself in a dialog based application Modeless Pin
Adrian Brutus17-Jul-07 22:08
Adrian Brutus17-Jul-07 22:08 
GeneralRe: How to make first dialog iitself in a dialog based application Modeless Pin
Arman S.17-Jul-07 22:33
Arman S.17-Jul-07 22:33 
QuestionRe: How to make first dialog iitself in a dialog based application Modeless Pin
robshere17-Jul-07 22:15
robshere17-Jul-07 22:15 
AnswerRe: How to make first dialog iitself in a dialog based application Modeless Pin
Arman S.17-Jul-07 22:43
Arman S.17-Jul-07 22:43 
GeneralRe: How to make first dialog iitself in a dialog based application Modeless Pin
robshere17-Jul-07 22:59
robshere17-Jul-07 22:59 
GeneralRe: How to make first dialog iitself in a dialog based application Modeless Pin
David Crow18-Jul-07 2:49
David Crow18-Jul-07 2:49 
GeneralRe: How to make first dialog iitself in a dialog based application Modeless Pin
robshere18-Jul-07 3:17
robshere18-Jul-07 3:17 
GeneralRe: How to make first dialog iitself in a dialog based application Modeless Pin
David Crow18-Jul-07 3:39
David Crow18-Jul-07 3:39 
QuestionConvert ActiveX DLL to ActiveX EXE. Pin
Anand Todkar17-Jul-07 20:30
Anand Todkar17-Jul-07 20:30 

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.