Help WNetGetUniversalName ... in vba

J

Joao Rodrigues

Can anyone tell me why this doesn't work ?

Option Explicit
Declare Function WNetGetUniversalName Lib "mpr"
Alias "WNetGetUniversalNameA" (ByVal lpLocalPath As
String, ByVal dwInfoLevel As Long, lpBuffer As UUU,
lpBufferSize As Long) As Long

Type UUU
UU As String
End Type


Public Sub AA()
Dim CC As UUU
Dim TT As Long
Dim kk As Long
kk = WNetGetUniversalName("H:\", 1, CC, TT)
If TT > 0 Then
MsgBox TT
MsgBox CC.UU
End If
kk = WNetGetUniversalName("H:\", 2, CC, TT)
If TT > 0 Then
MsgBox TT
MsgBox CC.UU
End If
End Sub

the problem is that CC always "stay" empty

H: is a share in a server...

what i want to know is the name of the server and share...

Thanks in advance

João Rodrigues
 
R

Rob Bovey

Hi Joao,

Here's a working example of WNetGetUniversalNameA:

Declare Function WNetGetUniversalNameA Lib "mpr" _
(ByVal lpLocalPath As String, _
ByVal dwInfoLevel As Long, _
lpBuffer As Any, _
lpBufferSize As Long) As Long

Public Sub GetUNCPath()

Dim bytBuffer(1 To 256) As Byte
Dim lReturn As Long
Dim szPath As String

lReturn = WNetGetUniversalNameA("H:\", 1, bytBuffer(1), 256)

If lReturn = 0 Then
szPath = StrConv(bytBuffer, vbUnicode)
szPath = Left$(szPath, InStr(szPath, vbNullChar) - 1)
szPath = Right$(szPath, Len(szPath) - 4)
Debug.Print szPath
Else
MsgBox "Error getting UNC path.", vbCritical
End If

End Sub

The WNetGetConnection API is a lot simpler if all you want to get is the
UNC equivalent of a mapped drive:

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

Public Sub GetUNCPath()

Dim lReturn As Long
Dim szBuffer As String

szBuffer = String$(256, vbNullChar)
lReturn = WNetGetConnectionA("Z:", szBuffer, 256)

If lReturn = 0 Then
Debug.Print Left$(szBuffer, InStr(szBuffer, vbNullChar))
Else
MsgBox "Error getting UNC path.", vbCritical
End If

End Sub

--
Rob Bovey, MCSE, MCSD, Excel MVP
Application Professionals
http://www.appspro.com/

* Please post all replies to this newsgroup *
* I delete all unsolicited e-mail responses *


Can anyone tell me why this doesn't work ?

Option Explicit
Declare Function WNetGetUniversalName Lib "mpr"
Alias "WNetGetUniversalNameA" (ByVal lpLocalPath As
String, ByVal dwInfoLevel As Long, lpBuffer As UUU,
lpBufferSize As Long) As Long

Type UUU
UU As String
End Type


Public Sub AA()
Dim CC As UUU
Dim TT As Long
Dim kk As Long
kk = WNetGetUniversalName("H:\", 1, CC, TT)
If TT > 0 Then
MsgBox TT
MsgBox CC.UU
End If
kk = WNetGetUniversalName("H:\", 2, CC, TT)
If TT > 0 Then
MsgBox TT
MsgBox CC.UU
End If
End Sub

the problem is that CC always "stay" empty

H: is a share in a server...

what i want to know is the name of the server and share...

Thanks in advance

João Rodrigues
 
Top