zoukankan      html  css  js  c++  java
  • C# 调用C++ dll

            [DllImport("kernel32.dll", EntryPoint = "LoadLibrary", CallingConvention = CallingConvention.StdCall)]
            public static extern int LoadLibrary([MarshalAs(UnmanagedType.LPStr)] string lpLibFileName);

            [DllImport("kernel32.dll", EntryPoint = "GetProcAddress", CallingConvention = CallingConvention.StdCall)]
            public static extern IntPtr GetProcAddress(int hModule, [MarshalAs(UnmanagedType.LPStr)] string lpProcName);

            [DllImport("kernel32.dll", EntryPoint = "FreeLibrary", CallingConvention = CallingConvention.StdCall)]
            public static extern bool FreeLibrary(int hModule);

            /// <summary>
            /// 根据dll地址获取委托
            /// </summary>
            /// <param name="dllModule">调用 LoadLibrary后得到的int 值</param>
            /// <param name="functionName">在dll中约定的函数名称</param>
            /// <param name="t">接收dll约定函数的委托</param>
            /// <returns></returns>
            public static Delegate GetDelegateFromAddress(int dllModule, string functionName, Type t)
            {
                IntPtr address = GetProcAddress(dllModule, functionName);

                if (address == IntPtr.Zero)
                    return null;

                return Marshal.GetDelegateForFunctionPointer(address, t);
            }

            /// <summary>
            /// 根据指针获取委托
            /// </summary>
            /// <param name="address"></param>
            /// <param name="t"></param>
            /// <returns></returns>
            public static Delegate GetDelegateFromIntPtr(IntPtr address, Type t)
            {
                if (address == IntPtr.Zero)
                    return null;

                return Marshal.GetDelegateForFunctionPointer(address, t);
            }

  • 相关阅读:
    刷题-力扣-541. 反转字符串 II
    刷题-力扣-515. 在每个树行中找最大值
    刷题-力扣-513. 找树左下角的值
    刷题-力扣-404. 左叶子之和
    刷题-力扣-257. 二叉树的所有路径
    刷题-力扣-226. 翻转二叉树
    刷题-力扣-236. 二叉树的最近公共祖先
    刷题-力扣-235. 二叉搜索树的最近公共祖先
    刷题-力扣-145. 二叉树的后序遍历
    扛把子组2018092609-2 选题 Scrum立会报告+燃尽图 06
  • 原文地址:https://www.cnblogs.com/SLchuck/p/3068548.html
Copyright © 2011-2022 走看看