ホーム › NetworkManagement.WNet › WNetAddConnection2W
WNetAddConnection2W
関数資格情報を指定してネットワークリソースに接続する(Unicode版)。
シグネチャ
// MPR.dll (Unicode / -W)
#include <windows.h>
WIN32_ERROR WNetAddConnection2W(
NETRESOURCEW* lpNetResource,
LPCWSTR lpPassword, // optional
LPCWSTR lpUserName, // optional
NET_CONNECT_FLAGS dwFlags
);パラメーター
| 名前 | 型 | 方向 |
|---|---|---|
| lpNetResource | NETRESOURCEW* | in |
| lpPassword | LPCWSTR | inoptional |
| lpUserName | LPCWSTR | inoptional |
| dwFlags | NET_CONNECT_FLAGS | in |
戻り値の型: WIN32_ERROR
各言語での呼び出し定義
// MPR.dll (Unicode / -W)
#include <windows.h>
WIN32_ERROR WNetAddConnection2W(
NETRESOURCEW* lpNetResource,
LPCWSTR lpPassword, // optional
LPCWSTR lpUserName, // optional
NET_CONNECT_FLAGS dwFlags
);[DllImport("MPR.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern uint WNetAddConnection2W(
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 WNetAddConnection2W(
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' lpNetResource : NETRESOURCEW*
' lpPassword : LPCWSTR optional
' lpUserName : LPCWSTR optional
' dwFlags : NET_CONNECT_FLAGS
Declare PtrSafe Function WNetAddConnection2W Lib "mpr" ( _
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
WNetAddConnection2W = ctypes.windll.mpr.WNetAddConnection2W
WNetAddConnection2W.restype = wintypes.DWORD
WNetAddConnection2W.argtypes = [
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')
WNetAddConnection2W = Fiddle::Function.new(
lib['WNetAddConnection2W'],
[
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 WNetAddConnection2W(
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 WNetAddConnection2W(IntPtr lpNetResource, [MarshalAs(UnmanagedType.LPWStr)] string lpPassword, [MarshalAs(UnmanagedType.LPWStr)] string lpUserName, uint dwFlags);
"@
$api = Add-Type -MemberDefinition $sig -Name 'MPR_WNetAddConnection2W' -Namespace Win32 -PassThru
# $api::WNetAddConnection2W(lpNetResource, lpPassword, lpUserName, dwFlags)#uselib "MPR.dll"
#func global WNetAddConnection2W "WNetAddConnection2W" wptr, wptr, wptr, wptr
; WNetAddConnection2W varptr(lpNetResource), lpPassword, lpUserName, dwFlags ; 戻り値は stat
; 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 WNetAddConnection2W "WNetAddConnection2W" var, wstr, wstr, int ; res = WNetAddConnection2W(lpNetResource, lpPassword, lpUserName, dwFlags) ; lpNetResource : NETRESOURCEW* -> "var" ; lpPassword : LPCWSTR optional -> "wstr" ; lpUserName : LPCWSTR optional -> "wstr" ; dwFlags : NET_CONNECT_FLAGS -> "int" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "MPR.dll" #cfunc global WNetAddConnection2W "WNetAddConnection2W" sptr, wstr, wstr, int ; res = WNetAddConnection2W(varptr(lpNetResource), lpPassword, lpUserName, dwFlags) ; lpNetResource : NETRESOURCEW* -> "sptr" ; lpPassword : LPCWSTR optional -> "wstr" ; lpUserName : LPCWSTR optional -> "wstr" ; dwFlags : NET_CONNECT_FLAGS -> "int" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; WIN32_ERROR WNetAddConnection2W(NETRESOURCEW* lpNetResource, LPCWSTR lpPassword, LPCWSTR lpUserName, NET_CONNECT_FLAGS dwFlags) #uselib "MPR.dll" #cfunc global WNetAddConnection2W "WNetAddConnection2W" var, wstr, wstr, int ; res = WNetAddConnection2W(lpNetResource, lpPassword, lpUserName, dwFlags) ; lpNetResource : NETRESOURCEW* -> "var" ; lpPassword : LPCWSTR optional -> "wstr" ; lpUserName : LPCWSTR optional -> "wstr" ; dwFlags : NET_CONNECT_FLAGS -> "int" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; WIN32_ERROR WNetAddConnection2W(NETRESOURCEW* lpNetResource, LPCWSTR lpPassword, LPCWSTR lpUserName, NET_CONNECT_FLAGS dwFlags) #uselib "MPR.dll" #cfunc global WNetAddConnection2W "WNetAddConnection2W" intptr, wstr, wstr, int ; res = WNetAddConnection2W(varptr(lpNetResource), lpPassword, lpUserName, dwFlags) ; lpNetResource : NETRESOURCEW* -> "intptr" ; lpPassword : LPCWSTR optional -> "wstr" ; lpUserName : LPCWSTR optional -> "wstr" ; dwFlags : NET_CONNECT_FLAGS -> "int" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
mpr = windows.NewLazySystemDLL("MPR.dll")
procWNetAddConnection2W = mpr.NewProc("WNetAddConnection2W")
)
// lpNetResource (NETRESOURCEW*), lpPassword (LPCWSTR optional), lpUserName (LPCWSTR optional), dwFlags (NET_CONNECT_FLAGS)
r1, _, err := procWNetAddConnection2W.Call(
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_ERRORfunction WNetAddConnection2W(
lpNetResource: Pointer; // NETRESOURCEW*
lpPassword: PWideChar; // LPCWSTR optional
lpUserName: PWideChar; // LPCWSTR optional
dwFlags: DWORD // NET_CONNECT_FLAGS
): DWORD; stdcall;
external 'MPR.dll' name 'WNetAddConnection2W';result := DllCall("MPR\WNetAddConnection2W"
, "Ptr", lpNetResource ; NETRESOURCEW*
, "WStr", lpPassword ; LPCWSTR optional
, "WStr", lpUserName ; LPCWSTR optional
, "UInt", dwFlags ; NET_CONNECT_FLAGS
, "UInt") ; return: WIN32_ERROR●WNetAddConnection2W(lpNetResource, lpPassword, lpUserName, dwFlags) = DLL("MPR.dll", "dword WNetAddConnection2W(void*, char*, char*, dword)")
# 呼び出し: WNetAddConnection2W(lpNetResource, lpPassword, lpUserName, dwFlags)
# 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 版の利用を推奨。