C#调用非托管的DLL

Posted on 2010-02-23 11:16  Leon0812  阅读(381)  评论(0)    收藏  举报

using System;

using System.Runtime.InteropServices;

 

/// <summary>

/// C#调用非托管的DLL

/// <description>

/// 非托管的就特殊处理(实际上你拷贝到bin是没有任何作用的,因为CLR会把文件拷贝到一个临时目录下,然后在那运行Web,而CLR只会拷贝托管文件,这就是为什么把非托管的DLL放到bin目录下仍然提示找不到该模块)。

///解决方案:首先在服务器上建立一个新建的目录,假设是(C:ProgramDirWinDLL).然后在环境变量中,给Path变量添加这个目录,最后把非托管的DLL文件都拷贝到该目录下。或者更干脆把DLL放到System32目录中。

///对于自己部署的应用程序,这样的确能很好的解决问题。然而如果我们用的是虚空间,我们有没有办法吧注册Path变量或者把我们自己的DLL拷贝System32目录下。同时我们也不一定知道我们DLL的物理路径.

///DllImport里面只能用字符常量,而不能使用Server.MapPath来确认物理绝对路径。

///这样的话我们需要动态的取得我们DLL的物理路径(Server.MapPath),并通过API来取得DLL里面的函数(先加载LoadLibrary后获得函数地址GetProcAddress)

/// </description>

/// </summary>

public class CustomDLLInvoke

{

 

    [DllImport("kernel32.dll")]

    private extern static IntPtr LoadLibrary(string path);

 

    [DllImport("kernel32.dll")]

    private extern static IntPtr GetProcAddress(IntPtr lib, String funcName);

 

    [DllImport("kernel32.dll")]

    private extern static bool FreeLibrary(IntPtr lib);

 

    private IntPtr MLib;

 

    public CustomDLLInvoke(string dllPath)

    {

        MLib = LoadLibrary(dllPath);

    }

 

    ~CustomDLLInvoke()

    {

        FreeLibrary(MLib);

    }

 

    public Delegate Invoke(string APIName, Type t)

    {

        IntPtr api = GetProcAddress(MLib, APIName);

        return (Delegate)Marshal.GetDelegateForFunctionPointer(api, t);

    }

 

}

 

public sealed class SoftGuard

     {

 

        /// <summary>

           /// 单机版:连结授权数据文件; 网络版:连结后端授权管理员

           /// </summary>

           /// <param>Application's Handle</param>

           /// <param>产品代码</param>

           /// <param>产品种子码</param>

           /// <param></param>

           /// <param>保护方式(1:软件保护; 2:Usb保护; 3:KeyPro保护; 4:旧KeyPro #4[黑]保护; 5:旧KeyPro #8[绿]保护); 6:Keycard)</param>

           /// <returns>

           /// 0 -> 试用

        /// 1 -> 网络正式版

        /// 2 -> 单机正式版

        /// 3 -> 网络教育版

        /// 4 -> 单机教育版

        /// 101 -> 没有授权

        /// 102 -> 授权已过期

        /// 103 -> 超出授权使用人数

        /// 104 -> 已过试用期限

        /// 100 -> 读取授权数据时发生错误,可能为以下几种原因:

        ///         1.无法连结授权数据文件(LICENSEINFO.DAT)

        ///         2.无法连结授权管理员(LinceseMan)

           ///         3.授权管理员(LinceseMan)无响应

           /// </returns>

        [DllImport("SoftGuardC.dll", EntryPoint = "ConnectLinceseMan9511DotNet")]

        public static extern int ConnectLinceseMan9511DotNet(int AHandle, string ASysName, string AProductCode, string AHardProductVer, int AProductType);

 

           /// <summary>

           /// 检查某子系统是否授权

           /// </summary>

           /// <param>子系统代码</param>

           /// <returns>True:已授权使用; False:未授权</returns>

        [DllImport("SoftGuardC.dll", EntryPoint = "CheckUsefulSystemDotNet")]

        public static extern bool CheckUsefulSystemDotNet(string ASystemId);

 

           /// <summary>

           ///

           /// </summary>

           /// <returns></returns>

           [DllImport("SoftGuardC.dll", EntryPoint = "GetVersionId")]

           public static extern int GetVersionId();

 

           /// <summary>

           /// 取得已授权使用子系统

           /// </summary>

           /// <returns>已授权使用子系统代码字符串</returns>

        [DllImport("SoftGuardC.dll", EntryPoint = "GetUsefulSystemsDotNet")]

        public static extern string GetUsefulSystemsDotNet();

 

           /// <summary>

           /// 取得授权版本别名称

           /// </summary>

           /// <returns>授权版本别名称</returns>

        [DllImport("SoftGuardC.dll", EntryPoint = "GetVersionNameDotNet")]

        public static extern string GetVersionNameDotNet();

 

           /// <summary>

           ///

           /// </summary>

           /// <returns>0:单机版1:网络版-Client 2:未知</returns>

        [DllImport("SoftGuardC.dll", EntryPoint = "IsNetClientDotNet")]

        public static extern int IsNetClientDotNet(string AProductCode);

 

           /// <summary>

           /// 取得所有可用功能项目字符串

           /// </summary>

           /// <returns>所有可用功能项目字符串</returns>

        [DllImport("SoftGuardC.dll", EntryPoint = "GetUsefulFunctionsDotNet")]

        public static extern string GetUsefulFunctionsDotNet();

 

           /// <summary>

           /// 取得某子系统下所有可用功能项目字符串

           /// </summary>

           /// <returns>该子系统下所有可用功能项目字符串</returns>

        [DllImport("SoftGuardC.dll", EntryPoint = "GetUsefulFunctionsBySysIdDotNet")]

        public static extern string GetUsefulFunctionsBySysIdDotNet(string ASysId);

 

           /// <summary>

           /// 检查某功能项目是否授权

           /// </summary>

           /// <returns>True:已授权使用False:未授权</returns>

        [DllImport("SoftGuardC.dll", EntryPoint = "CheckUsefulFunctionDotNet")]

        public static extern bool CheckUsefulFunctionDotNet(string AFunctionId);

 

           /// <summary>

           /// 取得LicenseMan所安装之计算机名称或IP地址

           /// </summary>

           /// <returns>LicenseMan所安装之计算机名称或IP地址</returns>

        [DllImport("SoftGuardC.dll", EntryPoint = "GetLicenseServerDotNet")]

        public static extern string GetLicenseServerDotNet(string AProductCode);

 

           /// <summary>

           /// 取得LicenseMan中所设定的用户信息

           /// </summary>

           /// <returns>LicenseMan中所设定的用户信息(ex:客户代号$$客户名称)</returns>

        [DllImport("SoftGuardC.dll", EntryPoint = "GetCustomerInfoDotNet")]

        public static extern string GetCustomerInfoDotNet();

 

           /// <summary>

           /// 取得LicenseMan中所设定用户信息中之客户代号

           /// </summary>

           /// <returns>LicenseMan中所设定用户信息中之客户代号</returns>

        [DllImport("SoftGuardC.dll", EntryPoint = "GetCustomerIdDotNet")]

        public static extern string GetCustomerIdDotNet();

 

           /// <summary>

           /// 取得LicenseMan中所设定用户信息中之客户名称

           /// </summary>

           /// <returns>LicenseMan中所设定用户信息中之客户名称</returns>

        [DllImport("SoftGuardC.dll", EntryPoint = "GetCustomerNameDotNet")]

        public static extern string GetCustomerNameDotNet();

 

           /// <summary>

           /// 取得授权人数

           /// </summary>

           /// <returns>授权人数</returns>

        [DllImport("SoftGuardC.dll", EntryPoint = "GetUserCountDotNet")]

        public static extern int GetUserCountDotNet();

 

     }

 

 

博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3