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. to send message to console exe

to send message to console exe

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
17 Posts 9 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.
  • Z Offline
    Z Offline
    zon_cpp
    wrote on last edited by
    #1

    Hi, i have a console project. then an console exe. i want in another project(dlg project), to execute this console exe. i execute it with calling ShellExecute() function. now, i want to send some messages to this exe.(to send message from dlg to console) how do i do it? i don't have any hWnd of console exe in dlg project! please help me.

    Zo.Naderi-Iran

    L R CPalliniC C S 8 Replies Last reply
    0
    • Z zon_cpp

      Hi, i have a console project. then an console exe. i want in another project(dlg project), to execute this console exe. i execute it with calling ShellExecute() function. now, i want to send some messages to this exe.(to send message from dlg to console) how do i do it? i don't have any hWnd of console exe in dlg project! please help me.

      Zo.Naderi-Iran

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

      Console projects do not have the capability to interact in this way, as they do not have message queues.

      Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

      1 Reply Last reply
      0
      • Z zon_cpp

        Hi, i have a console project. then an console exe. i want in another project(dlg project), to execute this console exe. i execute it with calling ShellExecute() function. now, i want to send some messages to this exe.(to send message from dlg to console) how do i do it? i don't have any hWnd of console exe in dlg project! please help me.

        Zo.Naderi-Iran

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

        You are trying to do the impossible here, as the other poster stated. But you can pass data between proceses outside of the windows messaging system. Check out interproces communication in the MSDN. Windows hooks are one method I have used a lot.

        ============================== Nothing to say.

        1 Reply Last reply
        0
        • Z zon_cpp

          Hi, i have a console project. then an console exe. i want in another project(dlg project), to execute this console exe. i execute it with calling ShellExecute() function. now, i want to send some messages to this exe.(to send message from dlg to console) how do i do it? i don't have any hWnd of console exe in dlg project! please help me.

          Zo.Naderi-Iran

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

          A console application does not have a message pump and so cannot accept a windows message. However, you can still communicate with a console application with the help of several IPC mechanisms available (for example - sockets[^], pipes[^], etc.,)

          "Real men drive manual transmission" - Rajesh.

          1 Reply Last reply
          0
          • Z zon_cpp

            Hi, i have a console project. then an console exe. i want in another project(dlg project), to execute this console exe. i execute it with calling ShellExecute() function. now, i want to send some messages to this exe.(to send message from dlg to console) how do i do it? i don't have any hWnd of console exe in dlg project! please help me.

            Zo.Naderi-Iran

            CPalliniC Online
            CPalliniC Online
            CPallini
            wrote on last edited by
            #5

            As already (correctly) stated you can't use Windows messages to communicate with the console child process. Have a look at an alternative (to the proposed ones) way: "How to spawn console processes with redirected standard handles"[^].

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            In testa che avete, signor di Ceprano?

            1 Reply Last reply
            0
            • Z zon_cpp

              Hi, i have a console project. then an console exe. i want in another project(dlg project), to execute this console exe. i execute it with calling ShellExecute() function. now, i want to send some messages to this exe.(to send message from dlg to console) how do i do it? i don't have any hWnd of console exe in dlg project! please help me.

              Zo.Naderi-Iran

              C Offline
              C Offline
              Chuck OToole
              wrote on last edited by
              #6

              Windows offers a number of ways for two processes to communicate. See Here[^] Once you decide on a method, you can search for a reference implementation of that method on Code Project to on the net.

              1 Reply Last reply
              0
              • Z zon_cpp

                Hi, i have a console project. then an console exe. i want in another project(dlg project), to execute this console exe. i execute it with calling ShellExecute() function. now, i want to send some messages to this exe.(to send message from dlg to console) how do i do it? i don't have any hWnd of console exe in dlg project! please help me.

                Zo.Naderi-Iran

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

                Hi, For some reason I have been seeing this question alot... and the adequate responses. Although it is correct that console applications initially do not have a message queue... the first time a win32k.sys/GDI syscall greater or equal to 0x1000 is invoked... the windows kernel increases the thread stack-size and invokes KiConvertToGuiThread which will convert the console thread into a GUI thread with a message queue. How do you 'invoke a system call above 0x1000' ? WIN32K.SYS System Call Table[^] As you can see in that table... the first GDI system call NtGdiAbortDoc is at index 0x1000. Because we know that the GetMessage function[^] results in a call to NtUserGetMessage and the DispatchMessage function[^] results in a call to NtUserDispatchMessage. We can infer that by simply creating a message loop... we will have in-fact promoted the thread into a GUI thread. In other words... by simply attempting to pump the message queue... the thread is given one.

                MSG msg;
                while(GetMessage(&msg, NULL, 0, 0))
                {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
                }

                So what do you have now? Now you have a console process with a thread containing a message queue that has no window HANDLE so cannot recieve window messages. Is this the end of the road? No, window messages are not the only type of message. You can still use the PostThreadMessage function[^] to post thread messages to the console. Step 1: Create two console applications... Sender and Reciever. Step 2: In the reciever begin an infinite message pump (console thread will be promoted to gui thread by the subsystem) Step 3: In the sender call CreateProcess with the CREATE_NEW_CONSOLE flag that creates the reciever pr

                CPalliniC L Richard Andrew x64R A 4 Replies Last reply
                0
                • L Lost User

                  Hi, For some reason I have been seeing this question alot... and the adequate responses. Although it is correct that console applications initially do not have a message queue... the first time a win32k.sys/GDI syscall greater or equal to 0x1000 is invoked... the windows kernel increases the thread stack-size and invokes KiConvertToGuiThread which will convert the console thread into a GUI thread with a message queue. How do you 'invoke a system call above 0x1000' ? WIN32K.SYS System Call Table[^] As you can see in that table... the first GDI system call NtGdiAbortDoc is at index 0x1000. Because we know that the GetMessage function[^] results in a call to NtUserGetMessage and the DispatchMessage function[^] results in a call to NtUserDispatchMessage. We can infer that by simply creating a message loop... we will have in-fact promoted the thread into a GUI thread. In other words... by simply attempting to pump the message queue... the thread is given one.

                  MSG msg;
                  while(GetMessage(&msg, NULL, 0, 0))
                  {
                  TranslateMessage(&msg);
                  DispatchMessage(&msg);
                  }

                  So what do you have now? Now you have a console process with a thread containing a message queue that has no window HANDLE so cannot recieve window messages. Is this the end of the road? No, window messages are not the only type of message. You can still use the PostThreadMessage function[^] to post thread messages to the console. Step 1: Create two console applications... Sender and Reciever. Step 2: In the reciever begin an infinite message pump (console thread will be promoted to gui thread by the subsystem) Step 3: In the sender call CreateProcess with the CREATE_NEW_CONSOLE flag that creates the reciever pr

                  CPalliniC Online
                  CPalliniC Online
                  CPallini
                  wrote on last edited by
                  #8

                  Nice.

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  In testa che avete, signor di Ceprano?

                  1 Reply Last reply
                  0
                  • L Lost User

                    Hi, For some reason I have been seeing this question alot... and the adequate responses. Although it is correct that console applications initially do not have a message queue... the first time a win32k.sys/GDI syscall greater or equal to 0x1000 is invoked... the windows kernel increases the thread stack-size and invokes KiConvertToGuiThread which will convert the console thread into a GUI thread with a message queue. How do you 'invoke a system call above 0x1000' ? WIN32K.SYS System Call Table[^] As you can see in that table... the first GDI system call NtGdiAbortDoc is at index 0x1000. Because we know that the GetMessage function[^] results in a call to NtUserGetMessage and the DispatchMessage function[^] results in a call to NtUserDispatchMessage. We can infer that by simply creating a message loop... we will have in-fact promoted the thread into a GUI thread. In other words... by simply attempting to pump the message queue... the thread is given one.

                    MSG msg;
                    while(GetMessage(&msg, NULL, 0, 0))
                    {
                    TranslateMessage(&msg);
                    DispatchMessage(&msg);
                    }

                    So what do you have now? Now you have a console process with a thread containing a message queue that has no window HANDLE so cannot recieve window messages. Is this the end of the road? No, window messages are not the only type of message. You can still use the PostThreadMessage function[^] to post thread messages to the console. Step 1: Create two console applications... Sender and Reciever. Step 2: In the reciever begin an infinite message pump (console thread will be promoted to gui thread by the subsystem) Step 3: In the sender call CreateProcess with the CREATE_NEW_CONSOLE flag that creates the reciever pr

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

                    How do you create/register the message handler func?

                    ============================== Nothing to say.

                    L S 2 Replies Last reply
                    0
                    • Z zon_cpp

                      Hi, i have a console project. then an console exe. i want in another project(dlg project), to execute this console exe. i execute it with calling ShellExecute() function. now, i want to send some messages to this exe.(to send message from dlg to console) how do i do it? i don't have any hWnd of console exe in dlg project! please help me.

                      Zo.Naderi-Iran

                      S Offline
                      S Offline
                      Software_Developer
                      wrote on last edited by
                      #10

                      It is not possible but you can cheat. Reverse engineer it. Create a Windows app (win32 or MFC) and redirect messages to a console window. But once you add a window, your app will no longer be a console app. So it is still not possible. See [this]. More reversed console apps [here] and [here].

                      1 Reply Last reply
                      0
                      • L Lost User

                        How do you create/register the message handler func?

                        ============================== Nothing to say.

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

                        Hi, What message handler function are you referring to? The GetMessage function[^] is exported from the user32 library. Best Wishes, -David Delaune

                        1 Reply Last reply
                        0
                        • L Lost User

                          How do you create/register the message handler func?

                          ============================== Nothing to say.

                          S Offline
                          S Offline
                          Software_Developer
                          wrote on last edited by
                          #12

                          #include #include #include using namespace std;
                          #pragma comment(lib, "user32")

                          int main(int argc, char *argv[], char *envp[])
                          {
                          DWORD idThread;
                          MSG Msg;
                          if (argc == 2)
                          {
                          idThread = atoi(argv[1]);
                          if (!PostThreadMessage(idThread, WM_COMMAND, (WPARAM)0, (LPARAM)0))
                          cout << GetLastError() << " PostThreadMessage error\n";
                          return 0;
                          }
                          cout << GetCurrentThreadId() << " thread id\n";

                          while (GetMessage(&Msg, NULL, 0, WM\_USER)) 
                          {
                          	if (Msg.message == WM\_COMMAND)
                          		cout << "WM\_COMMAND\\n";
                          	else
                          		cout << "Message: " << Msg.message << endl;
                          }
                          

                          return 0;
                          }

                          L 1 Reply Last reply
                          0
                          • S Software_Developer

                            #include #include #include using namespace std;
                            #pragma comment(lib, "user32")

                            int main(int argc, char *argv[], char *envp[])
                            {
                            DWORD idThread;
                            MSG Msg;
                            if (argc == 2)
                            {
                            idThread = atoi(argv[1]);
                            if (!PostThreadMessage(idThread, WM_COMMAND, (WPARAM)0, (LPARAM)0))
                            cout << GetLastError() << " PostThreadMessage error\n";
                            return 0;
                            }
                            cout << GetCurrentThreadId() << " thread id\n";

                            while (GetMessage(&Msg, NULL, 0, WM\_USER)) 
                            {
                            	if (Msg.message == WM\_COMMAND)
                            		cout << "WM\_COMMAND\\n";
                            	else
                            		cout << "Message: " << Msg.message << endl;
                            }
                            

                            return 0;
                            }

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

                            Hi, TopCoder23, Yep, it is perfectly valid to perform a test within a single console application. If you would like to see a working IPC sample, I have converted my forum posting into a Tip/Trick: How to post a thread message to a console application[^] (Pending state at the moment) If you are looking for an IPC sample you can download it here: Sample: Posting thread messages between console applications[^] Best Wishes, (Too much free-time on my hands, it is the Thanksgiving weekend!) -David Delaune

                            1 Reply Last reply
                            0
                            • L Lost User

                              Hi, For some reason I have been seeing this question alot... and the adequate responses. Although it is correct that console applications initially do not have a message queue... the first time a win32k.sys/GDI syscall greater or equal to 0x1000 is invoked... the windows kernel increases the thread stack-size and invokes KiConvertToGuiThread which will convert the console thread into a GUI thread with a message queue. How do you 'invoke a system call above 0x1000' ? WIN32K.SYS System Call Table[^] As you can see in that table... the first GDI system call NtGdiAbortDoc is at index 0x1000. Because we know that the GetMessage function[^] results in a call to NtUserGetMessage and the DispatchMessage function[^] results in a call to NtUserDispatchMessage. We can infer that by simply creating a message loop... we will have in-fact promoted the thread into a GUI thread. In other words... by simply attempting to pump the message queue... the thread is given one.

                              MSG msg;
                              while(GetMessage(&msg, NULL, 0, 0))
                              {
                              TranslateMessage(&msg);
                              DispatchMessage(&msg);
                              }

                              So what do you have now? Now you have a console process with a thread containing a message queue that has no window HANDLE so cannot recieve window messages. Is this the end of the road? No, window messages are not the only type of message. You can still use the PostThreadMessage function[^] to post thread messages to the console. Step 1: Create two console applications... Sender and Reciever. Step 2: In the reciever begin an infinite message pump (console thread will be promoted to gui thread by the subsystem) Step 3: In the sender call CreateProcess with the CREATE_NEW_CONSOLE flag that creates the reciever pr

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

                              If you have access to the console app source code, and you go to the trouble of adding the message pump, you may as well add code to create a message-only window, no?

                              The difficult we do right away... ...the impossible takes slightly longer.

                              L 1 Reply Last reply
                              0
                              • Z zon_cpp

                                Hi, i have a console project. then an console exe. i want in another project(dlg project), to execute this console exe. i execute it with calling ShellExecute() function. now, i want to send some messages to this exe.(to send message from dlg to console) how do i do it? i don't have any hWnd of console exe in dlg project! please help me.

                                Zo.Naderi-Iran

                                D Offline
                                D Offline
                                David Crow
                                wrote on last edited by
                                #15

                                Here is another example, to go along with Carlos'.

                                "One man's wage rise is another man's price increase." - Harold Wilson

                                "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                                "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

                                1 Reply Last reply
                                0
                                • Richard Andrew x64R Richard Andrew x64

                                  If you have access to the console app source code, and you go to the trouble of adding the message pump, you may as well add code to create a message-only window, no?

                                  The difficult we do right away... ...the impossible takes slightly longer.

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

                                  Hi Richard,

                                  Richard Andrew x64 wrote:

                                  you may as well add code to create a message-only window, no?

                                  It doesn't really matter to me how you decide to implement IPC between console applications. As Rajesh suggested you could use sockets, pipes or as Carlo Pallini suggested you could use STDIN/STDOUT/STDERR or as you suggested a hidden window or the option I have suggested; PostThreadMessage. It is great to have so many options put on the table. :) Best Wishes, -David Delaune

                                  1 Reply Last reply
                                  0
                                  • L Lost User

                                    Hi, For some reason I have been seeing this question alot... and the adequate responses. Although it is correct that console applications initially do not have a message queue... the first time a win32k.sys/GDI syscall greater or equal to 0x1000 is invoked... the windows kernel increases the thread stack-size and invokes KiConvertToGuiThread which will convert the console thread into a GUI thread with a message queue. How do you 'invoke a system call above 0x1000' ? WIN32K.SYS System Call Table[^] As you can see in that table... the first GDI system call NtGdiAbortDoc is at index 0x1000. Because we know that the GetMessage function[^] results in a call to NtUserGetMessage and the DispatchMessage function[^] results in a call to NtUserDispatchMessage. We can infer that by simply creating a message loop... we will have in-fact promoted the thread into a GUI thread. In other words... by simply attempting to pump the message queue... the thread is given one.

                                    MSG msg;
                                    while(GetMessage(&msg, NULL, 0, 0))
                                    {
                                    TranslateMessage(&msg);
                                    DispatchMessage(&msg);
                                    }

                                    So what do you have now? Now you have a console process with a thread containing a message queue that has no window HANDLE so cannot recieve window messages. Is this the end of the road? No, window messages are not the only type of message. You can still use the PostThreadMessage function[^] to post thread messages to the console. Step 1: Create two console applications... Sender and Reciever. Step 2: In the reciever begin an infinite message pump (console thread will be promoted to gui thread by the subsystem) Step 3: In the sender call CreateProcess with the CREATE_NEW_CONSOLE flag that creates the reciever pr

                                    A Offline
                                    A Offline
                                    Albert Holguin
                                    wrote on last edited by
                                    #17

                                    Yep, definitely not impossible (people get too used to the framework doing magic)... I think it's just a bit of a hassle more than anything else. Good job pointing this out! :thumbsup:

                                    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