Pointers, casting, SetGetWindowLong problem...

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Fri Mar 9 15:33:09 PST 2007


Chris Warwick wrote:
> 
>     TWindow window = cast(TWindow) cast(void*)  GetWindowLongA(handle, 0);
>     if (window == null)  return DefWindowProcA(handle, msg, wparam, lparam);
> 
[snip]
> 
> I cant work out why, even if i have fecked up the casting or setting of the
> the windowLong var, testing what was returned against null shouldnt cause an
> AV should it?

Actually, the test is *exactly* what's causing this.
Comparing class references (including null, if the type is a class type) 
is done by calling a member function on the left operand with the right 
operand as a parameter (in this case, window.opEquals(null)). To call 
opEquals, the vtable pointer needs to be found, which produces an access 
violation for null references (since it needs to dereference the 
reference to get at the vtable pointer).

What you want to do is not test equality, but testing identity. For 
that, replace '==' by 'is':
---
     TWindow window = /* whatever */;
     if (window is null)  /* something */;
---
should do what you want.


More information about the Digitalmars-d-learn mailing list