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

NdfCreateWinSockIncident

関数
Winsockソケットに関するネットワーク診断インシデントを作成する。
DLLNDFAPI.dll呼出規約winapi対応OSWindows Vista 以降

シグネチャ

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

HRESULT NdfCreateWinSockIncident(
    SOCKET sock,
    LPCWSTR host,   // optional
    WORD port,
    LPCWSTR appId,   // optional
    SID* userId,   // optional
    void** handle
);

パラメーター

名前方向説明
sockSOCKETin診断対象のソケットハンドル。INVALID_SOCKET可。
hostLPCWSTRinoptional接続先ホスト名またはIPアドレスを指定する。NULL可。
portWORDin接続先ポート番号を指定する。
appIdLPCWSTRinoptional対象アプリケーションの識別子を指定する。NULL可。
userIdSID*inoptional対象ユーザーのSIDへのポインター。NULL可。
handlevoid**out作成された診断インシデントのハンドルを受け取るポインター。

戻り値の型: HRESULT

各言語での呼び出し定義

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

HRESULT NdfCreateWinSockIncident(
    SOCKET sock,
    LPCWSTR host,   // optional
    WORD port,
    LPCWSTR appId,   // optional
    SID* userId,   // optional
    void** handle
);
[DllImport("NDFAPI.dll", ExactSpelling = true)]
static extern int NdfCreateWinSockIncident(
    UIntPtr sock,   // SOCKET
    [MarshalAs(UnmanagedType.LPWStr)] string host,   // LPCWSTR optional
    ushort port,   // WORD
    [MarshalAs(UnmanagedType.LPWStr)] string appId,   // LPCWSTR optional
    IntPtr userId,   // SID* optional
    IntPtr handle   // void** out
);
<DllImport("NDFAPI.dll", ExactSpelling:=True)>
Public Shared Function NdfCreateWinSockIncident(
    sock As UIntPtr,   ' SOCKET
    <MarshalAs(UnmanagedType.LPWStr)> host As String,   ' LPCWSTR optional
    port As UShort,   ' WORD
    <MarshalAs(UnmanagedType.LPWStr)> appId As String,   ' LPCWSTR optional
    userId As IntPtr,   ' SID* optional
    handle As IntPtr   ' void** out
) As Integer
End Function
' sock : SOCKET
' host : LPCWSTR optional
' port : WORD
' appId : LPCWSTR optional
' userId : SID* optional
' handle : void** out
Declare PtrSafe Function NdfCreateWinSockIncident Lib "ndfapi" ( _
    ByVal sock As LongPtr, _
    ByVal host As LongPtr, _
    ByVal port As Integer, _
    ByVal appId As LongPtr, _
    ByVal userId As LongPtr, _
    ByVal handle As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

NdfCreateWinSockIncident = ctypes.windll.ndfapi.NdfCreateWinSockIncident
NdfCreateWinSockIncident.restype = ctypes.c_int
NdfCreateWinSockIncident.argtypes = [
    ctypes.c_size_t,  # sock : SOCKET
    wintypes.LPCWSTR,  # host : LPCWSTR optional
    ctypes.c_ushort,  # port : WORD
    wintypes.LPCWSTR,  # appId : LPCWSTR optional
    ctypes.c_void_p,  # userId : SID* optional
    ctypes.c_void_p,  # handle : void** out
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('NDFAPI.dll')
NdfCreateWinSockIncident = Fiddle::Function.new(
  lib['NdfCreateWinSockIncident'],
  [
    Fiddle::TYPE_UINTPTR_T,  # sock : SOCKET
    Fiddle::TYPE_VOIDP,  # host : LPCWSTR optional
    -Fiddle::TYPE_SHORT,  # port : WORD
    Fiddle::TYPE_VOIDP,  # appId : LPCWSTR optional
    Fiddle::TYPE_VOIDP,  # userId : SID* optional
    Fiddle::TYPE_VOIDP,  # handle : void** out
  ],
  Fiddle::TYPE_INT)
#[link(name = "ndfapi")]
extern "system" {
    fn NdfCreateWinSockIncident(
        sock: usize,  // SOCKET
        host: *const u16,  // LPCWSTR optional
        port: u16,  // WORD
        appId: *const u16,  // LPCWSTR optional
        userId: *mut SID,  // SID* optional
        handle: *mut *mut ()  // void** out
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("NDFAPI.dll")]
public static extern int NdfCreateWinSockIncident(UIntPtr sock, [MarshalAs(UnmanagedType.LPWStr)] string host, ushort port, [MarshalAs(UnmanagedType.LPWStr)] string appId, IntPtr userId, IntPtr handle);
"@
$api = Add-Type -MemberDefinition $sig -Name 'NDFAPI_NdfCreateWinSockIncident' -Namespace Win32 -PassThru
# $api::NdfCreateWinSockIncident(sock, host, port, appId, userId, handle)
#uselib "NDFAPI.dll"
#func global NdfCreateWinSockIncident "NdfCreateWinSockIncident" sptr, sptr, sptr, sptr, sptr, sptr
; NdfCreateWinSockIncident sock, host, port, appId, varptr(userId), handle   ; 戻り値は stat
; sock : SOCKET -> "sptr"
; host : LPCWSTR optional -> "sptr"
; port : WORD -> "sptr"
; appId : LPCWSTR optional -> "sptr"
; userId : SID* optional -> "sptr"
; handle : void** out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "NDFAPI.dll"
#cfunc global NdfCreateWinSockIncident "NdfCreateWinSockIncident" sptr, wstr, int, wstr, var, sptr
; res = NdfCreateWinSockIncident(sock, host, port, appId, userId, handle)
; sock : SOCKET -> "sptr"
; host : LPCWSTR optional -> "wstr"
; port : WORD -> "int"
; appId : LPCWSTR optional -> "wstr"
; userId : SID* optional -> "var"
; handle : void** out -> "sptr"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; HRESULT NdfCreateWinSockIncident(SOCKET sock, LPCWSTR host, WORD port, LPCWSTR appId, SID* userId, void** handle)
#uselib "NDFAPI.dll"
#cfunc global NdfCreateWinSockIncident "NdfCreateWinSockIncident" intptr, wstr, int, wstr, var, intptr
; res = NdfCreateWinSockIncident(sock, host, port, appId, userId, handle)
; sock : SOCKET -> "intptr"
; host : LPCWSTR optional -> "wstr"
; port : WORD -> "int"
; appId : LPCWSTR optional -> "wstr"
; userId : SID* optional -> "var"
; handle : void** out -> "intptr"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	ndfapi = windows.NewLazySystemDLL("NDFAPI.dll")
	procNdfCreateWinSockIncident = ndfapi.NewProc("NdfCreateWinSockIncident")
)

// sock (SOCKET), host (LPCWSTR optional), port (WORD), appId (LPCWSTR optional), userId (SID* optional), handle (void** out)
r1, _, err := procNdfCreateWinSockIncident.Call(
	uintptr(sock),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(host))),
	uintptr(port),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(appId))),
	uintptr(userId),
	uintptr(handle),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // HRESULT
function NdfCreateWinSockIncident(
  sock: NativeUInt;   // SOCKET
  host: PWideChar;   // LPCWSTR optional
  port: Word;   // WORD
  appId: PWideChar;   // LPCWSTR optional
  userId: Pointer;   // SID* optional
  handle: Pointer   // void** out
): Integer; stdcall;
  external 'NDFAPI.dll' name 'NdfCreateWinSockIncident';
result := DllCall("NDFAPI\NdfCreateWinSockIncident"
    , "UPtr", sock   ; SOCKET
    , "WStr", host   ; LPCWSTR optional
    , "UShort", port   ; WORD
    , "WStr", appId   ; LPCWSTR optional
    , "Ptr", userId   ; SID* optional
    , "Ptr", handle   ; void** out
    , "Int")   ; return: HRESULT
●NdfCreateWinSockIncident(sock, host, port, appId, userId, handle) = DLL("NDFAPI.dll", "int NdfCreateWinSockIncident(int, char*, int, char*, void*, void*)")
# 呼び出し: NdfCreateWinSockIncident(sock, host, port, appId, userId, handle)
# sock : SOCKET -> "int"
# host : LPCWSTR optional -> "char*"
# port : WORD -> "int"
# appId : LPCWSTR optional -> "char*"
# userId : SID* optional -> "void*"
# handle : void** out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "ndfapi" fn NdfCreateWinSockIncident(
    sock: usize, // SOCKET
    host: [*c]const u16, // LPCWSTR optional
    port: u16, // WORD
    appId: [*c]const u16, // LPCWSTR optional
    userId: [*c]SID, // SID* optional
    handle: ?*anyopaque // void** out
) callconv(std.os.windows.WINAPI) i32;
proc NdfCreateWinSockIncident(
    sock: uint,  # SOCKET
    host: WideCString,  # LPCWSTR optional
    port: uint16,  # WORD
    appId: WideCString,  # LPCWSTR optional
    userId: ptr SID,  # SID* optional
    handle: pointer  # void** out
): int32 {.importc: "NdfCreateWinSockIncident", stdcall, dynlib: "NDFAPI.dll".}
pragma(lib, "ndfapi");
extern(Windows)
int NdfCreateWinSockIncident(
    size_t sock,   // SOCKET
    const(wchar)* host,   // LPCWSTR optional
    ushort port,   // WORD
    const(wchar)* appId,   // LPCWSTR optional
    SID* userId,   // SID* optional
    void** handle   // void** out
);
ccall((:NdfCreateWinSockIncident, "NDFAPI.dll"), stdcall, Int32,
      (Csize_t, Cwstring, UInt16, Cwstring, Ptr{SID}, Ptr{Cvoid}),
      sock, host, port, appId, userId, handle)
# sock : SOCKET -> Csize_t
# host : LPCWSTR optional -> Cwstring
# port : WORD -> UInt16
# appId : LPCWSTR optional -> Cwstring
# userId : SID* optional -> Ptr{SID}
# handle : void** out -> Ptr{Cvoid}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t NdfCreateWinSockIncident(
    uintptr_t sock,
    const uint16_t* host,
    uint16_t port,
    const uint16_t* appId,
    void* userId,
    void** handle);
]]
local ndfapi = ffi.load("ndfapi")
-- ndfapi.NdfCreateWinSockIncident(sock, host, port, appId, userId, handle)
-- sock : SOCKET
-- host : LPCWSTR optional
-- port : WORD
-- appId : LPCWSTR optional
-- userId : SID* optional
-- handle : void** out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('NDFAPI.dll');
const NdfCreateWinSockIncident = lib.func('__stdcall', 'NdfCreateWinSockIncident', 'int32_t', ['uintptr_t', 'str16', 'uint16_t', 'str16', 'void *', 'void *']);
// NdfCreateWinSockIncident(sock, host, port, appId, userId, handle)
// sock : SOCKET -> 'uintptr_t'
// host : LPCWSTR optional -> 'str16'
// port : WORD -> 'uint16_t'
// appId : LPCWSTR optional -> 'str16'
// userId : SID* optional -> 'void *'
// handle : void** out -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("NDFAPI.dll", {
  NdfCreateWinSockIncident: { parameters: ["usize", "buffer", "u16", "buffer", "pointer", "pointer"], result: "i32" },
});
// lib.symbols.NdfCreateWinSockIncident(sock, host, port, appId, userId, handle)
// sock : SOCKET -> "usize"
// host : LPCWSTR optional -> "buffer"
// port : WORD -> "u16"
// appId : LPCWSTR optional -> "buffer"
// userId : SID* optional -> "pointer"
// handle : void** out -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t NdfCreateWinSockIncident(
    size_t sock,
    const uint16_t* host,
    uint16_t port,
    const uint16_t* appId,
    void* userId,
    void** handle);
C, "NDFAPI.dll");
// $ffi->NdfCreateWinSockIncident(sock, host, port, appId, userId, handle);
// sock : SOCKET
// host : LPCWSTR optional
// port : WORD
// appId : LPCWSTR optional
// userId : SID* optional
// handle : void** 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 Ndfapi extends StdCallLibrary {
    Ndfapi INSTANCE = Native.load("ndfapi", Ndfapi.class);
    int NdfCreateWinSockIncident(
        long sock,   // SOCKET
        WString host,   // LPCWSTR optional
        short port,   // WORD
        WString appId,   // LPCWSTR optional
        Pointer userId,   // SID* optional
        Pointer handle   // void** out
    );
}
@[Link("ndfapi")]
lib LibNDFAPI
  fun NdfCreateWinSockIncident = NdfCreateWinSockIncident(
    sock : LibC::SizeT,   # SOCKET
    host : UInt16*,   # LPCWSTR optional
    port : UInt16,   # WORD
    appId : UInt16*,   # LPCWSTR optional
    userId : SID*,   # SID* optional
    handle : Void**   # void** out
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef NdfCreateWinSockIncidentNative = Int32 Function(UintPtr, Pointer<Utf16>, Uint16, Pointer<Utf16>, Pointer<Void>, Pointer<Void>);
typedef NdfCreateWinSockIncidentDart = int Function(int, Pointer<Utf16>, int, Pointer<Utf16>, Pointer<Void>, Pointer<Void>);
final NdfCreateWinSockIncident = DynamicLibrary.open('NDFAPI.dll')
    .lookupFunction<NdfCreateWinSockIncidentNative, NdfCreateWinSockIncidentDart>('NdfCreateWinSockIncident');
// sock : SOCKET -> UintPtr
// host : LPCWSTR optional -> Pointer<Utf16>
// port : WORD -> Uint16
// appId : LPCWSTR optional -> Pointer<Utf16>
// userId : SID* optional -> Pointer<Void>
// handle : void** out -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function NdfCreateWinSockIncident(
  sock: NativeUInt;   // SOCKET
  host: PWideChar;   // LPCWSTR optional
  port: Word;   // WORD
  appId: PWideChar;   // LPCWSTR optional
  userId: Pointer;   // SID* optional
  handle: Pointer   // void** out
): Integer; stdcall;
  external 'NDFAPI.dll' name 'NdfCreateWinSockIncident';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "NdfCreateWinSockIncident"
  c_NdfCreateWinSockIncident :: CUIntPtr -> CWString -> Word16 -> CWString -> Ptr () -> Ptr () -> IO Int32
-- sock : SOCKET -> CUIntPtr
-- host : LPCWSTR optional -> CWString
-- port : WORD -> Word16
-- appId : LPCWSTR optional -> CWString
-- userId : SID* optional -> Ptr ()
-- handle : void** out -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let ndfcreatewinsockincident =
  foreign "NdfCreateWinSockIncident"
    (size_t @-> (ptr uint16_t) @-> uint16_t @-> (ptr uint16_t) @-> (ptr void) @-> (ptr void) @-> returning int32_t)
(* sock : SOCKET -> size_t *)
(* host : LPCWSTR optional -> (ptr uint16_t) *)
(* port : WORD -> uint16_t *)
(* appId : LPCWSTR optional -> (ptr uint16_t) *)
(* userId : SID* optional -> (ptr void) *)
(* handle : void** out -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library ndfapi (t "NDFAPI.dll"))
(cffi:use-foreign-library ndfapi)

(cffi:defcfun ("NdfCreateWinSockIncident" ndf-create-win-sock-incident :convention :stdcall) :int32
  (sock :uint64)   ; SOCKET
  (host (:string :encoding :utf-16le))   ; LPCWSTR optional
  (port :uint16)   ; WORD
  (app-id (:string :encoding :utf-16le))   ; LPCWSTR optional
  (user-id :pointer)   ; SID* optional
  (handle :pointer))   ; void** out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $NdfCreateWinSockIncident = Win32::API::More->new('NDFAPI',
    'int NdfCreateWinSockIncident(WPARAM sock, LPCWSTR host, WORD port, LPCWSTR appId, LPVOID userId, LPVOID handle)');
# my $ret = $NdfCreateWinSockIncident->Call($sock, $host, $port, $appId, $userId, $handle);
# sock : SOCKET -> WPARAM
# host : LPCWSTR optional -> LPCWSTR
# port : WORD -> WORD
# appId : LPCWSTR optional -> LPCWSTR
# userId : SID* optional -> LPVOID
# handle : void** out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

使用する型