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

NetServiceGetInfo

関数
指定したネットワークサービスの情報を取得する。
DLLNETAPI32.dll呼出規約winapi

シグネチャ

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

DWORD NetServiceGetInfo(
    LPCWSTR servername,   // optional
    LPCWSTR service,
    DWORD level,
    BYTE** bufptr
);

パラメーター

名前方向説明
servernameLPCWSTRinoptional操作対象サーバー名。NULLでローカルコンピュータを指す。
serviceLPCWSTRin情報を取得するネットワークサービス名。
levelDWORDin返す情報レベル(SERVICE_INFO_0/1/2)。
bufptrBYTE**out確保された結果バッファへのポインタを受け取る出力引数。NetApiBufferFreeで解放する。

戻り値の型: DWORD

各言語での呼び出し定義

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

DWORD NetServiceGetInfo(
    LPCWSTR servername,   // optional
    LPCWSTR service,
    DWORD level,
    BYTE** bufptr
);
[DllImport("NETAPI32.dll", ExactSpelling = true)]
static extern uint NetServiceGetInfo(
    [MarshalAs(UnmanagedType.LPWStr)] string servername,   // LPCWSTR optional
    [MarshalAs(UnmanagedType.LPWStr)] string service,   // LPCWSTR
    uint level,   // DWORD
    IntPtr bufptr   // BYTE** out
);
<DllImport("NETAPI32.dll", ExactSpelling:=True)>
Public Shared Function NetServiceGetInfo(
    <MarshalAs(UnmanagedType.LPWStr)> servername As String,   ' LPCWSTR optional
    <MarshalAs(UnmanagedType.LPWStr)> service As String,   ' LPCWSTR
    level As UInteger,   ' DWORD
    bufptr As IntPtr   ' BYTE** out
) As UInteger
End Function
' servername : LPCWSTR optional
' service : LPCWSTR
' level : DWORD
' bufptr : BYTE** out
Declare PtrSafe Function NetServiceGetInfo Lib "netapi32" ( _
    ByVal servername As LongPtr, _
    ByVal service As LongPtr, _
    ByVal level As Long, _
    ByVal bufptr As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

NetServiceGetInfo = ctypes.windll.netapi32.NetServiceGetInfo
NetServiceGetInfo.restype = wintypes.DWORD
NetServiceGetInfo.argtypes = [
    wintypes.LPCWSTR,  # servername : LPCWSTR optional
    wintypes.LPCWSTR,  # service : LPCWSTR
    wintypes.DWORD,  # level : DWORD
    ctypes.c_void_p,  # bufptr : BYTE** out
]
require 'fiddle'
require 'fiddle/import'

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

var (
	netapi32 = windows.NewLazySystemDLL("NETAPI32.dll")
	procNetServiceGetInfo = netapi32.NewProc("NetServiceGetInfo")
)

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

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

typedef NetServiceGetInfoNative = Uint32 Function(Pointer<Utf16>, Pointer<Utf16>, Uint32, Pointer<Uint8>);
typedef NetServiceGetInfoDart = int Function(Pointer<Utf16>, Pointer<Utf16>, int, Pointer<Uint8>);
final NetServiceGetInfo = DynamicLibrary.open('NETAPI32.dll')
    .lookupFunction<NetServiceGetInfoNative, NetServiceGetInfoDart>('NetServiceGetInfo');
// servername : LPCWSTR optional -> Pointer<Utf16>
// service : LPCWSTR -> Pointer<Utf16>
// level : DWORD -> Uint32
// bufptr : BYTE** out -> Pointer<Uint8>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function NetServiceGetInfo(
  servername: PWideChar;   // LPCWSTR optional
  service: PWideChar;   // LPCWSTR
  level: DWORD;   // DWORD
  bufptr: Pointer   // BYTE** out
): DWORD; stdcall;
  external 'NETAPI32.dll' name 'NetServiceGetInfo';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "NetServiceGetInfo"
  c_NetServiceGetInfo :: CWString -> CWString -> Word32 -> Ptr Word8 -> IO Word32
-- servername : LPCWSTR optional -> CWString
-- service : LPCWSTR -> CWString
-- level : DWORD -> Word32
-- bufptr : BYTE** out -> Ptr Word8
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let netservicegetinfo =
  foreign "NetServiceGetInfo"
    ((ptr uint16_t) @-> (ptr uint16_t) @-> uint32_t @-> (ptr uint8_t) @-> returning uint32_t)
(* servername : LPCWSTR optional -> (ptr uint16_t) *)
(* service : LPCWSTR -> (ptr uint16_t) *)
(* level : DWORD -> uint32_t *)
(* bufptr : BYTE** out -> (ptr uint8_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library netapi32 (t "NETAPI32.dll"))
(cffi:use-foreign-library netapi32)

(cffi:defcfun ("NetServiceGetInfo" net-service-get-info :convention :stdcall) :uint32
  (servername (:string :encoding :utf-16le))   ; LPCWSTR optional
  (service (:string :encoding :utf-16le))   ; LPCWSTR
  (level :uint32)   ; DWORD
  (bufptr :pointer))   ; BYTE** out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $NetServiceGetInfo = Win32::API::More->new('NETAPI32',
    'DWORD NetServiceGetInfo(LPCWSTR servername, LPCWSTR service, DWORD level, LPVOID bufptr)');
# my $ret = $NetServiceGetInfo->Call($servername, $service, $level, $bufptr);
# servername : LPCWSTR optional -> LPCWSTR
# service : LPCWSTR -> LPCWSTR
# level : DWORD -> DWORD
# bufptr : BYTE** out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。