Page 1 of 1
Using Wnetcancelconnection and wnetaddconnection
Posted: Tue Apr 19, 2005 6:30 am
by trather
Here is some code that tries to use the Wnetaddconnection but I am having trouble getting it to do anything. The code doesn't error out but it also doesn't do anything.
Can you help? Or have a better way to do this?
Code: Select all
#dllMPR = 1
If OpenLibrary(#dllMPR,"MPR.DLL")
maptype.s = "RESOURCETYPE_DISK"
drivelet.s = "H:"
drivepath.s = "\\server"
folder.s = "\test123"
path.s = drivepath + folder
password.s = ""
username.s = ""
core.s = maptype + " " + drivelet + " " + path
profile.s = "CONNECT_UPDATE_PROFILE"
; Disconnect drive letter first just in case
error.l = CallFunction(#dllMPR,"WnetCancelConnection2",drivelet ,profile ,1)
; map Drive letter to corrct place
dummy.l = CallFunction(#dllMPR,"WNetAddConnection2",core, password,username, profile)
Else
MessageRequester("Info","MPR.DLL not found",0)
EndIf
MessageRequester("Worrking", "Finished correcly",0)
End
Posted: Tue Apr 19, 2005 8:48 pm
by Droopy
Try this
Code: Select all
; PureBasic 3.92
; Droopy 19/04/05
; WNetAddConnection : Map Network Drive
; WNetCancelConnection : Disconnect Network Map Driver
Procedure WNetAddConnection(ShareName.s,Password.s,DriveLetter.s)
; ShareName : Ex \\ComputerName\SharePoint
; Password & Driveletter : Ex F:
OpenLibrary(0,"Mpr.dll")
Retour=CallFunction(0,"WNetAddConnectionA",ShareName,Password,DriveLetter)
CloseLibrary(0)
If Retour=0 : Retour=1 : Else : Retour=0 : EndIf
ProcedureReturn Retour
EndProcedure
Procedure WNetCancelConnection(DriveLetter.s,force.l)
; Driveletter : Ex F:
; Force = #True or #False ( Deconnect when file is in use )
OpenLibrary(0,"Mpr.dll")
Retour=CallFunction(0,"WNetCancelConnectionA",DriveLetter,force)
CloseLibrary(0)
If Retour=0 : Retour=1 : Else : Retour=0 : EndIf
ProcedureReturn Retour
EndProcedure
;- Example :
WNetAddConnection("\\passerelle\toto","","s:")
MessageRequester("WNetAddConnection","Drive Mapped")
WNetCancelConnection("s:",#True)
MessageRequester("WNetCancelConnection","Drive UnMapped")
Posted: Wed Apr 20, 2005 3:37 am
by NoahPhense
Very nice.. what about specifying a userID ?
- np
Posted: Wed Apr 20, 2005 5:59 am
by trather
Code looks and works great. But I need to be able to pass unsername and passord. I was looking at WNetAddConnection2 it looks like it will pass the username and password. I am using a system id and password for this path.

Posted: Wed Apr 20, 2005 11:42 pm
by Droopy
Here it is
Code: Select all
;/ PureBasic 3.92
;/ Droopy 20/04/05
;/
;/ WNetAddConnection2 : Map Network Drive with Username / Password
;/ WNetCancelConnection : Disconnect Network Map Drive
Procedure WNetAddConnection2(UserName.s,Password.s,ShareName.s,DriveLetter.s,Persistant.l)
;/ Username / Password
;/ Sharename : \\Server\Share
;/ DriveLetter : F:
;/ Persistant : #True or #False
;/ Return 1 if success or 0 if fail
lpNetResource.NETRESOURCE
lpNetResource\dwtype=1 ; RESOURCETYPE_DISK
lpNetResource\lpLocalName=@DriveLetter
lpNetResource\lpRemoteName=@ShareName
OpenLibrary(0,"Mpr.dll")
Retour=CallFunction(0,"WNetAddConnection2A",lpNetResource,Password,UserName,Persistant)
CloseLibrary(0)
If Retour=0 : Retour=1 : Else : Retour=0 : EndIf
ProcedureReturn Retour
EndProcedure
Procedure WNetCancelConnection(DriveLetter.s,force.l)
;/ Driveletter : Ex F:
;/ Force = #True or #False ( Deconnect when file is in use )
;/ Return 1 if success or 0 if fail
OpenLibrary(0,"Mpr.dll")
Retour=CallFunction(0,"WNetCancelConnectionA",DriveLetter,force)
CloseLibrary(0)
If Retour=0 : Retour=1 : Else : Retour=0 : EndIf
ProcedureReturn Retour
EndProcedure
;- Test
WNetAddConnection2("username","password","\\Server\Share","Z:",#True)
MessageRequester("Network drive mapped","Click to Unmap")
WNetCancelConnection("Z:",#True)
Posted: Thu Apr 21, 2005 3:17 am
by trather
Posted: Thu Apr 21, 2005 3:43 am
by NoahPhense
Nice code Droopy! Where did you find the structure.. msdn ?
- np
Posted: Thu Apr 21, 2005 5:53 am
by Droopy
The structure is in Msdn / also in the Win32.hlp
Posted: Thu Apr 21, 2005 8:59 am
by dell_jockey
Thanks Droopy,
it also works great when connecting to IPC$, which is what I need to remotely check whethers users have somehow managed to revoke administrative rights from us poor administrators...
If a connect to IPC$ is succesful, we have access and administrative rights, if not, we have a client to check and a user to talk to...
Thanks again!
Posted: Fri Mar 09, 2007 9:54 am
by uweb
Hello
does anybody have a idea to this :
Code: Select all
;- Test
;WNetAddConnection2("username","password","\\Server\Share","Z:",#True)
; failed try to connect direct to the FTP-Server
;WNetAddConnection2("a","b","ftp://127.0.0.1/","Z:",#True)
;failed try to use a working connection with the name "127.0.0.1"
;from "My Network Places" ( or like, dont know, in german : "Netzwerkumgebung")
WNetAddConnection2("a","b","127.0.0.1","Z:",#True)
MessageRequester("Network drive mapped","Click to Unmap")
WNetCancelConnection("Z:",#True)
My second question :
How should i implement it so that it starts when a other user (as the actual) connects to my computer (i.e. with a palm) ?
Do i have to write a proxy or can i use it for this case ?
PS
With normal shares it works fine. Only to map a ftp-account is a problem.