1

我正在用 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;
}

我尝试了其他使用窗口通知的方法。但它没有用。

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

4

0 回答 0