Creating a basic window - CreateWindowEx() error

Started by
6 comments, last by yckx 18 years, 4 months ago
Im currently reading Tricks of the Windows Game Programming Gurus by André LaMothe. In chapter 2 the author shows how to create a basic window. The result is this code: #define WIN32_LEAN_AND_MEAN #include <windows.h> #include <windowsx.h> #include <stdio.h> #include <math.h> #define WINDOW_CLASS_NAME "WINCLASS1" LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) { PAINTSTRUCT ps; HDC hdc; switch(msg) { case WM_CREATE: { return(0); } break; case WM_PAINT: { hdc = BeginPaint(hwnd,&ps); EndPaint(hwnd,&ps); return(0); } break; case WM_DESTROY: { PostQuitMessage(0); return(0); } break; default:break; } return (DefWindowProc(hwnd, msg, wparam, lparam)); } INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int ncmdshow) { WNDCLASSEX winclass; HWND hwnd; MSG msg; winclass.cbSize = sizeof(WNDCLASSEX); winclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW; winclass.lpfnWndProc = WindowProc; winclass.cbClsExtra = 0; winclass.cbWndExtra = 0; winclass.hInstance = hinstance; winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); winclass.hCursor = LoadCursor(NULL, IDC_ARROW); winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); winclass.lpszMenuName = NULL; winclass.lpszClassName = WINDOW_CLASS_NAME; winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION); if (!RegisterClassEx(&winclass)) return(0); if (!(hwnd = CreateWindowEx(NULL, WINDOW_CLASS_NAME, "Your Basic Window", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 0,0, 400,400, NULL, NULL, hinstance, NULL))) return(0); while(GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return(msg.wParam); } When trying to compile the code in Dev-C++ or MSVC++ 6.0 Introductory i get an error from the CreateWindowEx() function. It is in the line with the third "NULL" - or the last parameter of the function. The error message says something like: [Warning] passing NULL used for non-pointer converting 1 of `HWND__* CreateWindowExA(DWORD, const CHAR*, const CHAR*, DWORD, int, int, int, int, HWND__*, HMENU__*, HINSTANCE__*, void*)' and: ld returned 1 exit status Can anyone tell me what is wrong with this code and maybe suggest some changes to make my basic window compile and run?
Advertisement
The first parameter should not be NULL. It should be a list of extended window styles you want. Or possibly 0. I'm not sure what a 0 value would give you, but I don't see why it wouldn't work. Use WS_EX_APPWINDOW and see if you get what you want.

Read about CreateWindowEx on MSDN to learn more about the function and what extended window styles you can use.
If it does not work what yckx had written you may try to get the last error using GetLastError() function and check in MSDN what does the error mean.
There has to be more to the compiler/linker output. The only message you've posted is just a warning. There must be something before/after that that is causing the linker to return an error.
I did as yckx wrote and it seemed to fix some of my problems. Now all i get when compiling/linking is:

[Linker error] undefined reference to `GetStockObject@4'
ld returned 1 exit status

There is no reference to any line anymore. I use Dev-C++.
Quote:Original post by Soren_O
I did as yckx wrote and it seemed to fix some of my problems. Now all i get when compiling/linking is:

[Linker error] undefined reference to `GetStockObject@4'
ld returned 1 exit status

There is no reference to any line anymore. I use Dev-C++.


add -lgdi32 to your linker flags, while you're at it, throw in -luser32 too just in case.
It works now! I can compile, link and get an .exe! :) ...but i get 2 windows when i run the exe. I get the one as described in the book and then a second one that looks like a console window in the background. As its title it has the destination of my exe. How do i get rid of this window?

Thank you all for the help so far.
Quote:Original post by Dave Hunt
The only message you've posted is just a warning. There must be something before/after that that is causing the linker to return an error.
I should have cought that.
Quote:Original post by Soren_O
i get 2 windows when i run the exe. I get the one as described in the book and then a second one that looks like a console window in the background. As its title it has the destination of my exe. How do i get rid of this window?
There is a checkbox in either Build Options or Project Options or something similar that fixes that. I don't have Dev-C++ installed anymore, so I can't be more specific than that.

This topic is closed to new replies.

Advertisement