2

I'm trying to write a simple program using WinSnmp in C++. There is very little information out there regarding this, and I am completely lost. All I want to do is query the printers on my network & read the info sent to me. Here is my code:

#include <WinSnmp.h>
#include <stdio.h>

smiLPUINT32 majorVers;
smiLPUINT32 minorVers;
smiLPUINT32 nLevel;
smiLPUINT32 translateMode;
smiLPUINT32 retranslateMode;

BYTE pdu;


int main()
{
    //Starting the snmp session
    SnmpStartup(majorVers, minorVers, nLevel, translateMode, retranslateMode);

    printf("%i majorVers \n %i minorVers \n "
           "%i nLevel \n "
           "%i translateMode \n "
           "%i retranslateMode \n\n", 
           majorVers, minorVers, nLevel, translateMode, retranslateMode);


    SnmpCleanup();
}

I've been following the API so far, trying to figure out how the program is supposed to be structured, but it's difficult to write a program based entirely off of the API.

I can't find any good tutorials, code examples, or helpful documentation online. I was wondering if anybody knew of anything that could lead me in the right direction, for example working code snippets or helpful tutorials, that would be great. Thanks in advance :)

EDIT: Ive made a bit of progress, but I'm still stuck. I'm trying to figure out the value of the console window so I can pass it to the SnmpCreateSession function, but I'm Having issues. heres my updated code:

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

smiLPUINT32 majorVers;
smiLPUINT32 minorVers;
smiLPUINT32 nLevel;
smiLPUINT32 translateMode;
smiLPUINT32 retranslateMode;

HWND window;
HWND hwndFound;

char* returnInfo;
char newWindowTitle[MY_BUFSIZE];
char oldWindowTitle[MY_BUFSIZE];

LPWSTR consoleTitle;
BYTE pdu = 1;

void Startup(){

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

    printf("%i majorVers \n"
           "%i minorVers \n"
           "%i nLevel \n"
           "%i translateMode \n"
           "%i retranslateMode \n\n", 
            majorVers, minorVers, nLevel, translateMode, retranslateMode);

    GetConsoleTitle(oldWindowTitle, MY_BUFSIZE);
    hwndFound = FindWindow(NULL, oldWindowTitle);

}

void CreateSession(){

    SnmpCreateSession(window,5,0,0);
    printf("create session returns: %s", SnmpCreateSession(window,5,0,0));

}

int main(){

    Startup();
    CreateSession();
    SnmpCleanup();
}

All of the values I end up with are NULL at this point... I don't know what to do next.

Ashton
  • 81
  • 3
  • 11

2 Answers2

2

I had to do a SNMP module for a Windows application a few years ago and due to the lack of documentation and online resources I ended up buying the following books:

yms
  • 10,361
  • 3
  • 38
  • 68
2

For out params like the ones to SnmpStartup, pass the address of UINT32s that will receive the values:

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

SnmpStartup(&majorVers, &minorVers, &nLevel, &translateMode, &retranslateMode);

There may be more wrong, but this might get you past first base.

Roddy
  • 66,617
  • 42
  • 165
  • 277
  • Thanks! I had to typecast each parameter as (smiLPUINT32) to get it to compile, but now all the values are returning successfully. – Ashton Jul 12 '11 at 16:25
  • 1
    @Ashton - ah. using smiUINT32 should get rid of the casts. Updated code snippet... – Roddy Jul 12 '11 at 16:30
  • 1
    @Ashton - Maybe read the books @yms suggests? SNMP is a murky and horrible area, and those references look as good as anything. – Roddy Jul 12 '11 at 16:53
  • I've downloaded & looked through them both. If I were making a career out of SNMP then they would be perfect; lots of high level information & design principles, very knowledgeable & comprehensive. But this is a demo project my boss asked for, to see if a larger implementation in our product was feasible. So the 800 pages of information is a little bit out of my scope right now... But thank you Roddy & @yms !!! I really appreciate your help. – Ashton Jul 12 '11 at 17:02
  • 1
    @Ashton Maybe you should download and take a look on the sample projects I pointed out for the first book, they might help to get you started without reading the 800 pages. – yms Jul 12 '11 at 19:54