C++ 无法使用WinSNMP API接收SNMP陷阱

C++ 无法使用WinSNMP API接收SNMP陷阱,c++,networking,snmp,winsnmp,snmp-trap,C++,Networking,Snmp,Winsnmp,Snmp Trap,我正在用WinSNMP编写一个简单的小型SNMP管理程序。 但它不能接收陷阱事件。。。 有人能帮我吗 下面是示例代码: #include <stdio.h> #include <Winsnmp.h> SNMPAPI_STATUS CALLBACK MySnmpCallback( HSNMP_SESSION hSession, HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam,

我正在用WinSNMP编写一个简单的小型SNMP管理程序。 但它不能接收陷阱事件。。。 有人能帮我吗

下面是示例代码:

#include <stdio.h>
#include <Winsnmp.h>

SNMPAPI_STATUS CALLBACK MySnmpCallback(
    HSNMP_SESSION hSession,
    HWND hWnd,
    UINT wMsg,
    WPARAM wParam,
    LPARAM lParam,
    LPVOID lpClientData
)
{
    printf("MySnmpCallback!\n");
    return SNMPAPI_SUCCESS;
}

void SnmpTest()
{
    smiUINT32 nMajorVersion;
    smiUINT32 nMinorVersion;
    smiUINT32 nLevel;
    smiUINT32 nTranslateMode;
    smiUINT32 nRetransmitMode;

    SNMPAPI_STATUS statusStartup = SnmpStartupEx(
        &nMajorVersion,
        &nMinorVersion,
        &nLevel,
        &nTranslateMode,
        &nRetransmitMode
    );

    if (SNMPAPI_SUCCESS == statusStartup)
    {
        printf("  MajorVersion = %u\n", nMajorVersion);
        printf("  MinorVersion = %u\n", nMinorVersion);
        printf("         Level = %u\n", nLevel);
        printf("RetransmitMode = %u\n", nRetransmitMode);
        SnmpSetTranslateMode(SNMPAPI_UNTRANSLATED_V2);
        SnmpGetTranslateMode(&nTranslateMode);
        printf(" TranslateMode = %u\n", nTranslateMode);
    }
    else
    {
        printf("SnmpStartup Failed. (%u)\n", SnmpGetLastError(NULL));
    }

    if (SNMPAPI_SUCCESS == statusStartup)
    {
        HSNMP_SESSION hSession = SnmpCreateSession(NULL, NULL, (SNMPAPI_CALLBACK)&MySnmpCallback, NULL);
        if (SNMPAPI_FAILURE != hSession)
        {
            HSNMP_ENTITY localEntity = SnmpStrToEntity(hSession, "0.0.0.0");
            SNMPAPI_STATUS regStatus = SnmpRegister(hSession, localEntity, NULL, NULL, NULL, SNMPAPI_ON);
            if (SNMPAPI_SUCCESS == regStatus)
            {
                while (TRUE)
                {
                    Sleep(100);
                    if (GetKeyState('A') && 0x8000)
                        break;
                }

                SnmpClose(hSession);
            }
            else
            {
                printf("SnmpRegister Failed. (%u)\n", SnmpGetLastError(hSession));
            }
        }
        else
        {
            printf("SnmpCreateSession Failed. (%u)\n", SnmpGetLastError(NULL));
        }
    }

    if (SNMPAPI_SUCCESS == statusStartup)
    {
        SnmpCleanup();
    }

}

int _tmain(int argc, _TCHAR* argv[])
{
    SnmpTest();
    return 0;
}
#包括
#包括
SNMPAPI_状态回调MySnmpCallback(
HSNMP_会议期间,
HWND HWND,
UINT wMsg,
WPARAM WPARAM,
LPARAM LPARAM,
LPVOID lpClientData
)
{
printf(“MySnmpCallback!\n”);
返回SNMPAPI_成功;
}
void SnmpTest()
{
smiUINT32主要版本;
smiUINT32 NMinorvision;
微笑32 N级;
smuint32转换模式;
SMUINT32非传输模式;
SNMPAPI_STATUS STATUS startup=SnmpStartupEx(
&主要版本,
&nminorvision,
&N级,
&nTranslateMode,
&nRE传输模式
);
if(SNMPAPI_SUCCESS==statusStartup)
{
printf(“主版本=%u\n”,n主版本);
printf(“MinorVersion=%u\n”,nMinorVersion);
printf(“级别=%u\n”,nLevel);
printf(“重新传输模式=%u\n”,nRetransmode);
SnmpSetTranslateMode(SNMPAPI_UNTRANSLATED_V2);
SnmpGetTranslateMode(&ntTranslateMode);
printf(“TranslateMode=%u\n”,nTranslateMode);
}
其他的
{
printf(“SnmpStartup失败。(%u)\n”,SnmpGetLastError(NULL));
}
if(SNMPAPI_SUCCESS==statusStartup)
{
HSNMP_SESSION hSession=SnmpCreateSession(NULL,NULL,(SNMPAPI_回调)和MySnmpCallback,NULL);
如果(SNMPAPI_故障!=hsSession)
{
HSNMP_ENTITY localEntity=snmpstrotentity(hSession,“0.0.0.0”);
SNMPAPI_STATUS regStatus=SnmpRegister(hSession、localEntity、NULL、NULL、SNMPAPI_ON);
if(SNMPAPI_SUCCESS==regStatus)
{
while(TRUE)
{
睡眠(100);
if(GetKeyState('A')&&0x8000)
打破
}
SnmpClose(hSession);
}
其他的
{
printf(“SnmpRegister失败。(%u)\n”,SnmpGetLastError(hsSession));
}
}
其他的
{
printf(“SnmpCreateSession失败。(%u)\n”,SnmpGetLastError(NULL));
}
}
if(SNMPAPI_SUCCESS==statusStartup)
{
SnmpCleanup();
}
}
int _tmain(int argc,_TCHAR*argv[]
{
SnmpTest();
返回0;
}
我尝试过使用窗口通知的其他方法。 但它不起作用

我试图用PowerShell事件验证此代码。 打开evntwin并添加PowerShell事件。(ID:400/401/402/403) 为了实现陷阱事件,我启动了powershell并退出。 SNMP服务和陷阱服务正在运行。 上面代码中的循环进程正在运行。 但是示例程序无法接收任何陷阱。
*其他管理器(如Snmpb)可以接收陷阱。

您是否使用wireshark或类似工具验证过您的测试用例是否实际生成了SNMP陷阱?