Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > [win32] invalid conversion from 'void*' to 'HBRUSH__*' ?

Reply
Thread Tools

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

 
 
Thomas Barth
Guest
Posts: n/a
 
      11-11-2004
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.



 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      11-11-2004

"Thomas Barth" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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 news:comp.os.ms-windows.programmer.win32

john


 
Reply With Quote
 
 
 
 
Thomas Barth
Guest
Posts: n/a
 
      11-11-2004
>> 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 news:comp.os.ms-windows.programmer.win32
>
> 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.


 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      11-11-2004
Thomas Barth wrote:
>
>
> 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.


--
Karl Heinz Buchegger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      11-11-2004

"Thomas Barth" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> >> 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 news:comp.os.ms-windows.programmer.win32
> >
> > 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?
>


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


 
Reply With Quote
 
Thomas Barth
Guest
Posts: n/a
 
      11-11-2004
> 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.


 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      11-11-2004
Thomas Barth wrote:
>
> > 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


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


--
Karl Heinz Buchegger
(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
invalid character in base-64 string and invalid postback or callba kevin ASP .Net 0 01-16-2008 09:39 PM
How do fix: "Wrong number of arguments" / "invalid propert assignment"/ "Invalid argument" Java Guy Javascript 1 10-15-2006 08:41 PM
How do fix: "Wrong number of arguments" / "invalid propert assignment"/ "Invalid argument" Java Guy Java 1 10-15-2006 08:31 PM
Invalid URI: There is an invalid sequence in the string. Error!! Patrick.O.Ige ASP .Net 1 07-02-2006 11:21 AM
invalid@invalid.org Hank Sniadoch Computer Information 1 08-20-2004 11:24 AM



Advertisments