console app which handle windows events
-
Hello, Can you guide me though simple steps to create a console application which can also handle windows events?
-
My mean to say... Can I create windows console based app which can receive events? I think in Windows service we can receive events, same way I want. Is that possible?
-
Hello, Can you guide me though simple steps to create a console application which can also handle windows events?
If you mean by events Windows messages, you just have to implement a message loop. But a console application does not have a window that is required as message recipient. So you can only receive messages posted to threads. See How to post a thread message to a console application[^] for an example. Alternatively you can create a (optionally hidden) window that receives the messages. Or create a GUI application (usually dialog based) and hide the window.
-
My mean to say... Can I create windows console based app which can receive events? I think in Windows service we can receive events, same way I want. Is that possible?
Fedrer wrote:
Can I create windows console based app which can receive events? I think in Windows service we can receive events, same way I want. Is that possible?
What do you mean by "events"? Is it what we see in the Windows Event Viewer? Or you meant the Windows Messages? If you mean a kind a "messaging" for services then you should choose some of the IPC mechanism such as Named Pipes. See also [winapi - Receive Windows Messages in a Service - Stack Overflow](https://stackoverflow.com/questions/15141529/receive-windows-messages-in-a-service)
-
Fedrer wrote:
windows console based app
What does that mean? An app is either Windows based or Console based, it cannot be both.
Not true for a long time .. there is no such thing as a "real console" anymore the whole DOS kick has been dead since Windows 7. The console is entirely emulated now on windows, they just use a different message handler by default.
In vino veritas
-
Hello, Can you guide me though simple steps to create a console application which can also handle windows events?
Ok so the whole console now is full emulated since Windows 7 it is just a normal window. However it has a special message handler which filters out the normal windows messages and it doesn't have a proper message queue setup. It's rather technical to go thru and add the steps to convert it when there is a much simpler way. For the record WM_TIMERTICK and a few other windows message do get passed thru (you can use timers now on a console app) .. This will work as illustration there is a normal windows queue present on a console window however expanding the queue and removing the filter is far more problematic
int main()
{
SetTimer(0, 1, 1000, 0);
SetTimer(0, 2, 750, 0);
MSG Message;
while(GetMessage(&Message, 0, 0, 0))
{
if(Message.message == WM_TIMER)
{
std::cout << "Timer" << std::endl;
}
}
}By far the easiest way to do what you want is actually setup for a proper windows app but then create and attach a console to it which is the new fancy feature. AllocConsole function - Windows Console | Microsoft Docs[^] AttachConsole function - Windows Console | Microsoft Docs[^] So it basically does exactly what you are after you have a full windows message queue underneath and you can pump whatever message you want to the console. The actual GUI window can remain hidden by simply never setting the WS_VISIBLE flag or attaching it to the taskbar in minimized form. Your whole app is basically a minimal hidden standard window throwing messages up to a console which is I believe what you are after.
In vino veritas
-
Not true for a long time .. there is no such thing as a "real console" anymore the whole DOS kick has been dead since Windows 7. The console is entirely emulated now on windows, they just use a different message handler by default.
In vino veritas