⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 demo.cpp

📁 SMS sender using SMPT in Windows platform
💻 CPP
字号:
// Demo.cpp : Defines the entry point for the console application.
//

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

#include "..\..\include\aXmsConstants.h"
#include "..\..\include\aXmsCtrl.h"
#include "..\..\include\aXmsCtrl_i.c"

LPSTR ReadInput( LPCSTR lpszTitle, BOOL bAllowEmpty = FALSE );
void ReadSmppProvider( ISmsProtocolSmpp *pSmppProtocol );
LPSTR GetErrorDescription( LONG lLastError, ISmsProtocolSmpp *pSmppProtocol );
LPSTR GetProvidersDirectory();



int main(int argc, char* argv[])
{
	ISmsProtocolSmpp    *pSmppProtocol = NULL;
	ISmsMessage			*pMessage   = NULL;
	HRESULT				hr;
	LONG				lLastError;
	BSTR				bstrProviderResponse = NULL;
	BSTR				bstrMessageReference = NULL;
	BSTR				bstrMessageStatus    = NULL;
	VARIANT				vtVariant;

	// Initialize Variant
	VariantInit ( &vtVariant );		

	// Initialize COM  
	CoInitialize(NULL);

	// Create objects
	hr = CoCreateInstance(CLSID_SmsProtocolSmpp, NULL, CLSCTX_INPROC_SERVER, IID_ISmsProtocolSmpp, (void**) &pSmppProtocol);
	if( SUCCEEDED( hr ) )
		hr = CoCreateInstance(CLSID_SmsMessage, NULL, CLSCTX_INPROC_SERVER, IID_ISmsMessage, (void**) &pMessage );
	if( ! SUCCEEDED( hr ) )
	{
		printf( "Unable to create one or more objects.\n" );
		goto _EndMain;
	}

	// SmsProtocolSmpp: read provider settings
	ReadSmppProvider( pSmppProtocol );

	// SmsMessage: prompt for messgae proprties
	pMessage->Clear();
	pMessage->put_Recipient( _bstr_t( ReadInput( "Enter recipient (must start with a '+')" ) ) );
	pMessage->put_Format( asMESSAGEFORMAT_TEXT );
	pMessage->put_Data( _bstr_t( ReadInput( "Enter SMS message" ) ) );
	pMessage->put_Sender( _bstr_t( "+31638740160" ) );

	// SmsProtocolSmpp: prompt for properties
	printf( "Connecting...\n" );
	pSmppProtocol->Connect();
	pSmppProtocol->get_LastError( &lLastError );
	printf( "Connect, result: %ld (%s)\n", lLastError, GetErrorDescription( lLastError, pSmppProtocol ) );
	if( lLastError != 0L )
		goto _EndMain;

	// SmsProtocolSmpp: send the message
	printf( "Sending message...\n" );
	pSmppProtocol->Send( &_variant_t ( ( IDispatch*) pMessage ), &bstrMessageReference );
	pSmppProtocol->get_LastError( &lLastError );
	printf( "Send, result: %ld (%s)\n", lLastError, GetErrorDescription( lLastError, pSmppProtocol ) );

	Sleep ( 2000 );

	printf( "Querying status...\n" );
	pSmppProtocol->QueryStatus ( bstrMessageReference, TRUE, &vtVariant );

	if ( vtVariant.vt == VT_DISPATCH )
	{
		ISmsDeliveryStatus * pStatus = ( ISmsDeliveryStatus * ) vtVariant.pdispVal;

		if ( pStatus )
		{
			pStatus->get_StatusDescription ( &bstrMessageStatus );

			printf ( "Final status of submitted message : %ls\n", bstrMessageStatus );
		}
	
		VariantClear ( &vtVariant );
	}


_EndMain:

	if( pSmppProtocol != NULL ) 
	{
		pSmppProtocol->Disconnect();
		pSmppProtocol->Release();
	}

	if( pMessage != NULL ) 
		pMessage->Release();

	if( bstrProviderResponse != NULL )
		SysFreeString( bstrProviderResponse );

	if( bstrMessageReference != NULL )
		SysFreeString( bstrMessageReference );

	if( bstrMessageStatus != NULL )
		SysFreeString ( bstrMessageStatus );

	CoUninitialize();

	printf("Ready.\n");

	return 0;
}

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

LPSTR ReadInput( LPCSTR lpszTitle, BOOL bAllowEmpty )
{
	static CHAR		szInput [ 255 + 1 ] = { 0 };

	printf ( "%s:\n", lpszTitle );
	do
	{
		printf ( "   > " );
		fflush(stdin); 
		fflush(stdout); 
		fgets( szInput, 255, stdin );
		if( szInput[ 0 ] != '\0' && szInput[ strlen( szInput ) - 1  ] == '\n' )
			szInput[ strlen( szInput ) - 1  ] = '\0';
	} while( lstrlen ( szInput ) == 0 && ! bAllowEmpty );
	printf( "\n" );
	return szInput;
}

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

void ReadSmppProvider( ISmsProtocolSmpp *pSmppProtocol )
{
	CHAR		szInput [ 255 + 1 ] = { 0 };
	CHAR		szProviderFile [ 255 + 1 ] = { 0 };
	LPSTR		lpszChoise = NULL;
	BSTR		bstrVal = NULL;
	LONG		lLastError = 0L;
	LONG		lVal = 0L;

	lpszChoise = ReadInput("Enter SMPP provider (IP or hostname); Type <ENTER> to use the free ActiveXperts SMS gateway.", TRUE );
	if( lpszChoise[ 0 ] == '\0' ) 
	{
		strcpy( szProviderFile, GetProvidersDirectory() );
		strcat( szProviderFile, "\\Sms\\Smpp\\activexperts.smpp" );
		pSmppProtocol->ProviderLoadConfig( _bstr_t( szProviderFile ) );
		pSmppProtocol->get_LastError( &lLastError );
		printf( "ProviderLoadConfig, result: %ld (%s)\n", lLastError, GetErrorDescription( lLastError, pSmppProtocol ) );
	}
	else
	{
		pSmppProtocol->put_Server( _bstr_t( lpszChoise ) );
		pSmppProtocol->put_ServerPort( atol( ReadInput( "Enter Port" ) ) );
		pSmppProtocol->put_SystemID( _bstr_t( ReadInput( "Enter SystemID" ) ) );
		pSmppProtocol->put_SystemPassword( _bstr_t( ReadInput( "Enter SystemPassword" ) ) );
	}

	printf("\nProvider Settings:\n" );

	// Print Server
	pSmppProtocol->get_Server( &bstrVal );
	printf("  Server: %ls\n", bstrVal );
	SysFreeString( bstrVal );

	// Print ServerPort
	pSmppProtocol->get_ServerPort( &lVal );
	printf("  ServerPort: %ld\n", lVal );

	// Print SystemID
	pSmppProtocol->get_SystemID( &bstrVal );
	printf("  SystemID: %ls\n", bstrVal );
	SysFreeString( bstrVal );

	// Print SystemPassword
	pSmppProtocol->get_SystemPassword( &bstrVal );
	printf("  SystemPassword: %ls\n", bstrVal );
	SysFreeString( bstrVal );
	
	printf( "\n" );
}

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

LPSTR GetProvidersDirectory()
{
  	HKEY			hKey;
	static CHAR		szProviderDirectory[ 255 + 1 ] = { 0 };
	DWORD			dwSize = 255 * sizeof( CHAR );
	DWORD			dwType;

	if( RegOpenKeyEx( HKEY_LOCAL_MACHINE, "Software\\ActiveXperts\\SMS and MMS Toolkit", 0, KEY_READ, &hKey ) != ERROR_SUCCESS )
		return szProviderDirectory;	

	if( RegQueryValueEx( hKey, "InstallRoot", NULL, &dwType, ( unsigned char * ) szProviderDirectory, &dwSize ) != ERROR_SUCCESS )
	{
		RegCloseKey( hKey );
		return szProviderDirectory;
	}

	strcat( szProviderDirectory, "\\Providers" );
	RegCloseKey( hKey );
	return szProviderDirectory;
}

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

LPSTR GetErrorDescription( LONG lLastError, ISmsProtocolSmpp *pSmppProtocol )
{
	static CHAR		szErrorDescription[ 1024 + 1 ] = { 0 };
	BSTR			bstrErrDescr = NULL;

	szErrorDescription[ 0 ] = '\0';
	pSmppProtocol->GetErrorDescription( lLastError, &bstrErrDescr );
	if( bstrErrDescr != NULL )
	{
		sprintf( szErrorDescription, "%ls", bstrErrDescr );
		SysFreeString ( bstrErrDescr );
	}
	return szErrorDescription;
}


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

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -