///////////////////////////////////////////////////////////////////////////////
// File: ScriptObject.cpp
///////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include <comutil.h>
#include "ScriptObject.h"
///////////////////////////////////////////////////////////////////////////////
// Construction
CScriptObject::CScriptObject()
{
// CoInitialize(NULL);
CommonConstruct(); // will throw exception if failed
}
CScriptObject::~CScriptObject()
{
// Destroy object- and release
// CoUninitialize();
// m_pScript->Release();
// if(m_pScript != NULL)
// m_pScript = NULL;
}
///////////////////////////////////////////////////////////////////////////////
// Members
///////////////////////////////////////////////////////////////////////////////
// CommonConstruct
void CScriptObject::CommonConstruct()
{
HRESULT hr = m_pScript.CreateInstance(__uuidof(ScriptControl));
_com_util::CheckError( hr ); // will throw an exception if failed
// will not come here if exception
_tcscpy(m_szLanguage, LANGUAGE_DEFAULT);
m_pScript->PutAllowUI( VARIANT_FALSE );
m_pScript->PutLanguage( _bstr_t(m_szLanguage ) );
}
///////////////////////////////////////////////////////////////////////////////
// GetLanguage : Get current script language
LPCTSTR CScriptObject::GetLanguage()
{
return m_szLanguage;
}
///////////////////////////////////////////////////////////////////////////////
// SetLanguage : Set current script language
void CScriptObject::SetLanguage(LPCTSTR szLanguage)
{
_tcscpy(m_szLanguage, szLanguage);
if (m_pScript != NULL)
{
m_pScript->PutLanguage( _bstr_t( szLanguage ) );
m_pScript->Reset();
m_FunctionList.clear();
}
}
///////////////////////////////////////////////////////////////////////////////
// GetMethodsCount
int CScriptObject::GetMethodsCount() const
{
return m_FunctionList.size();
}
///////////////////////////////////////////////////////////////////////////////
// GetNameAt : Get method name at specified index
LPCTSTR CScriptObject::GetNameAt(int index)
{
if( index >= 0 && index < m_FunctionList.size())
{
stl_string_list::iterator iter = m_FunctionList.begin();
while( index > 0)
{
iter++;
index--;
}
return (*iter).c_str();
}
return TEXT("");
}
///////////////////////////////////////////////////////////////////////////////
// Reset : Reset script control object
void CScriptObject::Reset()
{
if (m_pScript != NULL)
{
m_pScript->Reset();
// empty list...
m_FunctionList.clear();
}
}
///////////////////////////////////////////////////////////////////////////////
// GetErrorString : Get Script error string
LPCTSTR CScriptObject::GetErrorString()
{
m_szError[0] = 0;
if (m_pScript != NULL)
{
try{
IScriptErrorPtr pError = m_pScript->GetError();
if (pError != NULL)
{
_bstr_t desc = _bstr_t("Error: ") + pError->GetDescription() + _bstr_t(", ");
desc += pError->GetText() + _bstr_t("; in line ");
desc += _bstr_t(pError->GetLine());
int count = __min(desc.length(), ERROR_DESC_LEN); // string may be truncated...
_tcsncpy(m_szError, (LPCTSTR) desc, count);
m_szError[count] = 0;
pError->Clear();
}
}
catch(_com_error& e)
{
TRACE( (LPSTR)e.Description() );
TRACE( (LPSTR)"\n" );
}
}
return m_szError;
}
///////////////////////////////////////////////////////////////////////////////
// GetMethodsName: Get methods name list
bool CScriptObject::GetMethodsName()
{
bool bResult = false;
if (m_pScript != NULL)
{
IScriptProcedureCollectionPtr pIProcedures = m_pScript->GetProcedures();
// empty list...
m_FunctionList.clear();
try{
long count = pIProcedures->GetCount();
for(long index=1; index <= count; index++)
{
IScriptProcedurePtr pIProcPtr = pIProcedures->GetItem( _variant_t(index) );
_bstr_t name = pIProcPtr->GetName();
m_FunctionList.insert(m_FunctionList.end(), (LPCTSTR) name );
pIProcPtr = NULL;
}
bResult = true;
}
catch(...)
{
// Just catch the exception, call GetErrorString()
// to retreive last error
}
pIProcedures = NULL;
}
return bResult;
}
///////////////////////////////////////////////////////////////////////////////
// GetScriptFunction
// Get Script function name, this is useful for script with case sensitive
// function name.
LPCTSTR CScriptObject::GetScriptFunction(LPCTSTR szName)
{
stl_string_list::iterator iter = m_FunctionList.begin();
while( iter != m_FunctionList.end() )
{
if ((*iter).compare(szName) == 0)
return (*iter).c_str();
iter++;
}
return TEXT("");
}
//////////////////////////////////////////////////////////////////////////
// LoadScriptResource:
// Load a Script resource. This function loads and insert all functions
// and procedures to the component. The script resource may contain comments
// as well. Global variables may also be defined. You may want to see the
// script resource as a Module file in Visual Basic.
//////////////////////////////////////////////////////////////////////////
bool CScriptObject::LoadScriptResource(LPCTSTR lpName, LPCTSTR lpType, HINSTANCE hInstance )
{
try{
if (m_pScript)
{
HRSRC res = ::FindResource(hInstance, lpName, lpType);
ASSERT( res != NULL);
BYTE* pbytes = (BYTE*) LockResource(LoadResource(hInstance, res ));
ASSERT( pbytes != NULL);
_bstr_t strCode = (LPCSTR) (pbytes);
m_pScript->AddCode( strCode );
GetMethodsName();
return true;
}
}
catch(...)
{
// Just catch the exception, call GetErrorString()
// to retreive last error
}
return false;
}
///////////////////////////////////////////////////////////////////////////////
// LoadScript
// Load a Script File. This function loads and insert all functions and
// procedures to the component. The script file may contain comments as well.
// Global variables may also be define. You may want to see the script file
// as a Module file in Visual Basic.
// The script file is probably a simple text file (ASCII format)
///////////////////////////////////////////////////////////////////////////////
bool CScriptObject::LoadScript(LPCTSTR szFilename)
{
HANDLE hFile = CreateFile(szFilename, GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
DWORD dwSize = GetFileSize(hFile, NULL);
if (0xFFFFFFFF != dwSize)
{
BYTE* pbytes = (BYTE*) GlobalAlloc(GPTR, dwSize+1);
if (pbytes != NULL)
{
DWORD dwRead = 0;
bool bResult = false;
if (ReadFile(hFile, pbytes, dwSize, &dwRead, NULL))
{
try{
if (m_pScript)
{
_bstr_t strCode = (LPCSTR) (pbytes);
m_pScript->AddCode( strCode );
GetMethodsName();
bResult = true;
}
}
catch(...)
{
// Just catch the exception, call GetErrorString()
// to retreive last error
}
GlobalFree( (HGLOBAL) pbytes);
CloseHandle(hFile);
return bResult;
}
GlobalFree( (HGLOBAL) pbytes);
}
}
CloseHandle(hFile);
return false;
}
return false;
}
///////////////////////////////////////////////////////////////////////////////
// AddScript
// Use this function to add a script function, useful for internal use
// not global script (resource file).
///////////////////////////////////////////////////////////////////////////////
bool CScriptObject::AddScript(LPCTSTR szCode)
{
try {
if (m_pScript != NULL)
{
ULONG ref = m_pScript->AddRef();
_bstr_t strCode = szCode;
m_pScript->AddCode( strCode );
GetMethodsName();
ref = m_pScript->Release();
return true;
}
}
catch(...)
{
// Just catch the exception, call GetErrorString()
// to retre
ScriptObject (脚本解析类)
需积分: 50 198 浏览量
2010-07-21
15:49:54
上传
评论
收藏 3KB RAR 举报
rap2009
- 粉丝: 0
- 资源: 12
最新资源
- ctf web解题 找flag夺旗赛概述、原理及应用.pdf
- 2024%E5%B7%A5%E4%BD%9C%E7%BB%A9%E6%95%88%E8%A1%A8-%E4%BA%A7%E5%93%81%E4%B8%AD%E5%BF%83-%E7%A0%94%E5%8F%91%E4%BA%BA%E5%91%98-%E4%BF%A1%E9%98%B3.xlsx
- windows64位上运动的SSH代理
- 基于stm32f407vet6的智能小车
- 卡方数据.sav
- Python实现基于Django框架的二手物品购物网站设计源码+数据库.zip
- 基于Python和Django的二手物品购物网站设计源码+数据库脚本.zip
- 【计算机毕业设计】基于h5的移动网赚项目小程序【源码+lw+部署文档+讲解】
- 【计算机毕业设计】基于ssm-vue的oa系统设计与实现【源码+lw+部署文档+讲解】
- 基于pytorch实现的YOLOV5+SORT的车辆行人目标识别及追踪系统源码.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈