I have Xamarin forms cross platform project - Android, iOS and UWP. I have a C++ DLL project from which I have created a DLL. My intention is to integrate/call these C++ code within Xamarin forms cross. I know how to call C++ from C# Console application with DLLImport
assembly directive, but on Xamarin I get DllNotFoundException
.
So where the DLL file has to be copied or referenced for this to work?
- I have copied
.dll
and other files in Debug folder(where the current project is trying to reference DLL. (I referred this video and it works for C# console ) - Even tried Add Reference to DLL file - gives error cannot add file
- Adding C++ DLL project to same solution and referencing it is giving
System.DllNotFoundException
. - Created cross platform shared C++ (I think this is comes in NDK)library project but still get the same issue -
DllNotFoundException
.
Code :
C++
#include "stdafx.h" extern "C"{ __declspec(dllexport) int add(int a, int b) { return a + b; } __declspec(dllexport) int subtract(int a, int b) { return a - b; } }
App.xaml.cs or any other .Net standard shared project
public partial class App : Application { [DllImport("Dll1.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int subtract(int a, int b); [DllImport("Dll1.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int add(int a, int b); [DllImport("XamCppDll.Shared.so")] public static extern int cpp_GetValue(int a, int b); public App() { InitializeComponent(); int x = 100; int y = 15; int z = 0; int z1 = 0; try { cpp_GetValue(5, 6);//XamCppDll.Shared.so z = subtract(x, y);//Dll1.dll z1 = add(x, y);//Dll1.dll } catch (System.Exception ex1) { Debug.WriteLine(ex1.Message); } Debug.WriteLine(" {0} - {1} = {2}", x, y, z); Debug.WriteLine(" {0} + {1} = {2}", x, y, z1); MainPage = new MainPage(); }
}
Project structure
Tutorials/codes referred
https://blogs.msdn.microsoft.com/vcblog/2015/02/23/developing-xamarin-android-native-applications/
D/Mono (20164): DllImport attempting to load: 'libXamCppDll.so'. W/Mono (20164): DllImport unable to load library 'dlopen failed: library "/data/app/com.companyname.C__Test-1/lib/arm64/libXamCppDll.so" not found'.
– Morse