473,473 Members | 1,985 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Creating unmanaged C++ object in C#

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, Vin
Mar 8 '07 #1
6 7063

"Vincent Finn" <1@2.comwrote in message
news:39*************************@msnews.microsoft. com...
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.
Mar 8 '07 #2
"Vincent Finn" <1@2.comwrote in message
news:39*************************@msnews.microsoft. com...
>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.
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
Mar 9 '07 #3

"Vincent Finn" <1@2.comwrote in message
news:39*************************@msnews.microsoft. com...
>"Vincent Finn" <1@2.comwrote in message
news:39*************************@msnews.microsoft .com...
>>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.

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.
Mar 9 '07 #4
"Vincent Finn" <1@2.comwrote in message
news:39*************************@msnews.microsoft. com...
>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.
Bugger, don't want the hassle of trying to set COM up on Linux :o(
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
Mar 12 '07 #5
Bugger, don't want the hassle of trying to set COM up on Linux :o(
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.
>
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


Mar 12 '07 #6
Hello Ben,
>Bugger, don't want the hassle of trying to set COM up on Linux :o(
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.
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, Vin
Mar 13 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Steve | last post by:
Can anyone point me to a primer for creating OCX controls in VB .net? In particular, I want to access a web service in a VB6 application (not an easy thing to do). So, if I can write an ActiveX...
1
by: Vannela | last post by:
In my unmanaged there is code i n this way Class1 c1= new Class1(); // Class1 is one object and creating a instance of it\ Class1 c2= new Class2();// Class2 is another object a...
2
by: JRoe | last post by:
Hello, I am beside myself with this problem. I need to access unmanged C++ code in a C# environment. I have created a managed C++ wrapper that wraps the unmanaged class. When I call the...
4
by: Rachel Suddeth | last post by:
What is the difference between a managed/unmanaged resource, and how do you tell which is which? I'm trying to understand how to write some Dispose() methods, and we are supposed to put code that...
3
by: James Coleman | last post by:
Hello, The following error is appearing when attempting to create a directory using the availale system.io methods: System.IO.DirectoryNotFoundException: Could not find a part of the path...
4
by: tshad | last post by:
I am trying to set up an Image authorization where you type in the value that is in a picture to log on to our site. I found a program that is supposed to do it, but it doesn't seem to work. ...
0
by: Mayur | last post by:
Hello all, Im trying to call my .net c# library functions from my unmanaged vc++(dialog based MFC) application . From that application i want add the event handler for the event generated in c#...
7
by: Ragnar Agustsson | last post by:
Hi all I have been wandering about the best way to sandbox memory leaks in 3rd party libraries when using them from the .Net framework. I have a 3rd party library, written in C++, that leaks a...
0
by: =?Utf-8?B?c2Vy?= | last post by:
Hi, I am working on a windows application whose primary task is to apply all the properties of one control to another. Hence i am using the SetValue method of the PropertyDescriptor. The...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.