0

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.

Jim McKeeth
  • 38,225
  • 23
  • 120
  • 194
Alan Clark
  • 2,017
  • 21
  • 28

1 Answers1

4

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.

jamiei
  • 2,006
  • 3
  • 20
  • 28