Unity/C#加载C++(非托管)程序集

常用的Dllimport方式

暂时省略

使用Api进行加载

工具类:

 public class DLLWrapper
    {
        ///<summary>
        /// API LoadLibrary
        ///</summary>
        [DllImport("Kernel32")]
        public static extern Int64 LoadLibrary(string funcname);

        ///<summary>
        /// API GetProcAddress
        ///</summary>
        [DllImport("Kernel32")]
        public static extern Int64 GetProcAddress(Int64 handle, string funcname);

        ///<summary>
        /// API FreeLibrary
        ///</summary>
        [DllImport("Kernel32")]
        public static extern Int64 FreeLibrary(Int64 handle);

        ///<summary>
        ///通过非托管函数名转换为对应的委托, by jingzhongrong
        ///</summary>
        ///<param name="dllModule">Get DLL handle by LoadLibrary</param>
        ///<param name="functionName">Unmanaged function name</param>
        ///<param name="t">ManageR type对应的委托类型</param>
        ///<returns>委托实例,可强制转换为适当的委托类型</returns>
        public static Delegate GetFunctionAddress(Int64 dllModule, string functionName, Type t)
        {
            Int64 address = GetProcAddress(dllModule, functionName);
            if (address == 0)
                return null;
            else
                return Marshal.GetDelegateForFunctionPointer(new IntPtr(address), t);
        }

        ///<summary>
        ///将表示函数地址的IntPtr实例转换成对应的委托
        ///</summary>
        public static Delegate GetDelegateFromIntPtr(IntPtr address, Type t)
        {
            if (address == IntPtr.Zero)
                return null;
            else
                return Marshal.GetDelegateForFunctionPointer(address, t);
        }

        ///<summary>
        ///将表示函数地址的int转换成对应的委托
        ///</summary>
        public static Delegate GetDelegateFromIntPtr(int address, Type t)
        {
            if (address == 0)
                return null;
            else
                return Marshal.GetDelegateForFunctionPointer(new IntPtr(address), t);
        }
    }

notice: 作者加载的是64程序集,所有handle采用int64进行接收。如果32程序集,可使用int(int32)
但64位必须使用(int64),使用int32 进行内存寻址默认高32位全部补0寻址错误。

函数调用

class Program
    {
        static void Main(string[] args)
        {
            string dllpath = "NetCom.dll";
            Console.WriteLine(dllpath);
            Int64 handle = hModule(dllpath);
            bool b =ConnectToModel(handle, "224.0.0.0",4196, "./MMI1");
            Console.WriteLine(b.ToString()+handle);
            DLLWrapper.FreeLibrary(handle);
            Console.ReadKey();
        }

        private static Int64 hModule(string dllpath)
        {
            Int64 _hModule = DLLWrapper.LoadLibrary(dllpath);
            if (_hModule == 0)
            {
                return 0;
            }

            return _hModule;

        }

        delegate bool _connectToModel(string ip,int port,string dir);

        private static bool ConnectToModel(Int64 handle,string ip, int port, string dir)
        {
            try
            {
                _connectToModel connect = (_connectToModel) DLLWrapper.GetFunctionAddress(handle, "ConnectToModel", typeof(_connectToModel));
                return connect(ip,port,dir);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
    }

疑问请各位指教

在64位操作系统中,内存为4g大小,是否采用int32也可以正常使用?
2022/09/05 经过一端时间的了解 windows操作系统,真实内存大小与应用程序无关,应用程序寻址采用VA。此问题过于小白,关闭。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zheenyuan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值