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

📄 vmodbcinstaller.cpp

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

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

   Description:    

                      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 <string.h>
#include <ctype.h>
#include <assert.h>
#include <odbcinst.h>
#include "VMODBCInstaller.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#endif



/*****************************************************************************/
/*
     FUNCTION NAME:  VMOdbcDsnInstaller

       DESCRIPTION:  Constructor

             INPUT:  void
            OUTPUT:  none
           RETURNS: void
*/
VMOdbcDsnInstaller::VMOdbcDsnInstaller( void )
{
  // get the system to define the path to the drivers
  //
  GetSystemDirectory( m_achSysDir, MAX_PATH );
  RetrieveInstalledDrivers();
}
/*  end of function "VMOdbcDsnInstaller" */
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  ~VMOdbcDsnInstaller

       DESCRIPTION:  Destructor

             INPUT:  void
            OUTPUT:  none
           RETURNS: void
*/
VMOdbcDsnInstaller::~VMOdbcDsnInstaller( void )
{
}
/*  end of function "~VMOdbcDsnInstaller" */
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  CreateDSNStringForSQLServer

       DESCRIPTION:  Does Just That......

             INPUT:  void
            OUTPUT:  none
           RETURNS:  void
*/
BOOL VMOdbcDsnInstaller::CreateDSNForSQLServer( char* pchDsnName, 
                                                char* pchDescription,
                                                char* pchServerName,
                                                char* pchUseDBName ) 
{
  int   iOffset = 0;
  char  achWorking[ 255 ];

  char  achOutput[ 1024 ];
  int   iOutBufferSize = 1024; 
                                                      
  memset( achOutput, '\0', iOutBufferSize );

  sprintf( achWorking , "DSN=%s", pchDsnName );
  memcpy( achOutput + iOffset, achWorking, strlen( achWorking ) );
  iOffset = strlen( achWorking ) + 1;

  sprintf( achWorking , "DESCRIPTION=%s", pchDescription );
  memcpy( achOutput + iOffset, achWorking, strlen( achWorking ) );
  iOffset += strlen( achWorking ) + 1;

  sprintf( achWorking, "SERVER=%s", pchServerName );
  memcpy( achOutput + iOffset, achWorking, strlen( achWorking ) );
  iOffset += strlen( achWorking ) + 1;

  sprintf( achWorking, "DATABASE=%s", pchUseDBName );
  memcpy( achOutput + iOffset, achWorking, strlen( achWorking ) );
  iOffset += strlen( achWorking ) + 1;

  strcpy( achWorking, "NETWORK=dbmssocn" );
  memcpy( achOutput + iOffset, achWorking, strlen( achWorking ) );
  iOffset += strlen( achWorking ) + 1;

  strcpy( achWorking, "Trusted_Connection=no" );
  memcpy( achOutput + iOffset, achWorking, strlen( achWorking ) );
  iOffset += strlen( achWorking ) + 1;

  return( CreateDsn( "SQL Server", achOutput ) );
}
/*  end of function "CreateDSNStringForSQLServer" */
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  CreateDsn

       DESCRIPTION: Will add DSNs to the system configuration according to 
                    the arguments given

             INPUT: pchDriver - the driver to use for the dsn
                    pchDsnString - specially constructed string for the creation

            OUTPUT:  none
           RETURNS: TRUE if all worked FALSE otherwise.
*/
BOOL VMOdbcDsnInstaller::CreateDsn( char* pchDriver, char* pchDsnString )
{
  // If the Driver is not installed then dont try it
  //
  if ( CheckDriverExists( pchDriver ) )
  {
    try
    {
#if (ODBCVER >= 0x0250)
      if ( !SQLConfigDataSource( NULL, 
                                 ODBC_ADD_SYS_DSN,               
                                 pchDriver,
                                 pchDsnString ) )
#else
      if ( !SQLConfigDataSource( NULL, 
                                 ODBC_ADD_DSN,               
                                 pchDriver,
                                 pchDsnString ) )
#endif
      {
         WORD     iError;
         DWORD    fErrorCode;
         char     lpszErrorMsg[1024];
         WORD     pcbErrorMsg;

         for ( iError = 1; iError < 9; iError++ )
         {
            SQLInstallerError(iError, &fErrorCode, lpszErrorMsg, 1023, &pcbErrorMsg );
         }
        return( FALSE );
      }
    }
    catch(...)
    {
      return( FALSE );
    }
  }
  return TRUE;
}
/*  end of function "CreateDsn" */
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  RetrieveInstalledDrivers

       DESCRIPTION: queries the system to discover all of the drivers which
                    have been installed. 

             INPUT: none
           RETURNS: TRUE if successful , FALSE otherwise
*/
BOOL VMOdbcDsnInstaller::RetrieveInstalledDrivers( void )
{
  WORD wCount;

  // Retrieve Double Null terminated list of installed Drivers
  //
  if ( !SQLGetInstalledDrivers( m_achInstalledDrivers, 2048, &wCount ) )
  {
    return FALSE;
  }

  // Check to see if any driver are installed.
  //
  if ( wCount == 0 )
  {
    return( FALSE );
  }

  return( TRUE );
}              
/*  end of function "RetrieveInstalledDrivers" */
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  CheckDriverExists

       DESCRIPTION:  compares the list of drivers installed in the system with
                    the name of the driver in lpSearch

             INPUT:  
            OUTPUT: none
           RETURNS: TRUE if a match is found ; FALSE if not
*/
BOOL VMOdbcDsnInstaller::CheckDriverExists( char* pchToFind )
{
  LPSTR lpTempDrvrs = m_achInstalledDrivers;

  while ( *lpTempDrvrs != '\0' )
  {
    if ( stricmp( lpTempDrvrs, pchToFind ) == 0 )
    {
      return( TRUE );
    }
    lpTempDrvrs += lstrlen( lpTempDrvrs ) + 1;
  }
  return( FALSE );
}
/*  end of function "CheckDriverExists" */
/*****************************************************************************/



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

⌨️ 快捷键说明

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