Click here to Skip to main content
15,894,646 members
Articles / Programming Languages / VBScript

Calling scripts within scripts

Rate me:
Please Sign up or sign in to vote.
4.40/5 (5 votes)
29 Jan 2000CPOL 158K   1K   43  
A Component for calling a URL from an ASP script and reading back the output
// ###########################################################################
// #
// # file:
// #	Xept.cpp
// #
// ###########################################################################

#include "stdafx.h"
#pragma warning(disable:4786)
#include "xept.h"

#ifdef _DEBUG
	#define new DEBUG_NEW
	#undef THIS_FILE
	static TCHAR THIS_FILE[] = __FILE__;
#endif


//////////////////////////////////////////////////////////////////////////////
// some helpers.

std::string GetLastErrorMsg( const DWORD last_error )
{
	void *msgbuf;

	if ( !FormatMessage( 
		FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
		0, last_error,
		MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),
		reinterpret_cast<LPTSTR>( &msgbuf ), 0, 0 ) )
		return (LPCTSTR)_T("");
	else {
		const CString str = static_cast<LPCSTR>( msgbuf );
		LocalFree( msgbuf );
		return (LPCTSTR)str;
	}
}

std::string GetExceptionErrorMsg( CException *const e, const bool del_lf )
{
	enum { bufsize=10000 };
	TCHAR buf[bufsize];
	memset( buf, 0, sizeof buf );
	VERIFY( e->GetErrorMessage( buf, bufsize ) );
	CString str = buf;

	if ( del_lf ) {	// remove any "\r" and "\n".
		while ( true ) {
			const int pos = str.FindOneOf( "\r\n");
			if ( pos==-1 )
				break;
			else {
				const CString tmpL = str.Left( pos );
				const CString tmpR = str.Right( str.GetLength()-(pos+1) );
				str = tmpL+tmpR;
			}
		}
	}

	return (LPCTSTR)str;
}


//////////////////////////////////////////////////////////////////////////////
// base exception class.

Xept::Xept() :
	m_LastError(::GetLastError()),
	m_Line(0)
{
}

Xept::Xept( const TCHAR *const file, const int line, 
	const TCHAR *const text, ... ) :
	m_Line(0),
	m_LastError(::GetLastError())
{
	va_list lst;
	va_start( lst, text );
	Xept::Fmt( file, line, text, lst );
	va_end( lst );
}

const char* Xept::what() const
{
	// last error.
	CString last_error;
	const CString last_error_msg=::GetLastErrorMsg( m_LastError ).c_str();
	if ( !last_error_msg.IsEmpty() )
		last_error.Format( _T(" (%s)"), (LPCTSTR)last_error_msg );
	last_error.Replace( _T("\r"), _T("") );
	last_error.Replace( _T("\n"), _T("") );

	m_ConstructWhat = m_Text.c_str()+last_error; 
	return m_ConstructWhat.c_str();
}

void Xept::Fmt( const TCHAR *const file, const int line, 
	const TCHAR *const text, va_list arg_list )
{
	m_File = file;
	m_Line = line;

	// occurence of the error in the source file.
	TCHAR name[1000];
	TCHAR ext [1000];
	_tsplitpath( file, 0, 0, name, ext );
	CString source;
	source.Format( _T("%s%s (%d) "), name, ext, line );

	enum { bufsize=10000 };
	TCHAR buf[bufsize];
	memset( buf, 0, sizeof buf );
	vsprintf( buf, text, arg_list );
	va_end( arg_list );

	m_Text = source+buf;
}


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

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer Zeta Software GmbH
Germany Germany
Uwe does programming since 1989 with experiences in Assembler, C++, MFC and lots of web- and database stuff and now uses ASP.NET and C# extensively, too. He has also teached programming to students at the local university.

➡️ Give me a tip 🙂

In his free time, he does climbing, running and mountain biking. In 2012 he became a father of a cute boy and in 2014 of an awesome girl.

Some cool, free software from us:

Windows 10 Ereignisanzeige  
German Developer Community  
Free Test Management Software - Intuitive, competitive, Test Plans.  
Homepage erstellen - Intuitive, very easy to use.  
Offline-Homepage-Baukasten

Comments and Discussions