Advertisement

Please Help - Newbie here! - Weird Error-message

Started by April 13, 2001 02:54 PM
7 comments, last by MainForze 23 years, 10 months ago
Hi! I''m really new to programming in VC++6, so this question just might be the most stupid one you''ve ever heard, but here goes... I keyed in the classic "Hello World!" program that stood in a book I bought on VC++6 programming. When I compiled it for the first time, it had 15 errors in it, and no warnings (yay!! ). I managed to get rid of 14 of em, but there''s one left that I can''t get rid of. It occurs when I try to load the hbrBackGround member of the WNDCLASSEX structure with the value returned by GetStockObject(WHITE_BRUSH). I looked up WNDCLASSEX in my Win32API reference and it said that I can load WndClass.hbrBackground with COLOR_WINDOW (among others). I tried that but it gave the same results. I even tried reinstalling VC++6, because I thought maybe there was an installation-error in one of the VC++6 libraries!!!!!! The error it returns is like Chinese for me, so I included a copy of what the Debug-window in VC++6 spitted out: ################################################################# --------------------Configuration: Prog1_1 - Win32 Debug-------------------- Compiling... Prog1_1.cpp C:\WINDOWS\Profiles\MainForze\Desktop\DUMMIE\Prog1_1\Prog1_1.cpp(43) : error C2440: ''='' : cannot convert from ''const int'' to ''struct HBRUSH__ *'' Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast Error executing cl.exe. Prog1_1.exe - 1 error(s), 0 warning(s) ################################################################# I''ve also included a listing of the "Hello, World" program, should there be any error in it. Don''t try to read the comments, as they are in Dutch. But all you C++-gurus out there shouldn''t have a hard time figuring out what this code does. ################################################################# // Prog1_1 - Een met de hand geschreven Hello World-programma // zonder hulp van moderne gemakken zoals AppWizard #include // prototypedeclaraties int WINAPI WinMain( HINSTANCE hInstance, // handle van huidige instantie HINSTANCE hPrevInstance, // handle van vorige instantie LPSTR pszCmdLine, // opdrachtregel int nCmdShow // toon status van venster ); LRESULT CALLBACK WindowProc( HWND hWnd, // handle van venster UINT uMsgId, // identificatie bericht WPARAM wParam, // eerste parameter van bericht LPARAM lParam // tweede parameter van bericht ); // WinMain - hier begint de uitvoering int WINAPI WinMain( HINSTANCE hInstance, // handle van de huidige instantie HINSTANCE hPrevInstance, // handle van de vorige instantie LPSTR pszCmdLine, // opdrachtregel int nCmdShow // toon status van venster ) { static char szAppName[] = "Prog1"; HWND hWnd; MSG msg; WNDCLASSEX wndClass; // eerst de vensterklasse registreren wndClass.style = 0; wndClass.lpfnWndProc = WindowProc; // callback-functie wndClass.cbClsExtra = 0; wndClass.cbWndExtra = 0; wndClass.hInstance = hInstance; wndClass.hIcon = LoadIcon(hInstance, szAppName); wndClass.hCursor = LoadCursor(NULL, IDC_ARROW); ------> wndClass.hbrBackground = GetStockObject(WHITE_BRUSH); <---------------------------------- wndClass.lpszMenuName = NULL; wndClass.lpszClassName = szAppName; wndClass.hIconSm = wndClass.hIcon; if (RegisterClassEx(&wndClass) == 0) // registreer klasse ... return 0; // ... afsluiten als het niet lukt // het enige venster maken van de toepassing hWnd = CreateWindow( szAppName, // naam van de vensterklasse szAppName, // opschrift van venster WS_OVERLAPPEDWINDOW, // stijl van venster CW_USEDEFAULT, // eerste x- en... CW_USEDEFAULT, // ...y-positie CW_USEDEFAULT, // vensteromvang in x-... CW_USEDEFAULT, // ...en y-dimensie NULL, // handle van hoofdvenster NULL, // handle van menu hInstance, // handle van programma-instantie NULL); // parameters bij het maken if (hWnd == NULL) return 0; ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } // WindowProc - deze functie wordt aangeroepen om // Windows-berichten te verwerken LRESULT CALLBACK WindowProc( HWND hWnd, // handle van venster UINT uMsgId, // identificatie bericht WPARAM wParam, // eerste parameter van bericht LPARAM lParam // tweede parameter van bericht ) { static char *pszHello = "Hello, world"; // beslissen wat moet gebeuren met elk berichttype switch (uMsgId) { // het tekenbericht van het venster verwerken door // de string "Hello, world" weer te geven // in de linkerbovenhoek (coord 0,0) van het venster case WM_PAINT: { HDC hDC; PAINTSTRUCT paintStruct; hDC = BeginPaint(hWnd, &paintStruct); TextOut(hDC, // te gebruiken apparaatcontext 0, 0, // x,y locatie pszHello, // te schrijven string lstrlen(pszHello) // lengte string ); EndPaint(hWnd, &paintStruct); } break; // het vernietigingsbericht verwerken // door de toepadding af te sluiten case WM_DESTROY: PostQuitMessage(0); break; default: // Windows alle andere berichttypen laten verwerken return DefWindowProc(hWnd, uMsgId, wParam, lParam); } return(0); } ################################################################# I hope you guys can figure out what''s wrong with this code, so that I can go on learning VC++, because the whole book I have builds up on the "Hello World" prog listed here!!!! Thanks in advance, MainForze
"It's strange, isn't it? You stand in the middle of a library and go 'AAAAAAAGGHHHH' and everyone just stares at you. But you do the same thing on an airplane, and everyone joins in!"
Don''t worry, that''s a common error. Just change it to wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); and everything should be fine. What''s happening is the value returned by GetStockObject is (according to the error message) a const int, which is different than an HBRUSH, so you have to cast it manually or it won''t let you do it.



-Deku-chan

DK Art (my site, which has little programming-related stuff on it, but you should go anyway^_^)
Advertisement
Heya,

You might try casting the return value from GetStockObject to a HBRUSH. eg:

  wndClass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);   


I think the error is just telling you that the compiler refuses to make this cast implicitly.



-ns-
-ns-
Do you have:
#define STRICT
before you include the windows header files?

Without it the code would normally compile just fine. But it is still a good idea to keep it and use the methods the ppl above suggested: explicitly cast the return from GetStockObject() to an HBRUSH.

An alternative is to #include There are lots of helpful things in there. One of which is a macro called GetStockBrush() which basically performs the cast for you.
For example:
wndclass.hbrBackground = (HBRUSH)GetStockObject( WHITE_BRUSH );
is the same as:
wndclass.hbrBackground = GetStockBrush( WHITE_BRUSH );
Aargh! That''s me above, it is supposed to say #include "windowsx.h"
I used the ''<'' and ''>'' and that made the whole thing disappear!
Thanks you guys for all the replies, but there still another error that the compiler didn''t mention!! The program compiles OK, but when I run it, I still don''t see anything on the screen! The Window just doesn''t appear! The debug window says something about missing functions or something in various DLL files like gdi32.dll, etc. This MUST be wrong, because MFC-apps can display windows. What''s wrong now??? please help!!!!

Thanks in advance,

MainForze
"It's strange, isn't it? You stand in the middle of a library and go 'AAAAAAAGGHHHH' and everyone just stares at you. But you do the same thing on an airplane, and everyone joins in!"
Advertisement
your program exits at:

if (RegisterClassEx(&wndClass) == 0) // registreer klasse ...
return 0; // ... afsluiten als het niet lukt

because you are not filling in the WNDCLASSEX struct correctly

first of all you (or the book) forgot one very important member:

wndClass.cbSize = sizeof(WNDCLASSEX);

and second:

wndClass.hIcon = LoadIcon(hInstance, szAppName);

you are trying to load a resource icon, that's an icon that you compile into the exe, i assume you haven't created such an icon, so replace it by one of the standard windows-icons:

wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);

now it should work


BTW: instead of just exiting your app when there's an error you could show an error message, so you know when something goes wrong:

if (RegisterClassEx(&wndClass) == 0) // registreer klasse ...
{
MessageBox(NULL, "RegisterClassEx() failed", "error", MB_OK | MB_ICONERROR);
return 0; // ... afsluiten als het niet lukt
}


Edited by - kvh on April 14, 2001 9:25:11 AM
quote:

wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);




you can probably leave this out just set wndClass.hIcon = NULL;

PS Am I the only person on this planet that doesn''t know german?


-----------------------------------------------------------
"If I wanted to hear the pitter patter of little feet I would put shoes on my cat"
-----------------------------------------------------------"People who usualy use the word pedantic usualy are pedantic!"-me
Thanks for the suggestion!! BTW: Dutch is the language that is spoken in the Netherlands, not in Germany!!!
"It's strange, isn't it? You stand in the middle of a library and go 'AAAAAAAGGHHHH' and everyone just stares at you. But you do the same thing on an airplane, and everyone joins in!"

This topic is closed to new replies.

Advertisement