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

📄 demo.cpp

📁 MMS sender in Windows
💻 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 ReadMm1Provider( IMmsProtocolMm1 *pMm1Protocol );

LPSTR AskDevice( IMmsProtocolMm1 *pMm1Protocol );
LPSTR GetErrorDescription( LONG lLastError, IMmsProtocolMm1 *pMm1Protocol );


int main(int argc, char* argv[])
{
	IMmsProtocolMm1     *pMm1Protocol		= NULL;
	IMmsSlide			*pSlide	    = NULL;
	IMmsMessage			*pMessage   = NULL;
	LPSTR				lpszPincode	= NULL;
	HRESULT				hr;
	LONG				lLastError;

	// Initialize COM  
	CoInitialize(NULL);

	// Create objects
	hr = CoCreateInstance(CLSID_MmsProtocolMm1, NULL, CLSCTX_INPROC_SERVER, IID_IMmsProtocolMm1, (void**) &pMm1Protocol);
	if( SUCCEEDED( hr ) )
		hr = CoCreateInstance(CLSID_MmsSlide, NULL, CLSCTX_INPROC_SERVER, IID_IMmsSlide, (void**) &pSlide );
	if( SUCCEEDED( hr ) )
		hr = CoCreateInstance(CLSID_MmsMessage, NULL, CLSCTX_INPROC_SERVER, IID_IMmsMessage, (void**) &pMessage );
	if( ! SUCCEEDED( hr ) )
	{
		printf( "Unable to create one or more objects.\n" );
		goto _EndMain;
	}

	// MmsSlide: Clear
	pSlide->Clear();

	// MmsSlide: Add attachment and text
	pSlide->AddAttachment( _bstr_t( "c:\\windows\\clock.avi" ) );
	pSlide->AddText( _bstr_t( "Waht a nice clock!" ) );

	// MmsMessage: Clear
	pMessage->Clear();

	// MmsMessage: Recipient and sender
	pMessage->AddRecipient( _bstr_t( ReadInput( "Enter recipient (must start with a '+')" ) ), asMMS_RECIPIENT_TO  );
	pMessage->put_From( _bstr_t( ReadInput( "Enter sender (optional)", TRUE ) ) );
	pMessage->put_Subject( _bstr_t( ReadInput( "Enter subject (optional)", TRUE ) )  );

	// MmsMessage: Add slide
	pMessage->AddSlide( &_variant_t ( ( IDispatch*) pSlide ) );

	// MmsProtocolMm1: Clear
	pMm1Protocol->Clear();

	// MmsProtocolMm1: Device property
	pMm1Protocol->put_Device( _bstr_t( AskDevice( pMm1Protocol ) ) );

	// MmsProtocolMm1: PIN code
	lpszPincode = ReadInput( "Enter PIN code (leave blank for no PIN code)", TRUE );
	if( strlen( lpszPincode ) > 0 )
	{
		printf( "Passing PIN code...\n" );
		pMm1Protocol->EnterPin( _bstr_t( lpszPincode ) );
		pMm1Protocol->get_LastError( &lLastError );
		printf( "EnterPin, result: %ld (%s)\n\n", lLastError, GetErrorDescription( lLastError, pMm1Protocol ) );
	}

	// MmsProtocolMm1: properties
	pMm1Protocol->put_ProviderMMSC( _bstr_t( ReadInput( "Enter MMSC IP/host address" ) ) );
	pMm1Protocol->put_ProviderAPN( _bstr_t( ReadInput( "Enter APN" ) ) );
	pMm1Protocol->put_ProviderAPNAccount( _bstr_t( ReadInput( "Enter APN Account (optional)", TRUE ) ) );
	pMm1Protocol->put_ProviderAPNPassword( _bstr_t( ReadInput( "Enter APN Password (optional)", TRUE ) ) );
	pMm1Protocol->put_ProviderWAPGateway( _bstr_t( ReadInput( "Enter WAP Gateway" ) ) );

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

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

_EndMain:

	if( pMm1Protocol != NULL ) 
	{
		// MmsProtocolMm4: Disconnect
		pMm1Protocol->Disconnect();
		pMm1Protocol->Release();
	}

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

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

	CoUninitialize();

	printf("Ready.\n");

	return 0;
}

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

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

	printf ( "%s:\n", lpszTitle );
	do
	{
		printf ( "   > " );
		// scanf ( "%s", szInput );
		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;
}

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

LPSTR AskDevice( IMmsProtocolMm1 *pMm1Protocol )
{
	LONG		lDeviceCount		= 0L;
	LONG		lDevice				= 0L;
	static CHAR	szDevice[ 256 + 1 ]	= { 0 };

	pMm1Protocol->GetDeviceCount ( &lDeviceCount );

	printf ( "Select a device:\n" );

	for ( int j = 0 ; j < lDeviceCount ; j++ )
	{
		BSTR	bstrTemp = NULL;
		pMm1Protocol->GetDevice ( j, &bstrTemp );
		printf ( "   %ld: %ls\n", j, bstrTemp );
		SysFreeString( bstrTemp );
	}

	while ( lDevice == 0L )
	{
		printf ( "   > " );
		scanf ( "%d", &lDevice );
		if( lDevice < j ) 
		{
			BSTR	bstrDevice = NULL;
			pMm1Protocol->GetDevice ( lDevice, &bstrDevice );
			sprintf ( szDevice, "%ls", bstrDevice );
			SysFreeString( bstrDevice );
		}
		else
		{
			lDevice = 0L;
		}
	}
	printf ( "  Selected device: %s\n\n", szDevice );
	return szDevice;
}



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

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

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

	}
	return szErrorDescription;
}


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

⌨️ 快捷键说明

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