26

I'm trying to use CreateWindowEx to generate a message-only window:

_hWnd = CreateWindowEx( 0, NULL, NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL );

When my application executes this line it always returns _hWnd = 0. What am I doing wrong?

Axalo
  • 2,953
  • 4
  • 25
  • 39
Jim Fell
  • 13,750
  • 36
  • 127
  • 202
  • You should be able to call GetLastError() to get more info. –  Nov 02 '10 at 19:31
  • 1
    There is example code to do this via MFC [here](http://www.codeproject.com/KB/dialog/messageonly.aspx). This should hide some of the Win32 API parameter complexity. – Steve Townsend Nov 02 '10 at 19:43

2 Answers2

53

lpClassName shouldn't be NULL. Register class using RegisterClassEx function and pass it to CreateWindowEx.

static const char* class_name = "DUMMY_CLASS";
WNDCLASSEX wx = {};
wx.cbSize = sizeof(WNDCLASSEX);
wx.lpfnWndProc = pWndProc;        // function which will handle messages
wx.hInstance = current_instance;
wx.lpszClassName = class_name;
if ( RegisterClassEx(&wx) ) {
  CreateWindowEx( 0, class_name, "dummy_name", 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL );
}
Kirill V. Lyadvinsky
  • 97,037
  • 24
  • 136
  • 212
  • 1
    Oh, dread! I was hoping to avoid `RegisterClass` as the documentation for it is very ambiguous. Do you know of a good example? – Jim Fell Nov 02 '10 at 19:33
  • 1
    Without a class, how else would you feed the window procedure to the system? (I know about subclassing via GetWindowLong()) – Seva Alekseyev Nov 02 '10 at 19:48
  • 3
    @Kirill V. Lyadvinsky: Is `current_instance` important? How does it get set? – Jim Fell Nov 02 '10 at 20:00
  • 6
    If you don't want to register the window class, you can use the pre-defined `STATIC` class (used for text labels and image boxes). If you don't give it a `hWndParent` parameter, it creates a standalone window! – André Caron Nov 02 '10 at 20:46
  • @Kirill V. Lyadvinsky: Odd, it started working when I copied the example as given. It does work, though, so you get the points. Cheers! – Jim Fell Nov 02 '10 at 21:39
  • You may use any of standard window classes, like BUTTON, as well. – Fr0sT Aug 08 '16 at 12:51
  • 2
    If you don't register a class there is no way to retrieve the messages. – AnArrayOfFunctions Apr 29 '19 at 18:03
  • This is quite an old answer. Is it still up to date? – Akito Apr 26 '21 at 01:58
-9

According to the Microsoft docs the class name should be "Message".

Cheers & hth.,

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • 14
    The table listing `Message` window class is described as "The following table describes the system classes that are available only for use by the system. They are listed here for completeness sake." so I don't think Windows expects you to use `Message` window class. It would be useless anyway as then you would have to sub-class to provide own window procedure... – Adam Badura Dec 02 '11 at 09:13
  • downgrading to make sure others don't consider that to be a valid answer. See the comment above for a reason (it's a system-only class, can't be used by non-system) – YePhIcK Jun 20 '13 at 11:54