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

WNetGetConnectionW

関数
ローカル名に対応するリモートネットワーク名を取得する(Unicode版)。
DLLMPR.dll文字セットUnicode (-W)呼出規約winapi対応OSWindows 2000 以降

シグネチャ

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

WIN32_ERROR WNetGetConnectionW(
    LPCWSTR lpLocalName,
    LPWSTR lpRemoteName,   // optional
    DWORD* lpnLength
);

パラメーター

名前方向
lpLocalNameLPCWSTRin
lpRemoteNameLPWSTRoutoptional
lpnLengthDWORD*inout

戻り値の型: WIN32_ERROR

各言語での呼び出し定義

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

WIN32_ERROR WNetGetConnectionW(
    LPCWSTR lpLocalName,
    LPWSTR lpRemoteName,   // optional
    DWORD* lpnLength
);
[DllImport("MPR.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern uint WNetGetConnectionW(
    [MarshalAs(UnmanagedType.LPWStr)] string lpLocalName,   // LPCWSTR
    [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder lpRemoteName,   // LPWSTR optional, out
    ref uint lpnLength   // DWORD* in/out
);
<DllImport("MPR.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function WNetGetConnectionW(
    <MarshalAs(UnmanagedType.LPWStr)> lpLocalName As String,   ' LPCWSTR
    <MarshalAs(UnmanagedType.LPWStr)> lpRemoteName As System.Text.StringBuilder,   ' LPWSTR optional, out
    ByRef lpnLength As UInteger   ' DWORD* in/out
) As UInteger
End Function
' lpLocalName : LPCWSTR
' lpRemoteName : LPWSTR optional, out
' lpnLength : DWORD* in/out
Declare PtrSafe Function WNetGetConnectionW Lib "mpr" ( _
    ByVal lpLocalName As LongPtr, _
    ByVal lpRemoteName As LongPtr, _
    ByRef lpnLength 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

WNetGetConnectionW = ctypes.windll.mpr.WNetGetConnectionW
WNetGetConnectionW.restype = wintypes.DWORD
WNetGetConnectionW.argtypes = [
    wintypes.LPCWSTR,  # lpLocalName : LPCWSTR
    wintypes.LPWSTR,  # lpRemoteName : LPWSTR optional, out
    ctypes.POINTER(wintypes.DWORD),  # lpnLength : DWORD* in/out
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('MPR.dll')
WNetGetConnectionW = Fiddle::Function.new(
  lib['WNetGetConnectionW'],
  [
    Fiddle::TYPE_VOIDP,  # lpLocalName : LPCWSTR
    Fiddle::TYPE_VOIDP,  # lpRemoteName : LPWSTR optional, out
    Fiddle::TYPE_VOIDP,  # lpnLength : DWORD* in/out
  ],
  -Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "mpr")]
extern "system" {
    fn WNetGetConnectionW(
        lpLocalName: *const u16,  // LPCWSTR
        lpRemoteName: *mut u16,  // LPWSTR optional, out
        lpnLength: *mut u32  // DWORD* in/out
    ) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("MPR.dll", CharSet = CharSet.Unicode)]
public static extern uint WNetGetConnectionW([MarshalAs(UnmanagedType.LPWStr)] string lpLocalName, [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder lpRemoteName, ref uint lpnLength);
"@
$api = Add-Type -MemberDefinition $sig -Name 'MPR_WNetGetConnectionW' -Namespace Win32 -PassThru
# $api::WNetGetConnectionW(lpLocalName, lpRemoteName, lpnLength)
#uselib "MPR.dll"
#func global WNetGetConnectionW "WNetGetConnectionW" wptr, wptr, wptr
; WNetGetConnectionW lpLocalName, varptr(lpRemoteName), varptr(lpnLength)   ; 戻り値は stat
; lpLocalName : LPCWSTR -> "wptr"
; lpRemoteName : LPWSTR optional, out -> "wptr"
; lpnLength : DWORD* in/out -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "MPR.dll"
#cfunc global WNetGetConnectionW "WNetGetConnectionW" wstr, var, var
; res = WNetGetConnectionW(lpLocalName, lpRemoteName, lpnLength)
; lpLocalName : LPCWSTR -> "wstr"
; lpRemoteName : LPWSTR optional, out -> "var"
; lpnLength : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; WIN32_ERROR WNetGetConnectionW(LPCWSTR lpLocalName, LPWSTR lpRemoteName, DWORD* lpnLength)
#uselib "MPR.dll"
#cfunc global WNetGetConnectionW "WNetGetConnectionW" wstr, var, var
; res = WNetGetConnectionW(lpLocalName, lpRemoteName, lpnLength)
; lpLocalName : LPCWSTR -> "wstr"
; lpRemoteName : LPWSTR optional, out -> "var"
; lpnLength : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	mpr = windows.NewLazySystemDLL("MPR.dll")
	procWNetGetConnectionW = mpr.NewProc("WNetGetConnectionW")
)

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