We help IT Professionals succeed at work.

Inherit COM in Delphi?

foxvision
foxvision asked
on
Medium Priority
640 Views
Last Modified: 2012-06-27
Dear experts,

Using VB, I could do a new ActiveX DLL, import any COM objects, then I could start to write codes which actually overrite existing methods in the COM, like this:

Option Explicit

Implements Desktop.IProteanTool
'This function is activated from protean UI
Private Sub IProteanTool_Launch(ByVal aToolContext As Desktop.IProteanToolContext)
    Set aToolContext = Nothing
    frmContractCreditLimitEntry.Show 1
End Sub

With this, I override what is originally in IProteanTool_Launch and show my own form.

How can I do this in Delphi? When I imported the COM as TLB type library, all I can see is:

// *********************************************************************//
// DispIntf:  IProteanTool
// Flags:     (4096) Dispatchable
// GUID:      {9AF5DC81-2498-11D1-8C31-0000C0328CEF}
// *********************************************************************//
  IProteanTool = dispinterface
    ['{9AF5DC81-2498-11D1-8C31-0000C0328CEF}']
    procedure Launch(const aToolContext: IProteanToolContext); dispid 1;
  end;

Please advise. Thanks.

Regards,
Fox
Comment
Watch Question

Commented:
Create/Open an ActiveX library/exe.
Make a new COM object named ProteanToolEx.
The COM object should implement the IProteanTool interface.
In the Type Library Editor, add methods to the IProteanToolEx interface.
Save everything (Ctrl+Shift+S) and go to the code editor.
Go to the new unit that contains the implementation of TProteanToolEx and write the code for the Launch method and the other methods you added.

If you want you new object to use another ProteanTool's Launch method, simply declare a private variable of the given object type. Let's say there's a class in the Protean Tool type library called CoProteanTool.
Your implementation of TProteanToolEx would look like:

TProteanToolEx = class(TComObject, IProteanTool, IProteanToolEx.....)
private
  FProteanTool: IProteanTool;
protected
  procedure Launch(const aToolContext: IProteanToolContext); safecall;
  //your new methods would be here too
public
  //the private object must be initialized and destroyed
  procedure Initialize; override;
  destructor Destroy; override;
end;

further down in the unit, you'd write your Launch procedure like so:

procedure TProteanToolEx.Launch(const aToolContext: IProteanToolContext);
begin
  FProteanTool.Launch(aToolContext);
end;

//don't forget the init & destruction methods
procedure TProteanToolEx.Initialize;
begin
  inherited Initialize;
  FProteanTool := CoProteanTool.Create;
end;

destructor TProteanToolEx.Destroy;
begin
  FProteanTool := nil;
  inherited;
end;

That's all there is to it.

Author

Commented:
Dear Rondi,

Thanks for your guidance. Let me experience the codes above and I will get back to you very soon.

Regards,
Fox

Author

Commented:
Dear Rondi,

I tried the code yesterday and there are a few points which I don't quite understand.

When you said:

Make a new COM object named ProteanToolEx.
The COM object should implement the IProteanTool interface.

What does the above mean? How should the new COM object ProteanToolEx implement IProteanTool interface?

As I indicated in the 1st post, IProteanTool is a dispinterface.

2nd point:

You said:

TProteanToolEx = class(TComObject, IProteanTool, IProteanToolEx.....)
private
 FProteanTool: IProteanTool;
protected

I got a compilation error saying that IProteanTool is not an interface and it is expecting an interface. Again, the code

CoProteanTool.Create

simply not possible because there is no CoProteanTool as it is a dispinterface.

Hmm?!

Regards,
Fox

Commented:
Hey Fox,

I assumed the code you posted the first time was just
a small part of the Type Library import unit. If that's all there was, then u got a problemo!
You're gonna need to import the ProteanTool type library
again, so you get the full unit with interfaces, dispinterfaces and CoClasses. You won't be able to do anything with just dispinterfaces.

So, start a new ActiveX Library DLL project. Import the protean type library. Go to File -> New -> Other -> [ActiveX] COM object. In the COM object wizard, add the IProteanTool interface to the new object's implemented interfaces (you may have to browse for the type lib first). Hit OK, and you're ready to go.
Go to the new unit and write code for the launch procedure.

You can add a new form to your project and show it from your new COM object's Launch procedure.

rondi.

Author

Commented:
Dear Rondi,

I think I still can't did what you said because I look through the entire TLB file and ProteanTool is a DispInterface.

Your answer let me learn some new things in COM as well and you deserve the points.

Though I just marvel at VB that it has the capability to do what I want so easily. I always believe that whatever can be done in VB is surely no problem in Delphi, well, I guess I am wrong this time, ha.

Regards,
Fox

Explore More ContentExplore courses, solutions, and other research materials related to this topic.