Posted on 1997-12-23
Last Modified: 2008-02-01
Declare Function WNetGetConnection Lib "mpr.dll" Alias "WNetGetConnectionA" (ByVal lpszLocalName As String, ByVal lpszRemoteName As String, cbRemoteName As Long) As Long

could someone please give me an example of how to get this to work.
Question by:LostOne
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Accepted Solution

mrmick earned 100 total points
ID: 1447026
'Create a new project, Add a CommandButton and a DriveListBox
'Use Windows Explorer verify the ServerName and ResourceName...
'- go down to Network Neighborhood and open the tree...
'- open the server tree to view mapable resources...

'If, for instance, the Server Name is "TheServer"...
'and under the "TheServer" tree you wanted to map to "C"...
'The resource name would be:


'Replace the following for this example:
Const ServerResource = "\\TheServer\C"
Const Password = "YourPassword"
Const MapToDrive = "X:"

Private Declare Function WNetAddConnection Lib "mpr.dll" Alias "WNetAddConnectionA" (ByVal lpszNetPath As String, ByVal lpszPassword As String, ByVal lpszLocalName As String) As Long

Const WN_Success = &H0
Const WN_Not_Supported = &H1
Const WN_Net_Error = &H2
Const WN_Bad_Pointer = &H4
Const WN_Bad_NetName = &H32
Const WN_Bad_Password = &H6
Const WN_Bad_Localname = &H33
Const WN_Access_Denied = &H7
Const WN_Out_Of_Memory = &HB
Const WN_Already_Connected = &H34

Private Sub Command1_Click()

   If ConnectResource(ServerResource, Password, MapToDrive) Then
   End If

End Sub

Function ConnectResource(strRes$, strPass, strDrive) As Boolean
Dim RC As Long

   RC = WNetAddConnection(UCase(strRes) & Chr$(0), strPass & Chr$(0), strDrive & Chr$(0))
   If RC = WN_Success Then
      ConnectResource = True
      MsgBox "Connection Error: " & RC
   End If

End Function


Author Comment

ID: 1447027
Actually I was looking for WNetGetConnection which I thought would give me the NetResource from a drive letter. Sorry if I didn't make that clear.

Expert Comment

ID: 1447028
My fault, I misread it.  Create a new project and add a single command button to Form1.  Paste code below into the From1 general declarations section.  Run the project, click the button and watch the immediate window for the result.

Note: Replace "K" in the Command1_Click event with the local drive for which you wish to retrieve the associated network resource name.

Private Declare Function WNetGetConnection Lib "mpr.dll" Alias "WNetGetConnectionA" (ByVal lpszLocalName As String, ByVal lpszRemoteName As String, cbRemoteName As Long) As Long

Const NO_ERROR = 0

Private Sub Command1_Click()

 Debug.Print GetResource("K:")

End Sub
Function GetResource(strDrv$) As String

 Dim strRes$, ResLen&, RC&

 ResLen = 32 'Buffersize - will be enlarged if necessary.
  strRes = Space(ResLen)
  RC = WNetGetConnection(strDrv & Chr$(0), strRes, ResLen)

 If RC = NO_ERROR Then
  GetResource = Left(strRes, ResLen)
  MsgBox "Connection Error: " & RC
 End If

End Function


Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question