JConnell
JConnell

Reputation: 93

How to mimick VB use of COM Object Interface using C#

I am working with a vendor's API. It is supplied as various COM object DLL's. I am trying to gain access to method "CreateDisconnectedADORecordset" contained within the interface IBBUtilityCode.

A code sample from the vendor is as follows:

Dim oReservices As New REServices
Dim oUtilCode As IBBUtilityCode
Dim rs As Recordset
Dim sSQL As String

sSQL = "SELECT * FROM CONSTITUENT_BANK"

oReservices.Init SessionContext
Set oUtilCode = oReservices

Set rs = oUtilCode.CreateDisconnectedADORecordset(sSQL)

They seem to be using the interface "IBBUtilityCode" by first declaring a pointer to one, then casting "oReservices" as a pointer to the interface???

I am not a VB programmer, so I really haven't a clue.

How can this be done with C#? Any help and/or pointers you can give me would be greatly appreciated.

Thanks, Jimmy

Upvotes: 0

Views: 105

Answers (2)

Hans Passant
Hans Passant

Reputation: 942187

They seem to be using the interface "IBBUtilityCode" by first declaring a pointer to one, then casting "oReservices" as a pointer to the interface???

The concept of a class implementing an interface is not special to COM or Visual Basic, you should be familiar with it in C# as well. Just as in this VB6 code, you don't need a cast to obtain the interface reference. The translation is entirely mechanical:

var oReservices = new REServices();
oReservices.Init(SessionContext);
IBBUtilityCode oUtilCode = oReservices
var sSQL = "SELECT * FROM CONSTITUENT_BANK"
var rs = oUtilCode.CreateDisconnectedADORecordset(sSQL)

No lack of traps with this kind of component, ADO has not aged well. A minor install or configuration problem can turn into a pretty unsolvable headache. Be sure to contact the vendor or author of this component when you have trouble.

Upvotes: 1

attila
attila

Reputation: 2229

I would suggest using cominvoke. Here is a link from Microsoft explaining it: http://msdn.microsoft.com/en-us/library/aa645736(v=vs.71).aspx

Here is an example of it from that page:

// Create an instance of a COM coclass:
FilgraphManager graphManager = new FilgraphManager();

// See if it supports the IMediaControl COM interface. 
// Note that this will throw a System.InvalidCastException if 
// the cast fails. This is equivalent to QueryInterface for 
// COM objects:
IMediaControl mc = (IMediaControl) graphManager;

// Now you call a method on a COM interface: 
mc.Run();

Upvotes: 0

Related Questions