ActiveSocket can be well integrated into Visual C++ environments. This document describes how the ActiveSocket Toolkit can be integrated into Visual Studio C++ 5.x/6.x projects.
Download the ActiveSocket Toolkit 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 ActiveSocket, you need to refer to the ActiveSocket library. The actually reference files are shipped with the product and are located in the following directory:
C:\Program Files\ActiveXperts\ActiveSocket\Examples\Visual C++\Include
Copy all files in the above directory ('ASocket.h', 'ASocket_i.c' and 'ASocketConstants.h') to your project directory.
On top of your code, declare the following object:
IIPtoCountry * pIPtoCountry = NULL;
Since the ActiveComport Toolkit is a COM object, you must initialize the COM library before they can call COM library functions (e.g. ActiveComport functions):
CoInitialize(NULL);
Create the object in the following way:
CoCreateInstance(CLSID_IPtoCountry, NULL, CLSCTX_INPROC_SERVER, IID_IIPtoCountry, (void**) &pIPtoCountry );
You can now lookup IP addresses.
The following code shows how to create a simple IP to country lookup utility using Visual C++:
#include "stdafx.h"
/////////////////////////////////////////////////////////////////////////////
#include "..\include\ASocketConstants.h"
#include "..\include\ASocket_i.c"
#include "..\include\ASocket.h"
/////////////////////////////////////////////////////////////////////////////
#include "activesocket demo.h"
#include "DialogIPtoCountry.h"
/////////////////////////////////////////////////////////////////////////////
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
CDialogIPtoCountry::CDialogIPtoCountry(CWnd* pParent /*=NULL*/) : CDialog(CDialogIPtoCountry::IDD, pParent)
{
m_pIPtoCountry = NULL;
//{{AFX_DATA_INIT(CDialogIPtoCountry)
m_strCountry = _T("");
m_strCountryCode = _T("");
m_strHost = _T("forum.activexperts.com");
m_strLogFile = _T("");
m_strResult = _T("");
//}}AFX_DATA_INIT
}
/////////////////////////////////////////////////////////////////////////////
CDialogIPtoCountry::~CDialogIPtoCountry ()
{
if ( m_pIPtoCountry )
{
m_pIPtoCountry->Release ();
m_pIPtoCountry = NULL;
}
}
/////////////////////////////////////////////////////////////////////////////
void CDialogIPtoCountry::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDialogIPtoCountry)
DDX_Text(pDX, IDC_EDIT_COUNTRY, m_strCountry);
DDX_Text(pDX, IDC_EDIT_COUNTRYCODE, m_strCountryCode);
DDX_Text(pDX, IDC_EDIT_HOST, m_strHost);
DDX_Text(pDX, IDC_EDIT_LOGFILE, m_strLogFile);
DDX_Text(pDX, IDC_STATIC_RESULT, m_strResult);
//}}AFX_DATA_MAP
}
/////////////////////////////////////////////////////////////////////////////
BEGIN_MESSAGE_MAP(CDialogIPtoCountry, CDialog)
//{{AFX_MSG_MAP(CDialogIPtoCountry)
ON_BN_CLICKED(IDC_BUTTON_QUERY, OnButtonQuery)
ON_BN_CLICKED(IDC_BUTTON_VIEW, OnButtonView)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
BOOL CDialogIPtoCountry::OnInitDialog()
{
CDialog::OnInitDialog();
TCHAR szTempPath [ MAX_PATH + 1 ];
CoInitialize( NULL );
HRESULT hr = CoCreateInstance( CLSID_IPtoCountry, NULL, CLSCTX_INPROC_SERVER, IID_IIPtoCountry, (void**) &m_pIPtoCountry );
if( ! SUCCEEDED( hr ) )
{
m_pIPtoCountry = NULL;
m_strResult = "Unable to load the ActiveSocket:IPtoCountry object";
UpdateData( FALSE );
AfxMessageBox( m_strResult, MB_OK | MB_ICONERROR );
return TRUE;
}
GetTempPath( MAX_PATH - 14, szTempPath );
if( szTempPath[ 0 ] == '\0' || szTempPath[ lstrlen( szTempPath ) - 1 ] != '\\' )
_tcscat( szTempPath, _T("\\") );
m_strLogFile = szTempPath;
m_strLogFile += "IPtoCountryLog.txt";
UpdateData( FALSE );
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
void CDialogIPtoCountry::OnButtonQuery()
{
LONG lLastError = 0L;
BSTR bstrTemp;
UpdateData( TRUE );
if( m_pIPtoCountry == NULL )
{
return;
}
AfxGetApp( )->DoWaitCursor( 1 ); // turn on the wait cursor
m_pIPtoCountry->Clear ();
m_pIPtoCountry->put_LogFile ( _bstr_t ( ( LPCSTR ) m_strLogFile ) );
m_pIPtoCountry->put_Host ( _bstr_t ( ( LPCSTR ) m_strHost ) );
m_pIPtoCountry->Query ();
m_pIPtoCountry->get_LastError( &lLastError );
m_strCountry = "";
m_strCountryCode = "";
if( lLastError == 0L )
{
m_pIPtoCountry->get_CountryName ( &bstrTemp );
m_strCountry = bstrTemp;
SysFreeString ( bstrTemp );
m_pIPtoCountry->get_CountryCode ( &bstrTemp );
m_strCountryCode = bstrTemp;
SysFreeString ( bstrTemp );
}
m_pIPtoCountry->GetErrorDescription( lLastError, &bstrTemp );
m_strResult.Format ( "%ld : %ls", lLastError, bstrTemp );
SysFreeString( bstrTemp );
UpdateData( FALSE );
AfxGetApp( )->DoWaitCursor( 0 ); // turn off the wait cursor
}
/////////////////////////////////////////////////////////////////////////////
void CDialogIPtoCountry::OnButtonView()
{
UpdateData();
if (!m_strLogFile.GetLength()) return;
if (ShellExecute(NULL,_T("open"),m_strLogFile,NULL,NULL,SW_SHOWDEFAULT ) == INVALID_HANDLE_VALUE)
{
AfxMessageBox (_T("Can't run viewer"),MB_OK);
}
}
/////////////////////////////////////////////////////////////////////////////
There are many working samples included with the product. You can also find them on the ActiveXperts FTP site: ftp.activexperts-labs.com/samples/asocket.