那些年,用C#调用过的外部Dll
2021-01-16 21:13
标签:bool VID parallel start ret print timeout jit unit
经常有人找到我咨询以前在csdn资源里分享的dll调用。算算也写过N多接口程序。翻一翻试试写篇随笔。 还有很多,以后补上。大多找不到了。也是懒得找了。 DllImport是System.Runtime.InteropServices命名空间下的一个属性类,其功能是提供从非托管DLL导出的函数的必要调用信息。
先抄点名词解释
DllImport属性应用于方法,要求最少要提供包含入口点的dll的名称。
DllImport的定义如下:
[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卡)
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 = "用户取消操作(按下
posdll.dll(北洋打印机封装的OPOS指令打印DLL)
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.IO.Ports;
namespace PosPrintService
{
///
上一篇:WPF多窗口磁性对齐贴边功能实现
下一篇:三种方法,让WPF项目生成单文件