IDispatch parameters passed from Delphi to C# - 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 marshalling 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.

Related

.net types replaced by IUnknown in type library

When I import tlb file that is specific to a .net dll, into Delphi using type library importer, the methods which accept parameters of type .net specific, are replaced by IUnknown.
When I want to invoke such method from my Delphi client application, I would like to pass a parameter value of type SQLTransaction. How do I achieve this? Do I need to change all the .net dll method parameters to user defined types that inherit from .net specific types?
I also have mscorlib_TLB.pas imported when I improted .net tlb.
.net method
public class MyConnection : IDisposable
{
public int BulkInsert(SqlTransaction tran);
{...}
}
Method in Delphi imported tlb:
_MyConnection = interface(IDispatch)
['{9FB088F8-1033-3A99-B9C6-C7D7D2D40140}']
function BulkInsert(const SQLTransaction: IUnknown): int; safecall;
end;
CoMyConnection = class
class function Create: _MyConnection;
class function CreateRemote(const MachineName: string): _MyConnection;
end;
how do I call the BulkInsert method from my Delphi client application?
That is entirely to be expected. The SqlTransaction type is a .net type, unknown to your COM interface. How can you expect some other party, a Delphi program in this case, to be able to obtain an instance of the .net SqlTransaction type.
Your COM interface needs to restrict itself to use only COM types. You will need to replace SqlTransaction with a COM interface. One way to approach the problem is to declare a COM interface, that exposes the necessary functionality of SqlTransaction. Then in your C# Code create a type that implements this interface by delegating the implementation to an instance of SqlTransaction. Then expose a method that allows instantiation of your wrapper interface. On the Delphi side the consumer will instantiate your wrapper interface and pass that to BulkInsert. Naturally you'll change BulkInsert to accept as its parameter the wrapper interface rather than SqlTransaction.
maybe you should first obtain the Interop proxy objects, usually importing a type library in a visual studio project it is done automatically by the ide, I think that Delphy ought to implement the same feature, but I don't sure..

Passing List<String> from C# to native COM interface

I'm doing some prototyping, plugging into a big legacy C++ application. I'm using C# to do this as a proof of concept (eventually I will write the library in C++). I've setup a COM interface between my C++ application and a .net class library and am able to pass strings and ints between. They get automatically marshalled as appropriate (e.g. a managed string goes to BSTR*).
I now have added a new method to my COM interface which returns a List<String>. I figured the method type generated would return an array of BSTR* (BSTR**) and a count a SAFEARRAY, however when I look at the IDL (using oleview), this interface method doesn't even appear. I'm guessing the COM framework isn't sure what a List<String> looks like, so what are my options here? Just using string[]?
I'm not using C++/CX, and I cannot.
Thanks

Using DLLs in VBScript

I've compiled C# code into a DLL, but have little experience with them. My C# code contains a class HelloWorld with a static method Print(). I'd like to use this DLL in VBScript to call the method Print(). I know this is base, but I'm using this as a test for a larger scale project that will be compiled to DLL in the end. What's the declare look like for that and how would the method call look?
Important: Both methods will work only if the DLL exposes a COM interface.
If your dll is registered with the system, use CreateObject with it's ProgID.
Set myObject = CreateObject("MyReallyCoolObject.HelloWorld")
myObject.Print
If your object is not registered on the system, use GetObject with a path to the file containing your object. Make sure your object exposes the proper interface. (The second parameter is optional. Here you can provide a class name if your object exposes more than one.)
Set myObject = GetObject("C:\some\path\helloworld.dll", "appname.HelloWorld")
myObject.Print
I think you might be looking for Registration-Free COM. This SO answer regarding the Microsoft.Windows.ActCtx should help specifically for VBScript.
Keep in mind that COM doesn't support static methods, so you'll have to make your Print method into an instance method.
How to call a .NET DLL from a VBScript

Adding an in-proc COM Server object to a C# application

I am new to COM and need to add a Server COM object to my c# application so I can call its methods and implement events. The documentation I have says this requires a COM-aware language such as Visual C++. My app is written in C# so I'm not sure how this is going to work. Any direction would be appreciated.
I am writing an app that communicates with a serial hypercom terminal. The device came with a DLL (the com server interface) that I will need to figure out how to use in my c# application.
I added a reference to the DLL to my project, which shows up as a type library. In object explorer I get interfaces, and some classes etc.
Thanks,
Matt
You can add the COM object as a reference. .NET will create an interop assembly to work with the COM object, just like it was a .NET type.
CComObjectRoot can be used as a base class for all COM objects implemented with ATL. As such you don't have to worry to implement something similar in C#, the required methods (AddRef, Release, QueryInterface) will be generated by tlbexp for classes that are tagged with ClassInterface.
STDMETHODIMP is a define which serves to declare the most common methods that can be called from COM (#define STDMETHODIMP HRESULT STDMETHODCALLTYPE). Again if your class is tagged with ClassInterface you will not have to worry about.
Such construction is required in C++ when your class implements several interfaces. I think this is not required if you tell C# compiler that your C# object implement IDispatch, IFPESOlementationEvents. The appropriate code will be written automatically by the compiler.
Probably everything will not make much sense if you are new to COM and C#, I'll suggest to take a look at the various introduction that you may find on the web, like this.

Using a COM dll from C# without a type library

I need to use a COM component (a dll) developed in Delphi ages ago. The problem is: the dll does not contain a type library... and every interop feature (eg. TlbImp) in .NET seem to rely on TLBs. The component has been used in Delphi programs here for many years without problems because "It's not much of a problem using COM objects from Delphi, because we know the interfaces" (quote Delphi developer).
Is there any way I can use this DLL from c# without a TLB? I've tried using the DLL as unmanaged, but the only method it exports are DllUnregisterServer, DllRegisterServer, DllCanUnloadNow and DllGetClassObject. I know the names of the classes and functions I'm going to use, if that can be of any help.
UPDATE:
I've tried implementing Jeff's suggestion, but I'm getting this error:
"Unable to cast COM object of type 'ComTest.ResSrvDll' to interface type 'ComTest.IResSrvDll'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{75400500-939F-11D4-9E44-0050040CE72C}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE))."
This is what I've done:
I got this interface definition from one of the Delphi-guys:
unit ResSrvDllIf;
interface
type
IResSrvDll = interface
['{75400500-939F-11D4-9E44-0050040CE72C}']
procedure clearAll;
function ResObjOpen(const aClientID: WideString; const aClientSubID: WideString;
const aResFileName: WideString; aResShared: Integer): Integer; {safecall;}
...
end;
implementation
end.
From this I've made this interface
using System.Runtime.InteropServices;
namespace ComTest
{
[ComImport]
[Guid("75400500-939F-11D4-9E44-0050040CE72C")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IResSrvDll
{
int ResObjOpen(string aClientID, string aClientSubID, string aResFileName, int aResShared);
}
}
And this coclass (got the guid from the delphi-guys)
using System.Runtime.InteropServices;
namespace ComTest
{
[ComImport]
[Guid("75400503-939F-11D4-9E44-0050040CE72C")]
public class ResSrvDll
{
}
}
UPDATE
The solution from Jeff is the way to do it. It is worth noticing, though, that the interface definition must match the COM-components exactly! ie. same order, same names, etc.
You just need the CLS_ID and interface id. I wrote about this specific issue on my blog:
"Using Obscure Windows COM APIs in .NET"
Write a wrapper in VB.Net. VB.Net supports true late binding (no messy reflection). All you need is the progId. You should also implement IDisposable to explicitely manage the component lifecycle.
It is quite frequent that you will encounter an interface implementation that is not backed by a type library (Delphi or otherwise). Shell extensions are one example.
You basically need to make a Windows API call to create the instance through the proper COM function calls. The API will take care of managing the DLL via the exported functions you mentioned earlier.
You will need to recreate the interface definition in C# code but after that you simply create the object, cast it to the interface, and it is no different than anything else. The only real caveat here is, depending on your usage, you may have some threading issues to deal with so check the "threading model" that was used for the DLL and consider your usage based on that.
Here is a link to a tutorial on consuming the interfaces that are not TLB based.
Tutorial
If you've managed to create an instance of the object, you're over the first major hurdle!
Now try this:
myObject.GetType().InvokeMember(
"ResObjOpen", // method name goes here
BindingFlags.InvokeMethod,
null,
myObject,
new object[] {
someClientID, // arguments go here
someSubId,
somFileName,
someInt} );
The reason I think you may need to do this is if the Delphi COM object is not a "dual" object. It may only support late binding, i.e. the kind of invocation you see above.
(In C# 4.0 they're making this easier with the dynamic keyword.)
EDIT: Just noticed something very suspicious. The IID for the interface and the CLSID for the object itself appear to be the same. That's not right.
Given that you've succeeded in creating the object, it would appear to be the CLSID of the object. So it's not the right IID. You need to go back to your Delphi folks and ask them to tell you what the IID of the interface IResSrvDll is.
Edit again: You could try changing the enum member you specify from ComInterfaceType. There should be ones for IDispatch and "dual" - although as your object doesn't support IDispatch, neither of those should be the right choice. The IUnknown setting (which appears in your sample code) should work - suggesting that the IID is wrong.
Yes and no.
All C# (and any CLR language) needs in order communicate with a COM object is a compatible interface signature. Typically specifying the methods, GUID and apartment style of the interface. If you can add this definition into your code base then the TLB is not necessary.
There is a small caveat that comes with that statement. I believe you will get into trouble if you try and use a COM object across apartment boundaries and don't have a suitable TLB registered. I can't 100% remember on this point though.
You can also do late binding and then invoke methods through reflection (myObject.InvokeMember("NameOfTheMethod", options, params, etc.)).
A wrapper should, however, offer better performance and faster marshalling.
I suspect that the dynamic keyword (C# 4.0) will accomplish this. If it does, it will give results which are largely equivalent to invoking the methods, i.e. how Groo suggests.

Categories