tags:

views:

478

answers:

6

This may be the simplest win32 program ever ..

#include <windows.h>

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR cmdLine, int show)
{
    MessageBox(0, "Hello world..", "Salutations!", MB_OK);
    return 0;
}

.. which makes no calls whatsoever to the usual GetMessage() call. My question is this: if my program doesn't process any window messages, can the OS cope with that? I.e., does it cause memory leaks? Or some other resource that wouldn't be apparent unless I ran it 16K times?

In a broader sense, exactly how 'dependent' is Win32 on applications taking care of their messages? I would hope that when the compiler links the executable as a windows program, that the run-time would be capable of cleaning up any kind of message queue, be it emptied or not.

+4  A: 

Since you don't have a window, you don't need a message loop. In Win32 the messages are sent to windows, not to applications.

Robert
Sorry to say this is 100% wrong. The above program does have a window, and it does have a message loop.
1800 INFORMATION
I guess I was over-thinking the problem, imagining that Windows would create a message queue for every process, and that certain things (esp. initialization/cleanup) that might be required of a 'windows program' might be expected to operate in this way.
JustJeff
Sure the messagebox creates a window, and hence a loop, but is that what he was asking about??
Robert
Message queues are created for each thread that pumps messages, and or that create windows, as needed. Usually this implies there is one message queue per process (but not always)
1800 INFORMATION
@1800 INFORMATION: Sure it displays a dialog, but this dialog resides in one of the Win32 API DLLs (user32.dll, if I remember correctly). Therefore the message loop resides there as well...
milan1612
If you are going to ask a question about a program that doesn't have a message loop or any windows, then it is probably a good idea to not post sample code that has a message loop and windows. There isn't a whole lot of difference between a program that uses an implicit message loop and one that you wrote yourself
1800 INFORMATION
+2  A: 

You don't have to create windows. But still, there are some kind of messages, like

  • WM_TIMER
  • WM_TIMECHANGE
  • WM_CLIPBOARDUPDATE
  • WM_COPYDATA
  • WM_POWER

that you may need. So, a ghost window hanging around wouldn't be bad.

Nick D
ghost window? could you elaborate? that sounds like something that might be useful!
JustJeff
A ghost window is actually different from what you're talking about (see my post). You're referring to a hidden or invisible window, which is referred to as a message-only window, or sometimes a message pump or message sink.
P Daddy
Yes you are right. I mean a window that acts as a message pump, a window that the user can't see and can't interact.
Nick D
+2  A: 

You do have a message loop - MessageBox is a modal dialog and therefore contains a message loop within.

Michael
well, ok, but the message box was only intended as a trivial operation to illustrate that the program actually does something.
JustJeff
+7  A: 

Just a technicality, but you do have a window, and you do have a message loop, just not in your code.

The call to MessageBox() creates a window (of class #32770) and runs a local message loop, not returning to your code till the message loop drops out, presumably when WM_NCDESTROY is sent. I think it's the same message loop that runs in response to DialogBox().

But you could substitute your call to MessageBox() with anything else that really doesn't create a message loop, and you'll still be fine. Windows doesn't care if you have a message loop, although some functionality (primarily UI related) is difficult or impossible to use without it. In fact, you don't have to link to user32 at all, and some apps that have no user interface don't.

Now if you create a window and don't process messages for it in some way, Windows XP and up will replace your window with a "ghost" window that has a white client area and Task Manager will tell the user that the application is not responding.

Although it seems so at first, the message loop is not magic or a strictly required part of Windows boilerplate. It is highly ingrained as a standard in most Windows applications, though, because it's the best way to handle the dispatching of window messages. The "event-driven" nature of most Windows applications makes us forget sometimes that Windows applications were originally designed to be single-threaded, and in this model, it is code running within that single thread, not some unseen force within the operating system, that must make every function call within our code. The addition of multithreading changed that somewhat, but the basic model still remains the same.

EDIT

A note about message queues:

As is mentioned elsewhere, a message queue is only created (and on a per-thread basis) when a window is created by that thread. Your example program, upon creating a message box, does create a message queue. But this queue need not be empty when your application exits. This queue is just a memory structure. It's a block of memory that can hold a certain number of message objects (specifying destination hWnd, message id, wParam, lParam, system time when message was posted, mouse position when message was posted, and some data that allows the derivation of keyboard and mouse button state when the message was posted), along with pointers to the head and tail of the queue (I assume it's a circular queue). When the application exits, this memory, like all memory belonging to the process, is summarily freed.

There are, of course, other things that must be cleaned up outside your process. The OS must keep a table of all existing windows, for example, along with the thread and process that created them. Of course, these are all cleaned up automatically as well.

P Daddy
+1  A: 

If you don't have a window, then that is fine, but if you do then you need to make sure that you pump messages for it. Otherwise the system can hang on broadcast messages waiting for you to respond. This is important for things like COM which create hidden windows for message processing. If your main thread does not pump messages (e.g., by calling WaitForSingleObject) then calls to your COM objects will not be processed, and any programs which send broadcasts will appear to hang.

1800 INFORMATION
A: 

I have read somewhere (and can't find the reference) is that Windows will create a message queue on demand. If you never call a function that looks for a message queue, one will never be created. And this occurs on a per-thread basis.

Joel Lucsy