中控考勤二次开发中的下载考勤时照片

考勤机是m880,需要在考勤机中设置保存考勤时照片。

SDK的开发文档中只有下载考勤记录的函数

ReadGeneralLogData

SSR_GetGeneralLogData

两者配合能读取到打卡记录。

文档中没有下载考勤照片的函数,在代码提示中查看photo。发现以下三个函数

public virtual bool GetPhotoNamesByTime(int dwMachineNumber, int iFlag, string sTime, string eTime, out string AllPhotoName);

public virtual bool GetPhotoCount(int dwMachineNumber, out int count, int iFlag);

public virtual bool GetPhotoByName(int dwMachineNumber, string PhotoName, out byte PhotoData, out int PhotoLength);

根据名字可以猜到功能。

GetPhotoNamesByTime读取考勤机中的照片名列表,照片名命名规则是时间+考勤号,多个名字用' '分隔。

GetPhotoByName根据名字下载照片。

但是在代码中如下调用时,失败,返回错误码-2

byte photoData;
int photolength = 0;
//////照片名需要加上“.jpg”
axCZKEM1.GetPhotoByName(iMachineNumber,"20150910063045" + ".jpg", out  photoData, out photolength)

找中控的售后,没人理。只能靠自己了。

用Wireshark分析电脑和考勤机的通讯,发现考勤机已经正确返回了照片的数据,照片数据是二进制数据。

GetPhotoByName函数的参数是out photoData,这个应该保存照片数据。

问题在于photoData是byte类型的,照片的二进制数据应该是byte[]类型。调用函数造成了向内存中非法写入数据。

想了个非常规的解决方法

unsafe
{
  byte[] photoData = new byte[1024 * 10];
  int photolength = 0;
  try
  {
    if (axCZKEM1.GetPhotoByName(iMachineNumber, arr[j].Trim() + ".jpg", out photoData[0], out photolength))
    {
      if (photolength < photoData.Length)
      {
        byte[] tmp = new byte[photolength];
        Array.Copy(photoData, tmp, photolength);
        System.IO.File.WriteAllBytes( "d:\photos\20150910063045.jpg", tmp);
       }
         }
       }
     catch (Exception ex) { }
 }

最后在项目的属性里设置:允许不安全代码。

原文地址:https://www.cnblogs.com/darksied/p/4797392.html