IDispatch parameters passed from Delphi to C#
I am using Delphi 2010 to access objects stored in a C# Assembly written in Visual Studio 2008 using the techniques described in the Hosting CLR in Delphi. These techniques do work and I am able to obtain an instance of a .NET class - via an interface - and call its methods and read it properties. Calling the methods works fine as long as long as the parameters are simple parameters, such as strings, integers, chars, and booleans. But if I try to pass a parameter that is an IDispatch reference, the C#/.NET code only sees the parameter as a null value.
The one difference from the Hosting CLR in Delphi example is that I do not define the interfaces in both Delphi and C# by hand. Instead, I write the interface type library as a RIDL file in Delphi 2010. Then I compile that RIDL file into a TLB file, then use Microsoft's TLBIMP.EXE to build a COM/Interop DLL from the DLL, and then I add a reference to that DLL in the C# project. I use this approach for defining the interfaces of the COM objects that I use for both the interface usage (as described in Hosting CLR in Delphi) and as for defining the interface of the IDispatch interface that I pass as a parameter.
Here is the interface that I use from Delphi and the one that the C# classes that I use implement (.NET Metadata):
[TypeLibType(4160)]
[Guid("C4D342E4-62A0-4049-BF1E-9F2A6EE19E5E")]
public interface ITestInterface
{
[DispId(203)]
int Run(object Test);
}
Delphi can startup the CLR, then instantiate a C# object that implements ITestInterface. Delphi can call Run and I can verify that Run executes properly. Return values are as expected and if I pass parameters other than "object Test" (rewrite the interface), then those parameters are properly seen. But when I pass a reference to an IDispatch object for the Test interface, the C# code sees it only as NULL. Trying to cast the parameter using "as" to the right interface doesn't fail - but it results in a NULL because the initial value seems to be NULL.
I have double checked and the value is not NULL when it leaves Delphi.
I'm guessing that there is some sort of extra m开发者_如何学编程arshalling step that I have to perform on the C# side to properly obtain an interface supported by the IDispatch object.
Any thoughts, suggestion would be greatly appreciated.
The CLR won't know what to do with a raw interface pointer and no type info. You'll need to pass a VARIANT of type VT_DISPATCH. That will map to a __ComObject on the C# side, late binding is required to make calls on that reference.
精彩评论