Creating unmanaged C++ object in C#

V

Vincent Finn

Hi,

I have what I thought was a simple question but I can't find an answer.

I need to access (create) unmanaged C++ classes in C#.
They aren't COM and they aren't struct just simple classes.

What I currently have is the following

File view:
Form1.exe -> Controller_net.dll -> Controller.lib
Class creation:
"Form1 (C#)" creates "Controller_net (Managed C++)" creates "Controller (unmanaged
C++)"

The unmanaged code has a set of classes derived from a Command class, these
contain strings, vectors, etc.

Controller has a single public function, Execute, that takes a Command* as
an argument and then executes the command using members as inputs and setting
member variables as outputs.

The Managed dll has a matching set of classes derived from a .Net class called
Command_net, these classes create their unmanged equivalents and handle conversion
from C++ to .Net types and vice versa.

This is a very simple structure allowing the Managed C++ to handle conversion
since it understands C++ and .Net types.


I am now porting this to Linux so I wish to use Mono for the .Net side of
things, Mono doesn't support managed C++.
So I now need to take the Controller_net.dll out of the equation.

I can create the Command_net classes in C#, and I can convert Controller.lib
to a dll and use dllexport to export the unmanaged classes.

I can't see how to hook them together.
PInvoke seems to simply call functions on the dll and I need to create objects.
The only interop object creation I can see is for COM objects.

Can anyone point me in the right direction?

Thanks, Vi
 
B

Ben Voigt

Vincent Finn said:
Hi,

I have what I thought was a simple question but I can't find an answer.

I need to access (create) unmanaged C++ classes in C#.
They aren't COM and they aren't struct just simple classes.

C++ makes no different between struct and class.
 
V

Vincent Finn

Vincent Finn said:
C++ makes no different between struct and class.

True but in the examples I have seen the structs are C style structs i.e.
no base classes, no functions.
I should have made that clearer in my post.

The question is how to get C# to create a real C++ class and marshal it,
vtable and all.

What I want to do is this

// in C#
Controller* pController = new Controller();
Command* pCommand = new ParseCommand("file path");
pController->Execute(pCommand);

I can do this in Managed C++ but I can't see how to in C#

Vin
 
B

Ben Voigt

Vincent Finn said:
True but in the examples I have seen the structs are C style structs i.e.
no base classes, no functions.
I should have made that clearer in my post.
The question is how to get C# to create a real C++ class and marshal it,
vtable and all.

You're going to need to use COM for that, (not IDispatch). COM uses
v-tables that are compatible with the C++ compiler v-table layout.

I guess you could also create a class with a bunch of delegate members for
the vtable, and a structure with that class type as the first member.

Look at what the IDL compiler generates when the output language is C, not
C++, and it will make more sense.
 
V

Vincent Finn

Vincent Finn said:
You're going to need to use COM for that, (not IDispatch). COM uses
v-tables that are compatible with the C++ compiler v-table layout.

I guess you could also create a class with a bunch of delegate members
for the vtable, and a structure with that class type as the first
member.

Look at what the IDL compiler generates when the output language is C,
not C++, and it will make more sense.

Bugger, don't want the hassle of trying to set COM up on Linux :blush:(
I was hoping I was missing something obvious that would give a simple solution.

Oh well, I only wanted one level of inheritance so easy to work around.
I'll dump the base class from the C# and replace my single Execute function
with one for each structure and confine the inheritance to the C++, ugly
but functional.

Thanks, Vin
 
B

Ben Voigt

Bugger, don't want the hassle of trying to set COM up on Linux :blush:(
I was hoping I was missing something obvious that would give a simple
solution.

I didn't mean you necessarily need the COM runtime. However, on Linux the
C++ object format could be different anyway. Any program trying to do what
you're talking about would be very fragile -- that sort of intimate
knowledge of C++ ABI would be appropriate for a C++.NET compiler but not for
application code.
 
V

Vincent Finn

Hello Ben,
I didn't mean you necessarily need the COM runtime. However, on Linux
the C++ object format could be different anyway. Any program trying
to do what you're talking about would be very fragile -- that sort of
intimate knowledge of C++ ABI would be appropriate for a C++.NET
compiler but not for application code.

Yeah, I was hoping for a magic bullet of sorts.
Oh well, it is straight-forward to do without inheritance just a bit messier.

Thanks for the help, Vi
 

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