2

我正在尝试从 winapi 的 apc ups 获取信息,但我的代码失败SnmpMgrRequest 并且当检查错误代码时,它给出的错误代码为 40,这在 winapi 的 SNMP 错误代码列表中未提及。我已经发布了下面的代码。解决此问题的任何帮助都会很棒。

#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;
    }
}
4

1 回答 1

1

它给出的错误代码为 40,在 winapi 的 SNMP 错误代码列表中未提及

如果SnmpMgrRequest()返回 0 并GetLastError()返回 40,那么就是SNMP_MGMTAPI_TIMEOUT,它在 Mgmtapi.h 中定义:

#define SNMP_MGMTAPI_TIMEOUT                40

SNMP_MGMTAPI_TIMEOUT 记录为可能的错误代码SnmpMgrRequest()

如果函数失败,则返回值为 NULL。要获取扩展错误信息,请调用 GetLastError,它可能会返回以下错误代码之一。

SNMP_MGMTAPI_TIMEOUT 请求超时。

于 2013-07-18T21:13:41.663 回答