473,386 Members | 1,864 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.

Sample of Unmanaged C++ code calling Managed C++

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 VS2003, and related techniques.
I have found managed to unmanaged technique very easy in VS2005, but have
not been able to build anything with unmanaged to managed calls. I have
legacy (unmanaged C++) that would like to leverage the .NET framework; I'd
like to build a managed utility DLL that my unmanaged code can call. How
about this approach?
Sep 29 '06 #1
1 3394
"MC-Advantica" <MC*********@discussions.microsoft.comwrote in message
news:ED**********************************@microsof t.com...
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 VS2003, and related
techniques.
I have found managed to unmanaged technique very easy in VS2005, but have
not been able to build anything with unmanaged to managed calls. I have
legacy (unmanaged C++) that would like to leverage the .NET framework; I'd
like to build a managed utility DLL that my unmanaged code can call. How
about this approach?
The approach may be fine. But as always, the devil is in the details. You
don't say what you have tried and how it failed.

I just built this trivial unmanaged toy application

//-------------------------------------------------------------------------
#include <stdlib.h>
#include <iostream>

__declspec(dllimport) void Square(int);

int main(int argc, char **argv)
{
int n;
char *p;
bool ok;

ok = false;

if ( argc == 2 )
{
n = strtoul(argv[1], &p, 10);
if ( *p <= ' ' )
ok = true;
}

if ( ok )
Square(n);

else
std::cout << "Bad command line";

return 0;
}

And this managed toy DLL

//-------------------------------------------------------------------------
#using <mscorlib.dll>

#pragma managed

void DoIt(int n)
{
System::Console::WriteLine("The square of {0} is {1}", n, n * n);
}

#pragma unmanaged

void Square(int n)
{
DoIt(n);
}

It works as expected. I built it wthe VS2005 but cleverly <gI avoided any
issues related to MC++ or C++/CLI. Even more cleverly, I didn't try to pass
much ( a 32 bit integer ) in the way of parameters over the fence dividing
the two envirionments.

In other words, my sample demonstrates a simple transition into managed
code. In the real world, things get complicated in a hurry. If you post some
details as to what you need to do you may get some suggestions from someone
here who has been there.

Regards,
Will


Sep 29 '06 #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,...
8
by: _iycrd | last post by:
Specifically I need to wrap an older Win32 DLL in a managed class. I had this running with VS2003's Managed Extensions, though it required two separate classes. With C++/CLI this was supposed...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.