Skip to content

Conversation

Copy link

Copilot AI commented Dec 12, 2025

Four nearly identical functions for retrieving owner module information from TCP/UDP table entries contained ~100 lines of duplicated code differing only in API calls and output format.

Changes

  • Consolidated logic: Extracted common pattern into GetOwnerModuleFromEntryEx<TRow, TGetFunc> template in pch.h
  • Refactored functions: GetOwnerModuleFromTcp4EntryEx, GetOwnerModuleFromTcp6EntryEx, GetOwnerModuleFromUdpEntryEx, GetOwnerModuleFromUdp6EntryEx now delegate to template
  • Preserved behavior: Template parameter expectInsufficientBuffer handles protocol differences (TCP handles NO_ERROR/ERROR_NOT_FOUND on first call; UDP expects ERROR_INSUFFICIENT_BUFFER)

Before

void GetOwnerModuleFromTcp4EntryEx(_In_ PMIB_TCPROW_OWNER_MODULE pTcpEntry)
{
    TCPIP_OWNER_MODULE_BASIC_INFO Buffer = {};
    DWORD Size = sizeof(TCPIP_OWNER_MODULE_BASIC_INFO);
    DWORD ret = GetOwnerModuleFromTcpEntry(pTcpEntry, TCPIP_OWNER_MODULE_INFO_BASIC, &Buffer, &Size);
    if (NO_ERROR == ret) {
        printf("\tModuleName: %ls\n", Buffer.pModuleName);
        printf("\tModulePath: %ls\n", Buffer.pModulePath);
        return;
    }
    // ... 20+ more lines
}

// Nearly identical for Tcp6, Udp, Udp6 variants

After

void GetOwnerModuleFromTcp4EntryEx(_In_ PMIB_TCPROW_OWNER_MODULE pTcpEntry)
{
    GetOwnerModuleFromEntryEx(pTcpEntry, GetOwnerModuleFromTcpEntry);
}

void GetOwnerModuleFromUdpEntryEx(_In_ PMIB_UDPROW_OWNER_MODULE pTcpEntry)
{
    GetOwnerModuleFromEntryEx(pTcpEntry, GetOwnerModuleFromUdpEntry, true);
}

Impact

  • -52 lines across tcp.cpp and udp.cpp
  • +52 lines for shared template in pch.h
  • Net: 20 lines saved, ~78 lines of duplication eliminated
  • Future changes to GetOwnerModule logic now require single edit
Original prompt

Find and refactor duplicated code


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Refactor duplicated code in the project Refactor duplicated GetOwnerModule functions into template Dec 12, 2025
Copilot AI requested a review from kouzhudong December 12, 2025 02:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants