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 / C++ / MFC
  4. Choosing an IPC mechanism

Choosing an IPC mechanism

Scheduled Pinned Locked Moved C / C++ / MFC
c++comdata-structurestoolstutorial
7 Posts 4 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.
  • R Offline
    R Offline
    Rajesh R Subramanian
    wrote on last edited by
    #1

    I have a console application with no MFC support (I cannot add MFC support due to several reasons). This console executes another process and the new process does a very lengthy operation (I cannot use a thread within the console to do the lengthy operation, as is performed by a third party library that the app links to, which cannot work in console mode). The console app must be notified of the progress of this lengthy operation periodically. Now, when I thought of how to notify the console, I cannot do a PostMessage (there's no window handle), I cannot do a PostThreadMessage (the primary thread does not have a message queue). Therefore, I am considering the following options: 1. Use a named pipe. The console creates the pipe and uses CreateProcess to spawn the other executable, passing the pipe handle while spawning it. The executable write into the pipe and the console reads. 2. Use Overlapped IO 3. Any other suggestions (No, code injection or CreateRemoteThread is not an option that I will consider). I have the source code of both the applications, but I don't want to do a lot of changes in the console app (thousands of lines, OLD stuff, no docs). However, I'm open to be convinced to do that as well. Thank you!

    It is a crappy thing, but it's life -^ Carlo Pallini

    P S L 3 Replies Last reply
    0
    • R Rajesh R Subramanian

      I have a console application with no MFC support (I cannot add MFC support due to several reasons). This console executes another process and the new process does a very lengthy operation (I cannot use a thread within the console to do the lengthy operation, as is performed by a third party library that the app links to, which cannot work in console mode). The console app must be notified of the progress of this lengthy operation periodically. Now, when I thought of how to notify the console, I cannot do a PostMessage (there's no window handle), I cannot do a PostThreadMessage (the primary thread does not have a message queue). Therefore, I am considering the following options: 1. Use a named pipe. The console creates the pipe and uses CreateProcess to spawn the other executable, passing the pipe handle while spawning it. The executable write into the pipe and the console reads. 2. Use Overlapped IO 3. Any other suggestions (No, code injection or CreateRemoteThread is not an option that I will consider). I have the source code of both the applications, but I don't want to do a lot of changes in the console app (thousands of lines, OLD stuff, no docs). However, I'm open to be convinced to do that as well. Thank you!

      It is a crappy thing, but it's life -^ Carlo Pallini

      P Offline
      P Offline
      ParagPatel
      wrote on last edited by
      #2

      Socket communication may become good option for you

      Parag Patel Sr. Software Eng, Varaha Systems

      R 1 Reply Last reply
      0
      • P ParagPatel

        Socket communication may become good option for you

        Parag Patel Sr. Software Eng, Varaha Systems

        R Offline
        R Offline
        Rajesh R Subramanian
        wrote on last edited by
        #3

        It looked like a bad option for me, because I will then need to link to the winsock libraries, will have the overhead of sockets, and will need to ensure that the machine will have a loopback adapter. On the contrary, a locally named pipe will execute in kernel mode, the OS has built-in support for it (no additional libraries). So, I had excluded sockets. :) Thank you!

        It is a crappy thing, but it's life -^ Carlo Pallini

        1 Reply Last reply
        0
        • R Rajesh R Subramanian

          I have a console application with no MFC support (I cannot add MFC support due to several reasons). This console executes another process and the new process does a very lengthy operation (I cannot use a thread within the console to do the lengthy operation, as is performed by a third party library that the app links to, which cannot work in console mode). The console app must be notified of the progress of this lengthy operation periodically. Now, when I thought of how to notify the console, I cannot do a PostMessage (there's no window handle), I cannot do a PostThreadMessage (the primary thread does not have a message queue). Therefore, I am considering the following options: 1. Use a named pipe. The console creates the pipe and uses CreateProcess to spawn the other executable, passing the pipe handle while spawning it. The executable write into the pipe and the console reads. 2. Use Overlapped IO 3. Any other suggestions (No, code injection or CreateRemoteThread is not an option that I will consider). I have the source code of both the applications, but I don't want to do a lot of changes in the console app (thousands of lines, OLD stuff, no docs). However, I'm open to be convinced to do that as well. Thank you!

          It is a crappy thing, but it's life -^ Carlo Pallini

          S Offline
          S Offline
          Stuart Dootson
          wrote on last edited by
          #4

          A mailslot[^]? That way, the mailslot name is the only hard-coded link between the app calling the third party library and the console app that monitors them - so, it's a highly decoupled design, which is preferable.

          Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

          R 1 Reply Last reply
          0
          • S Stuart Dootson

            A mailslot[^]? That way, the mailslot name is the only hard-coded link between the app calling the third party library and the console app that monitors them - so, it's a highly decoupled design, which is preferable.

            Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

            R Offline
            R Offline
            Rajesh R Subramanian
            wrote on last edited by
            #5

            Thanks Stuart, that looks interesting. Now, I'll choose one between Named Pipes and mailslots. :)

            It is a crappy thing, but it's life -^ Carlo Pallini

            1 Reply Last reply
            0
            • R Rajesh R Subramanian

              I have a console application with no MFC support (I cannot add MFC support due to several reasons). This console executes another process and the new process does a very lengthy operation (I cannot use a thread within the console to do the lengthy operation, as is performed by a third party library that the app links to, which cannot work in console mode). The console app must be notified of the progress of this lengthy operation periodically. Now, when I thought of how to notify the console, I cannot do a PostMessage (there's no window handle), I cannot do a PostThreadMessage (the primary thread does not have a message queue). Therefore, I am considering the following options: 1. Use a named pipe. The console creates the pipe and uses CreateProcess to spawn the other executable, passing the pipe handle while spawning it. The executable write into the pipe and the console reads. 2. Use Overlapped IO 3. Any other suggestions (No, code injection or CreateRemoteThread is not an option that I will consider). I have the source code of both the applications, but I don't want to do a lot of changes in the console app (thousands of lines, OLD stuff, no docs). However, I'm open to be convinced to do that as well. Thank you!

              It is a crappy thing, but it's life -^ Carlo Pallini

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Rajesh R Subramanian wrote:

              The console app must be notified of the progress of this lengthy operation periodically.

              Mailslots, sockets and named pipes.... all for a simple integer percentage value? SendMessage returns a value in its LRESULT which can be used in the console application to retrieve small values such as percentage complete. Something like this:#include "stdafx.h" #include <stdio.h> #include <string.h> #include <conio.h> #include <windows.h> #include <Psapi.h> #pragma comment(lib, "psapi") #define WM_PERCENTAGE WM_USER + 1033 BOOL CALLBACK GetPercentageProc(HWND hWnd, LPARAM lParam) { int nLen = GetWindowTextLength(hWnd); TCHAR szTitle[MAX_PATH]; GetWindowText(hWnd, szTitle, MAX_PATH); if(_tcsstr(szTitle,_T("YourApplicationTitle"))) { DWORD dwPID; TCHAR szModule[MAX_PATH]; HMODULE hModule; DWORD dwNeeded; GetWindowThreadProcessId(hWnd,&dwPID); HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,FALSE,dwPID); if(EnumProcessModules(hProcess,&hModule,sizeof(hModule),&dwNeeded)) { GetModuleFileNameEx(hProcess,hModule,szModule,sizeof(szModule)); } else { GetProcessImageFileName(hProcess,szModule,sizeof(szModule)); } TCHAR * p = _tcsrchr(szModule,_T('\\')); if(0 == _tcscmp(++p,_T("YourExecutable.exe"))) { LRESULT dwPercent = SendMessage(hWnd,WM_PERCENTAGE,0,0); DWORD dwP = dwPercent; //Do whatever with your percentage value here. } } return TRUE; } int main() { EnumWindows(GetPercentageProc, NULL); _getch(); return 0; }
              Note that its recommended to use RegisterWindowMessage [^] for IPC although I did not use it in my example. Best Wishes, -David Delaune

              R 1 Reply Last reply
              0
              • L Lost User

                Rajesh R Subramanian wrote:

                The console app must be notified of the progress of this lengthy operation periodically.

                Mailslots, sockets and named pipes.... all for a simple integer percentage value? SendMessage returns a value in its LRESULT which can be used in the console application to retrieve small values such as percentage complete. Something like this:#include "stdafx.h" #include <stdio.h> #include <string.h> #include <conio.h> #include <windows.h> #include <Psapi.h> #pragma comment(lib, "psapi") #define WM_PERCENTAGE WM_USER + 1033 BOOL CALLBACK GetPercentageProc(HWND hWnd, LPARAM lParam) { int nLen = GetWindowTextLength(hWnd); TCHAR szTitle[MAX_PATH]; GetWindowText(hWnd, szTitle, MAX_PATH); if(_tcsstr(szTitle,_T("YourApplicationTitle"))) { DWORD dwPID; TCHAR szModule[MAX_PATH]; HMODULE hModule; DWORD dwNeeded; GetWindowThreadProcessId(hWnd,&dwPID); HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,FALSE,dwPID); if(EnumProcessModules(hProcess,&hModule,sizeof(hModule),&dwNeeded)) { GetModuleFileNameEx(hProcess,hModule,szModule,sizeof(szModule)); } else { GetProcessImageFileName(hProcess,szModule,sizeof(szModule)); } TCHAR * p = _tcsrchr(szModule,_T('\\')); if(0 == _tcscmp(++p,_T("YourExecutable.exe"))) { LRESULT dwPercent = SendMessage(hWnd,WM_PERCENTAGE,0,0); DWORD dwP = dwPercent; //Do whatever with your percentage value here. } } return TRUE; } int main() { EnumWindows(GetPercentageProc, NULL); _getch(); return 0; }
                Note that its recommended to use RegisterWindowMessage [^] for IPC although I did not use it in my example. Best Wishes, -David Delaune

                R Offline
                R Offline
                Rajesh R Subramanian
                wrote on last edited by
                #7

                Hi David, Isn't SendMessage going to be blocking? I was looking for something that would be asynchronous. (OK, I can use a thread, but I am just asking). I'll try this as well. Thank you for your suggestions.

                It is a crappy thing, but it's life -^ Carlo Pallini

                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