Resolved How to resolve EIntfCastError when calling function in an Active X DLL

MWENV

Member
Joined
Aug 4, 2022
Messages
10
Programming Experience
10+
I'm developing a C# class library which calls functions and procedures in an ActiveX DLL developed in Delphi. For discussion sake, I'll refer to the Delphi DLL as Delphi.dll and the C# class library as CSharp.dll. A reference to Delphi.dll has been added to the C# project.

Delphi.dll includes a TCollection class, functions Get_Count(), Get_Item(), and procedures Add(), Remove(). CSharp.dll is able to call Get_Count(), Add() and Remove() without any issues, however a call to Get_Item() raises the following error:
C#:
Type: EIntfCastError
Message: Interface not supported
Here's the declaration for Get_Item() in Delphi.dll:
C#:
function TCollection.Get_Item(Index: OleVariant): IDispatch;
begin
  Result := fList.Items[ConvertIndex(Index)] as IDispatch;
end;
Here's an example of CSharp.dll calling Get_Item():
C#:
aRecord = myCollection.GetItem(1)
Where myCollection is type (Delphi.dll).Collection and I've tried declaring aRecord as type dynamic, object and (Delphi.dll).Collection.

I believe the error is due to Get_Item() returning type IDispatch, which C# does not know how to handle.

What modifications do I need to make in C# to resolve this issue?
 
Last edited by a moderator:
Solution
The issue has been resolved after the following changes were made to the C# dll:
1.) set ComVisble to true in AssemblyInfo.cs
2.) Changed the Project\Build setting to Register for COM interop (checked the box)

Skydiver I do appreciate your feedback over the past couple of days.
Let me make sure that I understand correctly, then. You put a non-COM object that does not support IDispatch into a Delphi collection.

Then you ask that object to be pulled out from the Delphi collection as an object exposing an IDispatch interface? Or at least that is how I am reading this line:
C#:
Result := fList.Items[ConvertIndex(Index)] as IDispatch;

How is the IDispatch interface implementation for that object supposed to magically appear?

Let me make sure that I understand correctly, then. You put a non-COM object that does not support IDispatch into a Delphi collection.

Then you ask that object to be pulled out from the Delphi collection as an object exposing an IDispatch interface? Or at least that is how I am reading this line:
C#:
Result := fList.Items[ConvertIndex(Index)] as IDispatch;

How is the IDispatch interface implementation for that object supposed to magically appear?
Your understanding is correct. My expectation is the COM interop assembly (created when a project reference was added to the Delphi.dll) knows how to Marshall IDispatch and dynamic Types. Apparently that's not the case and why I am here ... looking for a solution.
 
The issue has been resolved after the following changes were made to the C# dll:
1.) set ComVisble to true in AssemblyInfo.cs
2.) Changed the Project\Build setting to Register for COM interop (checked the box)

Skydiver I do appreciate your feedback over the past couple of days.
 
Solution
You're welcome.

Glad you solved it!
 
Back
Top Bottom