ActiveSocket

 Product Overview

 ActiveSocket Objects:
 
 How to use

 Online Samples

 Download (.exe)

 Brochure (.pdf)

 Manual (.htm)

 Release Notes


Support

 Knowledge Base

 Forum

 Contact Support


Purchase

 Licensing

 Pricing

 Order now


Related documents

 Tutorials

 Tools


  Download ActiveSocket Network Communications Toolkit 4.1  (5094 KB - .exe file)
  Download Manual  (505 KB - .htm file)


Visual C++ 5.x/6.x SNMP Sample Source Code


ActiveSocket provides an easy-to-use development interface to a variety of IP protocols. By using ActiveSocket, you can very easily create or enhance applications with network features.

ActiveSocket features the following: ICMP, HTTP and HTTPs with support for proxy servers and secure web sites, Telnet, NTP time protocol, RSH remote shell script interface, SNMP (Simple Network Management Protcol), SNMP Traps, Sockets (TCP and UDP), DNS, IP to country lookup, WOL (Wake-On-LAN), and more.

SNMP can be well integrated into Visual C++ environments.
This document describes how ActiveSocket's SNMP objects can be integrated into Visual C++ projects.

ActiveSocket is compliant with SNMP v1 and SNMP v2c.
Different SNMP data types are supported, including:
  • String types (also called "octet strings");
  • Integer types (16bit, 32bit, 64bit and unsigned integers);
  • IP Address types;
  • Timetick types;
  • Counter types (32bit and 64bit counters);
  • OID types (also called "Object ID's");
  • Other, less frequently used datatypes.
The following operations are supported:
  • Get - retrieve an object variable from the (remote) agent;
  • GetNext - retrieve the next object variable from a table or list within an agent;
  • Set - set values for object variables within an agent.


Step 1: Download and install the ActiveSocket Toolkit

Download the ActiveSocket Toolkit from the ActiveXperts Download Site and start the installation. The installation guides you through the installation process.



Step 2: Create a new Visual C++ project

Launch 'Microsoft Visual C++' from the Start menu, and choose 'New' from the 'File Menu'. The 'New' dialog appears.
Select the type of project (for instance: 'Win32 Console Application'), enter a 'Project name' and select the 'Location':

    
    (Click on the picture to enlarge)

Select the kind of project, for instance a 'Hello, world!' application and click 'Finish':

    
    (Click on the picture to enlarge)



Step 3: Refer to the ActiveSocket Library and declare the objects

A new Project is created now.

Before you can use ActiveSocket, you need to refer to the ActiveSocket library. The actually reference files are shipped with the product and are located in the following directory:
    C:\Program Files\ActiveXperts\ActiveSocket\Examples\Visual C++\Include
Copy all files in the above directory ('ASocket.h', 'ASocket_i.c' and 'ASocketConstants.h') to your project directory.

On top of your code, declare the following object:
    ISnmpManager  *pSnmpManager = NULL;


Step 4: Create the objects

Since the ActiveComport Toolkit is a COM object, you must initialize the COM library before they can call COM library functions (e.g. ActiveComport functions):
   CoInitialize(NULL);
Create the object in the following way:
   CoCreateInstance(CLSID_SnmpManager, NULL, CLSCTX_INPROC_SERVER, IID_ISnmpManager, (void**) &pSnmpManager );


Step 5: Creating a simple SNMP manager application

When the objects have been declared and created, you can add the rest of the code to build a simple SNMP manager application.
You can find the sourcecode of this application below:

#include "stdafx.h"

/////////////////////////////////////////////////////////////////////////////

#include "..\include\ASocketConstants.h"
#include "..\include\ASocket_i.c"
#include "..\include\ASocket.h"

/////////////////////////////////////////////////////////////////////////////

#include "ActiveSocket Demo.h"
#include "DlgSnmpGet.h"

/////////////////////////////////////////////////////////////////////////////

#include <comdef.h>

/////////////////////////////////////////////////////////////////////////////

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////

CDlgSnmpGet::CDlgSnmpGet(CWnd* pParent) : CDialog(CDlgSnmpGet::IDD, pParent)
{
  CHAR  szComputerName[ 255 + 1 ] = { 0 };
  DWORD dwSize = 255;

  GetComputerName( szComputerName, &dwSize );

  //{{AFX_DATA_INIT(CDlgSnmpGet)
  m_strGetOID           = _T("system.sysName.0");
  m_strSessionAgent     = _T(szComputerName );
  m_strSessionCommunity = _T("public");
  m_strGetValue         = _T("");
  m_strGetDataType      = _T("");
  m_strSetValue         = _T("");
  m_strTranslationMode  = _T("SNMP v2c");
  m_lPort               = 161;
  m_strLogFile          = _T("");
  m_strResult           = _T("");
  //}}AFX_DATA_INIT

  m_pManager        = NULL;
  m_bSessionOpened  = FALSE;
  m_lDisplayMode    = 0L;   // ASCII by default
}

/////////////////////////////////////////////////////////////////////////////

CDlgSnmpGet::~CDlgSnmpGet()
{
  if ( m_pManager )
  {
    m_pManager->Shutdown ();
    m_pManager->Release  ();
    m_pManager = NULL;
  }
  
  m_bSessionOpened = FALSE;
}

/////////////////////////////////////////////////////////////////////////////

void CDlgSnmpGet::DoDataExchange(CDataExchange* pDX)
{
  CDialog::DoDataExchange(pDX);
  //{{AFX_DATA_MAP(CDlgSnmpGet)
  DDX_Text(pDX, IDC_EDIT_GETOID, m_strGetOID);
  DDX_Text(pDX, IDC_EDIT_SESSIONAGENT, m_strSessionAgent);
  DDX_Text(pDX, IDC_EDIT_SESSIONCOMMUNITY, m_strSessionCommunity);
  DDX_Text(pDX, IDC_EDIT_GETVALUE, m_strGetValue);
  DDX_Text(pDX, IDC_EDIT_GETDATATYPE, m_strGetDataType);
  DDX_Text(pDX, IDC_EDIT_SETVALUE, m_strSetValue);
  DDX_CBString(pDX, IDC_COMBO_TRANSLATIONMODE, m_strTranslationMode);
  DDX_Text(pDX, IDC_EDIT_SESSIONPORT, m_lPort);
  DDX_Text(pDX, IDC_EDIT_LOGFILE, m_strLogFile);
  DDX_Text(pDX, IDC_EDIT_RESULT, m_strResult);
  //}}AFX_DATA_MAP
}

/////////////////////////////////////////////////////////////////////////////

BEGIN_MESSAGE_MAP(CDlgSnmpGet, CDialog)
  //{{AFX_MSG_MAP(CDlgSnmpGet)
  ON_BN_CLICKED(IDC_BUTTON_GET, OnButtonGet)
  ON_BN_CLICKED(IDC_BUTTON_SESSIONOPEN, OnButtonSessionopen)
  ON_BN_CLICKED(IDC_BUTTON_SESSIONCLOSE, OnButtonSessionclose)
  ON_BN_CLICKED(IDC_BUTTON_GETNEXT, OnButtonGetnext)
  ON_BN_CLICKED(IDC_BUTTON_SET, OnButtonSet)
  ON_BN_CLICKED(IDC_BUTTON_VIEW, OnButtonView)
  ON_BN_CLICKED(IDC_RADIO1, OnRadio1)
  ON_BN_CLICKED(IDC_RADIO2, OnRadio2)
  //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////

BOOL CDlgSnmpGet::OnInitDialog() 
{
  CDialog::OnInitDialog();
  
  TCHAR szTempPath    [ MAX_PATH + 1 ];

  CoInitialize( NULL );

  HRESULT hr = CoCreateInstance(CLSID_SnmpManager, NULL, CLSCTX_INPROC_SERVER, IID_ISnmpManager, (void**) &m_pManager);

  if( ! SUCCEEDED( hr ) )
  {
    m_pManager = NULL;
    m_strResult = "Unable to load the ActiveSocket:SnmpManager object";

    UpdateData( FALSE );
    AfxMessageBox( m_strResult, MB_OK | MB_ICONERROR );

    return TRUE;
  }

  GetTempPath( MAX_PATH - 14, szTempPath );
  
  if( szTempPath[ 0 ] == '\0' || szTempPath[ lstrlen( szTempPath ) - 1 ] != '\\' )
    _tcscat( szTempPath, _T("\\") );
  m_strLogFile  = szTempPath;
  m_strLogFile += "SnmpLog.txt";

  UpdateData ( FALSE );

  m_pManager->put_LogFile ( _bstr_t ( ( LPCSTR ) m_strLogFile ) );
  m_pManager->Initialize();

  ( ( CButton* ) GetDlgItem ( IDC_RADIO1 ) )->EnableWindow ( IsString () );
  ( ( CButton* ) GetDlgItem ( IDC_RADIO1 ) )->SetCheck   ( BST_CHECKED ); 
  ( ( CButton* ) GetDlgItem ( IDC_RADIO2 ) )->EnableWindow ( IsString () );

  UpdateControls();
  
  return TRUE;
}

/////////////////////////////////////////////////////////////////////////////

void CDlgSnmpGet::OnButtonSessionopen() 
{
  LONG  lLastError;

  UpdateData( TRUE );

  if( m_pManager == NULL )
    return;

  m_pManager->put_LogFile ( _bstr_t ( ( LPCSTR ) m_strLogFile ) );

  if( m_strTranslationMode == "SNMP v2c" )
    m_pManager->put_ProtocolVersion( asSNMP_VERSION_V2C );
  else
    m_pManager->put_ProtocolVersion( asSNMP_VERSION_V1 );

  AfxGetApp( )->DoWaitCursor( 1 ); // turn on the wait cursor
  m_pManager->Open( _bstr_t( ( LPCTSTR ) m_strSessionAgent ), _bstr_t( ( LPCTSTR ) m_strSessionCommunity ), m_lPort );
  AfxGetApp( )->DoWaitCursor( 0 ); // turn off the wait cursor

  m_pManager->get_LastError( &lLastError );
  
  UpdateResult( lLastError );

  if( lLastError == 0 )
    m_bSessionOpened = TRUE;

  UpdateControls();
}

/////////////////////////////////////////////////////////////////////////////

void CDlgSnmpGet::OnButtonSessionclose() 
{
  UpdateData( TRUE );

  if( m_pManager == NULL )
    return;

  m_pManager->Close();
  UpdateResult( 0L ); // Close is always successfull

  m_bSessionOpened = FALSE;

  UpdateControls();
}

/////////////////////////////////////////////////////////////////////////////

void CDlgSnmpGet::OnButtonGet() 
{
  LONG    lLastError  = 0L;
  LONG    lDataType   = 0L;

  BSTR    bstrValue   = NULL;
  BSTR    bstrOID     = NULL;

  VARIANT   vtVar;

  VariantInit ( &vtVar );

  if( m_pManager == NULL )
    return;

  UpdateData( TRUE );

  AfxGetApp( )->DoWaitCursor( 1 ); // turn on the wait cursor
  m_pManager->Get( _bstr_t( ( LPCTSTR ) m_strGetOID ), &vtVar );
  AfxGetApp( )->DoWaitCursor( 0 ); // turn off the wait cursor

  m_pManager->get_LastError( &lLastError );
  UpdateResult( lLastError );

  if( lLastError == 0L )
  {
    if ( vtVar.vt == VT_DISPATCH )
    {
      ISnmpObject * pSnmpObject = ( ISnmpObject * ) vtVar.pdispVal;

      if ( pSnmpObject )
      {
        pSnmpObject->get_Type( &lDataType );
        m_strGetDataType = GetDataType( lDataType );
        m_lCurrentType = lDataType;

        pSnmpObject->get_Value ( &bstrValue );
        DisplayValue ( bstrValue );
        SysFreeString( bstrValue );

        pSnmpObject->get_OID ( &bstrOID );
        m_strGetOID = bstrOID;
        SysFreeString ( bstrOID );
      
        VariantClear ( &vtVar );
      }
    }

    ( ( CWnd* ) GetDlgItem ( IDC_RADIO1 ) )->EnableWindow ( IsString () );
    ( ( CWnd* ) GetDlgItem ( IDC_RADIO2 ) )->EnableWindow ( IsString () );
  }

  UpdateData( FALSE );

  UpdateControls();
}

/////////////////////////////////////////////////////////////////////////////

void CDlgSnmpGet::OnButtonGetnext() 
{
  LONG    lLastError  = 0L;
  LONG    lDataType   = 0L;

  BSTR    bstrValue   = NULL;
  BSTR    bstrOID     = NULL;

  VARIANT   vtVar;

  VariantInit ( &vtVar );

  if( m_pManager == NULL )
    return;

  UpdateData( TRUE );

  AfxGetApp( )->DoWaitCursor( 1 ); // turn on the wait cursor
  m_pManager->GetNext ( &vtVar );
  AfxGetApp( )->DoWaitCursor( 0 ); // turn off the wait cursor

  m_pManager->get_LastError( &lLastError );
  UpdateResult( lLastError );

  if( lLastError == 0L )
  {
    if ( vtVar.vt == VT_DISPATCH )
    {
      ISnmpObject * pSnmpObject = ( ISnmpObject * ) vtVar.pdispVal;

      if ( pSnmpObject )
      {
        pSnmpObject->get_Type( &lDataType );
        m_strGetDataType = GetDataType( lDataType );
        m_lCurrentType = lDataType;

        pSnmpObject->get_Value ( &bstrValue );
        DisplayValue ( bstrValue );
        SysFreeString( bstrValue );

        pSnmpObject->get_OID ( &bstrOID );
        m_strGetOID = bstrOID;
        SysFreeString ( bstrOID );
      
        VariantClear ( &vtVar );

        UpdateData( FALSE );
      }
    }

    ( ( CWnd* ) GetDlgItem ( IDC_RADIO1 ) )->EnableWindow ( IsString () );
    ( ( CWnd* ) GetDlgItem ( IDC_RADIO2 ) )->EnableWindow ( IsString () );
  }
}

/////////////////////////////////////////////////////////////////////////////

void CDlgSnmpGet::OnButtonSet() 
{
  LONG  lLastError;

  if( m_pManager == NULL )
    return;

  UpdateData( TRUE );

  ISnmpObject * pSnmpObject = NULL;

  HRESULT hr = CoCreateInstance(CLSID_SnmpObject, NULL, CLSCTX_INPROC_SERVER, IID_ISnmpObject, (void**) &pSnmpObject );
  
  if( ! SUCCEEDED( hr ) )
  {
    m_strResult = "Unable to load the ActiveSocket:SnmpData object";

    UpdateData( FALSE );
    AfxMessageBox( m_strResult, MB_OK | MB_ICONERROR );

    return;
  }

  AfxGetApp( )->DoWaitCursor( 1 ); // turn on the wait cursor
  

  pSnmpObject->Clear ();

  pSnmpObject->put_OID  ( _bstr_t( ( LPCTSTR ) m_strGetOID ) );
  pSnmpObject->put_Value  ( _bstr_t( ( LPCTSTR ) m_strSetValue ) );
  pSnmpObject->put_Type ( m_lCurrentType );

  m_pManager->Set   ( &_variant_t ( ( IDispatch* ) pSnmpObject ) );
  
  AfxGetApp()->DoWaitCursor( 0 ); // turn off the wait cursor

  m_pManager->get_LastError( &lLastError );

  UpdateResult( lLastError );

  if( lLastError == 0L )
  {
    UpdateData( FALSE );
    OnButtonGet();
  }
  
  UpdateControls();
}

/////////////////////////////////////////////////////////////////////////////

VOID CDlgSnmpGet::UpdateResult( LONG lLastError )
{
  UpdateData ( TRUE );

  if ( m_pManager )
  {
    BSTR  bstrTemp  = NULL;

    m_pManager->GetErrorDescription ( lLastError, &bstrTemp );
    m_strResult.Format ( "%ld : %ls", lLastError, bstrTemp );
    SysFreeString( bstrTemp );
  }

  UpdateData( FALSE );

}

/////////////////////////////////////////////////////////////////////////////

VOID CDlgSnmpGet::UpdateControls()
{
  LONG  lDataType = m_lCurrentType;

  ( ( CWnd * ) GetDlgItem( IDC_BUTTON_SESSIONOPEN ) )->EnableWindow( m_pManager != NULL && ! m_bSessionOpened );
  ( ( CWnd * ) GetDlgItem( IDC_BUTTON_SESSIONCLOSE ) )->EnableWindow( m_pManager != NULL && m_bSessionOpened);
  ( ( CWnd * ) GetDlgItem( IDC_EDIT_SESSIONAGENT ) )->EnableWindow( m_pManager != NULL && ! m_bSessionOpened );
  ( ( CWnd * ) GetDlgItem( IDC_EDIT_SESSIONPORT ) )->EnableWindow( m_pManager != NULL && ! m_bSessionOpened );
  ( ( CWnd * ) GetDlgItem( IDC_EDIT_SESSIONCOMMUNITY ) )->EnableWindow( m_pManager != NULL && ! m_bSessionOpened );
  ( ( CWnd * ) GetDlgItem( IDC_COMBO_TRANSLATIONMODE ) )->EnableWindow( m_pManager != NULL && ! m_bSessionOpened );

  ( ( CWnd * ) GetDlgItem( IDC_EDIT_GETOID ) )->EnableWindow( m_pManager != NULL && m_bSessionOpened );
  ( ( CWnd * ) GetDlgItem( IDC_BUTTON_GET ) )->EnableWindow( m_pManager != NULL  && m_bSessionOpened );
  ( ( CWnd * ) GetDlgItem( IDC_BUTTON_GETNEXT ) )->EnableWindow( m_pManager != NULL  && m_bSessionOpened );

  ( ( CWnd * ) GetDlgItem( IDC_EDIT_GETVALUE ) )->EnableWindow( m_pManager != NULL  && m_bSessionOpened);
  ( ( CWnd * ) GetDlgItem( IDC_EDIT_GETDATATYPE ) )->EnableWindow( m_pManager != NULL  && m_bSessionOpened);

  ( ( CWnd * ) GetDlgItem( IDC_EDIT_SETVALUE ) )->EnableWindow( m_pManager != NULL  && m_bSessionOpened && lDataType != 0L );
  ( ( CWnd * ) GetDlgItem( IDC_BUTTON_SET ) )->EnableWindow( m_pManager != NULL  && m_bSessionOpened && lDataType != 0L );

  // Remove a previous queried value that is not up-to-date anymore
  if( lDataType == 0 )
  {
    UpdateData        ( TRUE );
    m_strGetValue     = _T("");
    m_strGetDataType  = _T("");
    m_strSetValue     = _T("");
    UpdateData        ( FALSE );
  }
}

/////////////////////////////////////////////////////////////////////////////

LPSTR CDlgSnmpGet::GetDataType( LONG lType )
{
  LPSTR lpszRetVal = "";

  switch( lType )
  {
    case asSNMP_TYPE_INTEGER:           lpszRetVal = "ASN_INTEGER";           break;
    case asSNMP_TYPE_BITS:              lpszRetVal = "ASN_BITS";              break;
    case asSNMP_TYPE_OCTETSTRING:       lpszRetVal = "ASN_OCTETSTRING";       break;
    case asSNMP_TYPE_NULL:              lpszRetVal = "ASN_NULL";              break;
    case asSNMP_TYPE_OBJECTIDENTIFIER:  lpszRetVal = "ASN_OBJECTIDENTIFIER";  break;
    case asSNMP_TYPE_SEQUENCE:          lpszRetVal = "ASN_SEQUENCE";          break;
    case asSNMP_TYPE_IPADDRESS:         lpszRetVal = "ASN_IPADDRESS";         break;
    case asSNMP_TYPE_COUNTER32:         lpszRetVal = "ASN_COUNTER32";         break;
    case asSNMP_TYPE_GAUGE32:           lpszRetVal = "ASN_GAUGE32";           break;
    case asSNMP_TYPE_TIMETICKS:         lpszRetVal = "ASN_TIMETICKS";         break;
    case asSNMP_TYPE_OPAQUE:            lpszRetVal = "ASN_OPAQUE";            break;
    case asSNMP_TYPE_COUNTER64:         lpszRetVal = "ASN_COUNTER64";         break;
    case asSNMP_TYPE_UNSIGNED32:        lpszRetVal = "ASN_UNSIGNED32";        break;
    default:                            lpszRetVal = "Unknown";               break;
  }

  return lpszRetVal;
}

/////////////////////////////////////////////////////////////////////////////

void CDlgSnmpGet::OnButtonView() 
{
  UpdateData();
  
  if (!m_strLogFile.GetLength()) return;

  if (ShellExecute(NULL,_T("open"),m_strLogFile,NULL,NULL,SW_SHOWDEFAULT ) == INVALID_HANDLE_VALUE) 
  {
    AfxMessageBox (_T("Can't run viewer"),MB_OK);
  }
  
}

/////////////////////////////////////////////////////////////////////////////

void CDlgSnmpGet::OnRadio1() 
{
  m_lDisplayMode = 0;

  OnButtonGet ();
}

/////////////////////////////////////////////////////////////////////////////

void CDlgSnmpGet::OnRadio2() 
{
  m_lDisplayMode = 1;
  
  OnButtonGet ();
}

/////////////////////////////////////////////////////////////////////////////

BOOL CDlgSnmpGet::IsString()
{
  BOOL bIsString = FALSE;

  switch( m_lCurrentType )
  {
    case asSNMP_TYPE_BITS:          bIsString = TRUE;     break;
    case asSNMP_TYPE_OCTETSTRING:   bIsString = TRUE;     break;
    case asSNMP_TYPE_SEQUENCE:      bIsString = TRUE;     break;
    case asSNMP_TYPE_OPAQUE:        bIsString = TRUE;     break;
  }

  return bIsString;
}

/////////////////////////////////////////////////////////////////////////////

VOID CDlgSnmpGet::DisplayValue(BSTR bstrVal)
{
  if ( IsString () )
  {
    if ( m_lDisplayMode == 1 )      // HEX dump ?
    {
      INT nLength = SysStringLen ( bstrVal );

      m_strGetValue = "";

      for ( INT n  = 0 ; n < nLength ; n++ )
      {
        CHAR szTemp [ 4 ];

        sprintf ( szTemp, "%02X ", bstrVal [ n ] );
        
        m_strGetValue += szTemp;
      }
      
      UpdateData ( FALSE );

      return;
    }
  }

  m_strGetValue = bstrVal;

  UpdateData ( FALSE );
}

/////////////////////////////////////////////////////////////////////////////
You can download the complete sample on our ftp site ftp.activexperts-labs.com/samples/asocket.
There are many other working ActiveSocket scripts on our site and shipped with the product.




The ActiveSocket tool is a Network Communications ActiveX software component (SDK). This control supports SNMP, SMTP, POP3, Telnet, TCP, NTP, RSH, HTTP, HTTPs, FTP, DNS, ICMP and more, and can be used by any Windows development platform, including Visual Basic .NET, Visual CSharp .NET, ASP .NET (VB,CS), ASP, Visual Basic, Visual Studio/Visual C++, Delphi, PHP, ColdFusion, HTML, VBScript and any other ActiveX/COM compliant platform. The ActiveSocket Toolkit is an ActiveXperts Software B.V. Product.

Copyright ©1999-2007 ActiveXperts Software. All rights reserved.