Find the nature of a pointer

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Find the nature of a pointer

Felipe Monteiro de Carvalho
Hello,

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?

The following code will work when the Handle is an Object or nil, but
will crash the application when the Handle is a structure:

function TQtWidgetSet.IsValidGDIObject(const GDIObject: HGDIOBJ): Boolean;
var
  aObject: TObject;
begin
  Result := False;

  if GDIObject = 0 then Exit;

  aObject := TObject(GDIObject);

  if aObject is TObject then
  begin
    Result := (aObject is TQtFont) or (aObject is TQtBrush);
  end;
end;


thanks,
--
Felipe Monteiro de Carvalho
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Find the nature of a pointer

Jonas Maebe-2

On 1 sep 2006, at 19:54, Felipe Monteiro de Carvalho wrote:

> 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?

No, if only because there is no generic way to distinguish a random  
record which just looks like an object (because it happens to contain  
the right bytes at the right place) and a genuine object (or anything  
else for that matter).

The only way would be to make sure all objects are allocated from a  
separate memory pool, and checking whether the passed address lies in  
this pool.


Jonas
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Find the nature of a pointer

Marco van de Voort
In reply to this post by Felipe Monteiro de Carvalho
> 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?

No you can't.

> The following code will work when the Handle is an Object or nil, but
> will crash the application when the Handle is a structure:

It is not even 100% save for that, some systems use other values instead of
nil/0.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Find the nature of a pointer

Mattias Gaertner
On Fri, 1 Sep 2006 20:08:30 +0200 (CEST)
[hidden email] (Marco van de Voort) wrote:

> > 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?
>
> No you can't.
>
> > The following code will work when the Handle is an Object or nil,
> > but will crash the application when the Handle is a structure:
>
> It is not even 100% save for that, some systems use other values
> instead of nil/0.

For example?

Mattias
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Find the nature of a pointer

Marco van de Voort
> [hidden email] (Marco van de Voort) wrote:
>
> > > 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?
> >
> > No you can't.
> >
> > > The following code will work when the Handle is an Object or nil,
> > > but will crash the application when the Handle is a structure:
> >
> > It is not even 100% save for that, some systems use other values
> > instead of nil/0.
>
> For example?

Admitted, the examples I know are not-protected mode systems with rom or a
zp on address 0.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Find the nature of a pointer

Milan Marusinec
In reply to this post by Felipe Monteiro de Carvalho
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  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Re: Find the nature of a pointer

Felipe Monteiro de Carvalho
On 9/4/06, Milan Marusinec <[hidden email]> wrote:
> 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 ?

Thanks, seams a very interresting way to solve the problem

But I already solved my problem getting rid of the records. I made all
Handles that need to be passed to that function become Objects, then I
can just use the is operator to find out which class it belongs to.

--
Felipe Monteiro de Carvalho
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal