Solved

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

Posted on 2008-06-23
6
1,694 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
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

803 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