Writing unmanaged C++ DLLs for use in C# - c#

I'm a Linux C++ developer, and I need to write C++ dlls for windows, which is to be used in C# applications.
The problem is importing the DLLs into C#, which I have no idea how to do it. In my friends' project its probable that any kind of unmanaged dll will be used, and I'm charge of doing this :-D
I need to import all objects and functions in the DLL, and my search has led me to nothing more than DllImport and so.
Thanks so much for helps.

You can use C++/CLI as a wrapper for your unmanaged C++.For more info on C++/CLI and what it does you can use this link
http://www.functionx.com/cppcli
You can have a quick look at this
http://www.codeproject.com/Articles/19354/Quick-C-CLI-Learn-C-CLI-in-less-than-10-minutes#A8

you want DllImport
There's a bunch of info here :-
http://msdn.microsoft.com/en-us/library/aa288468(v=vs.71).aspx
and lots lots more all over google, and many stackoverflow questions related to DllImport

There are many ways to go about this. You can write a CLR dll in C++ which puts an interface that C# can directly "talk" to. This is a nice option cause you can keep native C++ still and not have all of your code be CLR based.
Do some searches for C++/CLI
You can also use dllimport and friends and create a standard dll.

Have you thought to keep the Dll's in C++ (port then recompiled under VS.NET) less effort than a C# port.
I have in the past made a shared memory DLL to allow LabVIEW (Windows7) and a Winforms C#.NET appl. to share data storage via this dll.

Take a look at this tutorial. It shows two methods for accomplishing what you want: How to Marshal a C++ Class

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 in __cdecl conventions

My bos, who is not a CS guy, asked me to develop a dll in c# for him.
further investigation on my side reveal that the dll should be in cdecl conventions.
If I'll take this project it will be my first dll development.
browsing through google tell me this is possible, but before I tell the bos
I can do it, I wanted to get your advice regarding this issue.
(lets assume nor time or code-quality are the main issue, but it needs to be done)
Any comments regarding such possibility would be much appreciated.
It sounds like you are looking for unmanaged function exports. There's no out-of-the box way to achieve that in C#. You can use Robert Giesecke's Unmanaged Exports. This performs some magic during compilation to modify the emitted IL to allow unmanaged exports.
The officially supported way to export unmanaged functions from a .net DLL is to make a mixed mode C++/CLI assembly.
Yet another way to export your functionality to unmanaged code is to expose a COM server from your C# code.

Read C# functions in C++ project

Good day,
I want to take an existing C# project and wrap functions into a C++/CLI DLL. I need to be able to read this DLL from VB 6. I choose this route because I won't have to register a .net DLL in order to use it with VB 6. Frankly I have no experience with this kind of thing so I would greatly appreciate a good example. I know there are plenty of similar questions like this but I haven't been able to find anything simple enough for me to understand.
This is a fairly unwise route to pursue, making C# code [ComVisible] is pretty trivial and Regasm.exe should not scare you. The usual mistake with Regasm is to forget to use its /codebase option on your dev machine.
If you insist on not taking advantage of this then you'll need to find a way to get the CLR loaded yourself so it can execute your C++/CLI and C# code. There are three basic ways to do so. You've written off COM interop and hosting the CLR yourself isn't very practical if the host app is VB6. You however can take advantage of the C++/CLI compiler's ability to generate unmanaged DLL entrypoint stubs that load the CLR for you and switches to managed code execution. Do so by writing a static function that you decorate with __declspec(dllexport). The technique is shown in this answer. Beware that it isn't particularly performant and you'll have to live with the restrictions imposed by the VB6 Declare statement.
Also check this post-processing tool that can inject these stubs directly into a C# assembly. Not sure how reliable it is, I haven't used it myself.
Be a man and make that DLL under a couple of different compilation/linking/function-call-protocol configurations. Then go ahead and try a couple of different referencing configurations in your VB6 project. Post what works. Contribute, go forth!
Take a look at Export Managed Code as Unmanaged
and C# Project Template for Unmanaged Exports

Creating C++ Dll, and call it from C#

In my project I got a device which comes with C++ Sample codes. The codes are ok and the device is working as expected.
But I need it to talk with my C# interface because all other devices are currently using C# interface.
So I am planning to create a DLL Wrapper for the driver. I will create a C++ Library of my own (from source code with proper interface) and Call this C++ Library from C# using DLLImport (just call my interfaces there.).
I am pretty sure it can be done this way, but I have never created a C++ Library and used it from C# yet. So, can anyone refer me to some tutorial that goes with my problem?
I am using C++/C# int VS.NET 2008.
Regards,
Maksud
Have a look at
using a class defined in a c++ dll in c# code
Another useful tool you have at your disposal is C++ CLI.
You can use C++ CLI to create an intermediate library - one that exposes managed classes but runs unmanaged C++ code. You can actually mix managed and unmanaged C++ in the same DLL.
The unmanaged portion can accesses the unmanaged DLLs without having to use the PInvoke functions.
Your C# code can access the managed classes in this intermediate library.
Depending on the DLL and what you need to do you may not need to create a wrapper directly. You might be able to get away with P/Invoke for the functions. You will need to evaluate your specific needs and what is already available in the libraries/code provided.
For anyone who comes to this question and are looking for answers, you may want to try xInterop NGen++ , a C# wrapper generator for native C++ DLL, which has been just released to the public, the tool can generate C# wrapper for native C++ DLL automatically and instantly by using advanced P/Invoke technologies. Check out the current version and a free version will be out soon.
(I am the author of the tool)

Call C# methods from C++ without using COM

Is there a way to create C# objects and call methods from unmanaged C++, but without using COM Iterop? I am looking for something like JNI (but for .Net), where you can manually create the VM, create objects, etc.
If you are using C++/CLI then you can interact directly with both the managed world and unmanaged code, so interop is trivial.
You can also host the CLR yourself, and whilst the hosting API is COM based, you can then create any managed object. The process isn't a difficult as it sounds as a few API calls encapsulate a lot of functionality. There is a lot of info online, for example the MSDN documentation on "Hosting the Common Language Runtime".
There is a somewhat "undocumented" way of exporting C style API from a .NET class / method.
This ultimately leads to a situation where a .NET dll has exported APIs that can be called from C/C++ or anything that can consume .DLLs for that matter.
If you are into "reading" (beh ;) you can get a book called: Inside Microsoft® .NET IL Assembler where you'll find this technique in chapter 15: "Managed Methods as Unmanaged Exports"
There's also a nice example project on code-project you can use as a starting point for 32-bit environments:
http://www.codeproject.com/KB/dotnet/DllExport.aspx
You can decide file-by-file in your C++ project whether or not to use managed C++. Try changing the settings a file in your project so that it compiles as managed. Put the calls there to your C# object.
There's a cost to crossing the C++/C# border, so you should analyse where to do it. Like, you wouldn't want to do it inside a loop.
I am author of jni4net, open source intraprocess bridge between JVM and CLR. It's build on top of JNI and PInvoke. No C/C++ code needed. I hope it will help you.

Categories

Resources