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

DsClientMakeSpnForTargetServerW

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

シグネチャ

// NTDSAPI.dll  (Unicode / -W)
#include <windows.h>

DWORD DsClientMakeSpnForTargetServerW(
    LPCWSTR ServiceClass,
    LPCWSTR ServiceName,
    DWORD* pcSpnLength,
    LPWSTR pszSpn
);

パラメーター

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

戻り値の型: DWORD

各言語での呼び出し定義

// NTDSAPI.dll  (Unicode / -W)
#include <windows.h>

DWORD DsClientMakeSpnForTargetServerW(
    LPCWSTR ServiceClass,
    LPCWSTR ServiceName,
    DWORD* pcSpnLength,
    LPWSTR pszSpn
);
[DllImport("NTDSAPI.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern uint DsClientMakeSpnForTargetServerW(
    [MarshalAs(UnmanagedType.LPWStr)] string ServiceClass,   // LPCWSTR
    [MarshalAs(UnmanagedType.LPWStr)] string ServiceName,   // LPCWSTR
    ref uint pcSpnLength,   // DWORD* in/out
    [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszSpn   // LPWSTR out
);
<DllImport("NTDSAPI.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function DsClientMakeSpnForTargetServerW(
    <MarshalAs(UnmanagedType.LPWStr)> ServiceClass As String,   ' LPCWSTR
    <MarshalAs(UnmanagedType.LPWStr)> ServiceName As String,   ' LPCWSTR
    ByRef pcSpnLength As UInteger,   ' DWORD* in/out
    <MarshalAs(UnmanagedType.LPWStr)> pszSpn As System.Text.StringBuilder   ' LPWSTR out
) As UInteger
End Function
' ServiceClass : LPCWSTR
' ServiceName : LPCWSTR
' pcSpnLength : DWORD* in/out
' pszSpn : LPWSTR out
Declare PtrSafe Function DsClientMakeSpnForTargetServerW Lib "ntdsapi" ( _
    ByVal ServiceClass As LongPtr, _
    ByVal ServiceName As LongPtr, _
    ByRef pcSpnLength As Long, _
    ByVal pszSpn 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

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

lib = Fiddle.dlopen('NTDSAPI.dll')
DsClientMakeSpnForTargetServerW = Fiddle::Function.new(
  lib['DsClientMakeSpnForTargetServerW'],
  [
    Fiddle::TYPE_VOIDP,  # ServiceClass : LPCWSTR
    Fiddle::TYPE_VOIDP,  # ServiceName : LPCWSTR
    Fiddle::TYPE_VOIDP,  # pcSpnLength : DWORD* in/out
    Fiddle::TYPE_VOIDP,  # pszSpn : LPWSTR out
  ],
  -Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "ntdsapi")]
extern "system" {
    fn DsClientMakeSpnForTargetServerW(
        ServiceClass: *const u16,  // LPCWSTR
        ServiceName: *const u16,  // LPCWSTR
        pcSpnLength: *mut u32,  // DWORD* in/out
        pszSpn: *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 DsClientMakeSpnForTargetServerW([MarshalAs(UnmanagedType.LPWStr)] string ServiceClass, [MarshalAs(UnmanagedType.LPWStr)] string ServiceName, ref uint pcSpnLength, [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszSpn);
"@
$api = Add-Type -MemberDefinition $sig -Name 'NTDSAPI_DsClientMakeSpnForTargetServerW' -Namespace Win32 -PassThru
# $api::DsClientMakeSpnForTargetServerW(ServiceClass, ServiceName, pcSpnLength, pszSpn)
#uselib "NTDSAPI.dll"
#func global DsClientMakeSpnForTargetServerW "DsClientMakeSpnForTargetServerW" wptr, wptr, wptr, wptr
; DsClientMakeSpnForTargetServerW ServiceClass, ServiceName, varptr(pcSpnLength), varptr(pszSpn)   ; 戻り値は stat
; ServiceClass : LPCWSTR -> "wptr"
; ServiceName : LPCWSTR -> "wptr"
; pcSpnLength : DWORD* in/out -> "wptr"
; pszSpn : LPWSTR out -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "NTDSAPI.dll"
#cfunc global DsClientMakeSpnForTargetServerW "DsClientMakeSpnForTargetServerW" wstr, wstr, var, var
; res = DsClientMakeSpnForTargetServerW(ServiceClass, ServiceName, pcSpnLength, pszSpn)
; ServiceClass : LPCWSTR -> "wstr"
; ServiceName : LPCWSTR -> "wstr"
; pcSpnLength : DWORD* in/out -> "var"
; pszSpn : LPWSTR out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; DWORD DsClientMakeSpnForTargetServerW(LPCWSTR ServiceClass, LPCWSTR ServiceName, DWORD* pcSpnLength, LPWSTR pszSpn)
#uselib "NTDSAPI.dll"
#cfunc global DsClientMakeSpnForTargetServerW "DsClientMakeSpnForTargetServerW" wstr, wstr, var, var
; res = DsClientMakeSpnForTargetServerW(ServiceClass, ServiceName, pcSpnLength, pszSpn)
; ServiceClass : LPCWSTR -> "wstr"
; ServiceName : LPCWSTR -> "wstr"
; pcSpnLength : DWORD* in/out -> "var"
; pszSpn : LPWSTR out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	ntdsapi = windows.NewLazySystemDLL("NTDSAPI.dll")
	procDsClientMakeSpnForTargetServerW = ntdsapi.NewProc("DsClientMakeSpnForTargetServerW")
)

// ServiceClass (LPCWSTR), ServiceName (LPCWSTR), pcSpnLength (DWORD* in/out), pszSpn (LPWSTR out)
r1, _, err := procDsClientMakeSpnForTargetServerW.Call(
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(ServiceClass))),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(ServiceName))),
	uintptr(pcSpnLength),
	uintptr(pszSpn),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // DWORD
function DsClientMakeSpnForTargetServerW(
  ServiceClass: PWideChar;   // LPCWSTR
  ServiceName: PWideChar;   // LPCWSTR
  pcSpnLength: Pointer;   // DWORD* in/out
  pszSpn: PWideChar   // LPWSTR out
): DWORD; stdcall;
  external 'NTDSAPI.dll' name 'DsClientMakeSpnForTargetServerW';
result := DllCall("NTDSAPI\DsClientMakeSpnForTargetServerW"
    , "WStr", ServiceClass   ; LPCWSTR
    , "WStr", ServiceName   ; LPCWSTR
    , "Ptr", pcSpnLength   ; DWORD* in/out
    , "Ptr", pszSpn   ; LPWSTR out
    , "UInt")   ; return: DWORD
●DsClientMakeSpnForTargetServerW(ServiceClass, ServiceName, pcSpnLength, pszSpn) = DLL("NTDSAPI.dll", "dword DsClientMakeSpnForTargetServerW(char*, char*, void*, char*)")
# 呼び出し: DsClientMakeSpnForTargetServerW(ServiceClass, ServiceName, pcSpnLength, pszSpn)
# ServiceClass : LPCWSTR -> "char*"
# ServiceName : LPCWSTR -> "char*"
# pcSpnLength : DWORD* in/out -> "void*"
# pszSpn : LPWSTR out -> "char*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。
const std = @import("std");

extern "ntdsapi" fn DsClientMakeSpnForTargetServerW(
    ServiceClass: [*c]const u16, // LPCWSTR
    ServiceName: [*c]const u16, // LPCWSTR
    pcSpnLength: [*c]u32, // DWORD* in/out
    pszSpn: [*c]u16 // LPWSTR out
) callconv(std.os.windows.WINAPI) u32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。
proc DsClientMakeSpnForTargetServerW(
    ServiceClass: WideCString,  # LPCWSTR
    ServiceName: WideCString,  # LPCWSTR
    pcSpnLength: ptr uint32,  # DWORD* in/out
    pszSpn: ptr uint16  # LPWSTR out
): uint32 {.importc: "DsClientMakeSpnForTargetServerW", stdcall, dynlib: "NTDSAPI.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "ntdsapi");
extern(Windows)
uint DsClientMakeSpnForTargetServerW(
    const(wchar)* ServiceClass,   // LPCWSTR
    const(wchar)* ServiceName,   // LPCWSTR
    uint* pcSpnLength,   // DWORD* in/out
    wchar* pszSpn   // LPWSTR out
);
ccall((:DsClientMakeSpnForTargetServerW, "NTDSAPI.dll"), stdcall, UInt32,
      (Cwstring, Cwstring, Ptr{UInt32}, Ptr{UInt16}),
      ServiceClass, ServiceName, pcSpnLength, pszSpn)
# ServiceClass : LPCWSTR -> Cwstring
# ServiceName : LPCWSTR -> Cwstring
# pcSpnLength : DWORD* in/out -> Ptr{UInt32}
# pszSpn : LPWSTR out -> Ptr{UInt16}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。
local ffi = require("ffi")
ffi.cdef[[
uint32_t DsClientMakeSpnForTargetServerW(
    const uint16_t* ServiceClass,
    const uint16_t* ServiceName,
    uint32_t* pcSpnLength,
    uint16_t* pszSpn);
]]
local ntdsapi = ffi.load("ntdsapi")
-- ntdsapi.DsClientMakeSpnForTargetServerW(ServiceClass, ServiceName, pcSpnLength, pszSpn)
-- ServiceClass : LPCWSTR
-- ServiceName : LPCWSTR
-- pcSpnLength : DWORD* in/out
-- pszSpn : 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 DsClientMakeSpnForTargetServerW = lib.func('__stdcall', 'DsClientMakeSpnForTargetServerW', 'uint32_t', ['str16', 'str16', 'uint32_t *', 'uint16_t *']);
// DsClientMakeSpnForTargetServerW(ServiceClass, ServiceName, pcSpnLength, pszSpn)
// ServiceClass : LPCWSTR -> 'str16'
// ServiceName : LPCWSTR -> 'str16'
// pcSpnLength : DWORD* in/out -> 'uint32_t *'
// pszSpn : LPWSTR out -> 'uint16_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("NTDSAPI.dll", {
  DsClientMakeSpnForTargetServerW: { parameters: ["buffer", "buffer", "pointer", "buffer"], result: "u32" },
});
// lib.symbols.DsClientMakeSpnForTargetServerW(ServiceClass, ServiceName, pcSpnLength, pszSpn)
// ServiceClass : LPCWSTR -> "buffer"
// ServiceName : LPCWSTR -> "buffer"
// pcSpnLength : DWORD* in/out -> "pointer"
// pszSpn : LPWSTR out -> "buffer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
uint32_t DsClientMakeSpnForTargetServerW(
    const uint16_t* ServiceClass,
    const uint16_t* ServiceName,
    uint32_t* pcSpnLength,
    uint16_t* pszSpn);
C, "NTDSAPI.dll");
// $ffi->DsClientMakeSpnForTargetServerW(ServiceClass, ServiceName, pcSpnLength, pszSpn);
// ServiceClass : LPCWSTR
// ServiceName : LPCWSTR
// pcSpnLength : DWORD* in/out
// pszSpn : 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 DsClientMakeSpnForTargetServerW(
        WString ServiceClass,   // LPCWSTR
        WString ServiceName,   // LPCWSTR
        IntByReference pcSpnLength,   // DWORD* in/out
        char[] pszSpn   // LPWSTR out
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("ntdsapi")]
lib LibNTDSAPI
  fun DsClientMakeSpnForTargetServerW = DsClientMakeSpnForTargetServerW(
    ServiceClass : UInt16*,   # LPCWSTR
    ServiceName : UInt16*,   # LPCWSTR
    pcSpnLength : UInt32*,   # DWORD* in/out
    pszSpn : 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 DsClientMakeSpnForTargetServerWNative = Uint32 Function(Pointer<Utf16>, Pointer<Utf16>, Pointer<Uint32>, Pointer<Utf16>);
typedef DsClientMakeSpnForTargetServerWDart = int Function(Pointer<Utf16>, Pointer<Utf16>, Pointer<Uint32>, Pointer<Utf16>);
final DsClientMakeSpnForTargetServerW = DynamicLibrary.open('NTDSAPI.dll')
    .lookupFunction<DsClientMakeSpnForTargetServerWNative, DsClientMakeSpnForTargetServerWDart>('DsClientMakeSpnForTargetServerW');
// ServiceClass : LPCWSTR -> Pointer<Utf16>
// ServiceName : LPCWSTR -> Pointer<Utf16>
// pcSpnLength : DWORD* in/out -> Pointer<Uint32>
// pszSpn : LPWSTR out -> Pointer<Utf16>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function DsClientMakeSpnForTargetServerW(
  ServiceClass: PWideChar;   // LPCWSTR
  ServiceName: PWideChar;   // LPCWSTR
  pcSpnLength: Pointer;   // DWORD* in/out
  pszSpn: PWideChar   // LPWSTR out
): DWORD; stdcall;
  external 'NTDSAPI.dll' name 'DsClientMakeSpnForTargetServerW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

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

let dsclientmakespnfortargetserverw =
  foreign "DsClientMakeSpnForTargetServerW"
    ((ptr uint16_t) @-> (ptr uint16_t) @-> (ptr uint32_t) @-> (ptr uint16_t) @-> returning uint32_t)
(* ServiceClass : LPCWSTR -> (ptr uint16_t) *)
(* ServiceName : LPCWSTR -> (ptr uint16_t) *)
(* pcSpnLength : DWORD* in/out -> (ptr uint32_t) *)
(* pszSpn : LPWSTR out -> (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 ("DsClientMakeSpnForTargetServerW" ds-client-make-spn-for-target-server-w :convention :stdcall) :uint32
  (service-class (:string :encoding :utf-16le))   ; LPCWSTR
  (service-name (:string :encoding :utf-16le))   ; LPCWSTR
  (pc-spn-length :pointer)   ; DWORD* in/out
  (psz-spn :pointer))   ; LPWSTR out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $DsClientMakeSpnForTargetServerW = Win32::API::More->new('NTDSAPI',
    'DWORD DsClientMakeSpnForTargetServerW(LPCWSTR ServiceClass, LPCWSTR ServiceName, LPVOID pcSpnLength, LPWSTR pszSpn)');
# my $ret = $DsClientMakeSpnForTargetServerW->Call($ServiceClass, $ServiceName, $pcSpnLength, $pszSpn);
# ServiceClass : LPCWSTR -> LPCWSTR
# ServiceName : LPCWSTR -> LPCWSTR
# pcSpnLength : DWORD* in/out -> LPVOID
# pszSpn : LPWSTR out -> LPWSTR
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

文字セット違い