using Newtonsoft.Json.Linq; using System; using System.Runtime.InteropServices; using System.Text; namespace ThCardReader { class SsCardBusiness { private static SiBusinessAddress address; #if !DISABLE_SSCARD [DllImport("SSCard.dll", EntryPoint = "Init", CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] private extern static Int32 Init(string pUrl, string pUser); [DllImport("SSCard.dll", EntryPoint = "ReadCardBas", CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] private extern static Int32 ReadCardBas(StringBuilder pOutBuff, int nOutBuffLen, StringBuilder pSignBuff, int nSignBuffLen); [DllImport("SSCard.dll", EntryPoint = "ReadSFZ", CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] private extern static Int32 ReadSFZ(StringBuilder pOutBuff, int nOutBuffLen, StringBuilder pSignBuff, int nSignBuffLen); #endif private static Boolean initialized = false; public static string Initialize() { #if DISABLE_SSCARD return "SSCard.dll已屏蔽,请使用华视读卡器"; #else address = MainForm.address; Int32 initResultCode = Init(address.readSiCardUrl, address.readSiCardUser); if (initResultCode == 0) { initialized = true; return "初始化成功"; } return "初始化失败。"; #endif } [System.Runtime.ExceptionServices.HandleProcessCorruptedStateExceptions] public static JObject ReadSiCard() { #if DISABLE_SSCARD JObject result = new JObject(); result.Add("code", 1001); result.Add("message", "SSCard.dll已屏蔽,请使用华视读卡器:http://localhost:8321/readcard/entry?param=huashi_readcard"); result.Add("device", "SSCard.dll(已屏蔽)"); result.Add("suggestion", "请使用华视读卡器测试功能"); return result; #else if (!initialized) { Initialize(); } StringBuilder outptr = new StringBuilder(address.readSiCardBuffSize); StringBuilder signptr = new StringBuilder(address.readSiCardBuffSize); string outstr, signstr; JObject result = new JObject(); try { Int32 businessResultCode = ReadCardBas(outptr, address.readSiCardBuffSize, signptr, address.readSiCardBuffSize); outstr = outptr.ToString(); signstr = signptr.ToString(); if (businessResultCode == 0) { result.Add("code", 200); result.Add("type", "sicard"); result.Add("data", outstr); result.Add("sign", signstr); result.Add("message", "读取社保卡成功。"); } else { result.Add("code", 1001); result.Add("message", outstr); } } catch (Exception e) { initialized = false; result.Add("code", 1001); result.Add("message", "系统出错,请重试(如此错误频繁出现,可尝试重启读卡服务)。" + e.ToString()); } finally { outptr = null; signptr = null; outstr = null; signstr = null; GC.Collect(); } return result; #endif } [System.Runtime.ExceptionServices.HandleProcessCorruptedStateExceptions] public static JObject ReadIdCard() { #if DISABLE_SSCARD JObject result = new JObject(); result.Add("code", 1001); result.Add("message", "SSCard.dll已屏蔽,请使用华视读卡器:http://localhost:8321/readcard/entry?param=huashi_readcard"); result.Add("device", "SSCard.dll(已屏蔽)"); result.Add("suggestion", "请使用华视读卡器测试功能"); return result; #else if (!initialized) { Initialize(); } StringBuilder outptr = new StringBuilder(address.readIdCardBuffSize); StringBuilder signptr = new StringBuilder(address.readIdCardBuffSize); string outstr, signstr; JObject result = new JObject(); try { Int32 businessResultCode = ReadSFZ(outptr, address.readIdCardBuffSize, signptr, address.readIdCardBuffSize); outstr = outptr.ToString(); signstr = signptr.ToString(); if (businessResultCode == 0) { result.Add("code", 200); result.Add("type", "idcard"); result.Add("data", outstr); result.Add("sign", signstr); result.Add("message", "读取身份证成功。"); } else { result.Add("code", 1001); result.Add("message", outstr); } } catch (Exception e) { initialized = false; result.Add("code", 1001); result.Add("message", "系统出错,请重试(如此错误频繁出现,可尝试重启读卡服务)。" + e.ToString()); } finally { outptr = null; signptr = null; outstr = null; signstr = null; GC.Collect(); } return result; #endif } } }