Using a C++ class in C#

T

Thomas Johansen

Hi.

I have an DLL written as "normal" C++ dll. I has an GetObject function,
which return an c++ class.
Now I whant to use this class (and all its members) in my C# applikation.
I know that C# classes and C++ class are incompatible.
So this is how I whant to solve it: (If possible)

My C# applikation uses a managed C++ class, that is added to my C# project.
The managed C++ class then uses the "old" dll and the C++ class returned by
it. It then has some members functions/Interfaces, that is used by the C#
class.
The managed C++ class allso must use WaitforMultipleObjects(..) (The DLL
uses named events for signaling)

So my Q's are:

1: Is this gonna work ?

2: If it does, how do I add an managed C++ file/class til my C# projects and
how does the C# class use the managed C++ class ? (How does the C# class now
about the managed C++ class)

3: Is there any other pretty solution for this ?

Thomas
 
R

Rami Saad

You can use the tool "RegAsm.exe" that makes .Net compatible with unmanaged
code.
It is found with the Visual Studio.Net folder.
 
W

Willy Denoyette [MVP]

Inline ***
Willy.
Thomas Johansen said:
Hi.

I have an DLL written as "normal" C++ dll. I has an GetObject function,
which return an c++ class.
Now I whant to use this class (and all its members) in my C# applikation.
I know that C# classes and C++ class are incompatible.
So this is how I whant to solve it: (If possible)

My C# applikation uses a managed C++ class, that is added to my C#
project.
The managed C++ class then uses the "old" dll and the C++ class returned
by
it. It then has some members functions/Interfaces, that is used by the C#
class.
The managed C++ class allso must use WaitforMultipleObjects(..) (The DLL
uses named events for signaling)

So my Q's are:

1: Is this gonna work ? Yes.
2: If it does, how do I add an managed C++ file/class til my C# projects
and
how does the C# class use the managed C++ class ? (How does the C# class
now
about the managed C++ class)

Add a reference to the C++ assembly.

3: Is there any other pretty solution for this ?
Isn't this pretty enough?.
 
T

Thomas Johansen

Add a reference to the C++ assembly.

In my solution explorer I right click and select Add Reference. I select the
Project tab and press Browse.
Then I select my "old" VS6 DLL and get the following error:

"A Reference to "MyPath\MyDll.dll" could not be added. This is not a valid
assembly or COM component. Only assemblies with extension 'dll' and COM
components can be referenced. Please make sure that the file is accessible,
and that it is a valid assembly or COM component."

I guess its because it has to be an .NET compiled dll or am I wrong here ?
Isn't this pretty enough?.
mmh... No.. I dont like it :) But it's the only way I can think of...
 
W

Willy Denoyette [MVP]

Hmmm your question....
< 2: If it does, how do I add an managed C++ file/class til my C# projects
and
how does the C# class use the managed C++ class ? (How does the C# class now
about the managed C++ class)>


Why are you adding a reference to your old VC6 dll? You must add a reference
to your managed C++ assembly not the old VC6 dll.

WIlly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top