Solved

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

Posted on 2008-06-23
6
1,709 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

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.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

635 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