ホーム › Networking.ActiveDirectory › DsGetSpnW
DsGetSpnW
関数サービス情報からSPNの配列を生成する(Unicode版)。
シグネチャ
// NTDSAPI.dll (Unicode / -W)
#include <windows.h>
DWORD DsGetSpnW(
DS_SPN_NAME_TYPE ServiceType,
LPCWSTR ServiceClass,
LPCWSTR ServiceName, // optional
WORD InstancePort,
WORD cInstanceNames,
LPCWSTR* pInstanceNames, // optional
const WORD* pInstancePorts, // optional
DWORD* pcSpn,
LPWSTR** prpszSpn
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| ServiceType | DS_SPN_NAME_TYPE | in | 生成するSPNの種類を示すDS_SPN_NAME_TYPE列挙値。 |
| ServiceClass | LPCWSTR | in | サービスクラス名(ワイド)。 |
| ServiceName | LPCWSTR | inoptional | サービス名。種類により省略可。NULL可。 |
| InstancePort | WORD | in | 既定のインスタンスポート番号。0で省略する。 |
| cInstanceNames | WORD | in | pInstanceNames配列の要素数。0で単一SPN生成。 |
| pInstanceNames | LPCWSTR* | inoptional | インスタンス名の配列へのポインタ。NULL可。 |
| pInstancePorts | WORD* | inoptional | 各インスタンスのポート番号配列へのポインタ。NULL可。 |
| pcSpn | DWORD* | out | 生成されたSPNの個数を受け取るDWORDへのポインタ。 |
| prpszSpn | LPWSTR** | out | 生成されたSPN文字列配列を受け取るポインタのアドレス。DsFreeSpnArrayWで解放する。 |
戻り値の型: DWORD
各言語での呼び出し定義
// NTDSAPI.dll (Unicode / -W)
#include <windows.h>
DWORD DsGetSpnW(
DS_SPN_NAME_TYPE ServiceType,
LPCWSTR ServiceClass,
LPCWSTR ServiceName, // optional
WORD InstancePort,
WORD cInstanceNames,
LPCWSTR* pInstanceNames, // optional
const WORD* pInstancePorts, // optional
DWORD* pcSpn,
LPWSTR** prpszSpn
);[DllImport("NTDSAPI.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern uint DsGetSpnW(
int ServiceType, // DS_SPN_NAME_TYPE
[MarshalAs(UnmanagedType.LPWStr)] string ServiceClass, // LPCWSTR
[MarshalAs(UnmanagedType.LPWStr)] string ServiceName, // LPCWSTR optional
ushort InstancePort, // WORD
ushort cInstanceNames, // WORD
IntPtr pInstanceNames, // LPCWSTR* optional
IntPtr pInstancePorts, // WORD* optional
out uint pcSpn, // DWORD* out
IntPtr prpszSpn // LPWSTR** out
);<DllImport("NTDSAPI.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function DsGetSpnW(
ServiceType As Integer, ' DS_SPN_NAME_TYPE
<MarshalAs(UnmanagedType.LPWStr)> ServiceClass As String, ' LPCWSTR
<MarshalAs(UnmanagedType.LPWStr)> ServiceName As String, ' LPCWSTR optional
InstancePort As UShort, ' WORD
cInstanceNames As UShort, ' WORD
pInstanceNames As IntPtr, ' LPCWSTR* optional
pInstancePorts As IntPtr, ' WORD* optional
<Out> ByRef pcSpn As UInteger, ' DWORD* out
prpszSpn As IntPtr ' LPWSTR** out
) As UInteger
End Function' ServiceType : DS_SPN_NAME_TYPE
' ServiceClass : LPCWSTR
' ServiceName : LPCWSTR optional
' InstancePort : WORD
' cInstanceNames : WORD
' pInstanceNames : LPCWSTR* optional
' pInstancePorts : WORD* optional
' pcSpn : DWORD* out
' prpszSpn : LPWSTR** out
Declare PtrSafe Function DsGetSpnW Lib "ntdsapi" ( _
ByVal ServiceType As Long, _
ByVal ServiceClass As LongPtr, _
ByVal ServiceName As LongPtr, _
ByVal InstancePort As Integer, _
ByVal cInstanceNames As Integer, _
ByVal pInstanceNames As LongPtr, _
ByVal pInstancePorts As LongPtr, _
ByRef pcSpn As Long, _
ByVal prpszSpn As LongPtr) As Long
' Unicode(W): 文字列は ByVal As LongPtr とし StrPtr(unicodeStr) を渡す
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
DsGetSpnW = ctypes.windll.ntdsapi.DsGetSpnW
DsGetSpnW.restype = wintypes.DWORD
DsGetSpnW.argtypes = [
ctypes.c_int, # ServiceType : DS_SPN_NAME_TYPE
wintypes.LPCWSTR, # ServiceClass : LPCWSTR
wintypes.LPCWSTR, # ServiceName : LPCWSTR optional
ctypes.c_ushort, # InstancePort : WORD
ctypes.c_ushort, # cInstanceNames : WORD
ctypes.c_void_p, # pInstanceNames : LPCWSTR* optional
ctypes.POINTER(ctypes.c_ushort), # pInstancePorts : WORD* optional
ctypes.POINTER(wintypes.DWORD), # pcSpn : DWORD* out
ctypes.c_void_p, # prpszSpn : LPWSTR** out
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('NTDSAPI.dll')
DsGetSpnW = Fiddle::Function.new(
lib['DsGetSpnW'],
[
Fiddle::TYPE_INT, # ServiceType : DS_SPN_NAME_TYPE
Fiddle::TYPE_VOIDP, # ServiceClass : LPCWSTR
Fiddle::TYPE_VOIDP, # ServiceName : LPCWSTR optional
-Fiddle::TYPE_SHORT, # InstancePort : WORD
-Fiddle::TYPE_SHORT, # cInstanceNames : WORD
Fiddle::TYPE_VOIDP, # pInstanceNames : LPCWSTR* optional
Fiddle::TYPE_VOIDP, # pInstancePorts : WORD* optional
Fiddle::TYPE_VOIDP, # pcSpn : DWORD* out
Fiddle::TYPE_VOIDP, # prpszSpn : LPWSTR** out
],
-Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"#[link(name = "ntdsapi")]
extern "system" {
fn DsGetSpnW(
ServiceType: i32, // DS_SPN_NAME_TYPE
ServiceClass: *const u16, // LPCWSTR
ServiceName: *const u16, // LPCWSTR optional
InstancePort: u16, // WORD
cInstanceNames: u16, // WORD
pInstanceNames: *const *const u16, // LPCWSTR* optional
pInstancePorts: *const u16, // WORD* optional
pcSpn: *mut u32, // DWORD* out
prpszSpn: *mut *mut *mut u16 // LPWSTR** out
) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("NTDSAPI.dll", CharSet = CharSet.Unicode)]
public static extern uint DsGetSpnW(int ServiceType, [MarshalAs(UnmanagedType.LPWStr)] string ServiceClass, [MarshalAs(UnmanagedType.LPWStr)] string ServiceName, ushort InstancePort, ushort cInstanceNames, IntPtr pInstanceNames, IntPtr pInstancePorts, out uint pcSpn, IntPtr prpszSpn);
"@
$api = Add-Type -MemberDefinition $sig -Name 'NTDSAPI_DsGetSpnW' -Namespace Win32 -PassThru
# $api::DsGetSpnW(ServiceType, ServiceClass, ServiceName, InstancePort, cInstanceNames, pInstanceNames, pInstancePorts, pcSpn, prpszSpn)#uselib "NTDSAPI.dll"
#func global DsGetSpnW "DsGetSpnW" wptr, wptr, wptr, wptr, wptr, wptr, wptr, wptr, wptr
; DsGetSpnW ServiceType, ServiceClass, ServiceName, InstancePort, cInstanceNames, varptr(pInstanceNames), varptr(pInstancePorts), varptr(pcSpn), varptr(prpszSpn) ; 戻り値は stat
; ServiceType : DS_SPN_NAME_TYPE -> "wptr"
; ServiceClass : LPCWSTR -> "wptr"
; ServiceName : LPCWSTR optional -> "wptr"
; InstancePort : WORD -> "wptr"
; cInstanceNames : WORD -> "wptr"
; pInstanceNames : LPCWSTR* optional -> "wptr"
; pInstancePorts : WORD* optional -> "wptr"
; pcSpn : DWORD* out -> "wptr"
; prpszSpn : LPWSTR** out -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "NTDSAPI.dll" #cfunc global DsGetSpnW "DsGetSpnW" int, wstr, wstr, int, int, var, var, var, var ; res = DsGetSpnW(ServiceType, ServiceClass, ServiceName, InstancePort, cInstanceNames, pInstanceNames, pInstancePorts, pcSpn, prpszSpn) ; ServiceType : DS_SPN_NAME_TYPE -> "int" ; ServiceClass : LPCWSTR -> "wstr" ; ServiceName : LPCWSTR optional -> "wstr" ; InstancePort : WORD -> "int" ; cInstanceNames : WORD -> "int" ; pInstanceNames : LPCWSTR* optional -> "var" ; pInstancePorts : WORD* optional -> "var" ; pcSpn : DWORD* out -> "var" ; prpszSpn : LPWSTR** out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "NTDSAPI.dll" #cfunc global DsGetSpnW "DsGetSpnW" int, wstr, wstr, int, int, sptr, sptr, sptr, sptr ; res = DsGetSpnW(ServiceType, ServiceClass, ServiceName, InstancePort, cInstanceNames, varptr(pInstanceNames), varptr(pInstancePorts), varptr(pcSpn), varptr(prpszSpn)) ; ServiceType : DS_SPN_NAME_TYPE -> "int" ; ServiceClass : LPCWSTR -> "wstr" ; ServiceName : LPCWSTR optional -> "wstr" ; InstancePort : WORD -> "int" ; cInstanceNames : WORD -> "int" ; pInstanceNames : LPCWSTR* optional -> "sptr" ; pInstancePorts : WORD* optional -> "sptr" ; pcSpn : DWORD* out -> "sptr" ; prpszSpn : LPWSTR** out -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; DWORD DsGetSpnW(DS_SPN_NAME_TYPE ServiceType, LPCWSTR ServiceClass, LPCWSTR ServiceName, WORD InstancePort, WORD cInstanceNames, LPCWSTR* pInstanceNames, WORD* pInstancePorts, DWORD* pcSpn, LPWSTR** prpszSpn) #uselib "NTDSAPI.dll" #cfunc global DsGetSpnW "DsGetSpnW" int, wstr, wstr, int, int, var, var, var, var ; res = DsGetSpnW(ServiceType, ServiceClass, ServiceName, InstancePort, cInstanceNames, pInstanceNames, pInstancePorts, pcSpn, prpszSpn) ; ServiceType : DS_SPN_NAME_TYPE -> "int" ; ServiceClass : LPCWSTR -> "wstr" ; ServiceName : LPCWSTR optional -> "wstr" ; InstancePort : WORD -> "int" ; cInstanceNames : WORD -> "int" ; pInstanceNames : LPCWSTR* optional -> "var" ; pInstancePorts : WORD* optional -> "var" ; pcSpn : DWORD* out -> "var" ; prpszSpn : LPWSTR** out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; DWORD DsGetSpnW(DS_SPN_NAME_TYPE ServiceType, LPCWSTR ServiceClass, LPCWSTR ServiceName, WORD InstancePort, WORD cInstanceNames, LPCWSTR* pInstanceNames, WORD* pInstancePorts, DWORD* pcSpn, LPWSTR** prpszSpn) #uselib "NTDSAPI.dll" #cfunc global DsGetSpnW "DsGetSpnW" int, wstr, wstr, int, int, intptr, intptr, intptr, intptr ; res = DsGetSpnW(ServiceType, ServiceClass, ServiceName, InstancePort, cInstanceNames, varptr(pInstanceNames), varptr(pInstancePorts), varptr(pcSpn), varptr(prpszSpn)) ; ServiceType : DS_SPN_NAME_TYPE -> "int" ; ServiceClass : LPCWSTR -> "wstr" ; ServiceName : LPCWSTR optional -> "wstr" ; InstancePort : WORD -> "int" ; cInstanceNames : WORD -> "int" ; pInstanceNames : LPCWSTR* optional -> "intptr" ; pInstancePorts : WORD* optional -> "intptr" ; pcSpn : DWORD* out -> "intptr" ; prpszSpn : LPWSTR** out -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
ntdsapi = windows.NewLazySystemDLL("NTDSAPI.dll")
procDsGetSpnW = ntdsapi.NewProc("DsGetSpnW")
)
// ServiceType (DS_SPN_NAME_TYPE), ServiceClass (LPCWSTR), ServiceName (LPCWSTR optional), InstancePort (WORD), cInstanceNames (WORD), pInstanceNames (LPCWSTR* optional), pInstancePorts (WORD* optional), pcSpn (DWORD* out), prpszSpn (LPWSTR** out)
r1, _, err := procDsGetSpnW.Call(
uintptr(ServiceType),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(ServiceClass))),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(ServiceName))),
uintptr(InstancePort),
uintptr(cInstanceNames),
uintptr(pInstanceNames),
uintptr(pInstancePorts),
uintptr(pcSpn),
uintptr(prpszSpn),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // DWORDfunction DsGetSpnW(
ServiceType: Integer; // DS_SPN_NAME_TYPE
ServiceClass: PWideChar; // LPCWSTR
ServiceName: PWideChar; // LPCWSTR optional
InstancePort: Word; // WORD
cInstanceNames: Word; // WORD
pInstanceNames: PPWideChar; // LPCWSTR* optional
pInstancePorts: Pointer; // WORD* optional
pcSpn: Pointer; // DWORD* out
prpszSpn: PPWideChar // LPWSTR** out
): DWORD; stdcall;
external 'NTDSAPI.dll' name 'DsGetSpnW';result := DllCall("NTDSAPI\DsGetSpnW"
, "Int", ServiceType ; DS_SPN_NAME_TYPE
, "WStr", ServiceClass ; LPCWSTR
, "WStr", ServiceName ; LPCWSTR optional
, "UShort", InstancePort ; WORD
, "UShort", cInstanceNames ; WORD
, "Ptr", pInstanceNames ; LPCWSTR* optional
, "Ptr", pInstancePorts ; WORD* optional
, "Ptr", pcSpn ; DWORD* out
, "Ptr", prpszSpn ; LPWSTR** out
, "UInt") ; return: DWORD●DsGetSpnW(ServiceType, ServiceClass, ServiceName, InstancePort, cInstanceNames, pInstanceNames, pInstancePorts, pcSpn, prpszSpn) = DLL("NTDSAPI.dll", "dword DsGetSpnW(int, char*, char*, int, int, void*, void*, void*, void*)")
# 呼び出し: DsGetSpnW(ServiceType, ServiceClass, ServiceName, InstancePort, cInstanceNames, pInstanceNames, pInstancePorts, pcSpn, prpszSpn)
# ServiceType : DS_SPN_NAME_TYPE -> "int"
# ServiceClass : LPCWSTR -> "char*"
# ServiceName : LPCWSTR optional -> "char*"
# InstancePort : WORD -> "int"
# cInstanceNames : WORD -> "int"
# pInstanceNames : LPCWSTR* optional -> "void*"
# pInstancePorts : WORD* optional -> "void*"
# pcSpn : DWORD* out -> "void*"
# prpszSpn : LPWSTR** out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。const std = @import("std");
extern "ntdsapi" fn DsGetSpnW(
ServiceType: i32, // DS_SPN_NAME_TYPE
ServiceClass: [*c]const u16, // LPCWSTR
ServiceName: [*c]const u16, // LPCWSTR optional
InstancePort: u16, // WORD
cInstanceNames: u16, // WORD
pInstanceNames: [*c][*c]u16, // LPCWSTR* optional
pInstancePorts: [*c]u16, // WORD* optional
pcSpn: [*c]u32, // DWORD* out
prpszSpn: [*c][*c][*c]u16 // LPWSTR** out
) callconv(std.os.windows.WINAPI) u32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。proc DsGetSpnW(
ServiceType: int32, # DS_SPN_NAME_TYPE
ServiceClass: WideCString, # LPCWSTR
ServiceName: WideCString, # LPCWSTR optional
InstancePort: uint16, # WORD
cInstanceNames: uint16, # WORD
pInstanceNames: ptr WideCString, # LPCWSTR* optional
pInstancePorts: ptr uint16, # WORD* optional
pcSpn: ptr uint32, # DWORD* out
prpszSpn: ptr WideCString # LPWSTR** out
): uint32 {.importc: "DsGetSpnW", stdcall, dynlib: "NTDSAPI.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。pragma(lib, "ntdsapi");
extern(Windows)
uint DsGetSpnW(
int ServiceType, // DS_SPN_NAME_TYPE
const(wchar)* ServiceClass, // LPCWSTR
const(wchar)* ServiceName, // LPCWSTR optional
ushort InstancePort, // WORD
ushort cInstanceNames, // WORD
wchar** pInstanceNames, // LPCWSTR* optional
ushort* pInstancePorts, // WORD* optional
uint* pcSpn, // DWORD* out
wchar*** prpszSpn // LPWSTR** out
);ccall((:DsGetSpnW, "NTDSAPI.dll"), stdcall, UInt32,
(Int32, Cwstring, Cwstring, UInt16, UInt16, Ptr{Ptr{UInt16}}, Ptr{UInt16}, Ptr{UInt32}, Ptr{Ptr{UInt16}}),
ServiceType, ServiceClass, ServiceName, InstancePort, cInstanceNames, pInstanceNames, pInstancePorts, pcSpn, prpszSpn)
# ServiceType : DS_SPN_NAME_TYPE -> Int32
# ServiceClass : LPCWSTR -> Cwstring
# ServiceName : LPCWSTR optional -> Cwstring
# InstancePort : WORD -> UInt16
# cInstanceNames : WORD -> UInt16
# pInstanceNames : LPCWSTR* optional -> Ptr{Ptr{UInt16}}
# pInstancePorts : WORD* optional -> Ptr{UInt16}
# pcSpn : DWORD* out -> Ptr{UInt32}
# prpszSpn : LPWSTR** out -> Ptr{Ptr{UInt16}}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。local ffi = require("ffi")
ffi.cdef[[
uint32_t DsGetSpnW(
int32_t ServiceType,
const uint16_t* ServiceClass,
const uint16_t* ServiceName,
uint16_t InstancePort,
uint16_t cInstanceNames,
uint16_t** pInstanceNames,
uint16_t* pInstancePorts,
uint32_t* pcSpn,
uint16_t*** prpszSpn);
]]
local ntdsapi = ffi.load("ntdsapi")
-- ntdsapi.DsGetSpnW(ServiceType, ServiceClass, ServiceName, InstancePort, cInstanceNames, pInstanceNames, pInstancePorts, pcSpn, prpszSpn)
-- ServiceType : DS_SPN_NAME_TYPE
-- ServiceClass : LPCWSTR
-- ServiceName : LPCWSTR optional
-- InstancePort : WORD
-- cInstanceNames : WORD
-- pInstanceNames : LPCWSTR* optional
-- pInstancePorts : WORD* optional
-- pcSpn : DWORD* out
-- prpszSpn : LPWSTR** out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。const koffi = require('koffi');
const lib = koffi.load('NTDSAPI.dll');
const DsGetSpnW = lib.func('__stdcall', 'DsGetSpnW', 'uint32_t', ['int32_t', 'str16', 'str16', 'uint16_t', 'uint16_t', 'void *', 'uint16_t *', 'uint32_t *', 'void *']);
// DsGetSpnW(ServiceType, ServiceClass, ServiceName, InstancePort, cInstanceNames, pInstanceNames, pInstancePorts, pcSpn, prpszSpn)
// ServiceType : DS_SPN_NAME_TYPE -> 'int32_t'
// ServiceClass : LPCWSTR -> 'str16'
// ServiceName : LPCWSTR optional -> 'str16'
// InstancePort : WORD -> 'uint16_t'
// cInstanceNames : WORD -> 'uint16_t'
// pInstanceNames : LPCWSTR* optional -> 'void *'
// pInstancePorts : WORD* optional -> 'uint16_t *'
// pcSpn : DWORD* out -> 'uint32_t *'
// prpszSpn : LPWSTR** out -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("NTDSAPI.dll", {
DsGetSpnW: { parameters: ["i32", "buffer", "buffer", "u16", "u16", "pointer", "pointer", "pointer", "pointer"], result: "u32" },
});
// lib.symbols.DsGetSpnW(ServiceType, ServiceClass, ServiceName, InstancePort, cInstanceNames, pInstanceNames, pInstancePorts, pcSpn, prpszSpn)
// ServiceType : DS_SPN_NAME_TYPE -> "i32"
// ServiceClass : LPCWSTR -> "buffer"
// ServiceName : LPCWSTR optional -> "buffer"
// InstancePort : WORD -> "u16"
// cInstanceNames : WORD -> "u16"
// pInstanceNames : LPCWSTR* optional -> "pointer"
// pInstancePorts : WORD* optional -> "pointer"
// pcSpn : DWORD* out -> "pointer"
// prpszSpn : LPWSTR** out -> "pointer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
uint32_t DsGetSpnW(
int32_t ServiceType,
const uint16_t* ServiceClass,
const uint16_t* ServiceName,
uint16_t InstancePort,
uint16_t cInstanceNames,
uint16_t** pInstanceNames,
uint16_t* pInstancePorts,
uint32_t* pcSpn,
uint16_t*** prpszSpn);
C, "NTDSAPI.dll");
// $ffi->DsGetSpnW(ServiceType, ServiceClass, ServiceName, InstancePort, cInstanceNames, pInstanceNames, pInstancePorts, pcSpn, prpszSpn);
// ServiceType : DS_SPN_NAME_TYPE
// ServiceClass : LPCWSTR
// ServiceName : LPCWSTR optional
// InstancePort : WORD
// cInstanceNames : WORD
// pInstanceNames : LPCWSTR* optional
// pInstancePorts : WORD* optional
// pcSpn : DWORD* out
// prpszSpn : LPWSTR** 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 Ntdsapi extends StdCallLibrary {
Ntdsapi INSTANCE = Native.load("ntdsapi", Ntdsapi.class, W32APIOptions.UNICODE_OPTIONS);
int DsGetSpnW(
int ServiceType, // DS_SPN_NAME_TYPE
WString ServiceClass, // LPCWSTR
WString ServiceName, // LPCWSTR optional
short InstancePort, // WORD
short cInstanceNames, // WORD
PointerByReference pInstanceNames, // LPCWSTR* optional
ShortByReference pInstancePorts, // WORD* optional
IntByReference pcSpn, // DWORD* out
PointerByReference prpszSpn // LPWSTR** out
);
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。@[Link("ntdsapi")]
lib LibNTDSAPI
fun DsGetSpnW = DsGetSpnW(
ServiceType : Int32, # DS_SPN_NAME_TYPE
ServiceClass : UInt16*, # LPCWSTR
ServiceName : UInt16*, # LPCWSTR optional
InstancePort : UInt16, # WORD
cInstanceNames : UInt16, # WORD
pInstanceNames : UInt16**, # LPCWSTR* optional
pInstancePorts : UInt16*, # WORD* optional
pcSpn : UInt32*, # DWORD* out
prpszSpn : UInt16*** # LPWSTR** out
) : UInt32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef DsGetSpnWNative = Uint32 Function(Int32, Pointer<Utf16>, Pointer<Utf16>, Uint16, Uint16, Pointer<Pointer<Utf16>>, Pointer<Uint16>, Pointer<Uint32>, Pointer<Pointer<Utf16>>);
typedef DsGetSpnWDart = int Function(int, Pointer<Utf16>, Pointer<Utf16>, int, int, Pointer<Pointer<Utf16>>, Pointer<Uint16>, Pointer<Uint32>, Pointer<Pointer<Utf16>>);
final DsGetSpnW = DynamicLibrary.open('NTDSAPI.dll')
.lookupFunction<DsGetSpnWNative, DsGetSpnWDart>('DsGetSpnW');
// ServiceType : DS_SPN_NAME_TYPE -> Int32
// ServiceClass : LPCWSTR -> Pointer<Utf16>
// ServiceName : LPCWSTR optional -> Pointer<Utf16>
// InstancePort : WORD -> Uint16
// cInstanceNames : WORD -> Uint16
// pInstanceNames : LPCWSTR* optional -> Pointer<Pointer<Utf16>>
// pInstancePorts : WORD* optional -> Pointer<Uint16>
// pcSpn : DWORD* out -> Pointer<Uint32>
// prpszSpn : LPWSTR** out -> Pointer<Pointer<Utf16>>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function DsGetSpnW(
ServiceType: Integer; // DS_SPN_NAME_TYPE
ServiceClass: PWideChar; // LPCWSTR
ServiceName: PWideChar; // LPCWSTR optional
InstancePort: Word; // WORD
cInstanceNames: Word; // WORD
pInstanceNames: PPWideChar; // LPCWSTR* optional
pInstancePorts: Pointer; // WORD* optional
pcSpn: Pointer; // DWORD* out
prpszSpn: PPWideChar // LPWSTR** out
): DWORD; stdcall;
external 'NTDSAPI.dll' name 'DsGetSpnW';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "DsGetSpnW"
c_DsGetSpnW :: Int32 -> CWString -> CWString -> Word16 -> Word16 -> Ptr CWString -> Ptr Word16 -> Ptr Word32 -> Ptr CWString -> IO Word32
-- ServiceType : DS_SPN_NAME_TYPE -> Int32
-- ServiceClass : LPCWSTR -> CWString
-- ServiceName : LPCWSTR optional -> CWString
-- InstancePort : WORD -> Word16
-- cInstanceNames : WORD -> Word16
-- pInstanceNames : LPCWSTR* optional -> Ptr CWString
-- pInstancePorts : WORD* optional -> Ptr Word16
-- pcSpn : DWORD* out -> Ptr Word32
-- prpszSpn : LPWSTR** out -> Ptr CWString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let dsgetspnw =
foreign "DsGetSpnW"
(int32_t @-> (ptr uint16_t) @-> (ptr uint16_t) @-> uint16_t @-> uint16_t @-> (ptr (ptr uint16_t)) @-> (ptr uint16_t) @-> (ptr uint32_t) @-> (ptr (ptr uint16_t)) @-> returning uint32_t)
(* ServiceType : DS_SPN_NAME_TYPE -> int32_t *)
(* ServiceClass : LPCWSTR -> (ptr uint16_t) *)
(* ServiceName : LPCWSTR optional -> (ptr uint16_t) *)
(* InstancePort : WORD -> uint16_t *)
(* cInstanceNames : WORD -> uint16_t *)
(* pInstanceNames : LPCWSTR* optional -> (ptr (ptr uint16_t)) *)
(* pInstancePorts : WORD* optional -> (ptr uint16_t) *)
(* pcSpn : DWORD* out -> (ptr uint32_t) *)
(* prpszSpn : LPWSTR** out -> (ptr (ptr uint16_t)) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library ntdsapi (t "NTDSAPI.dll"))
(cffi:use-foreign-library ntdsapi)
(cffi:defcfun ("DsGetSpnW" ds-get-spn-w :convention :stdcall) :uint32
(service-type :int32) ; DS_SPN_NAME_TYPE
(service-class (:string :encoding :utf-16le)) ; LPCWSTR
(service-name (:string :encoding :utf-16le)) ; LPCWSTR optional
(instance-port :uint16) ; WORD
(c-instance-names :uint16) ; WORD
(p-instance-names :pointer) ; LPCWSTR* optional
(p-instance-ports :pointer) ; WORD* optional
(pc-spn :pointer) ; DWORD* out
(prpsz-spn :pointer)) ; LPWSTR** out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $DsGetSpnW = Win32::API::More->new('NTDSAPI',
'DWORD DsGetSpnW(int ServiceType, LPCWSTR ServiceClass, LPCWSTR ServiceName, WORD InstancePort, WORD cInstanceNames, LPVOID pInstanceNames, LPVOID pInstancePorts, LPVOID pcSpn, LPVOID prpszSpn)');
# my $ret = $DsGetSpnW->Call($ServiceType, $ServiceClass, $ServiceName, $InstancePort, $cInstanceNames, $pInstanceNames, $pInstancePorts, $pcSpn, $prpszSpn);
# ServiceType : DS_SPN_NAME_TYPE -> int
# ServiceClass : LPCWSTR -> LPCWSTR
# ServiceName : LPCWSTR optional -> LPCWSTR
# InstancePort : WORD -> WORD
# cInstanceNames : WORD -> WORD
# pInstanceNames : LPCWSTR* optional -> LPVOID
# pInstancePorts : WORD* optional -> LPVOID
# pcSpn : DWORD* out -> LPVOID
# prpszSpn : LPWSTR** out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。関連項目
文字セット違い
- f DsGetSpnA (ANSI版) — サービス情報からSPNの配列を生成する(ANSI版)。
使用する型