private static Native.Native.LdapControl ToLdapControl(DirectoryControl sourceCtrl) { var ctrl = new Native.Native.LdapControl { // Get the control type. ldctl_oid = Encoder.Instance.StringToPtr(sourceCtrl.Type), // Get the control cricality. ldctl_iscritical = sourceCtrl.IsCritical }; // Get the control value. var byteControlValue = sourceCtrl.GetValue(); if (byteControlValue == null || byteControlValue.Length == 0) { // Treat the control value as null. ctrl.ldctl_value = new Native.Native.berval { bv_len = 0, bv_val = IntPtr.Zero }; } else { ctrl.ldctl_value = new Native.Native.berval { bv_len = byteControlValue.Length, bv_val = Marshal.AllocHGlobal(byteControlValue.Length) }; Marshal.Copy(byteControlValue, 0, ctrl.ldctl_value.bv_val, ctrl.ldctl_value.bv_len); } return(ctrl); }
private static void FreeManagedControl(Native.Native.LdapControl ctrl) { if (ctrl.ldctl_oid != IntPtr.Zero) { Marshal.FreeHGlobal(ctrl.ldctl_oid); } if (ctrl.ldctl_value != null && ctrl.ldctl_value.bv_val != IntPtr.Zero) { Marshal.FreeHGlobal(ctrl.ldctl_value.bv_val); } }