Created On:  29 March 2012

Problem:

I need to write a file to a remote drive on a server that is not part of the domain on which we are running.  How do we open the file on this drive and supply the username and password required to connect to the remote drive?

Resolution:

*----------------------------------------------------------------*
*                     NetConnect                                 *
*                                                                *
* This program demonstrates the use of the Windows SDK API calls *
* to map a local drive letter to a shared resource on a differ-  *
* ent machine on your network. The remote resource must be setup *
* as a shared resource in order for this sample to work.         *
*                                                                *
* This call accepts a User Name and a Password parameter which   *
* work as follows:                                               *
*                                                                *
* If lpPassword is NULL, the function uses the current default   *
* password associated with the user specified by lpUserName.     *
* If lpPassword points to an empty string, the function does not *
* use a password.                                                *
*                                                                *
* lpUsername points to a null-terminated string that specifies   *
* a user name to be used in making the connection. If lpUserName *
* is NULL, the function uses the default user name. The user     *
* context for the process provides the default user name.        *
* The lpUserName parameter is specified when users want to       *
* connect to a network resource for which they have been         *
* assigned a user name or account other than the default user    *
* name or account.                                               *
*                                                                *
* After the connection has been made a sequential file will be   *
* created and written to on the mapped drive.                    *
*                                                                *
* To run this sample simply change the LocalName, RemoteName,    *
* Username and Password parameters to values which are accepted  *
* on your network and rebuild the project.                       *
*                                                                *
* This sample loads the file MPR.DLL which is part of the        *
* operating system. This file must be available within the PATH  *
* in order for this program to work correctly.                   *
* By default it should be within the Windows system directory.   *
*----------------------------------------------------------------*

id division.
program-id. netconnect.
environment division.
special-names.
   call-convention 66 is WINAPI.
input-output section.

   select test-file assign to "J:\netconnect.txt"
                    organization is line sequential
                   
file status is file-status.
data division.
fd test-file.
01 test-record pic x(20).
working-storage section.
0
1 file-status pic x(2) value spaces.
copy "netvalue.cpy".
78 PROGRAM-NAME value "NETCONNECT".
*> LocalName contains the drive letter that you wish to map to
*> the remote drive.
01 LocalName pic x(20) value z"J:".
*> RemoteName is the machine name and resource to which you wish
*> to connect. The following will map the drive letter to the
*> C drive of the computer with the name Laptop. This must be
*> setup as a shared resource on the remote computer before you
*> can map to it.
01 RemoteName pic x(256) value z\\COMP-NAME\share-name.
*> Use username and password of a user account.
*> If this is passed as NULL then the default username and
*> password will be used.
01 lpUsername pic x(20) value z"USER".
01 lpPassword pic x(20) value z"password".
01 dwFlags pic s9(9) comp-5.
01 fForce pic s9(9) comp-5 value 0.
01 NetResource. *> structure as defined in Windows SDK
   05 dwScope pic s9(9) comp-5.
   05 dwType pic s9(9) comp-5.
   05 dwDisplayType pic s9(9) comp-5.
   05 dwUsage pic s9(9) comp-5.
   05 lpLocalName pointer.
   05 lpRemoteName pointer.
   05 lpComment pointer.
   05 lpProvider pointer.
01 ret-code pic s9(9) comp-5.
01 dll-point procedure-pointer.
procedure division.
000-begin.

    set
dll-point to entry "MPR.DLL"
    if dll-point = NULL
       display "Cannot Load MPR.DLL!"
       stop run
    end-if

    display
PROGRAM-NAME ": Starting"
*>  We want to map to a disk drive.
*>  You can also map to a printer by changing this parameter.
    move RESOURCETYPE-DISK to dwType
*>  The following items in the passed NetResource structure must
*>  be passed as pointers.
    set lpLocalName to address of LocalName
    set lpRemoteName to address of RemoteName
    set lpComment to NULL *> Not used in this call
    set lpProvider to NULL *> Not used in this call
*>  You could set dwFlags to CONNECT-UPDATE-PROFILE in which case
*>  the connection will be remembered and will be reconnected
*>  during future logins. In this example this option is not set.
    move 0 to dwFlags
    call WINAPI "WNetAddConnection2A"
       using by reference NetResource
                          lpPassword
*>  Replace lpPassword by BY VALUE 0 if you wish to use default.
                          lpUsername
*>  Replace lpUsername by BY VALUE 0 if you wish to use default.
             by value dwFlags
       returning ret-code
    end-call
    if ret-code = NO-ERROR
       display "Network drive mapping successful!"
       open output test-file
       if file-status = "00"
          display "file open OK"
          move "Test is successful!" to test-record
          write test-record
          close test-file
       else
          display "open error = " file-status
       end-if
       move 0 to dwFlags fForce
       call WINAPI "WNetCancelConnection2A"
          using by reference localName
                by value dwFlags
                         fForce
          returning ret-code
       end-call
       if ret-code = NO-ERROR
          display "Connection removed!"
       else
          display "Error on Connection remove = " ret-code
       end-if
    else
       display "Error Occurred = " ret-code
       *> Check error constants in working-storage.
    end-if
    display PROGRAM-NAME ": Ending"
    stop run.

Incident #2566374