Exporting a native C function from a .net DLL? [duplicate]
Asked Answered
K

4

15

I have a .net assembly written in C#, and I'm looking to export a native C function from the assembly.

I have an application which will perform a 'LoadLibrary()' on any DLL's found in a 'plugin' folder. After loading the DLL, the application performs a 'GetProcAddress' looking for a function named 'Register'. The application expects this function to follow the C calling convention.

How can I export a function named 'Register' from my .net assembly, so I can successfully hookup with the plugin system for this application?

Thanks, Andrew

Krenek answered 13/5, 2011 at 14:52 Comment(0)
G
11

Have a look at Unmanaged Exports.

Getup answered 13/5, 2011 at 14:57 Comment(3)
What is this tool. The link is blocked for me on my network.Brainard
@CathalMF: nuget.org/packages/UnmanagedExportsGetup
Note that the link in the answer is dead. The nuget package URL from @Getup is still valid, at this time. It targets .net framework 2.0 and doesn't work well with SDK style projects using PackageReference. This package is a somewhat newer fork of the latest version of the original and at least targets .net Framework 4.7.2 and netstandard2.0. The version says 1.2.1 for that package but it's built from the 1.2.7 source, after also having been forked and updated to .net framework 4 by UnmanagedExports.Repack.Quinze
O
3

Write a .Net library in Managed C++ and there you can export a "Native" method/function.

Obsequies answered 18/6, 2011 at 16:4 Comment(2)
it's doable but mixed mode C++ SUCKS!Ethbinium
That it does. And it's not portable. And C++/CLR is still PInvoke under the hood. It still marshals back and forth between the managed CLR and native, which is all PInvoke really is, but is a necessary evil. You just don't see the PInvoke work because it's implicit and thus hidden from you. The performance implications of every managed/unmanaged transition are the same as doing it from C# with explicit PInvoke. The only real advantage is you can stay in unmanaged-land for longer and do more "stuff" before having to come back to managed, unlike C#, where you can only do one call at a time.Quinze
C
0

What you want is a Reverse P/Invoke. You can't actually embed a C function in a C# dll, if by that you mean a function actually implemented in C, but by following the tutorials given on the linked page you can create a DLL export that's callable by unmanaged C/C++ code.

Conditioning answered 13/5, 2011 at 14:55 Comment(1)
None of those answers address his needs; he isn't using COM.Getup
E
0

Sadly Microsoft does not support this feature, and you have to change the msil after the build to expose those methods. It is possible as one guy has shown a reasonable solution on codeproject or here, but it requires a post build step and you are on your own after that. I don't know if this hack will work on .net 4.0 or later though. Hopefully Microsoft will listen to us and support this simple feature in C# since the CLR support is already there.

Ethbinium answered 7/7, 2011 at 16:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.