/// <summary> /// /// </summary> /// <param name="accessRights">Desired access rights for process handle</param> /// <param name="pid">Process ID</param> /// <returns>Handle to process or <see cref="IntPtr.Zero"/> if process doesn't exists</returns> /// <exception cref="Win32Exception">Unexpected WinApi error</exception> public static IntPtr OpenProcessHandle(ProcessAccessRights accessRights, int pid) { var handle = IntPtr.Zero; try { handle = Kernel32.OpenProcess(accessRights, false, pid); if (handle == IntPtr.Zero) { var errorCode = Marshal.GetLastWin32Error(); if (errorCode == (int)ErrorCodes.ERROR_INVALID_PARAMETER) { return(IntPtr.Zero); // ERROR_INVALID_PARAMETER means that process doesn't exist. Don't throw exception here, because this error is expected and shouldn't crash ctor's } Win32ExceptionUtility.Throw(errorCode); } } catch { Kernel32.CloseHandle(handle); throw; } return(handle); }
private static unsafe int EstimateBufferSize(AddressFamily addressFamily, TCP_TABLE_CLASS tableClass) { var size = 0; var status = IPHlpApi.GetExtendedTcpTable(null, ref size, false, addressFamily, tableClass, 0); if (status != (int)ErrorCodes.ERROR_INSUFFICIENT_BUFFER) { Win32ExceptionUtility.Throw(status); } return(size); }
private unsafe HashSet <string> BuildCache() { var size = 1024; lock (sync) { while (true) { var buffer = resizeableBuffer.Get(ref size, false); fixed(byte *b = buffer) { var result = IPHlpApi.GetAdaptersAddresses( AddressFamily.AF_UNSPEC, GetAdaptersAddressesFlags.GAA_FLAG_INCLUDE_WINS_INFO | GetAdaptersAddressesFlags.GAA_FLAG_INCLUDE_GATEWAYS | GetAdaptersAddressesFlags.GAA_FLAG_INCLUDE_ALL_INTERFACES, IntPtr.Zero, b, ref size); if (result == (int)ErrorCodes.ERROR_BUFFER_OVERFLOW) { continue; } if (result == (int)ErrorCodes.ERROR_NO_DATA) { return(new HashSet <string>(StringComparer.OrdinalIgnoreCase)); } if (result != (int)ErrorCodes.ERROR_SUCCESS) { Win32ExceptionUtility.Throw(result); } var upInterfaces = new HashSet <string>(StringComparer.OrdinalIgnoreCase); for (var ptr = (IP_ADAPTER_ADDRESSES *)b; ptr != null; ptr = ptr->Next) { UnsafeHelper.CheckBounds(b, size, ptr, sizeof(IP_ADAPTER_ADDRESSES)); if (ptr->OperStatus == OperationalStatus.Up && ptr->IfType != NetworkInterfaceType.Loopback) { upInterfaces.Add(GetAdapterIdentity(new string(ptr->Description))); } } return(upInterfaces); } } } }
public static void EnsureProcessIsRunning(IntPtr handle, int pid) { if (handle == IntPtr.Zero) { ThrowOnNonExistentProcess(pid); } if (!Kernel32.GetExitCodeProcess(handle, out var exitCode)) { Win32ExceptionUtility.Throw(); } if (exitCode != ProcessExitCodes.STILL_ALIVE) { ThrowOnProcessExit(pid); } }
private static unsafe void GetTcpTableWithPid(AddressFamily addressFamily, ResizeableBuffer resizeableBuffer) { var size = EstimateBufferSize(addressFamily, TCP_TABLE_CLASS.TCP_TABLE_OWNER_PID_ALL); var buffer = resizeableBuffer.Get(ref size); while (true) { fixed(byte *b = buffer) { var result = IPHlpApi.GetExtendedTcpTable(b, ref size, false, addressFamily, TCP_TABLE_CLASS.TCP_TABLE_OWNER_PID_ALL, 0); if (result == (int)ErrorCodes.ERROR_INSUFFICIENT_BUFFER) { buffer = resizeableBuffer.Get(ref size); continue; } if (result != 0) { Win32ExceptionUtility.Throw(result); } var itemSize = addressFamily == AddressFamily.AF_INET ? sizeof(MIB_TCPROW_OWNER_PID) : sizeof(MIB_TCP6ROW_OWNER_PID); var itemCount = *(int *)b; if (itemCount * itemSize + 4 > buffer.Length) { throw new InvalidOperationException($"Buffer overflow check failed. ItemCount: {itemCount}, ItemSize: {itemSize}, BufferSize: {buffer.Length}"); } return; } } }
public static bool Is64BitProcess(int pid) { if (!Environment.Is64BitOperatingSystem) { return(false); } var handle = OpenLimitedQueryInformationProcessHandle(pid); try { if (!Kernel32.IsWow64Process(handle, out var result)) { Win32ExceptionUtility.Throw(); } return(!result); } finally { Kernel32.CloseHandle(handle); } }