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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to use Wnetaddconnection2 API 1

Status
Not open for further replies.

v5652

Programmer
Mar 19, 2008
76
IN
hi friends,

i want to connect to a folder which is on server.
i have used wnetaddconnection2 api but it couldn't work for me. may be i am not having proper knowledge of it.

but i request that if anybody has been used it successfully can post the sample code of it.

if tried so much times but failed & getting frustated now.
so please help.

thanks.
 
A quick search on this forum for "wnetaddconnection2" brings back 4 threads which may have what you need.

Patrick
 
Nowadays there are easier ways than that API call ... For example:
Code:
[blue]Option Explicit

Public Sub example()
    vbMapDrive "X:", "\\Server\Share"
End Sub

Public Sub vbMapDrive(LocalName As String, RemoteName As String, Optional Persist As Boolean = False, Optional UserName As Variant, Optional Password As Variant)
    CreateObject("WScript.Network").MapNetworkDrive LocalName, RemoteName, Persist, UserName, Password
End Sub[/blue]
 
thanks for the reply sir.

i am giving below the code which i used. if there is any problem in code pl tell me.

thanks again.

i have created a form with two Command Buttons.
and also have a Module.

--------------
'Code in Module1
'--------------------
Declare Function WNetAddConnection2 Lib "mpr.dll" Alias _
"WNetAddConnection2A" (lpNetResource As NETRESOURCE, _
ByVal lpPassword As String, ByVal lpUserName As String, _
ByVal dwFlags As Long) As Long

Declare Function WNetCancelConnection2 Lib "mpr.dll" Alias _
"WNetCancelConnection2A" (ByVal lpName As String, _
ByVal dwFlags As Long, ByVal fForce As Long) As Long

Type NETRESOURCE
dwScope As Long
dwType As Long
dwDisplayType As Long
dwUsage As Long
lpLocalName As String
lpRemoteName As String
lpComment As String
lpProvider As String
End Type

Public Const NO_ERROR = 0
Public Const CONNECT_UPDATE_PROFILE = &H1
' The following includes all the constants defined for NETRESOURCE,
' not just the ones used in this example.
Public Const RESOURCETYPE_DISK = &H1
Public Const RESOURCETYPE_PRINT = &H2
Public Const RESOURCETYPE_ANY = &H0
Public Const RESOURCE_CONNECTED = &H1
Public Const RESOURCE_REMEMBERED = &H3
Public Const RESOURCE_GLOBALNET = &H2
Public Const RESOURCEDISPLAYTYPE_DOMAIN = &H1
Public Const RESOURCEDISPLAYTYPE_GENERIC = &H0
Public Const RESOURCEDISPLAYTYPE_SERVER = &H2
Public Const RESOURCEDISPLAYTYPE_SHARE = &H3
Public Const RESOURCEUSAGE_CONNECTABLE = &H1
Public Const RESOURCEUSAGE_CONTAINER = &H2
' Error Constants:
Public Const ERROR_ACCESS_DENIED = 5&
Public Const ERROR_ALREADY_ASSIGNED = 85&
Public Const ERROR_BAD_DEV_TYPE = 66&
Public Const ERROR_BAD_DEVICE = 1200&
Public Const ERROR_BAD_NET_NAME = 67&
Public Const ERROR_BAD_PROFILE = 1206&
Public Const ERROR_BAD_PROVIDER = 1204&
Public Const ERROR_BUSY = 170&
Public Const ERROR_CANCELLED = 1223&
Public Const ERROR_CANNOT_OPEN_PROFILE = 1205&
Public Const ERROR_DEVICE_ALREADY_REMEMBERED = 1202&
Public Const ERROR_EXTENDED_ERROR = 1208&
Public Const ERROR_INVALID_PASSWORD = 86&
Public Const ERROR_NO_NET_OR_BAD_PATH = 1203&


'--------------------
'Code in form1

'Add two CommandButtons to Form1. These will be Command1 and Command2 by default.
'Add the following code to Form1, substituting a valid share name for "\\ServerName\ShareName":
Option Explicit

Private Sub CmdConnect_Click()
Dim NetR As NETRESOURCE
Dim ErrInfo As Long
Dim MyPass As String, MyUser As String

NetR.dwScope = RESOURCE_GLOBALNET
NetR.dwType = RESOURCETYPE_DISK
NetR.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
NetR.dwUsage = RESOURCEUSAGE_CONNECTABLE
NetR.lpLocalName = "X:" ' If undefined, Connect with no device
NetR.lpRemoteName = "\\sap-server\xyz" ' Your valid share
'NetR.lpComment = "Optional Comment"
'NetR.lpProvider = ' Leave this undefined

' If the MyPass and MyUser arguments are null (use vbNullString), the
' user context for the process provides the default user name.

ErrInfo = WNetAddConnection2(NetR, "admin1", "administrator", CONNECT_UPDATE_PROFILE)

If ErrInfo = NO_ERROR Then
MsgBox "Net Connection Successful!", vbInformation, "Share Connected"
Else
MsgBox "ERROR: " & ErrInfo & " - Net Connection Failed!", vbExclamation, "Share not Connected"
End If
End Sub

Private Sub CmdCancelConnection_Click()
Dim ErrInfo As Long
Dim strLocalName As String

' You may specify either the lpRemoteName or lpLocalName
'strLocalName = "\\ServerName\ShareName"
strLocalName = "X:"

ErrInfo = WNetCancelConnection2(strLocalName, CONNECT_UPDATE_PROFILE, False)

If ErrInfo = NO_ERROR Then
MsgBox "Net Disconnection Successful!", vbInformation, "Share Disconnected"
Else
MsgBox "ERROR: " & ErrInfo & " - Net Disconnection Failed!", vbExclamation, "Share not Disconnected"
End If
End Sub

'----------------End
 
Your sample is just a minor variation of the standard MSDN article on this API call - which works fine ... most of the time.

However, there is some (mostly) undocumented behaviour to the rather ancient WNetAddConnection2 API call - specifically it fails if you already have any connection to any share on the remote sever under a different set of credentials from those that you try to map with the API call.

In your case, for example, if you already have a share on \\sap-server (say under your own credentials), then WNetAddConnection2 with error 1219

This is actually a fundamental behaviour of the underlying network provider - so my solution also suffers the same 'problem', although it produces a much more explicit error message:
[tt]Multiple connections to a server or shared resource by the same user, using more than one user name, are not allowed. Disconnect all previous connections to the server or shared resource and try again.[/tt]

 
thanks strongm for the reply.

i am not having any previous connection on the remote sever. but still it's not working.

but i will check this again.
thanks.
 
>but still it's not working.

In which case:

1) what error message are you getting?
2) have you tried my code instead?


 
hi strongm,

i have tried it again and found that if i will not give username & password it works find but readonly.
but if i give username & password it displays error that connection failed.

so what should i have to do.

 
i am using Windows 2003 server.
i have given,
Full permissions to administrator and
readonly to everyone.

what should i have to change with it?

thanks.
 
>i have tried it again

Have tried what again? Your code, or my code?

If your code, then what error message and number do you get. The number is the important bit.

If my code, then you should get a much more comprehensive error message if it fails


Note that given
i have given,
Full permissions to administrator and
readonly to everyone.
your result that
if i will not give username & password it works find but readonly.
is exactly what I'd expect, whether using my code or your code.
 
sorry sir,
i was out of station from last 4 days.

sorry for the late reply.

the error which i said was with my code.

but i have also tested your code.
and i get the following error.

Runtime Error : -2147023677 (800704c3)

Multiple connection to a server or shared resource by the same user, using more than one username, are not allowed. Disconnect all previous connections to the server or shared resource and try again.

now what should i do.
pl help.

thanks & ragards
vikas
 
>the error which i said was with my code.

I still can't see anywhere where you have quoted the error number; the code you are using has a generic message for failure accompanied by an error code. As far as I can tell you have only mentioned, at best, the generic text where you said "it displays error that connection failed", which is, ny iteslef, essentially useless information

> your code ... i get the following error.

Right - so, as I suggested earlier on, this looks to be because you already have a connection to a share on the target server under another account. Note that this may not be a mapping, but something as simple as having an Explorer window open against the UNC of the share ... example you might simply have browsed to \\sap-server\xyz (or, indeed, \\sap-server\anyothersharename) which means that, as far as Windows is concerned, you have a connection to that server under your own user credentials. Or, perhaps, an application that is accesing data from the server in question. You need to investigate this thoroughly.
 
Yes Sir,

There is already a folder mapped (not "\\sap-server\xyz") to "z:" drive.
i have unmapped that drive and then tested but it gives the same error.

also , there is an ERP software exe which access the database on the server.
but that was also not running at the time when i tested the code.
 
Ok, here is an (ugly) workaround - and it will only work for a single connction); detemine the IP address of the server and connect via that instead of by the name

e.g if the IP address of sap-server is 192.168.1.1 then use:

\\192.168.1.1\xyz

 
thank u thank u thank u so much...strongm

hats to you, sir.

its working fine.

and i am sorry that i couldn't give focus on this thing from last few days.

thanks again for your help.

i will be back soon if i need any more help ragrding this

thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top