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

NetQueryDisplayInformation

関数
ユーザーやグループの表示情報を高速に列挙する。
DLLNETAPI32.dll呼出規約winapi対応OSWindows 2000 以降

シグネチャ

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

DWORD NetQueryDisplayInformation(
    LPCWSTR ServerName,
    DWORD Level,
    DWORD Index,
    DWORD EntriesRequested,
    DWORD PreferredMaximumLength,
    DWORD* ReturnedEntryCount,
    void** SortedBuffer
);

パラメーター

名前方向説明
ServerNameLPCWSTRin関数を実行するリモートサーバーの DNS 名または NetBIOS 名を指定する定数文字列へのポインター。このパラメーターが NULL の場合、ローカルコンピューターが使用されます。
LevelDWORDin

データの情報レベルを指定します。このパラメーターには次のいずれかの値を指定できます。

意味
1
ユーザーアカウント情報を返します。SortedBuffer パラメーターは NET_DISPLAY_USER 構造体の配列を指します。
2
個々のコンピューター情報を返します。SortedBuffer パラメーターは NET_DISPLAY_MACHINE 構造体の配列を指します。
3
グループアカウント情報を返します。SortedBuffer パラメーターは NET_DISPLAY_GROUP 構造体の配列を指します。
IndexDWORDin情報を取得する最初のエントリのインデックスを指定します。最初の表示情報エントリからアカウント情報を取得するには、ゼロを指定します。詳細については、後述の「解説」セクションを参照してください。
EntriesRequestedDWORDin情報を取得するエントリの最大数を指定します。Windows 2000 以降では、 NetQueryDisplayInformation の各呼び出しで最大 100 個のオブジェクトが返されます。
PreferredMaximumLengthDWORDinSortedBuffer パラメーターで返される、システムによって割り当てられるバッファーの推奨最大サイズ (バイト単位) を指定します。このパラメーターには MAX_PREFERRED_LENGTH を設定することをお勧めします。
ReturnedEntryCountDWORD*inoutSortedBuffer パラメーターで返されるバッファー内のエントリ数を受け取る値へのポインター。このパラメーターがゼロの場合、指定されたインデックス以上のエントリは存在しません。関数の戻り値が NERR_Success または ERROR_MORE_DATA のいずれかの場合に、エントリが返されることがあります。
SortedBuffervoid**inout要求された情報のソート済みリストを格納する、システムによって割り当てられたバッファーへのポインターを受け取るバッファーへのポインター。このデータの形式は Level パラメーターの値に依存します。このバッファーはシステムによって割り当てられるため、 NetApiBufferFree 関数を使用して解放する必要があります。関数が ERROR_MORE_DATA で失敗した場合でも、バッファーを解放する必要があることに注意してください。詳細については、後述の「戻り値」セクション、および「 Network Management Function Buffers」と「 Network Management Function Buffer Lengths」のトピックを参照してください。

戻り値の型: DWORD

公式ドキュメント

NetQueryDisplayInformation 関数は、ユーザーアカウント、コンピューター、またはグループアカウントの情報を返します。ユーザーインターフェイスへの表示用にアカウント情報をすばやく列挙するには、この関数を呼び出します。

戻り値

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

関数が失敗した場合、戻り値は次のいずれかのエラーコードです。

戻りコード 説明
ERROR_ACCESS_DENIED
ユーザーが要求された情報へのアクセス権を持っていません。
ERROR_INVALID_LEVEL
Level パラメーターに無効な値が指定されています。
ERROR_MORE_DATA
さらにエントリが利用可能です。つまり、SortedBuffer パラメーターで返された最後のエントリは、利用可能な最後のエントリではありません。追加のエントリを取得するには、Index パラメーターに SortedBuffer 内の最後のエントリの next_index メンバーで返された値を設定して、 NetQueryDisplayInformation を再度呼び出します。next_index メンバーの値は、 NetQueryDisplayInformation の追加呼び出しでさらにデータを取得する目的以外には使用しないでください。

解説(Remarks)

Active Directory を実行しているドメインコントローラー上でこの関数を呼び出す場合、アクセスは セキュリティ保護可能なオブジェクト のアクセス制御リスト (ACL) に基づいて許可または拒否されます。既定の ACL では、認証されたすべてのユーザーと「Pre-Windows 2000 互換アクセス」グループのメンバーが情報を表示できます。メンバーサーバーまたはワークステーション上でこの関数を呼び出す場合、認証されたすべてのユーザーが情報を表示できます。これらのプラットフォームでの匿名アクセスおよび匿名アクセスの制限については、「 Security Requirements for the Network Management Functions」を参照してください。ACL、ACE、およびアクセストークンの詳細については、「 Access Control Model」を参照してください。

NetQueryDisplayInformation 関数は、呼び出し元が読み取りアクセス権を持つ情報のみを返します。呼び出し元は、Domain オブジェクトに対する List Contents アクセス権と、System コンテナーにある SAM Server オブジェクトに対する Enumerate Entire SAM Domain アクセス権を持っている必要があります。

NetQueryDisplayInformation 関数と NetGetDisplayInformationIndex 関数は、ユーザーアカウントとグループアカウントを効率的に列挙するメカニズムを提供します。可能な場合は、 NetUserEnum 関数や NetGroupEnum 関数の代わりに、これらの関数を使用してください。

信頼する側のドメインまたはメンバーコンピューターのアカウントを列挙するには、適切なフィルター値を指定して NetUserEnum を呼び出し、必要なアカウント情報を取得します。信頼される側のドメインを列挙するには、 LsaEnumerateTrustedDomains 関数または LsaEnumerateTrustedDomainsEx 関数を呼び出します。

この関数によって返されるエントリの数は、ルートドメインオブジェクト上のセキュリティ記述子に依存します。この API は、ユーザーのアクセス権限に応じて、最初の 100 エントリまたはドメイン内のエントリの全セットのいずれかを返します。この動作を制御するために使用される ACE は「SAM-Enumerate-Entire-Domain」であり、既定で Authenticated Users に付与されます。管理者はこの設定を変更して、ユーザーがドメイン全体を列挙できるようにすることができます。

NetQueryDisplayInformation の各呼び出しでは、最大 100 個のオブジェクトが返されます。ドメインアカウント情報を列挙するために NetQueryDisplayInformation 関数を呼び出すと、パフォーマンスの面でコストが高くなる場合があります。Active Directory 向けにプログラミングしている場合は、 IDirectorySearch インターフェイスのメソッドを使用して、ドメインに対するページ単位のクエリを実行できる可能性があります。詳細については、「 IDirectorySearch::SetSearchPreference」および「 IDirectorySearch::ExecuteSearch」を参照してください。信頼される側のドメインを列挙するには、 LsaEnumerateTrustedDomainsEx 関数を呼び出します。

次のコードサンプルは、 NetQueryDisplayInformation 関数の呼び出しを使用してグループアカウント情報を返す方法を示しています。ユーザーがサーバー名を指定した場合、サンプルはまず MultiByteToWideChar 関数を呼び出して名前を Unicode に変換します。サンプルは情報レベル 3 (NET_DISPLAY_GROUP) を指定して NetQueryDisplayInformation を呼び出し、グループアカウント情報を取得します。返すべきエントリがある場合、サンプルはデータを返してグループ情報を出力します。最後に、コードサンプルは情報バッファーに割り当てられたメモリを解放します。

#ifndef UNICODE
#define UNICODE
#endif

#include <windows.h>
#include <stdio.h>
#include <lm.h>

#pragma comment(lib, "netapi32.lib")

void main( int argc, char *argv[ ] )
{
   PNET_DISPLAY_GROUP pBuff, p;
   DWORD res, dwRec, i = 0;
   //
   // You can pass a NULL or empty string
   //  to retrieve the local information.
   //
   TCHAR szServer[255]=TEXT(""); 

   if(argc > 1) 
      //
      // Check to see if a server name was passed;
      //  if so, convert it to Unicode.
      //
      MultiByteToWideChar(CP_ACP, 0, argv[1], -1, szServer, 255); 

   do // begin do
   { 
      //
      // Call the NetQueryDisplayInformation function;
      //   specify information level 3 (group account information).
      //
      res = NetQueryDisplayInformation(szServer, 3, i, 1000, MAX_PREFERRED_LENGTH, &dwRec, (PVOID*) &pBuff);
      //
      // If the call succeeds,
      //
      if((res==ERROR_SUCCESS) || (res==ERROR_MORE_DATA))
      {
         p = pBuff;
         for(;dwRec>0;dwRec--)
         {
            //
            // Print the retrieved group information.
            //
            printf("Name:      %S\n"
                  "Comment:   %S\n"
                  "Group ID:  %u\n"
                  "Attributes: %u\n"
                  "--------------------------------\n",
                  p->grpi3_name,
                  p->grpi3_comment,
                  p->grpi3_group_id,
                  p->grpi3_attributes);
            //
            // If there is more data, set the index.
            //
            i = p->grpi3_next_index;
            p++;
         }
         //
         // Free the allocated memory.
         //
         NetApiBufferFree(pBuff);
      }
      else
         printf("Error: %u\n", res);
   //
   // Continue while there is more data.
   //
   } while (res==ERROR_MORE_DATA); // end do
   return;
}
出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

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

DWORD NetQueryDisplayInformation(
    LPCWSTR ServerName,
    DWORD Level,
    DWORD Index,
    DWORD EntriesRequested,
    DWORD PreferredMaximumLength,
    DWORD* ReturnedEntryCount,
    void** SortedBuffer
);
[DllImport("NETAPI32.dll", ExactSpelling = true)]
static extern uint NetQueryDisplayInformation(
    [MarshalAs(UnmanagedType.LPWStr)] string ServerName,   // LPCWSTR
    uint Level,   // DWORD
    uint Index,   // DWORD
    uint EntriesRequested,   // DWORD
    uint PreferredMaximumLength,   // DWORD
    ref uint ReturnedEntryCount,   // DWORD* in/out
    IntPtr SortedBuffer   // void** in/out
);
<DllImport("NETAPI32.dll", ExactSpelling:=True)>
Public Shared Function NetQueryDisplayInformation(
    <MarshalAs(UnmanagedType.LPWStr)> ServerName As String,   ' LPCWSTR
    Level As UInteger,   ' DWORD
    Index As UInteger,   ' DWORD
    EntriesRequested As UInteger,   ' DWORD
    PreferredMaximumLength As UInteger,   ' DWORD
    ByRef ReturnedEntryCount As UInteger,   ' DWORD* in/out
    SortedBuffer As IntPtr   ' void** in/out
) As UInteger
End Function
' ServerName : LPCWSTR
' Level : DWORD
' Index : DWORD
' EntriesRequested : DWORD
' PreferredMaximumLength : DWORD
' ReturnedEntryCount : DWORD* in/out
' SortedBuffer : void** in/out
Declare PtrSafe Function NetQueryDisplayInformation Lib "netapi32" ( _
    ByVal ServerName As LongPtr, _
    ByVal Level As Long, _
    ByVal Index As Long, _
    ByVal EntriesRequested As Long, _
    ByVal PreferredMaximumLength As Long, _
    ByRef ReturnedEntryCount As Long, _
    ByVal SortedBuffer As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

NetQueryDisplayInformation = ctypes.windll.netapi32.NetQueryDisplayInformation
NetQueryDisplayInformation.restype = wintypes.DWORD
NetQueryDisplayInformation.argtypes = [
    wintypes.LPCWSTR,  # ServerName : LPCWSTR
    wintypes.DWORD,  # Level : DWORD
    wintypes.DWORD,  # Index : DWORD
    wintypes.DWORD,  # EntriesRequested : DWORD
    wintypes.DWORD,  # PreferredMaximumLength : DWORD
    ctypes.POINTER(wintypes.DWORD),  # ReturnedEntryCount : DWORD* in/out
    ctypes.c_void_p,  # SortedBuffer : void** in/out
]
require 'fiddle'
require 'fiddle/import'

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

var (
	netapi32 = windows.NewLazySystemDLL("NETAPI32.dll")
	procNetQueryDisplayInformation = netapi32.NewProc("NetQueryDisplayInformation")
)

// ServerName (LPCWSTR), Level (DWORD), Index (DWORD), EntriesRequested (DWORD), PreferredMaximumLength (DWORD), ReturnedEntryCount (DWORD* in/out), SortedBuffer (void** in/out)
r1, _, err := procNetQueryDisplayInformation.Call(
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(ServerName))),
	uintptr(Level),
	uintptr(Index),
	uintptr(EntriesRequested),
	uintptr(PreferredMaximumLength),
	uintptr(ReturnedEntryCount),
	uintptr(SortedBuffer),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // DWORD
function NetQueryDisplayInformation(
  ServerName: PWideChar;   // LPCWSTR
  Level: DWORD;   // DWORD
  Index: DWORD;   // DWORD
  EntriesRequested: DWORD;   // DWORD
  PreferredMaximumLength: DWORD;   // DWORD
  ReturnedEntryCount: Pointer;   // DWORD* in/out
  SortedBuffer: Pointer   // void** in/out
): DWORD; stdcall;
  external 'NETAPI32.dll' name 'NetQueryDisplayInformation';
result := DllCall("NETAPI32\NetQueryDisplayInformation"
    , "WStr", ServerName   ; LPCWSTR
    , "UInt", Level   ; DWORD
    , "UInt", Index   ; DWORD
    , "UInt", EntriesRequested   ; DWORD
    , "UInt", PreferredMaximumLength   ; DWORD
    , "Ptr", ReturnedEntryCount   ; DWORD* in/out
    , "Ptr", SortedBuffer   ; void** in/out
    , "UInt")   ; return: DWORD
●NetQueryDisplayInformation(ServerName, Level, Index, EntriesRequested, PreferredMaximumLength, ReturnedEntryCount, SortedBuffer) = DLL("NETAPI32.dll", "dword NetQueryDisplayInformation(char*, dword, dword, dword, dword, void*, void*)")
# 呼び出し: NetQueryDisplayInformation(ServerName, Level, Index, EntriesRequested, PreferredMaximumLength, ReturnedEntryCount, SortedBuffer)
# ServerName : LPCWSTR -> "char*"
# Level : DWORD -> "dword"
# Index : DWORD -> "dword"
# EntriesRequested : DWORD -> "dword"
# PreferredMaximumLength : DWORD -> "dword"
# ReturnedEntryCount : DWORD* in/out -> "void*"
# SortedBuffer : void** in/out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "netapi32" fn NetQueryDisplayInformation(
    ServerName: [*c]const u16, // LPCWSTR
    Level: u32, // DWORD
    Index: u32, // DWORD
    EntriesRequested: u32, // DWORD
    PreferredMaximumLength: u32, // DWORD
    ReturnedEntryCount: [*c]u32, // DWORD* in/out
    SortedBuffer: ?*anyopaque // void** in/out
) callconv(std.os.windows.WINAPI) u32;
proc NetQueryDisplayInformation(
    ServerName: WideCString,  # LPCWSTR
    Level: uint32,  # DWORD
    Index: uint32,  # DWORD
    EntriesRequested: uint32,  # DWORD
    PreferredMaximumLength: uint32,  # DWORD
    ReturnedEntryCount: ptr uint32,  # DWORD* in/out
    SortedBuffer: pointer  # void** in/out
): uint32 {.importc: "NetQueryDisplayInformation", stdcall, dynlib: "NETAPI32.dll".}
pragma(lib, "netapi32");
extern(Windows)
uint NetQueryDisplayInformation(
    const(wchar)* ServerName,   // LPCWSTR
    uint Level,   // DWORD
    uint Index,   // DWORD
    uint EntriesRequested,   // DWORD
    uint PreferredMaximumLength,   // DWORD
    uint* ReturnedEntryCount,   // DWORD* in/out
    void** SortedBuffer   // void** in/out
);
ccall((:NetQueryDisplayInformation, "NETAPI32.dll"), stdcall, UInt32,
      (Cwstring, UInt32, UInt32, UInt32, UInt32, Ptr{UInt32}, Ptr{Cvoid}),
      ServerName, Level, Index, EntriesRequested, PreferredMaximumLength, ReturnedEntryCount, SortedBuffer)
# ServerName : LPCWSTR -> Cwstring
# Level : DWORD -> UInt32
# Index : DWORD -> UInt32
# EntriesRequested : DWORD -> UInt32
# PreferredMaximumLength : DWORD -> UInt32
# ReturnedEntryCount : DWORD* in/out -> Ptr{UInt32}
# SortedBuffer : void** in/out -> Ptr{Cvoid}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
uint32_t NetQueryDisplayInformation(
    const uint16_t* ServerName,
    uint32_t Level,
    uint32_t Index,
    uint32_t EntriesRequested,
    uint32_t PreferredMaximumLength,
    uint32_t* ReturnedEntryCount,
    void** SortedBuffer);
]]
local netapi32 = ffi.load("netapi32")
-- netapi32.NetQueryDisplayInformation(ServerName, Level, Index, EntriesRequested, PreferredMaximumLength, ReturnedEntryCount, SortedBuffer)
-- ServerName : LPCWSTR
-- Level : DWORD
-- Index : DWORD
-- EntriesRequested : DWORD
-- PreferredMaximumLength : DWORD
-- ReturnedEntryCount : DWORD* in/out
-- SortedBuffer : void** in/out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('NETAPI32.dll');
const NetQueryDisplayInformation = lib.func('__stdcall', 'NetQueryDisplayInformation', 'uint32_t', ['str16', 'uint32_t', 'uint32_t', 'uint32_t', 'uint32_t', 'uint32_t *', 'void *']);
// NetQueryDisplayInformation(ServerName, Level, Index, EntriesRequested, PreferredMaximumLength, ReturnedEntryCount, SortedBuffer)
// ServerName : LPCWSTR -> 'str16'
// Level : DWORD -> 'uint32_t'
// Index : DWORD -> 'uint32_t'
// EntriesRequested : DWORD -> 'uint32_t'
// PreferredMaximumLength : DWORD -> 'uint32_t'
// ReturnedEntryCount : DWORD* in/out -> 'uint32_t *'
// SortedBuffer : void** in/out -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("NETAPI32.dll", {
  NetQueryDisplayInformation: { parameters: ["buffer", "u32", "u32", "u32", "u32", "pointer", "pointer"], result: "u32" },
});
// lib.symbols.NetQueryDisplayInformation(ServerName, Level, Index, EntriesRequested, PreferredMaximumLength, ReturnedEntryCount, SortedBuffer)
// ServerName : LPCWSTR -> "buffer"
// Level : DWORD -> "u32"
// Index : DWORD -> "u32"
// EntriesRequested : DWORD -> "u32"
// PreferredMaximumLength : DWORD -> "u32"
// ReturnedEntryCount : DWORD* in/out -> "pointer"
// SortedBuffer : void** in/out -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
uint32_t NetQueryDisplayInformation(
    const uint16_t* ServerName,
    uint32_t Level,
    uint32_t Index,
    uint32_t EntriesRequested,
    uint32_t PreferredMaximumLength,
    uint32_t* ReturnedEntryCount,
    void** SortedBuffer);
C, "NETAPI32.dll");
// $ffi->NetQueryDisplayInformation(ServerName, Level, Index, EntriesRequested, PreferredMaximumLength, ReturnedEntryCount, SortedBuffer);
// ServerName : LPCWSTR
// Level : DWORD
// Index : DWORD
// EntriesRequested : DWORD
// PreferredMaximumLength : DWORD
// ReturnedEntryCount : DWORD* in/out
// SortedBuffer : void** 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 Netapi32 extends StdCallLibrary {
    Netapi32 INSTANCE = Native.load("netapi32", Netapi32.class);
    int NetQueryDisplayInformation(
        WString ServerName,   // LPCWSTR
        int Level,   // DWORD
        int Index,   // DWORD
        int EntriesRequested,   // DWORD
        int PreferredMaximumLength,   // DWORD
        IntByReference ReturnedEntryCount,   // DWORD* in/out
        Pointer SortedBuffer   // void** in/out
    );
}
@[Link("netapi32")]
lib LibNETAPI32
  fun NetQueryDisplayInformation = NetQueryDisplayInformation(
    ServerName : UInt16*,   # LPCWSTR
    Level : UInt32,   # DWORD
    Index : UInt32,   # DWORD
    EntriesRequested : UInt32,   # DWORD
    PreferredMaximumLength : UInt32,   # DWORD
    ReturnedEntryCount : UInt32*,   # DWORD* in/out
    SortedBuffer : Void**   # void** 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 NetQueryDisplayInformationNative = Uint32 Function(Pointer<Utf16>, Uint32, Uint32, Uint32, Uint32, Pointer<Uint32>, Pointer<Void>);
typedef NetQueryDisplayInformationDart = int Function(Pointer<Utf16>, int, int, int, int, Pointer<Uint32>, Pointer<Void>);
final NetQueryDisplayInformation = DynamicLibrary.open('NETAPI32.dll')
    .lookupFunction<NetQueryDisplayInformationNative, NetQueryDisplayInformationDart>('NetQueryDisplayInformation');
// ServerName : LPCWSTR -> Pointer<Utf16>
// Level : DWORD -> Uint32
// Index : DWORD -> Uint32
// EntriesRequested : DWORD -> Uint32
// PreferredMaximumLength : DWORD -> Uint32
// ReturnedEntryCount : DWORD* in/out -> Pointer<Uint32>
// SortedBuffer : void** in/out -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function NetQueryDisplayInformation(
  ServerName: PWideChar;   // LPCWSTR
  Level: DWORD;   // DWORD
  Index: DWORD;   // DWORD
  EntriesRequested: DWORD;   // DWORD
  PreferredMaximumLength: DWORD;   // DWORD
  ReturnedEntryCount: Pointer;   // DWORD* in/out
  SortedBuffer: Pointer   // void** in/out
): DWORD; stdcall;
  external 'NETAPI32.dll' name 'NetQueryDisplayInformation';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "NetQueryDisplayInformation"
  c_NetQueryDisplayInformation :: CWString -> Word32 -> Word32 -> Word32 -> Word32 -> Ptr Word32 -> Ptr () -> IO Word32
-- ServerName : LPCWSTR -> CWString
-- Level : DWORD -> Word32
-- Index : DWORD -> Word32
-- EntriesRequested : DWORD -> Word32
-- PreferredMaximumLength : DWORD -> Word32
-- ReturnedEntryCount : DWORD* in/out -> Ptr Word32
-- SortedBuffer : void** in/out -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let netquerydisplayinformation =
  foreign "NetQueryDisplayInformation"
    ((ptr uint16_t) @-> uint32_t @-> uint32_t @-> uint32_t @-> uint32_t @-> (ptr uint32_t) @-> (ptr void) @-> returning uint32_t)
(* ServerName : LPCWSTR -> (ptr uint16_t) *)
(* Level : DWORD -> uint32_t *)
(* Index : DWORD -> uint32_t *)
(* EntriesRequested : DWORD -> uint32_t *)
(* PreferredMaximumLength : DWORD -> uint32_t *)
(* ReturnedEntryCount : DWORD* in/out -> (ptr uint32_t) *)
(* SortedBuffer : void** in/out -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library netapi32 (t "NETAPI32.dll"))
(cffi:use-foreign-library netapi32)

(cffi:defcfun ("NetQueryDisplayInformation" net-query-display-information :convention :stdcall) :uint32
  (server-name (:string :encoding :utf-16le))   ; LPCWSTR
  (level :uint32)   ; DWORD
  (index :uint32)   ; DWORD
  (entries-requested :uint32)   ; DWORD
  (preferred-maximum-length :uint32)   ; DWORD
  (returned-entry-count :pointer)   ; DWORD* in/out
  (sorted-buffer :pointer))   ; void** in/out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $NetQueryDisplayInformation = Win32::API::More->new('NETAPI32',
    'DWORD NetQueryDisplayInformation(LPCWSTR ServerName, DWORD Level, DWORD Index, DWORD EntriesRequested, DWORD PreferredMaximumLength, LPVOID ReturnedEntryCount, LPVOID SortedBuffer)');
# my $ret = $NetQueryDisplayInformation->Call($ServerName, $Level, $Index, $EntriesRequested, $PreferredMaximumLength, $ReturnedEntryCount, $SortedBuffer);
# ServerName : LPCWSTR -> LPCWSTR
# Level : DWORD -> DWORD
# Index : DWORD -> DWORD
# EntriesRequested : DWORD -> DWORD
# PreferredMaximumLength : DWORD -> DWORD
# ReturnedEntryCount : DWORD* in/out -> LPVOID
# SortedBuffer : void** in/out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

公式の関連項目