views:

168

answers:

1

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.

+3  A: 

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

related questions