// ----------------------------------------------------------------------------------- public bool ValidateItems(OPCItemDef[] arrDef, bool blobUpd, out OPCItemResult[] arrRes) { arrRes = null; bool hasblobs = false; int count = arrDef.Length; IntPtr ptrDef = Marshal.AllocCoTaskMem(count * sizeOPCITEMDEF); int runDef = (int)ptrDef; OPCITEMDEFintern idf = new OPCITEMDEFintern(); idf.wReserved = 0; foreach (OPCItemDef d in arrDef) { idf.szAccessPath = d.AccessPath; idf.szItemID = d.ItemID; idf.bActive = d.Active; idf.hClient = d.HandleClient; idf.vtRequestedDataType = (short)d.RequestedDataType; idf.dwBlobSize = 0; idf.pBlob = IntPtr.Zero; if (d.Blob != null) { idf.dwBlobSize = d.Blob.Length; if (idf.dwBlobSize > 0) { hasblobs = true; idf.pBlob = Marshal.AllocCoTaskMem(idf.dwBlobSize); Marshal.Copy(d.Blob, 0, idf.pBlob, idf.dwBlobSize); } } Marshal.StructureToPtr(idf, (IntPtr)runDef, false); runDef += sizeOPCITEMDEF; } IntPtr ptrRes; IntPtr ptrErr; int hresult = ifItems.ValidateItems(count, ptrDef, blobUpd, out ptrRes, out ptrErr); runDef = (int)ptrDef; if (hasblobs) { for (int i = 0; i < count; i++) { IntPtr blob = (IntPtr)Marshal.ReadInt32((IntPtr)(runDef + 20)); if (blob != IntPtr.Zero) { Marshal.FreeCoTaskMem(blob); } Marshal.DestroyStructure((IntPtr)runDef, typeOPCITEMDEF); runDef += sizeOPCITEMDEF; } } else { for (int i = 0; i < count; i++) { Marshal.DestroyStructure((IntPtr)runDef, typeOPCITEMDEF); runDef += sizeOPCITEMDEF; } } Marshal.FreeCoTaskMem(ptrDef); if (HRESULTS.Failed(hresult)) { Marshal.ThrowExceptionForHR(hresult); } int runRes = (int)ptrRes; int runErr = (int)ptrErr; if ((runRes == 0) || (runErr == 0)) { Marshal.ThrowExceptionForHR(HRESULTS.E_ABORT); } arrRes = new OPCItemResult[count]; for (int i = 0; i < count; i++) { arrRes[i] = new OPCItemResult(); arrRes[i].Error = Marshal.ReadInt32((IntPtr)runErr); if (HRESULTS.Failed(arrRes[i].Error)) { continue; } arrRes[i].HandleServer = Marshal.ReadInt32((IntPtr)runRes); arrRes[i].CanonicalDataType = (VarEnum)(int)Marshal.ReadInt16((IntPtr)(runRes + 4)); arrRes[i].AccessRights = (OPCACCESSRIGHTS)Marshal.ReadInt32((IntPtr)(runRes + 8)); int ptrblob = Marshal.ReadInt32((IntPtr)(runRes + 16)); if ((ptrblob != 0)) { int blobsize = Marshal.ReadInt32((IntPtr)(runRes + 12)); if (blobsize > 0) { arrRes[i].Blob = new byte[blobsize]; Marshal.Copy((IntPtr)ptrblob, arrRes[i].Blob, 0, blobsize); } Marshal.FreeCoTaskMem((IntPtr)ptrblob); } runRes += sizeOPCITEMRESULT; runErr += 4; } Marshal.FreeCoTaskMem(ptrRes); Marshal.FreeCoTaskMem(ptrErr); return(hresult == HRESULTS.S_OK); }
public bool ValidateItems(OPCItemDef[] arrDef, bool blobUpd, out OPCItemResult[] arrRes) { IntPtr ptr2; IntPtr ptr3; int num4; arrRes = null; bool flag = false; int length = arrDef.Length; IntPtr pItemArray = Marshal.AllocCoTaskMem(length * this.sizeOPCITEMDEF); int num2 = (int)pItemArray; OPCITEMDEFintern structure = new OPCITEMDEFintern(); structure.wReserved = 0; foreach (OPCItemDef def in arrDef) { structure.szAccessPath = def.AccessPath; structure.szItemID = def.ItemID; structure.bActive = def.Active; structure.hClient = def.HandleClient; structure.vtRequestedDataType = (short)def.RequestedDataType; structure.dwBlobSize = 0; structure.pBlob = IntPtr.Zero; if (def.Blob != null) { structure.dwBlobSize = def.Blob.Length; if (structure.dwBlobSize > 0) { flag = true; structure.pBlob = Marshal.AllocCoTaskMem(structure.dwBlobSize); Marshal.Copy(def.Blob, 0, structure.pBlob, structure.dwBlobSize); } } Marshal.StructureToPtr(structure, (IntPtr)num2, false); num2 += this.sizeOPCITEMDEF; } int hresultcode = this.ifItems.ValidateItems(length, pItemArray, blobUpd, out ptr2, out ptr3); num2 = (int)pItemArray; if (flag) { for (num4 = 0; num4 < length; num4++) { IntPtr ptr = (IntPtr)Marshal.ReadInt32((IntPtr)(num2 + 20)); if (ptr != IntPtr.Zero) { Marshal.FreeCoTaskMem(ptr); } Marshal.DestroyStructure((IntPtr)num2, this.typeOPCITEMDEF); num2 += this.sizeOPCITEMDEF; } } else { num4 = 0; while (num4 < length) { Marshal.DestroyStructure((IntPtr)num2, this.typeOPCITEMDEF); num2 += this.sizeOPCITEMDEF; num4++; } } Marshal.FreeCoTaskMem(pItemArray); if (HRESULTS.Failed(hresultcode)) { Marshal.ThrowExceptionForHR(hresultcode); } int num5 = (int)ptr2; int num6 = (int)ptr3; if ((num5 == 0) || (num6 == 0)) { Marshal.ThrowExceptionForHR(-2147467260); } arrRes = new OPCItemResult[length]; for (num4 = 0; num4 < length; num4++) { arrRes[num4] = new OPCItemResult(); arrRes[num4].Error = Marshal.ReadInt32((IntPtr)num6); if (!HRESULTS.Failed(arrRes[num4].Error)) { arrRes[num4].HandleServer = Marshal.ReadInt32((IntPtr)num5); arrRes[num4].CanonicalDataType = (VarEnum)Marshal.ReadInt16((IntPtr)(num5 + 4)); arrRes[num4].AccessRights = (OPCACCESSRIGHTS)Marshal.ReadInt32((IntPtr)(num5 + 8)); int num7 = Marshal.ReadInt32((IntPtr)(num5 + 0x10)); if (num7 != 0) { int num8 = Marshal.ReadInt32((IntPtr)(num5 + 12)); if (num8 > 0) { arrRes[num4].Blob = new byte[num8]; Marshal.Copy((IntPtr)num7, arrRes[num4].Blob, 0, num8); } Marshal.FreeCoTaskMem((IntPtr)num7); } num5 += this.sizeOPCITEMRESULT; num6 += 4; } } Marshal.FreeCoTaskMem(ptr2); Marshal.FreeCoTaskMem(ptr3); return(hresultcode == 0); }
public void TestCreateOPCITEMDEFintern01() { OPCITEMDEFintern oPCITEMDEFintern = CreateOPCITEMDEFintern01(); Assert.IsNotNull(oPCITEMDEFintern); }
internal static OPCITEMDEFintern CreateOPCITEMDEFintern01() { OPCITEMDEFintern oPCITEMDEFintern = new OPCITEMDEFintern(); return(oPCITEMDEFintern); }
// ------------------------ IOPCItemMgt --------------- internal void AddItems(OPCItem[] items) { OPCItemDef[] arrDef = new OPCItemDef[items.Length]; for (int i = 0; i < items.Length; i++) { arrDef[i] = new OPCItemDef(items[i].ID, items[i].HandleClient); } bool hasblobs = false; int count = arrDef.Length; IntPtr ptrDef = Marshal.AllocCoTaskMem(count * sizeOPCITEMDEF); int runDef = (int)ptrDef; OPCITEMDEFintern idf = new OPCITEMDEFintern(); idf.wReserved = 0; foreach (OPCItemDef d in arrDef) { idf.szAccessPath = d.AccessPath; idf.szItemID = d.ItemID; idf.bActive = d.Active; idf.hClient = d.HandleClient; idf.vtRequestedDataType = (short)d.RequestedDataType; idf.dwBlobSize = 0; idf.pBlob = IntPtr.Zero; if (d.Blob != null) { idf.dwBlobSize = d.Blob.Length; if (idf.dwBlobSize > 0) { hasblobs = true; idf.pBlob = Marshal.AllocCoTaskMem(idf.dwBlobSize); Marshal.Copy(d.Blob, 0, idf.pBlob, idf.dwBlobSize); } } Marshal.StructureToPtr(idf, (IntPtr)runDef, false); runDef += sizeOPCITEMDEF; } IntPtr ptrRes; IntPtr ptrErr; int hresult = ifItems.AddItems(count, ptrDef, out ptrRes, out ptrErr); runDef = (int)ptrDef; if (hasblobs) { for (int i = 0; i < count; i++) { IntPtr blob = (IntPtr)Marshal.ReadInt32((IntPtr)(runDef + 20)); if (blob != IntPtr.Zero) { Marshal.FreeCoTaskMem(blob); } Marshal.DestroyStructure((IntPtr)runDef, typeOPCITEMDEF); runDef += sizeOPCITEMDEF; } } else { for (int i = 0; i < count; i++) { Marshal.DestroyStructure((IntPtr)runDef, typeOPCITEMDEF); runDef += sizeOPCITEMDEF; } } Marshal.FreeCoTaskMem(ptrDef); if (HRESULTS.Failed(hresult)) { Marshal.ThrowExceptionForHR(hresult); } int runRes = (int)ptrRes; int runErr = (int)ptrErr; if ((runRes == 0) || (runErr == 0)) { Marshal.ThrowExceptionForHR(HRESULTS.E_ABORT); } for (int i = 0; i < count; i++) { OPCItem item = items[i]; item.Group = this; item.Error = Marshal.ReadInt32((IntPtr)runErr); if (HRESULTS.Failed(item.Error)) { continue; } item.HandleServer = Marshal.ReadInt32((IntPtr)runRes); item.DataType = Helper.VT2TypeCode((VarEnum)(int)Marshal.ReadInt16((IntPtr)(runRes + 4))); item.AccessRights = (OPCACCESSRIGHTS)Marshal.ReadInt32((IntPtr)(runRes + 8)); int ptrblob = Marshal.ReadInt32((IntPtr)(runRes + 16)); if ((ptrblob != 0)) { int blobsize = Marshal.ReadInt32((IntPtr)(runRes + 12)); if (blobsize > 0) { item.Blob = new byte[blobsize]; Marshal.Copy((IntPtr)ptrblob, item.Blob, 0, blobsize); } Marshal.FreeCoTaskMem((IntPtr)ptrblob); } runRes += sizeOPCITEMRESULT; runErr += 4; } Marshal.FreeCoTaskMem(ptrRes); Marshal.FreeCoTaskMem(ptrErr); if (hresult == HRESULTS.S_OK) { return; } else { string itemstring = ""; foreach (OPCItem item in items) { itemstring += item.ID + ","; } try { Exception excep = Marshal.GetExceptionForHR(hresult); throw (new Exception(excep.Message + " 添加OPCItem时出错" + itemstring)); } catch { throw (new Exception("HRESULTS:" + hresult + " 添加OPCItem时出错" + itemstring)); } } }
public void TestOPCITEMDEFinternConstructor01() { OPCITEMDEFintern oPCITEMDEFintern = new OPCITEMDEFintern(); }