![]() |
| | #1 |
| Registered User Join Date: Apr 2004
Posts: 7
| Strange access violation 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 | |
| | #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 | |
| | #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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |