program without window or console

I want to make a program without a window or a console, but still be able to receive events. How is this done?
Create a hidden window. A console application does not have a message loop.
How do I create a hidden window? plz give an example. EDIT: Do i create a window but never use CreateWindowEX()?
Last edited on
closed account (3pj6b7Xj)
don't need to create a hidden window.....just call the winapi function and start a normal message loop...leave the handle part blank......just to WARN you.....add the ability to quit the application in the procedure, otherwise, you'll have to close it manually via the running tasks.

try this code at your own risk!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <windows.h>

/* declare window procedure for this application. */
LRESULT CALLBACK WinProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE,LPSTR szArgs,int nCmdShow)
{
    MSG Msg; // save window messages here.

    /* Run the message pump. It will run until GetMessage() returns 0 */
    while (GetMessage (&Msg, NULL, 0, 0))
    {
        TranslateMessage(&Msg); // Translate virtual-key messages to character messages
        DispatchMessage(&Msg); // Send message to WindowProcedure
    }

    return Msg.wParam;
}


/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WinProc(HWND hWnd,UINT Message,WPARAM wParam,LPARAM lParam)
{
    switch (Message)
    {
        case VK_ESCAPE:
        {
            MessageBox(NULL,"You hit escape, quitting now.","Message",0);
            PostQuitMessage(0);
            break;
        }

        // handle non-trapped messages.
        default:return DefWindowProc(hWnd,Message,wParam,lParam);
    }

    return 0; // this indicates a message was trapped.
}


Some things you should understand, since there is no window in focus, this application will not really respond to the escape key I set it to trap in the procedure. You must trap any events SYSTEM WIDE, good luck with that. :)
If you don't create a hidden window you will not have a message pump at all, windows operating system works that way.
closed account (3pj6b7Xj)
YOu are correct, otherwise, where would the messages go anyway, the hWnd doesn't even exist to begin with in my example.
The message queue is created when an API that requires it somehow is called. I believe the queue will exist once GetMessage() is called, but the OS probably won't send any messages there because there are no windows, so one top-level window is needed, I would think.
Why complicate things guys? http://msdn.microsoft.com/en-us/library/ms633548(VS.85).aspx

Also, because in my experiance most processes that should be run without a visible window should also be a service take a look here: http://msdn.microsoft.com/en-us/library/ms682450(VS.85).aspx
Running as a service will give you easy access to the System wide messages as well.
Meh, what the hell. I'll just leave this here: http://msdn.microsoft.com/en-us/library/ms632599(VS.85).aspx#message_only
Topic archived. No new replies allowed.