private static int GetTokenOffsetFromBlob(IntPtr blob) { Debug.Assert(blob != IntPtr.Zero); IntPtr tokenPointer = Marshal.ReadIntPtr(blob, (int)Marshal.OffsetOf <HttpApiTypes.HTTP_REQUEST_CHANNEL_BIND_STATUS>("ChannelToken")); Debug.Assert(tokenPointer != IntPtr.Zero); return((int)IntPtrHelper.Subtract(tokenPointer, blob)); }
public void IntPtrSubtractTest() { int[] arr = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 }; unsafe { fixed(int *parr = arr) { // Get the size of an array element. const int size = sizeof(int); var ptr = IntPtrHelper.Add(new IntPtr(parr), size * (arr.Length - 1)); var index = arr.Length - 1; for (var ctr = 0; ctr < arr.Length; ctr++) { var newPtr = IntPtrHelper.Subtract(ptr, ctr * size); Assert.AreEqual(arr[index], Marshal.ReadInt32(newPtr)); index--; } } } }