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

📄 serverobject.cpp

📁 vc++6.0开发网络典型应用实例导航 1. 本光盘提供了本书中所有的实例源程序文件。 2. 附录文件夹下是Winsock 函数参考以及错误码列表
💻 CPP
字号:
/********************************************************************/
/*																	*/
/*  ServerObject.cpp												*/
/*																	*/
/*  Implementation of the CServerObject class.						*/
/*																	*/
/*  Programmed by Pablo van der Meer								*/
/*	This code is stolen from: http://www.pablovandermeer.nl			*/
/*																	*/
/*  Last updated: July 4, 2003										*/
/*																	*/
/********************************************************************/


#include "stdafx.h"
#include "server.h"
#include "ServerObject.h"

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


IMPLEMENT_DYNCREATE(CServerObject, CCmdTarget)

CServerObject::CServerObject()
{
	EnableAutomation();
}

CServerObject::~CServerObject()
{
}


void CServerObject::OnFinalRelease()
{
	// When the last reference for an automation object is released
	// OnFinalRelease is called.  The base class will automatically
	// deletes the object.  Add additional cleanup required for your
	// object before calling the base class.

	CCmdTarget::OnFinalRelease();
}


BEGIN_MESSAGE_MAP(CServerObject, CCmdTarget)
	//{{AFX_MSG_MAP(CServerObject)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

BEGIN_DISPATCH_MAP(CServerObject, CCmdTarget)
	//{{AFX_DISPATCH_MAP(CServerObject)
	DISP_FUNCTION(CServerObject, "URLEncode", URLEncode, VT_BSTR, VTS_BSTR)
	DISP_FUNCTION(CServerObject, "CreateObject", CreateObjectEx, VT_DISPATCH, VTS_BSTR)
	DISP_FUNCTION(CServerObject, "MapPath", MapPath, VT_BSTR, VTS_BSTR)
	DISP_FUNCTION(CServerObject, "HTMLEncode", HTMLEncode, VT_BSTR, VTS_BSTR)
	//}}AFX_DISPATCH_MAP
END_DISPATCH_MAP()

// Note: we add support for IID_IServerObject to support typesafe binding
//  from VBA.  This IID must match the GUID that is attached to the 
//  dispinterface in the .ODL file.

// {7631CBBC-4CD8-4400-90A0-013E1AF29454}
static const IID IID_IServerObject =
{ 0x7631cbbc, 0x4cd8, 0x4400, { 0x90, 0xa0, 0x1, 0x3e, 0x1a, 0xf2, 0x94, 0x54 } };

BEGIN_INTERFACE_MAP(CServerObject, CCmdTarget)
	INTERFACE_PART(CServerObject, IID_IServerObject, Dispatch)
END_INTERFACE_MAP()



/********************************************************************/
/*																	*/
/* Function name : SetCurrentDirectory								*/
/* Description   : Set current directory							*/
/*																	*/
/********************************************************************/
void CServerObject::SetCurrentDirectory(LPCTSTR lpszDirectory)
{
	m_strCurrentDirectory = lpszDirectory;
	m_strCurrentDirectory.TrimRight('\\');
}


/********************************************************************/
/*																	*/
/* Function name : SetRootDirectory									*/
/* Description   : Set root directory								*/
/*																	*/
/********************************************************************/
void CServerObject::SetRootDirectory(LPCTSTR lpszDirectory)
{
	m_strRootDirectory = lpszDirectory;
	m_strRootDirectory.TrimRight('\\');
}


/********************************************************************/
/*																	*/
/* Function name : URLEncode										*/
/* Description   : Convert unsafe characters to %xx sequences		*/
/*																	*/
/********************************************************************/
BSTR CServerObject::URLEncode(LPCTSTR lpszURL) 
{
	CString strResult = "";
	CString strHex;

	char ch;
	while((ch = *lpszURL++) != '\0')
	{
		// check if it's an unsafe character
		if (AfxIsUnsafeUrlChar(ch))
		{
			if (ch == ' ')
				strResult += '+';
			else
			{
				// output the percent, followed by the hex value of the character
				strResult += '%';
				strHex.Format("%02X", ch);
				strResult += strHex;
			}
		}
		else 
		// safe character
		{
			strResult += ch;
		}
	}
	return strResult.AllocSysString();
}


/********************************************************************/
/*																	*/
/* Function name : CreateObjectEx									*/
/* Description   : Create an instance of a server object.			*/
/*																	*/
/********************************************************************/
LPDISPATCH CServerObject::CreateObjectEx(LPCTSTR lpszProgID) 
{
	IDispatchPtr pServerObject;

	// create instance of specified object using smart pointer
	HRESULT hResult = pServerObject.CreateInstance(lpszProgID);
	if (FAILED(hResult))
	{
		return NULL;
	}

	pServerObject->AddRef();
	return pServerObject;
}


/********************************************************************/
/*																	*/
/* Function name : MapPath											*/
/* Description   : Maps the specified relative or virtual path to	*/
/*				   the corresponding physical directory.			*/
/*																	*/
/********************************************************************/
BSTR CServerObject::MapPath(LPCTSTR lpszLogicalPath) 
{
	CString strPhysicalPath;
	CString strLogicalPath = lpszLogicalPath;

	CString strOffset;
	// make unix style
	strLogicalPath.Replace("\\","/");
	while(strLogicalPath.Replace("//","/"));
	
	if (strLogicalPath.Left(1) == '/')
	{
		// absolute path
		strOffset = m_strRootDirectory;
	}
	else
	{
		// relative path
		strOffset = m_strCurrentDirectory;
	}

	if (strOffset.Right(1) != '\\')
		strOffset += "\\";

	strLogicalPath.TrimLeft('/');
	strPhysicalPath = strOffset + strLogicalPath;

	// make Windows style
	strPhysicalPath.Replace("/", "\\");

	CStringList partList;
	CString strSub;
	int nCount=0;

	// split path in parts
	while(AfxExtractSubString(strSub, strPhysicalPath, nCount++, '\\'))
	{
		if (!strSub.IsEmpty())
			partList.AddTail(strSub);
	}
	
	strPhysicalPath.Empty();

	// fix dots
	while(!partList.IsEmpty())
	{
		CString strPart = partList.GetHead();
		partList.RemoveHead();

		if (strPart == "..")
		{
			// go back one level
			int nPos = strPhysicalPath.ReverseFind('\\');
			if (nPos != -1)
			{
				strPhysicalPath = strPhysicalPath.Left(nPos);
			}
		}
		else
		{
			if (!strPhysicalPath.IsEmpty())
			{
				strPhysicalPath += "\\";
			}
			strPhysicalPath += strPart;
		}
	}		
	return strPhysicalPath.AllocSysString();
}


/********************************************************************/
/*																	*/
/* Function name : HTMLEncode										*/
/* Description   : Apply HTML encoding to the specified string.		*/
/*																	*/
/********************************************************************/
BSTR CServerObject::HTMLEncode(LPCTSTR lpszIn) 
{
	CString strEncoded;

	int nLength = lstrlen(lpszIn);

	while (nLength--)
	{
		switch (*lpszIn)
		{
		case '<': 
			strEncoded += "&lt;";
			break;
		case '>':
			strEncoded += "&gt;";
			break;
		case '&':
			strEncoded += "&amp;";
			break;
		case '\'': 
			strEncoded += "&apos;";
			break;
		case '\"':
			strEncoded += "&quot;";
			break;
		default:
			// just copy the character
			strEncoded += *lpszIn;
			break;
		}
		lpszIn++;
	}
	return strEncoded.AllocSysString();
}

⌨️ 快捷键说明

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