tmighty
tmighty

Reputation: 11389

WNetAddConnection2 raises Access Denied error

I'm trying to connect to a file on local network. This file is shared with Anybody and has Read and Write privileges. I haven't set up any password.

I can access this file within Windows Explorer on my machine, I can read and write it.

However, when I try to connect to the file using the code below, WNetAddConnection2 returns 5, which means "Access denied".

I'm running VS 2017 as an administrator, just make sure that I have enough credentials, but that doesn't change anything.

When I run my code, I don't have this file open in Windows Explorer or so.

Can anybody suggest what I might check next?

Thank you!

Private Sub btnServer_Click(sender As Object, e As EventArgs) Handles btnServer.Click

    Dim nr As New NETRESOURCE
    nr.dwType = RESOURCETYPE_DISK
    nr.lpRemoteName = "\\WIN-AJUUS3V93E5\Users\MyUser\Desktop\vm7share\aa.user.db"

    Dim iRet As UInteger
    iRet = WNetAddConnection2(nr, "", "", 0)
    If iRet <> NO_ERROR Then
        If iRet = 65 Then
            MessageBox.Show("Wrong path!")
        ElseIf iRet = 1219 Then
            MessageBox.Show("Another connection (perhaps in Windows Explorer) already exists. Close that connection first!")
        ElseIf iRet = 5 Then
            MessageBox.Show("Access denied!")
        Else
            Throw New Exception("WNetAddConnection2 failed.")
        End If
    End If

End Sub

Upvotes: 1

Views: 2241

Answers (1)

Soonts
Soonts

Reputation: 21936

Couple problems here.

The 2nd and third arguments for the function are user & password. The documentation says to use currently logged user, you must pass null. You’re passing empty strings instead. Try to replace "" with null and see what happens.

Another one, I think you’re giving too specific remote resource. I don’t think you can WNetAddConnection2 to access an individual file. Specify only the server + share, i.e. "\\WIN-AJUUS3V93E5\Users" If this will work OK, try with the directory, i.e. "\\WIN-AJUUS3V93E5\Users\MyUser\Desktop\vm7share\

Upvotes: 1

Related Questions