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

DsInheritSecurityIdentityW

関数
移行元のSIDを移行先プリンシパルに継承させ移行元を削除する。
DLLNTDSAPI.dll文字セットUnicode (-W)呼出規約winapi対応OSWindows Vista 以降

シグネチャ

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

DWORD DsInheritSecurityIdentityW(
    HANDLE hDS,
    DWORD Flags,   // optional
    LPCWSTR SrcPrincipal,
    LPCWSTR DstPrincipal
);

パラメーター

名前方向説明
hDSHANDLEinDsBindで取得したドメインDCへのバインドハンドル。
FlagsDWORDoptional操作の挙動を制御するフラグ。現状は予約で通常0を指定する。
SrcPrincipalLPCWSTRinSIDHistoryの継承元プリンシパル名を指すワイド文字列。処理後この元アカウントは削除される。
DstPrincipalLPCWSTRin継承先プリンシパル名を指すワイド文字列。元のSIDHistoryを引き継ぐ対象。

戻り値の型: DWORD

各言語での呼び出し定義

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

DWORD DsInheritSecurityIdentityW(
    HANDLE hDS,
    DWORD Flags,   // optional
    LPCWSTR SrcPrincipal,
    LPCWSTR DstPrincipal
);
[DllImport("NTDSAPI.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern uint DsInheritSecurityIdentityW(
    IntPtr hDS,   // HANDLE
    uint Flags,   // DWORD optional
    [MarshalAs(UnmanagedType.LPWStr)] string SrcPrincipal,   // LPCWSTR
    [MarshalAs(UnmanagedType.LPWStr)] string DstPrincipal   // LPCWSTR
);
<DllImport("NTDSAPI.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function DsInheritSecurityIdentityW(
    hDS As IntPtr,   ' HANDLE
    Flags As UInteger,   ' DWORD optional
    <MarshalAs(UnmanagedType.LPWStr)> SrcPrincipal As String,   ' LPCWSTR
    <MarshalAs(UnmanagedType.LPWStr)> DstPrincipal As String   ' LPCWSTR
) As UInteger
End Function
' hDS : HANDLE
' Flags : DWORD optional
' SrcPrincipal : LPCWSTR
' DstPrincipal : LPCWSTR
Declare PtrSafe Function DsInheritSecurityIdentityW Lib "ntdsapi" ( _
    ByVal hDS As LongPtr, _
    ByVal Flags As Long, _
    ByVal SrcPrincipal As LongPtr, _
    ByVal DstPrincipal 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

DsInheritSecurityIdentityW = ctypes.windll.ntdsapi.DsInheritSecurityIdentityW
DsInheritSecurityIdentityW.restype = wintypes.DWORD
DsInheritSecurityIdentityW.argtypes = [
    wintypes.HANDLE,  # hDS : HANDLE
    wintypes.DWORD,  # Flags : DWORD optional
    wintypes.LPCWSTR,  # SrcPrincipal : LPCWSTR
    wintypes.LPCWSTR,  # DstPrincipal : LPCWSTR
]
require 'fiddle'
require 'fiddle/import'

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

var (
	ntdsapi = windows.NewLazySystemDLL("NTDSAPI.dll")
	procDsInheritSecurityIdentityW = ntdsapi.NewProc("DsInheritSecurityIdentityW")
)

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

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

typedef DsInheritSecurityIdentityWNative = Uint32 Function(Pointer<Void>, Uint32, Pointer<Utf16>, Pointer<Utf16>);
typedef DsInheritSecurityIdentityWDart = int Function(Pointer<Void>, int, Pointer<Utf16>, Pointer<Utf16>);
final DsInheritSecurityIdentityW = DynamicLibrary.open('NTDSAPI.dll')
    .lookupFunction<DsInheritSecurityIdentityWNative, DsInheritSecurityIdentityWDart>('DsInheritSecurityIdentityW');
// hDS : HANDLE -> Pointer<Void>
// Flags : DWORD optional -> Uint32
// SrcPrincipal : LPCWSTR -> Pointer<Utf16>
// DstPrincipal : LPCWSTR -> Pointer<Utf16>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function DsInheritSecurityIdentityW(
  hDS: THandle;   // HANDLE
  Flags: DWORD;   // DWORD optional
  SrcPrincipal: PWideChar;   // LPCWSTR
  DstPrincipal: PWideChar   // LPCWSTR
): DWORD; stdcall;
  external 'NTDSAPI.dll' name 'DsInheritSecurityIdentityW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "DsInheritSecurityIdentityW"
  c_DsInheritSecurityIdentityW :: Ptr () -> Word32 -> CWString -> CWString -> IO Word32
-- hDS : HANDLE -> Ptr ()
-- Flags : DWORD optional -> Word32
-- SrcPrincipal : LPCWSTR -> CWString
-- DstPrincipal : LPCWSTR -> CWString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let dsinheritsecurityidentityw =
  foreign "DsInheritSecurityIdentityW"
    ((ptr void) @-> uint32_t @-> (ptr uint16_t) @-> (ptr uint16_t) @-> returning uint32_t)
(* hDS : HANDLE -> (ptr void) *)
(* Flags : DWORD optional -> uint32_t *)
(* SrcPrincipal : LPCWSTR -> (ptr uint16_t) *)
(* DstPrincipal : LPCWSTR -> (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 ("DsInheritSecurityIdentityW" ds-inherit-security-identity-w :convention :stdcall) :uint32
  (h-ds :pointer)   ; HANDLE
  (flags :uint32)   ; DWORD optional
  (src-principal (:string :encoding :utf-16le))   ; LPCWSTR
  (dst-principal (:string :encoding :utf-16le)))   ; LPCWSTR
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $DsInheritSecurityIdentityW = Win32::API::More->new('NTDSAPI',
    'DWORD DsInheritSecurityIdentityW(HANDLE hDS, DWORD Flags, LPCWSTR SrcPrincipal, LPCWSTR DstPrincipal)');
# my $ret = $DsInheritSecurityIdentityW->Call($hDS, $Flags, $SrcPrincipal, $DstPrincipal);
# hDS : HANDLE -> HANDLE
# Flags : DWORD optional -> DWORD
# SrcPrincipal : LPCWSTR -> LPCWSTR
# DstPrincipal : LPCWSTR -> LPCWSTR
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

文字セット違い