Пример #1
0
        protected internal Win32Plugin(IntPtr moduleHandle)
        {
            const string PluginNameFuncName        = "Plugin_Name";
            const string PluginDisplayNameFuncName = "Plugin_DisplayName";
            const string PluginVersionFuncName     = "Plugin_Version";
            const string PluginMinVersionFuncName  = "Plugin_MinVersion";
            const string PluginDescriptionFuncName = "Plugin_Description";
            const string PluginGuidFuncName        = "Plugin_Guid";

            Win32Utility.MarshalDelegateFromFunctionPointer(out this.PluginNameFunc, Win32Utility.GetProcAddress, moduleHandle, PluginNameFuncName);
            Win32Utility.MarshalDelegateFromFunctionPointer(out this.PluginDisplayNameFunc, Win32Utility.GetProcAddress, moduleHandle, PluginDisplayNameFuncName);
            Win32Utility.MarshalDelegateFromFunctionPointer(out this.PluginVersionFunc, Win32Utility.GetProcAddress, moduleHandle, PluginVersionFuncName);
            Win32Utility.MarshalDelegateFromFunctionPointer(out this.PluginMinVersionFunc, Win32Utility.GetProcAddress, moduleHandle, PluginMinVersionFuncName);
            Win32Utility.MarshalDelegateFromFunctionPointer(out this.PluginDescriptionFunc, Win32Utility.GetProcAddress, moduleHandle, PluginDescriptionFuncName);
            Win32Utility.MarshalDelegateFromFunctionPointer(out this.PluginGuidFunc, Win32Utility.GetProcAddress, moduleHandle, PluginGuidFuncName);

            const string VersionMajorFuncName    = "Version_Major";
            const string VersionMinorFuncName    = "Version_Minor";
            const string VersionRevisionFuncName = "Version_Revision";
            const string VersionDateFuncName     = "Version_Date";
            const string VersionPeriodFuncName   = "Version_Period";

            Win32Utility.MarshalDelegateFromFunctionPointer(out this.VersionMajorFunc, Win32Utility.GetProcAddress, moduleHandle, VersionMajorFuncName);
            Win32Utility.MarshalDelegateFromFunctionPointer(out this.VersionMinorFunc, Win32Utility.GetProcAddress, moduleHandle, VersionMinorFuncName);
            Win32Utility.MarshalDelegateFromFunctionPointer(out this.VersionRevisionFunc, Win32Utility.GetProcAddress, moduleHandle, VersionRevisionFuncName);
            Win32Utility.MarshalDelegateFromFunctionPointer(out this.VersionDateFunc, Win32Utility.GetProcAddress, moduleHandle, VersionDateFuncName);
            Win32Utility.MarshalDelegateFromFunctionPointer(out this.VersionPeriodFunc, Win32Utility.GetProcAddress, moduleHandle, VersionPeriodFuncName);
        }
Пример #2
0
        /// <summary>
        /// 将导出函数封装到对应委托对象中。
        /// </summary>
        /// <typeparam name="T">对应委托类型。</typeparam>
        /// <param name="func">对应委托对象。</param>
        /// <param name="funcName">导出函数名称。</param>
        /// <param name="funcHandle">Win32Dll中对应导出函数的地址。</param>
        /// <param name="errorMessage">当参数<paramref name="funcHandle"/>指定的导出函数的函数指针封装为托管委托对象失败时抛出的异常的信息。</param>
        /// <exception cref="ArgumentException">
        /// <para>泛型类型<typeparamref name="T"/>不是从<see cref="Delegate"/>基类派生而来的委托类型。</para>
        /// <para>泛型类型<typeparamref name="T"/>是<see cref="Delegate"/>基类自身。</para>
        /// </exception>
        internal static void MarshalDelegateFromFunctionPointer <T>(out T func, string funcName, IntPtr funcHandle, string errorMessage = null) where T : class
        {
            if (!typeof(Delegate).IsAssignableFrom(typeof(T)) || typeof(T).Equals(typeof(Delegate)))
            {
                throw new ArgumentException(string.Format("泛型类型{0}必须为委托类型且不能为{1}", typeof(T).FullName, typeof(Delegate).FullName), nameof(T));
            }

            Win32Utility.MarshalDelegateFromFunctionPointer(out func, funcHandle, errorMessage ?? string.Format("导出函数{0}的函数指针封装为托管委托对象失败。", funcName));
        }
Пример #3
0
        /// <summary>
        /// 将导出函数封装到对应委托对象中。
        /// </summary>
        /// <typeparam name="T">对应委托类型。</typeparam>
        /// <param name="func">对应委托对象。</param>
        /// <param name="getProcAddressFunc">封装GetProcAddress系统API的委托对象。</param>
        /// <param name="moduleHandle">导出函数所属的DLL模块句柄。</param>
        /// <param name="funcName">导出函数名称。</param>
        /// <param name="errorMessage">当无法获取导出函数地址时抛出的异常的信息。</param>
        /// <exception cref="ArgumentException">
        /// <para>泛型类型<typeparamref name="T"/>不是从<see cref="Delegate"/>基类派生而来的委托类型。</para>
        /// <para>泛型类型<typeparamref name="T"/>是<see cref="Delegate"/>基类自身。</para>
        /// </exception>
        /// <exception cref="Win32Exception">
        /// <para>无法获取参数<paramref name="funcName"/>指定的导出函数的地址。</para>
        /// <param>若指定了参数<paramref name="errorMessage"/>的值且不为<see langword="null"/>,则异常的<see cref="Exception.Message"/>属性值为参数<paramref name="errorMessage"/>的值。否则,为默认值。</param>
        /// </exception>
        public static void MarshalDelegateFromFunctionPointer <T>(out T func, GetProcAddress getProcAddressFunc, IntPtr moduleHandle, string funcName, string errorMessage = null) where T : class
        {
            if (!typeof(Delegate).IsAssignableFrom(typeof(T)) || typeof(T).Equals(typeof(Delegate)))
            {
                throw new ArgumentException(string.Format("泛型类型{0}必须为委托类型且不能为{1}", typeof(T).FullName, typeof(Delegate).FullName), nameof(T));
            }

            IntPtr pFunc = getProcAddressFunc(moduleHandle, funcName);

            if (pFunc == IntPtr.Zero)
            {
                throw new Win32Exception(errorMessage ?? string.Format("无法获取导出函数{0}的地址。", funcName));
            }

            Win32Utility.MarshalDelegateFromFunctionPointer(out func, funcName, pFunc);
        }