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

📄 vmextdllmanager.cpp

📁 TOOL (Tiny Object Oriented Language) is an easily-embedded, object-oriented, C++-like-language inter
💻 CPP
字号:
/*****************************************************************************/
/*                              SOURCE FILE                                  */
/*****************************************************************************/
/*
       $Archive: $

      $Revision: $
          $Date: $
        $Author: $

    Description: Implementation of an object that knows how to dynamically
                 load and make calls to a dll that supports a known "callable
                 api" or set of exported methods.

                      TOOL And XML FORMS License
                      ==========================

                      Except where otherwise noted, all of the documentation 
                      and software included in the TOOL package is 
                      copyrighted by Michael Swartzendruber.

                      Copyright (C) 2005 Michael John Swartzendruber. 
                      All rights reserved.

                      Access to this code, whether intentional or accidental,
                      does NOT IMPLY any transfer of rights.

                      This software is provided "as-is," without any express 
                      or implied warranty. In no event shall the author be held
                      liable for any damages arising from the use of this software.

                      Permission is granted to anyone to use this software for 
                      any purpose, including commercial applications, and to 
                      alter and redistribute it, provided that the following 
                      conditions are met:

                      1. All redistributions of source code files must retain 
                         all copyright notices that are currently in place, 
                         and this list of conditions without modification.

                      2. The origin of this software must not be misrepresented;
                         you must not claim that you wrote the original software.

                      3. If you use this software in another product, an acknowledgment
                         in the product documentation would be appreciated but is
                         not required.

                      4. Modified versions in source or binary form must be plainly 
                         marked as such, and must not be misrepresented as being 
                         the original software.
*/
static char OBJECT_ID[] = "$Revision: $ : $Date: $";
/*****************************************************************************/


#include "../../../stdafx.h"
#include "../VMCoreGlobal.h"
#include "VMExtDllManager.h"



/*****************************************************************************/
/*
     FUNCTION NAME:  VMExtDllManager::VMExtDllManager

       DESCRIPTION:  Load and force registration of dll

             INPUT:  pchDllName - pointer to the name of dll to be managed by
                                  this object
            OUTPUT:  none

           RETURNS:  
*/
VMExtDllManager::VMExtDllManager( const char* pchDllName )
: m_hDLLHandle( NULL ), 
  m_bIsGood( true ),
  m_pInit( 0 ), 
  m_pRunProcAddr( 0 ), 
  m_pCleanup( 0 ) 
{
  LoadDLL( pchDllName );
  MapDllProcPtrs();
}
/* End of function "VMExtDllManager::VMExtDllManager"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMExtDllManager::~VMExtDllManager

       DESCRIPTION:  unload the dll

             INPUT:  void
            OUTPUT:  none

           RETURNS:  none
*/
VMExtDllManager::~VMExtDllManager( void )
{
  ::FreeLibrary( m_hDLLHandle );
}
/* End of function "VMExtDllManager::~VMExtDllManager"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMExtDllManager::RunCmd

       DESCRIPTION:  Calls the Run method for the dll

             INPUT:  poJobData - pointer to the data object to run
            OUTPUT:  none

           RETURNS:  results of call, or -1 if method was not bound
*/
HRESULT VMExtDllManager::RunCmd( int iMsg, int iCmd, int iParam, VMVariant* pvData ) const
{
  if ( m_pRunProcAddr != 0 )
  {
    return( (*m_pRunProcAddr)( iMsg, iCmd, iParam, pvData ) );
  }
  else
  {
    return( -1 );
  }
}
/* End of function "VMExtDllManager::RunCmd"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMExtDllManager::LoadDLL

       DESCRIPTION:  Attempts to load the dll and then will call the dll's
                     Registration function.

             INPUT:  pchDllName - pointer to the name of the dll to load
            OUTPUT:  none

           RETURNS:  Handle to the loaded module
*/
HMODULE VMExtDllManager::LoadDLL( const char* pchDllName )
{
  char achCurPath[ MAX_PATH + 1];

  GetCurrentDirectory( MAX_PATH, achCurPath );
  strcat( achCurPath, "\\" );
  strcat( achCurPath, pchDllName );

  m_hDLLHandle = ::LoadLibrary( pchDllName );
  if ( m_hDLLHandle == NULL ) 
  {
     DWORD  dwErr = GetLastError();
     LPVOID lpMsgBuf;

     FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | 
                    FORMAT_MESSAGE_FROM_SYSTEM     |     
                    FORMAT_MESSAGE_IGNORE_INSERTS,    
                    NULL,
                    dwErr,
                    MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),
                    (LPTSTR)&lpMsgBuf,    
                    0,    
                    NULL );

     // Free the buffer.
     //
     LocalFree( lpMsgBuf );

     m_bIsGood = false;
  }
  return( m_hDLLHandle );
}
/* End of function "VMExtDllManager::LoadDLL"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMExtDllManager::Init

       DESCRIPTION:  calls the Init method of the dll loaded by this object

             INPUT:  void 
            OUTPUT:  none

           RETURNS:  results of the call, or -1 on error
*/
HRESULT VMExtDllManager::Init( void ) const
{
  if ( m_pInit != 0 )
  {
    return( ( *m_pInit )() );
  }
  else
  {
    return( -1 );
  }
}
/* End of function "VMExtDllManager::Init"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMExtDllManager::Cleanup

       DESCRIPTION:  calls the cleanup method of the dll loaded by this object

             INPUT:  void 
            OUTPUT:  none

           RETURNS:  results of the call, or -1 on error
*/
HRESULT VMExtDllManager::Cleanup( void ) const
{
  if ( m_pCleanup != 0 )
  {
    return( ( *m_pCleanup )() );
  }
  else
  {
    return( -1 );
  }
}
/* End of function "VMExtDllManager::Cleanup"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMExtDllManager::MapDllProcPtrs

       DESCRIPTION:  After the dll is loaded, then this method will bind each
                     of expected methods to internal method pointers

             INPUT:  void
            OUTPUT:  none

           RETURNS:  true if worked, false if errors
*/
bool VMExtDllManager::MapDllProcPtrs( void )
{
  if ( ( m_hDLLHandle != NULL ) && ( m_hDLLHandle != INVALID_HANDLE_VALUE ) ) 
  {
    m_pRunProcAddr = (HR_Job_Proc)::GetProcAddress( m_hDLLHandle, RunProcName   );
    m_pInit        = (HR_v_Proc)  ::GetProcAddress( m_hDLLHandle, InitProcName );
    m_pCleanup     = (HR_v_Proc)  ::GetProcAddress( m_hDLLHandle, CleanupProcName );
  }

  m_bIsGood = m_pInit != 0 && m_pCleanup != 0 && m_pRunProcAddr != 0;

  return( m_bIsGood );
}
/* End of function "VMExtDllManager::MapDllProcPtrs"
/*****************************************************************************/



/*****************************************************************************/
/* Check-in history */
/*
 *$Log: $
*/
/*****************************************************************************/

⌨️ 快捷键说明

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