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

📄 _init.h

📁 http代理程序
💻 H
字号:


/************************************
  REVISION LOG ENTRY
  Revision By: Alex Turc
  Revised on 6/15/00 12:19:57 PM
  Comments: Application initialization classes and functions
 ************************************/

#ifndef ___init_h__
#define ___init_h__

#include <tchar.h>
#include <list>
using namespace std;

#include "_types.h"

namespace extension
{


// Function name	: parse_cmd_line
// Description	    : Parse the cmd line
// Return type		: list< string >
// Argument         : int nArgC
// Argument         : char* pArgV[]
inline list< string > parse_cmd_line( int nArgC, char* pArgV[] )
{
	list< string > lstCmdLine;
	for( int i=0; i<nArgC; i++ )
		lstCmdLine.push_back( string( pArgV[i] ) );

	return lstCmdLine;
}


// Function name	: get_module_directory
// Description	    : Returns the directory where the specified module is located
// Return type		: inline string 
// Argument         : HMODULE hModule = NULL
inline tstring get_module_directory( HMODULE hModule = NULL )
{
	tchar szBuffer[256] = _T("");

	szBuffer[0] = '\0';
	if( ::GetModuleFileName( hModule, szBuffer, 256 ) <= 0 )
		return tstring();

	tstring strResult( szBuffer );

	// Remove the module file name from the path to get only the directory
	// in which the file is placed
	tstring::size_type i = strResult.rfind( '\\' );
	if( i != tstring::npos )
		strResult = strResult.substr( 0, i + 1 );

	return strResult;
}


/*
Limited intance exception
*/
class instance_number_exception :
	public extended_exception
{
public:


	// Function name	: instance_number_exception
	// Description	    : 
	// Return type		: 
	// Argument         : HRESULT nResult
	// Argument         : char* pDescription = "Instance Number Exception"
	// Argument         : char* pFile = NULL
	// Argument         : long nLine = 0
	instance_number_exception( HRESULT nResult, char* pDescription = "Instance Number Exception", char* pFile = NULL, long nLine = 0 ) :
		extended_exception( nResult, pDescription, pFile, nLine )
	{	
	}
};


/*
Limits the number of instances that an application can have
*/
class limited_instance_number
{
public:

	// Construct an object and check to see if the semahore named szLockIdentifier.
	// It is recommanded that for each application in which is used to generate a GUID
	// and to use this as a name

	// Function name	: limited_instance_number
	// Description	    : 
	// Return type		: 
	// Argument         : DWORD dwInstances
	// Argument         : LPCTSTR szLockIdentifier
	// Argument         : DWORD dwTimeout = INFINITE
	limited_instance_number( DWORD dwInstances, LPCTSTR szLockIdentifier, DWORD dwTimeout = INFINITE )
		throw( instance_number_exception )
	{
		m_hSemaphore = ::CreateSemaphore( NULL, dwInstances, dwInstances, szLockIdentifier );
		DWORD dwResult = ::WaitForSingleObject( m_hSemaphore, dwTimeout );
		if( dwResult == WAIT_TIMEOUT )
			throw_exception( instance_number_exception, ::GetLastError(), "Maximum number of instances is reached." );
		if( dwResult != WAIT_OBJECT_0 )
			throw_exception( instance_number_exception, ::GetLastError(), "Error occured when waiting for semaphore." );
	}

	

	// Function name	: ~limited_instance_number
	// Description	    : 
	// Return type		: virtual 
	virtual ~limited_instance_number()
	{
		::ReleaseSemaphore( m_hSemaphore, 1, NULL );
		::CloseHandle( m_hSemaphore );
	}

private:
	
	// Handle to a semaphore object
	HANDLE m_hSemaphore;
};

} // Namespace extension

#endif // ___init_h__

⌨️ 快捷键说明

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