internal static Owner GetOwningModuleUDP(MIB_UDPROW_OWNER_MODULE row) { Owner ret = null; //if (ownerCache.TryGetValue(row, out ret)) //{ // return ret; //} IntPtr buffer = IntPtr.Zero; try { uint buffSize = 0; var retn = GetOwnerModuleFromUdpEntry(ref row, TCPIP_OWNER_MODULE_INFO_CLASS.TCPIP_OWNER_MODULE_INFO_BASIC, IntPtr.Zero, ref buffSize); if ((retn != NO_ERROR) && (retn != ERROR_INSUFFICIENT_BUFFER)) { //Cannot get owning module for this connection LogHelper.Info("Unable to get the connection owner."); return(ret); } if (buffSize == 0) { //No buffer? Probably means we can't retrieve any information about this connection; skip it LogHelper.Info("Unable to get the connection owner."); return(ret); } buffer = Marshal.AllocHGlobal((int)buffSize); //GetOwnerModuleFromUdpEntry might want the fields of TCPIP_OWNER_MODULE_INFO_BASIC to be NULL ZeroMemory(buffer, buffSize); var resp = GetOwnerModuleFromUdpEntry(ref row, TCPIP_OWNER_MODULE_INFO_CLASS.TCPIP_OWNER_MODULE_INFO_BASIC, buffer, ref buffSize); if (resp == 0) { ret = new Owner((TCPIP_OWNER_MODULE_BASIC_INFO)Marshal.PtrToStructure(buffer, typeof(TCPIP_OWNER_MODULE_BASIC_INFO))); } else if (resp != ERROR_NOT_FOUND) // Ignore closed connections { LogHelper.Error("Unable to get the connection owner.", new Win32Exception((int)resp)); } //ownerCache.Add(row, ret); return(ret); } finally { if (buffer != IntPtr.Zero) { Marshal.FreeHGlobal(buffer); } } }
public static extern uint GetOwnerModuleFromUdpEntry(ref MIB_UDPROW_OWNER_MODULE pUdpEntry, TCPIP_OWNER_MODULE_INFO_CLASS Class, IntPtr Buffer, ref uint pdwSize);