Search code examples
cnetwork-programmingsnmpwinsnmp

SnmpRegister() fails in WinSNMP


I just started with WinSNMP (in Visual Studio 2005), and have written the following code. (This is a snippet.)

m_Status = SnmpStartup(&m_MajorVersion, &m_MinorVersion, &m_Level, &m_TranslateMode, &m_RetransmissionMode);
ASSERT(m_Status != SNMPAPI_FAILURE);

m_SnmpSession = SnmpCreateSession(GetSafeHwnd(), WM_SNMP_TEST, &CallBackFunction, NULL);
ASSERT(m_SnmpSession != SNMPAPI_FAILURE);

SnmpStrToOid("1.3.6.1.2.1.1.3.0", m_pIOD);

m_Status = SnmpRegister(&m_SnmpSession, m_pSrcEntity, m_pDstEntity, m_pContext, m_pIOD, SNMPAPI_ON);

SNMPAPI_STATUS temp = SnmpGetLastError(m_SnmpSession);
ASSERT(m_Status != SNMPAPI_FAILURE);

CallBackFunction is defined as:

SNMPAPI_STATUS CALLBACK CallBackFunction
(HSNMP_SESSION hSession=0, HWND hWnd=0, UINT wMsg=0,
 WPARAM wParam=0, LPARAM lParam=0, LPVOID lpClientData=0)
{
    return (SNMPAPI_SUCCESS);
}

m_pSrcEntity, m_pDstEntity, m_pContext are NULL in the example. WM_SNMP_TEST has been registed by me as a Windows Message.

The first ASSERT succeeds. m_SnmpSession gets the value 0x00000001 when the SnmpCreateSession() is called, so second ASSERT also succeeds. Is that normal? I mean can the session have the mentioned value? Then the SnmpRegister() fails and SnmpGetLastError() returns 1. My Windows SNMP and SNMP Trap services are running. What am I doing wrong?


Solution

  • The issue was that I was passing LPHSNMP_ENTITY, LPHSNMP_CONTECXT, LPHSNMP_SESSION, etc to functions where HSNMP_ENTITY, HSNMP_CONTECXT, HSNMP_SESSION, etc were required. Visual Studio was not detecting these errors even at run-time as these all are basically HANDLEs.

    But I still wonder whether the value of m_SnmpSession can be 0x1 or not. Infact, any Source/Destination entity that I created is always equivalent to 0x1. I am confused because they are all HANDLEs, and can 2 handles be the same? Any ideas?

    EDIT: It worked.