1

I am using Indy9 with Delphi7. I would like to cast an Longword (Athread.Handle) back to an TIDPEERTHREAD Pointer. Is there a way how I could do this? Or is there any other way I could "store" the Pointer to a Longword?

Thank you in Advance.

Ben
  • 3,380
  • 2
  • 44
  • 98

1 Answers1

2

TIdPeerThread is a TThread descendant. Its Handle property contains the OS-level thread handle from CreateThread(). There is no way to cast a Handle value directly to a TIdPeerThread object pointer. You will have to either:

1) Store the TIdPeerThread object pointer itself in the LongWord instead of the TIdPeerThread.Handle value, and then cast it back when needed:

var
  LW: LongWord;
  Peer: TIdPeerThread;

Peer := ...;
LW := LongWord(Peer);
...
Peer := TIdPeerThread(LW);

2) Store the TIdPeerThead.Handle value in the LongWord, then loop through the TIdTCPServer.Threads list looking for a TIdPeerThread object that has a matching value when needed:

var
  LW: LongWord;
  Peer: TIdPeerThread;
  List: TList;
  I: Integer;

Peer := ...;
LW := LongWord(Peer.Handle);
...
Peer := nil; 
List := IdTCPServer1.Threads.LockList;
try
  for I := 0 to List.Count-1 do
  begin
    if LongWord(TIdPeerThread(List[I]).Handle) = LW then
    begin
      Peer := TIdPeerThread(List[I]);
      Break;
    end;
  end;
finally
  IdTCPServer1.Threads.UnlockList;
end;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Thanks for the answer. Could you show me a small example how I could store TIdPeerThread into a Longword and cast it back? Thanks in advance. – Ben Apr 27 '12 at 20:09
  • 2
    I have updated my answer, but did I really need to show you how to typecast a pointer to an integer and back? That is Delphi 101 programming (OK maybe 102). – Remy Lebeau Apr 28 '12 at 01:29