要用vs2005+winsnmp做一个通过SNMP协议构造网络拓扑图的程序,现在读取路由表的时候出现了一些问题,代码如下:#include <stdafx.h>
#include <iostream>
#include <Winsnmp.h>
#include <windows.h>using namespace std;int tickets=100;
HANDLE g_hEvent;static smiLPUINT32 nMajorVersion = 0;
static smiLPUINT32 nMinorVersion = 0;
static smiLPUINT32 nLevel = 0;
static smiLPUINT32 nTranslateMode = NULL;
static smiLPUINT32 nRetransmitMode = NULL;SNMPAPI_STATUS SNMP_status;
static smiUINT32 IpTable[]={1,3,6,1,2,1,4,21,1,0};//定义路由表mib
static smiUINT32 walkIpTable[]={1,3,1,2,1,4,21,1,0,0,0,0,0,0};//递归读取路由表每一行信息
static HANDLE xWait;//发送后的状态
static HSNMP_SESSION SNMP_session;
static bool mibStatus;void mainThread(int num);SNMPAPI_STATUS CALLBACK myCallback(HSNMP_SESSION hSession, HWND hWnd, UINT wMsg, WPARAM wParam,  LPARAM lParam, LPVOID lpClientData )
{
SNMPAPI_STATUS recv_isSucceed = NULL;
HSNMP_ENTITY recv_entity = NULL,recv2_entity = NULL;
HSNMP_CONTEXT recv_context = NULL;
HSNMP_PDU recv_pdu = NULL;
HSNMP_VBL recv_Vbl = NULL; cout<<"CallBack Running."<<endl;
recv_isSucceed = SnmpRecvMsg(hSession,&recv_entity,&recv2_entity,&recv_context,&recv_pdu);
//开始拆包
if(recv_isSucceed == SNMPAPI_SUCCESS)
{
cout<<"receive Msg Succeed."<<endl;
cout<<recv_pdu<<endl;
}
else
{
cout<<"receive Msg Failed."<<endl;
}
smiINT recv_pduType;
smiINT recv_pduErrorStatus;
recv_isSucceed = SnmpGetPduData(recv_pdu,&recv_pduType,0,&recv_pduErrorStatus,0,&recv_Vbl);
if(recv_isSucceed == SNMPAPI_SUCCESS)
{
cout<<"receive Pdu Data Succeed."<<endl;
}
else
{
cout<<"receive Pdu Data Failed."<<endl;
cout<<"Received Pdu Type:"<<recv_pduType<<endl;
recv_isSucceed = SnmpGetLastError(NULL);
if(recv_isSucceed == SNMPAPI_PDU_INVALID)
{
cout<<"receive SNMPAPI_PDU_INVALID Fail."<<endl; }
cout<<"Error Code:"<<recv_isSucceed<<endl;
cout<<"---------------------------------------------------------------"<<endl;
cout<<"Error Status:"<<recv_pduErrorStatus<<endl;
}
int nVblCount = SnmpCountVbl(recv_Vbl);
//获取变量绑定表的长度 smiOID recv_name;
smiVALUE recv_value;
LPSTR ResultValue = new char[1408];
cout<<"VblCount:"<<nVblCount<<endl;
for(int i=1;i<=nVblCount;i++)
{
SnmpGetVb(recv_Vbl,i,&recv_name,&recv_value);
recv_isSucceed = SnmpOidToStr(&recv_name,1408,ResultValue);
if(recv_isSucceed != SNMPAPI_FAILURE)
{
cout<<"Decode Succeed!!"<<endl;
}
else
{
cout<<"Decode Failed!!"<<endl;
recv_isSucceed = SnmpGetLastError(NULL);
cout<<recv_isSucceed<<endl;
}
cout<<ResultValue<<endl;
}
recv_isSucceed = SnmpFreeDescriptor(SNMP_SYNTAX_OCTETS,(smiLPOPAQUE)&recv_name);
recv_isSucceed = SnmpFreeDescriptor(recv_value.syntax, (smiLPOPAQUE)&recv_value.value.oid);
SetEvent(xWait); //接收部分结束
return SNMPAPI_SUCCESS;
}int main()
{
SNMP_status = SnmpStartup(    //加载smnp
nMajorVersion,
nMinorVersion,
nLevel,
nTranslateMode,
nRetransmitMode
);
if(SNMP_status == SNMPAPI_SUCCESS)
{
cout<<"Snmp Started."<<endl;
} SNMP_session = SnmpCreateSession(NULL,0,myCallback,NULL);//接受session
mibStatus = true; mainThread(1); cout<<"All finished."<<endl;
Sleep(5000);
SnmpCleanup();
return 0;
}
void mainThread(int num)
{
HSNMP_ENTITY send_entity;
HSNMP_ENTITY recv_entity;
HSNMP_CONTEXT send_context;
HSNMP_VBL send_Vbl; smiOID myOID;
smiVALUE myVALUE; smiLPCOID oid = &myOID;
smiLPCVALUE value = &myVALUE; char * temp = "public";
smiOCTETS comm;
comm.len = 6;
comm.ptr = (unsigned char *)temp; SNMPAPI_STATUS Send_Vb;
HSNMP_PDU send_pdu;
SNMPAPI_STATUS send_isSucceed;
//变量声明结束 SnmpSetTranslateMode(SNMPAPI_UNTRANSLATED_V1);//设置传输模式 send_entity = SnmpStrToEntity(SNMP_session,"192.168.0.2");//创建实体
recv_entity = SnmpStrToEntity(SNMP_session,"192.168.0.2");//创建实体
SnmpSetRetransmitMode(SNMPAPI_ON);
SnmpSetTimeout(send_entity, 5000);
SnmpSetRetry(send_entity, 2); send_context = SnmpStrToContext(SNMP_session, &comm); send_Vbl = SnmpCreateVbl(SNMP_session,NULL,NULL);//创建Vbl if(send_Vbl == SNMPAPI_FAILURE)
{
cout<<"Create Vbl Failed!"<<endl;
}
else
{
cout<<"Create Vbl succeed."<<endl;
} //while(mibStatus){
IpTable[9] = 1; myOID.len = sizeof(IpTable) /sizeof(smiUINT32);
myOID.ptr = IpTable;//设定要添加的oid项
myVALUE.syntax = SNMP_SYNTAX_NULL;//指明要添加的是一个oid
myVALUE.value.uNumber = 0; Send_Vb = SnmpSetVb(send_Vbl,0,oid,value); if(Send_Vb == SNMPAPI_SUCCESS)
{
cout<<"Set Vbl Succeed."<<endl;
}
else{
cout<<"Set Vbl Failed."<<endl;
} send_pdu = SnmpCreatePdu(SNMP_session, SNMP_PDU_GETNEXT,0,0,0 ,send_Vbl);//创建Pdu if(send_pdu == SNMPAPI_FAILURE){
cout<<"Create Pdu Failed!"<<endl;
Send_Vb = SnmpGetLastError(NULL);
cout<<send_pdu<<endl;
}
else
{
cout<<"Created Pdu:"<<send_pdu<<endl;
}
xWait = CreateEvent(NULL,true,false,NULL);//创建xWait事件 send_isSucceed = SnmpSendMsg(SNMP_session, send_entity,recv_entity,send_context,send_pdu); if(send_isSucceed == SNMPAPI_SUCCESS)
{
cout<<"Send completed."<<endl;
}
WaitForSingleObject(xWait,INFINITE);//等待
//}}目前这个程序运行之后可以正常读取路由表的第一行。问题1:对于路由表这样的表结构,如何循环读取整个表?问题2:读取完本机的路由表之后,如何递归读取其他节点的路由表?