/*------------------------------------------------------ * Execute SyncWrite * * (ret) True OK * False NG * ------------------------------------------------------*/ public bool SyncWrite(int[] ServerHd, object[] Value) { int iItemCount = ServerHd.Length; IOPCSyncIO OPCSyncIO; IOPCSyncIO2 OPCSyncIO2; IntPtr ppErrors; int[] errors = new int[iItemCount]; try { switch (m_OpcdaVer) { case DEF_OPCDA.VER_30: OPCSyncIO2 = (IOPCSyncIO2)m_OPCGroup2; OPCSyncIO2.Write(iItemCount, ServerHd, Value, out ppErrors); Marshal.Copy(ppErrors, errors, 0, iItemCount); Marshal.FreeCoTaskMem(ppErrors); break; case DEF_OPCDA.VER_10: case DEF_OPCDA.VER_20: default: OPCSyncIO = (IOPCSyncIO)m_OPCGroup; OPCSyncIO.Write(iItemCount, ServerHd, Value, out ppErrors); Marshal.Copy(ppErrors, errors, 0, iItemCount); Marshal.FreeCoTaskMem(ppErrors); break; } } catch (Exception exc) { MessageBox.Show(exc.ToString(), "SyncWrite"); return(false); } // erase errors System.GC.Collect(); //04/12/29 Revised Memory Leak return(true); }
/*------------------------------------------------------ * Execute ReadMaxAge * * (ret) True OK * False NG * ------------------------------------------------------*/ public bool ReadMaxAge(int MaxAge, int[] ServerHd, object[] Values, OpcRcw.Da.FILETIME[] TimeStamps, short[] Qualities) { if (m_OpcdaVer != DEF_OPCDA.VER_30) { //MsgBox("This function is for OPCDA3.0.") return(false); } int iItemCount = ServerHd.Length; IOPCSyncIO2 OPCSyncIO2; IntPtr ppItemVals; IntPtr ppQualities; IntPtr ppTimeStamps; IntPtr ppErrors; IntPtr posItem, posQual, posTime; int[] Errors = new int[iItemCount]; int[] MaxAges = new int[iItemCount]; OpcRcw.Da.FILETIME ftTimeStamp; int i; for (i = 0; i < iItemCount; i++) { MaxAges[i] = MaxAge; } try { OPCSyncIO2 = (IOPCSyncIO2)m_OPCGroup2; OPCSyncIO2.ReadMaxAge(iItemCount, ServerHd, MaxAges, out ppItemVals, out ppQualities, out ppTimeStamps, out ppErrors); Marshal.Copy(ppErrors, Errors, 0, iItemCount); posItem = ppItemVals; posQual = ppQualities; posTime = ppTimeStamps; for (i = 0; i < iItemCount; i++) { if (Errors[i] == 0) { Values[i] = Marshal.GetObjectForNativeVariant(posItem); Qualities[i] = Marshal.ReadInt16(posQual); ftTimeStamp = (OpcRcw.Da.FILETIME)Marshal.PtrToStructure(posTime, typeof(OpcRcw.Da.FILETIME)); TimeStamps[i] = ftTimeStamp; } VariantClear(posItem); // 05/02/08 Release memory Marshal.DestroyStructure(posQual, typeof(Int16)); // 1.0.0.5 10/06/21 Kishimoto Fixed for Memory Leak posItem = (IntPtr)(posItem.ToInt32() + 16); // "16" is size of "VARIANT" posQual = (IntPtr)(posQual.ToInt32() + 2); // "2" is size of "short" Marshal.DestroyStructure(posTime, typeof(OpcRcw.Da.FILETIME)); // 05/02/08 Release memory posTime = (IntPtr)(posTime.ToInt32() + Marshal.SizeOf(typeof(OpcRcw.Da.FILETIME))); // 1.0.0.5 10/06/21 Kishimoto Fixed for Memory Leak } Marshal.FreeCoTaskMem(ppItemVals); Marshal.FreeCoTaskMem(ppQualities); Marshal.FreeCoTaskMem(ppTimeStamps); Marshal.FreeCoTaskMem(ppErrors); } catch (Exception exc) { MessageBox.Show(exc.ToString(), "ReadMaxAge"); return(false); } // erase Errors // erase MaxAges System.GC.Collect(); //04/12/29 Revised Memory Leak return(true); }
/*------------------------------------------------------ * Execute SyncRead * * (ret) True OK * False NG * ------------------------------------------------------*/ public bool SyncRead(OpcRcw.Da.OPCDATASOURCE DataSource, int[] ServerHd, object[] Values, OpcRcw.Da.FILETIME[] TimeStamps, short[] Qualities) { int iItemCount = ServerHd.Length; IOPCSyncIO OPCSyncIO; IOPCSyncIO2 OPCSyncIO2; IntPtr ppItemVal; IntPtr ppErrors; IntPtr posItem; int[] Errors = new int[iItemCount]; OPCITEMSTATE ItemState; int i; try { switch (m_OpcdaVer) { case DEF_OPCDA.VER_30: OPCSyncIO2 = (IOPCSyncIO2)m_OPCGroup2; OPCSyncIO2.Read(DataSource, iItemCount, ServerHd, out ppItemVal, out ppErrors); Marshal.Copy(ppErrors, Errors, 0, iItemCount); posItem = ppItemVal; for (i = 0; i < iItemCount; i++) { ItemState = (OPCITEMSTATE)Marshal.PtrToStructure(posItem, typeof(OPCITEMSTATE)); if (Errors[i] == 0) { Values[i] = ItemState.vDataValue; TimeStamps[i] = ItemState.ftTimeStamp; Qualities[i] = ItemState.wQuality; } Marshal.DestroyStructure(posItem, typeof(OPCITEMSTATE)); // 05/02/08 Release memory //posItem = new IntPtr(posItem.ToInt32() + Marshal.SizeOf(typeof(OPCITEMSTATE))); // 1.0.0.5 10/06/21 Kishimoto Fixed for Memory Leak posItem = (IntPtr)(posItem.ToInt32() + Marshal.SizeOf(typeof(OpcRcw.Da.OPCITEMSTATE))); // 1.0.0.5 10/06/21 Kishimoto Fixed for Memory Leak } Marshal.FreeCoTaskMem(ppItemVal); Marshal.FreeCoTaskMem(ppErrors); break; case DEF_OPCDA.VER_10: case DEF_OPCDA.VER_20: default: OPCSyncIO = (IOPCSyncIO)m_OPCGroup; OPCSyncIO.Read(DataSource, iItemCount, ServerHd, out ppItemVal, out ppErrors); Marshal.Copy(ppErrors, Errors, 0, iItemCount); posItem = ppItemVal; for (i = 0; i < iItemCount; i++) { ItemState = (OPCITEMSTATE)Marshal.PtrToStructure(posItem, typeof(OPCITEMSTATE)); if (Errors[i] == 0) { Values[i] = ItemState.vDataValue; TimeStamps[i] = ItemState.ftTimeStamp; Qualities[i] = ItemState.wQuality; } Marshal.DestroyStructure(posItem, typeof(OPCITEMSTATE)); // 05/02/08 Release memory //posItem = new IntPtr(posItem.ToInt32() + Marshal.SizeOf(typeof(OPCITEMSTATE))); // 1.0.0.5 10/06/21 Kishimoto Fixed for Memory Leak posItem = (IntPtr)(posItem.ToInt32() + Marshal.SizeOf(typeof(OpcRcw.Da.OPCITEMSTATE))); // 1.0.0.5 10/06/21 Kishimoto Fixed for Memory Leak } Marshal.FreeCoTaskMem(ppItemVal); Marshal.FreeCoTaskMem(ppErrors); break; } } catch (Exception exc) { MessageBox.Show(exc.ToString(), "SyncRead"); return(false); } // erase Errors System.GC.Collect(); //04/12/29 Revised Memory Leak return(true); }