ホーム › NetworkManagement.WNet › WNetUseConnectionW
WNetUseConnectionW
関数ローカル名割り当てなしでネットワークリソースに接続する(Unicode版)。
シグネチャ
// MPR.dll (Unicode / -W)
#include <windows.h>
WIN32_ERROR WNetUseConnectionW(
HWND hwndOwner, // optional
NETRESOURCEW* lpNetResource,
LPCWSTR lpPassword, // optional
LPCWSTR lpUserId, // optional
NET_CONNECT_FLAGS dwFlags,
LPWSTR lpAccessName, // optional
DWORD* lpBufferSize, // optional
DWORD* lpResult // optional
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| hwndOwner | HWND | inoptional | 資格情報入力ダイアログの親ウィンドウハンドル。不要ならNULL可。 |
| lpNetResource | NETRESOURCEW* | in | 接続先や種別を記述したNETRESOURCEW構造体へのポインタ。 |
| lpPassword | LPCWSTR | inoptional | 接続に用いるパスワードのワイド文字列。既定資格情報ならNULL可。 |
| lpUserId | LPCWSTR | inoptional | 接続に用いるユーザー名のワイド文字列。既定ユーザーならNULL可。 |
| dwFlags | NET_CONNECT_FLAGS | in | 接続動作を制御するフラグ(CONNECT_REDIRECT等)。 |
| lpAccessName | LPWSTR | outoptional | 割り当てられたローカルデバイス名を受け取るワイドバッファ。 |
| lpBufferSize | DWORD* | inoutoptional | lpAccessNameの文字数を入出力で渡し、必要文字数を受け取る。 |
| lpResult | DWORD* | outoptional | 接続結果(CONNECT_LOCALDRIVE等)を受け取る出力先。 |
戻り値の型: WIN32_ERROR
各言語での呼び出し定義
// MPR.dll (Unicode / -W)
#include <windows.h>
WIN32_ERROR WNetUseConnectionW(
HWND hwndOwner, // optional
NETRESOURCEW* lpNetResource,
LPCWSTR lpPassword, // optional
LPCWSTR lpUserId, // optional
NET_CONNECT_FLAGS dwFlags,
LPWSTR lpAccessName, // optional
DWORD* lpBufferSize, // optional
DWORD* lpResult // optional
);[DllImport("MPR.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern uint WNetUseConnectionW(
IntPtr hwndOwner, // HWND optional
IntPtr lpNetResource, // NETRESOURCEW*
[MarshalAs(UnmanagedType.LPWStr)] string lpPassword, // LPCWSTR optional
[MarshalAs(UnmanagedType.LPWStr)] string lpUserId, // LPCWSTR optional
uint dwFlags, // NET_CONNECT_FLAGS
[MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder lpAccessName, // LPWSTR optional, out
IntPtr lpBufferSize, // DWORD* optional, in/out
IntPtr lpResult // DWORD* optional, out
);<DllImport("MPR.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function WNetUseConnectionW(
hwndOwner As IntPtr, ' HWND optional
lpNetResource As IntPtr, ' NETRESOURCEW*
<MarshalAs(UnmanagedType.LPWStr)> lpPassword As String, ' LPCWSTR optional
<MarshalAs(UnmanagedType.LPWStr)> lpUserId As String, ' LPCWSTR optional
dwFlags As UInteger, ' NET_CONNECT_FLAGS
<MarshalAs(UnmanagedType.LPWStr)> lpAccessName As System.Text.StringBuilder, ' LPWSTR optional, out
lpBufferSize As IntPtr, ' DWORD* optional, in/out
lpResult As IntPtr ' DWORD* optional, out
) As UInteger
End Function' hwndOwner : HWND optional
' lpNetResource : NETRESOURCEW*
' lpPassword : LPCWSTR optional
' lpUserId : LPCWSTR optional
' dwFlags : NET_CONNECT_FLAGS
' lpAccessName : LPWSTR optional, out
' lpBufferSize : DWORD* optional, in/out
' lpResult : DWORD* optional, out
Declare PtrSafe Function WNetUseConnectionW Lib "mpr" ( _
ByVal hwndOwner As LongPtr, _
ByVal lpNetResource As LongPtr, _
ByVal lpPassword As LongPtr, _
ByVal lpUserId As LongPtr, _
ByVal dwFlags As Long, _
ByVal lpAccessName As LongPtr, _
ByVal lpBufferSize As LongPtr, _
ByVal lpResult 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
WNetUseConnectionW = ctypes.windll.mpr.WNetUseConnectionW
WNetUseConnectionW.restype = wintypes.DWORD
WNetUseConnectionW.argtypes = [
wintypes.HANDLE, # hwndOwner : HWND optional
ctypes.c_void_p, # lpNetResource : NETRESOURCEW*
wintypes.LPCWSTR, # lpPassword : LPCWSTR optional
wintypes.LPCWSTR, # lpUserId : LPCWSTR optional
wintypes.DWORD, # dwFlags : NET_CONNECT_FLAGS
wintypes.LPWSTR, # lpAccessName : LPWSTR optional, out
ctypes.POINTER(wintypes.DWORD), # lpBufferSize : DWORD* optional, in/out
ctypes.POINTER(wintypes.DWORD), # lpResult : DWORD* optional, out
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('MPR.dll')
WNetUseConnectionW = Fiddle::Function.new(
lib['WNetUseConnectionW'],
[
Fiddle::TYPE_VOIDP, # hwndOwner : HWND optional
Fiddle::TYPE_VOIDP, # lpNetResource : NETRESOURCEW*
Fiddle::TYPE_VOIDP, # lpPassword : LPCWSTR optional
Fiddle::TYPE_VOIDP, # lpUserId : LPCWSTR optional
-Fiddle::TYPE_INT, # dwFlags : NET_CONNECT_FLAGS
Fiddle::TYPE_VOIDP, # lpAccessName : LPWSTR optional, out
Fiddle::TYPE_VOIDP, # lpBufferSize : DWORD* optional, in/out
Fiddle::TYPE_VOIDP, # lpResult : DWORD* optional, out
],
-Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"#[link(name = "mpr")]
extern "system" {
fn WNetUseConnectionW(
hwndOwner: *mut core::ffi::c_void, // HWND optional
lpNetResource: *mut NETRESOURCEW, // NETRESOURCEW*
lpPassword: *const u16, // LPCWSTR optional
lpUserId: *const u16, // LPCWSTR optional
dwFlags: u32, // NET_CONNECT_FLAGS
lpAccessName: *mut u16, // LPWSTR optional, out
lpBufferSize: *mut u32, // DWORD* optional, in/out
lpResult: *mut u32 // DWORD* optional, out
) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("MPR.dll", CharSet = CharSet.Unicode)]
public static extern uint WNetUseConnectionW(IntPtr hwndOwner, IntPtr lpNetResource, [MarshalAs(UnmanagedType.LPWStr)] string lpPassword, [MarshalAs(UnmanagedType.LPWStr)] string lpUserId, uint dwFlags, [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder lpAccessName, IntPtr lpBufferSize, IntPtr lpResult);
"@
$api = Add-Type -MemberDefinition $sig -Name 'MPR_WNetUseConnectionW' -Namespace Win32 -PassThru
# $api::WNetUseConnectionW(hwndOwner, lpNetResource, lpPassword, lpUserId, dwFlags, lpAccessName, lpBufferSize, lpResult)#uselib "MPR.dll"
#func global WNetUseConnectionW "WNetUseConnectionW" wptr, wptr, wptr, wptr, wptr, wptr, wptr, wptr
; WNetUseConnectionW hwndOwner, varptr(lpNetResource), lpPassword, lpUserId, dwFlags, varptr(lpAccessName), varptr(lpBufferSize), varptr(lpResult) ; 戻り値は stat
; hwndOwner : HWND optional -> "wptr"
; lpNetResource : NETRESOURCEW* -> "wptr"
; lpPassword : LPCWSTR optional -> "wptr"
; lpUserId : LPCWSTR optional -> "wptr"
; dwFlags : NET_CONNECT_FLAGS -> "wptr"
; lpAccessName : LPWSTR optional, out -> "wptr"
; lpBufferSize : DWORD* optional, in/out -> "wptr"
; lpResult : DWORD* optional, out -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "MPR.dll" #cfunc global WNetUseConnectionW "WNetUseConnectionW" sptr, var, wstr, wstr, int, var, var, var ; res = WNetUseConnectionW(hwndOwner, lpNetResource, lpPassword, lpUserId, dwFlags, lpAccessName, lpBufferSize, lpResult) ; hwndOwner : HWND optional -> "sptr" ; lpNetResource : NETRESOURCEW* -> "var" ; lpPassword : LPCWSTR optional -> "wstr" ; lpUserId : LPCWSTR optional -> "wstr" ; dwFlags : NET_CONNECT_FLAGS -> "int" ; lpAccessName : LPWSTR optional, out -> "var" ; lpBufferSize : DWORD* optional, in/out -> "var" ; lpResult : DWORD* optional, out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "MPR.dll" #cfunc global WNetUseConnectionW "WNetUseConnectionW" sptr, sptr, wstr, wstr, int, sptr, sptr, sptr ; res = WNetUseConnectionW(hwndOwner, varptr(lpNetResource), lpPassword, lpUserId, dwFlags, varptr(lpAccessName), varptr(lpBufferSize), varptr(lpResult)) ; hwndOwner : HWND optional -> "sptr" ; lpNetResource : NETRESOURCEW* -> "sptr" ; lpPassword : LPCWSTR optional -> "wstr" ; lpUserId : LPCWSTR optional -> "wstr" ; dwFlags : NET_CONNECT_FLAGS -> "int" ; lpAccessName : LPWSTR optional, out -> "sptr" ; lpBufferSize : DWORD* optional, in/out -> "sptr" ; lpResult : DWORD* optional, out -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; WIN32_ERROR WNetUseConnectionW(HWND hwndOwner, NETRESOURCEW* lpNetResource, LPCWSTR lpPassword, LPCWSTR lpUserId, NET_CONNECT_FLAGS dwFlags, LPWSTR lpAccessName, DWORD* lpBufferSize, DWORD* lpResult) #uselib "MPR.dll" #cfunc global WNetUseConnectionW "WNetUseConnectionW" intptr, var, wstr, wstr, int, var, var, var ; res = WNetUseConnectionW(hwndOwner, lpNetResource, lpPassword, lpUserId, dwFlags, lpAccessName, lpBufferSize, lpResult) ; hwndOwner : HWND optional -> "intptr" ; lpNetResource : NETRESOURCEW* -> "var" ; lpPassword : LPCWSTR optional -> "wstr" ; lpUserId : LPCWSTR optional -> "wstr" ; dwFlags : NET_CONNECT_FLAGS -> "int" ; lpAccessName : LPWSTR optional, out -> "var" ; lpBufferSize : DWORD* optional, in/out -> "var" ; lpResult : DWORD* optional, out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; WIN32_ERROR WNetUseConnectionW(HWND hwndOwner, NETRESOURCEW* lpNetResource, LPCWSTR lpPassword, LPCWSTR lpUserId, NET_CONNECT_FLAGS dwFlags, LPWSTR lpAccessName, DWORD* lpBufferSize, DWORD* lpResult) #uselib "MPR.dll" #cfunc global WNetUseConnectionW "WNetUseConnectionW" intptr, intptr, wstr, wstr, int, intptr, intptr, intptr ; res = WNetUseConnectionW(hwndOwner, varptr(lpNetResource), lpPassword, lpUserId, dwFlags, varptr(lpAccessName), varptr(lpBufferSize), varptr(lpResult)) ; hwndOwner : HWND optional -> "intptr" ; lpNetResource : NETRESOURCEW* -> "intptr" ; lpPassword : LPCWSTR optional -> "wstr" ; lpUserId : LPCWSTR optional -> "wstr" ; dwFlags : NET_CONNECT_FLAGS -> "int" ; lpAccessName : LPWSTR optional, out -> "intptr" ; lpBufferSize : DWORD* optional, in/out -> "intptr" ; lpResult : DWORD* optional, out -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
mpr = windows.NewLazySystemDLL("MPR.dll")
procWNetUseConnectionW = mpr.NewProc("WNetUseConnectionW")
)
// hwndOwner (HWND optional), lpNetResource (NETRESOURCEW*), lpPassword (LPCWSTR optional), lpUserId (LPCWSTR optional), dwFlags (NET_CONNECT_FLAGS), lpAccessName (LPWSTR optional, out), lpBufferSize (DWORD* optional, in/out), lpResult (DWORD* optional, out)
r1, _, err := procWNetUseConnectionW.Call(
uintptr(hwndOwner),
uintptr(lpNetResource),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpPassword))),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpUserId))),
uintptr(dwFlags),
uintptr(lpAccessName),
uintptr(lpBufferSize),
uintptr(lpResult),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // WIN32_ERRORfunction WNetUseConnectionW(
hwndOwner: THandle; // HWND optional
lpNetResource: Pointer; // NETRESOURCEW*
lpPassword: PWideChar; // LPCWSTR optional
lpUserId: PWideChar; // LPCWSTR optional
dwFlags: DWORD; // NET_CONNECT_FLAGS
lpAccessName: PWideChar; // LPWSTR optional, out
lpBufferSize: Pointer; // DWORD* optional, in/out
lpResult: Pointer // DWORD* optional, out
): DWORD; stdcall;
external 'MPR.dll' name 'WNetUseConnectionW';result := DllCall("MPR\WNetUseConnectionW"
, "Ptr", hwndOwner ; HWND optional
, "Ptr", lpNetResource ; NETRESOURCEW*
, "WStr", lpPassword ; LPCWSTR optional
, "WStr", lpUserId ; LPCWSTR optional
, "UInt", dwFlags ; NET_CONNECT_FLAGS
, "Ptr", lpAccessName ; LPWSTR optional, out
, "Ptr", lpBufferSize ; DWORD* optional, in/out
, "Ptr", lpResult ; DWORD* optional, out
, "UInt") ; return: WIN32_ERROR●WNetUseConnectionW(hwndOwner, lpNetResource, lpPassword, lpUserId, dwFlags, lpAccessName, lpBufferSize, lpResult) = DLL("MPR.dll", "dword WNetUseConnectionW(void*, void*, char*, char*, dword, char*, void*, void*)")
# 呼び出し: WNetUseConnectionW(hwndOwner, lpNetResource, lpPassword, lpUserId, dwFlags, lpAccessName, lpBufferSize, lpResult)
# hwndOwner : HWND optional -> "void*"
# lpNetResource : NETRESOURCEW* -> "void*"
# lpPassword : LPCWSTR optional -> "char*"
# lpUserId : LPCWSTR optional -> "char*"
# dwFlags : NET_CONNECT_FLAGS -> "dword"
# lpAccessName : LPWSTR optional, out -> "char*"
# lpBufferSize : DWORD* optional, in/out -> "void*"
# lpResult : DWORD* optional, out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。const std = @import("std");
extern "mpr" fn WNetUseConnectionW(
hwndOwner: ?*anyopaque, // HWND optional
lpNetResource: [*c]NETRESOURCEW, // NETRESOURCEW*
lpPassword: [*c]const u16, // LPCWSTR optional
lpUserId: [*c]const u16, // LPCWSTR optional
dwFlags: u32, // NET_CONNECT_FLAGS
lpAccessName: [*c]u16, // LPWSTR optional, out
lpBufferSize: [*c]u32, // DWORD* optional, in/out
lpResult: [*c]u32 // DWORD* optional, out
) callconv(std.os.windows.WINAPI) u32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。proc WNetUseConnectionW(
hwndOwner: pointer, # HWND optional
lpNetResource: ptr NETRESOURCEW, # NETRESOURCEW*
lpPassword: WideCString, # LPCWSTR optional
lpUserId: WideCString, # LPCWSTR optional
dwFlags: uint32, # NET_CONNECT_FLAGS
lpAccessName: ptr uint16, # LPWSTR optional, out
lpBufferSize: ptr uint32, # DWORD* optional, in/out
lpResult: ptr uint32 # DWORD* optional, out
): uint32 {.importc: "WNetUseConnectionW", stdcall, dynlib: "MPR.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。pragma(lib, "mpr");
extern(Windows)
uint WNetUseConnectionW(
void* hwndOwner, // HWND optional
NETRESOURCEW* lpNetResource, // NETRESOURCEW*
const(wchar)* lpPassword, // LPCWSTR optional
const(wchar)* lpUserId, // LPCWSTR optional
uint dwFlags, // NET_CONNECT_FLAGS
wchar* lpAccessName, // LPWSTR optional, out
uint* lpBufferSize, // DWORD* optional, in/out
uint* lpResult // DWORD* optional, out
);ccall((:WNetUseConnectionW, "MPR.dll"), stdcall, UInt32,
(Ptr{Cvoid}, Ptr{NETRESOURCEW}, Cwstring, Cwstring, UInt32, Ptr{UInt16}, Ptr{UInt32}, Ptr{UInt32}),
hwndOwner, lpNetResource, lpPassword, lpUserId, dwFlags, lpAccessName, lpBufferSize, lpResult)
# hwndOwner : HWND optional -> Ptr{Cvoid}
# lpNetResource : NETRESOURCEW* -> Ptr{NETRESOURCEW}
# lpPassword : LPCWSTR optional -> Cwstring
# lpUserId : LPCWSTR optional -> Cwstring
# dwFlags : NET_CONNECT_FLAGS -> UInt32
# lpAccessName : LPWSTR optional, out -> Ptr{UInt16}
# lpBufferSize : DWORD* optional, in/out -> Ptr{UInt32}
# lpResult : DWORD* optional, out -> Ptr{UInt32}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。local ffi = require("ffi")
ffi.cdef[[
uint32_t WNetUseConnectionW(
void* hwndOwner,
void* lpNetResource,
const uint16_t* lpPassword,
const uint16_t* lpUserId,
uint32_t dwFlags,
uint16_t* lpAccessName,
uint32_t* lpBufferSize,
uint32_t* lpResult);
]]
local mpr = ffi.load("mpr")
-- mpr.WNetUseConnectionW(hwndOwner, lpNetResource, lpPassword, lpUserId, dwFlags, lpAccessName, lpBufferSize, lpResult)
-- hwndOwner : HWND optional
-- lpNetResource : NETRESOURCEW*
-- lpPassword : LPCWSTR optional
-- lpUserId : LPCWSTR optional
-- dwFlags : NET_CONNECT_FLAGS
-- lpAccessName : LPWSTR optional, out
-- lpBufferSize : DWORD* optional, in/out
-- lpResult : DWORD* optional, out
-- 構造体/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 WNetUseConnectionW = lib.func('__stdcall', 'WNetUseConnectionW', 'uint32_t', ['void *', 'void *', 'str16', 'str16', 'uint32_t', 'uint16_t *', 'uint32_t *', 'uint32_t *']);
// WNetUseConnectionW(hwndOwner, lpNetResource, lpPassword, lpUserId, dwFlags, lpAccessName, lpBufferSize, lpResult)
// hwndOwner : HWND optional -> 'void *'
// lpNetResource : NETRESOURCEW* -> 'void *'
// lpPassword : LPCWSTR optional -> 'str16'
// lpUserId : LPCWSTR optional -> 'str16'
// dwFlags : NET_CONNECT_FLAGS -> 'uint32_t'
// lpAccessName : LPWSTR optional, out -> 'uint16_t *'
// lpBufferSize : DWORD* optional, in/out -> 'uint32_t *'
// lpResult : DWORD* optional, out -> 'uint32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("MPR.dll", {
WNetUseConnectionW: { parameters: ["pointer", "pointer", "buffer", "buffer", "u32", "buffer", "pointer", "pointer"], result: "u32" },
});
// lib.symbols.WNetUseConnectionW(hwndOwner, lpNetResource, lpPassword, lpUserId, dwFlags, lpAccessName, lpBufferSize, lpResult)
// hwndOwner : HWND optional -> "pointer"
// lpNetResource : NETRESOURCEW* -> "pointer"
// lpPassword : LPCWSTR optional -> "buffer"
// lpUserId : LPCWSTR optional -> "buffer"
// dwFlags : NET_CONNECT_FLAGS -> "u32"
// lpAccessName : LPWSTR optional, out -> "buffer"
// lpBufferSize : DWORD* optional, in/out -> "pointer"
// lpResult : DWORD* optional, out -> "pointer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
uint32_t WNetUseConnectionW(
void* hwndOwner,
void* lpNetResource,
const uint16_t* lpPassword,
const uint16_t* lpUserId,
uint32_t dwFlags,
uint16_t* lpAccessName,
uint32_t* lpBufferSize,
uint32_t* lpResult);
C, "MPR.dll");
// $ffi->WNetUseConnectionW(hwndOwner, lpNetResource, lpPassword, lpUserId, dwFlags, lpAccessName, lpBufferSize, lpResult);
// hwndOwner : HWND optional
// lpNetResource : NETRESOURCEW*
// lpPassword : LPCWSTR optional
// lpUserId : LPCWSTR optional
// dwFlags : NET_CONNECT_FLAGS
// lpAccessName : LPWSTR optional, out
// lpBufferSize : DWORD* optional, in/out
// lpResult : DWORD* optional, out
// 構造体/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 WNetUseConnectionW(
Pointer hwndOwner, // HWND optional
Pointer lpNetResource, // NETRESOURCEW*
WString lpPassword, // LPCWSTR optional
WString lpUserId, // LPCWSTR optional
int dwFlags, // NET_CONNECT_FLAGS
char[] lpAccessName, // LPWSTR optional, out
IntByReference lpBufferSize, // DWORD* optional, in/out
IntByReference lpResult // DWORD* optional, out
);
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。@[Link("mpr")]
lib LibMPR
fun WNetUseConnectionW = WNetUseConnectionW(
hwndOwner : Void*, # HWND optional
lpNetResource : NETRESOURCEW*, # NETRESOURCEW*
lpPassword : UInt16*, # LPCWSTR optional
lpUserId : UInt16*, # LPCWSTR optional
dwFlags : UInt32, # NET_CONNECT_FLAGS
lpAccessName : UInt16*, # LPWSTR optional, out
lpBufferSize : UInt32*, # DWORD* optional, in/out
lpResult : UInt32* # DWORD* optional, out
) : UInt32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef WNetUseConnectionWNative = Uint32 Function(Pointer<Void>, Pointer<Void>, Pointer<Utf16>, Pointer<Utf16>, Uint32, Pointer<Utf16>, Pointer<Uint32>, Pointer<Uint32>);
typedef WNetUseConnectionWDart = int Function(Pointer<Void>, Pointer<Void>, Pointer<Utf16>, Pointer<Utf16>, int, Pointer<Utf16>, Pointer<Uint32>, Pointer<Uint32>);
final WNetUseConnectionW = DynamicLibrary.open('MPR.dll')
.lookupFunction<WNetUseConnectionWNative, WNetUseConnectionWDart>('WNetUseConnectionW');
// hwndOwner : HWND optional -> Pointer<Void>
// lpNetResource : NETRESOURCEW* -> Pointer<Void>
// lpPassword : LPCWSTR optional -> Pointer<Utf16>
// lpUserId : LPCWSTR optional -> Pointer<Utf16>
// dwFlags : NET_CONNECT_FLAGS -> Uint32
// lpAccessName : LPWSTR optional, out -> Pointer<Utf16>
// lpBufferSize : DWORD* optional, in/out -> Pointer<Uint32>
// lpResult : DWORD* optional, out -> Pointer<Uint32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function WNetUseConnectionW(
hwndOwner: THandle; // HWND optional
lpNetResource: Pointer; // NETRESOURCEW*
lpPassword: PWideChar; // LPCWSTR optional
lpUserId: PWideChar; // LPCWSTR optional
dwFlags: DWORD; // NET_CONNECT_FLAGS
lpAccessName: PWideChar; // LPWSTR optional, out
lpBufferSize: Pointer; // DWORD* optional, in/out
lpResult: Pointer // DWORD* optional, out
): DWORD; stdcall;
external 'MPR.dll' name 'WNetUseConnectionW';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "WNetUseConnectionW"
c_WNetUseConnectionW :: Ptr () -> Ptr () -> CWString -> CWString -> Word32 -> CWString -> Ptr Word32 -> Ptr Word32 -> IO Word32
-- hwndOwner : HWND optional -> Ptr ()
-- lpNetResource : NETRESOURCEW* -> Ptr ()
-- lpPassword : LPCWSTR optional -> CWString
-- lpUserId : LPCWSTR optional -> CWString
-- dwFlags : NET_CONNECT_FLAGS -> Word32
-- lpAccessName : LPWSTR optional, out -> CWString
-- lpBufferSize : DWORD* optional, in/out -> Ptr Word32
-- lpResult : DWORD* optional, out -> Ptr Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let wnetuseconnectionw =
foreign "WNetUseConnectionW"
((ptr void) @-> (ptr void) @-> (ptr uint16_t) @-> (ptr uint16_t) @-> uint32_t @-> (ptr uint16_t) @-> (ptr uint32_t) @-> (ptr uint32_t) @-> returning uint32_t)
(* hwndOwner : HWND optional -> (ptr void) *)
(* lpNetResource : NETRESOURCEW* -> (ptr void) *)
(* lpPassword : LPCWSTR optional -> (ptr uint16_t) *)
(* lpUserId : LPCWSTR optional -> (ptr uint16_t) *)
(* dwFlags : NET_CONNECT_FLAGS -> uint32_t *)
(* lpAccessName : LPWSTR optional, out -> (ptr uint16_t) *)
(* lpBufferSize : DWORD* optional, in/out -> (ptr uint32_t) *)
(* lpResult : DWORD* optional, out -> (ptr uint32_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library mpr (t "MPR.dll"))
(cffi:use-foreign-library mpr)
(cffi:defcfun ("WNetUseConnectionW" wnet-use-connection-w :convention :stdcall) :uint32
(hwnd-owner :pointer) ; HWND optional
(lp-net-resource :pointer) ; NETRESOURCEW*
(lp-password (:string :encoding :utf-16le)) ; LPCWSTR optional
(lp-user-id (:string :encoding :utf-16le)) ; LPCWSTR optional
(dw-flags :uint32) ; NET_CONNECT_FLAGS
(lp-access-name :pointer) ; LPWSTR optional, out
(lp-buffer-size :pointer) ; DWORD* optional, in/out
(lp-result :pointer)) ; DWORD* optional, out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $WNetUseConnectionW = Win32::API::More->new('MPR',
'DWORD WNetUseConnectionW(HANDLE hwndOwner, LPVOID lpNetResource, LPCWSTR lpPassword, LPCWSTR lpUserId, DWORD dwFlags, LPWSTR lpAccessName, LPVOID lpBufferSize, LPVOID lpResult)');
# my $ret = $WNetUseConnectionW->Call($hwndOwner, $lpNetResource, $lpPassword, $lpUserId, $dwFlags, $lpAccessName, $lpBufferSize, $lpResult);
# hwndOwner : HWND optional -> HANDLE
# lpNetResource : NETRESOURCEW* -> LPVOID
# lpPassword : LPCWSTR optional -> LPCWSTR
# lpUserId : LPCWSTR optional -> LPCWSTR
# dwFlags : NET_CONNECT_FLAGS -> DWORD
# lpAccessName : LPWSTR optional, out -> LPWSTR
# lpBufferSize : DWORD* optional, in/out -> LPVOID
# lpResult : DWORD* optional, out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。関連項目
文字セット違い
- f WNetUseConnectionA (ANSI版) — ローカル名割り当てなしでネットワークリソースに接続する(ANSI版)。
類似 API
- f WNetUseConnection4W — 認証バッファとオプションを用いてリソースに接続する(Unicode版)。