我正在尝试从winapi获取apc的信息,但是我的代码失败了 SnmpMgrRequest ,当检查错误代码时,它将它作为40,这在SNMP错误中没有提到winapi的代码列表。我已经发布了以下代码。 解决这个问题的任何帮助都会很棒。
#include <stdio.h>
#include <windows.h>
#include <WINSNMP.H>
#include <tchar.h>
#include <Strsafe.h>
#include <WinNT.h>
#include <Mgmtapi.h>
#include <Snmp.h>
#pragma comment (lib, "WSNMP32.LIB")
#pragma comment (lib, "Mgmtapi.lib")
#pragma comment (lib, "Snmpapi.lib")
int main()
{
int choice;
LPSNMP_MGR_SESSION MgrSession;
INT nRetries = 1;
AsnObjectIdentifier oid;
RFC1157VarBindList variableBindings;
AsnInteger errorStatus = 0;
AsnInteger errorIndex = 0;
char lpAgentAddress[50] ;
LPSTR lpAgentCommunity ="public";
variableBindings.list = NULL;
variableBindings.len = 0;
SNMPAPI MgrStatus;
printf("\n SNMP Program \n ");
printf("\n What You want to know ? \n ");
printf("1---UPS Type \n");
printf("2---Battery capacity \n");
printf("3---Battery Temperature \n ");
printf("4---Battery runtime remain \n ");
printf("Enter your choice : ");
scanf("%d",&choice);
printf("\n Enter the ip address : ");
scanf_s(" %s",lpAgentAddress);
switch(choice)
{
case 1 :
variableBindings.len++;
SnmpMgrStrToOid(".1.3.6.1.4.1.318.1.1.1.1.1.1.0", &oid); // oid for apc ups
variableBindings.list = (SnmpVarBind *)SNMP_realloc(variableBindings.list, sizeof(SnmpVarBind) *variableBindings.len);
SnmpUtilOidCpy(&variableBindings.list[0].name,&oid);
variableBindings.list->value.asnType = ASN_NULL;
MgrSession = SnmpMgrOpen((LPSTR)lpAgentAddress,lpAgentCommunity,1500,2);
if (MgrSession == NULL)
{
printf("\n Session has failed ");
SnmpUtilVarBindListFree(&variableBindings);
return false;
}
MgrStatus = SnmpMgrRequest(MgrSession,SNMP_PDU_GET,&variableBindings,&errorStatus,&errorIndex);
if (MgrStatus == NULL)
{
printf("\n Could not query the device ");
printf("\n Error1 = %d",GetLastError());
SnmpUtilVarBindListFree(&variableBindings);
return false;
}
else
{
if (errorStatus > 0){
printf("Error Returned from the agent ");
SnmpUtilVarBindListFree(&variableBindings);
return false;
}
else
{
for(unsigned int i=0;i<variableBindings.len;i++)
{
int infolen;
char systeminfo[2048];
char *snmpstring;
infolen = variableBindings.list[i].value.asnValue.string.length;
snmpstring =(char *) variableBindings.list[i].value.asnValue.string.stream;
memcpy(systeminfo, snmpstring,infolen); //get the system information and copy in the array systeminfo
systeminfo[infolen] = '\0';
printf("%s",systeminfo);
}
}
}
break;
}
}
答案 0 :(得分:1)
它给出的错误代码为40,在winapi的SNMP ERROR代码列表中没有提到
如果SnmpMgrRequest()
返回0而GetLastError()
返回40,那么这是SNMP_MGMTAPI_TIMEOUT
,它在Mgmtapi.h中定义:
#define SNMP_MGMTAPI_TIMEOUT 40
SNMP_MGMTAPI_TIMEOUT
已记录为,可能是SnmpMgrRequest()
的错误代码:
如果函数失败,则返回值为NULL。要获取扩展错误信息,请调用GetLastError,它可能会返回以下错误代码之一。
SNMP_MGMTAPI_TIMEOUT 请求超时。