Help with SNMP in C++?
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, oldWind开发者_如何学CowTitle);
}
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.
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.
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:
- Windows NT SNMP (very old but very useful, it provides sample code)
- Essential SNMP
精彩评论