Question: GetStockObject()

This is a discussion on Question: GetStockObject() within the Windows Programming forums, part of the Platform Specific Boards category; Can some kindly person please explain what is wrong with this snippet of code from TheForgers WinAPI Tutorial? LRESULT CALLBACK ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    11

    Exclamation Question: GetStockObject()

    Can some kindly person please explain what is wrong with this snippet of code from TheForgers WinAPI Tutorial?

    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    switch(msg)
    {
    case WM_CREATE:
    {
    HFONT hfDefault;
    HWND hEdit;

    hEdit = CreateWindowEx
    (WS_EX_CLIENTEDGE, "EDIT", "",
    WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL, 0, 0, 100, 100, hwnd, (HMENU)IDC_MAIN_EDIT, GetModuleHandle(NULL), NULL);

    if(hEdit == NULL)
    MessageBox(hwnd, "Could not create edit box.", "Error", MB_OK | MB_ICONERROR);

    hfDefault = GetStockObject(DEFAULT_GUI_FONT);

    SendMessage(hEdit, WM_SETFONT, (WPARAM)hfDefault, MAKELPARAM(FALSE, 0));
    }
    break;
    // Switch statement continues...............







    The problem is with this line
    hfDefault = GetStockObject(DEFAULT_GUI_FONT);


    I get the following error when I try to compile (vc6)....

    error C2440: '=' : cannot convert from 'void *' to 'struct HFONT__ *'
    Conversion from 'void*' to pointer to non-'void' requires an explicit cast
    Error executing cl.exe.

    aptest.exe - 1 error(s), 0 warning(s)


    I looked up GetStockObject() at MSDN and it seems that the function does take an integer argument and should return the type HFONT (a pointer or handle?) so I'm a confused beginner.

    Thanks In Advance

    dicky
    Last edited by dicky; 05-30-2004 at 09:15 AM.

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>what is wrong with this snippet of code<<

    Nothing - it's a snippet of 'c' code and would compile just great as such. The error you're getting is because you're compiling it as c++.

    So, you have two choices:
    1. Recompile the code as 'C'
    2. Cast the return value to the required type ie
      Code:
      hfDefault =(HFONT)GetStockObject(DEFAULT_GUI_FONT);
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    11
    Thanks Ken, that fixed it. Is this a common problem when taking examples written in C? I guess I can use the same method in a lot of situations. Perhaps I need a good tutorial on type casting as it's an area I don't thoroughly understand (yet!)

    dicky

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,853
    Its due to the difference in casts(ing) in C and C++.

    You must be using .cpp (not .c) files to create a C program and so the compiler applies the C++ casting rules.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  5. #5
    Semi n00b Mithoric's Avatar
    Join Date
    Jun 2003
    Posts
    293
    There is a tutorial on casting on cprogramming.com .. If I remember correctly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21