Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
I have a dll generated in c++ code which has to be invoked in .net core. And its invoked successfully. But now based on platform, the dll should be invoked. If it is 'windows' platform, it should invoke '.dll' file and if it is 'linux' it should invoke '.so' file . Was looking for many solutions, but couldn't find a working one. Please help.
Thanks.

What I have tried:

using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using System.Runtime;
using System.Reflection;

namespace NativeLibConsumeApp
{
    class Program
    {
        public delegate void CallBackDelegate(int a, int b);

        [DllImport("CallbackThreadSample.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern void GetData();
        [DllImport("CallbackThreadSample.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern void SetCallback(CallBackDelegate aCallback);
        static void Main(string[] args)
        {
            CallBackDelegate lTD = new CallBackDelegate(Program.Callback);
            SetCallback(lTD); //Sets up the callback
            int thread = Thread.CurrentThread.ManagedThreadId;
            GetData(); //This calls the callback in unmanaged code
           // while (true) ;
        }

        //Callback function which is called from the unmanaged code
        public static void Callback(int a, int b)
        {
            Console.WriteLine("A: {0} B: {1}", a, b);
            int thread = Thread.CurrentThread.ManagedThreadId;
        }
    }
}
Posted
Updated 11-Jun-17 21:03pm

You have some real problem here...
In short, you have to compile two instances of your code, one with the .dll file and one with the .so file in it...
Some would say you may write a much more complicated solution, that loads the libraries/methods dynamically and execute them via Invoke, but for that you also need some native methods (LoadLibrary, GetProcAddress) so you are trapped...
 
Share this answer
 
Use different child classes for every platform and a common base class or interface.

Meta code
C#
PWorker worker = null;
if( IsWindow() {
 worker = new WindowWorker();
} else {
  worker = new LinuxWorker();
}
  work.DoMyWork();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900