ホーム › 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 | 接続先や種別を記述したNETRESOURCEW構造体へのポインタ。 |
| lpPassword | LPCWSTR | inoptional | 接続に用いるパスワードのワイド文字列。既定資格情報を使う場合はNULL可。 |
| lpUserName | LPCWSTR | inoptional | 接続に用いるユーザー名のワイド文字列。既定ユーザーならNULL可。 |
| dwFlags | NET_CONNECT_FLAGS | in | 接続動作を制御するフラグ(永続接続CONNECT_UPDATE_PROFILE等)。 |
戻り値の型: 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 版の利用を推奨。const std = @import("std");
extern "mpr" fn WNetAddConnection2W(
lpNetResource: [*c]NETRESOURCEW, // NETRESOURCEW*
lpPassword: [*c]const u16, // LPCWSTR optional
lpUserName: [*c]const u16, // LPCWSTR optional
dwFlags: u32 // NET_CONNECT_FLAGS
) callconv(std.os.windows.WINAPI) u32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。proc WNetAddConnection2W(
lpNetResource: ptr NETRESOURCEW, # NETRESOURCEW*
lpPassword: WideCString, # LPCWSTR optional
lpUserName: WideCString, # LPCWSTR optional
dwFlags: uint32 # NET_CONNECT_FLAGS
): uint32 {.importc: "WNetAddConnection2W", stdcall, dynlib: "MPR.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。pragma(lib, "mpr");
extern(Windows)
uint WNetAddConnection2W(
NETRESOURCEW* lpNetResource, // NETRESOURCEW*
const(wchar)* lpPassword, // LPCWSTR optional
const(wchar)* lpUserName, // LPCWSTR optional
uint dwFlags // NET_CONNECT_FLAGS
);ccall((:WNetAddConnection2W, "MPR.dll"), stdcall, UInt32,
(Ptr{NETRESOURCEW}, Cwstring, Cwstring, UInt32),
lpNetResource, lpPassword, lpUserName, dwFlags)
# lpNetResource : NETRESOURCEW* -> Ptr{NETRESOURCEW}
# lpPassword : LPCWSTR optional -> Cwstring
# lpUserName : LPCWSTR optional -> Cwstring
# dwFlags : NET_CONNECT_FLAGS -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。local ffi = require("ffi")
ffi.cdef[[
uint32_t WNetAddConnection2W(
void* lpNetResource,
const uint16_t* lpPassword,
const uint16_t* lpUserName,
uint32_t dwFlags);
]]
local mpr = ffi.load("mpr")
-- mpr.WNetAddConnection2W(lpNetResource, lpPassword, lpUserName, dwFlags)
-- lpNetResource : NETRESOURCEW*
-- lpPassword : LPCWSTR optional
-- lpUserName : LPCWSTR optional
-- dwFlags : NET_CONNECT_FLAGS
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。const koffi = require('koffi');
const lib = koffi.load('MPR.dll');
const WNetAddConnection2W = lib.func('__stdcall', 'WNetAddConnection2W', 'uint32_t', ['void *', 'str16', 'str16', 'uint32_t']);
// WNetAddConnection2W(lpNetResource, lpPassword, lpUserName, dwFlags)
// lpNetResource : NETRESOURCEW* -> 'void *'
// lpPassword : LPCWSTR optional -> 'str16'
// lpUserName : LPCWSTR optional -> 'str16'
// dwFlags : NET_CONNECT_FLAGS -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("MPR.dll", {
WNetAddConnection2W: { parameters: ["pointer", "buffer", "buffer", "u32"], result: "u32" },
});
// lib.symbols.WNetAddConnection2W(lpNetResource, lpPassword, lpUserName, dwFlags)
// lpNetResource : NETRESOURCEW* -> "pointer"
// lpPassword : LPCWSTR optional -> "buffer"
// lpUserName : LPCWSTR optional -> "buffer"
// dwFlags : NET_CONNECT_FLAGS -> "u32"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
uint32_t WNetAddConnection2W(
void* lpNetResource,
const uint16_t* lpPassword,
const uint16_t* lpUserName,
uint32_t dwFlags);
C, "MPR.dll");
// $ffi->WNetAddConnection2W(lpNetResource, lpPassword, lpUserName, dwFlags);
// lpNetResource : NETRESOURCEW*
// lpPassword : LPCWSTR optional
// lpUserName : LPCWSTR optional
// dwFlags : NET_CONNECT_FLAGS
// 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
// WINAPI(stdcall): x64 では呼出規約が統一されるため問題なし。x86 では __stdcall 対応のラッパが必要な場合あり。import com.sun.jna.*;
import com.sun.jna.ptr.*;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;
public interface Mpr extends StdCallLibrary {
Mpr INSTANCE = Native.load("mpr", Mpr.class, W32APIOptions.UNICODE_OPTIONS);
int WNetAddConnection2W(
Pointer lpNetResource, // NETRESOURCEW*
WString lpPassword, // LPCWSTR optional
WString lpUserName, // LPCWSTR optional
int dwFlags // NET_CONNECT_FLAGS
);
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。@[Link("mpr")]
lib LibMPR
fun WNetAddConnection2W = WNetAddConnection2W(
lpNetResource : NETRESOURCEW*, # NETRESOURCEW*
lpPassword : UInt16*, # LPCWSTR optional
lpUserName : UInt16*, # LPCWSTR optional
dwFlags : UInt32 # NET_CONNECT_FLAGS
) : UInt32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef WNetAddConnection2WNative = Uint32 Function(Pointer<Void>, Pointer<Utf16>, Pointer<Utf16>, Uint32);
typedef WNetAddConnection2WDart = int Function(Pointer<Void>, Pointer<Utf16>, Pointer<Utf16>, int);
final WNetAddConnection2W = DynamicLibrary.open('MPR.dll')
.lookupFunction<WNetAddConnection2WNative, WNetAddConnection2WDart>('WNetAddConnection2W');
// lpNetResource : NETRESOURCEW* -> Pointer<Void>
// lpPassword : LPCWSTR optional -> Pointer<Utf16>
// lpUserName : LPCWSTR optional -> Pointer<Utf16>
// dwFlags : NET_CONNECT_FLAGS -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function WNetAddConnection2W(
lpNetResource: Pointer; // NETRESOURCEW*
lpPassword: PWideChar; // LPCWSTR optional
lpUserName: PWideChar; // LPCWSTR optional
dwFlags: DWORD // NET_CONNECT_FLAGS
): DWORD; stdcall;
external 'MPR.dll' name 'WNetAddConnection2W';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "WNetAddConnection2W"
c_WNetAddConnection2W :: Ptr () -> CWString -> CWString -> Word32 -> IO Word32
-- lpNetResource : NETRESOURCEW* -> Ptr ()
-- lpPassword : LPCWSTR optional -> CWString
-- lpUserName : LPCWSTR optional -> CWString
-- dwFlags : NET_CONNECT_FLAGS -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let wnetaddconnection2w =
foreign "WNetAddConnection2W"
((ptr void) @-> (ptr uint16_t) @-> (ptr uint16_t) @-> uint32_t @-> returning uint32_t)
(* lpNetResource : NETRESOURCEW* -> (ptr void) *)
(* lpPassword : LPCWSTR optional -> (ptr uint16_t) *)
(* lpUserName : LPCWSTR optional -> (ptr uint16_t) *)
(* dwFlags : NET_CONNECT_FLAGS -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library mpr (t "MPR.dll"))
(cffi:use-foreign-library mpr)
(cffi:defcfun ("WNetAddConnection2W" wnet-add-connection2-w :convention :stdcall) :uint32
(lp-net-resource :pointer) ; NETRESOURCEW*
(lp-password (:string :encoding :utf-16le)) ; LPCWSTR optional
(lp-user-name (:string :encoding :utf-16le)) ; LPCWSTR optional
(dw-flags :uint32)) ; NET_CONNECT_FLAGS
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $WNetAddConnection2W = Win32::API::More->new('MPR',
'DWORD WNetAddConnection2W(LPVOID lpNetResource, LPCWSTR lpPassword, LPCWSTR lpUserName, DWORD dwFlags)');
# my $ret = $WNetAddConnection2W->Call($lpNetResource, $lpPassword, $lpUserName, $dwFlags);
# lpNetResource : NETRESOURCEW* -> LPVOID
# lpPassword : LPCWSTR optional -> LPCWSTR
# lpUserName : LPCWSTR optional -> LPCWSTR
# dwFlags : NET_CONNECT_FLAGS -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。関連項目
文字セット違い
- f WNetAddConnection2A (ANSI版) — 資格情報を指定してネットワークリソースに接続する(ANSI版)。
類似 API
- f WNetAddConnection3W — 所有ウィンドウを指定してネットワークリソースに接続する(Unicode版)。
- f WNetAddConnection4W — 認証バッファを用いてネットワークリソースに接続する(Unicode版)。
- f WNetAddConnectionW — ネットワーク上のリソースへの接続を確立する(Unicode版)。