Link to home
Start Free TrialLog in
Avatar of Sharp2b
Sharp2bFlag for Sweden

asked on

How to call functions in a COM OLE Automation DLL from C# .NET2.0

I'm trying to figure out how to call a COM DLL from my C# application.

I have referenced the DLL in my C# project but now I don't know how to call it.
After adding the reference, I got a reference to BaseType.

First I tried something similar to what I could do in VB 6 but this fails as in Case 1.
Intellisense gives me the BaseType.theCOMDLL.

I then tried to create a class that inherits the abstract class theCOMDLL.
The intellisense in VS recognizes what's inside as it creates all the methods for me but these are of course not implemented.

What do I need to do to be able to call the methods in theCOMDLL?
// Case 1
// This gives an error:
// Cannot create an instance of the abstract class or interface 'BaseType.theCOMDLL'
BaseType.theCOMDLL myCOM = new BaseType.theCOMDLL();
 
//Case 2
// A new class that inherits the abstract class works fine and all
// methods are expanded but not implemented of course.
public class isis : isisbase
{
    // A whole lot of methods defined here like:
    public void DoSomeStuff()
    {
        throw new NotImplementedException();
    }
}

Open in new window

Avatar of Refael Ackermann
Refael Ackermann
Flag of United States of America image

See if there's a BaseType.theCOMDLLClass.
Anyway you can double click on the reference and see the imported types in the Object browser.
Avatar of Sharp2b

ASKER

Yes, this is correct.
But how can I call the methods within the BaseType.theCOMDLL class?

As you can see in the code example, Case 2, I can also create a new class that inherits the abstract class and have VS generate stubs for all the contained methods. BUT, I don't want to override them, just use them as they are.

How can I get to the contained methods so that I can call them?
// Case 1
// This gives an error:
// Cannot create an instance of the abstract class or interface 'BaseType.theCOMDLL'
BaseType.theCOMDLL myCOM = new BaseType.theCOMDLL();
 
//Case 2
// A new class that inherits the abstract class works fine and all
// methods are expanded but not implemented of course.
public class someclass : BaseType.theCOMDLL
{
    // A whole lot of methods defined here like:
    public void DoSomeStuff()
    {
        throw new NotImplementedException();
    }
}

Open in new window

Avatar of Sharp2b

ASKER

Maybe I should try to clarify the question;

I have an Active X component that is registered as an OLE Automation server.
When adding a reference to my C# project in VS 2008, I can find this under the COM tab. When added to my project I get a reference to BaseType.

In the IDE editor, when typing BaseType. intellisense shows the option to expand this to BaseType.theCOMDLL.

In VB6, I was able to use this like:
set obj = CreateObject("theCOMDlL")

I could then call obj.SomeMethod()

If I try to call BaseType.theCOMDLL in my project, I get an error like:
Cannot create an instance of the abstract class or interface 'BaseType.theCOMDLL'

I have not been able to figure out how I can instantiate this class.
Usually the tlbmp tool imports the types as both an abstract class (for example MyType), and a concrete class for (example MyTypeClass).
You should be able to instantiate MyTypeClass, and call the methods on it.
ASKER CERTIFIED SOLUTION
Avatar of Sharp2b
Sharp2b
Flag of Sweden image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial