首页->技巧->网络->详细内容
Get UNC Path From a Mapped Network Share's Drive Letter

PRODUCT :Microsoft Visual Basic for Windows
PROD/VER:WINDOWS:4.0,5.0,6.0
OPER/SYS:WINDOWS
KEYWORDS:

======================================================================
---------------------------------------------------------------------
The information in this article applies to:

- Microsoft Visual Basic Learning, Professional, and Enterprise Editions
for Windows, version 6.0
- Microsoft Visual Basic Control Creation, Learning, Professional, and
Enterprise Editions for Windows, version 5.0
- Microsoft Visual Basic Standard, Professional, and Enterprise Editions,
32-bit only, for Windows, version 4.0
---------------------------------------------------------------------

SUMMARY
=======

Most network client software allows you to refer to network drives through
the use of mapped drive letters or the use of syntax known as UNC notation
(Universal Naming Convention). Use of UNC notation requires that you know
the name of the server and share, while a mapped drive letter only requires
the drive letter once the connection has been established. At times, it may
be necessary to determine the UNC path to a server given only it's mapped
drive letter. This article demonstrates a method for obtaining this
information.

MORE INFORMATION
================

Step-by-Step Example
--------------------

1. Map a drive letter to any available network share and make note of
the letter assigned.

2. Create a new Visual Basic project. Form1 is created by default.

3. Add a CommandButton to Form1.

4. Paste the following code into Form1's code window:

Option Explicit

Private Const RESOURCETYPE_ANY = &H0
Private Const RESOURCE_CONNECTED = &H1

Private Type NETRESOURCE
dwScope As Long
dwType As Long
dwDisplayType As Long
dwUsage As Long
lpLocalName As Long
lpRemoteName As Long
lpComment As Long
lpProvider As Long
End Type

Private Declare Function WNetOpenEnum Lib "mpr.dll" Alias _
"WNetOpenEnumA" (ByVal dwScope As Long, ByVal dwType As Long, _
ByVal dwUsage As Long, lpNetResource As Any, lphEnum As Long) _
As Long

Private Declare Function WNetEnumResource Lib "mpr.dll" Alias _
"WNetEnumResourceA" (ByVal hEnum As Long, lpcCount As Long, _
lpBuffer As Any, lpBufferSize As Long) As Long

Private Declare Function WNetCloseEnum Lib "mpr.dll" ( _
ByVal hEnum As Long) As Long

Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" _
(ByVal lpString As Any) As Long

Private Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" _
(ByVal lpString1 As Any, ByVal lpString2 As Any) As Long

Private Sub Command1_Click()
MsgBox LetterToUNC("F:") ' <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
End Sub

Function LetterToUNC(DriveLetter As String) As String
Dim hEnum As Long
Dim NetInfo(1023) As NETRESOURCE
Dim entries As Long
Dim nStatus As Long
Dim LocalName As String
Dim UNCName As String
Dim i As Long
Dim r As Long

' Begin the enumeration
nStatus = WNetOpenEnum(RESOURCE_CONNECTED, RESOURCETYPE_ANY, _
0&, ByVal 0&, hEnum)

LetterToUNC = "Drive Letter Not Found"

'Check for success from open enum
If ((nStatus = 0) And (hEnum <> 0)) Then
' Set number of entries
entries = 1024

' Enumerate the resource
nStatus = WNetEnumResource(hEnum, entries, NetInfo(0), _
CLng(Len(NetInfo(0))) * 1024)

' Check for success
If nStatus = 0 Then
For i = 0 To entries - 1
' Get the local name
LocalName = ""
If NetInfo(i).lpLocalName <> 0 Then
LocalName = Space(lstrlen(NetInfo(i).lpLocalName) + 1)
r = lstrcpy(LocalName, NetInfo(i).lpLocalName)
End If

' Strip null character from end
If Len(LocalName) <> 0 Then
LocalName = Left(LocalName, (Len(LocalName) - 1))
End If

If UCase$(LocalName) = UCase$(DriveLetter) Then
' Get the remote name
UNCName = ""
If NetInfo(i).lpRemoteName <> 0 Then
UNCName = Space(lstrlen(NetInfo(i).lpRemoteName) _
+ 1)
r = lstrcpy(UNCName, NetInfo(i).lpRemoteName)
End If

' Strip null character from end
If Len(UNCName) <> 0 Then
UNCName = Left(UNCName, (Len(UNCName) _
- 1))
End If

' Return the UNC path to drive
LetterToUNC = UNCName

' Exit the loop
Exit For
End If
Next i
End If
End If

' End enumeration
nStatus = WNetCloseEnum(hEnum)
End Function

5. Change the code in Command1's Click event to refer to the drive letter
created by step 1.

6. Run the sample.

7. Click the CommandButton. The UNC path to the mapped drive is shown in a
message box.
返回

映射网络驱动器

下面的语句可以显示“映射网络驱动器”对话框
Shell "rundll32 user,wnetconnectdialog"
返回

建立包含主题和正文的邮件

我们知道通过调用API函数ShellExecute就可以发送邮件了,但是如何让邮件同时包含主题和正文呢,看下面的代码:
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA"          _ 
    (ByVal hwnd As Long, ByVal lpOperation As String, _ 
    ByVal lpFile As String, ByVal lpParameters As String, _ 
    ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long 

Private Sub Command1_Click() 
    ShellExecute Me.hwnd, "", "mailto:a@c.com?subject=help&body=OK", _ 
        "", "", 0 
    End Sub 

可以看到,上面我们只是利用了一个&就可以将邮件的主题与正文连接了起来。
返回

Windows中一些Inter以及拨号的设定对话框的调用
下面的程序可以调用一些Windows中的网络设定,是什么设定呢?自己试一下吧。

Const MAX_PATH = 260
Const CSIDL_FAVORITES = &H6

Private Declare Function LaunchInternetControlPanel Lib "inetcpl.cpl" _
        (ByVal hwndParent As Long) As Long
Private Declare Function LaunchConnectionDialog Lib "inetcpl.cpl" _
        (ByVal hwndParent As Long) As Long
Private Declare Function LaunchSecurityDialog Lib "inetcpl.cpl" _
        (ByVal hwndParent As Long) As Long
Private Declare Function LaunchSiteCertDialog Lib "inetcpl.cpl" _
        (ByVal hwndParent As Long) As Long
Private Declare Function OpenFontsDialog Lib "inetcpl.cpl" _
        (ByVal hwndParent As Long) As Long
Private Declare Function DoOrganizeFavDlg Lib "shdocvw.dll" _
        (ByVal hwndParent As Long, ByVal lpszPath As String) As Long
Private Declare Function SHGetSpecialFolderPath Lib "shell32.dll" Alias _
        "SHGetSpecialFolderPathA" (ByVal hwndOwner As Long, _
        ByVal lpszPath As String, ByVal nFolder As Long, _
        ByVal fCreate As Long) As Long

Private Sub Command1_Click()
    Dim rc As Long
   
    rc = LaunchInternetControlPanel(Me.hWnd)
    'Debug.Print GetLastError
    If rc = 0 Then
        MsgBox "LaunchInternetControlPanel failed!", vbExclamation
    End If
End Sub

Private Sub Command2_Click()
    Dim rc As Long
   
    rc = LaunchConnectionDialog(Me.hWnd)
    'Debug.Print GetLastError
    If rc = 0 Then
        MsgBox "LaunchConnectionDialog failed!", vbExclamation
    End If
End Sub

Private Sub Command3_Click()
    Dim rc As Long
    Dim strFavPath As String * MAX_PATH
   
    SHGetSpecialFolderPath Me.hWnd, strFavPath, CSIDL_FAVORITES, False
    rc = DoOrganizeFavDlg(Me.hWnd, strFavPath)
    'Debug.Print GetLastError
    If rc = 0 Then
        MsgBox "DoOrganizeFavDlg failed!", vbExclamation
    End If
End Sub

Private Sub Command4_Click()
    Dim rc As Long
   
    rc = LaunchSiteCertDialog(Me.hWnd)
    'Debug.Print GetLastError
    If rc = 0 Then
        MsgBox "LaunchSiteCertDialog failed!", vbExclamation
    End If
End Sub
返回

Back to top