Using Wnetcancelconnection and wnetaddconnection

Windows specific forum
trather
User
User
Posts: 29
Joined: Sun Nov 07, 2004 9:57 pm

Using Wnetcancelconnection and wnetaddconnection

Post 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
 
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Post 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")
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

Post by NoahPhense »

Very nice.. what about specifying a userID ?

- np
trather
User
User
Posts: 29
Joined: Sun Nov 07, 2004 9:57 pm

Post 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. :D
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Post 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)
trather
User
User
Posts: 29
Joined: Sun Nov 07, 2004 9:57 pm

Post by trather »

Looks and works great. I understand so much more know. Thank you. :P :P :P
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

Post by NoahPhense »

Nice code Droopy! Where did you find the structure.. msdn ?

- np
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Post by Droopy »

The structure is in Msdn / also in the Win32.hlp
dell_jockey
Enthusiast
Enthusiast
Posts: 767
Joined: Sat Jan 24, 2004 6:56 pm

Post 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!
cheers,
dell_jockey
________
http://blog.forex-trading-ideas.com
uweb
User
User
Posts: 98
Joined: Wed Mar 15, 2006 9:40 am
Location: Germany

Post 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.
Please pardon my English, my native tongue is German.
Post Reply