惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

aimingoo的专栏
aimingoo的专栏
S
Securelist
博客园 - Franky
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
IT之家
IT之家
GbyAI
GbyAI
Microsoft Azure Blog
Microsoft Azure Blog
The Cloudflare Blog
云风的 BLOG
云风的 BLOG
N
News and Events Feed by Topic
AI
AI
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Schneier on Security
Schneier on Security
Attack and Defense Labs
Attack and Defense Labs
Vercel News
Vercel News
腾讯CDC
Google DeepMind News
Google DeepMind News
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
M
MIT News - Artificial intelligence
WordPress大学
WordPress大学
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
N
Netflix TechBlog - Medium
量子位
S
Schneier on Security
Hacker News: Ask HN
Hacker News: Ask HN
Cyberwarzone
Cyberwarzone
S
Security Affairs
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
N
News and Events Feed by Topic
T
Tenable Blog
PCI Perspectives
PCI Perspectives
MyScale Blog
MyScale Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Cyber Attacks, Cyber Crime and Cyber Security
W
WeLiveSecurity
N
News | PayPal Newsroom
P
Proofpoint News Feed
O
OpenAI News
C
CERT Recently Published Vulnerability Notes
B
Blog
Cisco Talos Blog
Cisco Talos Blog
Microsoft Security Blog
Microsoft Security Blog
V
Visual Studio Blog
MongoDB | Blog
MongoDB | Blog
大猫的无限游戏
大猫的无限游戏
A
Arctic Wolf
Y
Y Combinator Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Spread Privacy
Spread Privacy

博客园 - Leon0812

Export csv Export Excel Jquery pluign development example XML Mail Template Management Resource file management Excel Reader iframe 中显示内嵌页的局部 - Leon0812 - 博客园 easyslider - Leon0812 - 博客园 缩放页面字体 可编辑DIV - Leon0812 - 博客园 vs快捷键大全(转) SQL文件執行 利用IHttpModule、IHttpHandler做授权类 - Leon0812 - 博客园 无缝滚动 - Leon0812 - 博客园 获取-编译-打包 VS作业一条龙批处理 Reporting Service for SQL 2008匿名访问报表方法 獲取控件位置 - Leon0812 - 博客园 Installscript中修改web.config - Leon0812 - 博客园 publish
C#调用非托管的DLL
Leon0812 · 2010-02-23 · via 博客园 - Leon0812

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

     }