ホーム › NetworkManagement.WNet › WNetGetUserA
WNetGetUserA
関数ネットワーク接続に使用したユーザー名を取得する(ANSI版)。
シグネチャ
// MPR.dll (ANSI / -A)
#include <windows.h>
WIN32_ERROR WNetGetUserA(
LPCSTR lpName, // optional
LPSTR lpUserName,
DWORD* lpnLength
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| lpName | LPCSTR | inoptional | 問い合わせる接続のローカルデバイス名のANSI文字列。NULLで現在のユーザー。 |
| lpUserName | LPSTR | out | 接続に使用したユーザー名を受け取るANSIバッファ。 |
| lpnLength | DWORD* | inout | lpUserNameの文字数を入出力で渡し、必要文字数を受け取る。 |
戻り値の型: WIN32_ERROR
各言語での呼び出し定義
// MPR.dll (ANSI / -A)
#include <windows.h>
WIN32_ERROR WNetGetUserA(
LPCSTR lpName, // optional
LPSTR lpUserName,
DWORD* lpnLength
);[DllImport("MPR.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
static extern uint WNetGetUserA(
[MarshalAs(UnmanagedType.LPStr)] string lpName, // LPCSTR optional
[MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder lpUserName, // LPSTR out
ref uint lpnLength // DWORD* in/out
);<DllImport("MPR.dll", CharSet:=CharSet.Ansi, ExactSpelling:=True)>
Public Shared Function WNetGetUserA(
<MarshalAs(UnmanagedType.LPStr)> lpName As String, ' LPCSTR optional
<MarshalAs(UnmanagedType.LPStr)> lpUserName As System.Text.StringBuilder, ' LPSTR out
ByRef lpnLength As UInteger ' DWORD* in/out
) As UInteger
End Function' lpName : LPCSTR optional
' lpUserName : LPSTR out
' lpnLength : DWORD* in/out
Declare PtrSafe Function WNetGetUserA Lib "mpr" ( _
ByVal lpName As String, _
ByVal lpUserName As String, _
ByRef lpnLength As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
WNetGetUserA = ctypes.windll.mpr.WNetGetUserA
WNetGetUserA.restype = wintypes.DWORD
WNetGetUserA.argtypes = [
wintypes.LPCSTR, # lpName : LPCSTR optional
wintypes.LPSTR, # lpUserName : LPSTR out
ctypes.POINTER(wintypes.DWORD), # lpnLength : DWORD* in/out
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('MPR.dll')
WNetGetUserA = Fiddle::Function.new(
lib['WNetGetUserA'],
[
Fiddle::TYPE_VOIDP, # lpName : LPCSTR optional
Fiddle::TYPE_VOIDP, # lpUserName : LPSTR out
Fiddle::TYPE_VOIDP, # lpnLength : DWORD* in/out
],
-Fiddle::TYPE_INT)#[link(name = "mpr")]
extern "system" {
fn WNetGetUserA(
lpName: *const u8, // LPCSTR optional
lpUserName: *mut u8, // LPSTR out
lpnLength: *mut u32 // DWORD* in/out
) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("MPR.dll", CharSet = CharSet.Ansi)]
public static extern uint WNetGetUserA([MarshalAs(UnmanagedType.LPStr)] string lpName, [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder lpUserName, ref uint lpnLength);
"@
$api = Add-Type -MemberDefinition $sig -Name 'MPR_WNetGetUserA' -Namespace Win32 -PassThru
# $api::WNetGetUserA(lpName, lpUserName, lpnLength)#uselib "MPR.dll"
#func global WNetGetUserA "WNetGetUserA" sptr, sptr, sptr
; WNetGetUserA lpName, varptr(lpUserName), varptr(lpnLength) ; 戻り値は stat
; lpName : LPCSTR optional -> "sptr"
; lpUserName : LPSTR out -> "sptr"
; lpnLength : DWORD* in/out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "MPR.dll" #cfunc global WNetGetUserA "WNetGetUserA" str, var, var ; res = WNetGetUserA(lpName, lpUserName, lpnLength) ; lpName : LPCSTR optional -> "str" ; lpUserName : LPSTR out -> "var" ; lpnLength : DWORD* in/out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "MPR.dll" #cfunc global WNetGetUserA "WNetGetUserA" str, sptr, sptr ; res = WNetGetUserA(lpName, varptr(lpUserName), varptr(lpnLength)) ; lpName : LPCSTR optional -> "str" ; lpUserName : LPSTR out -> "sptr" ; lpnLength : DWORD* in/out -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; WIN32_ERROR WNetGetUserA(LPCSTR lpName, LPSTR lpUserName, DWORD* lpnLength) #uselib "MPR.dll" #cfunc global WNetGetUserA "WNetGetUserA" str, var, var ; res = WNetGetUserA(lpName, lpUserName, lpnLength) ; lpName : LPCSTR optional -> "str" ; lpUserName : LPSTR out -> "var" ; lpnLength : DWORD* in/out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; WIN32_ERROR WNetGetUserA(LPCSTR lpName, LPSTR lpUserName, DWORD* lpnLength) #uselib "MPR.dll" #cfunc global WNetGetUserA "WNetGetUserA" str, intptr, intptr ; res = WNetGetUserA(lpName, varptr(lpUserName), varptr(lpnLength)) ; lpName : LPCSTR optional -> "str" ; lpUserName : LPSTR out -> "intptr" ; lpnLength : DWORD* in/out -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
mpr = windows.NewLazySystemDLL("MPR.dll")
procWNetGetUserA = mpr.NewProc("WNetGetUserA")
)
// lpName (LPCSTR optional), lpUserName (LPSTR out), lpnLength (DWORD* in/out)
r1, _, err := procWNetGetUserA.Call(
uintptr(unsafe.Pointer(windows.BytePtrFromString(lpName))),
uintptr(lpUserName),
uintptr(lpnLength),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // WIN32_ERRORfunction WNetGetUserA(
lpName: PAnsiChar; // LPCSTR optional
lpUserName: PAnsiChar; // LPSTR out
lpnLength: Pointer // DWORD* in/out
): DWORD; stdcall;
external 'MPR.dll' name 'WNetGetUserA';result := DllCall("MPR\WNetGetUserA"
, "AStr", lpName ; LPCSTR optional
, "Ptr", lpUserName ; LPSTR out
, "Ptr", lpnLength ; DWORD* in/out
, "UInt") ; return: WIN32_ERROR●WNetGetUserA(lpName, lpUserName, lpnLength) = DLL("MPR.dll", "dword WNetGetUserA(char*, char*, void*)")
# 呼び出し: WNetGetUserA(lpName, lpUserName, lpnLength)
# lpName : LPCSTR optional -> "char*"
# lpUserName : LPSTR out -> "char*"
# lpnLength : DWORD* in/out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "mpr" fn WNetGetUserA(
lpName: [*c]const u8, // LPCSTR optional
lpUserName: [*c]u8, // LPSTR out
lpnLength: [*c]u32 // DWORD* in/out
) callconv(std.os.windows.WINAPI) u32;proc WNetGetUserA(
lpName: cstring, # LPCSTR optional
lpUserName: ptr char, # LPSTR out
lpnLength: ptr uint32 # DWORD* in/out
): uint32 {.importc: "WNetGetUserA", stdcall, dynlib: "MPR.dll".}pragma(lib, "mpr");
extern(Windows)
uint WNetGetUserA(
const(char)* lpName, // LPCSTR optional
char* lpUserName, // LPSTR out
uint* lpnLength // DWORD* in/out
);ccall((:WNetGetUserA, "MPR.dll"), stdcall, UInt32,
(Cstring, Ptr{UInt8}, Ptr{UInt32}),
lpName, lpUserName, lpnLength)
# lpName : LPCSTR optional -> Cstring
# lpUserName : LPSTR out -> Ptr{UInt8}
# lpnLength : DWORD* in/out -> Ptr{UInt32}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
uint32_t WNetGetUserA(
const char* lpName,
char* lpUserName,
uint32_t* lpnLength);
]]
local mpr = ffi.load("mpr")
-- mpr.WNetGetUserA(lpName, lpUserName, lpnLength)
-- lpName : LPCSTR optional
-- lpUserName : LPSTR out
-- lpnLength : DWORD* in/out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('MPR.dll');
const WNetGetUserA = lib.func('__stdcall', 'WNetGetUserA', 'uint32_t', ['str', 'char *', 'uint32_t *']);
// WNetGetUserA(lpName, lpUserName, lpnLength)
// lpName : LPCSTR optional -> 'str'
// lpUserName : LPSTR out -> 'char *'
// lpnLength : DWORD* in/out -> 'uint32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("MPR.dll", {
WNetGetUserA: { parameters: ["buffer", "buffer", "pointer"], result: "u32" },
});
// lib.symbols.WNetGetUserA(lpName, lpUserName, lpnLength)
// lpName : LPCSTR optional -> "buffer"
// lpUserName : LPSTR out -> "buffer"
// lpnLength : DWORD* in/out -> "pointer"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
uint32_t WNetGetUserA(
const char* lpName,
char* lpUserName,
uint32_t* lpnLength);
C, "MPR.dll");
// $ffi->WNetGetUserA(lpName, lpUserName, lpnLength);
// lpName : LPCSTR optional
// lpUserName : LPSTR 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.ASCII_OPTIONS);
int WNetGetUserA(
String lpName, // LPCSTR optional
byte[] lpUserName, // LPSTR out
IntByReference lpnLength // DWORD* in/out
);
}@[Link("mpr")]
lib LibMPR
fun WNetGetUserA = WNetGetUserA(
lpName : UInt8*, # LPCSTR optional
lpUserName : UInt8*, # LPSTR 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 WNetGetUserANative = Uint32 Function(Pointer<Utf8>, Pointer<Utf8>, Pointer<Uint32>);
typedef WNetGetUserADart = int Function(Pointer<Utf8>, Pointer<Utf8>, Pointer<Uint32>);
final WNetGetUserA = DynamicLibrary.open('MPR.dll')
.lookupFunction<WNetGetUserANative, WNetGetUserADart>('WNetGetUserA');
// lpName : LPCSTR optional -> Pointer<Utf8>
// lpUserName : LPSTR out -> Pointer<Utf8>
// lpnLength : DWORD* in/out -> Pointer<Uint32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function WNetGetUserA(
lpName: PAnsiChar; // LPCSTR optional
lpUserName: PAnsiChar; // LPSTR out
lpnLength: Pointer // DWORD* in/out
): DWORD; stdcall;
external 'MPR.dll' name 'WNetGetUserA';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "WNetGetUserA"
c_WNetGetUserA :: CString -> CString -> Ptr Word32 -> IO Word32
-- lpName : LPCSTR optional -> CString
-- lpUserName : LPSTR out -> CString
-- lpnLength : DWORD* in/out -> Ptr Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let wnetgetusera =
foreign "WNetGetUserA"
(string @-> string @-> (ptr uint32_t) @-> returning uint32_t)
(* lpName : LPCSTR optional -> string *)
(* lpUserName : LPSTR out -> string *)
(* 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 ("WNetGetUserA" wnet-get-user-a :convention :stdcall) :uint32
(lp-name :string) ; LPCSTR optional
(lp-user-name :pointer) ; LPSTR out
(lpn-length :pointer)) ; DWORD* in/out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $WNetGetUserA = Win32::API::More->new('MPR',
'DWORD WNetGetUserA(LPCSTR lpName, LPSTR lpUserName, LPVOID lpnLength)');
# my $ret = $WNetGetUserA->Call($lpName, $lpUserName, $lpnLength);
# lpName : LPCSTR optional -> LPCSTR
# lpUserName : LPSTR out -> LPSTR
# lpnLength : DWORD* in/out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
文字セット違い
- f WNetGetUserW (Unicode版) — ネットワーク接続に使用したユーザー名を取得する(Unicode版)。
使用する型