Link to home
Start Free TrialLog in
Avatar of LostOne
LostOne

asked on

WNetGetConnection??

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.
ASKER CERTIFIED SOLUTION
Avatar of mrmick
mrmick

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of LostOne
LostOne

ASKER

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.
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