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

extern "mpr" fn WNetGetConnectionW(
    lpLocalName: [*c]const u16, // LPCWSTR
    lpRemoteName: [*c]u16, // LPWSTR optional, out
    lpnLength: [*c]u32 // DWORD* in/out
) callconv(std.os.windows.WINAPI) u32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。
proc WNetGetConnectionW(
    lpLocalName: WideCString,  # LPCWSTR
    lpRemoteName: ptr uint16,  # LPWSTR optional, out
    lpnLength: ptr uint32  # DWORD* in/out
): uint32 {.importc: "WNetGetConnectionW", stdcall, dynlib: "MPR.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "mpr");
extern(Windows)
uint WNetGetConnectionW(
    const(wchar)* lpLocalName,   // LPCWSTR
    wchar* lpRemoteName,   // LPWSTR optional, out
    uint* lpnLength   // DWORD* in/out
);
ccall((:WNetGetConnectionW, "MPR.dll"), stdcall, UInt32,
      (Cwstring, Ptr{UInt16}, Ptr{UInt32}),
      lpLocalName, lpRemoteName, lpnLength)
# lpLocalName : LPCWSTR -> Cwstring
# lpRemoteName : LPWSTR optional, out -> Ptr{UInt16}
# lpnLength : DWORD* in/out -> Ptr{UInt32}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。
local ffi = require("ffi")
ffi.cdef[[
uint32_t WNetGetConnectionW(
    const uint16_t* lpLocalName,
    uint16_t* lpRemoteName,
    uint32_t* lpnLength);
]]
local mpr = ffi.load("mpr")
-- mpr.WNetGetConnectionW(lpLocalName, lpRemoteName, lpnLength)
-- lpLocalName : LPCWSTR
-- lpRemoteName : LPWSTR optional, out
-- lpnLength : DWORD* in/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 WNetGetConnectionW = lib.func('__stdcall', 'WNetGetConnectionW', 'uint32_t', ['str16', 'uint16_t *', 'uint32_t *']);
// WNetGetConnectionW(lpLocalName, lpRemoteName, lpnLength)
// lpLocalName : LPCWSTR -> 'str16'
// lpRemoteName : LPWSTR optional, out -> 'uint16_t *'
// lpnLength : DWORD* in/out -> 'uint32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("MPR.dll", {
  WNetGetConnectionW: { parameters: ["buffer", "buffer", "pointer"], result: "u32" },
});
// lib.symbols.WNetGetConnectionW(lpLocalName, lpRemoteName, lpnLength)
// lpLocalName : LPCWSTR -> "buffer"
// lpRemoteName : LPWSTR optional, out -> "buffer"
// lpnLength : DWORD* in/out -> "pointer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
uint32_t WNetGetConnectionW(
    const uint16_t* lpLocalName,
    uint16_t* lpRemoteName,
    uint32_t* lpnLength);
C, "MPR.dll");
// $ffi->WNetGetConnectionW(lpLocalName, lpRemoteName, lpnLength);
// lpLocalName : LPCWSTR
// lpRemoteName : LPWSTR optional, out
// lpnLength : DWORD* in/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 WNetGetConnectionW(
        WString lpLocalName,   // LPCWSTR
        char[] lpRemoteName,   // LPWSTR optional, out
        IntByReference lpnLength   // DWORD* in/out
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("mpr")]
lib LibMPR
  fun WNetGetConnectionW = WNetGetConnectionW(
    lpLocalName : UInt16*,   # LPCWSTR
    lpRemoteName : UInt16*,   # LPWSTR optional, out
    lpnLength : UInt32*   # DWORD* in/out
  ) : UInt32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef WNetGetConnectionWNative = Uint32 Function(Pointer<Utf16>, Pointer<Utf16>, Pointer<Uint32>);
typedef WNetGetConnectionWDart = int Function(Pointer<Utf16>, Pointer<Utf16>, Pointer<Uint32>);
final WNetGetConnectionW = DynamicLibrary.open('MPR.dll')
    .lookupFunction<WNetGetConnectionWNative, WNetGetConnectionWDart>('WNetGetConnectionW');
// lpLocalName : LPCWSTR -> Pointer<Utf16>
// lpRemoteName : LPWSTR optional, out -> Pointer<Utf16>
// lpnLength : DWORD* in/out -> Pointer<Uint32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function WNetGetConnectionW(
  lpLocalName: PWideChar;   // LPCWSTR
  lpRemoteName: PWideChar;   // LPWSTR optional, out
  lpnLength: Pointer   // DWORD* in/out
): DWORD; stdcall;
  external 'MPR.dll' name 'WNetGetConnectionW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "WNetGetConnectionW"
  c_WNetGetConnectionW :: CWString -> CWString -> Ptr Word32 -> IO Word32
-- lpLocalName : LPCWSTR -> CWString
-- lpRemoteName : LPWSTR optional, out -> CWString
-- lpnLength : DWORD* in/out -> Ptr Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let wnetgetconnectionw =
  foreign "WNetGetConnectionW"
    ((ptr uint16_t) @-> (ptr uint16_t) @-> (ptr uint32_t) @-> returning uint32_t)
(* lpLocalName : LPCWSTR -> (ptr uint16_t) *)
(* lpRemoteName : LPWSTR optional, out -> (ptr uint16_t) *)
(* lpnLength : DWORD* in/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 ("WNetGetConnectionW" wnet-get-connection-w :convention :stdcall) :uint32
  (lp-local-name (:string :encoding :utf-16le))   ; LPCWSTR
  (lp-remote-name :pointer)   ; LPWSTR optional, out
  (lpn-length :pointer))   ; DWORD* in/out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $WNetGetConnectionW = Win32::API::More->new('MPR',
    'DWORD WNetGetConnectionW(LPCWSTR lpLocalName, LPWSTR lpRemoteName, LPVOID lpnLength)');
# my $ret = $WNetGetConnectionW->Call($lpLocalName, $lpRemoteName, $lpnLength);
# lpLocalName : LPCWSTR -> LPCWSTR
# lpRemoteName : LPWSTR optional, out -> LPWSTR
# lpnLength : DWORD* in/out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

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