C Board  

Go Back   C Board > Platform Specific Boards > Windows Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 04-10-2004, 12:53 PM   #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);
}
jimmy_anttila is offline   Reply With Quote
Old 04-10-2004, 03:03 PM   #2
erstwhile
 
Join Date: Jan 2002
Posts: 2,223
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.
Ken Fitlike is offline   Reply With Quote
Old 04-11-2004, 03:10 AM   #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 .
jimmy_anttila is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 03:48 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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