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

T

Tim Menninger

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
 
S

Sam Gentile [MVP - C#/.NET]

I have articles on this:
http://www.ondotnet.com/pub/a/dotnet/2003/01/13/intromcpp.html
http://www.ondotnet.com/pub/a/dotnet/2003/03/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/default.asp?url=/library/en-us/dndotnet/html/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.
 
G

Guest

Tim Menninger said:
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.
 
P

Paul Brun

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
 
S

Sam Gentile [MVP - C#/.NET]

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.
 
P

Paul Brun

Do you have any ideas about why I am having runtime issues with
my program?

Please let em know.
Paul
 
G

Guest

It may help to implement your dllmain routine like below.
I read this somewhere

BOOL WINAPI _CRT_INIT(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved);

/*******************************************************************************
* FUNCTION: DllMain
*
* INPUTS: hDLL - handle of DLL
* dwReason - indicates why DLL called
* lpReserved - reserved
*
* RETURNS: TRUE (always, in this example.)
*
* Note that the return value is used only when
* dwReason = DLL_PROCESS_ATTACH.
*
* Normally the function would return TRUE if DLL initial-
* ization succeeded, or FALSE it it failed.
*
* COMMENTS: The function will display a dialog box informing user of
* each notification message & the name of the attaching/
* detaching process/thread. For more information see
* "DllMain" in the Win32 API reference.
*
* Note that DllMain is only called if the dll is 32bit
*
\******************************************************************************/

BOOL WINAPI DllMain (HINSTANCE hDLL, DWORD dwReason, LPVOID lpReserved)
{
if (dwReason == DLL_PROCESS_ATTACH || dwReason == DLL_THREAD_ATTACH)
if (!_CRT_INIT(hDLL, dwReason, lpReserved ))
return (FALSE);

if (dwReason == DLL_PROCESS_DETACH || dwReason == DLL_THREAD_DETACH)
if (!_CRT_INIT(hDLL, dwReason, lpReserved ))
return (FALSE);

return( TRUE );
}
 
G

Guest

Unused c++ classes and STL components are all being exported from a simple interface I'm putting together. Is there anyway to filter out or hide all the "extra" things that appear as system types from a mix mode C++ .net library

I've tried to disassemble, remove the references, and reassemble, however it breaks. Any help would greatly be appreciated

Thanks
Todd
 
R

Ronald Laeremans [MSFT]

Compile with /d1PrivateNativeTypes. We have a KB article out on what it
means and does that you can most easily find by a google search for the
switch name.

Ronald Laeremans
Visual C++ team

Todd said:
Unused c++ classes and STL components are all being exported from a simple
interface I'm putting together. Is there anyway to filter out or hide all
the "extra" things that appear as system types from a mix mode C++ .net
library?
I've tried to disassemble, remove the references, and reassemble, however
it breaks. Any help would greatly be appreciated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top