473,545 Members | 2,095 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9845

"Tim Menninger" <tm********@for ensicsexplorers .com> wrote in message
news:#2******** *****@tk2msftng p13.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********@for ensicsexplorers .com> wrote in message
news:%2******** *******@tk2msft ngp13.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
__DllMainCRTSta rtup@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(crt0 init.obj) : error LNK2005: ___xc_z already defined in
msvcrt.lib(cini texe.obj)
libcmt.lib(crt0 init.obj) : error LNK2005: ___xc_a already defined in
msvcrt.lib(cini texe.obj)
libcmt.lib(crt0 init.obj) : error LNK2005: ___xi_z already defined in
msvcrt.lib(cini texe.obj)
libcmt.lib(crt0 init.obj) : error LNK2005: ___xi_a already defined in
msvcrt.lib(cini texe.obj)
libcmt.lib(winx fltr.obj) : error LNK2005: ___CppXcptFilte r already defined
in msvcrt.lib(MSVC R71.dll)
libcmt.lib(crt0 .obj) : error LNK2005: __amsg_exit already defined in
msvcrt.lib(MSVC R71.dll)
libcmt.lib(crt0 .obj) : error LNK2019: unresolved external symbol _main
referenced in function _mainCRTStartup
D:\Mitel-Development\MiT AI.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**********@p brun.net> wrote in message
news:Or******** ******@TK2MSFTN GP10.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
__DllMainCRTSta rtup@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(crt0 init.obj) : error LNK2005: ___xc_z already defined in
msvcrt.lib(cini texe.obj)
libcmt.lib(crt0 init.obj) : error LNK2005: ___xc_a already defined in
msvcrt.lib(cini texe.obj)
libcmt.lib(crt0 init.obj) : error LNK2005: ___xi_z already defined in
msvcrt.lib(cini texe.obj)
libcmt.lib(crt0 init.obj) : error LNK2005: ___xi_a already defined in
msvcrt.lib(cini texe.obj)
libcmt.lib(winx fltr.obj) : error LNK2005: ___CppXcptFilte r already defined
in msvcrt.lib(MSVC R71.dll)
libcmt.lib(crt0 .obj) : error LNK2005: __amsg_exit already defined in
msvcrt.lib(MSVC R71.dll)
libcmt.lib(crt0 .obj) : error LNK2019: unresolved external symbol _main
referenced in function _mainCRTStartup
D:\Mitel-Development\MiT AI.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
1345
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. That's why I'm currently trying to figure out what's the best way to use this managed code snippet. Is it possible to create a managed Dll (in any...
5
1767
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 http://www.developersdex.com ***
10
2067
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 test function 125x more time to execute when the DLL is compiled with the /CLR parameter then when compiled without it. Average timings:...
1
1592
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 set, normalizing it to 0x00000001. If it wants to normalize the value then it should only operate on the al register since that's all that the...
1
2590
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, System::EventArgs * e) { MyDLLInit(MyAppDisplayFunction); }
9
2055
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
1812
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 written in C# for testing every thing worked fine. Now I need to call the managed c++ class library through web service written in C#. When I call...
2
2299
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 cannot hold onto a managed reference, we need to have a managed static member which we access during the callback and then get into the managed code. ...
2
2956
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 InnerException public members. One of these days I'll dig into how to implement a StackTrace property (I assume that this is possible using something...
0
7396
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7656
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
5968
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5323
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3449
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3440
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1874
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 we have to send another system
1
1012
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
700
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.