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

NetApiBufferFree

関数
ネットワークAPIが割り当てたメモリバッファを解放する。
DLLNETAPI32.dll呼出規約winapi対応OSWindows 2000 以降

シグネチャ

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

DWORD NetApiBufferFree(
    void* Buffer   // optional
);

パラメーター

名前方向説明
Buffervoid*inoptional別のネットワーク管理関数によって以前に返されたバッファ、または NetApiBufferAllocate 関数の呼び出しによって割り当てられたメモリへのポインタです。

戻り値の型: DWORD

公式ドキュメント

NetApiBufferFree 関数は、NetApiBufferAllocate 関数によって割り当てられたメモリを解放します。また、アプリケーションは、他のネットワーク管理関数が情報を返すために内部的に使用したメモリを解放するためにも NetApiBufferFree を呼び出す必要があります。

戻り値

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

関数が失敗した場合、戻り値はシステムエラーコードです。エラーコードの一覧については、 System Error Codes を参照してください。

解説(Remarks)

NetApiBufferFree 関数は、ネットワーク管理関数が使用したメモリを解放するために使用します。この関数は、次の 2 つのケースで使用されます。

多くのネットワーク管理関数は情報を取得し、その情報を、複雑な構造体、構造体の配列、またはネストされた構造体の配列を含む可能性のあるバッファとして返します。これらの関数は、ローカルコンピュータへの呼び出しかリモートサーバーへの呼び出しかにかかわらず、返却情報を格納するバッファを RPC ランタイムライブラリを使用して内部的に割り当てます。たとえば、NetServerEnum 関数はサーバーの一覧を取得し、その情報を bufptr パラメータが指す構造体の配列として返します。関数が成功すると、bufptr パラメータでアプリケーションに返される構造体の配列を格納するために、NetServerEnum 関数によって内部的にメモリが割り当てられます。この構造体の配列が不要になったときは、NetServerEnum によって返された bufptr パラメータを Buffer パラメータに設定して、アプリケーションから NetApiBufferFree 関数を呼び出し、使用された内部メモリを解放する必要があります。これらのケースでは、NetApiBufferFree 関数は、ネストされた構造体、文字列へのポインタ、その他のデータのためのメモリを含め、バッファに割り当てられたすべての内部メモリを解放します。

NetApiBufferFree 関数、またはその他の ApiBuffer 関数 を正常に実行するために、特別なグループメンバーシップは必要ありません。

アプリケーションによって明示的に割り当てられたメモリを解放するために NetApiBufferFree 関数を使用する方法を示すコードサンプルについては、 NetApiBufferAllocate 関数を参照してください。

ネットワーク管理関数が情報を返すために内部的に割り当てたメモリを解放するために NetApiBufferFree 関数を使用する方法を示すコードサンプルについては、 NetServerEnum 関数を参照してください。

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

各言語での呼び出し定義

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

DWORD NetApiBufferFree(
    void* Buffer   // optional
);
[DllImport("NETAPI32.dll", ExactSpelling = true)]
static extern uint NetApiBufferFree(
    IntPtr Buffer   // void* optional
);
<DllImport("NETAPI32.dll", ExactSpelling:=True)>
Public Shared Function NetApiBufferFree(
    Buffer As IntPtr   ' void* optional
) As UInteger
End Function
' Buffer : void* optional
Declare PtrSafe Function NetApiBufferFree Lib "netapi32" ( _
    ByVal Buffer As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

NetApiBufferFree = ctypes.windll.netapi32.NetApiBufferFree
NetApiBufferFree.restype = wintypes.DWORD
NetApiBufferFree.argtypes = [
    ctypes.POINTER(None),  # Buffer : void* optional
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('NETAPI32.dll')
NetApiBufferFree = Fiddle::Function.new(
  lib['NetApiBufferFree'],
  [
    Fiddle::TYPE_VOIDP,  # Buffer : void* optional
  ],
  -Fiddle::TYPE_INT)
#[link(name = "netapi32")]
extern "system" {
    fn NetApiBufferFree(
        Buffer: *mut ()  // void* optional
    ) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("NETAPI32.dll")]
public static extern uint NetApiBufferFree(IntPtr Buffer);
"@
$api = Add-Type -MemberDefinition $sig -Name 'NETAPI32_NetApiBufferFree' -Namespace Win32 -PassThru
# $api::NetApiBufferFree(Buffer)
#uselib "NETAPI32.dll"
#func global NetApiBufferFree "NetApiBufferFree" sptr
; NetApiBufferFree Buffer   ; 戻り値は stat
; Buffer : void* optional -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "NETAPI32.dll"
#cfunc global NetApiBufferFree "NetApiBufferFree" sptr
; res = NetApiBufferFree(Buffer)
; Buffer : void* optional -> "sptr"
; DWORD NetApiBufferFree(void* Buffer)
#uselib "NETAPI32.dll"
#cfunc global NetApiBufferFree "NetApiBufferFree" intptr
; res = NetApiBufferFree(Buffer)
; Buffer : void* optional -> "intptr"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	netapi32 = windows.NewLazySystemDLL("NETAPI32.dll")
	procNetApiBufferFree = netapi32.NewProc("NetApiBufferFree")
)

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

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

typedef NetApiBufferFreeNative = Uint32 Function(Pointer<Void>);
typedef NetApiBufferFreeDart = int Function(Pointer<Void>);
final NetApiBufferFree = DynamicLibrary.open('NETAPI32.dll')
    .lookupFunction<NetApiBufferFreeNative, NetApiBufferFreeDart>('NetApiBufferFree');
// Buffer : void* optional -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function NetApiBufferFree(
  Buffer: Pointer   // void* optional
): DWORD; stdcall;
  external 'NETAPI32.dll' name 'NetApiBufferFree';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "NetApiBufferFree"
  c_NetApiBufferFree :: Ptr () -> IO Word32
-- Buffer : void* optional -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let netapibufferfree =
  foreign "NetApiBufferFree"
    ((ptr void) @-> returning uint32_t)
(* Buffer : void* optional -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library netapi32 (t "NETAPI32.dll"))
(cffi:use-foreign-library netapi32)

(cffi:defcfun ("NetApiBufferFree" net-api-buffer-free :convention :stdcall) :uint32
  (buffer :pointer))   ; void* optional
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $NetApiBufferFree = Win32::API::More->new('NETAPI32',
    'DWORD NetApiBufferFree(LPVOID Buffer)');
# my $ret = $NetApiBufferFree->Call($Buffer);
# Buffer : void* optional -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

公式の関連項目