//-------------------------------------------------------------------
//-- Cancel Connection C++ sample - Copyright © 2006, Dave Moats
//--
//-- This sample is provided 'as-is', without any express or implied warranty.
//-- In no event will the authors be held liable for any damages arising from
//-- the use of this sample code.
//--
//-- Permission is granted to anyone to use this sample code for any purpose,
//-- including commercial applications, subject to the following restrictions:
//--
//-- The origin of this code must not be misrepresented;
//--       you must not claim that you wrote the original code.
//--       If you use this code, an acknowledgment in the
//--       documentation is requested - shown below:
//--
//-- Portions Copyright © 2006, Dave Moats (http://www.davemoats.com/).
//--
//-------------------------------------------------------------------

//-------------------------------------------------------------------
//-- To use this sample, you must include the following library and
//-- headers
//-------------------------------------------------------------------
#pragma comment ( lib, "Mpr.lib" )

#include <windows.h>
#include <tchar.h>

//-------------------------------------------------------------------
//-- Function:       closeConnection
//-- Arguments:    TCHAR *RemoteMachine - remote machine name
//-- Returns:       unsigned long error code from the cancelconnection call
//-- Description: Takes the incoming string and formats it into the match
//--                      for the bind example on this site.
//--
//--    NOTE: If you use a different share, NOT IPC$, this code will not work
//-------------------------------------------------------------------
unsigned long closeConnection ( TCHAR *RemoteMachine )
{
      // make sure the incoming value is not NULL
      if ( !RemoteMachine )
      {
            // should set a custom error here
            return 5;
      }

      // set up our local variables
      TCHAR *leadingSlashes = _T ( "\\\\" );
      TCHAR *targetShare = _T ( "\\IPC$" );
      TCHAR    targetMachine[256] = { NULL };

      bool addSlashes = false;

      unsigned long stringLength = 0;

      // see if we need to add the leading backslashes to the incoming string
      if ( *RemoteMachine != '\\' )
      {
         stringLength = _tcslen ( leadingSlashes) + _tcslen ( RemoteMachine) + _tcslen ( targetShare);
         addSlashes = true;
      }
      else
      {
         stringLength = _tcslen ( RemoteMachine) + _tcslen ( targetShare);
         addSlashes = false;
      }

      // since we are using an array make sure we don't try to overwrite it
      if ( stringLength > 254 )
      {
            // this should be replaced with a custom error code
            return 5;
      }

      // build the string to use as the target
      if ( addSlashes )
      {
         _tcscpy ( targetMachine, leadingSlashes );
         _tcscat ( targetMachine, RemoteMachine );
      }
      else
      {
         _tcscpy ( targetMachine, RemoteMachine );
      }

      _tcscat ( targetMachine, targetShare );

      // now try to break a connection to the machine
      ulErr =    WNetCancelConnection2 ( targetMachine,
                                                      CONNECT_UPDATE_PROFILE,
                                                      TRUE ));
  
      // send back the result
      return ulErr;

}


Copyright © 2005 - 2013 Dave Moats.  All rights reserved.  Links: Copyright © by their respective owners.
NO WARRANTIES EXTENDED.  Void where prohibited by law.  Please report any issues or broken links.
You may link to this site freely from your own site.  You may quote from this site, but please include a link to the original source on the originating site.