Hello Felipe,
I have a function that will receive a Handle. That handle could be a
pointer to a structure, or a object. Is there any safe way to discover
it's nature?
What you want is possible with a little adjustment of the classes
you want to identify at runtime.
Look at the included example I am sending.
Does it solves your problem ?
Milano
{$APPTYPE CONSOLE}
program
PointerNature ;
uses
SysUtils ;
const
CLASS_CHECK_STRING = 'This is some TSomeClass.';
type
TSomeClass = class
constructor Create;
procedure AbstractException; virtual; abstract;
function IsSomeClass : string; virtual;
end;
constructor TSomeClass.Create;
begin
end;
function TSomeClass.IsSomeClass;
begin
result:=CLASS_CHECK_STRING;
end;
function IsClassOfTSomeClass(x : pointer ) : boolean;
begin
result:=false;
if x <> NIL then
try
TSomeClass(x ).AbstractException;
except
on E:EAbstractError do
try
if TSomeClass(x ).IsSomeClass = CLASS_CHECK_STRING then
result:=true;
except
end;
else
end;
end;
var
TestClass : TSomeClass;
BEGIN
TestClass:=TSomeClass.Create;
if IsClassOfTSomeClass(pointer(1234 ) ) then
writeln('12345 is a class of TSomeClass :-)' )
else
writeln('12345 is NOT a class of TSomeClass !' );
if IsClassOfTSomeClass(TestClass ) then
writeln('TestClass is a class of TSomeClass :-)' )
else
writeln('TestClass is NOT a class of TSomeClass !' );
END.
_______________________________________________
fpc-pascal maillist - fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal