在使用snmpcreatesession函数时,它的第一个和第二个参数是指给一个什么功能的窗口?第三个参数是一个回调函数,但怎么用?回调函数可以写成:
SNMPAPI_STATUS CALLBACK SNMPAPI_CALLBACK(
  HSNMP_SESSION hSession,  // handle to the WinSNMP session
  HWND hWnd,               // handle to the notification window
  UINT wMsg,               // window notification message number
  WPARAM wParam,           // type of notification
  LPARAM lParam,           // request identifier of PDU
  LPVOID lpClientData      // optional application-defined data
);
但它的第一个参数又要是一个Session,这个session是在snmpcreatesession函数返回的那个session吗?具体怎么用?谢谢。

解决方案 »

  1.   

    如果通过消息机制接收了SNMP异步消息,没有必要用回调函数了,可以设为NULL。MSDN解释如下:
    Pointer to application-defined data to pass to the callback function specified by the fCallback parameter. This parameter is optional and can be NULL. If the fCallback parameter is NULL, the implementation ignores this parameter
      

  2.   

    如果必须用回调函数,给你一个例子:#include <stdlib.h>
    #include <stdio.h>
    #include "winsnmp.h"
    #define MAX_VARBINDS 7HANDLE xWait;
    //
    SNMPAPI_STATUS Val2Str (smiLPVALUE, smiUINT32, LPSTR);  //把SNMP变
    //量转换为字符串的函数
    //
    smiUINT32 sysGroup[9] = {1,3,6,1,2,1,1,0,0};
    //
    SNMPAPI_STATUS CALLBACK cbFunc
       (HSNMP_SESSION hSession, HWND hWnd, UINT wMsg,
        WPARAM wParam, LPARAM lParam, LPVOID lpClientData)
    {
    if (wParam ==  0) // Normal response
       {
       HSNMP_PDU hPdu;
       HSNMP_VBL hVbl;
       smiOID dName;
       smiVALUE dValue;
       smiUINT32 nVb;
       smiINT32 lReqId;
       smiINT32 lType, lErr, lIdx;
       smiINT32 lStat;
       smiUINT32 i;
       char szName[64];
       char szValue[256];
       lStat = SnmpRecvMsg (hSession, NULL, NULL, NULL, &hPdu);
       lStat = SnmpGetPduData (hPdu, &lType, &lReqId, &lErr, &lIdx, &hVbl);
       if (lErr != 0)
          printf ("SNMP Error for RequestID = %d: Value = %d, Index =  %d\n",
                   lReqId, lErr, lIdx);
       else
          {
          nVb = SnmpCountVbl (hVbl);
          for (i=0; i<nVb; i++)
             {
             lStat = SnmpGetVb (hVbl, i+1, &dName, &dValue);
             lStat = SnmpOidToStr (&dName, sizeof(szName), szName);
             lStat = Val2Str (&dValue, sizeof(szValue), szValue);
             printf ("%s: %s\n", szName, szValue);
             lStat = SnmpFreeDescriptor (SNMP_SYNTAX_OCTETS, (smiLPOPAQUE)&dName);
             lStat = SnmpFreeDescriptor (dValue.syntax,
                                         (smiLPOPAQUE)&dValue.value.oid);
             } // end_for
          } // end_else
       lStat = SnmpFreeVbl (hVbl);
       lStat = SnmpFreePdu (hPdu);
       } // end_if wParam == 0
    else
       {
       printf ("Request #%d timed out.\n", lParam);
       }
    SetEvent (xWait);
    return (SNMPAPI_SUCCESS);
    }
    //
    main (int argc, char* argv[])
    {
    SNMPAPI_CALLBACK cB = &cbFunc;
    LPHOSTENT lpHostent;
    IN_ADDR host;
    HSNMP_SESSION hSession;
    HSNMP_ENTITY hDst;
    HSNMP_PDU hPdu;
    HSNMP_VBL hVbl;
    HSNMP_CONTEXT hCtx;
    smiOCTETS dCtx;
    smiOID dName;
    smiVALUE dValue;
    smiUINT32 lM, lN, lL, lR, lX;
    SNMPAPI_STATUS lStat;
    smiUINT32 i;
    // Usage test
    if (argc < 3)
       {
       printf ("Usage: np_get <host> <community>\n");
       exit (1);
       }
    // Initialize WinSNMP
    lStat = SnmpStartup (&lM, &lN, &lL, &lX, &lR);
    if (lStat != SNMPAPI_SUCCESS)
       {
       printf ("SnmpStartup failed!\n");
       exit (2);
       }
    lStat = SnmpSetTranslateMode (SNMPAPI_UNTRANSLATED_V1);
    lStat = SnmpSetRetransmitMode (SNMPAPI_ON);
    hSession = SnmpCreateSession (NULL, 0, cB, NULL);
    if (hSession == SNMPAPI_FAILURE)
       {
       printf ("SnmpCreateSession failed!\n");
       SnmpCleanup ();
       exit (3);
       }
    hDst = SnmpStrToEntity (hSession, argv[1]);
    if (hDst == SNMPAPI_FAILURE)
       {
       // Get the host name and resolve it to an IP address
       lpHostent = gethostbyname (argv[1]);
       if (lpHostent == NULL)
          {
          printf ("gethostbyname failed.\n");
          SnmpCleanup ();
          exit (4);
          }
       memmove (&host, lpHostent->h_addr, sizeof(IN_ADDR));
       hDst = SnmpStrToEntity (hSession, inet_ntoa(host));
       }
    if (hDst == SNMPAPI_FAILURE)
       {
       printf ("SnmpStrToEntity failed!\n");
       SnmpCleanup ();
       exit (5);
       }
    // Get the community string and save it
    dCtx.len = strlen(argv[2]);
    dCtx.ptr = (smiLPBYTE)argv[2];
    hCtx = SnmpStrToContext (hSession, &dCtx);
    if (hCtx == SNMPAPI_FAILURE)
       {
       printf ("SnmpStrToContext failed!\n");
       SnmpCleanup ();
       exit (6);
       }
    dName.len = sizeof(sysGroup)/sizeof(smiUINT32);
    dName.ptr = sysGroup;
    dValue.syntax = SNMP_SYNTAX_NULL;
    dValue.value.uNumber = 0;
    hVbl = SnmpCreateVbl (hSession, NULL, NULL);
    for (i=1; i<=MAX_VARBINDS; i++)
       {
       sysGroup[7] = i;
       SnmpSetVb (hVbl, 0, &dName, &dValue);
       }
    hPdu = SnmpCreatePdu (hSession, SNMP_PDU_GET, 100, 0, 0, hVbl);
    //
    xWait = CreateEvent (NULL, TRUE, FALSE, NULL);
    lStat = SnmpSendMsg (hSession, 0, hDst, hCtx, hPdu);
    WaitForSingleObject (xWait, INFINITE);
    lStat = SnmpCleanup();
    return (0);
    }
      

  3.   

    有没有关于winsnmp编程的资料或书?
      

  4.   

    资料可以到网上去搜嘛,书好像目前没有发现专门关于winsnmp
      

  5.   

    书名:SNMP网络管理 
    书名:用SNMP管理互联网络(第三版)