Win32 API 日本語リファレンス
ホームNetworkManagement.WNet › WNetAddConnection3W

WNetAddConnection3W

関数
所有ウィンドウを指定してネットワークリソースに接続する(Unicode版)。
DLLMPR.dll文字セットUnicode (-W)呼出規約winapi対応OSWindows 2000 以降

シグネチャ

// MPR.dll  (Unicode / -W)
#include <windows.h>

WIN32_ERROR WNetAddConnection3W(
    HWND hwndOwner,   // optional
    NETRESOURCEW* lpNetResource,
    LPCWSTR lpPassword,   // optional
    LPCWSTR lpUserName,   // optional
    NET_CONNECT_FLAGS dwFlags
);

パラメーター

名前方向
hwndOwnerHWNDinoptional
lpNetResourceNETRESOURCEW*in
lpPasswordLPCWSTRinoptional
lpUserNameLPCWSTRinoptional
dwFlagsNET_CONNECT_FLAGSin

戻り値の型: WIN32_ERROR

各言語での呼び出し定義

// MPR.dll  (Unicode / -W)
#include <windows.h>

WIN32_ERROR WNetAddConnection3W(
    HWND hwndOwner,   // optional
    NETRESOURCEW* lpNetResource,
    LPCWSTR lpPassword,   // optional
    LPCWSTR lpUserName,   // optional
    NET_CONNECT_FLAGS dwFlags
);
[DllImport("MPR.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern uint WNetAddConnection3W(
    IntPtr hwndOwner,   // HWND optional
    IntPtr lpNetResource,   // NETRESOURCEW*
    [MarshalAs(UnmanagedType.LPWStr)] string lpPassword,   // LPCWSTR optional
    [MarshalAs(UnmanagedType.LPWStr)] string lpUserName,   // LPCWSTR optional
    uint dwFlags   // NET_CONNECT_FLAGS
);
<DllImport("MPR.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function WNetAddConnection3W(
    hwndOwner As IntPtr,   ' HWND optional
    lpNetResource As IntPtr,   ' NETRESOURCEW*
    <MarshalAs(UnmanagedType.LPWStr)> lpPassword As String,   ' LPCWSTR optional
    <MarshalAs(UnmanagedType.LPWStr)> lpUserName As String,   ' LPCWSTR optional
    dwFlags As UInteger   ' NET_CONNECT_FLAGS
) As UInteger
End Function
' hwndOwner : HWND optional
' lpNetResource : NETRESOURCEW*
' lpPassword : LPCWSTR optional
' lpUserName : LPCWSTR optional
' dwFlags : NET_CONNECT_FLAGS
Declare PtrSafe Function WNetAddConnection3W Lib "mpr" ( _
    ByVal hwndOwner As LongPtr, _
    ByVal lpNetResource As LongPtr, _
    ByVal lpPassword As LongPtr, _
    ByVal lpUserName As LongPtr, _
    ByVal dwFlags As Long) As Long
' Unicode(W): 文字列は ByVal As LongPtr とし StrPtr(unicodeStr) を渡す
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

WNetAddConnection3W = ctypes.windll.mpr.WNetAddConnection3W
WNetAddConnection3W.restype = wintypes.DWORD
WNetAddConnection3W.argtypes = [
    wintypes.HANDLE,  # hwndOwner : HWND optional
    ctypes.c_void_p,  # lpNetResource : NETRESOURCEW*
    wintypes.LPCWSTR,  # lpPassword : LPCWSTR optional
    wintypes.LPCWSTR,  # lpUserName : LPCWSTR optional
    wintypes.DWORD,  # dwFlags : NET_CONNECT_FLAGS
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('MPR.dll')
WNetAddConnection3W = Fiddle::Function.new(
  lib['WNetAddConnection3W'],
  [
    Fiddle::TYPE_VOIDP,  # hwndOwner : HWND optional
    Fiddle::TYPE_VOIDP,  # lpNetResource : NETRESOURCEW*
    Fiddle::TYPE_VOIDP,  # lpPassword : LPCWSTR optional
    Fiddle::TYPE_VOIDP,  # lpUserName : LPCWSTR optional
    -Fiddle::TYPE_INT,  # dwFlags : NET_CONNECT_FLAGS
  ],
  -Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "mpr")]
extern "system" {
    fn WNetAddConnection3W(
        hwndOwner: *mut core::ffi::c_void,  // HWND optional
        lpNetResource: *mut NETRESOURCEW,  // NETRESOURCEW*
        lpPassword: *const u16,  // LPCWSTR optional
        lpUserName: *const u16,  // LPCWSTR optional
        dwFlags: u32  // NET_CONNECT_FLAGS
    ) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("MPR.dll", CharSet = CharSet.Unicode)]
public static extern uint WNetAddConnection3W(IntPtr hwndOwner, IntPtr lpNetResource, [MarshalAs(UnmanagedType.LPWStr)] string lpPassword, [MarshalAs(UnmanagedType.LPWStr)] string lpUserName, uint dwFlags);
"@
$api = Add-Type -MemberDefinition $sig -Name 'MPR_WNetAddConnection3W' -Namespace Win32 -PassThru
# $api::WNetAddConnection3W(hwndOwner, lpNetResource, lpPassword, lpUserName, dwFlags)
#uselib "MPR.dll"
#func global WNetAddConnection3W "WNetAddConnection3W" wptr, wptr, wptr, wptr, wptr
; WNetAddConnection3W hwndOwner, varptr(lpNetResource), lpPassword, lpUserName, dwFlags   ; 戻り値は stat
; hwndOwner : HWND optional -> "wptr"
; lpNetResource : NETRESOURCEW* -> "wptr"
; lpPassword : LPCWSTR optional -> "wptr"
; lpUserName : LPCWSTR optional -> "wptr"
; dwFlags : NET_CONNECT_FLAGS -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "MPR.dll"
#cfunc global WNetAddConnection3W "WNetAddConnection3W" sptr, var, wstr, wstr, int
; res = WNetAddConnection3W(hwndOwner, lpNetResource, lpPassword, lpUserName, dwFlags)
; hwndOwner : HWND optional -> "sptr"
; lpNetResource : NETRESOURCEW* -> "var"
; lpPassword : LPCWSTR optional -> "wstr"
; lpUserName : LPCWSTR optional -> "wstr"
; dwFlags : NET_CONNECT_FLAGS -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; WIN32_ERROR WNetAddConnection3W(HWND hwndOwner, NETRESOURCEW* lpNetResource, LPCWSTR lpPassword, LPCWSTR lpUserName, NET_CONNECT_FLAGS dwFlags)
#uselib "MPR.dll"
#cfunc global WNetAddConnection3W "WNetAddConnection3W" intptr, var, wstr, wstr, int
; res = WNetAddConnection3W(hwndOwner, lpNetResource, lpPassword, lpUserName, dwFlags)
; hwndOwner : HWND optional -> "intptr"
; lpNetResource : NETRESOURCEW* -> "var"
; lpPassword : LPCWSTR optional -> "wstr"
; lpUserName : LPCWSTR optional -> "wstr"
; dwFlags : NET_CONNECT_FLAGS -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	mpr = windows.NewLazySystemDLL("MPR.dll")
	procWNetAddConnection3W = mpr.NewProc("WNetAddConnection3W")
)

// hwndOwner (HWND optional), lpNetResource (NETRESOURCEW*), lpPassword (LPCWSTR optional), lpUserName (LPCWSTR optional), dwFlags (NET_CONNECT_FLAGS)
r1, _, err := procWNetAddConnection3W.Call(
	uintptr(hwndOwner),
	uintptr(lpNetResource),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpPassword))),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpUserName))),
	uintptr(dwFlags),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // WIN32_ERROR
function WNetAddConnection3W(
  hwndOwner: THandle;   // HWND optional
  lpNetResource: Pointer;   // NETRESOURCEW*
  lpPassword: PWideChar;   // LPCWSTR optional
  lpUserName: PWideChar;   // LPCWSTR optional
  dwFlags: DWORD   // NET_CONNECT_FLAGS
): DWORD; stdcall;
  external 'MPR.dll' name 'WNetAddConnection3W';
result := DllCall("MPR\WNetAddConnection3W"
    , "Ptr", hwndOwner   ; HWND optional
    , "Ptr", lpNetResource   ; NETRESOURCEW*
    , "WStr", lpPassword   ; LPCWSTR optional
    , "WStr", lpUserName   ; LPCWSTR optional
    , "UInt", dwFlags   ; NET_CONNECT_FLAGS
    , "UInt")   ; return: WIN32_ERROR
●WNetAddConnection3W(hwndOwner, lpNetResource, lpPassword, lpUserName, dwFlags) = DLL("MPR.dll", "dword WNetAddConnection3W(void*, void*, char*, char*, dword)")
# 呼び出し: WNetAddConnection3W(hwndOwner, lpNetResource, lpPassword, lpUserName, dwFlags)
# hwndOwner : HWND optional -> "void*"
# lpNetResource : NETRESOURCEW* -> "void*"
# lpPassword : LPCWSTR optional -> "char*"
# lpUserName : LPCWSTR optional -> "char*"
# dwFlags : NET_CONNECT_FLAGS -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。