Thread: Strange access violation

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

    Strange access violation

    Hi,

    I have put the windows procedure inside a class as a static member function and I get a pointer to the class containing the function using GetWindowLong(), and everything works fine, like calling member functions trough the pointer and reading variables through the pointer, but when I try to write through the pointer, I get an Access violation writing location... Does anyone know why?

    the code:
    LRESULT CALLBACK Win32Window::WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    // Get a pointer to the class which this window belongs to
    Win32Window *w32Window;
    if (msg != WM_CREATE)
    w32Window = (Win32Window*)GetWindowLongPtr( hWnd, 0 );

    switch (msg)
    {
    case WM_CREATE:
    {
    CREATESTRUCT *cs = (CREATESTRUCT*)lParam;
    w32Window = (Win32Window*)cs->lpCreateParams;
    SetWindowLongPtr(hWnd, GWL_USERDATA, (LONG_PTR) w32Window);
    return 0;
    break;
    }

    case WM_KEYDOWN:
    {
    w32Window->aMemberVariable = 0; // this causes a Access violation writing...
    break;
    }


    }

    return DefWindowProc(hWnd, msg, wParam, lParam);
    }

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Please use code tags

    If you use 'GWL_USERDATA' to store your c++ class pointer with SetWindowLongPtr then use the same flag to retrieve it with GetWindowLongPtr.

    Also note that WM_CREATE is not the first message sent to your window procedure but WM_CREATE is where you first store your c++ class pointer in your window (most use WM_NCCREATE in this respect because it's sent to your window procedure before WM_CREATE). Checking the return value of GetWindowLongPtr is probably a good idea, too, to ensure that the GWL_USERDATA returned is valid.

    This is the most recent of a long line of threads on c++ class message handling which may be of some interest to you.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    7
    Thanks, using GWL_USERDATA when retrieving the pointer did the trick, I should have thought about that. Code tags will be used in my next post .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Istream::Release access violation? [C++]
    By A10 in forum Windows Programming
    Replies: 10
    Last Post: 01-13-2009, 10:56 PM
  2. Access violation... can't figure it out...
    By Raigne in forum C++ Programming
    Replies: 7
    Last Post: 10-11-2007, 10:52 AM
  3. FtpFileFind access violation with MS VC++ 6.0
    By earth_angel in forum C++ Programming
    Replies: 3
    Last Post: 09-22-2005, 07:02 PM
  4. Help! CListCtrl access violation
    By bonkey in forum Windows Programming
    Replies: 4
    Last Post: 11-18-2003, 02:40 PM
  5. 0xC0000005: Access Violation
    By Strider in forum Windows Programming
    Replies: 3
    Last Post: 11-07-2001, 02:46 PM