Solved

WNetGetConnection??

Posted on 1997-12-23
3
760 Views
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.
0
Comment
Question by:LostOne
  • 2
3 Comments
 
LVL 8

Accepted Solution

by:
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:

'\\TheServer\C

'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
Const ERROR_BAD_NET_NAME = 67
Const ERROR_LOGON_FAILURE = 1326

Private Sub Command1_Click()

   If ConnectResource(ServerResource, Password, MapToDrive) Then
      Drive1.Refresh
   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
   Else
      MsgBox "Connection Error: " & RC
   End If

End Function

0
 

Author Comment

by:LostOne
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.
0
 
LVL 8

Expert Comment

by:mrmick
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 ERROR_MORE_DATA = 234
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.
 Do
  strRes = Space(ResLen)
  RC = WNetGetConnection(strDrv & Chr$(0), strRes, ResLen)
 Loop Until RC <> ERROR_MORE_DATA

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

End Function

0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now