Solved

Using DLLs with C++ Linkage in C# , CSharp

Posted on 2008-06-23
6
1,689 Views
Last Modified: 2008-06-27
I have recently started messing with C++ DLLs in C# but it seems that I am restricted to the kind of usage I have. I am able to export functions in a DLL that have C++ linkage but C# cannot find the entry points. The C linkage functions can be called. Someone told me that this probably has to do with name mangling. Is there a way around this?

I am currently using VS 2008 with C# and C++ projects in the same solution. I am using DLL import to load the C++ DLL (unmanaged route). I know that by using C++.NET and wrapping my functions in a class (managed), most of these problems with go away, but I found a ridiculous speed improvement in the unmanaged route. Am I correct on this?

Similar if I try to modify strings or classes passed into the C++ DLL, I get all sorts of garbage. Any suggestions?

0
Comment
Question by:aaborkar
  • 4
  • 2
6 Comments
 
LVL 3

Expert Comment

by:maliger
ID: 21855974
To avoid name mangling problem, try to import your C++ functions via ordinals.
Also do not forget to specify correct calling convetion (c/c++ got cdecl as default)

Managed C++ is another way, but I'd prefer not using it, if you own both managed and unmanaged part.

[This is mine first answer, so give me know if it helped!]
[DllImport("dllname", EntryPoint="#123")]

Open in new window

0
 

Author Comment

by:aaborkar
ID: 21856590
What will be the C++ prototype declaration and its equivalent in C#?
0
 
LVL 3

Accepted Solution

by:
maliger earned 125 total points
ID: 21863606
This is excerpt from MSDN:
"""
The simplest way to export functions from your DLL is to export them by name. This is what happens when you use __declspec(dllexport), for example. But you can instead export functions by ordinal. With this technique, you must use a .def file instead of __declspec(dllexport). To specify a function's ordinal value, append its ordinal to the function name in the .def file. For information about specifying ordinals, see Exporting from a DLL Using .def Files.
"""

Problem is, that in the .def file, you need to write decorated (mangled) names of functions, which is almost same as write this mangled name on c# side.

I dont catch why you need to export in C++ linkage. What is wrong with "C" linkage in your exports? Do you export overloads (same function name, different arguments)? If it is possible (and I think it is), I'd recommend change to "C" linkage and stdcall.
extern "C" __declspec( dllexport ) int __stdcall MyFunc(long parm1);

If not, you'll need mangled name somewhere. Try /MAP switch of linker to obtain them (Linker/Debugging in VS). I'd recommend stdcall here anyway. Then
1/ import with mangled name on c#
 [DllImport("dllname", EntryPoint="_EmitError@4")]
 2/ Create .DEF file on C++ side
specify ordinal there:
EXPORTS
   _EmitError@4  @123
and import via ordinal on c# side
[DllImport("dllname", EntryPoint="#123")]
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Expert Comment

by:maliger
ID: 21863634
There is also 3rd way. In .DEF file you can 'rename' your etry point
EXPORTS
  EmitError=_EmitError@4

Never tried this, personally.
entryname[=internalname] [@ordinal [NONAME]] [PRIVATE] [DATA]
0
 

Author Comment

by:aaborkar
ID: 21865242
This seems very complicated. I am currently using the extern "C" __declspec(dllexport).... way to write out my C linkage functions. The reason I was asking about the C++ way was because I would like to send custom classes or structures to C++ for number crunching. However, on one of my other posts, evilrix and mrwad99 pointed out that sending STL's beyond the dll boundary is painful. So sending classes that use dynamic memory objects like strings and array have some issues getting around.

My current option is to break my program into 3 or 4 parts.
1. C# program
2. C# wrapper that decodes the classes and structs in primitive types for the "C" linkage
3. Send the primitives to the C++ dll via C types and do processing

Any other suggestions.
 
0
 
LVL 3

Expert Comment

by:maliger
ID: 21884426
yes, your c++ code will be cleaner when not exposing any c++ class/struct and definitelly not any STL stuff. Those are real pain in c# (and any other non-cpp caller). For c# you can get strings via BSTR (CString.AllocSysString and dealoc it on c# with Marshal..::.FreeBSTR), but I personally would evade those too.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now