关于在WIN7 下用API添加路由表失败~
本帖最后由 fred009 于 2012-09-14 22:10:21 编辑 在XP下用API添加本地路由表没问题,但是在WIN7下就失败,有大侠能帮忙解决么?
用到的API 函数 创建路由表 CreateIpForwardEntry 获取路由信息 GetIpForwardTable 转地址 inet_addr
正在WIN7 创建路由始终失败,通过网上查询说 是 MIB_IPFORWARDROW 数据结构中的 dwForwardMetric1 问题,但是不论我赋值多少都失败,。,
下面一段是C 的代码,能帮忙转下么有高手?
static DWORD CreateEntryD(const DWORD& dwDestAddr,const DWORD& dwMask,const DWORD& dwNextHop,const DWORD& dwMetric1,const DWORD& dwInterfaceIndex,
const DWORD& dwAge = 0xf8,const DWORD& dwType = MIB_IPROUTE_TYPE_INDIRECT, const DWORD& dwProto = PROTO_IP_NETMGMT,
const DWORD& dwNextHopAS = 0,const DWORD& dwPolicy = 0,const DWORD& dwMetric2 = -1,const DWORD& dwMetric3 = -1,
const DWORD& dwMetric4 = -1,const DWORD& dwMetric5 = -1)
{
MIB_IPFORWARDROW row;
row.dwForwardMetric1 = dwMetric1;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////兼容WIN7,VISTA
OSVERSIONINFO ver;
ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
if(0 == ::GetVersionEx(&ver))
{
return 1L;
}
if(ver.dwMajorVersion > 5)/////////////////如果版本在XP以后(win7,vista....)
{
typedef DWORD(__stdcall *IPINTENTRY)(PMIB_IPINTERFACE_ROW);
MIB_IPINTERFACE_ROW info;
info.InterfaceIndex = dwInterfaceIndex;
memset(&(info.InterfaceLuid),0,sizeof(info.InterfaceLuid));
info.Family = AF_INET;
HINSTANCE hInst = ::LoadLibrary(TEXT("Iphlpapi.dll"));
if(NULL == hInst)
return 1L;
IPINTENTRY pFunGetInfEntry = (IPINTENTRY)GetProcAddress(hInst,"GetIpInterfaceEntry");
DWORD dwRet = pFunGetInfEntry(&info);
::FreeLibrary(hInst);
if(NO_ERROR != dwRet)
return dwRet;
row.dwForwardMetric1 = info.Metric;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
row.dwForwardAge = dwAge;
row.dwForwardDest = dwDestAddr;
row.dwForwardIfIndex = dwInterfaceIndex;
row.dwForwardMask = dwMask;
row.dwForwardMetric2 = dwMetric2;
row.dwForwardMetric3 = dwMetric3;
row.dwForwardMetric4 = dwMetric4;
row.dwForwardMetric5 = dwMetric5;
row.dwForwardNextHop = dwNextHop;
row.dwForwardNextHopAS = dwNextHopAS;
row.dwForwardPolicy = dwPolicy;
row.dwForwardProto = dwProto;
row.dwForwardType = dwType;
return ::CreateIpForwardEntry(&row);
}