[win32] invalid conversion from 'void*' to 'HBRUSH__*' ?

T

Thomas Barth

Hi,
I'm new to windows programming and still reading a book about
windows-programming with C++. I copied the following code from
the book into my ide (Eclipse/CDT) to comprehend the code, but
two errors occur.

In function `LRESULT WndProc(HWND__*, unsigned int, unsigned int, long int)':
line 48: error: invalid conversion from `void*' to `HBRUSH__*'
line 49: error: invalid conversion from `void*' to `HPEN__*'

<code>
#include <windows.h>

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);

int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow ) {

WNDCLASS WndClass;
WndClass.style = 0;
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
WndClass.lpfnWndProc = WndProc;
WndClass.hInstance = hInstance;
WndClass.hbrBackground = (HBRUSH) (COLOR_WINDOW+1);
WndClass.hCursor = LoadCursor (NULL, IDC_ARROW);
WndClass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
WndClass.lpszMenuName = 0;
WndClass.lpszClassName = "WinProg";
RegisterClass(&WndClass);

HWND hWindow;
hWindow = CreateWindow("WinProg","Fenster", WS_OVERLAPPEDWINDOW,
0,0,600,460,NULL,NULL, hInstance, NULL);
ShowWindow (hWindow, nCmdShow);
UpdateWindow (hWindow);

MSG Message;
while (GetMessage(&Message, NULL, 0, 0)) {
DispatchMessage(&Message);
}
return (Message.wParam);
}

LRESULT CALLBACK WndProc (HWND hWnd, UINT uiMessage,
WPARAM wParam,LPARAM lParam) {
switch(uiMessage) {
case WM_PAINT: HPEN hPen;
HPEN hPenalt;
HBRUSH hBrush;
HBRUSH hBrushalt;
hBrush = CreateSolidBrush (RGB(255,100,0));
hPen = CreatePen (PS_SOLID,2,RGB(0,255,255));
HDC hdc;
PAINTSTRUCT ps;
hdc = BeginPaint (hWnd, &ps);
hBrushalt = SelectObject (hdc, hBrush);
hPenalt = SelectObject (hdc, hPen);
MoveToEx (hdc, 20, 20, NULL);
LineTo (hdc, 100, 100);
Rectangle (hdc, 120, 20, 240, 140);
RoundRect (hdc, 260, 20, 420, 140, 20, 20);
RECT rect;
SetRect (&rect, 20, 260, 240, 420);
FrameRect (hdc, &rect, hBrush);
SetRect (&rect, 260, 260, 420, 420);
FillRect (hdc, &rect, hBrush);
Ellipse (hdc, 440, 260, 480, 420);
SelectObject (hdc, hBrushalt);
SelectObject (hdc, hPenalt);
DeleteObject (hPen);
DeleteObject (hBrush);
EndPaint (hWnd, &ps);
return 0;
case WM_DESTROY: PostQuitMessage(0);
return 0;
default: return DefWindowProc (hWnd, uiMessage, wParam, lParam);
}
}
</code>

Anyone who can help me out?

Thanks in advance,

Thomas B.
 
J

John Harrison

Thomas Barth said:
Hi,
I'm new to windows programming and still reading a book about
windows-programming with C++. I copied the following code from
the book into my ide (Eclipse/CDT) to comprehend the code, but
two errors occur.

In function `LRESULT WndProc(HWND__*, unsigned int, unsigned int, long int)':
line 48: error: invalid conversion from `void*' to `HBRUSH__*'
line 49: error: invalid conversion from `void*' to `HPEN__*'

In C++ it is not legal to convert from void* to any other pointer
implicitly. Unforuntately Windows programming forces you to use casts, so
just add the necessary casts to the indicated lines.

Please not that Windows programming questions are not on topic in this
group. Try
john
 
T

Thomas Barth

Hi,
In C++ it is not legal to convert from void* to any other pointer
implicitly. Unforuntately Windows programming forces you to use casts, so
just add the necessary casts to the indicated lines.

Please not that Windows programming questions are not on topic in this
group. Try
john

Thanks for answering anyway, I wonder why the author of the book didn't get
the error-messages. Does it depend on the compiler?

Thomas B.
 
K

Karl Heinz Buchegger

Thomas said:
Thanks for answering anyway, I wonder why the author of the book didn't get
the error-messages. Does it depend on the compiler?

You will find that lots of books contian lots of errors.
This is why it is a good idea to first learn C++ and only
then to switch to 'use C++ for xxx programming'. Even
if this looks like a tedious job.
 
J

John Harrison

Thomas Barth said:
Thanks for answering anyway, I wonder why the author of the book didn't get
the error-messages. Does it depend on the compiler?

It could be that the book was written for C, although looking at the code
that doesn't seem right.

It could be that the book was written for non-STRICT mode Windows. In STRICT
mode all the different types of handle are different types, this is
obviously a good thing. But in non-STRICT mode HPEN, HBRUSH etc are all
void*. STRICT mode has been the default for a long time now IIRC, so if that
might indicate that your book is a bit out of date. This is compiler
dependent though, do you have -DSTRICT on your compiler command line?

There is only one book worth learning Windows programming from and that is
'Programming Windows' by Charles Petzold (make sure you get the C edition
not the C# edition).

I'm not really much of a Windows programmer any more so my information might
be out-of-date.

John
 
T

Thomas Barth

You will find that lots of books contian lots of errors.
This is why it is a good idea to first learn C++ and only
then to switch to 'use C++ for xxx programming'. Even
if this looks like a tedious job.

You are right, but I ve already read a lot about C++.
Now I want to learn C++ by programming and by fixing the
errors of experienced coders :)

Thomas B.
 
K

Karl Heinz Buchegger

Thomas said:
You are right, but I ve already read a lot about C++.
Now I want to learn C++ by programming and by fixing the
errors of experienced coders :)

No insult intended:
If this is so, then correcting this casting problem shouldn't
be much of a problem :)
You fill find that during Windows programming you constantly
will cast things. The tricky part is to figure out, when the
compiler emits an error and a cast looks like the thing to do
to shut it up, but in reality the compiler is right and a cast
is *not* the thing that's needed :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,034
Messages
2,570,356
Members
47,002
Latest member
RobertoLip

Latest Threads

Top