In VC++ I have been able to redirect Window Messages (via Wndproc) to an instance of a class's specific message handler by storing the instance address with SetWindowLong. Found this detailed on the net somewhere. Worked Perfectly.
Does anyone know why the same idea doesnt work (as I have it) in Delphi?
At the moment, if I try to change a variable within the class the program just dies - no errors. It executes code with the class's message handler, but doesnt seem to like variable access.
Code Follows:
This is the main WndProc associated with the WndClass. As you can see, it retrieves a pointer with GetWindowLong, and if its not NULL it jumps to the class's own message handler, otherwise just passes it to DefWindowProc.
This part of the code seems to execute quite happily. In another part of the code is a SetWindowLong which stores a pointer to 'self'.
function WndProc(hWnd: HWND; uMsg: UINT; wParam: WPARAM; lParam: LPARAM):LRESULT; stdcall;
var
wndobj: pGLEngine;
begin
wndobj:= pGLEngine(ptr(GetWindowLong(hWnd, GWL_USERDATA)));
if wndobj = nil then
result:= DefWindowProc(hWnd, uMsg, wParam, lParam)
else
result:= wndobj.MsgHandler(hWnd, uMsg, wParam, lParam)
end;
This is the class's 'internal' message handler. The problem occurs within the function. All the code seems to execute, as long as I don't touch a class variable. The case WM_ACTIVATE causes an error when it trys to set Active:=....
Active is declared in the private section of the GLEngine class. If I {..} that line out, its fine. If I add something trivial like 'Active:= true', the program dies without error. I have tested other variables as well that are within the class - all cause the program to die.
function GLEngine.MsgHandler(hWnd: HWND; uMsg: UINT; wParam: WPARAM; lParam: LPARAM):LRESULT;
begin
case uMsg of
WM_ACTIVATE:
begin
if (hi(wParam)=0) then active:= true else active:= false;
result:= 0;
exit;
end;
WM_SYSCOMMAND:
begin
if (wParam = SC_SCREENSAVE) or (wParam = SC_MONITORPOWER) then
begin
result:= 0;
exit;
end;
end;
...
...
end;
Is there a trick to delphi's classes when it comes to variables?
[Edited by - Sentient2k5 on July 4, 2006 3:02:59 PM]