c# 调用 vc dll 函数方法
需要写刷卡器系统, 调用vc dll,有部分我调用成功了,如下:
icdev = IC.rf_init_com(3,9600);
//对设备进行初始化
public unsafe class IC
{
[DllImport("MasterRD.dll", EntryPoint = "rf_init_com", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern int rf_init_com(int port, Int64 baud);
}
//方法2
[DllImport("MasterRD.dll", EntryPoint = "rf_M1_authentication2", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern int rf_M1_authentication2(IntPtr dve, byte model, byte block,ref byte pKey);
[解决办法]
byte 本色就是指针 不需要ref 你去掉ref试试
[解决办法]
试试这样
[DllImport("MasterRD.dll", EntryPoint = "rf_M1_authentication2", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern int rf_M1_authentication2(ushort dve, byte model, byte block, byte[] pKey);
byte[] key = new byte[6] { 255, 255, 255, 255, 255, 255 };
int checkIC_pwd = IC.rf_M1_authentication2(icdev1, mode, block, key);
[DllImport("Kernel32")]
public static extern int GetProcAddress(int handle, string funcname);
[DllImport("Kernel32")]
public static extern int LoadLibrary(string funcname);
[DllImport("Kernel32")]
public static extern int FreeLibrary(int handle);
/// <summary>
/// 委托
/// </summary>
/// <param name="dllModule"></param>
/// <param name="functionname"></param>
/// <param name="t"></param>
/// <returns></returns>
public static Delegate qqGetAddress(int dllModule, string functionname, Type t)
{
int addr = MyLoadLibrary.GetProcAddress(dllModule, functionname);
if (addr == 0)
return null;
else
return Marshal.GetDelegateForFunctionPointer(new IntPtr(addr), t);
}
//声明
/// <summary>
/// QQ/TM聊天记录获取
/// </summary>
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct SendQQMsgInfo
{
/// <summary>
/// 聊天类型:0=单人聊天,1=群聊天,2=讨论组聊天
/// </summary>
public uint QQMsgType;
/// <summary>
/// 本机QQ号码
/// </summary>
public uint SourceQQNum;
/// <summary>
/// 对方QQ号码
/// </summary>
public uint DestinationQQNum;
/// <summary>
/// 群号码或聊天组号码
/// </summary>
public uint GroupNum;
/// <summary>
/// 消息类型:true=发送,false=接收
/// </summary>
public bool bSend;
/// <summary>
/// 本机QQ昵称
/// </summary>
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 30)]
public string SourceString;
/// <summary>
/// 对方QQ昵称或群聊天中消息发送人的昵称
/// </summary>
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 40)]
public string DestinationString;
/// <summary>
/// 群名或聊天组名
/// </summary>
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 30)]
public string GroupNameString;
/// <summary>
/// 消息内容
/// </summary>
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 7000)]
public string TextString;
}
/// <summary>
/// 获取QQ/TM聊天记录
/// </summary>
/// <param name="sendmess"></param>
/// <returns></returns>
public delegate bool GetQQMsg(ref SendQQMsgInfo sendmess);
/// <summary>
/// 获取QQ聊天记录接口
/// </summary>
static DCNetClient.BLL.NetWordInit.GetQQMsg myQQmsg;
//使用
int huser32 = MyLoadLibrary.LoadLibrary("GetIM32Msg.dll");
myQQmsg = (DCNetClient.BLL.NetWordInit.GetQQMsg)MyLoadLibrary.qqGetAddress(huser32, "SendMsgInfo", typeof(DCNetClient.BLL.NetWordInit.GetQQMsg));
SendQQMsgInfo MyQQMsg = new SendQQMsgInfo();
myQQmsg(ref MyQQMsg);
/// <summary>
/// 获取网页浏览记录
/// </summary>
/// <returns></returns>
[DllImport("DcNetFilterDll.dll")]
public static extern bool GetNetLink(int hander, int hProcessEvent, byte[] pNetLinkBuf, int iNetLinkBufSize, ref int iGetLen);
{
IC.rf_M1_authentication2(dve, 0x60, block, pkey);
}
}