Results 1 to 12 of 12

Thread: this pointer to access

  1. #1

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500

    this pointer to access

    in my window creation class, i have a static WndProc which will be used for adding events. i read here http://www.mvps.org/windev/cpp/class.html that by passing the this pointer to the lParam of CreateWindow, i can then retrieve that pointer when i handle WM_CREATE in my WndProc, and store it.. after i can cast it back to the class, and use the pointer to access and modify members...the problem is that everytime i try to actually modify a member, the program crashes..could anybody help me?
    thanks

    Amon Ra
    Amon Ra
    The Power of Learning.

  2. #2

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500
    ok so here is my wndproc, which is a static member of the WINDOW class

    PHP Code:
    //WindowProc: this is the real one...the one that does all the work
    LRESULT CALLBACK WINDOW::WindowProc (HWND hWndUINT MsgWPARAM wParamLPARAM lParam)
    {
        
    PAINTSTRUCT ps;
        
    WINDOW *const ptThis = (WINDOW*) lParam;    //store "this" in variable;
        
        
    switch (Msg)
        {
        case 
    WM_CREATE:
            
    SetWindowLong (hWndGWL_USERDATA, (LONGlParam);    //stores "this" on window data just in case
                
    return 0;
        case 
    WM_LBUTTONDOWN:
            
    MessageBox (hWndptThis->m_pcText "BLAH"MB_OK MB_ICONINFORMATION);
                return 
    0;
        case 
    WM_PAINT:            // this must be handled
            
    BeginPaint(hWnd, &ps);
            
    EndPaint(hWnd, &ps);
                return 
    0;
        case 
    WM_DESTROY:            // must also be handled in the main window
            
    PostQuitMessage(0);
                return 
    0;
        }
            return    
    DefWindowProc (hWndMsgwParamlParam);
    }    
    //WindowProc 
    to have the "this" pointer stored in the lParam, i used:

    PHP Code:
            hWnd CreateWindowEx (    m_dwExStyle,
                                                    
    m_pcClassName,
                                                    
    m_pcText,
                                                    
    m_dwStyle,
                                                    
    m_XPosm_YPosm_Widthm_Height,
                                                    
    m_hParent,
                                                    
    NULL,
                                                    
    m_hInstance,
                                                    
    this    ); 
    The problem is that when i get to
    PHP Code:
    MessageBox (hWndptThis->m_pcText "BLAH"MB_OK MB_ICONINFORMATION); 
    it crashes the program. anyone have any idea? thanks in advance

    Amon Ra
    Amon Ra
    The Power of Learning.

  3. #3

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500
    any body please? basically i want a class with a windowproc in it, so that the user has no acces to it
    Amon Ra
    The Power of Learning.

  4. #4

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500
    great! i got it..after adjusting a few things.. =)
    Amon Ra
    The Power of Learning.

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Not sure if you already have the solution...
    the lParam passed to the WM_CREATE message is not the lParam you passed to CreateWindow, but rather the address of a CREATESTRUCT.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500
    yea exactly..i found that out in the msdn... i did this

    PHP Code:
    pThis = (WINDOW*) ((LPCREATESTRUCT)lParam)->lpCreateParams
    and i passed the "this" to the lParam of CreateWindow
    Amon Ra
    The Power of Learning.

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    You didn't notice the code I posted ages ago about this then?

    Everyone and his dog seems to be writing window-wrapper classes at the moment, strange really.

    I want .NET *sigh* Was so much easier to use
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  8. #8

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500
    lol..no i didn't see it..i ma writing this for the info tech class at our school (high school)... =)
    Amon Ra
    The Power of Learning.

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    The problem of passing the class as create parameter and saving it in a static variable of the WndProc is that every new class will replace the last! Keep that in mind! (someone here did it that way)
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Use a method like the SetWindowLong(hWnd, GWL_USERDATA, this) one.

    I've posted about that one a LOT now...so I won't repeat it
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  11. #11

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500
    ok apparently, as you said CornedBee, when everything is created , and i use the pointer to this, i get an error..
    i am gonna see how i can fix, and try to find yours Parksie..
    thanks =

    Amon Ra
    Amon Ra
    The Power of Learning.

  12. #12

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500
    i fixed it and it works beautifully now

    Amon Ra
    Amon Ra
    The Power of Learning.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width