public static int AuthorizationCopyRights(IntPtr authorization, AuthorizationItemSet rights, IntPtr environment, int flags, out AuthorizationItemSet authorizedRights) { var itemSize = Marshal.SizeOf <InternalAuthorizationItem>(); IntPtr itemsPointer = IntPtr.Zero; int result; // Convert the inner struct to a more "C native"-friendly struct var items = rights.Items.Select( x => new InternalAuthorizationItem { Name = Marshal.StringToHGlobalAuto(x.Name), Flags = x.Flags, Value = x.Value, ValueLength = x.ValueLength, }).ToList(); try { // Allocate space so we can copy the items-array into it itemsPointer = Marshal.AllocHGlobal(items.Count * itemSize); var iRights = new InternalAuthorizationItemSet { Count = rights.Count, Items = itemsPointer, }; var ptr = new IntPtr(itemsPointer.ToInt64()); // Copy all structures into our allocated area for (var i = 0; i < items.Count; i++) { Marshal.StructureToPtr(items[i], ptr, false); ptr = IntPtr.Add(ptr, itemSize); } result = InternalAuthorizationCopyRights(authorization, iRights, environment, flags, out var outRights); authorizedRights = new AuthorizationItemSet { Count = 0, Items = new AuthorizationItem[0], }; } finally { //if (itemsPointer.IsAllocated) // itemsPointer.Free(); for (var i = 0; i < items.Count; i++) { Marshal.FreeHGlobal(items[i].Name); } if (itemsPointer != IntPtr.Zero) { Marshal.FreeHGlobal(itemsPointer); } } return(result); }
private static extern int InternalAuthorizationCopyRights(IntPtr authorization, InternalAuthorizationItemSet rights, IntPtr environment, int flags, out InternalAuthorizationItemSet authorizedRights);