2
votes

I have played with a third-party library that is accessed with a COM interface.
For some tests, I have created the main form as:

TForm1 = class (TForm, IThirdParyCOMInterface)

TForm1 implements all methods of IThirdParyCOMInterface, and I get the functionality that I need.

The next step is to create a separate class only for dealing with this COM-Interface.
Doing something like this:

TMyClass = class (TInterfacedObject, IThirdParyCOMInterface)

results in compiler errors:

E2291 Missing implementation of interface method IDispatch.GetTypeInfoCount
E2291 Missing implementation of interface method IDispatch.GetTypeInfo
E2291 Missing implementation of interface method IDispatch.GetIDsOfNames
E2291 Missing implementation of interface method IDispatch.Invoke

( IThirdPartyCOMInterface = interface (IDispatch) )
It seems that TForm already implements these IDispatch methods, but my new simple class doesn't.

How can I do this for a new class like the above?
From which class should I inherit my new class, so that these IDispatch methods are already implemented?

EDIT:
I just found TAutoIntfObject. Is that what I need?
(COM is a bit confusing to me, and I am just guessing here)

2

2 Answers

5
votes

The traditional thing to do in COM, especially if you're writing an EXE process that functions as an Automation Server (aka DCOM Server) is to separate your COM interfaces from your forms.

Heck it's traditional to do that even when you're writing normal Delphi applications that you don't want to turn into a Giant ball of Mud. So ask yourself WHY you want to make the FORM implement your dispatch interfaces. even in a quick and dirty demo, conflating COM Server classes and Forms is just fun you don't need to be having.

Dual interface (Automation) objects are typically built as their own class, which may privately have a reference to your form. I recommend you check out doing it that way.

IThirdPartyComInterface should be implemented by your own class TThirdPartyComInterface, and should be a dual-COM (Native vtable + Dispatch) object.

You should then register your COM server object, and you will be in business.

There are various tutorials but none seem up to date. here's one for Delphi 7.

Perhaps the best place to go is the DocWiki which will show you the Com object wizard, which you should be using to create your TThirdPartyComInterface class which will implement your IDispatch, plus native VTable COM (higher performance) all at once. You could hack IDispatch into your TForm, but every time you do that, a sub-deity or archon of my choice executes a small furry animal of my choice. Don't do it. Think of all those small furry animals you're killing.

2
votes

Try using TAutoObject instead of TInterfacedObject. The former implements IDispatch, the latter only implements IUnknown. (It's in the ComObj unit)