Solved

WNetGetConnection??

Posted on 1997-12-23
3
763 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…

863 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

18 Experts available now in Live!

Get 1:1 Help Now