How to instantiate a COM object using interop in Delphi Prism

943 views Asked by At

What is the correct syntax for instantiating a COM object in Delphi Prism using COM interop - new does not seem to do the job.

I've added it as a reference to the website project. Here is the relevant code:

method _Default.Button1_Click(sender: System.Object; e: System.EventArgs);
var
   FModel: MarketBuilderLib.MarketBuilderModel;
begin
  FModel := New MarketBuilderLib.MarketBuilderModel;
end;

Fails to compile with the message:

Error 1 
(PE190) "MarketBuilderLib.MarketBuilderModel" is an interface and cannot be 
instantiated

I understand the message but not sure how to do it. Many thanks for any help.

1

There are 1 answers

0
jamiei On BEST ANSWER

You can attempt to instantiate your COM object by using the CreateInstance method in the System.Activator class. The equivalent code might look like this:

var
  FModel: MarketBuilderLib.MarketBuilderModel;
begin
  FModel := (MarketBuilderLib.MarketBuilderModel)Activator.CreateInstance(GetTypeFromProgID("{PROG ID}"));
end;

Note that you will need to get the type from GetTypeFromProgID using the Program Identifier otherwise you will generate an InvalidComObjectException.