Microsoft has an article dedicated to working with native codes in .Net Core on various platforms. Yes, they give some examples, but they do not agree much about it. For example, here they call a function from WinAPI to display a message on the screen enter image description here But if the dll is compiled with a 32-bit compiler, for example, the standard bundled Code :: Blocks on Windows with MinGW32, then the program flies with the exception of BadImageFormatException enter image description here I would like to see a LIVE example of working with native code on the example of access to pop-up notifications on Ubuntu and Windows 10

  • Judging by your own answer, the exception crashed because you were trying to load a 32-bit dll into a 64-bit process. Attribute DllImport and others like him appeared not yesterday and is described in detail in the documentation. There is no "much is said" no. - VTT
  • @VIT For me, there is a difference in calling the native code and the part of the dynamically connected library, since this native code still needs to be compiled ... - Tripolsky Peter

1 answer 1

I left the link to download the whole example at the very bottom of the answer to the question

Linux

Everything is simple, on a 64-bit system, gcc will by default compile 64-bit binaries. Accordingly, the task is reduced to writing the simplest sishnaya function, the code below.

 #include <stdio.h> #include <libnotify/notify.h> int notify(const char *title, const char *description,int timeout) { notify_init("dcnn"); NotifyNotification* n = notify_notification_new (title, description, 0); notify_notification_set_timeout(n, timeout); if (!notify_notification_show(n, 0)) { printf("Notification fallen"); return -1; } printf("Notificated"); //notify_uninit (void); return 1; } 

Nothing complicated. Do not peer into the content, it is just a function that can return a value. main () is by the way not needed, as we will later render it in .so, which itself will not run. Compile with gcc with the following parameters:

 gcc -Wall -shared -o native.so -fPIC native.c `pkg-config --cflags --libs gtk+-2.0 libnotify` -lnotify 

It is important to compile with one command. Yes, it is possible to first get the object modules .o, but the compiler will surely forget to add something ... I also recommend that you try to make a dynamic import of the library from another program in the repository in the ubuntu folder in the ubuntu folder its implementation. But if you can write right away, you can skip it.

After the build, we get the native.so file. It needs to be thrown into the folder with the assembly for .Net Core. By default, my build appeared in the directory

 DotnetCoreNativeNotifications/bin/Debug/netcoreapp2.0/ 

Further this business is called simply. Just like in MS documentation

 public class UbuntuNotify : AbstractNotify { public UbuntuNotify(){} [DllImport("native.so")] private static extern int notify([MarshalAs(UnmanagedType.LPStr)]string title,[MarshalAs(UnmanagedType.LPStr)]string description, int timeout); public override void Notify(string title, string desc, int timeout) { UbuntuNotify.notify(title,desc,timeout); } } 

In Program.cs, I check the if-th platform, create an instance of this class, and call the Notify method.

enter image description here

Windows

To get started, choose the right software for application programming. I do not recommend trying to write a dll in Visual Studio. In my opinion, it is sharpened EXACTLY under C # and the process of writing a dynamically connected library in it will be delayed for a long time, as there is no stupid project template collecting dll

Take the Code :: Blocks, http://www.codeblocks.org/

Next, we put a 64 bit compiler on it. This is important, without it .Net Core will find the dll incorrect

Article how to put this here, https://medium.com/@yzhong.cs/code-blocks-compile-64-bit-under-windows-with-mingw-w64-79101f5bbc02

Create project dll, C ++ language enter image description here

Don't forget to choose the RIGHT compiler. enter image description here

Next, it will generate enough readable code. You can see the dll code that displays notifications on Windows here https://github.com/tripolskypetr/DotnetCoreNativeNotifications/blob/master/windows/native/main.cpp

After finished writing the dll, just throw it into the folder with the assembly. Net Core, for the conversion, I implemented the following class

 public class WindowsNotify : AbstractNotify { public WindowsNotify(){} [DllImport("native.dll", CallingConvention = CallingConvention.Cdecl)] private extern static void NotifyFunc([MarshalAs(UnmanagedType.LPStr)]string title, [MarshalAs(UnmanagedType.LPStr)]string desc); public override void Notify(string title, string desc, int timeout) { WindowsNotify.NotifyFunc(title,desc); } } 

As a result, we got the .Net Core project, which brought us a notification on both Ubuntu and Windows 10. Miracles enter image description here

IMPORTANT: Dozens of different versions may not work, since WinAPI is used and it also changes ... My computer runs on version 1703, it is quite old. I recommend to try the same example, but in the dll on Windows, call the MessageBoxA function as in the dll pattern of the Code :: Blocks environment, I hope it won't get to small ones. About linux, I am calm ...

 void DLL_EXPORT SomeFunction(const LPCSTR sometext) { MessageBoxA(0, sometext, "DLL Message", MB_OK | MB_ICONINFORMATION); } 

View the full code of this example here https://github.com/tripolskypetr/DotnetCoreNativeNotifications

  • “They don’t finish speaking about MANY” - and what unspoken moments have you revealed here? "stupidly there is no project template that collects dll" oh well? - VTT
  • @VTT mandatory 64 bit compiler on Windows for dll, for example ... - Tripolsky Peter
  • Nothing like this. VS out of the box allows you to collect 32-bit and 64-bit dll. - VTT
  • @VTT MSVC can build, of course. But it seemed to me not obvious requirement for the bit finite dll to work from. Net Core. - Tripolsky Peter
  • How is this "not obvious"? If you compile a 32-bit .Net Core application, then it can load libraries only 32-bit, if you compile a 64-bit .Net Core application, then it can load libraries only 64-bit ones. Has it ever been different? - VTT