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

DsServerRegisterSpnW

関数
サーバーのSPNをユーザーオブジェクトに登録する(Unicode版)。
DLLNTDSAPI.dll文字セットUnicode (-W)呼出規約winapi対応OSWindows Vista 以降

シグネチャ

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

DWORD DsServerRegisterSpnW(
    DS_SPN_WRITE_OP Operation,
    LPCWSTR ServiceClass,
    LPCWSTR UserObjectDN   // optional
);

パラメーター

名前方向説明
OperationDS_SPN_WRITE_OPinSPNの追加・置換・削除を指定するDS_SPN_WRITE_OP列挙値。
ServiceClassLPCWSTRin登録するサービスクラス名(ワイド)。
UserObjectDNLPCWSTRinoptionalSPNを登録する対象ユーザーオブジェクトのDN。NULLで自コンピュータ。

戻り値の型: DWORD

各言語での呼び出し定義

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

DWORD DsServerRegisterSpnW(
    DS_SPN_WRITE_OP Operation,
    LPCWSTR ServiceClass,
    LPCWSTR UserObjectDN   // optional
);
[DllImport("NTDSAPI.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern uint DsServerRegisterSpnW(
    int Operation,   // DS_SPN_WRITE_OP
    [MarshalAs(UnmanagedType.LPWStr)] string ServiceClass,   // LPCWSTR
    [MarshalAs(UnmanagedType.LPWStr)] string UserObjectDN   // LPCWSTR optional
);
<DllImport("NTDSAPI.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function DsServerRegisterSpnW(
    Operation As Integer,   ' DS_SPN_WRITE_OP
    <MarshalAs(UnmanagedType.LPWStr)> ServiceClass As String,   ' LPCWSTR
    <MarshalAs(UnmanagedType.LPWStr)> UserObjectDN As String   ' LPCWSTR optional
) As UInteger
End Function
' Operation : DS_SPN_WRITE_OP
' ServiceClass : LPCWSTR
' UserObjectDN : LPCWSTR optional
Declare PtrSafe Function DsServerRegisterSpnW Lib "ntdsapi" ( _
    ByVal Operation As Long, _
    ByVal ServiceClass As LongPtr, _
    ByVal UserObjectDN 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

DsServerRegisterSpnW = ctypes.windll.ntdsapi.DsServerRegisterSpnW
DsServerRegisterSpnW.restype = wintypes.DWORD
DsServerRegisterSpnW.argtypes = [
    ctypes.c_int,  # Operation : DS_SPN_WRITE_OP
    wintypes.LPCWSTR,  # ServiceClass : LPCWSTR
    wintypes.LPCWSTR,  # UserObjectDN : LPCWSTR optional
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('NTDSAPI.dll')
DsServerRegisterSpnW = Fiddle::Function.new(
  lib['DsServerRegisterSpnW'],
  [
    Fiddle::TYPE_INT,  # Operation : DS_SPN_WRITE_OP
    Fiddle::TYPE_VOIDP,  # ServiceClass : LPCWSTR
    Fiddle::TYPE_VOIDP,  # UserObjectDN : LPCWSTR optional
  ],
  -Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "ntdsapi")]
extern "system" {
    fn DsServerRegisterSpnW(
        Operation: i32,  // DS_SPN_WRITE_OP
        ServiceClass: *const u16,  // LPCWSTR
        UserObjectDN: *const u16  // LPCWSTR optional
    ) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("NTDSAPI.dll", CharSet = CharSet.Unicode)]
public static extern uint DsServerRegisterSpnW(int Operation, [MarshalAs(UnmanagedType.LPWStr)] string ServiceClass, [MarshalAs(UnmanagedType.LPWStr)] string UserObjectDN);
"@
$api = Add-Type -MemberDefinition $sig -Name 'NTDSAPI_DsServerRegisterSpnW' -Namespace Win32 -PassThru
# $api::DsServerRegisterSpnW(Operation, ServiceClass, UserObjectDN)
#uselib "NTDSAPI.dll"
#func global DsServerRegisterSpnW "DsServerRegisterSpnW" wptr, wptr, wptr
; DsServerRegisterSpnW Operation, ServiceClass, UserObjectDN   ; 戻り値は stat
; Operation : DS_SPN_WRITE_OP -> "wptr"
; ServiceClass : LPCWSTR -> "wptr"
; UserObjectDN : LPCWSTR optional -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "NTDSAPI.dll"
#cfunc global DsServerRegisterSpnW "DsServerRegisterSpnW" int, wstr, wstr
; res = DsServerRegisterSpnW(Operation, ServiceClass, UserObjectDN)
; Operation : DS_SPN_WRITE_OP -> "int"
; ServiceClass : LPCWSTR -> "wstr"
; UserObjectDN : LPCWSTR optional -> "wstr"
; DWORD DsServerRegisterSpnW(DS_SPN_WRITE_OP Operation, LPCWSTR ServiceClass, LPCWSTR UserObjectDN)
#uselib "NTDSAPI.dll"
#cfunc global DsServerRegisterSpnW "DsServerRegisterSpnW" int, wstr, wstr
; res = DsServerRegisterSpnW(Operation, ServiceClass, UserObjectDN)
; Operation : DS_SPN_WRITE_OP -> "int"
; ServiceClass : LPCWSTR -> "wstr"
; UserObjectDN : LPCWSTR optional -> "wstr"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	ntdsapi = windows.NewLazySystemDLL("NTDSAPI.dll")
	procDsServerRegisterSpnW = ntdsapi.NewProc("DsServerRegisterSpnW")
)

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

extern "ntdsapi" fn DsServerRegisterSpnW(
    Operation: i32, // DS_SPN_WRITE_OP
    ServiceClass: [*c]const u16, // LPCWSTR
    UserObjectDN: [*c]const u16 // LPCWSTR optional
) callconv(std.os.windows.WINAPI) u32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。
proc DsServerRegisterSpnW(
    Operation: int32,  # DS_SPN_WRITE_OP
    ServiceClass: WideCString,  # LPCWSTR
    UserObjectDN: WideCString  # LPCWSTR optional
): uint32 {.importc: "DsServerRegisterSpnW", stdcall, dynlib: "NTDSAPI.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "ntdsapi");
extern(Windows)
uint DsServerRegisterSpnW(
    int Operation,   // DS_SPN_WRITE_OP
    const(wchar)* ServiceClass,   // LPCWSTR
    const(wchar)* UserObjectDN   // LPCWSTR optional
);
ccall((:DsServerRegisterSpnW, "NTDSAPI.dll"), stdcall, UInt32,
      (Int32, Cwstring, Cwstring),
      Operation, ServiceClass, UserObjectDN)
# Operation : DS_SPN_WRITE_OP -> Int32
# ServiceClass : LPCWSTR -> Cwstring
# UserObjectDN : LPCWSTR optional -> Cwstring
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。
local ffi = require("ffi")
ffi.cdef[[
uint32_t DsServerRegisterSpnW(
    int32_t Operation,
    const uint16_t* ServiceClass,
    const uint16_t* UserObjectDN);
]]
local ntdsapi = ffi.load("ntdsapi")
-- ntdsapi.DsServerRegisterSpnW(Operation, ServiceClass, UserObjectDN)
-- Operation : DS_SPN_WRITE_OP
-- ServiceClass : LPCWSTR
-- UserObjectDN : LPCWSTR optional
-- 構造体/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 DsServerRegisterSpnW = lib.func('__stdcall', 'DsServerRegisterSpnW', 'uint32_t', ['int32_t', 'str16', 'str16']);
// DsServerRegisterSpnW(Operation, ServiceClass, UserObjectDN)
// Operation : DS_SPN_WRITE_OP -> 'int32_t'
// ServiceClass : LPCWSTR -> 'str16'
// UserObjectDN : LPCWSTR optional -> 'str16'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("NTDSAPI.dll", {
  DsServerRegisterSpnW: { parameters: ["i32", "buffer", "buffer"], result: "u32" },
});
// lib.symbols.DsServerRegisterSpnW(Operation, ServiceClass, UserObjectDN)
// Operation : DS_SPN_WRITE_OP -> "i32"
// ServiceClass : LPCWSTR -> "buffer"
// UserObjectDN : LPCWSTR optional -> "buffer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
uint32_t DsServerRegisterSpnW(
    int32_t Operation,
    const uint16_t* ServiceClass,
    const uint16_t* UserObjectDN);
C, "NTDSAPI.dll");
// $ffi->DsServerRegisterSpnW(Operation, ServiceClass, UserObjectDN);
// Operation : DS_SPN_WRITE_OP
// ServiceClass : LPCWSTR
// UserObjectDN : LPCWSTR 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 Ntdsapi extends StdCallLibrary {
    Ntdsapi INSTANCE = Native.load("ntdsapi", Ntdsapi.class, W32APIOptions.UNICODE_OPTIONS);
    int DsServerRegisterSpnW(
        int Operation,   // DS_SPN_WRITE_OP
        WString ServiceClass,   // LPCWSTR
        WString UserObjectDN   // LPCWSTR optional
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("ntdsapi")]
lib LibNTDSAPI
  fun DsServerRegisterSpnW = DsServerRegisterSpnW(
    Operation : Int32,   # DS_SPN_WRITE_OP
    ServiceClass : UInt16*,   # LPCWSTR
    UserObjectDN : UInt16*   # LPCWSTR optional
  ) : UInt32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef DsServerRegisterSpnWNative = Uint32 Function(Int32, Pointer<Utf16>, Pointer<Utf16>);
typedef DsServerRegisterSpnWDart = int Function(int, Pointer<Utf16>, Pointer<Utf16>);
final DsServerRegisterSpnW = DynamicLibrary.open('NTDSAPI.dll')
    .lookupFunction<DsServerRegisterSpnWNative, DsServerRegisterSpnWDart>('DsServerRegisterSpnW');
// Operation : DS_SPN_WRITE_OP -> Int32
// ServiceClass : LPCWSTR -> Pointer<Utf16>
// UserObjectDN : LPCWSTR optional -> Pointer<Utf16>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function DsServerRegisterSpnW(
  Operation: Integer;   // DS_SPN_WRITE_OP
  ServiceClass: PWideChar;   // LPCWSTR
  UserObjectDN: PWideChar   // LPCWSTR optional
): DWORD; stdcall;
  external 'NTDSAPI.dll' name 'DsServerRegisterSpnW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "DsServerRegisterSpnW"
  c_DsServerRegisterSpnW :: Int32 -> CWString -> CWString -> IO Word32
-- Operation : DS_SPN_WRITE_OP -> Int32
-- ServiceClass : LPCWSTR -> CWString
-- UserObjectDN : LPCWSTR optional -> CWString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let dsserverregisterspnw =
  foreign "DsServerRegisterSpnW"
    (int32_t @-> (ptr uint16_t) @-> (ptr uint16_t) @-> returning uint32_t)
(* Operation : DS_SPN_WRITE_OP -> int32_t *)
(* ServiceClass : LPCWSTR -> (ptr uint16_t) *)
(* UserObjectDN : LPCWSTR optional -> (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 ("DsServerRegisterSpnW" ds-server-register-spn-w :convention :stdcall) :uint32
  (operation :int32)   ; DS_SPN_WRITE_OP
  (service-class (:string :encoding :utf-16le))   ; LPCWSTR
  (user-object-dn (:string :encoding :utf-16le)))   ; LPCWSTR optional
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $DsServerRegisterSpnW = Win32::API::More->new('NTDSAPI',
    'DWORD DsServerRegisterSpnW(int Operation, LPCWSTR ServiceClass, LPCWSTR UserObjectDN)');
# my $ret = $DsServerRegisterSpnW->Call($Operation, $ServiceClass, $UserObjectDN);
# Operation : DS_SPN_WRITE_OP -> int
# ServiceClass : LPCWSTR -> LPCWSTR
# UserObjectDN : LPCWSTR optional -> LPCWSTR
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

文字セット違い
使用する型