Exporting C++ code as a DLL and import using C#? - c#

I have a program written in C++ with a main function that calls a bunch of other C++ classes/functions.
I am new to C++ (been a python programmer), so I'm wondering - What are the steps I need to follow to be able to export this as a DLL that is importable from a C# program? Any suggestions?

If you want your C++ classes to be usable in a C# application you will need to use COM or target the CLR in your C++ program (i.e., use C++/CLI).
If you simply have some functions in the C++ DLL that you want to call from C# that take POD type arguments then declare each function as extern "C" to avoid name mangling and use the DLLImport attribute to import the function. PInvoke.net is a great resource here.

If you are only exporting plain functions, not classes, you can p/invoke them. if ypu really need the classes, you have to write your project as a C++/CLI and reference it in your c# project like any other .NET assembly.

Related

VBA runtime error 453 with declare statement [duplicate]

In VS C/C++ you could use extern "C" __declspec(dllexport) -function declaration-.
How do I accomplish this in a C# dll? Is there C# code equivalent to the above code?
Edit: More info
I am trying to create an add in for Notepad++ and I want to use C#, but the common way I've seen so far is to use legacy C++ code with the above call to export a few of the functions that Notepad++ expects to import and call. There is an example app using C#, but this still requires a loader DLL, which I assume from the comments/answers below is the only way for C#.
Unmanaged Exports =>
https://sites.google.com/site/robertgiesecke/Home/uploads/unmanagedexports
DLLExport => https://github.com/3F/DllExport
How does it work?
Create a new classlibrary or proceed with an existing one.
Then add the UnmanagedExports Nuget package.
This is pretty much all setup that is required.
Now you can write any kind of static method, decorate it with [DllExport] and use it from native code.
It works just like DllImport, so you can customize the marshalling of parameters/result with MarshalAsAttribute.
During compilation, my task will modify the IL to add the required exports...
I've seen people do this before, but it required ildasm, adding the MSIL .export directive, and then reassembling. A program named dll_tool can do these steps for you.
If you want to build a mixed-mode DLL with both native and managed exports, you should be using C++/CLI, which is specially designed for this purpose.
Yes, it is possible to export functions from a C# dll in much the same way that C++ does it! You need a little help from an add-in Unmanaged Exports (DllExport for .Net) that facilitates this process, or from a similar method such as Exporting functions in C#/VB.NET to native code.
Please see Code to Export C# DLL to Metatrader Build 600+ for a working example using Robert Giesecke's C# Project Template for Unmanaged Exports to export a C# dll to a legacy application (Metatrader) that has a great deal of similarity to C++.
Additionally, you might find Native and .NET Interopability interesting though it is mostly geared toward accessing native code from within .NET rather than the other way around.
No, you cannot do that in the same sense as you do in C and C++.
But you can create COM API to achieve that which then you can use in C and C++ code.
See these articles
C# Classes as COM Objects
Calling Managed .NET C# COM Objects from Unmanaged C++ Code
COM Interop Part 1: C# Client Tutorial

C++ DLL to be used in C# or Java

I am very new to C++ programming...
I was assigned an assignment to do a SkipList in C++ that will do some functionalities..
Apart from these functionalities, there was also the following included:
Export the Skip List to a .DOT file that can be rendered in GraphViz
Any ideas how I can do this?
Also, I need to export the DLL so that I can use it in either Java or else in C#..
any help is greatly appreciated
Thanks
If you're looking to use C++ code in Java, check out JNI. It requires you to modify your C++ code with JNI wrappers (see the Wikipedia link for examples).
C#, on the other hand, can call DLL functions which are exported normally, i.e. with extern "C" __declspec(dllexport), via PInvoke.
The Java has got a JNI technology
In fact it's c/c++(with extern "c" calls) library with some specific types and rules
And yes you can use your dll in Java with some jni bridge

Creating an instance of a C++ class in a .lib file from C#

As the description states. I have some static .lib files created in other rather large c++ projects containing a class I would like to be able to use in C#. What would the best way to use these classes from inside c#. The C++ libraries are not managed.
By far the easiest option is to build a wrapper library in C++/CLI.
I am afraid class exporting from native .lib files is not possible inside C#.
But using C++ Cli, you could wrap the class and make use of it via wrapper .net class. But this approach also requires a header (.h or .hpp) file to included.
COM dlls are different story. They can be referenced in a C# project and MSVS automatically wrapps the native classes.
What #marcelo-cantos said: use C++/CLI as a bridge between the managed and unmanaged worlds. It is easy.
The only other options you have are:
(1) Write a wrapper DLL that exports extern "C" functions that call onto your existing C++ objects. Then use [DllImport] and PInvoke in C# to call the DLL's C functions.
(2) Wrap your native C++ classes in COM interfaces and use C# COM Interop to invoke methods on the classes.
The C++/CLI bridge is by far the easiest option.

How to call c# code from Fortran dll

Currently I have abc.dll which is fortran dll. Now I want to call C# code from abc.dll. Is there any way to call the C# code from fortran dll ?
thanks
Sagar
Typically, if your program is written entirely in native code (as I believe the Fortran dll would be), you'll need to call a method that's been exported (dllexport) from another native code module. In this case, you'll want to create a Managed C++ dll that exposes a native interface and internally makes the call into the C# code.
Edit: If the hosting program is managed code, and you need to do a C#->Fortran (native)->C# calling sequence, then delegates as unmanaged function pointers can be used as linked in the comments above. However, if the executable is not managed code, you'll need to go the route I mentioned.
Compilers supporting recent Fortran language features (the 2003 standard) will support C-interoperation. You interface with other code through its C interface, using the ISO_C_BINDING module and the BIND construct. Most recent compilers have it, it's standard and you can find a lot of documentation (like this one) by Google'ing the keyword ISO_C_BINDING.

how to import a header file into a C# project which imports a C++ dll

The header file used by the C++ dll contains a few user defined type definitions and function declarations.I have browsed many sites and all the posters say that its not possible to import header file into C#.I would like to know if there is any way to import header file into C# code as it is required to declare the functions of the imported dll in the C# application class.
Thanks
This is more or less a duplicate of Importing a C++ dll in C# however that question has some poor "information lite" answers.
Unfortunately there is no short answer to this, however you do have several options.
Wrap your C++ class in a managed C++
class. Then your C# project just
references the managed C++ project,
and everything works.
Like 1, create a managed C++ class,
but create a facade. This a
simplified interface to your class
exposing only the functionality you
need.
The other approach is to use PInvoke
to call the methods on the class.
You'd need to built a C#
representation of the class for the
pinvoke calls. This can be
problematic and involves a lot of
trial and error if you don't know
what you're doing.
All of the above involve learning some technologies that will be new to you (manged C++ or the intracies of PInvoke). Unfortunatly there's no other way.
If you can I'd go with 2.
Have you considered bridging this gap with a C++/CLI project? If you write all your wrapper library and interoperability code in a C++/CLI project you can easily define managed types that expose the defines in C/C++ header files.
When I have submbled upon the same problem - given .h and .lib files intended for C/C++ use, I have written a wrapper for it in C++ with managed extensions. It is really quite simple. Your C++ assembly will play neatly in your Visual Studio solution and you'll be able to single step right from C# into managed C++ into unmanaged C++ and back again light as a breeze! :-)

Categories

Resources