那些年,用C#调用过的外部Dll

2021-01-16 21:13

阅读:410

标签:bool   VID   parallel   start   ret   print   timeout   jit   unit   

原文:那些年,用C#调用过的外部Dll

经常有人找到我咨询以前在csdn资源里分享的dll调用。算算也写过N多接口程序。翻一翻试试写篇随笔。

  • 明华IC读写器DLL
  • 爱迪尔门锁接口DLL
  • 通用OPOS指令打印之北洋pos打印机dll
  • 明泰非接触式RF读写器DLL
  • 二代身份证读取
  • 语音盒API的使用

还有很多,以后补上。大多找不到了。也是懒得找了。

先抄点名词解释

DllImport是System.Runtime.InteropServices命名空间下的一个属性类,其功能是提供从非托管DLL导出的函数的必要调用信息。
DllImport属性应用于方法,要求最少要提供包含入口点的dll的名称。
DllImport的定义如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
[AttributeUsage(AttributeTargets.Method)]
  public class DllImportAttribute: System.Attribute
  {
   public DllImportAttribute(string dllName) {…} //定位参数为dllName
   public CallingConvention CallingConvention; //入口点调用约定
   public CharSet CharSet; //入口点采用的字符接
   public string EntryPoint; //入口点名称
   public bool ExactSpelling; //是否必须与指示的入口点拼写完全一致,默认false
   public bool PreserveSig; //方法的签名是被保留还是被转换
   public bool SetLastError; //FindLastError方法的返回值保存在这里
   public string Value { get {…} }
  }

Mwic_32.dll(明华IC卡)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using System;
using System.Text;
using System.Runtime.InteropServices;
 
namespace mw_rdp
{
    ///
    /// IC4442 的摘要说明。
    ///
    public unsafe class IC4442
    {
        public IC4442()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
 
        //向IC卡中写数据
 
        [DllImport("Mwic_32.dll", EntryPoint = "swr_4442", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
 
        public static extern int swr_4442(int icdev, int offset, int len, char* w_string);
 
        [DllImport("Mwic_32.dll", EntryPoint="srd_4442",  SetLastError=true,
             CharSet=CharSet.Auto, ExactSpelling=false,
             CallingConvention=CallingConvention.StdCall)]
            //说明:    从指定地址读数据 
            //         调用:    icdev:    通讯设备标识符 offset:   偏移地址,其值范围0~255
            //          len:      字符串长度,其值范围1~256       r_string: 读出数据所存放地址指针
            //返回:     0   错误    =0 正确
        public  static extern  Int16 srd_4442(int icdev, Int16 offset, Int16 len,[MarshalAs(UnmanagedType.LPArray)]byte[] r_string );
         
 
        [DllImport("Mwic_32.dll", EntryPoint="chk_4442",  SetLastError=true,
             CharSet=CharSet.Auto , ExactSpelling=false,
             CallingConvention=CallingConvention.StdCall)]
            //  说明:    检查卡型是否正确 
            //调用:    icdev:   通讯设备标识符
            //返回:    
        public static extern  Int16 chk_4442(int icdev);
 
 
        [DllImport("Mwic_32.dll", EntryPoint = "csc_4442", SetLastError = true,
     CharSet = CharSet.Auto, ExactSpelling = true,
     CallingConvention = CallingConvention.Winapi)]
        public static extern Int16 Csc_4442(int icdev, int len, [MarshalAs(UnmanagedType.LPArray)] byte[] p_string);
 
 
 
        [DllImport("Mwic_32.dll", EntryPoint="wsc_4442",  SetLastError=true,
             CharSet=CharSet.Auto, ExactSpelling=false,
             CallingConvention=CallingConvention.StdCall)]
            //说明:    改写卡密码
            //调用:    icdev:    通讯设备标识符 len: 密码个数,其值为3 p_string: 新密码地址指针
            //返回:   
        public static extern Int16 wsc_4442(int icdev, Int16 len, [MarshalAs(UnmanagedType.LPArray)]byte[] p_string);
 
        [DllImport("Mwic_32.dll", EntryPoint="rsc_4442",  SetLastError=true,
             CharSet=CharSet.Auto, ExactSpelling=false,
             CallingConvention=CallingConvention.StdCall)]
            //说明:    读出卡密码 
            //调用:    icdev:    通讯设备标识符  len:      密码个数,其值为3     p_string: 存放密码地址指针
            // 返回:    0   错误   =0   正确 
          
        public static extern Int16 rsc_4442(int icdev, Int16 len,  [MarshalAs(UnmanagedType.LPArray)]byte[] p_string);
 
        [DllImport("Mwic_32.dll", EntryPoint="rsct_4442",  SetLastError=true,
             CharSet=CharSet.Auto, ExactSpelling=false,
             CallingConvention=CallingConvention.StdCall)]
            //说明:    读出密码错误计数器值
            //调用:    icdev:    通讯设备标识符 counter:  密码错误记数值存放指针
            //返回:     =0   正确
        public static extern Int16 rsct_4442(int icdev, out byte counter);
 
 
    }
}

MainDll.Dll(爱迪尔门锁)

技术图片
public static class AdelDoorCardIfc
    {
        public enum AdelSoftType
        {
            Lock3200 = 1,
            Lock3200K,
            Lock4200,
            Lock4200D,
            Lock5200,
            Lock6200,
            Lock7200,
            Lock7200D,
            Lock9200,
            Lock9200T,
            A30,
            A50 = 14,
            A90 = 18,
            A92 = 22
        }
        public enum TMEncoder
        {
            DS9097E = 1,
            DS9097U = 5
        }
        public enum EnCoderType
        {
            手动发行机,
            自动发行机,
            MSR206磁卡
        }
        private const string IFC_DllFileName = "MainDll.Dll";
        [DllImport("MainDll.Dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
        public static extern int SetPort(int Soft, int Port, int EncoderType, int TMEncoder);
        [DllImport("MainDll.Dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
        public static extern int Reader_Beep(int Sound);
        [DllImport("MainDll.Dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
        public static extern int EndSession();
        [DllImport("MainDll.Dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
        public static extern void ChangeUser(string username);
        [DllImport("MainDll.Dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
        public static extern int Init(int Soft, string Server, string UName, int EnCoderType, int TMEncoder);
        [DllImport("MainDll.Dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
        public static extern int NewKey(string RoomNo, string Gate, string StartTime, string GuestName, string GuestId, int OverFlag, out int CardNo, string str1, string str2);
        [DllImport("MainDll.Dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
        public static extern int AddKey(string RoomNo, string Gate, string StartTime, string GuestName, string GuestId, int OverFlag, out int CardNo, string str1, string str2);
        [DllImport("MainDll.Dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
        public static extern int DupKey(string RoomNo, string Gate, string StartTime, string GuestName, string GuestId, int OverFlag, out int CardNo, string str1, string str2);
        [DllImport("MainDll.Dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
        public static extern int ReadCard(StringBuilder room, StringBuilder gate, StringBuilder stime, StringBuilder guestname, StringBuilder guestid, StringBuilder track1, StringBuilder track2, ref int cardno, ref int st);
        [DllImport("MainDll.Dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
        public static extern int EraseCard(int CardNo, string str1, string str2);
        [DllImport("MainDll.Dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
        public static extern int CheckOut(string RoomNo, int CardNo);
        [DllImport("MainDll.Dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
        public static extern int LostCard(string RoomNo, int CardNo);
        [DllImport("MainDll.Dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
        public static extern int ReadCardId(ref uint cardNo);
        [DllImport("MainDll.Dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
        public static extern int ReadIC(int start, int len, StringBuilder buff);
        [DllImport("MainDll.Dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
        public static extern int WriteIC(int start, int len, StringBuilder buff);
        public static void ShowMessageResult(int iResult, bool ifSystemBarNotity)
        {
            string text = string.Empty;
            switch (iResult)
            {
            case 0:
                text = "门口接口调用,操作成功!";
                break;
            case 1:
                text = "读写错误或者数据错误!";
                break;
            case 2:
                text = "卡已损坏!";
                break;
            case 3:
                text = "没有检测到卡!";
                break;
            case 4:
                text = "串口通信错误,请检测连接线!";
                break;
            case 5:
                text = "卡被更换!";
                break;
            case 6:
                text = "不是新卡!";
                break;
            case 7:
                text = "卡是新卡!";
                break;
            case 8:
                text = "非本系统卡!";
                break;
            case 9:
                text = "不是客人卡!";
                break;
            case 10:
                text = "不是会员卡!";
                break;
            case 11:
                text = "密码错误!";
                break;
            case 12:
                text = "无开门记录!";
                break;
            case 13:
                text = "卡型不正确!";
                break;
            case 14:
                text = "参数错误!";
                break;
            case 15:
                text = "用户取消操作(按下键)!";
                break;
            case 16:
                text = "等待超时!";
                break;
            case 17:
                text = "插卡错误!";
                break;
            case 18:
                text = "卡是空白卡或插卡错误!";
                break;
            case 19:
                text = "19";
                break;
            case 20:
                text = "没有调用Init函数!";
                break;
            case 21:
                text = "不支持该版本的门锁软件!";
                break;
            case 22:
                text = "连接(门锁系统数据库)错误!";
                break;
            case 23:
                text = "门锁系统参数不存在!";
                break;
            case 24:
                text = "初始化失败!";
                break;
            case 25:
                text = "没有客人在住/指定客人不存在!";
                break;
            case 26:
                text = "客房已满!";
                break;
            case 27:
                text = "没有此卡记录!";
                break;
            case 28:
                text = "没有调用SetPort函数!";
                break;
            case 29:
                text = "无效的客房房号!";
                break;
            case 30:
                text = "错误的时间范围!";
                break;
            case 31:
                text = "卡号已存在,无法登记!";
                break;
            case 32:
                text = "不支持调用!";
                break;
            case 33:
                text = "无效的授权码,授权码错误或过期!";
                break;
            }
            if (ifSystemBarNotity)
            {
                Utility.NotifyMessageBox(text, "接口系统提示");
            }
            else
            {
                MessageBox.Show(text, "接口系统提示");
            }
        }
    }
技术图片

posdll.dll(北洋打印机封装的OPOS指令打印DLL)

技术图片
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.IO.Ports;

namespace PosPrintService
{
    /// 
    /// 北洋OPOS指令集二次开发包DLL调用  
    ///   
    public class BeiYangOPOS
    {
        const string _DllVer = "1.4";
        /// 
        /// 获取动态库版本号
        /// 
        public string GetDllVer
        {
            get { return _DllVer;}
        }

      
        /// 
        /// 设备打开后的句柄
        /// 
        public IntPtr POS_IntPtr;

        /// 
        /// 函数返回值
        /// 
        public uint POS_SUCCESS = 1001;//  函数执行成功 
        public uint POS_FAIL = 1002;   //  函数执行失败 
        public uint POS_ERROR_INVALID_HANDLE = 1101; // 端口或文件的句柄无效 
        public uint POS_ERROR_INVALID_PARAMETER = 1102;// 参数无效 
        public uint POS_ERROR_NOT_BITMAP = 1103 ; // 不是位图格式的文件 
        public uint POS_ERROR_NOT_MONO_BITMAP = 1104;// 位图不是单色的 
        public uint POS_ERROR_BEYONG_AREA = 1105 ;//位图超出打印机可以处理的大小 
        public uint POS_ERROR_INVALID_PATH = 1106; // 没有找到指定的文件路径或名 

        /// 
        /// 停止位
        /// 
        public uint POS_COM_ONESTOPBIT = 0x00;//停止位为1
        public uint POS_COM_ONE5STOPBITS = 0x01;//停止位为1.5
        public uint POS_COM_TWOSTOPBITS = 0x02;//停止位为2
        /// 
        /// 奇偶校验
        /// 
        public uint POS_COM_NOPARITY = 0x00;//无校验
        public uint POS_COM_ODDPARITY = 0x01;//奇校验
        public uint POS_COM_EVENPARITY = 0x02;//偶校验
        public uint POS_COM_MARKPARITY = 0x03;//标记校验
        public uint POS_COM_SPACEPARITY = 0x04;//空格校验
        /// 
        /// 其他COM口参数及端口类型定义
        /// 
        public uint POS_COM_DTR_DSR = 0x00;// 流控制为DTR/DST  
        public uint POS_COM_RTS_CTS = 0x01;// 流控制为RTS/CTS 
        public uint POS_COM_XON_XOFF = 0x02;// 流控制为XON/OFF 
        public uint POS_COM_NO_HANDSHAKE = 0x03;//无握手 
        public uint POS_OPEN_PARALLEL_PORT = 0x12;//打开并口通讯端口 
        public uint POS_OPEN_BYUSB_PORT = 0x13;//打开USB通讯端口 
        public uint POS_OPEN_PRINTNAME = 0X14;// 打开打印机驱动程序 
        public uint POS_OPEN_NETPORT = 0x15;// 打开网络接口 

        public uint POS_CUT_MODE_FULL = 0x00;// 全切 
        public uint POS_CUT_MODE_PARTIAL = 0x01;// 半切 

        /// 
        /// 打开POS机的端口 开始会话
        /// 
        /// 
        ///指向以 null 结尾的打印机名称或端口名称。
        ///当参数nParam的值为POS_COM_DTR_DSR、POS_COM_RTS_CTS、POS_COM_XON_XOFF或POS_COM_NO_HANDSHAKE 时, “COM1”,“COM2”,“COM3”,“COM4”等表示串口;
        ///当参数nParam的值为POS_OPEN_PARALLEL_PORT时,“LPT1”,“LPT2”等表示并口;
        ///当参数nParam的值为POS_OPEN_BYUSB_PORT时,“BYUSB-0”、“BYUSB-1”、“BYUSB-2”、“BYUSB-3”等表示USB端口。
        ///当参数nParam的值为POS_OPEN_PRINTNAME时,表示打开指定的打印机。
        ///当参数nParam的值为POS_OPEN_NETPORT时,表示打开指定的网络接口,如“192.168.10.251”表示网络接口IP地址
        /// 串口通信需要的波特率
        /// 串口通信需要的数据位
        /// 串口通信需要的停止位
        /// 串口通信需要的是否要奇偶校验
        /// 指向以 null 结尾的打印机名称或端口名称。
        /// 参数nParam的值为POS_COM_DTR_DSR、POS_COM_RTS_CTS、POS_COM_XON_XOFF或POS_COM_NO_HANDSHAKE 时,
        /// “COM1”,“COM2”,“COM3”,“COM4”等表示串口;
        /// 当参数nParam的值为POS_OPEN_PARALLEL_PORT时,“LPT1”,“LPT2”等表示并口;
        /// 当参数nParam的值为POS_OPEN_BYUSB_PORT时,“BYUSB-0”、“BYUSB-1”、“BYUSB-2”、“BYUSB-3”等表示USB端口。
        /// 当参数nParam的值为POS_OPEN_PRINTNAME时,表示打开指定的打印机。
        /// 如果函数调用成功,返回一个已打开的端口句柄。如果函数调用失败,返回值为 INVALID_HANDLE_VALUE (-1)。
        [DllImport("POSDLL.dll", CharSet = CharSet.Ansi)]
        public static extern IntPtr POS_Open([MarshalAs(UnmanagedType.LPStr)]string lpName, 
                                             uint nComBaudrate, 
                                             uint nComDataBits, 
                                             uint nComStopBits, 
                                             uint nComParity,
                                             uint nParam);

        /// 
        /// 关闭已经打开的并口或串口,USB端口,网络接口或打印机。
        /// 
        /// 
        [DllImport("POSDLL.dll", SetLastError = true)]
        public static extern IntPtr POS_Close();

        /// 
        /// 复位打印机,把打印缓冲区中的数据清除,字符和行高的设置被清除,打印模式被恢复到上电时的缺省模式。
        /// 
        /// 
        [DllImport("POSDLL.dll", SetLastError = true)]
        public static extern IntPtr POS_Reset();

        /// 
        /// 设置打印机的移动单位。
        /// 
        /// 把水平方向上的移动单位设置为 25.4 / nHorizontalMU 毫米。可以为0到255。
        /// 把垂直方向上的移动单位设置为 25.4 / nVerticalMU 毫米。可以为0到255。
        /// 
        /// 如果函数成功,则返回值为 POS_SUCCESS。
        /// 如果函数失败,则返回值为以下值之一:POS_FAIL POS_ERROR_INVALID_HANDLE POS_ERROR_INVALID_PARAMETER 
        [DllImport("POSDLL.dll", SetLastError = true)]
        public static extern IntPtr POS_SetMotionUnit(uint nHorizontalMU, uint nVerticalMU);
        /// 
        /// 选择国际字符集和代码页
        /// 
        /// 
        /// 指定国际字符集。不同的国际字符集对0x23到0x7E的ASCII码值对应的符号定义是不同的。
        /// 可以为以下列表中所列值之一。
        /// 0x00 U.S.A  0x01 France  0x02 Germany  0x03 U.K. 0x04 Denmark I 0x05 Sweden 
        /// 0x06 Italy 0x07 Spain I  0x08 Japan 0x09 Nonway 0x0A Denmark II 0x0B Spain II 
        /// 0x0C Latin America 0x0D Korea 
        /// 
        /// 指定字符的代码页。不同的代码页对0x80到0xFF的ASCII码值对应的符号定义是不同的。
        /// 0x00 PC437 [U.S.A. Standard Europe 0x01 Reserved 0x02 PC850 [Multilingual] 
        /// 0x03 PC860 [Portuguese] 0x04 PC863 [Canadian-French] 0x05 PC865 [Nordic] 
        /// 0x12 PC852 0x13 PC858 
        /// 
        /// 
        /// 如果函数成功,则返回值为 POS_SUCCESS。
        /// 如果函数失败,则返回值为以下值之一:POS_FAIL POS_ERROR_INVALID_HANDLE POS_ERROR_INVALID_PARAMETER 
        [DllImport("POSDLL.dll", SetLastError = true)]
        public static extern IntPtr POS_SetCharSetAndCodePage(uint nCharSet, uint nCodePage);

        /// 
        /// POS字体样式
        /// 
        /// 
        public uint  POS_FONT_TYPE_STANDARD  = 0x00;// 标准 ASCII 
        public uint  POS_FONT_TYPE_COMPRESSED = 0x01;// 压缩 ASCII  
        public uint  POS_FONT_TYPE_UDC = 0x02;       // 用户自定义字符 
        public uint  POS_FONT_TYPE_CHINESE = 0x03;   // 标准 “宋体” 
        public uint  POS_FONT_STYLE_NORMAL =  0x00;   //  正常 
        public uint  POS_FONT_STYLE_BOLD =  0x08;   //  加粗 
        public uint  POS_FONT_STYLE_THIN_UNDERLINE =  0x80;   //  1点粗的下划线 
        public uint  POS_FONT_STYLE_THICK_UNDERLINE =  0x100;   //  2点粗的下划线 
        public uint  POS_FONT_STYLE_UPSIDEDOWN =  0x200;   //  倒置(只在行首有效) 
        public uint  POS_FONT_STYLE_REVERSE =  0x400;   //  反显(黑底白字) 
        public uint  POS_FONT_STYLE_SMOOTH =  0x800;   //  平滑处理(用于放大时) 
        public uint POS_FONT_STYLE_CLOCKWISE_90 = 0x1000;   //  每个字符顺时针旋转 90 度

        /// 
        /// 把将要打印的字符串数据发送到打印缓冲区中,并指定X 方向(水平)上的绝对起始点位置,
        /// 指定每个字符宽度和高度方向上的放大倍数、类型和风格。
        /// 
        /// 指向以 null 结尾的字符串缓冲区
        /// 指定 X 方向(水平)的起始点位置离左边界的点数。
        /// 指定字符的宽度方向上的放大倍数。可以为 1到 6。
        /// 指定字符高度方向上的放大倍数。可以为 1 到 6。
        /// 指定字符的字体类型。
        /// 指定字符的字体风格。
        /// 
        
        [DllImport("POSDLL.dll"


评论


亲,登录后才可以留言!