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

  • 相关阅读:
    从零开始学Oracle—约束(三)
    ewebeditor安全解决方案 j神
    12款很棒的浏览器兼容性测试工具推荐 j神
    文件无法删除,找不到指定文件-解决办法 j神
    如何应对Global.asa木马 j神
    2012年最佳免费网站和移动应用 PSD 界面素材揭晓 j神
    PHP上传RAR压缩包并解压目录 j神
    数据库查询语句:left join ... on ... j神
    js 与或运算符 || && 妙用 j神
    实时监听输入框值变化的完美方案:oninput & onpropertychange j神
  • 原文地址:https://www.cnblogs.com/SLchuck/p/3068548.html
Copyright © 2011-2022 走看看