Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2006-09

Find the nature of a pointer

7 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

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

Reply by email

Jonas Maebe
to Felipe Monteiro de Carvalho
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

Reply by email

Marco van de Voort
to Felipe Monteiro de Carvalho: I have a function that will receive a Handle. That handle could be a pointer to a…
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.

Reply by email

Mattias Gaertner
to Marco van de Voort
Show quoted text 11 lines
> 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

Reply by email

Marco van de Voort
to Mattias Gaertner
Show quoted text 15 lines
(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.

Reply by email

Felipe Monteiro de Carvalho
to Milan Marusinec
Milan Marusinec 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

Reply by email