コード例 #1
0
        public bool AddItems(OPCItemDef[] arrDef, 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.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);
            }

            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);
        }
コード例 #2
0
ファイル: OPCGroup.cs プロジェクト: Wolfury/MTConnect4OPC
        public bool AddItems(OPCItemDef[] arrDef, 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.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);

            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;
        }