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
lpUserNameLPWSTRout
lpnLengthDWORD*inout

戻り値の型: 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 版の利用を推奨。