3

I am creating messages windows from my console application. The window class is registered correctly and the window is created correctly however it never has a title (while my createwindow function call does specify a title). Got me thinking, can console programs create windows with name? Googled it, found nothing. This is my code, kept to the minimum :

using namespace std;
hInstance = GetModuleHandle(NULL);
WNDCLASS WndClass = {};
WndClass.style = CS_HREDRAW | CS_VREDRAW; // == 0x03
WndClass.lpfnWndProc = pWndProc;
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
WndClass.hIcon = 0;
WndClass.hCursor = 0;
WndClass.hbrBackground = (HBRUSH)COLOR_WINDOWFRAME;
WndClass.lpszMenuName = 0;
WndClass.lpszClassName = "EME.LauncherWnd";
int style = WS_OVERLAPPED | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_SYSMENU | WS_THICKFRAME | WS_CAPTION;
if (RegisterClassA(&WndClass))
{
    cout << "class registered. Hinstance : " << hInstance <<  " style : (expect 0xcf0000) " << std::hex << style << endl;
    HWND hwind2 = CreateWindowExA(0, "EME.LauncherWnd", "Mytitle", style, 0x80000000, 0x80000000, 0x80000000, 0x80000000, NULL, NULL, hInstance, NULL);
    if (hwind2 == 0)
        cout << "Couldn't create window" << endl;
    else
        cout << "created window" << endl;
}

output :

class registered. Hinstance : 00E40000
created window

Checking with Nirsoft's Winlister, the window exists, has the right class ("EME.LauncherWnd"), but has no name. furthermore, adding these lines of code in the block :

if (0 == SetWindowText(hwind2, "aTitle"))
            cout << "couldn't set a title" << endl;
        else
            cout << "title set " << endl;

The output is

title set

And yet, the window still doesn't have a title. If console program couldn't have title I'd assume the SetWindowText call would return 0. What am I doing wrong ? Edit : Adding pWndProc as requested

LRESULT CALLBACK pWndProc(HWND hwnd,            // Handle to our main window
    UINT Msg,             // Our message that needs to be processed
    WPARAM wParam,        // Extra values of message 
    LPARAM lParam)        // Extra values of message
{
        switch (Msg)

        {
    case WM_DESTROY: 
....
break; 
         }
}

Though after the comment pointing out the pWndProc (which body i thought was irrelevant to the construction of the window), it turns out inserting this code line as a default in the switch case

return DefWindowProc(hwnd, Msg, wParam, lParam);

solves the problem.

user
  • 934
  • 6
  • 17
  • 2
    "*can console programs create windows with name?*" - yes, of course. The console itself is just an ordinary Win32 app, afterall. Console apps have full access to the Win32 API. That being said, what does `pWndProc` point to exactly, and is it processing window messages correctly? – Remy Lebeau Feb 14 '17 at 02:56
  • u nead a massage loop – Cheers and hth. - Alf Feb 14 '17 at 03:14
  • Voting to close as lacking reproducible example. – Cheers and hth. - Alf Feb 14 '17 at 03:15
  • in passing, `(HBRUSH)COLOR_WINDOWFRAME` is wrong. see the docs. – Cheers and hth. - Alf Feb 14 '17 at 03:16
  • 3
    Are you passing all unhandled messages to `DefWindowProc()` ? – Jonathan Potter Feb 14 '17 at 03:30
  • @Jonathan Potter this was indeed the problem, thanks – user Feb 14 '17 at 03:42
  • This is the wrong way to create a messages window. Don't create an off screen window. Create an actual message only window. HWND_MESSAGE. – David Heffernan Feb 14 '17 at 07:15
  • If your issue is resolved, please post the solution in a dedicated answer. Doing so allows future visitors to more easily discover and navigate to the solution. See [Can I answer my own question?](http://stackoverflow.com/help/self-answer) for information. – IInspectable Feb 14 '17 at 08:19
  • 1
    Possible duplicate of [MS Windows- Main window no response when show menu on a tray notify icon(use a hidden window to process message)](http://stackoverflow.com/questions/7214368/ms-windows-main-window-no-response-when-show-menu-on-a-tray-notify-iconuse-a-h) – Raymond Chen Feb 14 '17 at 15:12
  • I stumbled upon here while solving a problem of a window title not showing up. Turns out one has to pass [`WM_NCCREATE`](https://learn.microsoft.com/en-us/windows/win32/winmsg/wm-nccreate) message to [`DefWindowProcW`](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-defwindowprocw). I returned `TRUE` as the documentation suggests, so the window got created but had no title. – Joe Jul 17 '19 at 11:06

1 Answers1

1

I am posting the answer to the question as suggested by a comment : The answer is that for the window creation to complete, the pWndProc passed to RegisterClass WINAPI has to process default messages (in particular OS messages). During the execution of CreateWindow(after the call has started and before it has returned), the pWndProc function already receives messages that it has to process, in my case it didn't process them. This is a standard pWndProc function:

LRESULT CALLBACK pWndProc(HWND hwnd,            // Handle to our main window
    UINT Msg,             // Our message that needs to be processed
    WPARAM wParam,        // Extra values of message 
    LPARAM lParam)        // Extra values of message
{
        switch (Msg)

        {
    case WM_DESTROY: 
...
    default:
        return DefWindowProc(hwnd, Msg, wParam, lParam);
         }
}

source :

A window procedure does not usually ignore a message. If it does not process a message, it must send the message back to the system for default processing. The window procedure does this by calling the DefWindowProc function, which performs a default action and returns a message result. The window procedure must then return this value as its own message result. Most window procedures process just a few messages and pass the others on to the system by calling DefWindowProc.

user
  • 934
  • 6
  • 17
  • Hmm, curious that it works at all. Not processing WM_NCCREATE normally also puts an end to it. Well, message windows are special, probably why. You shouldn't be setting their text since nobody can see it, they were meant to be cheap. A tool like Spy++ can show the class name, plenty good enough. – Hans Passant Feb 14 '17 at 14:39
  • @HansPassant Although asker talks of message windows, he doesn't pass `HWND_MESSAGE`, so this isn't a message window. I think he just hopes to put it off screen and hope it is not noticed! – David Heffernan Feb 14 '17 at 15:52
  • I am in fact reverse engineering a program for fun and knowledge gain (no profit). I code my own launcher of a MMORPG which purpose is to login and launch game in 5 s instead of the 1 minute with retail launcher. I created my window the same way the original company did (I debugged their arguments and function calls on createwindow and registerclass) and i use the same title/class names which are not up to me. – user Feb 14 '17 at 17:17