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資格情報入力ダイアログの親ウィンドウハンドル。不要ならNULL可。
lpNetResourceNETRESOURCEW*in接続先や種別を記述したNETRESOURCEW構造体へのポインタ。
lpPasswordLPCWSTRinoptional接続に用いるパスワードのワイド文字列。既定資格情報を使う場合はNULL可。
lpUserNameLPCWSTRinoptional接続に用いるユーザー名のワイド文字列。既定ユーザーならNULL可。
dwFlagsNET_CONNECT_FLAGSin接続動作を制御するフラグ(CONNECT_INTERACTIVE等)。

戻り値の型: 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 版の利用を推奨。
const std = @import("std");

extern "mpr" fn WNetAddConnection3W(
    hwndOwner: ?*anyopaque, // HWND optional
    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 WNetAddConnection3W(
    hwndOwner: pointer,  # HWND optional
    lpNetResource: ptr NETRESOURCEW,  # NETRESOURCEW*
    lpPassword: WideCString,  # LPCWSTR optional
    lpUserName: WideCString,  # LPCWSTR optional
    dwFlags: uint32  # NET_CONNECT_FLAGS
): uint32 {.importc: "WNetAddConnection3W", stdcall, dynlib: "MPR.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "mpr");
extern(Windows)
uint WNetAddConnection3W(
    void* hwndOwner,   // HWND optional
    NETRESOURCEW* lpNetResource,   // NETRESOURCEW*
    const(wchar)* lpPassword,   // LPCWSTR optional
    const(wchar)* lpUserName,   // LPCWSTR optional
    uint dwFlags   // NET_CONNECT_FLAGS
);
ccall((:WNetAddConnection3W, "MPR.dll"), stdcall, UInt32,
      (Ptr{Cvoid}, Ptr{NETRESOURCEW}, Cwstring, Cwstring, UInt32),
      hwndOwner, lpNetResource, lpPassword, lpUserName, dwFlags)
# hwndOwner : HWND optional -> Ptr{Cvoid}
# 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 WNetAddConnection3W(
    void* hwndOwner,
    void* lpNetResource,
    const uint16_t* lpPassword,
    const uint16_t* lpUserName,
    uint32_t dwFlags);
]]
local mpr = ffi.load("mpr")
-- mpr.WNetAddConnection3W(hwndOwner, lpNetResource, lpPassword, lpUserName, dwFlags)
-- hwndOwner : HWND optional
-- 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 WNetAddConnection3W = lib.func('__stdcall', 'WNetAddConnection3W', 'uint32_t', ['void *', 'void *', 'str16', 'str16', 'uint32_t']);
// WNetAddConnection3W(hwndOwner, lpNetResource, lpPassword, lpUserName, dwFlags)
// hwndOwner : HWND optional -> 'void *'
// 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", {
  WNetAddConnection3W: { parameters: ["pointer", "pointer", "buffer", "buffer", "u32"], result: "u32" },
});
// lib.symbols.WNetAddConnection3W(hwndOwner, lpNetResource, lpPassword, lpUserName, dwFlags)
// hwndOwner : HWND optional -> "pointer"
// 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 WNetAddConnection3W(
    void* hwndOwner,
    void* lpNetResource,
    const uint16_t* lpPassword,
    const uint16_t* lpUserName,
    uint32_t dwFlags);
C, "MPR.dll");
// $ffi->WNetAddConnection3W(hwndOwner, lpNetResource, lpPassword, lpUserName, dwFlags);
// hwndOwner : HWND optional
// 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 WNetAddConnection3W(
        Pointer hwndOwner,   // HWND optional
        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 WNetAddConnection3W = WNetAddConnection3W(
    hwndOwner : Void*,   # HWND optional
    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 WNetAddConnection3WNative = Uint32 Function(Pointer<Void>, Pointer<Void>, Pointer<Utf16>, Pointer<Utf16>, Uint32);
typedef WNetAddConnection3WDart = int Function(Pointer<Void>, Pointer<Void>, Pointer<Utf16>, Pointer<Utf16>, int);
final WNetAddConnection3W = DynamicLibrary.open('MPR.dll')
    .lookupFunction<WNetAddConnection3WNative, WNetAddConnection3WDart>('WNetAddConnection3W');
// hwndOwner : HWND optional -> Pointer<Void>
// 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 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';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "WNetAddConnection3W"
  c_WNetAddConnection3W :: Ptr () -> Ptr () -> CWString -> CWString -> Word32 -> IO Word32
-- hwndOwner : HWND optional -> Ptr ()
-- 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 wnetaddconnection3w =
  foreign "WNetAddConnection3W"
    ((ptr void) @-> (ptr void) @-> (ptr uint16_t) @-> (ptr uint16_t) @-> uint32_t @-> returning uint32_t)
(* hwndOwner : HWND optional -> (ptr void) *)
(* 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 ("WNetAddConnection3W" wnet-add-connection3-w :convention :stdcall) :uint32
  (hwnd-owner :pointer)   ; HWND optional
  (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 $WNetAddConnection3W = Win32::API::More->new('MPR',
    'DWORD WNetAddConnection3W(HANDLE hwndOwner, LPVOID lpNetResource, LPCWSTR lpPassword, LPCWSTR lpUserName, DWORD dwFlags)');
# my $ret = $WNetAddConnection3W->Call($hwndOwner, $lpNetResource, $lpPassword, $lpUserName, $dwFlags);
# hwndOwner : HWND optional -> HANDLE
# 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 変換を行う。

関連項目

文字セット違い
類似 API
使用する型