ActiveXperts SMS Component

Quicklinks

Visual C++ sample - Send SMS text through an SMPP provider

The ActiveXperts SMS Component is a software development kit (SDK) to enhance an application or script with SMS or Pager functionality. SMS messages can be sent/received using a GSM modem, an SMPP provider or an HTTP compliant SMSC.


Introduction

In this example we are going to use Visual Studio 2008 to create a C++ application project named 'DemoApp'. We are going tot store this project in the directory 'C:\MyProjects'. All of these names can be changed according to your preferences. This demo project will ask the user to give a phone number and a message body on the command prompt.

A subscription to an SMPP provider is required. For this demo you can send a limited number of messages through our own gateway.

Step 1: Download and install the ActiveXperts SMS Component

Download the ActiveXperts SMS Component 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: 'Win32 Console Application', enter a 'Project name' and select the 'Location':

Visual C

(Click on the picture to enlarge)

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

Visual C

(Click on the picture to enlarge)

Step 3: Refer to the ActiveXperts SMS Component Library

Before you can use the ActiveXperts SMS Component, you need to refer to the ActiveXperts SMS Component library. The actually reference files ship with the product and are located in the following directory:

C:\Program Files\ActiveXperts\SMS Component\Samples\Visual C++\Include

ActiveXperts recommends to make use of the MmWrapper.h and MmWrapper.cpp COM wrappers. To do so, include 'MmWrapper.h' in your source file, and add 'MmWrapper.cpp' to your project so it will be compiled and linked together with your program code.

Step 4: Declare and create the ActiveXperts SMS Component objects

On top of your code, declare the following objects (defined in 'MmWrapper.h'):

#include <windows.h>
#include <stdio.h>
#include "..\..\..\include\MmWrapper.h"

VOID  ReadSmppProvider( CMmString &strServer, INT &nServerPort, CMmString &strSystemID, 
                        CMmString &strSystemPassword );
VOID  GetSmsDemoAccountInfo( CMmString &strAccount, CMmString &strPassword );
LPTSTR ReadInput( LPCTSTR lpszTitle, BOOL bAllowEmpty = FALSE );

int _tmain(int argc, CHAR* argv[])
{
  CSmpp          objSmpp;
  CSmsConstants  objConstants;
  CSmsMessage    objMessage, *pobjRcvSmsMessage = NULL;

Step 5: Send SMS messages

The following code shows how to send an SMS message using the data that was stored in the 'objSmsMessage' and 'objSmsConstants' objects. After the message is sent the error result will be displayed. Error 0 means the message was sent succesfully.

// Submit the SMS message; allow multiple-part (i.e. more than 160 chars for standard text, or 
// more tan 70 chars for Unicode text)
lLastError = objSmpp.SubmitSms( &objMessage, objConstants.MULTIPART_OK );
_tprintf( _T("SubmitSms, result: %ld (%s)\n"), lLastError, 
objSmpp.GetErrorDescription( lLastError, strResult ).t_str() );
if (lLastError != 0)
  goto _EndMain;

Appendix: Full source code

Following you can find the full source code which is also included in the ActiveXperts SMS Component package.

// SmppProgram.cpp : Defines the entry point for the console application.
//

#include <windows.h>
#include <stdio.h>


#include "..\..\..\include\MmWrapper.h"

VOID  ReadSmppProvider( CMmString &strServer, INT &nServerPort, CMmString &strSystemID, 
                        CMmString &strSystemPassword );
VOID  GetSmsDemoAccountInfo( CMmString &strAccount, CMmString &strPassword );
LPTSTR ReadInput( LPCTSTR lpszTitle, BOOL bAllowEmpty = FALSE );

int _tmain(int argc, CHAR* argv[])
{
  CSmpp          objSmpp;
  CSmsConstants  objConstants;
  CSmsMessage    objMessage, *pobjRcvSmsMessage = NULL;
  CMmString      strServer = _T(""), strSystemID = _T(""), strSystemPassword = _T("");
  CMmString      strReference, strValue;
  CMmString      strResult;
  INT            nServerPort = 0;
  LONG           lLastError = 0L;
  CMmString      strVersion, strBuild, strModule, strExpDate;
  BOOL           bResult = FALSE,  bWaitForDeliveryReport = TRUE;

  _tprintf( _T( "SMS Component Version: %s\nSMS Component Build  : 
              %s\nSMS Component Module : %s\nLicense Status : %s\n\n" ), 
              objSmpp.getVersion( strVersion ).t_str(), 
              objSmpp.getBuild( strBuild ).t_str(), 
              objSmpp.getModule( strModule ).t_str(), 
              objSmpp.getLicenseStatus( strExpDate ).t_str() );

  if( ! objSmpp.IsInitialized() || ! objMessage.IsInitialized() )
  {
    _tprintf( _T("Unable to initialized one or more objects.\n") );
    goto _EndMain;
  }
  
  // Set Log File for debugging purposes
  // objSmpp.setLogFile( "c:\\smpp.log" );

    // Prompt user for SMPP server details; by default, the ActiveXperts SMPP demo is used
  ReadSmppProvider( strServer, nServerPort, strSystemID, strSystemPassword );

    // Connect to the SMPP server using host/IP-addess, a specific TCP port and a timeout
    _tprintf( _T("Connecting...\n"));
    lLastError = objSmpp.Connect( strServer.t_str(), nServerPort, 10000 );
  _tprintf( _T("Connect, result: %ld (%s)\n"), lLastError, 
                objSmpp.GetErrorDescription( lLastError, strResult ).t_str() );
  if( lLastError != 0L )
    goto _EndMain;

    // Bind to the SMPP server as transceiver using your SystemID(login) and password
    // Note: although we only send, we could use SMPP_BIND_TRANSMITTER. However, we would like to 
    // receive a delivery report and therefore connect as SMPP_BIND_TRANSCEIVER
    _tprintf(_T("Binding..."));
    lLastError = objSmpp.Bind( objConstants.SMPP_BIND_TRANSCEIVER, strSystemID.t_str(), 
                                strSystemPassword.t_str(), _T(""), 
                                objConstants.SMPP_VERSION_34, 0, 0, _T(""), 10000);
  _tprintf( _T("Binding, result: %ld (%s)\n"), lLastError, 
          objSmpp.GetErrorDescription( lLastError, strResult ).t_str() );
    if (lLastError != 0)
    goto _EndMain;

    objMessage.Clear();
    objMessage.setToAddress( ReadInput( _T("Enter recipient (start with a '+')"), false ) );
    objMessage.setBodyFormat( objConstants.BODYFORMAT_TEXT );
    objMessage.setBody( ReadInput(_T("Type SMS text"), false ) );
    objMessage.setRequestDeliveryReport( true );

    // Submit the SMS message; allow multiple-part (i.e. more than 160 chars for standard text, or
    // more tan 70 chars for Unicode text)
    lLastError = objSmpp.SubmitSms( &objMessage, objConstants.MULTIPART_OK );
  _tprintf( _T("SubmitSms, result: %ld (%s)\n"), lLastError, 
              objSmpp.GetErrorDescription( lLastError, strResult ).t_str() );
    if (lLastError != 0)
    goto _EndMain;

    // Wait for the SMPP server for max. 5000 ms to accept the submitted SMS message. 
    // 5000 ms will usually suffice
    bResult = objSmpp.WaitForSmsUpdate(5000);
  _tprintf( _T("WaitForSmsUpdate, result: %s\n"), bResult ? "TRUE" : "FALSE" );
    if( ! bResult )
        goto _EndMain;

    // WaitForSmsUpdate succeeded, so there's the updated SMS message available. Fetch it now
    lLastError = objSmpp.FetchSmsUpdate( &pobjRcvSmsMessage );
  _tprintf( _T("FetchSmsUpdate, result: %ld (%s)\n"), lLastError, 
      objSmpp.GetErrorDescription( lLastError, strResult ).t_str() );
    if (lLastError != 0)
        goto _EndMain;

    // Get the message reference , that will be matched against a delivery report (still to
    // be received) later on
    pobjRcvSmsMessage->getReference( strReference );
    _tprintf( _T("Message Reference: %s\n"), strReference.t_str());
  delete pobjRcvSmsMessage;

    _tprintf( _T("Waiting for delivery report...\n") );
    while( bWaitForDeliveryReport )
    {
        lLastError = objSmpp.ReceiveMessage( &pobjRcvSmsMessage );
    _tprintf( _T("ReceiveMessage, result: %ld (%s)\n"), lLastError, 
        objSmpp.GetErrorDescription( lLastError, strResult ).t_str() );
        if( lLastError == 0L && pobjRcvSmsMessage->getSmppIsDeliveryReport() )
        {
      _tprintf( _T("Delivery Report found, status:%s, body:%s\n"), strReference.t_str(), 
          pobjRcvSmsMessage->getBody( strValue ).t_str() );
            bWaitForDeliveryReport = false;
      delete pobjRcvSmsMessage;
            break;
        }

        delete pobjRcvSmsMessage;
        Sleep(2000);
  }
  

_EndMain:

  objSmpp.Unbind();
  objSmpp.Disconnect();

  _tprintf( _T("Ready.\n") );

  return 0;
}


LPTSTR ReadInput( LPCTSTR lptszTitle, BOOL bAllowEmpty )
{
  static TCHAR    tszInput [ 255 + 1 ] = { _T('\0') };

  _tprintf( _T("%s:\n"), lptszTitle );
  do
  {
    _tprintf ( _T("   > ") );
    // scanf ( "%s", tszInput );
    fflush(stdin); 
    fflush(stdout); 
    _fgetts( tszInput, 255, stdin );
    if( tszInput[ 0 ] != _T('\0') && tszInput[ _tcsclen( tszInput ) - 1  ] == _T('\n') )
      tszInput[ _tcsclen( tszInput ) - 1  ] = _T('\0');
  } while( _tcsclen ( tszInput ) == 0 && ! bAllowEmpty );

  return tszInput;
}

VOID ReadSmppProvider( CMmString &strServer, INT &nServerPort, CMmString &strSystemID, 
        CMmString &strSystemPassword )
{
    CMmString strInput;
    strInput = ReadInput(_T("Enter SMPP provider. Type <ENTER> to use the free 
        ActiveXperts SMPP gateway"), true );
    if (strInput == _T(""))
  {
        strServer = _T("smpp.activexperts-labs.com");
    nServerPort = 2775;
    GetSmsDemoAccountInfo( strSystemID, strSystemPassword );
  }
    else
  {
        strInput = ReadInput(_T("Enter port. Type <ENTER> to use default port 2775"), true );
    if( ( nServerPort = _tstol( strInput.t_str() ) ) == 0 )
      nServerPort = 2775;

    strSystemID = ReadInput(_T("Enter SystemID (account ID to login to the provider)"), false );
    strSystemPassword = ReadInput(_T("Enter SystemPassword associated with account ID)"), false );
  }


    _tprintf( _T("Provider settings:\n") );
    _tprintf( _T("  Server: %s\n"), strServer.t_str() );
    _tprintf( _T("  ServerPort: %ld\n"), nServerPort );
    _tprintf( _T("  SystemID: %s\n"), strSystemID.t_str() );
    _tprintf( _T("  SystemPassword: %s\n"), strSystemPassword.t_str() );
    _tprintf( _T("") );
}

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

VOID  GetSmsDemoAccountInfo( CMmString &strAccount, CMmString &strPassword )
{
    HKEY      hKey = NULL;
  static TCHAR  tszAccountFile[ 255 + 1 ] = { 0 };
  DWORD      dwSize = 255 * sizeof( TCHAR );
  DWORD      dwType;
  FILE      *fp = NULL;
  TCHAR      *cp = NULL, tszEntry[ 1024 ] = { _T('\0') };

  strAccount  = _T("");
  strPassword  = _T("");

  if( RegOpenKeyEx( HKEY_LOCAL_MACHINE, _T("Software\\ActiveXperts\\SMS Component"), 
0, KEY_READ, &hKey ) != ERROR_SUCCESS )
    goto _EndGetSmsDemoAccountInfo;  

  if( RegQueryValueEx( hKey, _T("InstallRoot"), NULL, &dwType, ( unsigned char * ) tszAccountFile, 
&dwSize ) != ERROR_SUCCESS )
    goto _EndGetSmsDemoAccountInfo;  

  _tcscat( tszAccountFile, _T("\\Utilities\\activexperts-labs.txt") );

  if( ( fp = _tfopen( tszAccountFile, _T("rt") ) ) == NULL ) 
  {
    _tprintf( _T("Failed to open [%s]\n"), tszAccountFile );
    goto _EndGetSmsDemoAccountInfo;
  }

  // Read Header
  tszEntry[ 0 ] = _T('\0');
  _fgetts( tszEntry,  1024, fp );
  // Read Account
  tszEntry[ 0 ] = _T('\0');
  _fgetts( tszEntry,  1024, fp );
  if( ( cp = _tcschr( tszEntry, _T('\n') ) ) != NULL ) *cp = _T('\0');  // Trim trailing newline
  strAccount = tszEntry;
  // Read Password
  tszEntry[ 0 ] = _T('\0');
  _fgetts( tszEntry,  1024, fp );
  if( ( cp = _tcschr( tszEntry, _T('\n') ) ) != NULL ) *cp = _T('\0');  // Trim trailing newline
  strPassword = tszEntry;

_EndGetSmsDemoAccountInfo:

  if( fp != NULL )
  {
    fclose( fp );
    fp = NULL;
  }

  if( hKey != NULL )
  {
    RegCloseKey( hKey );
    hKey = NULL;
  }
}

You can download the full source code of this project from the ActiveXperts FTP site: ftp.activexperts-labs.com/samples/sms-component. There are many other working samples included with the product or on the FTP site.

NOTE: Demo Projects are created with Microsoft Visual Studio 2008

The ActiveXperts SMS Component project ships with a set of Microsoft Visual Studio .NET samples. The projects are created with Microsoft Visual Studio 2008.

Users with a later version of Microsoft Visual Studio can open such a project. The Visual Studio Conversion Wizard will guide you through the process of converting the project to the version used.