Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Connecting to Network Drive with Win API

Tips -N- Tricks

Connecting to Network Drive with Win API

by  danceman  Posted    (Edited  )
DECLARE INTEGER WNetAddConnection IN "mpr.dll" ;
STRING cRemoteName, ;
STRING cPassword, ;
STRING cLocalName
DECLARE INTEGER WNetCancelConnection in WIN32API String,integer


#DEFINE NO_ERROR 0
* Error Constants: for connecting
#DEFINE ERROR_ACCESS_DENIED = 5
#DEFINE ERROR_ALREADY_ASSIGNED = 85
#DEFINE ERROR_BAD_DEV_TYPE = 66
#DEFINE ERROR_BAD_DEVICE = 1200
#DEFINE ERROR_BAD_NET_NAME = 67
#DEFINE ERROR_BAD_PROFILE = 1206
#DEFINE ERROR_BAD_PROVIDER = 1204
#DEFINE ERROR_BUSY = 170
#DEFINE ERROR_CANCELLED = 1223
#DEFINE ERROR_CANNOT_OPEN_PROFILE = 1205
#DEFINE ERROR_DEVICE_ALREADY_REMEMBERED = 1202
#DEFINE ERROR_EXTENDED_ERROR = 1208
#DEFINE ERROR_INVALID_PASSWORD = 86
#DEFINE ERROR_NO_NET_OR_BAD_PATH = 1203

* Error Constants: for disconnecting
#DEFINE ERROR_BAD_PROFILE 1206
#DEFINE ERROR_CANNOT_OPEN_PROFILE 1205
#DEFINE ERROR_DEVICE_IN_USE 2404
#DEFINE ERROR_EXTENDED_ERROR 1208
#DEFINE ERROR_NOT_CONNECTED 2250
#DEFINE ERROR_OPEN_FILES 2401

nReturn = 0
lForce = .t. && disconnect even if files or jobs are on connection
lNoForce = .f. && fail to dissconnect if files or jobs are in connection

cResource = "\\Server\ShareName"
cPassword = "MyPassword" && empty if not needed
cDriveLetter = "Y:"

nReturn = WNetAddConnection(cResource,cPassword,cDriveLetter)
if nReturn = NO_ERROR
messagebox('Connection to server Successfull',0,'Shared Connect')
else
messagebox('Connection to server Failed',0,'Shared Connect')
endif


nReturn = WNetCancelConnection(cDriveLetter,lNoForce)


** if failed you could process the nReturn in a case on the error codes
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top