身份证阅读器(新中新)写入受保护内存问题,已解决

来源:互联网 发布:js 中替换eval方法 编辑:程序博客网 时间:2024/05/04 16:32

使用:

[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_ReadMsg", CharSet = CharSet.Ansi)]        public static extern int Syn_ReadMsg(int iPortID, int iIfOpen, ref IDCardData pIDCardData);

Syn_ReadBaseMsg 虽然不存在资源释放问题,但处理照片信息十分麻烦。

Syn_ReadMsg返回结构化信息,处理方便,但在其它线程中处理图片,直接使用图片报写入受保护内存。此问题处理方式分两步:

  1. 管理员方式运行;
  2. 先将图片复制到临时文件夹,再使用。

    一.使用管理员方式运行VS。
    项目中 添加应用程序清单文件:

打开app.manifest文件,将:

<requestedExecutionLevel  level="asInvoker" uiAccess="false" />

修改为:

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

然后打开 项目属性 ,将 应用程序 标签页中的 资源 中的 清单 修改为新建的 app.manifest。

重新生成项目
二.

     //设置路径及返回参数格式     private void Setter()    {        if (!System.IO.Directory.Exists(BaseConfig.SavePathPhoto))            System.IO.Directory.CreateDirectory(BaseConfig.SavePathPhoto);        if (!System.IO.Directory.Exists(BaseConfig.SavePathPhoto+"\\tmp"))            System.IO.Directory.CreateDirectory(BaseConfig.SavePathPhoto+"\\tmp");//临时文件夹        if (!System.IO.Directory.Exists(BaseConfig.SavePath))            System.IO.Directory.CreateDirectory(BaseConfig.SavePath);        player.LoadAsync();        byte[] cPath = new byte[255];        cPath = System.Text.Encoding.Default.GetBytes(BaseConfig.SavePathPhoto);        Syn_SetPhotoPath(2, ref cPath[0]);        // Syn_SetPhotoType(1);        Syn_SetPhotoName(3);        Syn_SetSexType(1);        Syn_SetNationType(2);        Syn_SetBornType(2);        Syn_SetUserLifeBType(2);        Syn_SetUserLifeEType(2,0);    }

            //删除临时文件夹的照片            try            {                string[] files = Directory.GetFiles(BaseConfig.SavePathPhoto + "\\tmp");                foreach (var f in files)                {                    try                    {                        File.Delete(f);                    }                    catch { }                }            }if(Syn_ReadMsg(BaseConfig.NPort, 0, ref CardMsg) == 0)               {                    Name.Text = CardMsg.Name.Trim();                    Sex.Text = CardMsg.Sex;                    Nation.Text = CardMsg.Nation;                    Born.Text = CardMsg.Born;                    Address.Text = CardMsg.Address;                    ID.Text = CardMsg.IDCardNo;                    GrantDept.Text = CardMsg.GrantDept;                    UserLifeBegin.Text = CardMsg.UserLifeBegin;                    Link.Text = "--";                    UserLifeEnd.Text = CardMsg.UserLifeEnd;//先将图片复制到临时文件夹,再显示                    string fileName = System.IO.Path.Combine(BaseConfig.SavePathPhoto, string.Format("{0}_{1}.bmp", CardMsg.Name.Trim(), CardMsg.IDCardNo));                    string newFileName = string.Format("{0}_{1}_{2}.bmp", Guid.NewGuid().ToString(), CardMsg.Name.Trim(), CardMsg.IDCardNo);                    string newPath = System.IO.Path.Combine(BaseConfig.SavePathPhoto, "tmp", newFileName);                    File.Copy(fileName, newPath);                    fileNameForUpdate = newPath;                    photo.Source = new BitmapImage(new Uri(newPath, UriKind.Absolute));                    }
0 0
原创粉丝点击