Exemple #1
0
HRESULT CScript::LoadScript( LPCTSTR pszFileName, LPCTSTR pszScriptName )
{
   ITypeInfoPtr pTypeInfo;
   HRESULT hResult;
   CLSID clsid;
   CFile file;
   ULONGLONG nFileSize;
   BSTR bstrScript;
   BOOL tSuccess;
   int iMethod;
   UINT nNames;
   BSTR bstrName;
   CString strName;
   CString strExtension;
   int iChar;

   ENSURE( pszFileName != NULL );
   ENSURE( pszScriptName != NULL );
   ENSURE( m_pActiveScript == NULL );

   iChar = lstrlen( pszFileName )-1;
   while( (iChar >= 0) && (pszFileName[iChar] != _T( '.' )) )
   {
	  iChar--;
   }

   if( (iChar >= 0) && (_tcsicmp( &pszFileName[iChar], _T( ".js" ) ) == 0) )
   {
	  hResult = CLSIDFromProgID( L"JScript", &clsid );
	  if( FAILED( hResult ) )
	  {
		 return( hResult );
	  }
   }
   else
   {
	  hResult = CLSIDFromProgID( L"VBScript", &clsid );
	  if( FAILED( hResult ) )
	  {
		 return( hResult );
	  }
   }

   hResult = m_pActiveScript.CreateInstance( clsid, NULL,
	  CLSCTX_INPROC_SERVER );
   if( FAILED( hResult ) )
   {
	  return( hResult );
   }

   m_pActiveScriptParse = m_pActiveScript;
   if( m_pActiveScriptParse == NULL )
   {
	  return( E_NOINTERFACE );
   }

   hResult = m_pActiveScript->SetScriptSite( &m_xActiveScriptSite );
   if( FAILED( hResult ) )
   {
	  return( hResult );
   }

   hResult = m_pActiveScriptParse->InitNew();
   if( FAILED( hResult ) )
   {
	  return( hResult );
   }

   CFileException error;
   tSuccess = file.Open( pszFileName, CFile::modeRead|CFile::shareDenyWrite,
	  &error );
   if( !tSuccess )
   {
	  return( HRESULT_FROM_WIN32( error.m_lOsError ) );
   }

   nFileSize = file.GetLength();
   if( nFileSize > INT_MAX )
   {
      return( E_OUTOFMEMORY );
   }

   nFileSize = file.Read( m_strScriptText.GetBuffer( ULONG( nFileSize ) ), ULONG( nFileSize ) );
   file.Close();
   m_strScriptText.ReleaseBuffer( ULONG( nFileSize ) );
   bstrScript = m_strScriptText.AllocSysString();

   hResult = m_pActiveScriptParse->ParseScriptText( bstrScript, NULL, NULL,
	  NULL, DWORD( this ), 0, SCRIPTTEXT_ISVISIBLE, NULL, NULL );
   SysFreeString( bstrScript );
   if( FAILED( hResult ) )
   {
	  return( hResult );
   }

   hResult = m_pManager->AddNamedItems( m_pActiveScript );
   if( FAILED( hResult ) )
   {
	  return( hResult );
   }

   hResult = m_pActiveScript->SetScriptState( SCRIPTSTATE_CONNECTED );
   if( FAILED( hResult ) )
   {
	  return( hResult );
   }

   hResult = m_pActiveScript->GetScriptDispatch( NULL, &m_pDispatch );
   if( FAILED( hResult ) )
   {
	  return( hResult );
   }

   hResult = m_pDispatch->GetTypeInfo( 0, GetUserDefaultLCID(), &pTypeInfo );
   if( FAILED( hResult ) )
   {
	  return( hResult );
   }

   CSmartTypeAttr pTypeAttr( pTypeInfo );
   CSmartFuncDesc pFuncDesc( pTypeInfo );

   hResult = pTypeInfo->GetTypeAttr( &pTypeAttr );
   if( FAILED( hResult ) )
   {
	  return( hResult );
   }

   for( iMethod = 0; iMethod < pTypeAttr->cFuncs; iMethod++ )
   {
	  hResult = pTypeInfo->GetFuncDesc( iMethod, &pFuncDesc );
	  if( FAILED( hResult ) )
	  {
		 return( hResult );
	  }

	  if( (pFuncDesc->funckind == FUNC_DISPATCH) && (pFuncDesc->invkind ==
		 INVOKE_FUNC) && (pFuncDesc->cParams == 0) )
	  {
		 bstrName = NULL;
		 hResult = pTypeInfo->GetNames( pFuncDesc->memid, &bstrName, 1,
			&nNames );
		 if( FAILED( hResult ) )
		 {
			return( hResult );
		 }
		 ASSERT( nNames == 1 );

		 strName = bstrName;
		 SysFreeString( bstrName );
		 bstrName = NULL;

		 // Macros can't contain underscores, since those denote event handlers
		 if( strName.Find( _T( '_' ) ) == -1 )
		 {
			m_mapMacros.SetAt( strName, pFuncDesc->memid );
		 }
	  }

	  pFuncDesc.Release();
   }

   m_strScriptName = pszScriptName;

   return( S_OK );
}
Exemple #2
0
HRESULT CInterfaceInfo::Init( ITypeInfo* pTypeInfo )
{
	HRESULT hResult;
	CSmartTypeAttr pTypeAttr( pTypeInfo );
	CSmartFuncDesc pFuncDesc( pTypeInfo );
	CSmartVarDesc pVarDesc( pTypeInfo );
	int iVar;
	int iMethod;
	int iDestMethod;
	bool tFound;

	DbgAssert( pTypeInfo != NULL );

	hResult = pTypeInfo->GetTypeAttr( &pTypeAttr );
	if( FAILED( hResult ) )
	{
		return( hResult );
	}

	m_iid = pTypeAttr->guid;

	// Allocate enough space for the maximum number of methods that we could
	// possibly have, plus room for two methods for each property in case the
	// type info doesn't specify properties as put and set methods.
	m_apMethodInfo.SetCount( pTypeAttr->cFuncs+(2*pTypeAttr->cVars) );

	iDestMethod = 0;
	for( iMethod = 0; iMethod < pTypeAttr->cFuncs; iMethod++ )
	{
		hResult = pTypeInfo->GetFuncDesc( iMethod, &pFuncDesc );
		if( FAILED( hResult ) )
		{
			return( hResult );
		}

		// Only add the function to our list if it is at least at nesting level
		// 2 (i.e. defined in an interface derived from IDispatch).
		if( !(pFuncDesc->wFuncFlags&FUNCFLAG_FRESTRICTED) &&
		(pFuncDesc->funckind == FUNC_DISPATCH) )
		{
			m_apMethodInfo[iDestMethod] = new CMethodInfo;
			if( m_apMethodInfo[iDestMethod] == NULL )
			{
				return( E_OUTOFMEMORY );
			}
			hResult = m_apMethodInfo[iDestMethod]->Init( pTypeInfo, pFuncDesc );
			if( FAILED( hResult ) )
			{
				return( hResult );
			}
			iDestMethod++;
		}
		pFuncDesc.Release();
	}

	for( iVar = 0; iVar < pTypeAttr->cVars; iVar++ )
	{
		hResult = pTypeInfo->GetVarDesc( iVar, &pVarDesc );
		if( FAILED( hResult ) )
		{
			return( hResult );
		}

		if( (pVarDesc->varkind == VAR_DISPATCH) && !(pVarDesc->wVarFlags&(VARFLAG_FRESTRICTED ) ))
		{
			tFound = false;
			for( iMethod = 0; iMethod < iDestMethod; iMethod++ )
			{
				if( (m_apMethodInfo[iMethod]->GetID() == pVarDesc->memid) &&
					(m_apMethodInfo[iMethod]->GetInvokeKind() == INVOKE_PROPERTYGET) )
				{
					tFound = true;
				}
			}

			if( !tFound )
			{
				// We haven't already added this one as a method, so do it now.
				m_apMethodInfo[iDestMethod] = new CMethodInfo;
				if( m_apMethodInfo[iDestMethod] == NULL )
				{
					return( E_OUTOFMEMORY );
				}
				hResult = m_apMethodInfo[iDestMethod]->InitPropertyGet(pTypeInfo, pVarDesc);
				if( FAILED( hResult ) )
				{
					return( hResult );
				}
				iDestMethod++;
			}

			if( !(pVarDesc->wVarFlags&VARFLAG_FREADONLY) )
			{
				tFound = false;
				for( iMethod = 0; iMethod < iDestMethod; iMethod++ )
				{
					if( (m_apMethodInfo[iMethod]->GetID() == pVarDesc->memid) &&
						(m_apMethodInfo[iMethod]->GetInvokeKind() ==
					INVOKE_PROPERTYPUT) )
					{
						tFound = true;
					}
				}

				if( !tFound )
				{
					// We haven't already added this one as a method, so do it now.

					m_apMethodInfo[iDestMethod] = new CMethodInfo;
					if( m_apMethodInfo[iDestMethod] == NULL )
					{
						return( E_OUTOFMEMORY );
					}
					hResult = m_apMethodInfo[iDestMethod]->InitPropertyPut(
					pTypeInfo, pVarDesc );
					if( FAILED( hResult ) )
					{
						return( hResult );
					}
					iDestMethod++;
				}
			}
		}

		pVarDesc.Release();
	}

	// Trim the array down to the actual size.
	m_apMethodInfo.SetCount( iDestMethod );

	return S_OK;
}