Click to See Complete Forum and Search --> : WNetAddConnection2 problem


Lars_V_J
December 2nd, 2008, 07:33 AM
I try to map a network drive in VB using WNetAddConnection2. I've done it in C++ with no problems, but it won't work in VB. I get an error 487 (Unknown error, I think)


#Region "External_stuff"

Private Const NO_ERROR As Long = 0
Private Const CONNECT_UPDATE_PROFILE As Long = &H1
Private Const RESOURCETYPE_DISK As Long = &H1
Private Const RESOURCE_GLOBALNET As Long = &H2
Private Const RESOURCEDISPLAYTYPE_SHARE As Long = &H3
Private Const RESOURCEUSAGE_CONNECTABLE As Long = &H1
Private Const RESOURCETYPE_ANY As Long = &H0

Private Const SW_SHOWNORMAL As Long = 1

Private Structure NETRESOURCE
Public dwScope As Long
Public dwType As Long
Public dwDisplayType As Long
Public dwUsage As Long
Public lpLocalName As String
Public lpRemoteName As String
Public lpComment As String
Public lpProvider As String
End Structure


Private Declare Function WNetAddConnection2 Lib "mpr.dll" _
Alias "WNetAddConnection2A" _
(ByVal lpNetResource As NETRESOURCE, _
ByVal lpPassword As String, _
ByVal lpUserName As String, _
ByVal dwFlags As Long) As Long

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

Private Declare Function ShellExecute Lib "shell32.dll" _
Alias "ShellExecuteA" _
(ByVal hwnd As Long, _
ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long

#End Region

Private Function ConnectNetDrive(ByVal serverpath As String, ByVal driveletter As String) As Boolean

'attempts to connect to the passed network
'connection to the specified drive.
'ErrInfo=NO_ERROR if sucessful.

Dim NETR As NETRESOURCE
Dim errInfo As Long

NETR = New NETRESOURCE


With NETR
.dwScope = RESOURCE_GLOBALNET
.dwType = RESOURCETYPE_DISK
.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
.dwUsage = RESOURCEUSAGE_CONNECTABLE
.lpRemoteName = serverpath
.lpLocalName = driveletter
.lpProvider = vbNullString
End With

errInfo = WNetAddConnection2(NETR, TextBox4.Text, TextBox3.Text, 0) 'CONNECT_UPDATE_PROFILE)
'vbNullString, "birchr", CONNECT_UPDATE_PROFILE)
TextBox5.Text = errInfo.ToString()


ConnectNetDrive = errInfo = NO_ERROR

End Function



Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
ConnectNetDrive(TextBox2.Text, TextBox1.Text)

End Sub




Any ideas?

Thank you in advance

George1111
December 2nd, 2008, 08:34 AM
How does this work ?

(I cant even key it into a VB6 Program)

#Region "External_stuff"

Lars_V_J
December 2nd, 2008, 08:42 AM
Hm, actually it is VB .NET 2003 - but I hoped it would be sort of similar. I hope I didn't drop this one in the wrong forum...

WizBang
December 4th, 2008, 04:06 AM
Seems to me, that a Long in VB6 is an Integer in .net.