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

WNetAddConnectionW

関数
ネットワーク上のリソースへの接続を確立する(Unicode版)。
DLLMPR.dll文字セットUnicode (-W)呼出規約winapi対応OSWindows 2000 以降

シグネチャ

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

WIN32_ERROR WNetAddConnectionW(
    LPCWSTR lpRemoteName,
    LPCWSTR lpPassword,   // optional
    LPCWSTR lpLocalName   // optional
);

パラメーター

名前方向
lpRemoteNameLPCWSTRin
lpPasswordLPCWSTRinoptional
lpLocalNameLPCWSTRinoptional

戻り値の型: WIN32_ERROR

各言語での呼び出し定義

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

WIN32_ERROR WNetAddConnectionW(
    LPCWSTR lpRemoteName,
    LPCWSTR lpPassword,   // optional
    LPCWSTR lpLocalName   // optional
);
[DllImport("MPR.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern uint WNetAddConnectionW(
    [MarshalAs(UnmanagedType.LPWStr)] string lpRemoteName,   // LPCWSTR
    [MarshalAs(UnmanagedType.LPWStr)] string lpPassword,   // LPCWSTR optional
    [MarshalAs(UnmanagedType.LPWStr)] string lpLocalName   // LPCWSTR optional
);
<DllImport("MPR.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function WNetAddConnectionW(
    <MarshalAs(UnmanagedType.LPWStr)> lpRemoteName As String,   ' LPCWSTR
    <MarshalAs(UnmanagedType.LPWStr)> lpPassword As String,   ' LPCWSTR optional
    <MarshalAs(UnmanagedType.LPWStr)> lpLocalName As String   ' LPCWSTR optional
) As UInteger
End Function
' lpRemoteName : LPCWSTR
' lpPassword : LPCWSTR optional
' lpLocalName : LPCWSTR optional
Declare PtrSafe Function WNetAddConnectionW Lib "mpr" ( _
    ByVal lpRemoteName As LongPtr, _
    ByVal lpPassword As LongPtr, _
    ByVal lpLocalName As LongPtr) 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

WNetAddConnectionW = ctypes.windll.mpr.WNetAddConnectionW
WNetAddConnectionW.restype = wintypes.DWORD
WNetAddConnectionW.argtypes = [
    wintypes.LPCWSTR,  # lpRemoteName : LPCWSTR
    wintypes.LPCWSTR,  # lpPassword : LPCWSTR optional
    wintypes.LPCWSTR,  # lpLocalName : LPCWSTR optional
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('MPR.dll')
WNetAddConnectionW = Fiddle::Function.new(
  lib['WNetAddConnectionW'],
  [
    Fiddle::TYPE_VOIDP,  # lpRemoteName : LPCWSTR
    Fiddle::TYPE_VOIDP,  # lpPassword : LPCWSTR optional
    Fiddle::TYPE_VOIDP,  # lpLocalName : LPCWSTR optional
  ],
  -Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "mpr")]
extern "system" {
    fn WNetAddConnectionW(
        lpRemoteName: *const u16,  // LPCWSTR
        lpPassword: *const u16,  // LPCWSTR optional
        lpLocalName: *const u16  // LPCWSTR optional
    ) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("MPR.dll", CharSet = CharSet.Unicode)]
public static extern uint WNetAddConnectionW([MarshalAs(UnmanagedType.LPWStr)] string lpRemoteName, [MarshalAs(UnmanagedType.LPWStr)] string lpPassword, [MarshalAs(UnmanagedType.LPWStr)] string lpLocalName);
"@
$api = Add-Type -MemberDefinition $sig -Name 'MPR_WNetAddConnectionW' -Namespace Win32 -PassThru
# $api::WNetAddConnectionW(lpRemoteName, lpPassword, lpLocalName)
#uselib "MPR.dll"
#func global WNetAddConnectionW "WNetAddConnectionW" wptr, wptr, wptr
; WNetAddConnectionW lpRemoteName, lpPassword, lpLocalName   ; 戻り値は stat
; lpRemoteName : LPCWSTR -> "wptr"
; lpPassword : LPCWSTR optional -> "wptr"
; lpLocalName : LPCWSTR optional -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "MPR.dll"
#cfunc global WNetAddConnectionW "WNetAddConnectionW" wstr, wstr, wstr
; res = WNetAddConnectionW(lpRemoteName, lpPassword, lpLocalName)
; lpRemoteName : LPCWSTR -> "wstr"
; lpPassword : LPCWSTR optional -> "wstr"
; lpLocalName : LPCWSTR optional -> "wstr"
; WIN32_ERROR WNetAddConnectionW(LPCWSTR lpRemoteName, LPCWSTR lpPassword, LPCWSTR lpLocalName)
#uselib "MPR.dll"
#cfunc global WNetAddConnectionW "WNetAddConnectionW" wstr, wstr, wstr
; res = WNetAddConnectionW(lpRemoteName, lpPassword, lpLocalName)
; lpRemoteName : LPCWSTR -> "wstr"
; lpPassword : LPCWSTR optional -> "wstr"
; lpLocalName : LPCWSTR optional -> "wstr"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	mpr = windows.NewLazySystemDLL("MPR.dll")
	procWNetAddConnectionW = mpr.NewProc("WNetAddConnectionW")
)

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