首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

c# 调用 vc dll 函数步骤

2013-06-26 
c# 调用 vc dll 函数方法需要写刷卡器系统, 调用vc dll,有部分我调用成功了,如下: icdev IC.rf_init_com

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


现在就差验证密匙的调用不成功,

下面是vc dll 函数说明
Mifareone 授权2

/*********************************************************

功能:用当前指定的密钥对卡片授权认证

参数: icdev: 通讯设备标识符

       mode: 密码验证模式

       block:要验证密码的绝对块号

       key:   下载的密钥(6Byte),(我加的,key 验证密匙,6字节)

返回:成功则返回0

说明:model=0x60:验证A密钥

       model=0x61:验证B密钥

*********************************************************/

int (WINAPI *rf_M1_authentication2)(unsigned short icdev,unsigned char mode,unsigned char block,unsigned char  *key);


我写了2种方法:如下,但是都是返回值错误,本该返回0,2中方法都是返回1, 求助::写代码最好,主要感觉问题出在最后一个参数,也就是密匙key。
调用:
                ////方法1
                //byte[] key = new byte[6] { 255, 255, 255, 255, 255, 255 };
                //Int16 checkIC_pwd = (short)IC.rf_M1_authentication2(icdev1,mode,block, ref key[0]);

                //方法2

                byte[] dd = new byte[6];
                dd[0] = (byte)0xFF;
                dd[1] = (byte)0xFF;
                dd[2] = (byte)0xFF;
                dd[3] = (byte)0xFF;
                dd[4] = (byte)0xFF;
                dd[5] = (byte)0xFF;
                int xx = IC.rf_get_device_number(ref dve);
                int checkIC_pwd = IC.rf_M1_authentication2(dve, 0x60, block,ref dd[0]);


下面是定义:
    //方法1
    //[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 icdev, byte mode,  byte block, ref byte key);


  
    //方法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);

[解决办法]
看你是静态编译还是动态编译的dll   最好是静态编译的
下面给你贴几个我通过的c#代码     c#调用vc dll 


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


pNetLinkBuf 是char* 指针
这里c#不需要用ref 本生byte就是指针
[解决办法]
[DllImport("MasterRD.dll", EntryPoint = "rf_M1_authentication2", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
   public static unsafe extern int rf_M1_authentication2(IntPtr dve, byte model, byte block,byte* pKey);



byte[] key = new byte[6] { 255, 255, 255, 255, 255, 255 };
unsafe
{
      fixed (byte* pkey = key)


      {
             IC.rf_M1_authentication2(dve, 0x60, block, pkey);
      }
}

热点排行