//-------------------------------------------------------------------
//-- Create 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:       connectToMachine
//-- Arguments:    TCHAR *RemoteMachine - remote machine name
//--                      TCHAR *BindUid - userid to use when attempting to connect
//--                      TCHAR *BindPwd - password to use when binding
//-- Returns:       unsigned long error code from the WNetAddConnection2 call
//-- Description: Takes the incoming string and formats it into a connection
//--                      string something like \\machine\IPC$ and then uses the
//--                      userid and password to attempt a bind to this machine
//--
//--    NOTE: The password is not encrypted when it sent over the network
//-------------------------------------------------------------------
unsigned long connectToMachine ( TCHAR *RemoteMachine, TCHAR *BindUid, TCHAR *BindPwd )
{
      // make sure the incoming values are not NULL
      if ( ( !RemoteMachine ) || ( !BindUid ) || ( !BindPwd ))
      {
            // 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 );

      // set up our NETRESOURCE structure
      NETRESOURCE nr;
      nr.dwType = RESOURCETYPE_ANY;
      nr.lpLocalName = NULL;
      nr.lpRemoteName = targetMachine;
      nr.lpProvider = NULL;

      // now try to create a connection to the machine
      ulErr =    WNetAddConnection2 ( &nr,
                                                BindPwd,
                                                BindUid,
                                                FALSE );

      // 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.