Is Is Possible to Export Functions from a C# Dll Like in VS C++

Is is possible to export functions from a C# DLL like in VS 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...

How to export C# dll method/functions to use it in C++

Don't. Exporting methods from C# is not supported. It is just barely possible by manually editting the emitted assembly, but really - don't.

There's a few proper solutions you could use:

  • Use a C++/CLI project to expose the managed methods to unmanaged code
  • Use COM (.NET is basically COM 2.0, so this is very easy; it's also quite easy to consume on the C++ side)
  • Pass a delegate to the unmanaged code on initialization, and call that when needed
  • Use some other method of inter-process communication (e.g. named pipes, TCP...)

Exporting a native C function from a .net DLL?

Have a look at Unmanaged Exports.

Exported functions from C# DLL not working

Your code works fine, apart from the fact that the code you posted does not compile. You omitted the using System.Runtime.InteropServices line. Dependency Walker for an x86 class library build of your (fixed) code says this:

Sample Image

The most obvious cause for the problem could be the following from the NuGet page for the library:

You have to set your platform target to either x86, ia64 or x64. AnyCPU assemblies cannot export functions.

Exporting functions from C++ dll to C# P/Invoke

Are you using a .def file in your dll project to export those functions? If so, remove it and try again. This is just a guess because it looks like your exports are not what they should be when you do an extern "C" declspec(dllexports).

I tried this out with a simple C++ dll using

extern "C" __declspec(dllexport) BOOL Install();
extern "C" __declspec(dllexport) BOOL PPPConnect();

and a simple C# app using your PInvoke declarations and it worked fine.

When I did a dumpbin/exports on the dll I saw:

Dump of file PPPManager.dll

File Type: DLL

Section contains the following exports for PPPManager.dll

00000000 characteristics
499F6C2D time date stamp Fri Feb 20 20:51:25 2009
0.00 version
1 ordinal base
2 number of functions
2 number of names

ordinal hint RVA name

1 0 000110CD Install = @ILT+200(_Install)
2 1 00011069 PPPConnect = @ILT+100(_PPPConnect)

Notice that the exported names are different in my case.

How can I make a library face to any other program language?

You can use DllExport to generate exports like you would do with for example C. Any language that has some form of C bindings can then call your exported functions.

how to call a C++ dll exported function from c#

The default calling convention for DLLImport is stdcall, but the default of your C++ code is cdecl. The error message you have seen is what is shown when the calling conventions don't match. The parameter stack cleanup requirements are different for these two calling conventions, and the P/Invoke marshaller detects and reports this.

The fix is to make your calling conventions match.

For example you could change your P/Invoke like so:

[DllImport("firstDLL.Dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int Add(int a, int b);

The other option is to change your C++:

#if defined FIRSTDLL_EXPORTS(returntype)
#define DECLDIR __declspec(dllexport) returntype __stdcall
#else
#define DECLDIR __declspec(dllimport) returntype __stdcall
#endif

Clearly you should only do one of these. If you change both C# and C++ you'll have the same problem in reverse!

If I were you I would leave the C++ code as cdecl and change the C# to match.



Related Topics



Leave a reply



Submit