Go Premium for a chance to win a PS4. Enter to Win


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

Posted on 2008-06-23
Medium Priority
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?

Question by:aaborkar
  • 4
  • 2

Expert Comment

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


Author Comment

ID: 21856590
What will be the C++ prototype declaration and its equivalent in C#?

Accepted Solution

maliger earned 375 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:
   _EmitError@4  @123
and import via ordinal on c# side
[DllImport("dllname", EntryPoint="#123")]
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Expert Comment

ID: 21863634
There is also 3rd way. In .DEF file you can 'rename' your etry point

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

Author Comment

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.

Expert Comment

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.

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

916 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