Пример #1
0
        public override byte[] GetValue()
        {
            IntPtr intPtr;
            byte   num;
            IntPtr intPtr1 = (IntPtr)0;
            int    num1    = Marshal.SizeOf(typeof(SortKey));
            IntPtr intPtr2 = Utility.AllocHGlobalIntPtrArray((int)this.keys.Length + 1);

            try
            {
                int i = 0;
                for (i = 0; i < (int)this.keys.Length; i++)
                {
                    IntPtr intPtr3 = Marshal.AllocHGlobal(num1);
                    Marshal.StructureToPtr(this.keys[i], intPtr3, false);
                    intPtr = (IntPtr)((long)intPtr2 + (long)(Marshal.SizeOf(typeof(IntPtr)) * i));
                    Marshal.WriteIntPtr(intPtr, intPtr3);
                }
                intPtr = (IntPtr)((long)intPtr2 + (long)(Marshal.SizeOf(typeof(IntPtr)) * i));
                Marshal.WriteIntPtr(intPtr, (IntPtr)0);
                bool             isCritical = base.IsCritical;
                ConnectionHandle handle     = UtilityHandle.GetHandle();
                IntPtr           intPtr4    = intPtr2;
                if (isCritical)
                {
                    num = 1;
                }
                else
                {
                    num = 0;
                }
                int num2 = Wldap32.ldap_create_sort_control(handle, intPtr4, num, ref intPtr1);
                if (num2 == 0)
                {
                    LdapControl ldapControl = new LdapControl();
                    Marshal.PtrToStructure(intPtr1, ldapControl);
                    berval ldctlValue = ldapControl.ldctl_value;
                    this.directoryControlValue = null;
                    if (ldctlValue != null)
                    {
                        this.directoryControlValue = new byte[ldctlValue.bv_len];
                        Marshal.Copy(ldctlValue.bv_val, this.directoryControlValue, 0, ldctlValue.bv_len);
                    }
                }
                else
                {
                    if (!Utility.IsLdapError((LdapError)num2))
                    {
                        throw new LdapException(num2);
                    }
                    else
                    {
                        string str = LdapErrorMappings.MapResultCode(num2);
                        throw new LdapException(num2, str);
                    }
                }
            }
            finally
            {
                if (intPtr1 != (IntPtr)0)
                {
                    Wldap32.ldap_control_free(intPtr1);
                }
                if (intPtr2 != (IntPtr)0)
                {
                    for (int j = 0; j < (int)this.keys.Length; j++)
                    {
                        IntPtr intPtr5 = Marshal.ReadIntPtr(intPtr2, Marshal.SizeOf(typeof(IntPtr)) * j);
                        if (intPtr5 != (IntPtr)0)
                        {
                            IntPtr intPtr6 = Marshal.ReadIntPtr(intPtr5);
                            if (intPtr6 != (IntPtr)0)
                            {
                                Marshal.FreeHGlobal(intPtr6);
                            }
                            intPtr6 = Marshal.ReadIntPtr(intPtr5, Marshal.SizeOf(typeof(IntPtr)));
                            if (intPtr6 != (IntPtr)0)
                            {
                                Marshal.FreeHGlobal(intPtr6);
                            }
                            Marshal.FreeHGlobal(intPtr5);
                        }
                    }
                    Marshal.FreeHGlobal(intPtr2);
                }
            }
            return(base.GetValue());
        }
Пример #2
0
        public override byte[] GetValue()
        {
            IntPtr control    = (IntPtr)0;
            int    structSize = Marshal.SizeOf(typeof(SortKey));
            int    keyCount   = _keys.Length;
            IntPtr memHandle  = Utility.AllocHGlobalIntPtrArray(keyCount + 1);

            try
            {
                IntPtr tempPtr = (IntPtr)0;
                IntPtr sortPtr = (IntPtr)0;
                int    i       = 0;
                for (i = 0; i < keyCount; i++)
                {
                    sortPtr = Marshal.AllocHGlobal(structSize);
                    Marshal.StructureToPtr(_keys[i], sortPtr, false);
                    tempPtr = (IntPtr)((long)memHandle + Marshal.SizeOf(typeof(IntPtr)) * i);
                    Marshal.WriteIntPtr(tempPtr, sortPtr);
                }
                tempPtr = (IntPtr)((long)memHandle + Marshal.SizeOf(typeof(IntPtr)) * i);
                Marshal.WriteIntPtr(tempPtr, (IntPtr)0);

                bool critical = IsCritical;
                int  error    = Wldap32.ldap_create_sort_control(UtilityHandle.GetHandle(), memHandle, critical ? (byte)1 : (byte)0, ref control);

                if (error != 0)
                {
                    if (Utility.IsLdapError((LdapError)error))
                    {
                        string errorMessage = LdapErrorMappings.MapResultCode(error);
                        throw new LdapException(error, errorMessage);
                    }
                    else
                    {
                        throw new LdapException(error);
                    }
                }

                LdapControl managedControl = new LdapControl();
                Marshal.PtrToStructure(control, managedControl);
                berval value = managedControl.ldctl_value;
                // reinitialize the value
                directoryControlValue = null;
                if (value != null)
                {
                    directoryControlValue = new byte[value.bv_len];
                    Marshal.Copy(value.bv_val, directoryControlValue, 0, value.bv_len);
                }
            }
            finally
            {
                if (control != (IntPtr)0)
                {
                    Wldap32.ldap_control_free(control);
                }

                if (memHandle != (IntPtr)0)
                {
                    //release the memory from the heap
                    for (int i = 0; i < keyCount; i++)
                    {
                        IntPtr tempPtr = Marshal.ReadIntPtr(memHandle, Marshal.SizeOf(typeof(IntPtr)) * i);
                        if (tempPtr != (IntPtr)0)
                        {
                            // free the marshalled name
                            IntPtr ptr = Marshal.ReadIntPtr(tempPtr);
                            if (ptr != (IntPtr)0)
                            {
                                Marshal.FreeHGlobal(ptr);
                            }
                            // free the marshalled rule
                            ptr = Marshal.ReadIntPtr(tempPtr, Marshal.SizeOf(typeof(IntPtr)));
                            if (ptr != (IntPtr)0)
                            {
                                Marshal.FreeHGlobal(ptr);
                            }

                            Marshal.FreeHGlobal(tempPtr);
                        }
                    }
                    Marshal.FreeHGlobal(memHandle);
                }
            }

            return(base.GetValue());
        }
        public override byte[] GetValue()
        {
            IntPtr zero   = IntPtr.Zero;
            int    cb     = Marshal.SizeOf(typeof(SortKey));
            int    length = this.keys.Length;
            IntPtr keys   = Marshal.AllocHGlobal((int)(Marshal.SizeOf(typeof(IntPtr)) * (length + 1)));

            try
            {
                IntPtr ptr   = IntPtr.Zero;
                IntPtr ptr4  = IntPtr.Zero;
                int    index = 0;
                index = 0;
                while (index < length)
                {
                    ptr4 = Marshal.AllocHGlobal(cb);
                    Marshal.StructureToPtr(this.keys[index], ptr4, false);
                    ptr = (IntPtr)(((long)keys) + (Marshal.SizeOf(typeof(IntPtr)) * index));
                    Marshal.WriteIntPtr(ptr, ptr4);
                    index++;
                }
                ptr = (IntPtr)(((long)keys) + (Marshal.SizeOf(typeof(IntPtr)) * index));
                Marshal.WriteIntPtr(ptr, IntPtr.Zero);
                bool isCritical = base.IsCritical;
                int  errorCode  = Wldap32.ldap_create_sort_control(UtilityHandle.GetHandle(), keys, isCritical ? ((byte)1) : ((byte)0), ref zero);
                if (errorCode != 0)
                {
                    if (Utility.IsLdapError((LdapError)errorCode))
                    {
                        string message = LdapErrorMappings.MapResultCode(errorCode);
                        throw new LdapException(errorCode, message);
                    }
                    throw new LdapException(errorCode);
                }
                LdapControl structure = new LdapControl();
                Marshal.PtrToStructure(zero, structure);
                berval berval = structure.ldctl_value;
                base.directoryControlValue = null;
                if (berval != null)
                {
                    base.directoryControlValue = new byte[berval.bv_len];
                    Marshal.Copy(berval.bv_val, base.directoryControlValue, 0, berval.bv_len);
                }
            }
            finally
            {
                if (zero != IntPtr.Zero)
                {
                    Wldap32.ldap_control_free(zero);
                }
                if (keys != IntPtr.Zero)
                {
                    for (int i = 0; i < length; i++)
                    {
                        IntPtr ptr5 = Marshal.ReadIntPtr(keys, Marshal.SizeOf(typeof(IntPtr)) * i);
                        if (ptr5 != IntPtr.Zero)
                        {
                            IntPtr hglobal = Marshal.ReadIntPtr(ptr5);
                            if (hglobal != IntPtr.Zero)
                            {
                                Marshal.FreeHGlobal(hglobal);
                            }
                            hglobal = Marshal.ReadIntPtr(ptr5, Marshal.SizeOf(typeof(IntPtr)));
                            if (hglobal != IntPtr.Zero)
                            {
                                Marshal.FreeHGlobal(hglobal);
                            }
                            Marshal.FreeHGlobal(ptr5);
                        }
                    }
                    Marshal.FreeHGlobal(keys);
                }
            }
            return(base.GetValue());
        }