gpt4 book ai didi

c++ - 帮助 C++ 中的 SNMP?

转载 作者:行者123 更新时间:2023-11-30 04:33:37 25 4
gpt4 key购买 nike

我一直在尝试使用 WinSNMP 在 C++ 中构建应用程序,虽然我正在慢慢取得进展,但我一直遇到问题。到目前为止,我的程序尝试启动、创建和打开 SNMP session ,设置端口,然后尝试监听该端口,然后退出。我让它打印出它访问的所有变量的值,这样我就可以跟踪程序中的所有内容。我现在得到的值根本没有意义,其中一些对于没有关系的不同变量重复,有些保持 null 或 0,即使它们被赋予值并且没有改变。我被困住了,不知道该怎么办,也不知道下一步该怎么做来构建程序。我的代码如下:

#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();

}

它返回的值如下:

        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

我迷路了。有什么建议/帮助吗?

请注意,除了恒定的单位和双位数之外,每次返回的值通常都是不同的/随机的。

最佳答案

您只需将两个参数传递给printf:一个格式字符串和retranslateMode。去掉调用中变量周围的括号,它应该会按预期工作。

背景:表达式 (a, b) 计算 a,丢弃结果,然后产生 b。应用此逗号运算符 的一个地方是 for 循环中的代码,例如 ++i,++j

关于c++ - 帮助 C++ 中的 SNMP?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6681650/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com