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

📄 vmnetworkdrive.cpp

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

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

    Description: This class knows how to connect to remote hosts

                      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 <winbase.h>
#include <winnt.h>
#include "VMNetworkDrive.h"



/*****************************************************************************/
/*
     FUNCTION NAME:  VMNetworkDrive::VMNetworkDrive

       DESCRIPTION:  ctor

             INPUT:  void
            OUTPUT:  none

           RETURNS:  none
*/
VMNetworkDrive::VMNetworkDrive( bool bAutoDelete ) : VMSysLastError()
{
  m_bAutoDelete  = bAutoDelete;
  m_bIsConnected = false;
}
/* End of function "VMNetworkDrive::VMNetworkDrive"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMNetworkDrive::Connect

       DESCRIPTION:  creates a path on a remote server

             INPUT:  pchServerShare - \\SERVER\SHARE
                     pchUserID - the logon id to log on to the server with
                     pchPassword - the logon pwd for the remote server
                     pchRelativePath - the path to create under the share
            OUTPUT:  

           RETURNS:  true if successful, false otherwise
*/
bool VMNetworkDrive::Connect( const char* pchServerShare, 
                             const char* pchUserID, 
                             const char* pchPassword )
{
  // only one drive connection per class instance
  //
  if ( m_bIsConnected )
  {
    return( false );
  } 

  if ( ConnectToNetworkDrive( m_achNetworkDrive, pchServerShare, pchPassword, pchUserID ) )
  {
    m_bIsGood       = true;
    m_bIsConnected  = true;

    m_achNetworkDrive[1] = ':';
    m_achNetworkDrive[2] = 0x5c;  // put in a backslash " \ "
    m_achNetworkDrive[3] = 0;
  }
  else
  {
    m_bIsGood = false;
  }
  return( m_bIsGood );
}
/* End of function "VMNetworkDrive::Connect"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMNetworkDrive::~VMNetworkDrive

       DESCRIPTION:  dtor. if this allocated a network connection, then the
                     dtor must free the network connection before this goes
                     away

             INPUT:  void
            OUTPUT:  none

           RETURNS:  none
*/
VMNetworkDrive::~VMNetworkDrive( void )
{
  if ( m_bAutoDelete && m_bIsConnected )
  {
    DisconnectNetworkDrive( m_achNetworkDrive[ 0 ] );
  }
}
/* End of function "VMNetworkDrive::~VMNetworkDrive"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:	VMNetworkDrive::FindAvailableDrive

       DESCRIPTION: Searches from "z" backwards, looking for an available
                    drive letter.

             INPUT:	none
           RETURNS:	the letter if found,
*/
char VMNetworkDrive::FindAvailableDrive( void )
{
  DWORD       dwResult;
  char        chDriveLetter;
  char        achLocalName[ 3 ];
  char        achDeviceName[ 2 * MAX_PATH ];
  DWORD       dwCount = sizeof( achDeviceName );

  // find an available drive letter
  // 
  achLocalName[ 1 ] = ':';
  achLocalName[ 2 ] = 0;

  for( chDriveLetter = 'z'; chDriveLetter > 'f'; chDriveLetter-- )
  {
    achLocalName[ 0 ] = chDriveLetter;
    dwResult = WNetGetConnection( achLocalName, achDeviceName, &dwCount );
    if( dwResult == ERROR_NOT_CONNECTED )
    {
      return( chDriveLetter );
    }
  }
  return( -1 );
}
/* end of function "VMNetworkDrive::FindAvailableDrive" */
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:	VMNetworkDrive::ConnectToNetworkDrive

       DESCRIPTION:	Connects to a server and assigns a "drive" letter to it.

             INPUT:	pchDriveLetter - the drive letter to assign
                    pchRemoteName - reference to the server name, e.g. \\sacdevs0\lthomas
                    pchPassword - reference to the password for the user ID
                    pchUserName - reference to the user ID for the connection
            OUTPUT: modifies the value of cDriveLetter for passage back to superordinate
           RETURNS:	true if no error, false otherwise.
*/
bool VMNetworkDrive::ConnectToNetworkDrive ( char*       pchDriveLetter, 
                                            const char* pchRemoteName, 
                                            const char* pchPassword, 
                                            const char* pchUserName )
{
  bool        bReturn = true;
  DWORD       dwResult;
  NETRESOURCE xNetResource;
  char        chAvailableDriveLetter;
  char        achLocalName[3];
  char*       pchPass = NULL;
  char*       pchUser = NULL;

  chAvailableDriveLetter = FindAvailableDrive();

  *pchDriveLetter = chAvailableDriveLetter;
   
  if ( (-1) == (const int) *pchDriveLetter ) 
  {
    return( false ); 
  }

  if ( NULL != pchPassword )
  {
    pchPass = ( strlen( pchPassword ) ) ? const_cast<char*>( pchPassword ) : NULL;
  }

  if ( NULL != pchUserName )
  {
    pchUser = ( strlen( pchUserName ) ) ? const_cast<char*>( pchUserName ) : NULL;
  }

  achLocalName[ 0 ] = chAvailableDriveLetter;
  achLocalName[ 1 ] = ':';
  achLocalName[ 2 ] = 0;

  memset( (void*)&xNetResource, 0, sizeof( xNetResource ) );
  xNetResource.dwType        = RESOURCETYPE_DISK;
  xNetResource.lpLocalName   = achLocalName;
  xNetResource.lpRemoteName  = (char*)pchRemoteName;
  xNetResource.lpProvider    = NULL;

  dwResult = WNetAddConnection2( &xNetResource, pchPass, pchUser, 0 );

  if ( dwResult != NO_ERROR ) 
  {
    SetLastError();

    switch (dwResult)
    {
      case ERROR_ACCESS_DENIED:
        SetExtendedErrorInfo( "Access to the network resource was denied." );
      break;

      case ERROR_ALREADY_ASSIGNED:
        SetExtendedErrorInfo( "The local device specified is already connected to a network resource." );
      break;

      case ERROR_BAD_DEV_TYPE:
        SetExtendedErrorInfo( "The type of local device and the type of network resource do not match." );
      break;

      case ERROR_BAD_DEVICE:
        SetExtendedErrorInfo( "The value specified by lpLocalName is invalid." );
      break;

      case ERROR_BAD_NET_NAME:
        SetExtendedErrorInfo( "The value specified by lpRemoteName is not acceptable to any "
                              "network resource provider. The resource name is invalid, or the "
                              "named resource cannot be located." );
      break;

      case ERROR_BAD_PROFILE:
        SetExtendedErrorInfo( "The user profile is in an incorrect format." );
      break;

      case ERROR_BAD_PROVIDER:
        SetExtendedErrorInfo( "The value specified by lpProvider does not match any provider." );
      break;

      case ERROR_BUSY:
        SetExtendedErrorInfo( "The router or provider is busy, possibly initializing. "
                              "The caller should retry." );
      break;

      case ERROR_CANCELLED:
        SetExtendedErrorInfo( "The attempt to make the connection was cancelled by the "
                              "user through a dialog box from one of the network resource "
                              "providers, or by a called resource." );
      break;

      case ERROR_CANNOT_OPEN_PROFILE:
        SetExtendedErrorInfo( "The system is unable to open the user profile to process persistent connections." );
      break;

      case ERROR_DEVICE_ALREADY_REMEMBERED:
        SetExtendedErrorInfo( "An entry for the device specified is already in the user profile." );
      break;

      case ERROR_EXTENDED_ERROR:
        SetExtendedErrorInfo( "A network-specific error occured. Call the WNetGetLastError "
                              "function to get a description of the error." );
      break;

      case ERROR_INVALID_PASSWORD:
        SetExtendedErrorInfo( "The specified password is invalid." );
      break;

      case ERROR_NO_NET_OR_BAD_PATH:
        SetExtendedErrorInfo( "A network component has not started, "
                              "or the specified name could not be handled." );
      break;

      case ERROR_NO_NETWORK:
        SetExtendedErrorInfo( "There is no network present." );
      break;
    }
    bReturn = false;
  }

  return( bReturn );
}
/* end of function "VMNetworkDrive::ConnectToNetworkDrive" */
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:	VMNetworkDrive::DisconnectNetworkDrive

       DESCRIPTION: Disconnects from a server connection.	

             INPUT:	cDriveLetter - the letter assigned to the server connection
           RETURNS:	void
*/
void VMNetworkDrive::DisconnectNetworkDrive( char chDriveLetter )
{
  DWORD   dwResult;
  char    achLocalName[ 3 ];

  achLocalName[0] = chDriveLetter;
  achLocalName[1] = ':';
  achLocalName[2] = 0;

  dwResult = WNetCancelConnection2( achLocalName, 0, true );

  switch( dwResult )
  {
    case NO_ERROR:
    case ERROR_NOT_CONNECTED:
      m_bIsConnected = false;
    break;

    default:
      SetLastError();
    break;
  }
  m_bIsConnected = false;
}
/* end of function "VMNetworkDrive::DisconnectNetworkDrive" */
/*****************************************************************************/


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


⌨️ 快捷键说明

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