コード例 #1
0
ファイル: BerConverter.cs プロジェクト: vossjannik/ldap4net
        private static int EncodingBerValMultiByteArrayHelperW(BerSafeHandle berElement, char fmt, byte[][] value)
        {
            var berValArray = IntPtr.Zero;

            Native.Native.SafeBerval[] managedBerVal = null;
            int rc;

            try
            {
                if (value != null)
                {
                    var structSize = Marshal.SizeOf(typeof(Native.Native.SafeBerval));

                    berValArray   = Marshal.AllocHGlobal((value.Length + 1) * structSize);
                    managedBerVal = new Native.Native.SafeBerval[value.Length];

                    for (var i = 0; i < value.Length; i++)
                    {
                        var byteArray = value[i];

                        // construct the managed berval
                        managedBerVal[i] = new Native.Native.SafeBerval();

                        if (byteArray == null)
                        {
                            managedBerVal[i].bv_len = 0;
                            managedBerVal[i].bv_val = IntPtr.Zero;
                        }
                        else
                        {
                            managedBerVal[i].bv_len = byteArray.Length;
                            managedBerVal[i].bv_val = Marshal.AllocHGlobal(byteArray.Length);
                            if (managedBerVal[i].bv_val != IntPtr.Zero)
                            {
                                Marshal.Copy(byteArray, 0, managedBerVal[i].bv_val, byteArray.Length);
                            }
                        }

                        var valPtr = new IntPtr((long)berValArray + i * structSize);
                        Marshal.StructureToPtr(managedBerVal[i], valPtr, false);
                    }

                    Marshal.StructureToPtr(new Native.Native.SafeBerval {
                        bv_len = 0, bv_val = IntPtr.Zero
                    }, new IntPtr((long)berValArray + value.Length * structSize), false);
                }

                rc = LdapNative.Instance.ber_printf_berarray(berElement, $"{{{fmt}}}", berValArray);

                GC.KeepAlive(managedBerVal);
            }
            finally
            {
                if (berValArray != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(berValArray);
                }
            }

            return(rc);
        }
コード例 #2
0
ファイル: BerConverter.cs プロジェクト: vossjannik/ldap4net
        private static int EncodingBerValMultiByteArrayHelper(BerSafeHandle berElement, char fmt, byte[][] value)
        {
            var berValArray = IntPtr.Zero;

            Native.Native.SafeBerval[] managedBerVal = null;
            int rc;

            try
            {
                if (value != null)
                {
                    berValArray = MarshalUtils.AllocHGlobalIntPtrArray(value.Length + 1);
                    var structSize = Marshal.SizeOf(typeof(Native.Native.SafeBerval));
                    managedBerVal = new Native.Native.SafeBerval[value.Length];

                    for (var i = 0; i < value.Length; i++)
                    {
                        var byteArray = value[i];

                        // construct the managed berval
                        managedBerVal[i] = new Native.Native.SafeBerval();

                        if (byteArray == null)
                        {
                            managedBerVal[i].bv_len = 0;
                            managedBerVal[i].bv_val = IntPtr.Zero;
                        }
                        else
                        {
                            managedBerVal[i].bv_len = byteArray.Length;
                            managedBerVal[i].bv_val = Marshal.AllocHGlobal(byteArray.Length);
                            if (managedBerVal[i].bv_val != IntPtr.Zero)
                            {
                                Marshal.Copy(byteArray, 0, managedBerVal[i].bv_val, byteArray.Length);
                            }
                        }

                        // allocate memory for the unmanaged structure
                        var valPtr = Marshal.AllocHGlobal(structSize);
                        Marshal.StructureToPtr(managedBerVal[i], valPtr, false);

                        Marshal.WriteIntPtr(berValArray, IntPtr.Size * i, valPtr);
                    }

                    Marshal.WriteIntPtr(berValArray, IntPtr.Size * value.Length, IntPtr.Zero);
                }

                rc = LdapNative.Instance.ber_printf_berarray(berElement, new string(fmt, 1), berValArray);

                GC.KeepAlive(managedBerVal);
            }
            finally
            {
                if (berValArray != IntPtr.Zero)
                {
                    foreach (var ptr in MarshalUtils.GetPointerArray(berValArray))
                    {
                        Marshal.FreeHGlobal(ptr);
                    }
                    Marshal.FreeHGlobal(berValArray);
                }
            }

            return(rc);
        }