如何在 C# 中使用 zKemKeeper 连接考勤打孔机

本文关键字:连接 zKemKeeper 打孔机 | 更新日期: 2023-09-27 18:36:24

我正在使用 asp.net c#开发考勤软件,当我尝试使用 asp.net C#连接生物识别设备(考勤打孔机)时,它显示错误

错误:"试图读取或写入受保护的内存。这通常表明其他内存已损坏。

我的代码 :

//internal Axzkemkeeper.AxCZKEM AxCZKEM1;
    //public Axzkemkeeper.AxCZKEM axCZKEM1 = new Axzkemkeeper.AxCZKEM();
    //public zkemkeeper.CZKEM axCZKEM1 = new zkemkeeper.CZKEM();
    //public delegate int DecompressMCX(int hComp,IntPtr in, uint in_len, IntPtr out, ref uint out_len, bool eod);
    public zkemkeeper.CZKEM axCZKEM1 = new zkemkeeper.CZKEM();
private void Essl_Connect(string IpAddress, int MachineNo)
    {
        try
        {
            bConn = axCZKEM1.Connect_Net(IpAddress.Trim(), 8080);
            //bConn = axCZKEM1.Connect_Net(IpAddress.Trim(), 4370);
            if (bConn == true)
            {
                ScriptManager.RegisterStartupScript(this, GetType(), "Message", "alert('Connected')", true);
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, GetType(), "Message", "alert('Not Connected')", true);
            }
        }
        catch (Exception ex)
        {
            ScriptManager.RegisterStartupScript(this, GetType(), "Error Message", "alert('" + ex.Message.ToString() + "')", true);
        }
    }

任何人都可以纠正这个问题,有没有其他方法来连接机器?..

如果 IAM 使用

公共zkemkeeper。CZKEMClass axCZKEM1 = new zkemkeeper.CZKEMClass();

它显示以下错误:

"互操作类型'zkemkeeper。CZKEMClass'不能嵌入。请改用适用的接口。

如何在 C# 中使用 zKemKeeper 连接考勤打孔机

更改(在属性中)该 dll interop.zkemkeeper,将类型嵌入为 False。

  1. 确保注册表正确的 dll 文件 x64/x32
  2. 将此代码添加到创建类的代码顶部

    [SecurityPermission(SecurityAction.Assert, Unrestricted = true)]

     public static CZKEM axCZKEM1 = new CZKEM();
    private static bool bIsConnected;
    private static int iMachineNumber;
    public static bool ConnectDevice(string IP,int Port)
    {
        int idwErrorCode=0;
        try
        {
            bIsConnected = axCZKEM1.Connect_Net(IP.Trim(),Port);
            if (bIsConnected)
            {
                iMachineNumber = 1; //In fact,when you are using the tcp/ip communication,this parameter will be ignored,that is any integer will all right.Here we use 1.
                axCZKEM1.EnableDevice(iMachineNumber, false);
                axCZKEM1.RegEvent(iMachineNumber, 65535); //
                return true;
            }
            else
            {
                axCZKEM1.GetLastError(idwErrorCode);
                new Exception($"Error : Unable to connect the device, ErrorCode =  {idwErrorCode}");
                return false;
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    public static bool DisconnectDevice()
    {
        try
        {
            axCZKEM1.Disconnect();
            return true;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    
  1. 使用 C#.NET 将生物识别考勤机与 .NET 连接。现在在连接按钮的单击事件上编写下面的代码。

    首先声明连接对象:

    public zkemkeeper.CZKEM axCZKEM1 = new zkemkeeper.CZKEM();
    private void btnconnect_Click(object sender, EventArgs e)
    {
        try
        {
            bool bIsConnected = axCZKEM1.Connect_Net(ip_address_of_your_machine, 4370);   // 4370 is port no of attendance machine
            if (bIsConnected == true)
            {
                MessageBox.Show("Device Connected Successfully");
            }
            else
            {
                MessageBox.Show("Device Not Connect");
            }
        }
        Catch( (Exception ex)
        {
            MessageBox.Show(ex.Message.ToString());
        }
    }
    
  2. 使用 C#.NET 从数据库中的生物识别考勤机下载考勤日志。

    在下载出席按钮的点击事件中编写以下代码:

    private bool IsRead = false;
    string dwEnrollNumber;
    int dwVerifyMode, dwInOutMode, dwYear, dwMonth, dwDay, dwHour, dwMinute, dwSecond,           dwWorkcode, dwMachineNumber;
    public zkemkeeper.CZKEM axCZKEM1 = new zkemkeeper.CZKEM();
    public class ClsMachineBL
    {
        public String DownloadDataFromBiomatrix(ClsMachineML prp)
        {
            try
            {
                string constr = CommonConnection.ConStr;
                con = new SqlConnection(constr);
                con.Open();
                cmd = new SqlCommand("Prc_InsertDatafromBiomatrix", con);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                cmd.Parameters.AddWithValue("@dwMachineNumber", prp.dwMachineNumber);
                cmd.Parameters.AddWithValue("@dwEnrollNumber", prp.dwEnrollNumber);
                cmd.Parameters.AddWithValue("@dwVerifyMode", prp.dwVerifyMode);
                cmd.Parameters.AddWithValue("@dwInOutMode", prp.dwInOutMode);
                cmd.Parameters.AddWithValue("@dwYear", prp.dwYear);
                cmd.Parameters.AddWithValue("@dwMonth", prp.dwMonth);
                cmd.Parameters.AddWithValue("@dwDay", prp.dwDay);
                cmd.Parameters.AddWithValue("@dwHour", prp.dwHour);
                cmd.Parameters.AddWithValue("@dwMinute", prp.dwMinute);
                cmd.Parameters.AddWithValue("@dwSecond", prp.dwSecond);
                cmd.Parameters.AddWithValue("@dwWorkcode", prp.dwWorkcode);
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                BL.clsCommon objerr = new BL.clsCommon();
                objerr.InesrtError("Error IS " + ex.Message + "_" + ex.StackTrace);
            }
            finally
            {
                cmd.Dispose();
                con.Close();
                con.Dispose();
            }
            return result;
        }
    }
    public class ClsMachineML
    {
        public string Id { get; set; }
        public string MachineNo { get; set; }
        public string MachineIP { get; set; }
        public string PortNo { get; set; }
        public string Remark { get; set; }
        public string Tuser { get; set; }
        public string Tdate { get; set; }
        public string Status { get; set; }
        public int  dwMachineNumber { get; set; }
        public string dwEnrollNumber { get; set; }
        public int dwVerifyMode { get; set; }
        public int dwInOutMode { get; set; }
        public int dwYear { get; set; }
        public int dwMonth { get; set; }
        public int dwDay { get; set; }
        public int dwHour { get; set; }
        public int dwMinute { get; set; }
        public int dwSecond { get; set; }
        public int dwWorkcode { get; set; }
        public string User_Id { get; set; }
        public string Name { get; set; }
        public int Finger_Index { get; set; }
        public string Finger_Image { get; set; }
        public int Privilege { get; set; }
        public string Passwords { get; set; }
        public bool Enabled { get; set; }
        public int Flag { get; set; }
        public string  Fromdate { get; set; }
        public string Todate { get; set; }
    }
    private void btndownload_Click(object sender, EventArgs e)
    {
         ClsMachineBL obj = new ClsMachineBL();
         ClsMachineML prp = new ClsMachineML();
        try
        {
            if (cbmachine.Text == "" || cbmachine.Text == "Select")
            {
                MessageBox.Show("Please Select Machine");
                cbmachine.Focus();
                return;
            }
            progressBar1.Visible = true;
              bool bIsConnected = axCZKEM1.Connect_Net(ip_address_of_your_machine, 4370);   // 4370 is port no of attendance machine
            if (bIsConnected == true)
            {
                IsRead = axCZKEM1.ReadGeneralLogData(dwMachineNumber);
                if (IsRead == true)
                {
                    progressBar1.Maximum = 100;
                    progressBar1.Step = 1;
                    progressBar1.Value = 0;
                    while (axCZKEM1.SSR_GetGeneralLogData(dwMachineNumber, out dwEnrollNumber, out dwVerifyMode, out dwInOutMode, out dwYear, out                                dwMonth, out dwDay, out dwHour, out dwMinute, out dwSecond, ref dwWorkcode))
                    {
                        prp.dwDay = dwDay;
                        prp.dwEnrollNumber = dwEnrollNumber;
                        prp.dwHour = dwHour;
                        prp.dwInOutMode = dwInOutMode;
                        prp.dwMachineNumber = dwMachineNumber;
                        prp.dwMinute = dwMinute;
                        prp.dwMonth = dwMonth;
                        prp.dwSecond = dwSecond;
                        prp.dwVerifyMode = dwVerifyMode;
                        prp.dwWorkcode = dwWorkcode;
                        prp.dwYear = dwYear;
                        string add = obj.DownloadDataFromBiomatrix(prp);
                        progressBar1.PerformStep();
                    }
                    string export = obj.ExportToAttendance(prp);
                    MessageBox.Show("Attendance Downloaded Successfully");
                    progressBar1.Visible = false;
                }
                else
                {
                    MessageBox.Show("No Log Found....");
                    progressBar1.Visible = false;
                }
            }
            else
            {
                MessageBox.Show("Device Not Connected");
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message.ToString());
        }
    }
    
  3. 使用 C#.NET 从数据库中的生物识别考勤机中删除考勤日志。

    public zkemkeeper.CZKEM axCZKEM1 = new zkemkeeper.CZKEM();
    private void btndownload_Click(object sender, EventArgs e)
    {
        bool bIsConnected = axCZKEM1.Connect_Net(ip_address_of_your_machine, 4370);   // 4370 is port no of attendance machine
        if (bIsConnected == true)
        {
            bool delete = axCZKEM1.ClearGLog(dwMachineNumber);
            if (delete == true)
            {
                MessageBox.Show("Deleted.....");
            }
            if (delete == false)
            {
                MessageBox.Show("No Log Found To Delete.....");
            }
        }
    }