Search code examples
c++snmpwinsnmp

Help with SNMP in C++?


I've been trying to build an application in C++ using WinSNMP, and although I'm slowly making progress, I keep running into issues. So far, my program tries to startup, create, and open an SNMP session, sets a port, and then attempts to listen to that port, then exits. I have it printing out the values of all the variables its accessing, so I can track everything in the program. The values I'm getting now make no sense at all, some of them are repeated for different variables that have no relation, and some stay null or at 0 even though they are given values & aren't changed. I'm stuck & don't know what to do about this, or what to do next to build the program. My code is as follows:

#include <WinSnmp.h>
#include <stdio.h>
#define MY_BUFSIZE 1024 // Buffer size for console window titles.

smiUINT32  majorVers;
smiUINT32  minorVers;
smiUINT32  nLevel;
smiUINT32  translateMode;
smiUINT32  retranslateMode;

UINT       msgNum=1;
UINT       port = 80;

HWND       window;

char oldWindowTitle[MY_BUFSIZE];

HSNMP_ENTITY      entity;
SNMPAPI_STATUS    status;
SNMPAPI_CALLBACK  callBackNum;


void Startup(){

    //Starting the snmp session
    SnmpStartup(&majorVers, &minorVers, &nLevel, &translateMode, &retranslateMode);

    printf( "Major Version:    %i \n"
            "Minor Version:    %i \n"
            "nLevel:           %i \n"
            "Translate Mode:   %i \n"
            "Retranslate Mode: %i \n\n", 
            (majorVers, minorVers, nLevel, translateMode, retranslateMode));

    GetConsoleTitle((LPWSTR)oldWindowTitle, MY_BUFSIZE);
    window = FindWindow(NULL, (LPCWSTR)oldWindowTitle);

}

void CreateSession(){

    SnmpCreateSession(window,msgNum,callBackNum,NULL);
    printf("Create session returns: %i \n\n", SnmpCreateSession(window,msgNum,callBackNum,NULL));
    printf( "Window:        %i\n"
            "msg num:       %i\n"
            "Call Back num: %i\n\n",
            (window,msgNum,callBackNum));
}

void OpenSession(){
    SnmpOpen(window, msgNum);
    printf("Open session returns: %i\n\n", SnmpOpen(window, msgNum));

}

void SetPort(){
    SnmpSetPort(entity,port);
    printf( "Entity: %i\n"
            "Port:   %i\n\n",
            (entity,port));
}

void Listen(){
    SnmpListen(entity,status);
    printf( "Entity: %i\n"
            "Status: %i\n\n",
            (entity,status));
}
int main(){

    Startup();
    CreateSession();
    OpenSession();
    SetPort();
    Listen();
    SnmpCleanup();

}

The values it is returning are as follows:

        Major Version:    1
        Minor Version:    4320440
        nLevel:           4320760
        Translate Mode:   4320628
        Retranslate Mode: 1358752

        Create Session returns:  2

        Window:        0
        msg num:       4320436
        Call Back num: 4320760

        Open Session returns:  4

        Entity:   80
        Port:     4320444

        Entity:   0
        Status:   4320444

Im lost here. any advice/help?

note that the returned values are generally different/random every time, aside from the single & double digit numbers which are constant.


Solution

  • You only pass two arguments to printf: one format string and retranslateMode. Get rid of the parenthesis around your variables in the call and it should work as you expect.

    Background: The expression (a, b) evaluates a, discards the result, and then yields b. One place where applying this comma operator is useful is code like ++i, ++j in for loops.