Skip to main content
GameDev.net gamedev.net
🔒 Locked

Win32 Problem

Started by Tazel Jan 26, 2003 at 12:34 AM 20 replies 550+ views
Original Post
Tazel
Tazel
This has worked before:
  
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

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 WinMain(HINSTANCE hinstance, 
			HINSTANCE hprevinstance,
			LPSTR lpcmdline, 
			int nshowcmd)
{
	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 = GetStockObject(BLACK_BRUSH);
	winclass.lpszMenuName = NULL;
	winclass.lpszClassName = "WINCLASS1";
	winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

	if(!RegisterClassEx(&winclass))
		return(0);

	if(!(hwnd = CreateWindowEx(NULL,
					"WINCLASS1",
					"Cong",
					WS_POPUP | WS_VISIBLE,
					0,0,
					800,600,
					NULL,
					NULL,
					hinstance,
					NULL)))

	return(0);

	while(GetMessage(&msg,NULL,0,0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return(msg.wParam);
}
	  
Now these erros appear:
c:\program files\microsoft visual studio\my projects\pong\main.cpp(43) : warning C4007: ''WinMain'' : must be ''__stdcall''
c:\program files\microsoft visual studio\my projects\pong\main.cpp(56) : error C2440: ''='' : cannot convert from ''void *'' to ''struct HBRUSH__ *''
        Conversion from ''void*'' to pointer to non-''void'' requires an explicit cast
 
What''s wrong!? :''(
tcache Contact Me ----------- AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
tcacheContact Me-----------AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
mutex
mutex
You need to declare WinMain as:

int WINAPI WinMain(...

The second error means you have to cast from void* to HBRUSH.

Beer Hunter
Beer Hunter
int WinMain(...) // warning C4007: ''WinMain'' : must be ''__stdcall''
The __stdcall goes here:
int __stdcall WinMain(...)
There''re some macros for __stdcall (APIENTRY, WINAPI, ..?). Flip some coins, I guess.

winclass.hbrBackground = GetStockObject(BLACK_BRUSH); // error C2440: ''='' : cannot convert from ''void *'' to ''struct HBRUSH__ *'', etc...
Implicit casts from void* aren''t allowed in c++. Explicit casting is necessary:
winclass.hbrBackground = reinterpret_cast<HBRUSH>(GetStockObject(BLACK_BRUSH));
Tazel
Tazel
With cast<>()? Why does Lamothe do it with GetStockObject? It''s supposed to work! It did before...



tcache
Contact Me
-----------
AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
tcacheContact Me-----------AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
mutex
mutex
Were you using plain C before? I think C allows for such things.

Tazel
Tazel
No. I exactly copied Lamothe''s code once and it worked perfectly.



tcache
Contact Me
-----------
AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
tcacheContact Me-----------AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
ctoan
ctoan
You don''t need to use the static cast method with GetStockObject() (the static cast is like ()). You can also do it with a type case which looks like this (HBRUSH).

The reason you do this is because GetStockObject() returns a void* type. With a void* type, the compiler has no idea what is being pointed to, and you have to cast it to the type you want. The first error, the other posters are right, you need the WinMain declaration to be:

int WINAPI WinMain(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)

To solve the second problem, simply put (HBRUSH) in front of the GetStockObject() function. Good luck!

Chris Pergrossi
Chris Pergrossi< ctoan >My Realm
BitMaster
BitMaster
I might be mistaken about this, but I think if STRICT (as in strict types) is not defined before windows.h is included, all those special handles are just #define HSOMETHING void*. If it is defined, they are truely typedefed. While without STRICT it is more convenient sometimes, I strongly suggest using STRICT at all times. Type safety (even if it is limitited) is always something to be cherished. If it worked before, my guess is either on a new include or a default setting in your IDE that somehow got triggered.
Tazel
Tazel
quote:
Original post by ctoan
You don''t need to use the static cast method with GetStockObject() (the static cast is like ()). You can also do it with a type case which looks like this (HBRUSH).

The reason you do this is because GetStockObject() returns a void* type. With a void* type, the compiler has no idea what is being pointed to, and you have to cast it to the type you want. The first error, the other posters are right, you need the WinMain declaration to be:

int WINAPI WinMain(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)

To solve the second problem, simply put (HBRUSH) in front of the GetStockObject() function. Good luck!

Chris Pergrossi




Thank you. I think that''s what happened last time. So that''s a fifth type of casts? I need to tell Lamothe that that''s an error...




tcache
Contact Me
-----------
AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
tcacheContact Me-----------AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
Kippesoep
Kippesoep
STRICT is defined by default in MSVC6. In MSVC5, it''s not. So, in MSVC5, the code works without the cast.

Kippesoep
Tazel
Tazel
Yes, but in the front of TOTWGPG, it says he''s developing with 6 so that must be an error.



tcache
Contact Me
-----------
AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
tcacheContact Me-----------AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
Dave Hunt
Dave Hunt
From TOTWGPG Page 909:

"Typecast Errors - If the compiler gives you a typecast error on a line of code (VC++ 6.0 users beware), just cast it! I have over 3,000 emails from people who don't know what a typecast is. If you don't, look it up in a C/C++ book. But I will tell you that some of my code may still be missing an explicit typecast here or there. VC++ 6.0 might get belligerent. If you get one of these errors, look at what the compiler expects and just put it in front of the rvalue in the expression to fix it."

Also, in the front of the book (page 5), he states that "I wrote the code for this book with Microsoft Visual C++ 5.0/6.0." That's not the same as "developing with 6".




[edited by - Dave Hunt on January 26, 2003 3:48:30 PM]
niyaw
niyaw
quote:

If you get one of these errors, look at what the compiler expects and just put it in front of the rvalue in the expression to fix it.

using this technique, you can typecast yourself straight to hell.
niyaw
niyaw
quote:
Original post by Dave Hunt
Also, in the front of the book (page 5), he states that "I wrote the code for this book with Microsoft Visual C++ 5.0/6.0." That''s not the same as "developing with 6".


however, it does include "developing with 6". if he mentioned vc6, he should''ve tested all of his code on vc6 as well.

how would i know that compiler errors from his code result from his lameness or from something having changed, making his code incorrect?
zealouselixir
zealouselixir
quote:
Original post by niyaw
using this technique, you can typecast yourself straight to hell.


Yes. Especially with function pointers. I've had some really difficult to find bugs caused by lack of judicious typecasting.

[edited by - zealouselixir on January 26, 2003 4:01:22 PM]
[twitter]warrenm[/twitter]
zealouselixir
zealouselixir
quote:
Original post by niyaw
however, it does include "developing with 6". if he mentioned vc6, he should''ve tested all of his code on vc6 as well.

how would i know that compiler errors from his code result from his lameness or from something having changed, making his code incorrect?


1) He really has no such obligation.
2) He did indicate the potential problem.
3) He did tell the readers how to fix it.
[twitter]warrenm[/twitter]
Dave Hunt
Dave Hunt
quote:
Original post by niyaw
using this technique, you can typecast yourself straight to hell.


I''m sure he wasn''t recommending this as a general, fix-all technique. Rather, he was referring specifically to the cases in his book code where the casts were missing.
BitMaster
BitMaster
My guess is that he just imported his VC 5 projects into VC 6. If that is the case, then I don''t think the STRICT would be automatically added. If I''m correct, this is a problem that only happens when creating a new project.

And, anyway, if push comes to shove, the code can be compiled without any modifications by removing the STRICT define from the project settings.
Tazel
Tazel
I''m sorry about making this discussion without reading completely and thoroughly but I didn''t mean to start a flame war...sheesh!



tcache
Contact Me
-----------
AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
tcacheContact Me-----------AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
zealouselixir
zealouselixir
This is not a flame war. There have been no offensive statements made, and no personal attacks. This is a logical debate that is completely under control as far as I can tell.
[twitter]warrenm[/twitter]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.