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

WNetAddConnect2 ERROR_BAD_DEVICE

Status
Not open for further replies.

Xerus

Programmer
Jan 30, 2007
6
US
I'm new to using windows api's via vb.net.

I'm trying to dynamically create a network connection using WNetAddConnection2 and I keep getting ERROR_BAD_DEVICE.

MSDN states that this error means "The value specified by lpLocalName is invalid."

Any thoughts on how to resolve this error would be appreciated (thank you's in advance)

I'm initializing the lpLocalName as follows:

Code:
NetR.lpLocalName = "Y:"

where NetR is a NETRESOURCE

I've defined the NETRESOURCE structure as follows:

Code:
<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

My api functions are defined as follows:

Code:
Public Declare Auto Function WNetAddConnection2 Lib "mpr.dll" Alias "WNetAddConnection2A" ( _
          ByVal lpNetResource As IntPtr, _
          ByVal lpPassword As String, _
          ByVal lpUserName As String, _
          ByVal dwFlags As Integer) _
          As Integer

    Public Declare Auto Function WNetCancelConnection2 Lib "mpr.dll" Alias "WNetCancelConnection2A" ( _
      ByVal lpName As IntPtr, _
      ByVal dwFlags As Integer, _
      ByVal fForce As Boolean) _
      As Integer

----------
Sam

"Part of the inhumanity of the computer is that, once it is competently programmed and working smoothly, it is completely honest."

- Isaac Asimov
 
An Ip or localname normally means an IPAddress(127.0.0.1) or a PC Name (LocalHost)
 
Thanks for the feedback.

I resolved the issue by removing the marshalling statements from the structure.

I can now connect to a network.



----------
Sam

"Part of the inhumanity of the computer is that, once it is competently programmed and working smoothly, it is completely honest."

- Isaac Asimov
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top