DEV Community

Loading Native Libraries in C#

jeikabu on September 09, 2018

While possible for C# to call functions in native libraries, you must avoid mixing 32 and 64-bit binaries at runtime lest you invite the wrath of B...
Collapse
 
isatindersingh profile image
Satinder Singh

Hi,
I must say this is nice detailed article, well I just came to know about a broken link in this post which pointing to one of my article. Can you update it with working link
i.e codepedia.info/dotnet-core-to-dete...
instead of
json-formatter.codepedia.info/dotn...

Thanks & Happy Coding

Collapse
 
jeikabu profile image
jeikabu

Done

Collapse
 
paulcc profile image
paulcc • Edited

Hello, I found your article while searching for solutions to the DllImport style difference in calling native code ( C/C++ ) between Windows and iOS.
I am trying to get common .netstandard2.0 implementation of native interface that needs to work both on Windows and iOS.
From reading your text, I got the impression that it's possible on Windows to preload a dll, which somehow makes the symbols from the DLL available, and then use the P/Invoke DllImport("__Internal") style. My thinking is that on the Windows I'd preload the DLL, and then proceed to using the same DllImport("__Internal") style, thus getting a single C# implementation working on both targets.
In my tests on Windows, the DllImport("__Internal") throws an exception complaining that the DLL does not exist - so it seems the "__Internal" is taken as DLL file name.

Could you, please, tell me if I understood your text correctly, and if so, suggest the simplest test code.
I am a C++ programmer just trying to figure this out....
Thanks, Paul.

Collapse
 
eri0o profile image
eri0

This was a great post, thanks for writing it. Running native code, with cross platform builds on C# and .NET is something that I couldn't find much material on the internet.

Collapse
 
jeikabu profile image
jeikabu

Glad you enjoyed it. Wish I had more time to investigate it further.

Collapse
 
borrrden profile image
Jim Borden

Glad somebody else is worry about this and not just me. In the end the "copy the files with nupkg script and then load based on IntPtr size" has served me pretty well!