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

NetEnumerateComputerNames

関数
コンピューターに設定された名前の一覧を列挙する。
DLLNETAPI32.dll呼出規約winapi対応OSWindows XP 以降

シグネチャ

// NETAPI32.dll
#include <windows.h>

DWORD NetEnumerateComputerNames(
    LPCWSTR Server,   // optional
    NET_COMPUTER_NAME_TYPE NameType,
    DWORD Reserved,
    DWORD* EntryCount,
    LPWSTR** ComputerNames
);

パラメーター

名前方向説明
ServerLPCWSTRinoptionalこの関数を実行するコンピューターの名前を指定する定数文字列へのポインター。このパラメーターが NULL の場合、ローカルコンピューターが使用されます。
NameTypeNET_COMPUTER_NAME_TYPEin

照会する名前の種類。このメンバーには、Lmjoin.h ヘッダーファイルで定義された NET_COMPUTER_NAME_TYPE 列挙体で定義されている次のいずれかの値を指定できます。

意味
NetPrimaryComputerName
プライマリコンピューター名。
NetAlternateComputerNames
代替コンピューター名。
NetAllComputerNames
すべてのコンピューター名。
NetComputerNameTypeMax
照会する名前の種類として指定可能な値の範囲の終端を示します。
ReservedDWORDin将来の使用のために予約されています。 このパラメーターは NULL にする必要があります。
EntryCountDWORD*out関数が成功した場合に、ComputerNames パラメーターが指すバッファーに返される名前の数を返す DWORD 値へのポインター。
ComputerNamesLPWSTR**out

名前へのポインターの配列へのポインター。関数呼び出しが成功すると、このパラメーターは NameType パラメーターで指定したコンピューターの種類名に一致するコンピューター名を返します。

アプリケーションがこの配列を必要としなくなったら、NetApiBufferFree 関数を呼び出してこのバッファーを解放する必要があります。

戻り値の型: DWORD

公式ドキュメント

指定したコンピューターの名前を列挙します。

戻り値

関数が成功した場合、戻り値は NERR_Success です。

関数が失敗した場合、戻り値は次のいずれかのエラーコード、または システムエラーコードのいずれかになります。

戻りコード 説明
ERROR_ACCESS_DENIED
アクセスが拒否されました。このエラーは、呼び出し元が対象コンピューターの Administrators ローカルグループのメンバーでなかった場合に返されます。
ERROR_INVALID_PARAMETER
パラメーターが正しくありません。
ERROR_NOT_ENOUGH_MEMORY
このコマンドを処理するのに十分なメモリがありません。
ERROR_NOT_SUPPORTED
要求はサポートされていません。このエラーは、この関数を実行する Server パラメーターで指定した対象コンピューターが Windows 2000 以前で動作している場合に返されます。
NERR_WkstaNotStarted
Workstation サービスが開始されていません。
RPC_S_CALL_IN_PROGRESS
このスレッドではリモートプロシージャ呼び出しが既に進行中です。
RPC_S_PROTSEQ_NOT_SUPPORTED
リモートプロシージャ呼び出しのプロトコルシーケンスはサポートされていません。

解説(Remarks)

NetEnumerateComputerNames 関数は Windows Vista 以降でサポートされています。

NetEnumerateComputerNames 関数は、コンピューターに現在構成されている名前を要求するために使用します。

NetEnumerateComputerNames 関数では、呼び出し元が対象コンピューターの Administrators ローカルグループのメンバーである必要があります。

出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

// NETAPI32.dll
#include <windows.h>

DWORD NetEnumerateComputerNames(
    LPCWSTR Server,   // optional
    NET_COMPUTER_NAME_TYPE NameType,
    DWORD Reserved,
    DWORD* EntryCount,
    LPWSTR** ComputerNames
);
[DllImport("NETAPI32.dll", ExactSpelling = true)]
static extern uint NetEnumerateComputerNames(
    [MarshalAs(UnmanagedType.LPWStr)] string Server,   // LPCWSTR optional
    int NameType,   // NET_COMPUTER_NAME_TYPE
    uint Reserved,   // DWORD
    out uint EntryCount,   // DWORD* out
    IntPtr ComputerNames   // LPWSTR** out
);
<DllImport("NETAPI32.dll", ExactSpelling:=True)>
Public Shared Function NetEnumerateComputerNames(
    <MarshalAs(UnmanagedType.LPWStr)> Server As String,   ' LPCWSTR optional
    NameType As Integer,   ' NET_COMPUTER_NAME_TYPE
    Reserved As UInteger,   ' DWORD
    <Out> ByRef EntryCount As UInteger,   ' DWORD* out
    ComputerNames As IntPtr   ' LPWSTR** out
) As UInteger
End Function
' Server : LPCWSTR optional
' NameType : NET_COMPUTER_NAME_TYPE
' Reserved : DWORD
' EntryCount : DWORD* out
' ComputerNames : LPWSTR** out
Declare PtrSafe Function NetEnumerateComputerNames Lib "netapi32" ( _
    ByVal Server As LongPtr, _
    ByVal NameType As Long, _
    ByVal Reserved As Long, _
    ByRef EntryCount As Long, _
    ByVal ComputerNames As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

NetEnumerateComputerNames = ctypes.windll.netapi32.NetEnumerateComputerNames
NetEnumerateComputerNames.restype = wintypes.DWORD
NetEnumerateComputerNames.argtypes = [
    wintypes.LPCWSTR,  # Server : LPCWSTR optional
    ctypes.c_int,  # NameType : NET_COMPUTER_NAME_TYPE
    wintypes.DWORD,  # Reserved : DWORD
    ctypes.POINTER(wintypes.DWORD),  # EntryCount : DWORD* out
    ctypes.c_void_p,  # ComputerNames : LPWSTR** out
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('NETAPI32.dll')
NetEnumerateComputerNames = Fiddle::Function.new(
  lib['NetEnumerateComputerNames'],
  [
    Fiddle::TYPE_VOIDP,  # Server : LPCWSTR optional
    Fiddle::TYPE_INT,  # NameType : NET_COMPUTER_NAME_TYPE
    -Fiddle::TYPE_INT,  # Reserved : DWORD
    Fiddle::TYPE_VOIDP,  # EntryCount : DWORD* out
    Fiddle::TYPE_VOIDP,  # ComputerNames : LPWSTR** out
  ],
  -Fiddle::TYPE_INT)
#[link(name = "netapi32")]
extern "system" {
    fn NetEnumerateComputerNames(
        Server: *const u16,  // LPCWSTR optional
        NameType: i32,  // NET_COMPUTER_NAME_TYPE
        Reserved: u32,  // DWORD
        EntryCount: *mut u32,  // DWORD* out
        ComputerNames: *mut *mut *mut u16  // LPWSTR** out
    ) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("NETAPI32.dll")]
public static extern uint NetEnumerateComputerNames([MarshalAs(UnmanagedType.LPWStr)] string Server, int NameType, uint Reserved, out uint EntryCount, IntPtr ComputerNames);
"@
$api = Add-Type -MemberDefinition $sig -Name 'NETAPI32_NetEnumerateComputerNames' -Namespace Win32 -PassThru
# $api::NetEnumerateComputerNames(Server, NameType, Reserved, EntryCount, ComputerNames)
#uselib "NETAPI32.dll"
#func global NetEnumerateComputerNames "NetEnumerateComputerNames" sptr, sptr, sptr, sptr, sptr
; NetEnumerateComputerNames Server, NameType, Reserved, varptr(EntryCount), varptr(ComputerNames)   ; 戻り値は stat
; Server : LPCWSTR optional -> "sptr"
; NameType : NET_COMPUTER_NAME_TYPE -> "sptr"
; Reserved : DWORD -> "sptr"
; EntryCount : DWORD* out -> "sptr"
; ComputerNames : LPWSTR** out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "NETAPI32.dll"
#cfunc global NetEnumerateComputerNames "NetEnumerateComputerNames" wstr, int, int, var, var
; res = NetEnumerateComputerNames(Server, NameType, Reserved, EntryCount, ComputerNames)
; Server : LPCWSTR optional -> "wstr"
; NameType : NET_COMPUTER_NAME_TYPE -> "int"
; Reserved : DWORD -> "int"
; EntryCount : DWORD* out -> "var"
; ComputerNames : LPWSTR** out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; DWORD NetEnumerateComputerNames(LPCWSTR Server, NET_COMPUTER_NAME_TYPE NameType, DWORD Reserved, DWORD* EntryCount, LPWSTR** ComputerNames)
#uselib "NETAPI32.dll"
#cfunc global NetEnumerateComputerNames "NetEnumerateComputerNames" wstr, int, int, var, var
; res = NetEnumerateComputerNames(Server, NameType, Reserved, EntryCount, ComputerNames)
; Server : LPCWSTR optional -> "wstr"
; NameType : NET_COMPUTER_NAME_TYPE -> "int"
; Reserved : DWORD -> "int"
; EntryCount : DWORD* out -> "var"
; ComputerNames : LPWSTR** out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	netapi32 = windows.NewLazySystemDLL("NETAPI32.dll")
	procNetEnumerateComputerNames = netapi32.NewProc("NetEnumerateComputerNames")
)

// Server (LPCWSTR optional), NameType (NET_COMPUTER_NAME_TYPE), Reserved (DWORD), EntryCount (DWORD* out), ComputerNames (LPWSTR** out)
r1, _, err := procNetEnumerateComputerNames.Call(
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(Server))),
	uintptr(NameType),
	uintptr(Reserved),
	uintptr(EntryCount),
	uintptr(ComputerNames),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // DWORD
function NetEnumerateComputerNames(
  Server: PWideChar;   // LPCWSTR optional
  NameType: Integer;   // NET_COMPUTER_NAME_TYPE
  Reserved: DWORD;   // DWORD
  EntryCount: Pointer;   // DWORD* out
  ComputerNames: PPWideChar   // LPWSTR** out
): DWORD; stdcall;
  external 'NETAPI32.dll' name 'NetEnumerateComputerNames';
result := DllCall("NETAPI32\NetEnumerateComputerNames"
    , "WStr", Server   ; LPCWSTR optional
    , "Int", NameType   ; NET_COMPUTER_NAME_TYPE
    , "UInt", Reserved   ; DWORD
    , "Ptr", EntryCount   ; DWORD* out
    , "Ptr", ComputerNames   ; LPWSTR** out
    , "UInt")   ; return: DWORD
●NetEnumerateComputerNames(Server, NameType, Reserved, EntryCount, ComputerNames) = DLL("NETAPI32.dll", "dword NetEnumerateComputerNames(char*, int, dword, void*, void*)")
# 呼び出し: NetEnumerateComputerNames(Server, NameType, Reserved, EntryCount, ComputerNames)
# Server : LPCWSTR optional -> "char*"
# NameType : NET_COMPUTER_NAME_TYPE -> "int"
# Reserved : DWORD -> "dword"
# EntryCount : DWORD* out -> "void*"
# ComputerNames : LPWSTR** out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "netapi32" fn NetEnumerateComputerNames(
    Server: [*c]const u16, // LPCWSTR optional
    NameType: i32, // NET_COMPUTER_NAME_TYPE
    Reserved: u32, // DWORD
    EntryCount: [*c]u32, // DWORD* out
    ComputerNames: [*c][*c][*c]u16 // LPWSTR** out
) callconv(std.os.windows.WINAPI) u32;
proc NetEnumerateComputerNames(
    Server: WideCString,  # LPCWSTR optional
    NameType: int32,  # NET_COMPUTER_NAME_TYPE
    Reserved: uint32,  # DWORD
    EntryCount: ptr uint32,  # DWORD* out
    ComputerNames: ptr WideCString  # LPWSTR** out
): uint32 {.importc: "NetEnumerateComputerNames", stdcall, dynlib: "NETAPI32.dll".}
pragma(lib, "netapi32");
extern(Windows)
uint NetEnumerateComputerNames(
    const(wchar)* Server,   // LPCWSTR optional
    int NameType,   // NET_COMPUTER_NAME_TYPE
    uint Reserved,   // DWORD
    uint* EntryCount,   // DWORD* out
    wchar*** ComputerNames   // LPWSTR** out
);
ccall((:NetEnumerateComputerNames, "NETAPI32.dll"), stdcall, UInt32,
      (Cwstring, Int32, UInt32, Ptr{UInt32}, Ptr{Ptr{UInt16}}),
      Server, NameType, Reserved, EntryCount, ComputerNames)
# Server : LPCWSTR optional -> Cwstring
# NameType : NET_COMPUTER_NAME_TYPE -> Int32
# Reserved : DWORD -> UInt32
# EntryCount : DWORD* out -> Ptr{UInt32}
# ComputerNames : LPWSTR** out -> Ptr{Ptr{UInt16}}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
uint32_t NetEnumerateComputerNames(
    const uint16_t* Server,
    int32_t NameType,
    uint32_t Reserved,
    uint32_t* EntryCount,
    uint16_t*** ComputerNames);
]]
local netapi32 = ffi.load("netapi32")
-- netapi32.NetEnumerateComputerNames(Server, NameType, Reserved, EntryCount, ComputerNames)
-- Server : LPCWSTR optional
-- NameType : NET_COMPUTER_NAME_TYPE
-- Reserved : DWORD
-- EntryCount : DWORD* out
-- ComputerNames : LPWSTR** out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('NETAPI32.dll');
const NetEnumerateComputerNames = lib.func('__stdcall', 'NetEnumerateComputerNames', 'uint32_t', ['str16', 'int32_t', 'uint32_t', 'uint32_t *', 'void *']);
// NetEnumerateComputerNames(Server, NameType, Reserved, EntryCount, ComputerNames)
// Server : LPCWSTR optional -> 'str16'
// NameType : NET_COMPUTER_NAME_TYPE -> 'int32_t'
// Reserved : DWORD -> 'uint32_t'
// EntryCount : DWORD* out -> 'uint32_t *'
// ComputerNames : LPWSTR** out -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("NETAPI32.dll", {
  NetEnumerateComputerNames: { parameters: ["buffer", "i32", "u32", "pointer", "pointer"], result: "u32" },
});
// lib.symbols.NetEnumerateComputerNames(Server, NameType, Reserved, EntryCount, ComputerNames)
// Server : LPCWSTR optional -> "buffer"
// NameType : NET_COMPUTER_NAME_TYPE -> "i32"
// Reserved : DWORD -> "u32"
// EntryCount : DWORD* out -> "pointer"
// ComputerNames : LPWSTR** out -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
uint32_t NetEnumerateComputerNames(
    const uint16_t* Server,
    int32_t NameType,
    uint32_t Reserved,
    uint32_t* EntryCount,
    uint16_t*** ComputerNames);
C, "NETAPI32.dll");
// $ffi->NetEnumerateComputerNames(Server, NameType, Reserved, EntryCount, ComputerNames);
// Server : LPCWSTR optional
// NameType : NET_COMPUTER_NAME_TYPE
// Reserved : DWORD
// EntryCount : DWORD* out
// ComputerNames : LPWSTR** 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 Netapi32 extends StdCallLibrary {
    Netapi32 INSTANCE = Native.load("netapi32", Netapi32.class);
    int NetEnumerateComputerNames(
        WString Server,   // LPCWSTR optional
        int NameType,   // NET_COMPUTER_NAME_TYPE
        int Reserved,   // DWORD
        IntByReference EntryCount,   // DWORD* out
        PointerByReference ComputerNames   // LPWSTR** out
    );
}
@[Link("netapi32")]
lib LibNETAPI32
  fun NetEnumerateComputerNames = NetEnumerateComputerNames(
    Server : UInt16*,   # LPCWSTR optional
    NameType : Int32,   # NET_COMPUTER_NAME_TYPE
    Reserved : UInt32,   # DWORD
    EntryCount : UInt32*,   # DWORD* out
    ComputerNames : UInt16***   # LPWSTR** out
  ) : UInt32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef NetEnumerateComputerNamesNative = Uint32 Function(Pointer<Utf16>, Int32, Uint32, Pointer<Uint32>, Pointer<Pointer<Utf16>>);
typedef NetEnumerateComputerNamesDart = int Function(Pointer<Utf16>, int, int, Pointer<Uint32>, Pointer<Pointer<Utf16>>);
final NetEnumerateComputerNames = DynamicLibrary.open('NETAPI32.dll')
    .lookupFunction<NetEnumerateComputerNamesNative, NetEnumerateComputerNamesDart>('NetEnumerateComputerNames');
// Server : LPCWSTR optional -> Pointer<Utf16>
// NameType : NET_COMPUTER_NAME_TYPE -> Int32
// Reserved : DWORD -> Uint32
// EntryCount : DWORD* out -> Pointer<Uint32>
// ComputerNames : LPWSTR** out -> Pointer<Pointer<Utf16>>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function NetEnumerateComputerNames(
  Server: PWideChar;   // LPCWSTR optional
  NameType: Integer;   // NET_COMPUTER_NAME_TYPE
  Reserved: DWORD;   // DWORD
  EntryCount: Pointer;   // DWORD* out
  ComputerNames: PPWideChar   // LPWSTR** out
): DWORD; stdcall;
  external 'NETAPI32.dll' name 'NetEnumerateComputerNames';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "NetEnumerateComputerNames"
  c_NetEnumerateComputerNames :: CWString -> Int32 -> Word32 -> Ptr Word32 -> Ptr CWString -> IO Word32
-- Server : LPCWSTR optional -> CWString
-- NameType : NET_COMPUTER_NAME_TYPE -> Int32
-- Reserved : DWORD -> Word32
-- EntryCount : DWORD* out -> Ptr Word32
-- ComputerNames : LPWSTR** out -> Ptr CWString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let netenumeratecomputernames =
  foreign "NetEnumerateComputerNames"
    ((ptr uint16_t) @-> int32_t @-> uint32_t @-> (ptr uint32_t) @-> (ptr (ptr uint16_t)) @-> returning uint32_t)
(* Server : LPCWSTR optional -> (ptr uint16_t) *)
(* NameType : NET_COMPUTER_NAME_TYPE -> int32_t *)
(* Reserved : DWORD -> uint32_t *)
(* EntryCount : DWORD* out -> (ptr uint32_t) *)
(* ComputerNames : LPWSTR** out -> (ptr (ptr uint16_t)) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library netapi32 (t "NETAPI32.dll"))
(cffi:use-foreign-library netapi32)

(cffi:defcfun ("NetEnumerateComputerNames" net-enumerate-computer-names :convention :stdcall) :uint32
  (server (:string :encoding :utf-16le))   ; LPCWSTR optional
  (name-type :int32)   ; NET_COMPUTER_NAME_TYPE
  (reserved :uint32)   ; DWORD
  (entry-count :pointer)   ; DWORD* out
  (computer-names :pointer))   ; LPWSTR** out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $NetEnumerateComputerNames = Win32::API::More->new('NETAPI32',
    'DWORD NetEnumerateComputerNames(LPCWSTR Server, int NameType, DWORD Reserved, LPVOID EntryCount, LPVOID ComputerNames)');
# my $ret = $NetEnumerateComputerNames->Call($Server, $NameType, $Reserved, $EntryCount, $ComputerNames);
# Server : LPCWSTR optional -> LPCWSTR
# NameType : NET_COMPUTER_NAME_TYPE -> int
# Reserved : DWORD -> DWORD
# EntryCount : DWORD* out -> LPVOID
# ComputerNames : LPWSTR** out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

公式の関連項目
使用する型