Articles   Members Online: 3
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to get the TObject from an Interface Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
14-Nov-02
Category
OO-related
Language
Delphi 3.x
Views
99
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Tomas Rutkauskas

I'm trying to find a way to get the TObject reference from an IInterface object. I 
know that if the interface loses the reference the object will destroy but I've got 
a special need. The environment is based on interfaces but there is one component 
that can't use interfaces. So to work around it I assign the interface to a local 
variable, convert the interface to an object, call the component and when all is 
done get rid of the interface variable. Is this the right approach or can anything 
go wrong?

Answer:
1   
2   function GetImplementingObject(const I: IInterface): TObject;
3   const
4     AddByte = $04244483; {opcode for ADD DWORD PTR [ESP+4], Shortint}
5     AddLong = $04244481; {opcode for ADD DWORD PTR [ESP+4], Longint}
6   type
7     PAdjustSelfThunk = ^TAdjustSelfThunk;
8     TAdjustSelfThunk = packed record
9       case AddInstruction: longint of
10        AddByte: (AdjustmentByte: shortint);
11        AddLong: (AdjustmentLong: longint);
12    end;
13    PInterfaceMT = ^TInterfaceMT;
14    TInterfaceMT = packed record
15      QueryInterfaceThunk: PAdjustSelfThunk;
16    end;
17    TInterfaceRef = ^PInterfaceMT;
18  var
19    QueryInterfaceThunk: PAdjustSelfThunk;
20  begin
21    Result := Pointer(I);
22    if Assigned(Result) then
23    try
24      QueryInterfaceThunk := TInterfaceRef(I)^.QueryInterfaceThunk;
25      case QueryInterfaceThunk.AddInstruction of
26        AddByte: Inc(PChar(Result), QueryInterfaceThunk.AdjustmentByte);
27        AddLong: Inc(PChar(Result), QueryInterfaceThunk.AdjustmentLong);
28      else
29        Result := nil;
30      end;
31    except
32      Result := nil;
33    end;
34  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC