免注册调用大漠插件,实际上是应用 dmreg.dll 来配合实现,这个文件有 2 个导出接口 SetDllPathW
和 SetDllPathA
。 SetDllPathW
对应 unicode,SetDllPathA
对应 ascii 接口。
一、下载大漠插件
下载地址:大漠插件
解压实现后,如下图所示:
再解压 dm.rar、大漠类库生成工具.rar、免注册.rar,解压密码为:1234
解压实现后,如下图所示:
二、生成大漠类库#
关上 大漠类库生成工具 文件夹,如下图所示:
关上 大漠类库生成工具 v24.0.exe 文件,如下图所示:
将 dm\7.2149\dm.dll
拖到 大漠类库生成工具 外面,如下图所示:
类名抉择应用自定义类名,指定类名输出 DmSoftCustomClassName(这里只是示例,你能够输出你喜爱的名字,如:abcde、aabbc、abab 等),如下图所示:
点击生成按钮,如下图所示:
关上 dm\7.2149\Output\C#
文件夹,能够看到生成的后果,如下图所示:
生成的 obj.cs 即是应用在 C# 平台下的类库封装,稍后在 C# 免注册调用大漠插件的示例中会用到。
三、创立控制台应用程序#
我这里创立的是 .NET Core 3.1 的控制台应用程序,你也能够创立你喜爱的。
3.1 引入大漠插件 dll#
在我的项目中创立 libs 文件夹,用于搁置大漠插件的 dll,如下图所示:
dm.dll 在 dm\7.2149
文件夹下,如下图所示:
DmReg.dll 在 免注册\不注册调用dm.dll的办法 v11.0
文件夹下,如下图所示:
设置 dll 属性 复制到输入目录 为 始终复制:
- 在 dm.dll 上单击鼠标右键,抉择 属性;
- 在属性面板中,“复制到输入目录”选项,抉择“始终复制”;
- 在 DmReg.dll 也反复下面的操作。
3.2 引入大漠类库#
在我的项目中创立 DmSoft 文件夹,用于搁置大漠类库。
将之前生成的大漠类库(obj.cs)复制到我的项目中的 DmSoft 文件夹,并改名为 DmSoftCustomClassName(能够改名,也能够不改名,还能够改成任意名,你喜爱就好……),如下图所示:
3.3 创立 Resources 文件夹#
在我的项目中创立 Resources 文件夹,用于搁置大漠插件应用到的资源,比方图片、字库等,如下图所示:
3.4 创立大漠插件配置类#
在我的项目中创立 Configs 文件夹,并在 Configs 中创立 DmConfig
类,用于设置大漠插件用到的常量。
DmConfig.cs
namespace DmSoftTestConsoleApp.Configs{ /// <summary> /// 大漠插件配置 /// </summary> public class DmConfig { /// <summary> /// 大漠插件免注册 DmReg.dll 门路 /// </summary> public const string DmRegDllPath = @"./libs/DmReg.dll"; /// <summary> /// 大漠插件 dm.dll 门路 /// </summary> public const string DmClassDllPath = @"./libs/dm.dll"; /// <summary> /// 大漠插件注册码 /// </summary> public const string DmRegCode = ""; /// <summary> /// 大漠插件版本附加信息 /// </summary> public const string DmVerInfo = ""; /// <summary> /// 大漠插件全局门路,设置了此门路后,所有接口调用中,相干的文件都绝对于此门路. 比方图片,字库等. /// </summary> public const string DmGlobalPath = @"./Resources"; }}
3.5 创立 C# 免注册调用大漠插件类
namespace DmSoftTestConsoleApp.Configs { /// <summary> /// 大漠插件配置 /// </summary> public class DmConfig { /// <summary> /// 大漠插件免注册 DmReg.dll 门路 /// </summary> public const string DmRegDllPath = @"./libs/DmReg.dll"; /// <summary> /// 大漠插件 dm.dll 门路 /// </summary> public const string DmClassDllPath = @"./libs/dm.dll"; /// <summary> /// 大漠插件注册码 /// </summary> public const string DmRegCode = ""; /// <summary> /// 大漠插件版本附加信息 /// </summary> public const string DmVerInfo = ""; /// <summary> /// 大漠插件全局门路,设置了此门路后,所有接口调用中,相干的文件都绝对于此门路. 比方图片,字库等. /// </summary> public const string DmGlobalPath = @"./Resources"; } }
在 DmSoft 文件夹创立 RegisterDmSoft
类,用于实现 C# 免注册调用大漠插件。
RegisterDmSoft.cs
using System.Runtime.InteropServices;using DmSoftTestConsoleApp.Configs;namespace DmSoftTestConsoleApp.DmSoft{ /// <summary> /// 免注册调用大漠插件 /// </summary> public static class RegisterDmSoft { // 不注册调用大漠插件,实际上是应用 dmreg.dll 来配合实现,这个文件有 2 个导出接口 SetDllPathW 和 SetDllPathA。 SetDllPathW 对应 unicode,SetDllPathA 对应 ascii 接口。 [DllImport(DmConfig.DmRegDllPath)] private static extern int SetDllPathA(string path, int mode); /// <summary> /// 免注册调用大漠插件 /// </summary> /// <returns></returns> public static bool RegisterDmSoftDll() { var setDllPathResult = SetDllPathA(DmConfig.DmClassDllPath, 1); if (setDllPathResult == 0) { // 加载 dm.dll 失败 return false; } return true; } }}
留神,在 .NET Core 中,无奈应用 64 位过程加载 32 位 dll。解决办法是将程序设置为 32 位的。
四、测试#
4.1 测试 C# 免注册调用大漠插件#
在 Program
类中编写测试代码。
Program.cs
using System;using DmSoftTestConsoleApp.DmSoft;namespace DmSoftTestConsoleApp{ class Program { static void Main(string[] args) { if (Environment.Is64BitProcess) { Console.WriteLine("这是 64 位程序"); Console.WriteLine("按任意键完结程序"); Console.ReadKey(); return; } // 免注册调用大漠插件 var registerDmSoftDllResult = RegisterDmSoft.RegisterDmSoftDll(); Console.WriteLine($"免注册调用大漠插件返回:{registerDmSoftDllResult}"); Console.WriteLine("按任意键完结程序"); Console.ReadKey(); } }}
4.2 测试 Capture
办法#
批改 Program
类。
Program.cs
using System;using System.IO;using DmSoftTestConsoleApp.Configs;using DmSoftTestConsoleApp.DmSoft;namespace DmSoftTestConsoleApp{ class Program { static void Main(string[] args) { if (Environment.Is64BitProcess) { Console.WriteLine("这是 64 位程序"); Console.WriteLine("按任意键完结程序"); Console.ReadKey(); return; } // 免注册调用大漠插件 var registerDmSoftDllResult = RegisterDmSoft.RegisterDmSoftDll(); Console.WriteLine($"免注册调用大漠插件返回:{registerDmSoftDllResult}"); if (!registerDmSoftDllResult) { throw new Exception("免注册调用大漠插件失败"); } // 创建对象 DmSoftCustomClassName dmSoft = new DmSoftCustomClassName(); // 免费注册 var regResult = dmSoft.Reg(DmConfig.DmRegCode, DmConfig.DmVerInfo); Console.WriteLine($"免费注册返回:{regResult}"); if (regResult != 1) { throw new Exception("免费注册失败"); } // 判断 Resources 是否存在,不存在就创立 if (!Directory.Exists(DmConfig.DmGlobalPath)) { Directory.CreateDirectory(DmConfig.DmGlobalPath); } // 设置全局门路,设置了此门路后,所有接口调用中,相干的文件都绝对于此门路. 比方图片,字库等 dmSoft.SetPath(DmConfig.DmGlobalPath); // 抓取指定区域(x1, y1, x2, y2)的图像,保留为file(24位位图) var captureResult = dmSoft.Capture(0, 0, 2000, 2000, "screen.bmp"); Console.WriteLine($"Capture 返回:{captureResult}"); if (captureResult != 1) { throw new Exception("Capture 失败"); } Console.WriteLine("按任意键完结程序"); Console.ReadKey(); } }}
运行程序,dmSoft.Reg()
办法返回 -2
(过程没有以管理员形式运行)。
五、在 VS 中设置程序以管理员身份运行#
增加利用程序清单文件到我的项目中(我的项目 → 右键 → 增加 → 新建项 → 利用程序清单文件),如下图所示:
关上 app.manifest 文件,将 requestedExecutionLevel
元素的 level
属性设置为 highestAvailable
。
也就是将
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
改为
<requestedExecutionLevel level="highestAvailable" uiAccess="false" />
按 F5 运行程序,VS 将提醒“此工作要求应用程序具备晋升的权限。”,点击“应用其余凭据重新启动(R)”,如下图所示:
VS 重新启动之后,按 F5 运行程序,screen.bmp 图片保留到 Resources 文件夹中。
六、源码地址#
源码地址:https://github.com/astrid9527...
七、总结#
本文曾经残缺地介绍了如何应用 C# 免注册调用大漠插件的办法,解决方案的构造如下图所示:
须要留神的中央有如下几点
- 在 .NET Core 中
LoadLibrary
无奈应用 64 位过程加载 32 位 dll。解决办法是将程序设置为 32 位的。 - dm.dll 和 DmReg.dll 须要设置为始终复制到输入目录。
- 留神查看
SetPath(path)
办法中的path
是否存在,不存在就创立。 - 在 VS 中设置程序以管理员身份运行。