25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

SecurityAttributes.cs 3.3 KiB

7 yıl önce
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* Copyright (C) 2008-2016 Peter Palotas, Jeffrey Jangli, Alexandr Normuradov
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. * THE SOFTWARE.
  20. */
  21. using System;
  22. using System.Diagnostics.CodeAnalysis;
  23. using System.Runtime.InteropServices;
  24. using System.Security.AccessControl;
  25. namespace Alphaleonis.Win32.Security
  26. {
  27. internal static partial class NativeMethods
  28. {
  29. /// <summary>Class used to represent the SECURITY_ATTRIBUES native Win32 structure. It provides initialization function from an <see cref="ObjectSecurity"/> object.</summary>
  30. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  31. internal sealed class SecurityAttributes : IDisposable
  32. {
  33. // Removing this member results in: "Invalid access to memory location: ..."
  34. [MarshalAs(UnmanagedType.U4)]
  35. private int _length;
  36. private readonly SafeGlobalMemoryBufferHandle _securityDescriptor;
  37. public SecurityAttributes(ObjectSecurity securityDescriptor)
  38. {
  39. SafeGlobalMemoryBufferHandle safeBuffer = ToUnmanagedSecurityAttributes(securityDescriptor);
  40. _length = safeBuffer.Capacity;
  41. _securityDescriptor = safeBuffer;
  42. }
  43. /// <summary>Marshals an ObjectSecurity instance to unmanaged memory.</summary>
  44. /// <returns>A safe handle containing the marshalled security descriptor.</returns>
  45. /// <param name="securityDescriptor">The security descriptor.</param>
  46. [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")]
  47. private static SafeGlobalMemoryBufferHandle ToUnmanagedSecurityAttributes(ObjectSecurity securityDescriptor)
  48. {
  49. if (securityDescriptor == null)
  50. return new SafeGlobalMemoryBufferHandle();
  51. byte[] src = securityDescriptor.GetSecurityDescriptorBinaryForm();
  52. var safeBuffer = new SafeGlobalMemoryBufferHandle(src.Length);
  53. try
  54. {
  55. safeBuffer.CopyFrom(src, 0, src.Length);
  56. return safeBuffer;
  57. }
  58. catch
  59. {
  60. safeBuffer.Close();
  61. throw;
  62. }
  63. }
  64. public void Dispose()
  65. {
  66. if (_securityDescriptor != null)
  67. _securityDescriptor.Close();
  68. }
  69. }
  70. }
  71. }