Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am trying to write a c++ library and i need to call a function from a C# library(portable class library).

Can any body help me doing this?
Posted
Comments
Sergey Alexandrovich Kryukov 3-Mar-14 14:49pm    
As C# is mentioned, does it mean that "C+" actually means "C++/CLI"? Then you need to reference C# assembly. If C++ is all native code, this is much more difficult.
—SA

Please see my comment to the question; it's not clear what exactly do you mean by C++.

However, here is the idea: once you use C# anyway, you should also use C++/CLI even if all or most of the C++ code is native. You can create a mixed-mode C++ project (managed + unmanaged) and use managed part for, in particular, using other .NET assemblies. Your mixed-mode project will create an executable model, which, from the CLI point of view, can be used as a regular CLI assembly, for example, referenced by other assemblies. But, as your question is concerned, this might be not your goal. It simply allows you to use C# code. Even if you need to use it in unmanaged part of the project, you can wrap your managed "ref" class using C# assembly in unmanaged code and use in other unmanaged code.

(It is inherently difficult to use a CLI assembly in unmanaged code in any other way. (The opposite problem, using unmanaged code in CLI is easy enough.) The usual approach is using COM component, but why contaminate the installation and your code, as well as system registry, with obsolete technology which you don't need in .NET or unmanaged code?

Actually, there is a different way using the fact that IL actually allows to be exported to unmanaged, but this is also more difficult; please see my past answers:
loading C# DLL in MFC[^],
How can I use a dll created in Visual Basic 2008 in Visual Basic 6.0[^],
Call Managed DLL written in C# from Unmanged Code VC++[^],
API's in .Net: Managed or UnManaged Code[^].
So, using C++/CLI, possibly in a mixed-mode project is the best way to solve such problem as soon as C++ is involved.

See also:
http://en.wikipedia.org/wiki/C%2B%2B/CLI[^],
http://www.ecma-international.org/publications/standards/Ecma-372.htm[^],
http://www.gotw.ca/publications/C++CLIRationale.pdf[^].

—SA
 
Share this answer
 
v2
The other answers are right, use C++/CLI for your DLL and it will be able to use the C# DLL without a problem.

The other thing I would add, is that if you need your C++ DLL to be completely native, then you can use a wrapper DLL around the C# DLL to make it work. The wrapper DLL would be C++/CLI and export its entry points in the unmanaged (native) part. I have had to do this for a project where we had a .EXE but no source. The .EXE could run DLLs through a proprietary scripting language, so I was able to make the native .EXE call C# DLLs.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900