如何在.NET核心中加载基于平台的原生DLL [英] How to load native DLL based on platform in .NET core

查看:83
本文介绍了如何在.NET核心中加载基于平台的原生DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个用c ++代码生成的dll,必须在.net核心中调用。并且它成功调用了。但是现在基于平台,应该调用dll。如果它是'windows'平台,它应该调用'.dll'文件,如果它是'linux',它应该调用'.so'文件。正在寻找许多解决方案,但找不到合适的解决方案。请帮忙。

谢谢。



我的尝试:



使用System; 
使用System.IO;
使用System.Runtime.InteropServices;
使用System.Threading;
使用System.Runtime;使用System.Reflection
;

名称空间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); //设置回调
int thread = Thread.CurrentThread.ManagedThreadId;
GetData(); //这将在非托管代码中调用回调
// while(true);
}

//从非托管代码调用的回调函数
public static void Callback(int a,int b)
{
Console。 WriteLine(A:{0} B:{1},a,b);
int thread = Thread.CurrentThread.ManagedThreadId;
}
}
}

解决方案

这里有一些真正的问题...

简而言之,你必须编译代码的两个实例,一个包含.dll文件,另一个包含.so文件...

有人会说你可能编写一个更复杂的解决方案,动态加载库/方法并通过Invoke执行它们,但为此您还需要一些本机方法(LoadLibrary,GetProcAddress),以便您被困......


< blockquote>为每个平台和公共基类或接口使用不同的子类。



元代码

 PWorker worker =  null ; 
if (IsWindow(){
worker = new WindowWorker();
} else {
worker = new LinuxWorker();
}
work.DoMyWork();


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;
        }
    }
}

解决方案

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...


Use different child classes for every platform and a common base class or interface.

Meta code

PWorker worker = null;
if( IsWindow() {
 worker = new WindowWorker();
} else {
  worker = new LinuxWorker();
}
  work.DoMyWork();


这篇关于如何在.NET核心中加载基于平台的原生DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆