to send message to console exe
-
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
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] -
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
-
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
-
How do you create/register the message handler func?
============================== Nothing to say.
-
How do you create/register the message handler func?
============================== Nothing to say.
#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;
} -
#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;
}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
-
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
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.
-
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
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
-
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.
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
-
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
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: