🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.delphi.language.delphi.dotnet
2 messages
2 total messages Started by Stephen Boyd Mon, 16 Jun 2008 13:44
Pointers & .Net & IntPtr
#28
Author: Stephen Boyd
Date: Mon, 16 Jun 2008 13:44
30 lines
1973 bytes
I came across a situation in the VCL component that I am porting where 
the original designer found it expedient to pass a pointer to a TObject 
as the LPARAM on a SendMessage call.

A lousy design and, in this case, completely unnecessary.  But it got me 
to thinking about pointers in .Net.  Generally a no-no, I know, but 
occasionally necessary if you are to use unmanaged code.

So I got to reading and experimenting.  Thinking that the IntPtr class 
was made for this I tried to convert the TObject reference to an integer 
so that I could try to pass it on the SendMessage call.  I couldn't make 
it work though.

Here is what I did:

var
   ip: IntPtr;
begin
   ip := IntPtr.Create(Self);
...

That gave me an error saying that there was no overloaded version of 
Create that matched the given parameters.  So, then I tried:

   ip := IntPtr.Create(Pointer(Self));

That gave me an invalid typecast error.

If neither of these worked, how do I use IntPtr in Delphi.Net?  Or is it 
just not possible to use TObject references in this way?
Re: Pointers & .Net & IntPtr
#29
Author: Marc Rohloff [TeamB]
Date: Mon, 16 Jun 2008 15:12
22 lines
1983 bytes
On Mon, 16 Jun 2008 09:44:00 -0400, Stephen Boyd wrote:

> So I got to reading and experimenting.  Thinking that the IntPtr class 
> was made for this I tried to convert the TObject reference to an integer 
> so that I could try to pass it on the SendMessage call.  I couldn't make 
> it work though.

The problem with converting an object reference to an IntPtr is that
you would hide the reference from .NET, this would mean that the
Garbage collector could either delete your object or move it somewhere
else.

I would fix the code. If you need an object reference create one
(although it doesn't make sense to me):
type
  TObjectReference = class
    Ref : TObject;
  end;
  
-- 
Marc Rohloff [TeamB]
marc -at- marc rohloff -dot- com
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads