public BrowseResult[] Browse(string ItemId) { if (!m_connected) { throw new InvalidOperationException("Not connected to OPC-Server!"); } BrowseResult[] ret = null; IntPtr pContinuationPoint = IntPtr.Zero; int MaxElementsReturned = 10; // There is no client side restriction on the number of returned elements. int MoreElements = 0; int BrowseElementCount = 0; IntPtr BrowseElements = new IntPtr(1); int[] PropertyIds = new int[1]; try { m_browserDA3.Browse( ItemId, ref pContinuationPoint, MaxElementsReturned, OPCBROWSEFILTER.OPC_BROWSE_FILTER_ALL, "", // no name filter "", // no vendow filter 1, // return all properties 1, // return property values 0, // Property count, is ignored since all properties are returned PropertyIds, //array of property ids, is ignored since all properties are returned out MoreElements, out BrowseElementCount, out BrowseElements); OpcRcw.Da.OPCBROWSEELEMENT[] elements = GetBrowseElements(BrowseElements, BrowseElementCount); ret = new BrowseResult[BrowseElementCount]; int i = 0; foreach (OpcRcw.Da.OPCBROWSEELEMENT element in elements) { OpcRcw.Da.OPCITEMPROPERTIES ItemProperties = element.ItemProperties; IntPtr Properties = ItemProperties.pItemProperties; int PropertyCount = ItemProperties.dwNumProperties; ret[i++] = new BrowseResult { ItemId = element.szItemID, ItemName = element.szName, ItemProperties = GetItemProperties(Properties, PropertyCount) }; } } catch (Exception e) { throw e; } return(ret); }
/// <summary> /// IOPCBrowse::GetProperties - Returns an array of OPCITEMPROPERTIES, one for each ItemID. /// </summary> public void GetProperties( int dwItemCount, string[] pszItemIDs, int bReturnPropertyValues, int dwPropertyCount, int[] pdwPropertyIDs, out System.IntPtr ppItemProperties) { lock (m_lock) { try { if (dwItemCount == 0 || pszItemIDs == null) { throw ComUtils.CreateComException("E_INVALIDARG", ResultIds.E_INVALIDARG); } ppItemProperties = IntPtr.Zero; OPCITEMPROPERTIES[] results = new OPCITEMPROPERTIES[dwItemCount]; for (int ii = 0; ii < dwItemCount; ii++) { // initialize result. OPCITEMPROPERTIES result = new OPCITEMPROPERTIES(); result.hrErrorID = ResultIds.S_OK; result.dwReserved = 0; result.dwNumProperties = 0; result.pItemProperties = IntPtr.Zero; // lookup item id. NodeId nodeId = ItemIdToNodeId(pszItemIDs[ii]); if (nodeId == null) { result.hrErrorID = ResultIds.E_INVALIDITEMID; continue; } // find node. INode node = m_session.NodeCache.FetchNode(nodeId); if (node == null) { result.hrErrorID = ResultIds.E_UNKNOWNITEMID; continue; } // get properties. List<ItemProperty> properties = GetItemProperties( node, false, pdwPropertyIDs, bReturnPropertyValues != 0); // marshal properties. result = GetItemProperties(properties); } ppItemProperties = GetItemProperties(results); } catch (Exception e) { throw ComUtils.CreateComException(e); } } }
/// <summary> /// Allocates and marshals an array of OPCITEMPROPERTIES structures. /// </summary> internal static OpcRcw.Da.OPCITEMPROPERTIES GetItemProperties(IList<ItemProperty> input) { OpcRcw.Da.OPCITEMPROPERTIES output = new OpcRcw.Da.OPCITEMPROPERTIES(); if (input != null && input.Count > 0) { output.hrErrorID = ResultIds.S_OK; output.dwReserved = 0; output.dwNumProperties = input.Count; output.pItemProperties = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(OpcRcw.Da.OPCITEMPROPERTY))*input.Count); bool error = false; IntPtr pos = output.pItemProperties; for (int ii = 0; ii < input.Count; ii++) { OpcRcw.Da.OPCITEMPROPERTY property = GetItemProperty(input[ii]); Marshal.StructureToPtr(property, pos, false); pos = (IntPtr)(pos.ToInt32() + Marshal.SizeOf(typeof(OpcRcw.Da.OPCITEMPROPERTY))); if (input[ii].ErrorId < 0) { error = true; } } // set flag indicating one or more properties contained errors. if (error) { output.hrErrorID = ResultIds.S_FALSE; } } return output; }