troubleshooting Question

Add network share

Avatar of Khel4Me
Khel4MeFlag for India asked on
Windows Server 2003Visual Basic.NET
3 Comments1 Solution245 ViewsLast Modified:
Hi Experts,
I want to add network share so that it can be available for use and I am using following code but it is not working. I search a lot but could not fould anything helpful.

Please guide...
Private Declare Function WNetAddConnection2 Lib "mpr.dll" Alias "WNetAddConnection2A" (ByRef lpNetResource As NETRESOURCE, ByVal lpPassword As String, ByVal lpUserName As String, ByVal dwFlags As Long) As Long
        Private Declare Function WNetCancelConnection2 Lib "mpr.dll" Alias "WNetCancelConnection2A" (ByVal lpName As String, ByVal dwFlags As Long, ByVal fForce As Long) As Long
        Public Const RESOURCETYPE_DISK = &H1
        Public Const RESOURCETYPE_ANY = &H0
        Public Const RESOURCE_GLOBALNET = &H2
        Public Const RESOURCEDISPLAYTYPE_SHARE = &H3
        Public Const RESOURCEUSAGE_CONNECTABLE = &H1
        Public Const CONNECT_UPDATE_PROFILE = &H1
 
        Private 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 Enum ErrorType As UInteger
            ERROR_ACCESS_DENIED = 5&
            ERROR_ALREADY_ASSIGNED = 85&
            ERROR_BAD_DEV_TYPE = 66&
            ERROR_BAD_DEVICE = 1200&
            ERROR_BAD_NET_NAME = 67&
            ERROR_BAD_PROFILE = 1206&
            ERROR_BAD_PROVIDER = 1204&
            ERROR_BUSY = 170&
            ERROR_CANCELLED = 1223&
            ERROR_CANNOT_OPEN_PROFILE = 1205&
            ERROR_DEVICE_ALREADY_REMEMBERED = 1202&
            ERROR_EXTENDED_ERROR = 1208&
            ERROR_INVALID_PASSWORD = 86&
            ERROR_NO_NET_OR_BAD_PATH = 1203&
        End Enum
 
Private Sub AddShare()
            Dim nr As NETRESOURCE = Nothing
            Dim l As Long
 
            With nr
                .dwType = RESOURCETYPE_ANY 'RESOURCETYPE_DISK
                .lpLocalName = ""
                .dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
                .lpRemoteName = "\\MyServerIP\MyShare"
                .dwUsage = RESOURCEUSAGE_CONNECTABLE
                '.lpProvider = ""
                .dwScope = RESOURCE_GLOBALNET
            End With
 
                l = WNetAddConnection2(nr, ConOption.Password, ConOption.Username, CONNECT_UPDATE_PROFILE)
End Sub
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 3 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros