-->

How to use GetIpInterfaceEntry on win7+vs2013

2019-07-15 12:36发布

问题:

I'm trying to add some rule to the route table with CreateIpForwardEntry, it's necessary to call the GetIpInterfaceEntry first on win7(see this). Here's part of my code:

#pragma warning(disable: 4996)
#define WIN32_LEAN_AND_MEAN

#define _WIN32_WINNT 0x601

//#define _WS2IPDEF_
//#define __IPHLPAPI_H__

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#include <netioapi.h>
#include <iphlpapi.h>

#include <iostream>
#include <vector>
#include <string>
using namespace std;

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

int main() {
    typedef DWORD(__stdcall *IPINTENTRY)(PMIB_IPINTERFACE_ROW);// undeclared identifier
    MIB_IPINTERFACE_ROW info;
    info.InterfaceIndex = 1;
    memset(&(info.InterfaceLuid),0,sizeof(info.InterfaceLuid));
    info.Family = AF_INET;
    HINSTANCE hInst = ::LoadLibrary("Iphlpapi.dll");
    IPINTENTRY pFunGetInfEntry = (IPINTENTRY)GetProcAddress(hInst,"GetIpInterfaceEntry");
    DWORD dwRet = pFunGetInfEntry(&info);
}

the code doesn't compile, PMIB_IPINTERFACE_ROW is undeclared identifier, I looked into the windows headers, it's the WS2IPDEF that controls the flow. I #defined it but still now working. Anyone has been using this function? Thanks.

回答1:

Is it not compiling, or compiling and not running? To get the code to compile I had to change the includes: I removed

#include <netioapi.h>
#include <iphlpapi.h>

and replaced it with

#include <ws2def.h>
#include <ws2ipdef.h>
#include <iphlpapi.h>

following the comments at the top of "netioapi.h".



回答2:

Include the following headers in this order:

#include <winsock2.h>
#include <windows.h>
#include <ws2def.h>
#include <ws2ipdef.h>
#include <iphlpapi.h>
#include <netioapi.h>