Results 1 to 8 of 8

Thread: [2005] DRIVE MAPPING PAssing the User ID. (using WNETAddConnection Function )

  1. #1

    Thread Starter
    Lively Member smilbuta's Avatar
    Join Date
    Apr 2005
    Location
    Orlando
    Posts
    104

    [2005] DRIVE MAPPING PAssing the User ID. (using WNETAddConnection Function )

    Hello community,

    Im connecting to a mapped drive via a smal app im trying to write.
    Im using the WNETAddConnection Function but i noticed it only takes 3 parameters.

    I need to pass it my user ID since the Authentication on the server im connectiong to uses a different USer ID for me instead of my systems ID.

    Is there a parameter im missing or am i using the correct function??

    Code:
    Private Declare Function WNetAddConnection Lib "mpr.dll" Alias "WNetAddConnectionA" (ByVal lpszNetPath As String, ByVal lpszPassword As String, ByVal lpszLocalName As String) As Long)
    VB.Net uber-noob since04, Yeababy!

  2. #2
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    Re: [2005] DRIVE MAPPING PAssing the User ID. (using WNETAddConnection Function )

    Look at the wnetaddconnection2 function

    http://msdn2.microsoft.com/en-us/library/aa385413.aspx

  3. #3

    Thread Starter
    Lively Member smilbuta's Avatar
    Join Date
    Apr 2005
    Location
    Orlando
    Posts
    104

    Re: [2005] DRIVE MAPPING PAssing the User ID. (using WNETAddConnection Function )

    OK thanks , i see the description so now im building the app but i have another question

    I see the new Methods for WNET..2 , the first parm LpNetResource is a structure.

    DO i have to populate all of the entrys within that structure or can i just populate the path and letter?
    VB.Net uber-noob since04, Yeababy!

  4. #4
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    Re: [2005] DRIVE MAPPING PAssing the User ID. (using WNETAddConnection Function )

    Code:
        Public Const ForceDisconnect As Integer = 1
        Public Const RESOURCETYPE_DISK As Long = &H1
    
    Public Structure NETRESOURCE
            Public dwScope As Integer
            Public dwType As Integer
            Public dwDisplayType As Integer
            Public dwUsage As Integer
            Public lpLocalName As String
            Public lpRemoteName As String
            Public lpComment As String
            Public lpProvider As String
        End Structure
    
    
    Dim nr As NETRESOURCE
    
     nr = New NETRESOURCE
            nr.lpRemoteName = UNCPath
            nr.lpLocalName = DriveLetter & ":"
            strUsername = Nothing '(add parameters to pass this if necessary)
            strPassword = Nothing '(add parameters to pass this if necessary)
            nr.dwType = RESOURCETYPE_DISK
    
     Try
                result = WNetAddConnection2(nr, strPassword, strUsername, 0)
            Catch ex As Exception
                MessageBox.Show("Error in MapDrive: " & vbCrLf & ex.Message.ToString)
            End Try

  5. #5

    Thread Starter
    Lively Member smilbuta's Avatar
    Join Date
    Apr 2005
    Location
    Orlando
    Posts
    104

    Re: [2005] DRIVE MAPPING PAssing the User ID. (using WNETAddConnection Function )

    This is my code, I have looked over it and found no eerors form what everyone has helped with.

    My environment is a XPpro system connecting to a Oracle server(unsure of OS nx or ms).

    I get no error and my message box returns false as it should since i have not connected. I triple verified my pw and id's to make sure they were correct by watching the values as the app debuged.
    any advice would be welcome.

    Code:
    Public Class Form1
    
        Public Declare Function WNetAddConnection2 Lib "mpr.dll" Alias "WNetAddConnection2A" _
      (ByRef lpNetResource As NETRESOURCE, ByVal lpPassword As String, ByVal lpUserName As String, ByVal dwFlags As Integer) As Integer
    
        Public Declare Function WNetCancelConnection2 Lib "mpr" Alias "WNetCancelConnection2A" _
      (ByVal lpName As String, ByVal dwFlags As Integer, ByVal fForce As Integer) As Integer
    
        Public Structure NETRESOURCE
            Public dwScope As Integer
            Public dwType As Integer
            Public dwDisplayType As Integer
            Public dwUsage As Integer
            Public lpLocalName As String
            Public lpRemoteName As String
            Public lpComment As String
            Public lpProvider As String
        End Structure
    
        Public Const ForceDisconnect As Integer = 1
        Public Const RESOURCETYPE_DISK As Long = &H1
    
        Public Function MapDrive(ByVal DriveLetter As String, ByVal UNCPath As String, ByVal uName As String, ByVal pWord As String) As Boolean
    
            Dim nr As NETRESOURCE
            Dim strUsername As String
            Dim strPassword As String
            nr = New NETRESOURCE
            nr.lpRemoteName = UNCPath
            nr.lpLocalName = DriveLetter & ":"
            strUsername = uName
            strPassword = pWord
            nr.dwType = RESOURCETYPE_DISK
            Dim result As Integer
            Try
                result = WNetAddConnection2(nr, strPassword, strUsername, 0)
            Catch ex As Exception
                MessageBox.Show("error: " & vbCrLf & ex.Message.ToString)
            End Try
    
            If result = 0 Then
                Return True
            Else
                Return False
            End If
        End Function
        Public Function UnMapDrive(ByVal DriveLetter As String) As Boolean
            Dim rc As Integer
            rc = WNetCancelConnection2(DriveLetter & ":", 0, ForceDisconnect)
            If rc = 0 Then
                Return True
            Else
                Return False
            End If
        End Function
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim user As String
            Dim passWrd As String
            Dim driveLtr As String
            Dim pathName As String
            user = "YaddaYadda"
            passWrd = "YaddaYadda"
            driveLtr = "Y"
            pathName = "\\Yadda.Yadda\Folder"
            If MapDrive(driveLtr, pathName, user, passWrd) = True Then
                MessageBox.Show("good")
            Else
                MessageBox.Show("false")
            End If
        End Sub
    End Class
    VB.Net uber-noob since04, Yeababy!

  6. #6
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2005] DRIVE MAPPING PAssing the User ID. (using WNETAddConnection Function )

    You should note that if a system is already has a mapped drive using user A credentials, you can not map another drive using different credentials... You should try this manually and you'll see what I'm taking about.

  7. #7

    Thread Starter
    Lively Member smilbuta's Avatar
    Join Date
    Apr 2005
    Location
    Orlando
    Posts
    104

    Re: [2005] DRIVE MAPPING PAssing the User ID. (using WNETAddConnection Function )

    OK,

    Ill explain my setup. Currently my org is logged into a domain under the following format.

    Username[at]sub3.sub2.sub1
    and all of our drives our mapped under this context.


    the drive i'm trying to reach is :
    Username[at]sub2.sub1

    I can in fact connect to this drive manualy by typing in the information and User and PW info but when i try to use WNET..2 it fails and also fails with WNET..

    The domain is under Novell if im not mistaken, might this have an issue to do with it?
    VB.Net uber-noob since04, Yeababy!

  8. #8
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2005] DRIVE MAPPING PAssing the User ID. (using WNETAddConnection Function )

    Try to map a drive using NET USE command from command prompt. If you succeed, simply write a function to map your drive using a process to start net use....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width