public void GetItemProperties(string id, out string[] descriptions, out MyVariant[] values) { //out descriptions = null; values = null; //local variable int count; IntPtr propertyIDs; IntPtr des; IntPtr dataTypes; IntPtr data; IntPtr Error; int ret = m_pIProperties.QueryAvailableProperties(id, out count, out propertyIDs, out des, out dataTypes); if (ret >= 0 && ((int)propertyIDs) != 0 && ((int)dataTypes) != 0 && ((int)des) != 0) { if (count > 0) { int[] propIDs = IntArrayFromIntPtr(propertyIDs, count); VarEnum[] types = VarEnumArrayFromIntPtr(dataTypes, count); ret = m_pIProperties.GetItemProperties(id, count, propIDs, out data, out Error); if (ret >= 0 && ((int)data) != 0 && ((int)Error) != 0) { int[] err = IntArrayFromIntPtr(Error, count); descriptions = StringArrayFromIntPtr(des, count); values = new MyVariant[count]; int i = 0; int actData = (int)data; foreach (int item in err) { if (item == 0) { object temp = Marshal.GetObjectForNativeVariant((IntPtr)(actData)); values[i] = new MyVariant(temp, types[i]); } else { values[i] = new MyVariant(null, VarEnum.VT_EMPTY); //parent.PrintMessage("Server: Read Item Properties " + id + "(" + descriptions[i] + ")" + " is failed hr =" + item, MessageStyl.error); } i++; actData += 16; } Marshal.FreeCoTaskMem(data); } else { throw new Exception("Function GetItemProperties failed. Error code = " + ret.ToString()); } } } else { throw new Exception("Function QueryAvailableProperties failed. Error code = " + ret.ToString()); } }
public void ReadItems(string[] id, int[] ItemHANDLES, VarEnum[] arrayType, out MyVariant[] returnValue) { int m_Res; try { IntPtr ptrStat; IntPtr ptrErr; int dwCount = ItemHANDLES.Length; IOPCSyncIO m_pISync = (IOPCSyncIO)pUnknown; m_Res = m_pISync.Read(OPCDATASOURCE.OPC_DS_DEVICE, dwCount, ItemHANDLES, out ptrStat, out ptrErr); if (m_Res < 0) { throw new Exception("Server: Read Items failed hr= " + m_Res.ToString()); } int runErr = (int)ptrErr; int runStat = (int)ptrStat; if ((runErr == 0) || (runStat == 0)) { throw new Exception("Server: Read Items failed hr= " + unchecked ((int)0x80004004).ToString()); } returnValue = new MyVariant[dwCount]; for (int i = 0; i < dwCount; i++) { int error = Marshal.ReadInt32((IntPtr)runErr); runErr += 4; if (error >= 0) { object o = Marshal.GetObjectForNativeVariant((IntPtr)(runStat + 16)); returnValue[i] = new MyVariant(o, arrayType[i]); } else { returnValue[i] = new MyVariant(null, VarEnum.VT_EMPTY); //parent.PrintMessage("Server: Read Item " + id[i] + " is failed hr =" + error, MessageStyl.error); } runStat += 32; } Marshal.FreeCoTaskMem(ptrStat); Marshal.FreeCoTaskMem(ptrErr); } catch (Exception e) { //parent.PrintMessage(e.Message, MessageStyl.error); throw e; } }
public void SetValue(string str, VarEnum t) { this.type = t; try { switch (t) { case VarEnum.VT_EMPTY: this.value = 0; break; case VarEnum.VT_BOOL: value = Convert.ToBoolean(str); break; case VarEnum.VT_UI1: value = Convert.ToByte(str); break; case VarEnum.VT_I1: value = Convert.ToChar(str); break; case VarEnum.VT_I2: value = Convert.ToInt16(str); break; case VarEnum.VT_UI4: value = Convert.ToInt32(str); break; case VarEnum.VT_I4: value = Convert.ToInt32(str); break; case VarEnum.VT_DECIMAL: value = Convert.ToDecimal(str); break; case VarEnum.VT_R4: value = Convert.ToSingle(str); break; case VarEnum.VT_R8: value = Convert.ToDouble(str); break; //if variant= case VarEnum.VT_VARIANT: value = new MyVariant(str, t); break; case VarEnum.VT_BSTR: str = str.Replace("\"", ""); str = str.Replace("\'", ""); value = str; break; case VarEnum.VT_UI1 | VarEnum.VT_ARRAY: value = null; break; case VarEnum.VT_BSTR | VarEnum.VT_ARRAY: value = null; break; case VarEnum.VT_I4 | VarEnum.VT_ARRAY: value = null; break; case VarEnum.VT_BOOL | VarEnum.VT_ARRAY: value = null; break; case VarEnum.VT_VARIANT | VarEnum.VT_ARRAY: value = null; break; default: { this.value = null; throw new Exception("Unknow type: " + t.ToString()); } } } catch (Exception e) { throw new Exception("Bad type conversion: (" + e.Message + ")"); } }