Peter Mortensen
Peter Mortensen

Reputation: 31598

How do I use a versioned COM interface from C#?

How do I make instances of and calls to COM components that have been extended?

I have used a third-party COM component (XRawFile2.dll from Finnigan/ Thermo Scientific) for many years in a mass spectrometry-related application written in .NET (mixed VB.NET and C#) for accessing raw spectrum data. This has worked well.

However, this COM component has been extended by way of interface inheritance. Expressed in IDL (extracted using the OLE/COM Object Viewer [OleView.Exe]):

interface IXRawfile3 : IXRawfile2 {
.
.

interface IXRawfile2 : IXRawfile {
.
.

interface IXRawfile : IDispatch {
.
.

coclass XRawfile {
    [default] interface IXRawfile;
};

The full extracted IDL for XRawFile2.dll is available (HTML page with <pre>).

I want to use a function available in the new interface (IXRawfile3),

GetMassListRangeFromScanNum()

instead of

GetMassListFromScanNum()

in the original interface (IXRawfile).

I have no trouble creating an instance of XRawFile and calling GetMassListFromScanNum().

But I can't get it to work with GetMassListRangeFromScanNum(). For instance, using GetMassListRangeFromScanNum() for an instance of XRawFile gives this compile error:

    Error 1 'XRAWFILE2Lib.XRawfile' does not contain a
    definition for 'GetMassListRangeFromScanNum' and no
    extension method 'GetMassListRangeFromScanNum' accepting a
    first argument of type 'XRAWFILE2Lib.XRawfile' could be
    found (are you missing a using directive or an assembly
    reference?)

The tryout C# source code is also available.

Platform: Windows XP 64 bit SP2. Visual Studio 2008. The interop file for XRawFile2.dll was created by Visual Studio 2008 in the normal manner.

Upvotes: 1

Views: 627

Answers (2)

Yi You
Yi You

Reputation: 1

I would suggest to use the following statement (for version 2.2):

MSFileReader_XRawfile rawfile = new MSFileReader_XRawfile();

This could simply give you an intense.

You may call any of the methods later.

Upvotes: 0

Ian Ringrose
Ian Ringrose

Reputation: 51897

Try casting your instance of XRawFile to IXRawfile3

e.g.

( (IXRawfile3) myRawFile ).GetMassListRangeFromScanNum()

This should do a query interface on the COM object, asking it for the IXRawfile3 interface. (Assuming the typelib you have imported does match the implementation of XRawfile)

Upvotes: 1

Related Questions