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

Discussion in 'C++' started by Thomas Barth, Nov 11, 2004.

  1. Thomas Barth

    Thomas Barth Guest

    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.
     
    Thomas Barth, Nov 11, 2004
    #1
    1. Advertising

  2. "Thomas Barth" <> wrote in message
    news:...
    > 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
     
    John Harrison, Nov 11, 2004
    #2
    1. Advertising

  3. Thomas Barth

    Thomas Barth Guest

    >> 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.
     
    Thomas Barth, Nov 11, 2004
    #3
  4. 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
     
    Karl Heinz Buchegger, Nov 11, 2004
    #4
  5. "Thomas Barth" <> wrote in message
    news:...
    > >> 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
     
    John Harrison, Nov 11, 2004
    #5
  6. Thomas Barth

    Thomas Barth Guest

    > 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.
     
    Thomas Barth, Nov 11, 2004
    #6
  7. 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
     
    Karl Heinz Buchegger, Nov 11, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ollej Reemt
    Replies:
    7
    Views:
    567
    Jack Klein
    Apr 22, 2005
  2. Stig Brautaset

    `void **' revisited: void *pop(void **root)

    Stig Brautaset, Oct 25, 2003, in forum: C Programming
    Replies:
    15
    Views:
    812
    The Real OS/2 Guy
    Oct 28, 2003
  3. Replies:
    5
    Views:
    850
    S.Tobias
    Jul 22, 2005
  4. philwozza
    Replies:
    3
    Views:
    3,563
    philwozza
    May 13, 2006
  5. Replies:
    1
    Views:
    418
    Victor Bazarov
    May 23, 2007
Loading...

Share This Page