Win32 API 日本語リファレンス
ホームStorage.FileSystem › NetSessionDel

NetSessionDel

関数
指定したネットワークセッションを切断する。
DLLNETAPI32.dll呼出規約winapi対応OSWindows XP 以降

シグネチャ

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

DWORD NetSessionDel(
    LPWSTR servername,   // optional
    LPWSTR UncClientName,   // optional
    LPWSTR username   // optional
);

パラメーター

名前方向説明
servernameLPWSTRinoptional関数を実行するリモートサーバーの DNS 名または NetBIOS 名を指定する文字列へのポインターです。このパラメーターが NULL の場合は、ローカルコンピューターが使用されます。
UncClientNameLPWSTRinoptional切断するクライアントのコンピューター名を指定する文字列へのポインターです。UncClientName パラメーターが NULL の場合は、username パラメーターで識別されるユーザーのすべてのセッションが、servername パラメーターで指定したサーバー上で削除されます。詳細については、 NetSessionEnum を参照してください。
usernameLPWSTRinoptionalセッションを終了するユーザーの名前を指定する文字列へのポインターです。このパラメーターが NULL の場合は、UncClientName パラメーターで指定したクライアントからのすべてのユーザーのセッションが終了されます。

戻り値の型: DWORD

公式ドキュメント

サーバーとワークステーションの間のネットワークセッションを終了します。

戻り値

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

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

戻り値 説明
ERROR_ACCESS_DENIED
ユーザーは要求された情報へのアクセス権を持っていません。
ERROR_INVALID_PARAMETER
指定されたパラメーターが無効です。
ERROR_NOT_ENOUGH_MEMORY
利用可能なメモリが不足しています。
NERR_ClientNameNotFound
そのコンピューター名のセッションは存在しません。

解説(Remarks)

NetSessionDel 関数を正常に実行できるのは、Administrators または Server Operators ローカルグループのメンバーのみです。

Active Directory 向けにプログラミングを行う場合は、特定の Active Directory Service Interface (ADSI) メソッドを呼び出すことで、ネットワーク管理セッション関数を呼び出して実現できる機能と同じ機能を実現できる場合があります。詳細については、 IADsSession および IADsFileServiceOperations を参照してください。

次のコードサンプルは、NetSessionDel 関数を呼び出して、サーバーとワークステーションの間のセッションを終了する方法を示しています。

#ifndef UNICODE
#define UNICODE
#endif
#pragma comment(lib, "Netapi32.lib")

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

int wmain(int argc, wchar_t *argv[])
{
   DWORD dwError = 0;
   LPTSTR pszServerName = NULL;
   LPTSTR pszClientName = NULL;
   LPTSTR pszUserName = NULL;
   NET_API_STATUS nStatus;
   //
   // Check command line arguments.
   //
   if (argc > 4)
   {
      wprintf(L"Usage: %s [\\\\ServerName] [\\\\ClientName] [UserName]\n", argv[0]);
      exit(1);
   }

   if (argc >= 2)
      pszServerName = argv[1];

   if (argc >= 3)
      pszClientName = argv[2];

   if (argc == 4)
      pszUserName = argv[3];
   //
   // Call the NetSessionDel function to delete the session.
   //
   nStatus = NetSessionDel(pszServerName,
                           pszClientName,
                           pszUserName);
   //
   // Display the result of the call.
   //
   if (nStatus == NERR_Success)
      fprintf(stderr, "The specified session(s) has been successfully deleted\n");
   else
      fprintf(stderr, "A system error has occurred: %d\n", nStatus);

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

各言語での呼び出し定義

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

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

NetSessionDel = ctypes.windll.netapi32.NetSessionDel
NetSessionDel.restype = wintypes.DWORD
NetSessionDel.argtypes = [
    wintypes.LPCWSTR,  # servername : LPWSTR optional
    wintypes.LPCWSTR,  # UncClientName : LPWSTR optional
    wintypes.LPCWSTR,  # username : LPWSTR optional
]
require 'fiddle'
require 'fiddle/import'

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

var (
	netapi32 = windows.NewLazySystemDLL("NETAPI32.dll")
	procNetSessionDel = netapi32.NewProc("NetSessionDel")
)

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

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

typedef NetSessionDelNative = Uint32 Function(Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>);
typedef NetSessionDelDart = int Function(Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>);
final NetSessionDel = DynamicLibrary.open('NETAPI32.dll')
    .lookupFunction<NetSessionDelNative, NetSessionDelDart>('NetSessionDel');
// servername : LPWSTR optional -> Pointer<Utf16>
// UncClientName : LPWSTR optional -> Pointer<Utf16>
// username : LPWSTR optional -> Pointer<Utf16>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function NetSessionDel(
  servername: PWideChar;   // LPWSTR optional
  UncClientName: PWideChar;   // LPWSTR optional
  username: PWideChar   // LPWSTR optional
): DWORD; stdcall;
  external 'NETAPI32.dll' name 'NetSessionDel';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "NetSessionDel"
  c_NetSessionDel :: CWString -> CWString -> CWString -> IO Word32
-- servername : LPWSTR optional -> CWString
-- UncClientName : LPWSTR optional -> CWString
-- username : LPWSTR optional -> CWString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let netsessiondel =
  foreign "NetSessionDel"
    ((ptr uint16_t) @-> (ptr uint16_t) @-> (ptr uint16_t) @-> returning uint32_t)
(* servername : LPWSTR optional -> (ptr uint16_t) *)
(* UncClientName : LPWSTR optional -> (ptr uint16_t) *)
(* username : LPWSTR optional -> (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 ("NetSessionDel" net-session-del :convention :stdcall) :uint32
  (servername (:string :encoding :utf-16le))   ; LPWSTR optional
  (unc-client-name (:string :encoding :utf-16le))   ; LPWSTR optional
  (username (:string :encoding :utf-16le)))   ; LPWSTR optional
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $NetSessionDel = Win32::API::More->new('NETAPI32',
    'DWORD NetSessionDel(LPCWSTR servername, LPCWSTR UncClientName, LPCWSTR username)');
# my $ret = $NetSessionDel->Call($servername, $UncClientName, $username);
# servername : LPWSTR optional -> LPCWSTR
# UncClientName : LPWSTR optional -> LPCWSTR
# username : LPWSTR optional -> LPCWSTR
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

公式の関連項目