You are here:
ActiveXperts.com > SMS Messaging Server > How to create a new Message > Visual C++ 5.x/6.x
Quicklinks
SMS Messaging Server is a mobile messaging framework that enables companies to send, receive and process SMS- and e-mail messages. The framework is designed support virtually any scenario where low-and high volume SMS messaging is required. Use SMS Messaging Server in the following scenarios:
SMS Messaging Server can be well integrated into Visual C++ environments. This document describes how the SMS Messaging Server can be integrated into Visual C++ projects.
Download ActiveXperts SMS Messaging Server from the ActiveXperts Download Site and start the installation. The installation guides you through the installation process.
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)
A new Project is created now.
Before you can use the SMS Messaging Server API, you need to refer to the SMS Messaging Server API library. The actually reference files are shipped with the product and are located in the following directory:
C:\Program Files\ActiveXperts\SMS Messaging Server\API Samples\Visual C++\Include
Add this path to your standard include directories (Tools->Options: Directories-tab). This directory includes the following files, necessary to link to the SMS Messaging Server API:
AxMmCfg.h AxMmCfg_i.c AxSmsConstants.h
On top of your code, declare the following objects:
IXMessageDB *pMessageDB = NULL; // Reference to the Message Database IXMessage *pMessage = NULL; // Reference to a single message
You must initialize the COM library before they can call COM library functions (e.g. SMS Messaging Server API functions):
CoInitialize( NULL );
Create a Message Database instance in the following way:
CoCreateInstance ( CLSID_XMessageDB, NULL, CLSCTX_INPROC_SERVER, IID_IXMessageDB, (void**) &pMessageDB );
You should not create an instance of a Message yourself; it is returned by functions like Create, FindFirstMessage, FindNextMessage and Load.
You can now send an SMS message.
The following code shows how to send an SMS message:
#include <comdef.h>
#include <atlbase.h>
#include <windows.h>
#include <stdio.h>
#include "..\..\include\AxSmsConstants.h"
#include "..\..\include\AxMmCfg.h"
#include "..\..\include\AxMmCfg_i.c"
int main(int argc, char* argv[])
{
IXMessageDB *pMessageDB = NULL;
IXMessage *pMessage = NULL;
LONG lLastError = 0L;
LONG lID = 0L;
VARIANT vtVar;
HRESULT hr = S_OK;
// Initialize COM
CoInitialize( NULL );
// Initialize Variant
VariantInit( &vtVar );
// Create a new Mesage Database instance
hr = CoCreateInstance ( CLSID_XMessageDB, NULL, CLSCTX_INPROC_SERVER, IID_IXMessageDB, (void**) &pMessageDB ) ;
if( ! SUCCEEDED( hr ) )
{
pMessageDB = NULL;
printf( "Unable to create instance of the object.\n" );
goto _EndMain;
}
// Open the Message Database
pMessageDB->Open();
pMessageDB->get_LastError( &lLastError );
printf( "Open, result: %ld\n", lLastError );
if( lLastError != 0L )
goto _EndMain;
// Create new Message in the Message Database
pMessageDB->Create( &vtVar );
pMessageDB->get_LastError( &lLastError );
printf( "Create, result: %ld\n", lLastError );
if( lLastError != 0L
|| vtVar.vt != ( VT_DISPATCH )
|| ( pMessage = ( IXMessage* ) vtVar.pdispVal ) == NULL )
{
printf( "Unable to create new message.\n" );
goto _EndMain;
}
// Get the ID of the new Message
pMessage->get_ID( &lID );
printf( "ID of the new message: %ld\n", lID );
// Modify the Message
pMessage->put_Direction( MESSAGEDIRECTION_OUT );
pMessage->put_Type( MESSAGETYPE_SMS );
pMessage->put_Status( MESSAGESTATUS_PENDING );
pMessage->put_ChannelID( 0L );
pMessage->put_ScheduledTime( _bstr_t( "" ) );
pMessage->put_Recipient( _bstr_t( "+31625044454" ) );
pMessage->put_Body( _bstr_t( "SMS Messaging Server - VC++ Test SMS" ) );
// Save the Message
pMessageDB->Save( &vtVar );
pMessageDB->get_LastError( &lLastError );
printf( "Save, result: %ld\n", lLastError );
if( lLastError != 0L )
goto _EndMain;
_EndMain:
if( pMessageDB != NULL )
{
pMessageDB->Close(); // Note: It's no problem to call Close when database is not opened
printf( "Database closed.\n" );
pMessageDB->Release();
}
// Clear Variant
VariantClear( &vtVar );
CoUninitialize();
printf("Ready.\n");
return 0;
}