Win32 API 日本語リファレンス
ホームNetworking.ActiveDirectory › DsClientMakeSpnForTargetServerA

DsClientMakeSpnForTargetServerA

関数
対象サーバー向けのクライアント用SPNを生成する(ANSI版)。
DLLNTDSAPI.dll文字セットANSI (-A)呼出規約winapi対応OSWindows Vista 以降

シグネチャ

// NTDSAPI.dll  (ANSI / -A)
#include <windows.h>

DWORD DsClientMakeSpnForTargetServerA(
    LPCSTR ServiceClass,
    LPCSTR ServiceName,
    DWORD* pcSpnLength,
    LPSTR pszSpn
);

パラメーター

名前方向説明
ServiceClassLPCSTRin対象サービスのクラス名(ANSI)。
ServiceNameLPCSTRin対象サーバのDNSホスト名(ANSI)。
pcSpnLengthDWORD*inout入力でpszSpn容量、出力で生成SPNの文字数を受け渡すDWORDへのポインタ。
pszSpnLPSTRout生成されたSPN文字列を受け取るバッファ。NULLで必要サイズ照会。

戻り値の型: DWORD

各言語での呼び出し定義

// NTDSAPI.dll  (ANSI / -A)
#include <windows.h>

DWORD DsClientMakeSpnForTargetServerA(
    LPCSTR ServiceClass,
    LPCSTR ServiceName,
    DWORD* pcSpnLength,
    LPSTR pszSpn
);
[DllImport("NTDSAPI.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
static extern uint DsClientMakeSpnForTargetServerA(
    [MarshalAs(UnmanagedType.LPStr)] string ServiceClass,   // LPCSTR
    [MarshalAs(UnmanagedType.LPStr)] string ServiceName,   // LPCSTR
    ref uint pcSpnLength,   // DWORD* in/out
    [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder pszSpn   // LPSTR out
);
<DllImport("NTDSAPI.dll", CharSet:=CharSet.Ansi, ExactSpelling:=True)>
Public Shared Function DsClientMakeSpnForTargetServerA(
    <MarshalAs(UnmanagedType.LPStr)> ServiceClass As String,   ' LPCSTR
    <MarshalAs(UnmanagedType.LPStr)> ServiceName As String,   ' LPCSTR
    ByRef pcSpnLength As UInteger,   ' DWORD* in/out
    <MarshalAs(UnmanagedType.LPStr)> pszSpn As System.Text.StringBuilder   ' LPSTR out
) As UInteger
End Function
' ServiceClass : LPCSTR
' ServiceName : LPCSTR
' pcSpnLength : DWORD* in/out
' pszSpn : LPSTR out
Declare PtrSafe Function DsClientMakeSpnForTargetServerA Lib "ntdsapi" ( _
    ByVal ServiceClass As String, _
    ByVal ServiceName As String, _
    ByRef pcSpnLength As Long, _
    ByVal pszSpn As String) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

DsClientMakeSpnForTargetServerA = ctypes.windll.ntdsapi.DsClientMakeSpnForTargetServerA
DsClientMakeSpnForTargetServerA.restype = wintypes.DWORD
DsClientMakeSpnForTargetServerA.argtypes = [
    wintypes.LPCSTR,  # ServiceClass : LPCSTR
    wintypes.LPCSTR,  # ServiceName : LPCSTR
    ctypes.POINTER(wintypes.DWORD),  # pcSpnLength : DWORD* in/out
    wintypes.LPSTR,  # pszSpn : LPSTR out
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('NTDSAPI.dll')
DsClientMakeSpnForTargetServerA = Fiddle::Function.new(
  lib['DsClientMakeSpnForTargetServerA'],
  [
    Fiddle::TYPE_VOIDP,  # ServiceClass : LPCSTR
    Fiddle::TYPE_VOIDP,  # ServiceName : LPCSTR
    Fiddle::TYPE_VOIDP,  # pcSpnLength : DWORD* in/out
    Fiddle::TYPE_VOIDP,  # pszSpn : LPSTR out
  ],
  -Fiddle::TYPE_INT)
#[link(name = "ntdsapi")]
extern "system" {
    fn DsClientMakeSpnForTargetServerA(
        ServiceClass: *const u8,  // LPCSTR
        ServiceName: *const u8,  // LPCSTR
        pcSpnLength: *mut u32,  // DWORD* in/out
        pszSpn: *mut u8  // LPSTR out
    ) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("NTDSAPI.dll", CharSet = CharSet.Ansi)]
public static extern uint DsClientMakeSpnForTargetServerA([MarshalAs(UnmanagedType.LPStr)] string ServiceClass, [MarshalAs(UnmanagedType.LPStr)] string ServiceName, ref uint pcSpnLength, [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder pszSpn);
"@
$api = Add-Type -MemberDefinition $sig -Name 'NTDSAPI_DsClientMakeSpnForTargetServerA' -Namespace Win32 -PassThru
# $api::DsClientMakeSpnForTargetServerA(ServiceClass, ServiceName, pcSpnLength, pszSpn)
#uselib "NTDSAPI.dll"
#func global DsClientMakeSpnForTargetServerA "DsClientMakeSpnForTargetServerA" sptr, sptr, sptr, sptr
; DsClientMakeSpnForTargetServerA ServiceClass, ServiceName, varptr(pcSpnLength), varptr(pszSpn)   ; 戻り値は stat
; ServiceClass : LPCSTR -> "sptr"
; ServiceName : LPCSTR -> "sptr"
; pcSpnLength : DWORD* in/out -> "sptr"
; pszSpn : LPSTR out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "NTDSAPI.dll"
#cfunc global DsClientMakeSpnForTargetServerA "DsClientMakeSpnForTargetServerA" str, str, var, var
; res = DsClientMakeSpnForTargetServerA(ServiceClass, ServiceName, pcSpnLength, pszSpn)
; ServiceClass : LPCSTR -> "str"
; ServiceName : LPCSTR -> "str"
; pcSpnLength : DWORD* in/out -> "var"
; pszSpn : LPSTR out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; DWORD DsClientMakeSpnForTargetServerA(LPCSTR ServiceClass, LPCSTR ServiceName, DWORD* pcSpnLength, LPSTR pszSpn)
#uselib "NTDSAPI.dll"
#cfunc global DsClientMakeSpnForTargetServerA "DsClientMakeSpnForTargetServerA" str, str, var, var
; res = DsClientMakeSpnForTargetServerA(ServiceClass, ServiceName, pcSpnLength, pszSpn)
; ServiceClass : LPCSTR -> "str"
; ServiceName : LPCSTR -> "str"
; pcSpnLength : DWORD* in/out -> "var"
; pszSpn : LPSTR out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	ntdsapi = windows.NewLazySystemDLL("NTDSAPI.dll")
	procDsClientMakeSpnForTargetServerA = ntdsapi.NewProc("DsClientMakeSpnForTargetServerA")
)

// ServiceClass (LPCSTR), ServiceName (LPCSTR), pcSpnLength (DWORD* in/out), pszSpn (LPSTR out)
r1, _, err := procDsClientMakeSpnForTargetServerA.Call(
	uintptr(unsafe.Pointer(windows.BytePtrFromString(ServiceClass))),
	uintptr(unsafe.Pointer(windows.BytePtrFromString(ServiceName))),
	uintptr(pcSpnLength),
	uintptr(pszSpn),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // DWORD
function DsClientMakeSpnForTargetServerA(
  ServiceClass: PAnsiChar;   // LPCSTR
  ServiceName: PAnsiChar;   // LPCSTR
  pcSpnLength: Pointer;   // DWORD* in/out
  pszSpn: PAnsiChar   // LPSTR out
): DWORD; stdcall;
  external 'NTDSAPI.dll' name 'DsClientMakeSpnForTargetServerA';
result := DllCall("NTDSAPI\DsClientMakeSpnForTargetServerA"
    , "AStr", ServiceClass   ; LPCSTR
    , "AStr", ServiceName   ; LPCSTR
    , "Ptr", pcSpnLength   ; DWORD* in/out
    , "Ptr", pszSpn   ; LPSTR out
    , "UInt")   ; return: DWORD
●DsClientMakeSpnForTargetServerA(ServiceClass, ServiceName, pcSpnLength, pszSpn) = DLL("NTDSAPI.dll", "dword DsClientMakeSpnForTargetServerA(char*, char*, void*, char*)")
# 呼び出し: DsClientMakeSpnForTargetServerA(ServiceClass, ServiceName, pcSpnLength, pszSpn)
# ServiceClass : LPCSTR -> "char*"
# ServiceName : LPCSTR -> "char*"
# pcSpnLength : DWORD* in/out -> "void*"
# pszSpn : LPSTR out -> "char*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "ntdsapi" fn DsClientMakeSpnForTargetServerA(
    ServiceClass: [*c]const u8, // LPCSTR
    ServiceName: [*c]const u8, // LPCSTR
    pcSpnLength: [*c]u32, // DWORD* in/out
    pszSpn: [*c]u8 // LPSTR out
) callconv(std.os.windows.WINAPI) u32;
proc DsClientMakeSpnForTargetServerA(
    ServiceClass: cstring,  # LPCSTR
    ServiceName: cstring,  # LPCSTR
    pcSpnLength: ptr uint32,  # DWORD* in/out
    pszSpn: ptr char  # LPSTR out
): uint32 {.importc: "DsClientMakeSpnForTargetServerA", stdcall, dynlib: "NTDSAPI.dll".}
pragma(lib, "ntdsapi");
extern(Windows)
uint DsClientMakeSpnForTargetServerA(
    const(char)* ServiceClass,   // LPCSTR
    const(char)* ServiceName,   // LPCSTR
    uint* pcSpnLength,   // DWORD* in/out
    char* pszSpn   // LPSTR out
);
ccall((:DsClientMakeSpnForTargetServerA, "NTDSAPI.dll"), stdcall, UInt32,
      (Cstring, Cstring, Ptr{UInt32}, Ptr{UInt8}),
      ServiceClass, ServiceName, pcSpnLength, pszSpn)
# ServiceClass : LPCSTR -> Cstring
# ServiceName : LPCSTR -> Cstring
# pcSpnLength : DWORD* in/out -> Ptr{UInt32}
# pszSpn : LPSTR out -> Ptr{UInt8}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
uint32_t DsClientMakeSpnForTargetServerA(
    const char* ServiceClass,
    const char* ServiceName,
    uint32_t* pcSpnLength,
    char* pszSpn);
]]
local ntdsapi = ffi.load("ntdsapi")
-- ntdsapi.DsClientMakeSpnForTargetServerA(ServiceClass, ServiceName, pcSpnLength, pszSpn)
-- ServiceClass : LPCSTR
-- ServiceName : LPCSTR
-- pcSpnLength : DWORD* in/out
-- pszSpn : LPSTR out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('NTDSAPI.dll');
const DsClientMakeSpnForTargetServerA = lib.func('__stdcall', 'DsClientMakeSpnForTargetServerA', 'uint32_t', ['str', 'str', 'uint32_t *', 'char *']);
// DsClientMakeSpnForTargetServerA(ServiceClass, ServiceName, pcSpnLength, pszSpn)
// ServiceClass : LPCSTR -> 'str'
// ServiceName : LPCSTR -> 'str'
// pcSpnLength : DWORD* in/out -> 'uint32_t *'
// pszSpn : LPSTR out -> 'char *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("NTDSAPI.dll", {
  DsClientMakeSpnForTargetServerA: { parameters: ["buffer", "buffer", "pointer", "buffer"], result: "u32" },
});
// lib.symbols.DsClientMakeSpnForTargetServerA(ServiceClass, ServiceName, pcSpnLength, pszSpn)
// ServiceClass : LPCSTR -> "buffer"
// ServiceName : LPCSTR -> "buffer"
// pcSpnLength : DWORD* in/out -> "pointer"
// pszSpn : LPSTR out -> "buffer"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
uint32_t DsClientMakeSpnForTargetServerA(
    const char* ServiceClass,
    const char* ServiceName,
    uint32_t* pcSpnLength,
    char* pszSpn);
C, "NTDSAPI.dll");
// $ffi->DsClientMakeSpnForTargetServerA(ServiceClass, ServiceName, pcSpnLength, pszSpn);
// ServiceClass : LPCSTR
// ServiceName : LPCSTR
// pcSpnLength : DWORD* in/out
// pszSpn : LPSTR 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.ASCII_OPTIONS);
    int DsClientMakeSpnForTargetServerA(
        String ServiceClass,   // LPCSTR
        String ServiceName,   // LPCSTR
        IntByReference pcSpnLength,   // DWORD* in/out
        byte[] pszSpn   // LPSTR out
    );
}
@[Link("ntdsapi")]
lib LibNTDSAPI
  fun DsClientMakeSpnForTargetServerA = DsClientMakeSpnForTargetServerA(
    ServiceClass : UInt8*,   # LPCSTR
    ServiceName : UInt8*,   # LPCSTR
    pcSpnLength : UInt32*,   # DWORD* in/out
    pszSpn : UInt8*   # LPSTR out
  ) : UInt32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef DsClientMakeSpnForTargetServerANative = Uint32 Function(Pointer<Utf8>, Pointer<Utf8>, Pointer<Uint32>, Pointer<Utf8>);
typedef DsClientMakeSpnForTargetServerADart = int Function(Pointer<Utf8>, Pointer<Utf8>, Pointer<Uint32>, Pointer<Utf8>);
final DsClientMakeSpnForTargetServerA = DynamicLibrary.open('NTDSAPI.dll')
    .lookupFunction<DsClientMakeSpnForTargetServerANative, DsClientMakeSpnForTargetServerADart>('DsClientMakeSpnForTargetServerA');
// ServiceClass : LPCSTR -> Pointer<Utf8>
// ServiceName : LPCSTR -> Pointer<Utf8>
// pcSpnLength : DWORD* in/out -> Pointer<Uint32>
// pszSpn : LPSTR out -> Pointer<Utf8>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function DsClientMakeSpnForTargetServerA(
  ServiceClass: PAnsiChar;   // LPCSTR
  ServiceName: PAnsiChar;   // LPCSTR
  pcSpnLength: Pointer;   // DWORD* in/out
  pszSpn: PAnsiChar   // LPSTR out
): DWORD; stdcall;
  external 'NTDSAPI.dll' name 'DsClientMakeSpnForTargetServerA';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "DsClientMakeSpnForTargetServerA"
  c_DsClientMakeSpnForTargetServerA :: CString -> CString -> Ptr Word32 -> CString -> IO Word32
-- ServiceClass : LPCSTR -> CString
-- ServiceName : LPCSTR -> CString
-- pcSpnLength : DWORD* in/out -> Ptr Word32
-- pszSpn : LPSTR out -> CString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let dsclientmakespnfortargetservera =
  foreign "DsClientMakeSpnForTargetServerA"
    (string @-> string @-> (ptr uint32_t) @-> string @-> returning uint32_t)
(* ServiceClass : LPCSTR -> string *)
(* ServiceName : LPCSTR -> string *)
(* pcSpnLength : DWORD* in/out -> (ptr uint32_t) *)
(* pszSpn : LPSTR out -> string *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library ntdsapi (t "NTDSAPI.dll"))
(cffi:use-foreign-library ntdsapi)

(cffi:defcfun ("DsClientMakeSpnForTargetServerA" ds-client-make-spn-for-target-server-a :convention :stdcall) :uint32
  (service-class :string)   ; LPCSTR
  (service-name :string)   ; LPCSTR
  (pc-spn-length :pointer)   ; DWORD* in/out
  (psz-spn :pointer))   ; LPSTR out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $DsClientMakeSpnForTargetServerA = Win32::API::More->new('NTDSAPI',
    'DWORD DsClientMakeSpnForTargetServerA(LPCSTR ServiceClass, LPCSTR ServiceName, LPVOID pcSpnLength, LPSTR pszSpn)');
# my $ret = $DsClientMakeSpnForTargetServerA->Call($ServiceClass, $ServiceName, $pcSpnLength, $pszSpn);
# ServiceClass : LPCSTR -> LPCSTR
# ServiceName : LPCSTR -> LPCSTR
# pcSpnLength : DWORD* in/out -> LPVOID
# pszSpn : LPSTR out -> LPSTR
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

文字セット違い