Esempio n. 1
0
        public bool SetClientHandles(int[] p_ArrHSrv, int[] p_ArrHClt, out int[] p_ArrErr)
        {
            p_ArrErr = null;
            int l_Count = p_ArrHSrv.Length;

            if (l_Count != p_ArrHClt.Length)
            {
                Marshal.ThrowExceptionForHR(HResults.E_ABORT);
            }

            IntPtr l_PtrErr;
            int    l_HResult = m_IfItems.SetClientHandles(l_Count, p_ArrHSrv, p_ArrHClt, out l_PtrErr);

            if (HResults.Failed(l_HResult))
            {
                Marshal.ThrowExceptionForHR(l_HResult);
            }

            p_ArrErr = new int[l_Count];
            Marshal.Copy(l_PtrErr, p_ArrErr, 0, l_Count);
            Marshal.FreeCoTaskMem(l_PtrErr);

            return(l_HResult == HResults.S_OK);
        }
Esempio n. 2
0
        public void AddItems(string[] itemID, out int[] ItemHANDLES, out VarEnum[] arrayType)
        {
            int m_Res = 0;

            try
            {
                int         dwCount = itemID.Length;
                IntPtr      pErrors;
                IntPtr      pResults;
                IOPCItemMgt m_pIItemMgt = (IOPCItemMgt)pUnknown;
                //ItemMgt: OK
                OPCITEMDEF pItem = new OPCITEMDEF();

                //set pItem
                int temp = 0;
                pItem.bActive             = true;
                pItem.szAccessPath        = null;
                pItem.hClient             = temp;
                pItem.dwBlobSize          = 0;                  // no blob support
                pItem.pBlob               = IntPtr.Zero;
                pItem.vtRequestedDataType = (int)VarEnum.VT_EMPTY;
                pItem.wReserved           = 0;

                //alloc memory
                IntPtr ptrDef = Marshal.AllocCoTaskMem(dwCount * Marshal.SizeOf(pItem));
                int    runDef = (int)ptrDef;

                for (int i = 0; i < dwCount; i++)
                {
                    pItem.szItemID = itemID[i];
                    Marshal.StructureToPtr(pItem, (IntPtr)runDef, false);
                    runDef += Marshal.SizeOf(pItem);
                }
                //add item
                m_Res = m_pIItemMgt.AddItems(dwCount, ptrDef, out pResults, out pErrors);

                //dealoc memory
                runDef = (int)ptrDef;
                for (int i = 0; i < dwCount; i++)
                {
                    Marshal.DestroyStructure((IntPtr)runDef, pItem.GetType());
                    runDef += Marshal.SizeOf(pItem);
                }
                Marshal.FreeCoTaskMem(ptrDef);

                if (m_Res < 0)
                {
                    throw new Exception("Server: Add Items failed hr= " + m_Res.ToString());
                }

                //read result
                int runRes = (int)pResults;
                int runErr = (int)pErrors;
                if ((runRes == 0) || (runErr == 0))
                {
                    throw new Exception("Server: Add Items failed hr= " + unchecked ((int)0x80004004).ToString());
                }

                //init output arry
                ItemHANDLES = new int[dwCount];
                arrayType   = new VarEnum [dwCount];
                int error;
                for (int i = 0; i < dwCount; i++)
                {
                    error = Marshal.ReadInt32((IntPtr)runErr);
                    if (error < 0)
                    {
                        //parent.PrintMessage("Server: Add Item " + itemID[i] + " is failed hr =" + error.ToString(), MessageStyl.error);
                        ItemHANDLES[i] = 0;
                        arrayType[i]   = VarEnum.VT_EMPTY;
                    }
                    else
                    {
                        ItemHANDLES[i] = Marshal.ReadInt32((IntPtr)runRes);
                        arrayType[i]   = (VarEnum)(int)Marshal.ReadInt16((IntPtr)(runRes + 4));
                        //set server handle same as client handle
                        int[] tempHandle = new int[1];
                        tempHandle[0] = ItemHANDLES[i];
                        IntPtr setClientError;
                        m_pIItemMgt.SetClientHandles(1, tempHandle, tempHandle, out setClientError);
                        int s_err = Marshal.ReadInt32(setClientError);
                        //if (s_err < 0) parent.PrintMessage("Server: Add Item " + itemID[i] + " : Set the client handle is failed hr =" + s_err.ToString(), MessageStyl.error);
                    }

                    runRes += Marshal.SizeOf(new OPCITEMRESULT());
                    runErr += 4;
                }

                Marshal.FreeCoTaskMem(pResults);
                Marshal.FreeCoTaskMem(pErrors);
            }
            catch (Exception e)
            {
                //parent.PrintMessage(e.Message, MessageStyl.error);
                throw e;
            }
        }