通过WinAPI删除默认路由

时间:2015-09-23 08:15:53

标签: winapi networking

我想通过WinAPI删除默认路由:

route delete 0.0.0.0

看起来路线正在通过未记录的Nsi *调用进行。有没有更简单的方法呢?

1 个答案:

答案 0 :(得分:1)

DeleteIpForwardEntry可以解决问题:

#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <stdio.h>

#pragma comment(lib, "iphlpapi.lib")
#pragma comment(lib, "ws2_32.lib")

// Execute as admin
int main()
{
    ULONG size = 10000;

    PMIB_IPFORWARDTABLE table = (PMIB_IPFORWARDTABLE)malloc(size);

    memset(table, 0, size);

    GetIpForwardTable(table,&size,true);

    for (int i = 0; i < (int) table->dwNumEntries; i++) {

        // Default route 0.0.0.0
        if(table->table[i].dwForwardDest == 0)
        {
            DWORD ret = DeleteIpForwardEntry(&table->table[i]);
            printf("Ret: %X\n", ret);
            break;
        }
    }

    return 0;
}