473,418 Members | 2,091 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,418 software developers and data experts.

Calling native C++ dll code from managed C++

Just started working on this and have not found any real good resources out
there. We have a lot of native C++ Dll code that we use for our app. We want
to share the code so that C# ASP.net code can use the same business logic as
our C++ client. Here are some questions:

1) Is there a way to call into native C++ classes directly? Meaning we do
not have C API. I believe we could use a C API using the [DllImport] in C#.
2) If can't do #1, do we need to write custom marshalers for all the data
types. I believe that this is the P/Invoke method. Is there any other
methods besides custom marchaling.
3) We have played with wrapping a native C++ class with managed C++ but as
soon as you include c runtime or stl in the .cpp of our unmanaged C++ the
managed DLL exposes every type in the C runtime and stl. We obviously do not
want to expose these types since the caller of the managed C++ will never
need them. This managed wrapper will also be given to customers so we would
like to only have the 1 or 2 managed classes exposed and nothing else.
4) How do exceptions work when wrapping unmanaged with managed. Some of the
exposed objects in #3 are exceptions. If an exception gets thrown in
unmanaged how does it get propagated through the managed. a) if the managed
catches the exception what does it throw and b) what happens if the managed
does not catch the exception?

There seems to be alot of people wanting to do the same thing but I haven't
found a good example that shows how.

Thanks,

Tim
Nov 17 '05 #1
4 9828

"Tim Menninger" <tm********@forensicsexplorers.com> wrote in message
news:#2*************@tk2msftngp13.phx.gbl...
Just started working on this and have not found any real good resources out there. We have a lot of native C++ Dll code that we use for our app. We want to share the code so that C# ASP.net code can use the same business logic as our C++ client. Here are some questions:

1) Is there a way to call into native C++ classes directly? Meaning we do
not have C API. I believe we could use a C API using the [DllImport] in C#.

COM or a managed C++ wrapper, pick your poison.

2) If can't do #1, do we need to write custom marshalers for all the data
types. I believe that this is the P/Invoke method. Is there any other
methods besides custom marchaling.
You cant expose C++ objects via DllImport
3) We have played with wrapping a native C++ class with managed C++ but as
soon as you include c runtime or stl in the .cpp of our unmanaged C++ the
managed DLL exposes every type in the C runtime and stl. We obviously do not want to expose these types since the caller of the managed C++ will never
need them. This managed wrapper will also be given to customers so we would like to only have the 1 or 2 managed classes exposed and nothing else.
Dont expose them then, the .NET assembly only exposes what you tell it to
and any .NE T language will only see .NET metadata.
4) How do exceptions work when wrapping unmanaged with managed. Some of the exposed objects in #3 are exceptions. If an exception gets thrown in
unmanaged how does it get propagated through the managed. a) if the managed catches the exception what does it throw and b) what happens if the managed does not catch the exception?

There seems to be alot of people wanting to do the same thing but I haven't found a good example that shows how.

Thanks,

Tim

Nov 17 '05 #2
I have articles on this:
http://www.ondotnet.com/pub/a/dotnet...intromcpp.html
http://www.ondotnet.com/pub/a/dotnet...03/mcppp2.html

I am currently working on a 3rd for managed wrappers.

--
--------------------------------------------------------------
Sam Gentile [C#/.NET MVP]
..NET Blog http://samgentile.com/blog/
MSDN Column:
http://msdn.microsoft.com/library/de...tml/bridge.asp
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
---------------------------------------------------------------
"Tim Menninger" <tm********@forensicsexplorers.com> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
Just started working on this and have not found any real good resources out there. We have a lot of native C++ Dll code that we use for our app. We want to share the code so that C# ASP.net code can use the same business logic as our C++ client. Here are some questions:

1) Is there a way to call into native C++ classes directly? Meaning we do
not have C API. I believe we could use a C API using the [DllImport] in C#. 2) If can't do #1, do we need to write custom marshalers for all the data
types. I believe that this is the P/Invoke method. Is there any other
methods besides custom marchaling.
3) We have played with wrapping a native C++ class with managed C++ but as
soon as you include c runtime or stl in the .cpp of our unmanaged C++ the
managed DLL exposes every type in the C runtime and stl. We obviously do not want to expose these types since the caller of the managed C++ will never
need them. This managed wrapper will also be given to customers so we would like to only have the 1 or 2 managed classes exposed and nothing else.
4) How do exceptions work when wrapping unmanaged with managed. Some of the exposed objects in #3 are exceptions. If an exception gets thrown in
unmanaged how does it get propagated through the managed. a) if the managed catches the exception what does it throw and b) what happens if the managed does not catch the exception?

There seems to be alot of people wanting to do the same thing but I haven't found a good example that shows how.

Thanks,

Tim

Nov 17 '05 #3
I have read your articles, but it seem to have a problem using the following
steps to build me Managed C++ Wrapper for a C library.

- Link with /NOENTRY. In Solution Explorer, right-click the project node and
click Properties. In the project's Property Pages dialog box, click Linker,
and then click Command Line. Add this switch to the Additional Options
field.
- Link msvcrt.lib. In the project's Property Pages dialog box, click Linker,
and then click Input. Add msvcrt.lib to the Additional Dependencies
property.
- Remove nochkclr.obj. On the Input page (same page as previous step),
remove nochkclr.obj from the Additional Dependencies property.
- Link in the CRT. On the Input page (same page as previous step), add
__DllMainCRTStartup@12 to the Force Symbol References property.

These steps are highlighted on the Microsoft website, however, in order for
my library to
compile and link properly, I have to skip step 2. If I don't, I get the
following errors:

libcmt.lib(crt0init.obj) : error LNK2005: ___xc_z already defined in
msvcrt.lib(cinitexe.obj)
libcmt.lib(crt0init.obj) : error LNK2005: ___xc_a already defined in
msvcrt.lib(cinitexe.obj)
libcmt.lib(crt0init.obj) : error LNK2005: ___xi_z already defined in
msvcrt.lib(cinitexe.obj)
libcmt.lib(crt0init.obj) : error LNK2005: ___xi_a already defined in
msvcrt.lib(cinitexe.obj)
libcmt.lib(winxfltr.obj) : error LNK2005: ___CppXcptFilter already defined
in msvcrt.lib(MSVCR71.dll)
libcmt.lib(crt0.obj) : error LNK2005: __amsg_exit already defined in
msvcrt.lib(MSVCR71.dll)
libcmt.lib(crt0.obj) : error LNK2019: unresolved external symbol _main
referenced in function _mainCRTStartup
D:\Mitel-Development\MiTAI.NET\MitaiNet\Debug\MitaiNet.dll : fatal error
LNK1120: 1 unresolved externals

However, if I were to leave my .NET Class Library settings like they were
when ti was created, I get the linker error that references the "_main"
symbol being unresolved.

Now, my application is not running correctly because of an exception within
the "strtok" function as outline in another post that nobody has been able
to help me with...

Thanks
Paul

Nov 17 '05 #4
You're right about the additions. Thanks for the feedback.

--
-----
Sam Gentile
Microsoft MVP - C#/.NET
..NET Blog http://samgentile.com/blog/

Please do NOT contact me directly but respond to
the newsgroup instead.
---------
"Paul Brun" <pb**********@pbrun.net> wrote in message
news:Or**************@TK2MSFTNGP10.phx.gbl...
I have read your articles, but it seem to have a problem using the
following
steps to build me Managed C++ Wrapper for a C library.

- Link with /NOENTRY. In Solution Explorer, right-click the project node
and
click Properties. In the project's Property Pages dialog box, click
Linker,
and then click Command Line. Add this switch to the Additional Options
field.
- Link msvcrt.lib. In the project's Property Pages dialog box, click
Linker,
and then click Input. Add msvcrt.lib to the Additional Dependencies
property.
- Remove nochkclr.obj. On the Input page (same page as previous step),
remove nochkclr.obj from the Additional Dependencies property.
- Link in the CRT. On the Input page (same page as previous step), add
__DllMainCRTStartup@12 to the Force Symbol References property.

These steps are highlighted on the Microsoft website, however, in order
for
my library to
compile and link properly, I have to skip step 2. If I don't, I get the
following errors:

libcmt.lib(crt0init.obj) : error LNK2005: ___xc_z already defined in
msvcrt.lib(cinitexe.obj)
libcmt.lib(crt0init.obj) : error LNK2005: ___xc_a already defined in
msvcrt.lib(cinitexe.obj)
libcmt.lib(crt0init.obj) : error LNK2005: ___xi_z already defined in
msvcrt.lib(cinitexe.obj)
libcmt.lib(crt0init.obj) : error LNK2005: ___xi_a already defined in
msvcrt.lib(cinitexe.obj)
libcmt.lib(winxfltr.obj) : error LNK2005: ___CppXcptFilter already defined
in msvcrt.lib(MSVCR71.dll)
libcmt.lib(crt0.obj) : error LNK2005: __amsg_exit already defined in
msvcrt.lib(MSVCR71.dll)
libcmt.lib(crt0.obj) : error LNK2019: unresolved external symbol _main
referenced in function _mainCRTStartup
D:\Mitel-Development\MiTAI.NET\MitaiNet\Debug\MitaiNet.dll : fatal error
LNK1120: 1 unresolved externals

However, if I were to leave my .NET Class Library settings like they were
when ti was created, I get the linker error that references the "_main"
symbol being unresolved.

Now, my application is not running correctly because of an exception
within
the "strtok" function as outline in another post that nobody has been able
to help me with...

Thanks
Paul


Nov 17 '05 #5

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

Similar topics

2
by: Peter Schmitz | last post by:
Hi, I just created a solution that is based on unmanaged C++ code. Now, the .net framework consists of some cool functions and classes - there is especially one namespace I'd like to use....
5
by: Virajitha Sarma | last post by:
Hi, Can u plz help me out with how to call a C# DLL from C... I tried to find out by searching in net..but i couldnot find anything on it.... Thanku Virajitha *** Sent via Developersdex...
10
by: Gustavo L. Fabro | last post by:
Greetings! I've been porting an application for Builder to VS .NET 2003 and searching for possible bottlenecks (the application is currently running slow). I found out one scenario that takes a...
1
by: Bern McCarty | last post by:
What do you make of this? I cannot tell for sure but it almost seems as the the transition thunk to get back from the native bool method to the managed caller is looking at eax and, if any bit is...
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,...
9
by: Herby | last post by:
Is possible to have a managed method within a Native(un-managed) class within a \clr project? E.g. class myClass { public: #pragma managed void myMethod(void);
2
by: Haroon_Saeed | last post by:
Hey, I wrote a managed c++ class library in viusal studio 2005. This managed c++ dll calls native C++ methods of an SDK. When I referenced this managed c++ library in a console applciation...
2
by: Dave Burns | last post by:
Hello, We have a situation where a managed C++ assembly links with native C++ dll. There is a callback mechanism which calls back into the managed code asynchronously. Since native classes...
2
by: Bob Altman | last post by:
Hi all, We have a native class modeled after the System::Exception class, and all exceptions that we throw derive from this class. For now this class is quite simple: just Description and...
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,...
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...
0
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,...
0
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...

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.