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. console app which handle windows events

console app which handle windows events

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
9 Posts 5 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.
  • F Offline
    F Offline
    Fedrer
    wrote on last edited by
    #1

    Hello, Can you guide me though simple steps to create a console application which can also handle windows events?

    L J L 3 Replies Last reply
    0
    • F Fedrer

      Hello, Can you guide me though simple steps to create a console application which can also handle windows events?

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

      A console app is not a Windows app, so it does not receive Windows events.

      F 1 Reply Last reply
      0
      • L Lost User

        A console app is not a Windows app, so it does not receive Windows events.

        F Offline
        F Offline
        Fedrer
        wrote on last edited by
        #3

        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?

        L V 2 Replies Last reply
        0
        • F Fedrer

          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?

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

          Fedrer wrote:

          windows console based app

          What does that mean? An app is either Windows based or Console based, it cannot be both.

          L 1 Reply Last reply
          0
          • F Fedrer

            Hello, Can you guide me though simple steps to create a console application which can also handle windows events?

            J Offline
            J Offline
            Jochen Arndt
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • F Fedrer

              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?

              V Offline
              V Offline
              Victor Nijegorodov
              wrote on last edited by
              #6

              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)

              1 Reply Last reply
              0
              • L Lost User

                Fedrer wrote:

                windows console based app

                What does that mean? An app is either Windows based or Console based, it cannot be both.

                L Offline
                L Offline
                leon de boer
                wrote on last edited by
                #7

                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

                L 1 Reply Last reply
                0
                • F Fedrer

                  Hello, Can you guide me though simple steps to create a console application which can also handle windows events?

                  L Offline
                  L Offline
                  leon de boer
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  • L leon de boer

                    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

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

                    leon de boer wrote:

                    there is no such thing as a "real console"

                    I never claimed that there was. I merely stated that the two application types are quite different.

                    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