Click here to Skip to main content
15,908,175 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: linker Pin
George_George19-Feb-08 14:50
George_George19-Feb-08 14:50 
GeneralRe: linker Pin
Stephen Hewitt18-Feb-08 17:55
Stephen Hewitt18-Feb-08 17:55 
GeneralRe: linker Pin
George_George18-Feb-08 18:50
George_George18-Feb-08 18:50 
GeneralNasty runtime error with WM_PAINT in native GUI framework. Pin
edu.net18-Feb-08 14:38
edu.net18-Feb-08 14:38 
GeneralRe: Nasty runtime error with WM_PAINT in native GUI framework Pin
Joe Woodbury18-Feb-08 16:05
professionalJoe Woodbury18-Feb-08 16:05 
GeneralRe: Nasty runtime error with WM_PAINT in native GUI framework Pin
edu.net18-Feb-08 22:00
edu.net18-Feb-08 22:00 
GeneralRe: Nasty runtime error with WM_PAINT in native GUI framework. Pin
Mark Salsbery19-Feb-08 8:33
Mark Salsbery19-Feb-08 8:33 
GeneralRe: Nasty runtime error with WM_PAINT in native GUI framework - WORKAROUND FOUND Pin
edu.net19-Feb-08 13:59
edu.net19-Feb-08 13:59 
I stepped through the

nControl->WndProc(Msg & nMsg)


and found that nControl was not a valid pointer. This was irregular because it only happened when the WM_PAINT and the WM_NCPAINT were sent to the window.

Here's the history:

I have a class, WinControl that is responsible for wrapping methods related to manipulation of a windows control. It has a RegisterWindowClass method that creates the WNDCLASSEX passing it to the the native RegisterClassEx API function. The lpfnWndProc is set to point to a public static GlobalWndProc member function of the WinControl class.

Upon window creation using CreateWindowEx all windows messages are passed to the GlobalWndProc function. In here the HWND sent by windows is associated with the WinControl being created (when the WM_NCCREATE message is received):

SetWindowLong(theHWND, GWL_USERDATA, pointerToTheWinControlObtainedFromTheLPARAM)


On subsequent system messages, the WinControl mapping to the HWND supplied by the system is retrieved by:

GetWindowLong(theHWND, GWL_USERDATA)


Once retrieved the message is passed to a protected WndProc method in the WinControl class where using the this pointer, the window is able to consume it appropriately.

The problem was arising because the retrieval of the WinControl was failing, specifically during the WM_PAINT message.

After lots of cursing, gnashing of teeth, the following workaround seems to have settled the issue:

1. Associate the WinControl's HWND using:
static VOID 
    AssociateControlWithHWnd(IN CONST WinControl * pControl,
                             IN CONST HWND pHWnd)
    {
      if(pHWnd != NULL)
      {
        SetWindowLongPtr (pHWnd, 0, 0);

        SetWindowLongPtr (pHWnd, 8, (LONG_PTR) pControl);

        SetWindowLongPtr (pHWnd, GWLP_USERDATA, improbableWindowNumber);
      }
    }


2. Retrieve the WinControl's HWND using:

static WinControl * 
    GetControlFromHWnd(HWND pHWnd)
    {
      if((pHWnd != NULL) && (::GetWindowLong(pHWnd, GWLP_USERDATA) == improbableWindowNumber))
      {
        return reinterpret_cast<WinControl *>GetWindowLong(pHWnd, 8));
      }

      return NULL;
    }


where improbableWindowNumber = 0xf965aa01.

I bumped onto JUCE and after a tweaking a thing or two, with the above code, the nControl was retrieved successfully and all messages were routed without any problems.

And that is half a day of my life I will never get back.

Thanks for the concern.

<h>Hope is the remainder of the of the number of times you thought you wouldn't make it divided by infinity. - e.m

Questionplease explain how it works Pin
ashwiny18-Feb-08 14:14
ashwiny18-Feb-08 14:14 
GeneralRe: please explain how it works Pin
Joe Woodbury18-Feb-08 16:07
professionalJoe Woodbury18-Feb-08 16:07 
QuestionRe: please explain how it works Pin
David Crow18-Feb-08 17:16
David Crow18-Feb-08 17:16 
GeneralBitmaps Painting Pin
Bram van Kampen18-Feb-08 13:21
Bram van Kampen18-Feb-08 13:21 
GeneralRe: Bitmaps Painting Pin
Sameerkumar Namdeo18-Feb-08 15:50
Sameerkumar Namdeo18-Feb-08 15:50 
AnswerRe: Bitmaps Painting Pin
Rajkumar R18-Feb-08 19:17
Rajkumar R18-Feb-08 19:17 
GeneralRe: Bitmaps Painting Pin
Hamid_RT18-Feb-08 19:43
Hamid_RT18-Feb-08 19:43 
AnswerRe: Bitmaps Painting Pin
Bram van Kampen20-Feb-08 13:57
Bram van Kampen20-Feb-08 13:57 
GeneralAn interesting problem Pin
tibiz18-Feb-08 12:07
tibiz18-Feb-08 12:07 
GeneralRe: An interesting problem Pin
BadKarma18-Feb-08 12:30
BadKarma18-Feb-08 12:30 
GeneralRe: An interesting problem Pin
Prasanth M V18-Feb-08 17:30
Prasanth M V18-Feb-08 17:30 
GeneralRe: An interesting problem Pin
Prasanth M V18-Feb-08 17:36
Prasanth M V18-Feb-08 17:36 
GeneralRe: An interesting problem Pin
tibiz18-Feb-08 23:48
tibiz18-Feb-08 23:48 
GeneralTcpClient in Visual C++ Pin
ashmanq18-Feb-08 11:27
ashmanq18-Feb-08 11:27 
GeneralRe: TcpClient in Visual C++ Pin
tibiz18-Feb-08 11:58
tibiz18-Feb-08 11:58 
GeneralCreating Services -- transitioning from interactive window app.... Pin
Peter Weyzen18-Feb-08 10:31
Peter Weyzen18-Feb-08 10:31 
GeneralRe: Creating Services -- transitioning from interactive window app.... Pin
Mark Salsbery18-Feb-08 10:35
Mark Salsbery18-Feb-08 10:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.