private PingReply InternalSend(IPAddress address, byte[] buffer, int timeout, PingOptions options) { if (handle == IntPtr.Zero) { handle = NativeMethods.IcmpCreateFile(); if (handle.ToInt32() == -1) { throw new Win32Exception(Marshal.GetLastWin32Error()); } } ICMP_ECHO_REPLY reply; var replyBuffer = new byte[65536]; var bufferHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned); var replyHandle = GCHandle.Alloc(replyBuffer, GCHandleType.Pinned); try { var ipoption = new IP_OPTION_INFORMATION(options); if (NativeMethods.IcmpSendEcho2(handle, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, BitConverter.ToUInt32(address.GetAddressBytes(), 0), bufferHandle.AddrOfPinnedObject(), (ushort)buffer.Length, ref ipoption, replyHandle.AddrOfPinnedObject(), (uint)replyBuffer.Length, (uint)timeout) == 0) { throw new Win32Exception(Marshal.GetLastWin32Error()); } } finally { reply = (ICMP_ECHO_REPLY)Marshal.PtrToStructure(replyHandle.AddrOfPinnedObject(), typeof(ICMP_ECHO_REPLY)); replyHandle.Free(); bufferHandle.Free(); } return(new PingReply(reply)); }
internal static extern uint IcmpSendEcho2(IntPtr icmpHandle, IntPtr hEvent, IntPtr apcRoutine, IntPtr apcContext, uint ipAddress, IntPtr data, ushort dataSize, ref IP_OPTION_INFORMATION options, IntPtr replyBuffer, uint replySize, uint timeout);
private PingReply InternalSend(IPAddress address, byte[] buffer, int timeout, PingOptions options) { if (handle == IntPtr.Zero) { handle = NativeMethods.IcmpCreateFile(); if (handle.ToInt32() == -1) { throw new Win32Exception(Marshal.GetLastWin32Error()); } } ICMP_ECHO_REPLY reply; var replyBuffer = new byte[65536]; var bufferHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned); var replyHandle = GCHandle.Alloc(replyBuffer, GCHandleType.Pinned); try { var ipoption = new IP_OPTION_INFORMATION(options); if (NativeMethods.IcmpSendEcho2(handle, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, BitConverter.ToUInt32(address.GetAddressBytes(), 0), bufferHandle.AddrOfPinnedObject(), (ushort)buffer.Length, ref ipoption, replyHandle.AddrOfPinnedObject(), (uint)replyBuffer.Length, (uint)timeout) == 0) { throw new Win32Exception(Marshal.GetLastWin32Error()); } } finally { reply = (ICMP_ECHO_REPLY)Marshal.PtrToStructure(replyHandle.AddrOfPinnedObject(), typeof(ICMP_ECHO_REPLY)); replyHandle.Free(); bufferHandle.Free(); } return new PingReply(reply); }