Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Transfer data between MFC Application and c# application using NamedPipe

Transfer data between MFC Application and c# application using NamedPipe

Scheduled Pinned Locked Moved C#
helpcsharpc++sysadminperformance
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    manoharbalu
    wrote on last edited by
    #1

    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); } }

    Richard Andrew x64R J 2 Replies Last reply
    0
    • M manoharbalu

      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); } }

      Richard Andrew x64R Offline
      Richard Andrew x64R Offline
      Richard Andrew x64
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • M manoharbalu

        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); } }

        J Offline
        J Offline
        JudyL_MD
        wrote on last edited by
        #3

        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 your pipeClient 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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups