{"record":{"id":"c29cb43bbfaf39f1","repo":"BornToBeRoot/NETworkManager","slug":"win32exception-for-native-error-code-int-result","errorCode":null,"errorMessage":"Win32Exception for native error code (int)result","messagePattern":"Win32Exception for native error code \\(int\\)result","errorType":"exception","errorClass":"Win32Exception","httpStatus":null,"severity":"error","filePath":"Source/NETworkManager.Models/Network/NeighborTable.cs","lineNumber":161,"sourceCode":"    /// are suppressed.\n    /// </summary>\n    private static List<NeighborInfo> GetTable()\n    {\n        var list = new List<NeighborInfo>();\n\n        var virtualMAC = new PhysicalAddress([0, 0, 0, 0, 0, 0]);\n        var broadcastMAC = new PhysicalAddress([255, 255, 255, 255, 255, 255]);\n\n        var aliasMap = GetCachedInterfaceAliasMap();\n\n        var table = IntPtr.Zero;\n\n        try\n        {\n            var result = GetIpNetTable2(AF_UNSPEC, out table);\n\n            if (result != 0)\n                throw new Win32Exception((int)result);\n\n            // First 4 bytes hold NumEntries; the array of MIB_IPNET_ROW2 starts after\n            // 4 bytes of padding (the row contains a ULONG64, requiring 8-byte alignment).\n            var numEntries = Marshal.ReadInt32(table);\n            var rowSize = Marshal.SizeOf<MIB_IPNET_ROW2>();\n            var arrayPtr = IntPtr.Add(table, 8);\n\n            for (var i = 0; i < numEntries; i++)\n            {\n                var row = Marshal.PtrToStructure<MIB_IPNET_ROW2>(IntPtr.Add(arrayPtr, i * rowSize));\n\n                var family = BitConverter.ToUInt16(row.Address, 0);\n\n                IPAddress ipAddress;\n                AddressFamily addressFamily;\n\n                switch (family)\n                {","sourceCodeStart":143,"sourceCodeEnd":179,"githubUrl":"https://github.com/BornToBeRoot/NETworkManager/blob/2780d65469917a296dbc15f206107d72e2d0115c/Source/NETworkManager.Models/Network/NeighborTable.cs#L143-L179","documentation":"GetTable calls the native GetIpNetTable2 (ARP/neighbor table) and, when it returns a non-zero HRESULT/error code, throws new Win32Exception((int)result). The Win32Exception message is derived from the native code (e.g. ERROR_NO_DATA = 'Element not found' when the neighbor table is empty, ERROR_NOT_ENOUGH_MEMORY, or access errors).","triggerScenarios":"Calling GetTable (via the public entry that enumerates neighbor entries) on a system/interface with no ARP entries (ERROR_NO_DATA, code 1168), when the API is unavailable, or on allocation failure.","commonSituations":"Querying immediately after an interface comes up with an empty neighbor cache; running in restricted environments (services with limited access to network stack APIs); older Windows versions without GetIpNetTable2 behavior expectations.","solutions":["Treat error 1168 (ERROR_NO_DATA / empty table) as an empty result instead of an exception.","Catch Win32Exception and check NativeErrorCode before surfacing the error to the UI.","Retry after a short delay if the interface was just enabled.","Run with sufficient privileges; verify the machine has at least one active IPv4/IPv6 interface."],"exampleFix":"// before\nvar result = GetIpNetTable2(AF_UNSPEC, out table);\nif (result != 0)\n    throw new Win32Exception((int)result);\n// after\nvar result = GetIpNetTable2(AF_UNSPEC, out table);\nif (result == 1168) // ERROR_NO_DATA: empty neighbor table\n    return Enumerable.Empty<MIB_IPNET_ROW2>();\nif (result != 0)\n    throw new Win32Exception((int)result);","handlingStrategy":"try-catch","validationCode":"null","typeGuard":null,"tryCatchPattern":"try\n{\n    var entries = NeighborTable.GetTable();\n}\ncatch (Win32Exception ex) when (ex.NativeErrorCode == 1168)\n{\n    // empty ARP table - not an error\n    var entries = Enumerable.Empty<NeighborTableEntryInfo>();\n}\ncatch (Win32Exception ex)\n{\n    Log.Warn($\"GetIpNetTable2 failed: {ex.NativeErrorCode} {ex.Message}\");\n    throw;\n}","preventionTips":["Handle ERROR_NO_DATA (1168) as an empty result, not a failure.","Catch the specific Win32Exception type, not bare Exception, to read NativeErrorCode.","Retry shortly after interface changes (NIC up/down clears the neighbor cache)."],"tags":["p-invoke","arp","windows","network"],"backgroundTag":"api-error-response","analyzedSha":"2780d65469917a296dbc15f206107d72e2d0115c","analyzedAt":"2026-09-12T17:00:17.986Z","contentChangedAt":"2026-09-12T17:00:17.986Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}