Solved

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

Posted on 2008-06-23
6
1,686 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

747 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