using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.ComponentModel; using System.Data; using System.Drawing; using System.Windows.Forms; using System.Reflection; using System.Net; using System.Management; using System.IO; using PTMedicalInsurance.Variables; using PTMedicalInsurance.Helper; using Newtonsoft.Json.Linq; using Newtonsoft.Json; using System.Runtime.InteropServices; using MedicareComLib; namespace PTMedicalInsurance { public class ResourceHolder : IDisposable { private bool disposed = false; // 假设这是一个非托管资源 private IntPtr unmanagedResource; public ResourceHolder() { // 获取非托管资源 this.unmanagedResource = GetUnmanagedResource(); } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (!disposed) { if (disposing) { // 释放托管资源 } // 释放非托管资源 ReleaseUnmanagedResource(this.unmanagedResource); this.unmanagedResource = IntPtr.Zero; } disposed = true; } ~ResourceHolder() { Dispose(false); } // 假设的非托管资源获取方法 private IntPtr GetUnmanagedResource() { // 获取非托管资源的代码 return IntPtr.Zero; } // 假设的非托管资源释放方法 private void ReleaseUnmanagedResource(IntPtr resource) { // 释放非托管资源的代码 } } class SaveLogInData { /// /// 保存中心交易日志到数据库 /// /// /// /// /// private void saveCenterLog(string inParam, string outParam, string inParamPlain, string outParamPlain) { dynamic joIris = new JObject(); string sRtn = ""; try { //解析postContent,插入医保交易日志表 JArray jaParams = new JArray(); JObject joParam = new JObject(); joParam.Add("inParam", inParam); joParam.Add("outParam", outParam); joParam.Add("inParamPlain", inParamPlain); joParam.Add("outParamPlain", outParamPlain); joParam.Add("HospitalDr", Global.inf.hospitalDr); joParam.Add("InterfaceDr", Global.inf.interfaceDr); joParam.Add("updateUserID", Global.user.ID); joParam.Add("psn_no", Global.pat.psn_no); jaParams.Add(joParam); joIris.code = "09010021"; joIris.Add("params", jaParams); InvokeHelper invoker = new InvokeHelper(); sRtn = invoker.invokeInsuService(joIris.ToString(), "保存日志到数据库").ToString(); } catch (Exception ex) { sRtn = JsonHelper.setExceptionJson(-100, "保存日志异常", ex.Message).ToString(); Global.writeLog_Iris("保存日志异常:" + sRtn.ToString()); } } } class ReadCardClass { OutpatientClass ShouXinObj = new OutpatientClass(); //释放资源方法一 public static void ReleaseComObject(object obj) { while ((Marshal.ReleaseComObject(obj)) > 0) ; } //释放资源方法二 public static void ReleaseComObject2(object obj) { int result = 0; do { result = Marshal.ReleaseComObject(obj); } while (result > 0); // 将引用设置为null obj = null; } //释放资源方法三 public static void ReleaseComObject3(object obj) { int result = 0; do { result = Marshal.FinalReleaseComObject(obj); } while (result > 0); // 将引用设置为null obj = null; } public class ResourceHolder : IDisposable { private bool disposed = false; // 用于跟踪资源是否已经被释放 // 实现 IDisposable 接口 public void Dispose() { Dispose(true); GC.SuppressFinalize(this); // 防止垃圾回收器再次调用析构函数 } protected virtual void Dispose(bool disposing) { if (!disposed) { if (disposing) { // 释放托管资源 } // 释放非托管资源 disposed = true; } } ~ResourceHolder() { Dispose(false); } // 其他对象方法 } public int CreateBusinessObj(out System.Type OutComType,out object OutComInstance) { string rtn = ""; string progID = "MedicareCom.Outpatient"; OutComType = null; OutComInstance = ""; try { OutComType = Type.GetTypeFromProgID(progID); // 创建Com的实例 if (OutComType != null) { //创建实例 OutComInstance = Activator.CreateInstance(OutComType); rtn = "成功"; return 0; } else { rtn = "失败"; return -1; } } catch (Exception ex) { rtn = "失败" + ",COM加载异常" + ex.Message; return -1; } finally { Global.writeLog("GetReadCardObj", "", "创建实例" + rtn); } } public void Open(System.Type ComType, object ComInstance,out string OutPut) { OutPut = ""; try { if (ComInstance == null) { OutPut = "实例不存在!"; return; } //设置需要设置的参数值 object[] ParamArray = new object[1]; ParamArray[0] = ""; //ParamArray[1] = input; ParameterModifier[] ParamMods = new ParameterModifier[1]; ParamMods[0] = new ParameterModifier(1); // 初始化为接口参数的个数 ParamMods[0][0] = true; ComType.InvokeMember("open", // 接口函数名 BindingFlags.Default | BindingFlags.InvokeMethod, null, ComInstance, // 调用的COM组件 ParamArray, // 参数数组 ParamMods, // 指定返回参数的ParameterModifier数组 null, null); OutPut = ParamArray[0].ToString(); //MessageBox.Show("Com输出:" + OutPut); return; } catch (Exception ex) { OutPut = ex.Message; MessageBox.Show("COM加载异常!" + OutPut); return; } finally { Global.writeLog("Open", "", OutPut); //this.saveCenterLog("", OutPut, "", OutPut); } } public void Close(System.Type ComType, object ComInstance, out string OutPut) { OutPut = ""; try { if (ComInstance == null) { OutPut = "实例不存在!"; return; } //GlobalVariables.writeLog("入参infno:" + infno); //GlobalVariables.writeLog("入参input:" + input); //设置需要设置的参数值 object[] ParamArray = new object[1]; ParamArray[0] = ""; //ParamArray[1] = input; ParameterModifier[] ParamMods = new ParameterModifier[1]; ParamMods[0] = new ParameterModifier(1); // 初始化为接口参数的个数 ParamMods[0][0] = true; ComType.InvokeMember("close", // 接口函数名 BindingFlags.Default | BindingFlags.InvokeMethod, null, ComInstance, // 调用的COM组件 ParamArray, // 参数数组 ParamMods, // 指定返回参数的ParameterModifier数组 null, null); OutPut = ParamArray[0].ToString(); //MessageBox.Show("Com输出:" + OutPut); return; } catch (Exception ex) { OutPut = ex.Message; MessageBox.Show("COM加载异常!" + OutPut); return; } finally { Global.writeLog("Close", "", OutPut); // 释放资源 (ComInstance as IDisposable)?.Dispose(); GC.Collect(); if (ComInstance is IDisposable disposable) { disposable.Dispose(); } //GC.Collect(); ////ReleaseComObject(ComInstance); ////ReleaseComObject2(ComInstance); //ReleaseComObject3(ComInstance); ////ReleaseComObject3(ComType); ComInstance = null; ComType = null; } } public void ReadCardObj(string FunName, string Input, System.Type ComType, object ComInstance, out string OutPut) { OutPut = ""; try { if (ComInstance == null) { OutPut = "实例不存在!"; return; } //设置需要设置的参数值 object[] ParamArray = new object[2]; ParamArray[0] = Input; ParamArray[1] = Input; ParameterModifier[] ParamMods = new ParameterModifier[1]; ParamMods[0] = new ParameterModifier(2); // 初始化为接口参数的个数 ParamMods[0][0] = true; ParamMods[0][1] = true; ComType.InvokeMember(FunName, // 接口函数名 BindingFlags.Default | BindingFlags.InvokeMethod, null, ComInstance, // 调用的COM组件 ParamArray, // 参数数组 ParamMods, // 指定返回参数的ParameterModifier数组 null, null); OutPut = ParamArray[1].ToString(); return; } catch (Exception ex) { OutPut = ex.Message; MessageBox.Show("COM加载异常!" + OutPut); return; } finally { Global.writeLog(FunName, Input, OutPut); } } public void BussniessObj(string FunName, string Input, System.Type ComType, object ComInstance, out string OutPut) { OutPut = ""; try { if (ComInstance == null) { OutPut = "实例不存在!"; return; } //设置需要设置的参数值 object[] ParamArray = new object[2]; ParamArray[0] = Input; ParamArray[1] = Input; ParameterModifier[] ParamMods = new ParameterModifier[1]; ParamMods[0] = new ParameterModifier(2); // 初始化为接口参数的个数 ParamMods[0][0] = true; ParamMods[0][1] = true; ComType.InvokeMember(FunName, // 接口函数名 BindingFlags.Default | BindingFlags.InvokeMethod, null, ComInstance, // 调用的COM组件 ParamArray, // 参数数组 ParamMods, // 指定返回参数的ParameterModifier数组 null, null); OutPut = ParamArray[1].ToString(); return; } catch (Exception ex) { OutPut = ex.Message; MessageBox.Show("COM加载异常!" + OutPut); return; } finally { Global.writeLog(FunName, Input, OutPut); this.saveCenterLog(Input, OutPut, Input, OutPut); } } public void Trade(System.Type ComType, object ComInstance, out string OutPut) { OutPut = ""; try { if (ComInstance == null) { OutPut = "实例不存在!"; return; } //设置需要设置的参数值 object[] ParamArray = new object[1]; ParamArray[0] = ""; //ParamArray[1] = input; ParameterModifier[] ParamMods = new ParameterModifier[1]; ParamMods[0] = new ParameterModifier(1); // 初始化为接口参数的个数 ParamMods[0][0] = true; ComType.InvokeMember("Trade", // 接口函数名 BindingFlags.Default | BindingFlags.InvokeMethod, null, ComInstance, // 调用的COM组件 ParamArray, // 参数数组 ParamMods, // 指定返回参数的ParameterModifier数组 null, null); OutPut = ParamArray[0].ToString(); //MessageBox.Show("Com输出:" + OutPut); return; } catch (Exception ex) { OutPut = ex.Message; MessageBox.Show("COM加载异常!" + OutPut); return; } finally { Global.writeLog("Trade", "", OutPut); } } public void TradeAll(System.Type ComType, object ComInstance, out string OutPut) { OutPut = ""; try { if (ComInstance == null) { OutPut = "实例不存在!"; return; } //设置需要设置的参数值 object[] ParamArray = new object[1]; ParamArray[0] = ""; //ParamArray[1] = input; ParameterModifier[] ParamMods = new ParameterModifier[1]; ParamMods[0] = new ParameterModifier(1); // 初始化为接口参数的个数 ParamMods[0][0] = true; ComType.InvokeMember("TradeAll", // 接口函数名 BindingFlags.Default | BindingFlags.InvokeMethod, null, ComInstance, // 调用的COM组件 ParamArray, // 参数数组 ParamMods, // 指定返回参数的ParameterModifier数组 null, null); OutPut = ParamArray[0].ToString(); return; } catch (Exception ex) { OutPut = ex.Message; MessageBox.Show("COM加载异常!" + OutPut); return; } finally { Global.writeLog("TradeAll", "", OutPut); //this.saveCenterLog("", OutPut, "", OutPut); } } public void Open(out string OutPut) { OutPut = ""; try { ShouXinObj.Open(out OutPut); return; } catch (Exception ex) { OutPut = ex.Message; MessageBox.Show("Open加载异常!" + OutPut); return; } finally { Global.writeLog("Open", "", OutPut); //this.saveCenterLog("", OutPut, "", OutPut); } } public void Close(out string OutPut) { OutPut = ""; try { ShouXinObj.Close(out OutPut); return; } catch (Exception ex) { OutPut = ex.Message; MessageBox.Show("Close加载异常!" + OutPut); return; } finally { ShouXinObj = null; Global.writeLog("Close", "", OutPut); //this.saveCenterLog("", OutPut, "", OutPut); } } public void GetPersonInfo(out string OutPut) { OutPut = ""; try { ShouXinObj.GetPersonInfo(out OutPut); return; } catch (Exception ex) { OutPut = ex.Message; MessageBox.Show("GetPersonInfo加载异常!" + OutPut); return; } finally { Global.writeLog("GetPersonInfo", "", OutPut); //this.saveCenterLog("", OutPut, "", OutPut); } } /// /// 保存中心交易日志到数据库 /// /// /// /// /// private void saveCenterLog(string inParam, string outParam, string inParamPlain, string outParamPlain) { dynamic joIris = new JObject(); string sRtn = ""; try { //解析postContent,插入医保交易日志表 JArray jaParams = new JArray(); JObject joParam = new JObject(); joParam.Add("inParam", inParam); joParam.Add("outParam", outParam); joParam.Add("inParamPlain", inParamPlain); joParam.Add("outParamPlain", outParamPlain); joParam.Add("HospitalDr", Global.inf.hospitalDr); joParam.Add("InterfaceDr", Global.inf.interfaceDr); joParam.Add("updateUserID", Global.user.ID); joParam.Add("psn_no", Global.pat.psn_no); jaParams.Add(joParam); joIris.code = "09010021"; joIris.Add("params", jaParams); InvokeHelper invoker = new InvokeHelper(); sRtn = invoker.invokeInsuService(joIris.ToString(), "保存日志到数据库").ToString(); } catch (Exception ex) { sRtn = JsonHelper.setExceptionJson(-100, "保存日志异常", ex.Message).ToString(); Global.writeLog_Iris("保存日志异常:" + sRtn.ToString()); } } } class MedOrdersShareClass { public int GetMedOrdersShareObj(out System.Type OutComType, out object OutComInstance) { Object rtnObj = ""; string progID = "EachCommunicationCom.Outpatient"; System.Type ComType; object ComInstance; ComType = Type.GetTypeFromProgID(progID); // 创建Com的实例 if (ComType != null) { //创建实例 ComInstance = Activator.CreateInstance(ComType); OutComType = ComType; OutComInstance = ComInstance; return 0; } else { OutComType = null; OutComInstance = ""; return -1; } } public void ConnectServer(System.Type ComType, object ComInstance, out string OutPut) { OutPut = ""; try { if (ComInstance == null) { OutPut = "实例不存在!"; return; } //GlobalVariables.writeLog("入参infno:" + infno); //GlobalVariables.writeLog("入参input:" + input); //设置需要设置的参数值 object[] ParamArray = new object[1]; ParamArray[0] = ""; //ParamArray[1] = input; ParameterModifier[] ParamMods = new ParameterModifier[1]; ParamMods[0] = new ParameterModifier(1); // 初始化为接口参数的个数 ParamMods[0][0] = true; ComType.InvokeMember("ConnectServer", // 接口函数名 BindingFlags.Default | BindingFlags.InvokeMethod, null, ComInstance, // 调用的COM组件 ParamArray, // 参数数组 ParamMods, // 指定返回参数的ParameterModifier数组 null, null); OutPut = ParamArray[0].ToString(); //MessageBox.Show("ConnectServer输出:" + OutPut); return; } catch (Exception ex) { OutPut = ex.Message; //MessageBox.Show("COM加载异常!" + OutPut); return; } finally { Global.writeLog("ConnectServer", "", OutPut); } } public void MedOrdersShareObj(string FunName, string Input, System.Type ComType, object ComInstance, out string OutPut) { OutPut = ""; try { if (ComInstance == null) { OutPut = "实例不存在!"; return; } //GlobalVariables.writeLog("入参infno:" + infno); //GlobalVariables.writeLog("入参input:" + input); //设置需要设置的参数值 object[] ParamArray = new object[2]; ParamArray[0] = Input; ParamArray[1] = Input; ParameterModifier[] ParamMods = new ParameterModifier[1]; ParamMods[0] = new ParameterModifier(2); // 初始化为接口参数的个数 ParamMods[0][0] = true; ParamMods[0][1] = true; ComType.InvokeMember(FunName, // 接口函数名 BindingFlags.Default | BindingFlags.InvokeMethod, null, ComInstance, // 调用的COM组件 ParamArray, // 参数数组 ParamMods, // 指定返回参数的ParameterModifier数组 null, null); OutPut = ParamArray[1].ToString(); return; } catch (Exception ex) { OutPut = ex.Message; MessageBox.Show("COM加载异常!" + OutPut); return; } finally { Global.writeLog(FunName, Input, OutPut); } } } }