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

Sharp2bAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Refael AckermannCommented:
See if there's a BaseType.theCOMDLLClass.
Anyway you can double click on the reference and see the imported types in the Object browser.
0
Sharp2bAuthor Commented:
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

0
Sharp2bAuthor Commented:
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.
0
Refael AckermannCommented:
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.
0
Sharp2bAuthor Commented:
Thanks for the hints but I have managed to find solution myself. One of the initial things I tried was:
BaseType.theCOMDLL MyCOM = Activator.CreateInstance(Type.GetTypeFromProgID("theCOMDLL32"));

It turns out that in that I also have to add a type cast like:
BaseType.theCOMDLL MyCOM =(BaseType.theCOMDLL)Activator.CreateInstance(Type.GetTypeFromProgID("theCOMDLL32"));

Now it works nicely and intellisense also shows all available methods when typing
MyCom. in the IDE editor.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.