实例介绍
【实例简介】
【实例截图】
【实例截图】
【核心代码】
#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 = "<"; break; case '>': strEncoded = ">"; break; case '&': strEncoded = "&"; break; case '\'': strEncoded = "'"; break; case '\"': strEncoded = """; break; default: // just copy the character strEncoded = *lpszIn; break; } lpszIn ; } return strEncoded.AllocSysString(); }
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论