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

WNetGetUserW

関数
ネットワーク接続に使用したユーザー名を取得する(Unicode版)。
DLLMPR.dll文字セットUnicode (-W)呼出規約winapi対応OSWindows 2000 以降

シグネチャ

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

WIN32_ERROR WNetGetUserW(
    LPCWSTR lpName,   // optional
    LPWSTR lpUserName,
    DWORD* lpnLength
);

パラメーター

名前方向説明
lpNameLPCWSTRinoptional問い合わせる接続のローカルデバイス名のワイド文字列。NULLで現在のユーザー。
lpUserNameLPWSTRout接続に使用したユーザー名を受け取るワイドバッファ。
lpnLengthDWORD*inoutlpUserNameの文字数を入出力で渡し、必要文字数を受け取る。

戻り値の型: WIN32_ERROR

各言語での呼び出し定義

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

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

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

lib = Fiddle.dlopen('MPR.dll')
WNetGetUserW = Fiddle::Function.new(
  lib['WNetGetUserW'],
  [
    Fiddle::TYPE_VOIDP,  # lpName : LPCWSTR optional
    Fiddle::TYPE_VOIDP,  # lpUserName : LPWSTR 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 WNetGetUserW(
        lpName: *const u16,  // LPCWSTR optional
        lpUserName: *mut u16,  // LPWSTR 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 WNetGetUserW([MarshalAs(UnmanagedType.LPWStr)] string lpName, [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder lpUserName, ref uint lpnLength);
"@
$api = Add-Type -MemberDefinition $sig -Name 'MPR_WNetGetUserW' -Namespace Win32 -PassThru
# $api::WNetGetUserW(lpName, lpUserName, lpnLength)
#uselib "MPR.dll"
#func global WNetGetUserW "WNetGetUserW" wptr, wptr, wptr
; WNetGetUserW lpName, varptr(lpUserName), varptr(lpnLength)   ; 戻り値は stat
; lpName : LPCWSTR optional -> "wptr"
; lpUserName : LPWSTR out -> "wptr"
; lpnLength : DWORD* in/out -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "MPR.dll"
#cfunc global WNetGetUserW "WNetGetUserW" wstr, var, var
; res = WNetGetUserW(lpName, lpUserName, lpnLength)
; lpName : LPCWSTR optional -> "wstr"
; lpUserName : LPWSTR out -> "var"
; lpnLength : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; WIN32_ERROR WNetGetUserW(LPCWSTR lpName, LPWSTR lpUserName, DWORD* lpnLength)
#uselib "MPR.dll"
#cfunc global WNetGetUserW "WNetGetUserW" wstr, var, var
; res = WNetGetUserW(lpName, lpUserName, lpnLength)
; lpName : LPCWSTR optional -> "wstr"
; lpUserName : LPWSTR out -> "var"
; lpnLength : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	mpr = windows.NewLazySystemDLL("MPR.dll")
	procWNetGetUserW = mpr.NewProc("WNetGetUserW")
)

// lpName (LPCWSTR optional), lpUserName (LPWSTR out), lpnLength (DWORD* in/out)
r1, _, err := procWNetGetUserW.Call(
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpName))),
	uintptr(lpUserName),
	uintptr(lpnLength),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // WIN32_ERROR
function WNetGetUserW(
  lpName: PWideChar;   // LPCWSTR optional
  lpUserName: PWideChar;   // LPWSTR out
  lpnLength: Pointer   // DWORD* in/out
): DWORD; stdcall;
  external 'MPR.dll' name 'WNetGetUserW';
result := DllCall("MPR\WNetGetUserW"
    , "WStr", lpName   ; LPCWSTR optional
    , "Ptr", lpUserName   ; LPWSTR out
    , "Ptr", lpnLength   ; DWORD* in/out
    , "UInt")   ; return: WIN32_ERROR
●WNetGetUserW(lpName, lpUserName, lpnLength) = DLL("MPR.dll", "dword WNetGetUserW(char*, char*, void*)")
# 呼び出し: WNetGetUserW(lpName, lpUserName, lpnLength)
# lpName : LPCWSTR optional -> "char*"
# lpUserName : LPWSTR 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 WNetGetUserW(
    lpName: [*c]const u16, // LPCWSTR optional
    lpUserName: [*c]u16, // LPWSTR out
    lpnLength: [*c]u32 // DWORD* in/out
) callconv(std.os.windows.WINAPI) u32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。
proc WNetGetUserW(
    lpName: WideCString,  # LPCWSTR optional
    lpUserName: ptr uint16,  # LPWSTR out
    lpnLength: ptr uint32  # DWORD* in/out
): uint32 {.importc: "WNetGetUserW", stdcall, dynlib: "MPR.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "mpr");
extern(Windows)
uint WNetGetUserW(
    const(wchar)* lpName,   // LPCWSTR optional
    wchar* lpUserName,   // LPWSTR out
    uint* lpnLength   // DWORD* in/out
);
ccall((:WNetGetUserW, "MPR.dll"), stdcall, UInt32,
      (Cwstring, Ptr{UInt16}, Ptr{UInt32}),
      lpName, lpUserName, lpnLength)
# lpName : LPCWSTR optional -> Cwstring
# lpUserName : LPWSTR 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 WNetGetUserW(
    const uint16_t* lpName,
    uint16_t* lpUserName,
    uint32_t* lpnLength);
]]
local mpr = ffi.load("mpr")
-- mpr.WNetGetUserW(lpName, lpUserName, lpnLength)
-- lpName : LPCWSTR optional
-- lpUserName : LPWSTR 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 WNetGetUserW = lib.func('__stdcall', 'WNetGetUserW', 'uint32_t', ['str16', 'uint16_t *', 'uint32_t *']);
// WNetGetUserW(lpName, lpUserName, lpnLength)
// lpName : LPCWSTR optional -> 'str16'
// lpUserName : LPWSTR out -> 'uint16_t *'
// lpnLength : DWORD* in/out -> 'uint32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("MPR.dll", {
  WNetGetUserW: { parameters: ["buffer", "buffer", "pointer"], result: "u32" },
});
// lib.symbols.WNetGetUserW(lpName, lpUserName, lpnLength)
// lpName : LPCWSTR optional -> "buffer"
// lpUserName : LPWSTR 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 WNetGetUserW(
    const uint16_t* lpName,
    uint16_t* lpUserName,
    uint32_t* lpnLength);
C, "MPR.dll");
// $ffi->WNetGetUserW(lpName, lpUserName, lpnLength);
// lpName : LPCWSTR optional
// lpUserName : LPWSTR 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 WNetGetUserW(
        WString lpName,   // LPCWSTR optional
        char[] lpUserName,   // LPWSTR out
        IntByReference lpnLength   // DWORD* in/out
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("mpr")]
lib LibMPR
  fun WNetGetUserW = WNetGetUserW(
    lpName : UInt16*,   # LPCWSTR optional
    lpUserName : UInt16*,   # LPWSTR 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 WNetGetUserWNative = Uint32 Function(Pointer<Utf16>, Pointer<Utf16>, Pointer<Uint32>);
typedef WNetGetUserWDart = int Function(Pointer<Utf16>, Pointer<Utf16>, Pointer<Uint32>);
final WNetGetUserW = DynamicLibrary.open('MPR.dll')
    .lookupFunction<WNetGetUserWNative, WNetGetUserWDart>('WNetGetUserW');
// lpName : LPCWSTR optional -> Pointer<Utf16>
// lpUserName : LPWSTR out -> Pointer<Utf16>
// lpnLength : DWORD* in/out -> Pointer<Uint32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function WNetGetUserW(
  lpName: PWideChar;   // LPCWSTR optional
  lpUserName: PWideChar;   // LPWSTR out
  lpnLength: Pointer   // DWORD* in/out
): DWORD; stdcall;
  external 'MPR.dll' name 'WNetGetUserW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

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

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

関連項目

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