Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to use API function WNetAddConnection2 in VB.net?

1,138 views
Skip to first unread message

Li Nan

unread,
Jan 13, 2003, 5:49:30 AM1/13/03
to
It works perfectly when I use API function WNetAddConnection2 in VB6, but it
always return error code 487(ERROR_INVALID_ADDRESS) when I use it in VB.net.
I think the problem occurs at the first parameter. In VB6, it should be
passed using ByRef. But in VB.net, it fails whether I use ByRef or ByVal. In
general, how to deal with a LPSTRUCT parameter in VB.net?

please help, thanks!

vb6:

'in Module1.bas
Public 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

Public 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 CONNECT_INTERACTIVE As Long = &H8
Public Const RESOURCETYPE_DISK As Long = &H1

'in form1.frm
Private Sub Command1_Click()
Dim nr As NETRESOURCE
nr.dwType = RESOURCETYPE_DISK
nr.lpRemoteName = "\\localhost\C"
MsgBox WNetAddConnection2(nr, "", "", CONNECT_INTERACTIVE)
End Sub

--------------------------------------------

vb.net

'in Module1.vb

Module Module1

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

Public 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

Public Const CONNECT_INTERACTIVE As Long = &H8
Public Const RESOURCETYPE_DISK As Long = &H1

End Module

'in form1.vb
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim nr As New NETRESOURCE()
nr.dwType = RESOURCETYPE_DISK
nr.lpRemoteName = "\\localhost\C"
MsgBox(WNetAddConnection2(nr, "", "", CONNECT_INTERACTIVE))
End Sub


Paul Clement

unread,
Jan 13, 2003, 9:21:14 AM1/13/03
to
On Mon, 13 Jan 2003 18:49:30 +0800, "Li Nan" <d...@public.bta.net.cn> wrote:

¤ It works perfectly when I use API function WNetAddConnection2 in VB6, but it


¤ always return error code 487(ERROR_INVALID_ADDRESS) when I use it in VB.net.
¤ I think the problem occurs at the first parameter. In VB6, it should be
¤ passed using ByRef. But in VB.net, it fails whether I use ByRef or ByVal. In
¤ general, how to deal with a LPSTRUCT parameter in VB.net?
¤
¤ please help, thanks!

You will need to change your Long data types to Int32. I would also see the following thread:

http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&th=bff428ddff5c39e&rnum=2


Paul ~~~ pcle...@ameritech.net
Microsoft MVP (Visual Basic)

0 new messages