Top > 気まぐれ日記 > 2006-04-05

WNetAddConnection3 *

昔はよくつかったけど、最近はさっぱりなWNetAddConnection3
VBA系の某所でパス入力ダイアログの出し方の話題があったので、手短に書いてみる。

Private Type NETRESOURCE
    dwScope As Long
    dwType As Long
    dwDisplayType As Long
    dwUsage As Long
    lpLocalName As String
    lpRemoteName As String
    lpComment As String
    lpProvider As String
End Type
Private Declare Function WNetAddConnection3 Lib "mpr.dll" Alias "WNetAddConnection3A" _
    (ByVal hWndOwner As Long, lpNetResource As NETRESOURCE, _
     ByVal lpPassword As String, ByVal lpUserName As String, ByVal dwFlags As Long) As Long
Private Const RESOURCETYPE_ANY = 0&
Private Const CONNECT_INTERACTIVE = &H8

Sub hoge()
    Dim udtResource As NETRESOURCE
    Dim lngRet As Long
    With udtResource
        .dwType = RESOURCETYPE_ANY
        .lpLocalName = vbNullString
        .lpRemoteName = "\\hoge"
        .lpProvider = vbNullString
    End With
    lngRet = WNetAddConnection3(0&, udtResource, vbNullString, vbNullString, CONNECT_INTERACTIVE)
    If lngRet = 0 Then
        ' 接続成功
    End If
End Sub

実際は、CONNECT_INTERACTIVEを指定しているのでhWndOwnerの指定を忘れずに。
そういえばVB+WNetAddConnection3でCONNECT_INTERACTIVEを使用したサンプルはあまり見かけない。

[出張]熊本へ *

急遽熊本へ