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

WNetAddConnection2 error 67 ??

Status
Not open for further replies.

turtlemaster

Programmer
Oct 4, 2001
93
0
0
US
I am new to .NET from VB6 and I am having trouble accessing a network resource. I have successfully used this API in VB6 but now I keep getting error 67 ERROR_BAD_NET_NAME. I have checked and double checked what I am passing to the api function but it still is not working. What am I missing??

This API requires a NETRESOURCE stucture (defined below)

<StructLayout(LayoutKind.Sequential, Pack:=1)> Public Structure NETRESOURCE
Public dwScope As Integer
Public dwType As Integer
Public dwDisplayType As Integer
Public dwUsage As Integer
<MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPTStr)> Public lpLocalName As String
<MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPTStr)> Public lpRemoteName As String
<MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPTStr)> Public lpComment As String
<MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPTStr)> Public lpProvider As String
End Structure

I then delclare the external subroutine from the DLL by

Public Declare Ansi Function WNetAddConnection2 Lib &quot;mpr.dll&quot; Alias &quot;WNetAddConnection2A&quot; (ByVal lpNetResource As IntPtr, ByVal lpPassword As String, ByVal lpUserName As String, ByVal dwFlags As Integer) As Integer

I created a function to make the API call as follows

Public Function ConnectThisNetworkDrive(ByVal sServer As String, _
ByVal sDrv As String, ByVal sUsername As String, ByVal sPassword As String) As Long

'attempts to connect to the passed network
'connection to the specified drive.
'ErrInfo=ERROR_SUCCESS if successful.

Dim NETR As New NETRESOURCE()
Dim errInfo As Long
' This is a point that hold the marshalled object on the stack.
Dim iptr As IntPtr
Dim SizePerStruct As Integer

With NETR
.dwScope = RESOURCE_GLOBALNET
.dwType = RESOURCETYPE_DISK
.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
.dwUsage = RESOURCEUSAGE_CONNECTABLE
.lpRemoteName = sServer
.lpLocalName = sDrv
End With

' In order to Marshal the object to the stack you must first allocate enough room on the stack
' for your structure.
' Find out size of struct
SizePerStruct = Marshal.SizeOf(GetType(NETRESOURCE))
' allocate the space on the stack and set a reference to the empty space to iptr
iptr = Marshal.AllocHGlobal(SizePerStruct)

' This Marshal's the .NET structure (object) on the heap into your structure on the stack
' following marshalling rules set when the structure is defined.
Marshal.StructureToPtr(NETR, iptr, False)

' When the API call is made the pointer to the marshalled object on the stack is made not
' The reference to the .NET object on the heap. The API function cannot use these objects.

errInfo = WNetAddConnection2(iptr, sPassword, sUsername, CONNECT_UPDATE_PROFILE)

' There is nothing I need back from this structure so I do not have to Marshal anything back.
' If required data was written to the structure at this point you would have to marshal it back
' into your object on the heap so you could use it with the CLR

' Eliminate Marshalled struct on Stack
Marshal.FreeHGlobal(iptr)

ConnectThisNetworkDrive = errInfo

End Function

Currently this return an error 67 and I am not sure why. It was returning invalid handle but I think I have fixed that problem.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top