473,386 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Unmanaged C++.Net calling Managed C#.Net

I have a 3rd party application that can reference external dll's. The dll's have to be written in unmanaged code with an exported function I can reference and call. I would like it to call a C# dll file but it cant call a managed C# dll. I need to create an unmanaged C++.net wrapper for a Managed C# dll. Does anyone know how to do this? can you please give me a simple example of what the C++.Net code would look like to do this

Take for example my C# dll has this functio

public int ExposedFunction(int X1, int X2

return (X1+X2)
What would the C++.Net code look like to call this...

Thank
Gavin Steven
ga***@abis.bi

Nov 17 '05 #1
1 9429
Well simple code is a bit hard in this case but here is a try...

If I have to (I'll talk about using the COM interop layer below), I usually wrap .Net objects in mixed mode C++ assemblies. In this kind of operation you compile some of your C++ files with the /clr switch, while others are compiled as straight unmanaged C++. In this case, I will wrap your function in a wrapper class, because this allows you to wrap .Net instances as well as functions.

So you would create a MC++ files that looks like -
MyExporter.h:
using namespace ExposedFunctionNamespace;

public __gc class MyExporter
{
int MyExposedFunction(int X1, int X2)
{
return ExposedFunctionNamespace::ExposedFunction(X1, X2);
}
};

MyExporter.cpp:
#include "MyExporter.h"
#include <vcclr.h>
gcroot<MyExporter*> MyExporterObject = new MyExporter();

In C++ your would write -
// This is the actual entry to managed space the gcroot template ensures
// unmanaged code that is not GC aware can safely interact with CLR
// objects.
#include <vcclr.h>
extern gcroot<MyExporter*> MyExporterObject;

__declspec(dllexport) int MyExposedFunction(int X1, int X2)
{
return MyExporterObject->ExposedFunction(X1, X2);
}

OK the above is not really enough, but you should be able to get a feel for the basic steps. In general the approach I take to interop with unmanaged code is to minimize the exposure of my .Net classes to basic types only. but for non-trival examples this starts to get more and more difficult and time consuming.

COM Interop via TLBExp

Now if you can afford it (perf wise), and you have a strong stomach you can always do the following:
tlbexp <your-C#-assembly>
This will produce a TLB file that can be directy used in standard C++ COM code. So in this case you would implement a completely normal C++ DLL that would use .Net code via the standard COM interop marshalling framework. This approach allows you some freedom in the design of your C# objects. You'll most likely want design you C# objects for easy interop with COM callers and then make a pure C++ layer that handles the DLL exposure for you (basically providing access to your .NET/COM objects via C functions).

Like I said above, if you can afford the overhead of marshalling, then using the later approach allows you to concentrate on your object design without worrying about the managed C++ details. Managed C++ will give you much finer grained control but it comes at a cost in terms of complexity. If you choose the COM route, you should think about limiting your .Net classes public interfaces to reference only each other and basic types (strings, ints, floats, etc). This prevents you from pulling in mscorlib.tlb into you COM interfaces. It doesn't hurt to pull in mscorlib, but my preference is to keep things minimal when doing this kind of exposure work.

Hope this helps,
Kevin

This posting is provided "AS IS" with no warranties, and confers no rights.
Nov 17 '05 #2

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

Similar topics

11
by: Yoni Rabinovitch | last post by:
Is it possible to invoke a C# delegate/event handler asynchronously, from unmanaged C++ ? I assume this requires a Managed C++ wrapper, which would call BeginInvoke on the delegate ? Is this...
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...
4
by: repstat | last post by:
Hi I have a project which is going to be doing some string manipulation which needs to be pretty fast. The user interface is going to be written in C#. I am going to write the string handling...
3
by: Thorsten | last post by:
HI I'm a C# developer and unfortunately I have to write now some code in managed and unmanaged C++. In this area I'm Newbie and therefore please forgive me if this is a really simple...
1
by: H.B. | last post by:
Hi, I need to make a function that can display data on my Managed C++ app and be called by an unmanaged C++ DLL. Something like : void Form1::Form1_Load(System::Object * sender,...
1
by: MC-Advantica | last post by:
Does anyone have a simple "Hello World" like application that demonstrates unmanaged C++ calling managed C++ developed in VS2005? I'm confused by many posts as they discuss managed extensions from...
9
by: Amit Dedhia | last post by:
Hi All I have a VC++ 2005 MFC application with all classes defined as unmanaged classes. I want to write my application data in xml format. Since ADO.NET has buit in functions available for...
6
by: Aston Martin | last post by:
Hi All, ********************** My Situation ********************** I am working on project that involves passing a structure to unmanaged code from .Net world (well using C#). Perhaps an example...
3
by: Klaus | last post by:
Hi, I have an existing VC 6 MFC application which communicates asynchronly with a VC 2005 managed code dll. I use an unmanaged base class with virtual functions to access methods in the MFC...
2
by: Jon Slaughter | last post by:
How difficult is it for one to integrate unmanaged C++ into C#? I know for functions one can use DLLimport but how does one go about doing it for classes? Do I have to completely reimplement the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.