Transfer data between MFC Application and c# application using NamedPipe
-
I have to transfer data from MFC Application to C# application every 1 second Please find below the MFC Application (Server) and c# application (Client). I have a issue. Both the application's memory keeps increasing in task manager every 1 or 2 seconds. Please help me to figure out the issue in my code. Server Code in MFC applicationvoid SendDataToNamedPipe(CString sData) { char czVal[PIPEMAXSIZE]; //PIPEMAXSIZE = 10000 memset(czVal,0,PIPEMAXSIZE); strcpy(czVal, sData /*buffer*/); CString sPipeName = "\\\\.\\pipe\\" + "Pipe"; LPCTSTR lpszPipename = (const char*) sPipeName; hNamedPipe = CreateNamedPipe( lpszPipename, // Name of the pipe PIPE_ACCESS_OUTBOUND, // Pipe access type PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT, 1, // Maximum number of instances 0, // Out buffer size 0, // In buffer size 0, // Timeout NULL ); if (hNamedPipe != INVALID_HANDLE_VALUE) { BOOL fConnected,fSuccess; fConnected = ConnectNamedPipe(hNamedPipe, NULL) ? TRUE : (GetLastError() == ERROR_PIPE_CONNECTED); //// Assuming 'dataToSend' contains the data to send WriteFile(hNamedPipe, czVal, PIPEMAXSIZE /*(DWORD)sizeof(czVal)*/, NULL, NULL); DisconnectNamedPipe(hNamedPipe); CloseHandle(hNamedPipe); } } Client Code C# Applicaition: /////////////////// public void RecivePipeClient() { NamedPipeClientStream pipeClient = new NamedPipeClientStream(".", "Pipe", PipeDirection.In, PipeOptions.None); pipeClient.Connect(); StreamReader reader = new StreamReader(pipeClient); string dataValue = reader.ReadLine(); } //Calling Thread function in MFC Application (Server) void ThreadNamedPipeTagLiveValue(LPVOID) { CString strValue= ""; while( 1 ) { POSITION pos1 = OLivePointList.GetHeadPosition(); while(pos1 != NULL) { CTagBase* OTemp = (CTagBase*) OLivePointList.GetAt(pos1); CTime TNow; TNow = CTime::GetCurrentTime(); strTime=TNow.Format("%m/%d/%Y %H:%M:%S"); CString strTemp; strTag = OTemp->GetTagName(); if((OTemp->GetTagType() == CONTROLLER)) { OTemp->GetTagValues(OLivePoints); sTempValue.Format("%5.2f", OLivePoints.PV); strValue = strValue + sTempValue + ","; } } SendDataToNamedPipe(strValue); Sleep(1000); } }
-
I have to transfer data from MFC Application to C# application every 1 second Please find below the MFC Application (Server) and c# application (Client). I have a issue. Both the application's memory keeps increasing in task manager every 1 or 2 seconds. Please help me to figure out the issue in my code. Server Code in MFC applicationvoid SendDataToNamedPipe(CString sData) { char czVal[PIPEMAXSIZE]; //PIPEMAXSIZE = 10000 memset(czVal,0,PIPEMAXSIZE); strcpy(czVal, sData /*buffer*/); CString sPipeName = "\\\\.\\pipe\\" + "Pipe"; LPCTSTR lpszPipename = (const char*) sPipeName; hNamedPipe = CreateNamedPipe( lpszPipename, // Name of the pipe PIPE_ACCESS_OUTBOUND, // Pipe access type PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT, 1, // Maximum number of instances 0, // Out buffer size 0, // In buffer size 0, // Timeout NULL ); if (hNamedPipe != INVALID_HANDLE_VALUE) { BOOL fConnected,fSuccess; fConnected = ConnectNamedPipe(hNamedPipe, NULL) ? TRUE : (GetLastError() == ERROR_PIPE_CONNECTED); //// Assuming 'dataToSend' contains the data to send WriteFile(hNamedPipe, czVal, PIPEMAXSIZE /*(DWORD)sizeof(czVal)*/, NULL, NULL); DisconnectNamedPipe(hNamedPipe); CloseHandle(hNamedPipe); } } Client Code C# Applicaition: /////////////////// public void RecivePipeClient() { NamedPipeClientStream pipeClient = new NamedPipeClientStream(".", "Pipe", PipeDirection.In, PipeOptions.None); pipeClient.Connect(); StreamReader reader = new StreamReader(pipeClient); string dataValue = reader.ReadLine(); } //Calling Thread function in MFC Application (Server) void ThreadNamedPipeTagLiveValue(LPVOID) { CString strValue= ""; while( 1 ) { POSITION pos1 = OLivePointList.GetHeadPosition(); while(pos1 != NULL) { CTagBase* OTemp = (CTagBase*) OLivePointList.GetAt(pos1); CTime TNow; TNow = CTime::GetCurrentTime(); strTime=TNow.Format("%m/%d/%Y %H:%M:%S"); CString strTemp; strTag = OTemp->GetTagName(); if((OTemp->GetTagType() == CONTROLLER)) { OTemp->GetTagValues(OLivePoints); sTempValue.Format("%5.2f", OLivePoints.PV); strValue = strValue + sTempValue + ","; } } SendDataToNamedPipe(strValue); Sleep(1000); } }
Is that the only thing that's wrong with it, the memory keeps increasing? Does it work otherwise?
The difficult we do right away... ...the impossible takes slightly longer.
-
I have to transfer data from MFC Application to C# application every 1 second Please find below the MFC Application (Server) and c# application (Client). I have a issue. Both the application's memory keeps increasing in task manager every 1 or 2 seconds. Please help me to figure out the issue in my code. Server Code in MFC applicationvoid SendDataToNamedPipe(CString sData) { char czVal[PIPEMAXSIZE]; //PIPEMAXSIZE = 10000 memset(czVal,0,PIPEMAXSIZE); strcpy(czVal, sData /*buffer*/); CString sPipeName = "\\\\.\\pipe\\" + "Pipe"; LPCTSTR lpszPipename = (const char*) sPipeName; hNamedPipe = CreateNamedPipe( lpszPipename, // Name of the pipe PIPE_ACCESS_OUTBOUND, // Pipe access type PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT, 1, // Maximum number of instances 0, // Out buffer size 0, // In buffer size 0, // Timeout NULL ); if (hNamedPipe != INVALID_HANDLE_VALUE) { BOOL fConnected,fSuccess; fConnected = ConnectNamedPipe(hNamedPipe, NULL) ? TRUE : (GetLastError() == ERROR_PIPE_CONNECTED); //// Assuming 'dataToSend' contains the data to send WriteFile(hNamedPipe, czVal, PIPEMAXSIZE /*(DWORD)sizeof(czVal)*/, NULL, NULL); DisconnectNamedPipe(hNamedPipe); CloseHandle(hNamedPipe); } } Client Code C# Applicaition: /////////////////// public void RecivePipeClient() { NamedPipeClientStream pipeClient = new NamedPipeClientStream(".", "Pipe", PipeDirection.In, PipeOptions.None); pipeClient.Connect(); StreamReader reader = new StreamReader(pipeClient); string dataValue = reader.ReadLine(); } //Calling Thread function in MFC Application (Server) void ThreadNamedPipeTagLiveValue(LPVOID) { CString strValue= ""; while( 1 ) { POSITION pos1 = OLivePointList.GetHeadPosition(); while(pos1 != NULL) { CTagBase* OTemp = (CTagBase*) OLivePointList.GetAt(pos1); CTime TNow; TNow = CTime::GetCurrentTime(); strTime=TNow.Format("%m/%d/%Y %H:%M:%S"); CString strTemp; strTag = OTemp->GetTagName(); if((OTemp->GetTagType() == CONTROLLER)) { OTemp->GetTagValues(OLivePoints); sTempValue.Format("%5.2f", OLivePoints.PV); strValue = strValue + sTempValue + ","; } } SendDataToNamedPipe(strValue); Sleep(1000); } }
Just from a design point of view, are you sure you want to be creating and closing a new pipe every second? If you're going to be up and running for a bit, why deal with the overhead of constantly creating and destroying a pipe? Also, in your C# code, you should use a
using
statement on yourpipeClient
variable. Its lack may be why you seem to be leaking memory due to incomplete cleanup of your pipe.Be wary of strong drink. It can make you shoot at tax collectors - and miss. Lazarus Long, "Time Enough For Love" by Robert A. Heinlein