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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Introduction While answering a recent question ( in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month9 days, 17 hours left to enroll

623 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