ActiveXperts SMS Component

Quicklinks

Visual C++ sample - Send SMS text through an HTTP 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 HTTP 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'):

CHttp         objHttp;
CSmsMessage   objSmsMessage;
CSmsConstants objConstants;

Step 5: Gather information

The following code will ask the user for the recipient telephone number and the content of the text message. This data will be stored in the 'objSmsMessage' object.

objMessage.Clear();
objMessage.setBodyFormat( objConstants.BODYFORMAT_TEXT );
objMessage.setBody( _T("ActiveXperts SMS Component HTTP-Post test message!") );
objMessage.setFromAddress( _T("+316123123123") );
objMessage.setToAddress( ReadInput( _T("Enter recipient (recommended to start with a '+')") ) );

Step 6: 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. The timeout value is set to 10 seconds, which means that the modem will try to send the message for 10 seconds, after this value it will stop trying.

_tprintf(_T("Sending..."));
objHttp.SendSms( &objMessage, objConstants.MULTIPART_OK, strResponse);
_tprintf( _T("Send, result: %ld (%s)\n"), lLastError, 
          objHttp.GetErrorDescription( lLastError, strResult ).t_str() );
_tprintf( _T("Response from provider: %s\n"), strResponse.t_str() );

Appendix: Full source code

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

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

#include 
#include 
#include 
#include 

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

LPTSTR ReadInput( LPCTSTR lpszTitle, BOOL bAllowEmpty = FALSE );
VOID  GetHttpURL(CSmsConstants &, CMmString &strUrl );
VOID  GetSmsDemoAccountInfo( CMmString &strAccount, CMmString &strPassword );


int _tmain(int argc, _TCHAR* argv[])
{
  CHttp      objHttp;
  CSmsConstants  objConstants;
  CSmsMessage    objMessage;
  LONG      lLastError = 0L;
  CMmString    strVersion, strBuild, strModule, strExpDate;
    CMmString    strRecipient, strUrl, strResult, strResponse;

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

    GetHttpURL(objConstants, strUrl );

    objMessage.Clear();
    objMessage.setBodyFormat( objConstants.BODYFORMAT_TEXT );
    objMessage.setBody( _T("ActiveXperts SMS Component HTTP-Post test message!") );
    objMessage.setFromAddress( _T("+316123123123") );
    objMessage.setToAddress(ReadInput( _T("Enter recipient (recommended to start with a '+')") ));

    objHttp.Clear();
  // Set Log File for debugging purposes
  // objHttp.setLogFile( _T("c::\\http.log") );
    objHttp.setUrl( strUrl.t_str() );    

    _tprintf(_T("Sending..."));
    objHttp.SendSms( &objMessage, objConstants.MULTIPART_OK, strResponse);
  _tprintf( _T("Send, result: %ld (%s)\n"), lLastError, 
            objHttp.GetErrorDescription( lLastError, strResult ).t_str() );
  _tprintf( _T("Response from provider: %s\n"), strResponse.t_str() );

  _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  GetHttpURL(CSmsConstants &objConstants, CMmString &strUrl )
{
  strUrl = ReadInput(_T("Enter HTTP Post provider URL.\nType  to use the free SMS gateway"),true);

    if (strUrl == _T(""))
    {
        CMmString strUsername, strPassword;
    TCHAR tszBuffer[ 512 ] = { _T('\0') };

        GetSmsDemoAccountInfo( strUsername,  strPassword );
    _tprintf( _T("Account:[%s], Password:[%s]\n"), strUsername.t_str(), strPassword.t_str() );


        _stprintf( tszBuffer, _T("post.activexperts-labs.com:8080/sendsms/default.asp
          ?username=%s&password=%s&text=%s&to=%s"), strUsername.t_str(), strPassword.t_str(), 
    objConstants.HTTP_PLACEHOLDER_BODY.t_str(), objConstants.HTTP_PLACEHOLDER_TOADDRESS.t_str());
    strUrl = tszBuffer;

    _tprintf( _T("URL: [%s]\n"), strUrl.t_str() );
    }
   
    _tprintf(_T("\n"));   
}

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

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 );
  // Trim trailing newline
  if( ( cp = _tcschr( tszEntry, _T('\n') ) ) != NULL ) *cp = _T('\0');  
  strAccount = tszEntry;
  // Read Password
  tszEntry[ 0 ] = _T('\0');
  _fgetts( tszEntry,  1024, fp );
  // Trim trailing newline
  if( ( cp = _tcschr( tszEntry, _T('\n') ) ) != NULL ) *cp = _T('\0');  
  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.