Win32 API 日本語リファレンス
ホームSecurity › CopySid

CopySid

関数
指定バッファにSIDを複製する。
DLLADVAPI32.dll呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

// ADVAPI32.dll
#include <windows.h>

BOOL CopySid(
    DWORD nDestinationSidLength,
    PSID pDestinationSid,
    PSID pSourceSid
);

パラメーター

名前方向説明
nDestinationSidLengthDWORDinSID のコピーを受け取るバッファの長さをバイト単位で指定します。
pDestinationSidPSIDoutコピー元の SID 構造体のコピーを受け取るバッファへのポインターです。
pSourceSidPSIDinpDestinationSid パラメーターが指すバッファに関数がコピーする SID 構造体へのポインターです。

戻り値の型: BOOL

公式ドキュメント

セキュリティ識別子 (SID) をバッファにコピーします。

戻り値

関数が成功した場合、戻り値は 0 以外の値です。

関数が失敗した場合、戻り値は 0 です。拡張エラー情報を取得するには、 GetLastError を呼び出します。

解説(Remarks)

アプリケーションは CopySid 関数を使用して、アクセス トークン内の SID (たとえば TOKEN_GROUPS 構造体内の SID) のコピーを作成し、アクセス制御エントリ (ACE) で使用できます。

この関数を使用する例については、ログオン SID の取得を参照してください。

出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

// ADVAPI32.dll
#include <windows.h>

BOOL CopySid(
    DWORD nDestinationSidLength,
    PSID pDestinationSid,
    PSID pSourceSid
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("ADVAPI32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool CopySid(
    uint nDestinationSidLength,   // DWORD
    IntPtr pDestinationSid,   // PSID out
    IntPtr pSourceSid   // PSID
);
<DllImport("ADVAPI32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function CopySid(
    nDestinationSidLength As UInteger,   ' DWORD
    pDestinationSid As IntPtr,   ' PSID out
    pSourceSid As IntPtr   ' PSID
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' nDestinationSidLength : DWORD
' pDestinationSid : PSID out
' pSourceSid : PSID
Declare PtrSafe Function CopySid Lib "advapi32" ( _
    ByVal nDestinationSidLength As Long, _
    ByVal pDestinationSid As LongPtr, _
    ByVal pSourceSid As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

CopySid = ctypes.windll.advapi32.CopySid
CopySid.restype = wintypes.BOOL
CopySid.argtypes = [
    wintypes.DWORD,  # nDestinationSidLength : DWORD
    wintypes.HANDLE,  # pDestinationSid : PSID out
    wintypes.HANDLE,  # pSourceSid : PSID
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('ADVAPI32.dll')
CopySid = Fiddle::Function.new(
  lib['CopySid'],
  [
    -Fiddle::TYPE_INT,  # nDestinationSidLength : DWORD
    Fiddle::TYPE_VOIDP,  # pDestinationSid : PSID out
    Fiddle::TYPE_VOIDP,  # pSourceSid : PSID
  ],
  Fiddle::TYPE_INT)
#[link(name = "advapi32")]
extern "system" {
    fn CopySid(
        nDestinationSidLength: u32,  // DWORD
        pDestinationSid: *mut core::ffi::c_void,  // PSID out
        pSourceSid: *mut core::ffi::c_void  // PSID
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("ADVAPI32.dll", SetLastError = true)]
public static extern bool CopySid(uint nDestinationSidLength, IntPtr pDestinationSid, IntPtr pSourceSid);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ADVAPI32_CopySid' -Namespace Win32 -PassThru
# $api::CopySid(nDestinationSidLength, pDestinationSid, pSourceSid)
#uselib "ADVAPI32.dll"
#func global CopySid "CopySid" sptr, sptr, sptr
; CopySid nDestinationSidLength, pDestinationSid, pSourceSid   ; 戻り値は stat
; nDestinationSidLength : DWORD -> "sptr"
; pDestinationSid : PSID out -> "sptr"
; pSourceSid : PSID -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "ADVAPI32.dll"
#cfunc global CopySid "CopySid" int, sptr, sptr
; res = CopySid(nDestinationSidLength, pDestinationSid, pSourceSid)
; nDestinationSidLength : DWORD -> "int"
; pDestinationSid : PSID out -> "sptr"
; pSourceSid : PSID -> "sptr"
; BOOL CopySid(DWORD nDestinationSidLength, PSID pDestinationSid, PSID pSourceSid)
#uselib "ADVAPI32.dll"
#cfunc global CopySid "CopySid" int, intptr, intptr
; res = CopySid(nDestinationSidLength, pDestinationSid, pSourceSid)
; nDestinationSidLength : DWORD -> "int"
; pDestinationSid : PSID out -> "intptr"
; pSourceSid : PSID -> "intptr"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	advapi32 = windows.NewLazySystemDLL("ADVAPI32.dll")
	procCopySid = advapi32.NewProc("CopySid")
)

// nDestinationSidLength (DWORD), pDestinationSid (PSID out), pSourceSid (PSID)
r1, _, err := procCopySid.Call(
	uintptr(nDestinationSidLength),
	uintptr(pDestinationSid),
	uintptr(pSourceSid),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function CopySid(
  nDestinationSidLength: DWORD;   // DWORD
  pDestinationSid: THandle;   // PSID out
  pSourceSid: THandle   // PSID
): BOOL; stdcall;
  external 'ADVAPI32.dll' name 'CopySid';
result := DllCall("ADVAPI32\CopySid"
    , "UInt", nDestinationSidLength   ; DWORD
    , "Ptr", pDestinationSid   ; PSID out
    , "Ptr", pSourceSid   ; PSID
    , "Int")   ; return: BOOL
●CopySid(nDestinationSidLength, pDestinationSid, pSourceSid) = DLL("ADVAPI32.dll", "bool CopySid(dword, void*, void*)")
# 呼び出し: CopySid(nDestinationSidLength, pDestinationSid, pSourceSid)
# nDestinationSidLength : DWORD -> "dword"
# pDestinationSid : PSID out -> "void*"
# pSourceSid : PSID -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "advapi32" fn CopySid(
    nDestinationSidLength: u32, // DWORD
    pDestinationSid: ?*anyopaque, // PSID out
    pSourceSid: ?*anyopaque // PSID
) callconv(std.os.windows.WINAPI) i32;
proc CopySid(
    nDestinationSidLength: uint32,  # DWORD
    pDestinationSid: pointer,  # PSID out
    pSourceSid: pointer  # PSID
): int32 {.importc: "CopySid", stdcall, dynlib: "ADVAPI32.dll".}
pragma(lib, "advapi32");
extern(Windows)
int CopySid(
    uint nDestinationSidLength,   // DWORD
    void* pDestinationSid,   // PSID out
    void* pSourceSid   // PSID
);
ccall((:CopySid, "ADVAPI32.dll"), stdcall, Int32,
      (UInt32, Ptr{Cvoid}, Ptr{Cvoid}),
      nDestinationSidLength, pDestinationSid, pSourceSid)
# nDestinationSidLength : DWORD -> UInt32
# pDestinationSid : PSID out -> Ptr{Cvoid}
# pSourceSid : PSID -> Ptr{Cvoid}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t CopySid(
    uint32_t nDestinationSidLength,
    void* pDestinationSid,
    void* pSourceSid);
]]
local advapi32 = ffi.load("advapi32")
-- advapi32.CopySid(nDestinationSidLength, pDestinationSid, pSourceSid)
-- nDestinationSidLength : DWORD
-- pDestinationSid : PSID out
-- pSourceSid : PSID
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('ADVAPI32.dll');
const CopySid = lib.func('__stdcall', 'CopySid', 'int32_t', ['uint32_t', 'void *', 'void *']);
// CopySid(nDestinationSidLength, pDestinationSid, pSourceSid)
// nDestinationSidLength : DWORD -> 'uint32_t'
// pDestinationSid : PSID out -> 'void *'
// pSourceSid : PSID -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("ADVAPI32.dll", {
  CopySid: { parameters: ["u32", "pointer", "pointer"], result: "i32" },
});
// lib.symbols.CopySid(nDestinationSidLength, pDestinationSid, pSourceSid)
// nDestinationSidLength : DWORD -> "u32"
// pDestinationSid : PSID out -> "pointer"
// pSourceSid : PSID -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t CopySid(
    uint32_t nDestinationSidLength,
    void* pDestinationSid,
    void* pSourceSid);
C, "ADVAPI32.dll");
// $ffi->CopySid(nDestinationSidLength, pDestinationSid, pSourceSid);
// nDestinationSidLength : DWORD
// pDestinationSid : PSID out
// pSourceSid : PSID
// 構造体/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 Advapi32 extends StdCallLibrary {
    Advapi32 INSTANCE = Native.load("advapi32", Advapi32.class);
    boolean CopySid(
        int nDestinationSidLength,   // DWORD
        Pointer pDestinationSid,   // PSID out
        Pointer pSourceSid   // PSID
    );
}
@[Link("advapi32")]
lib LibADVAPI32
  fun CopySid = CopySid(
    nDestinationSidLength : UInt32,   # DWORD
    pDestinationSid : Void*,   # PSID out
    pSourceSid : Void*   # PSID
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef CopySidNative = Int32 Function(Uint32, Pointer<Void>, Pointer<Void>);
typedef CopySidDart = int Function(int, Pointer<Void>, Pointer<Void>);
final CopySid = DynamicLibrary.open('ADVAPI32.dll')
    .lookupFunction<CopySidNative, CopySidDart>('CopySid');
// nDestinationSidLength : DWORD -> Uint32
// pDestinationSid : PSID out -> Pointer<Void>
// pSourceSid : PSID -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function CopySid(
  nDestinationSidLength: DWORD;   // DWORD
  pDestinationSid: THandle;   // PSID out
  pSourceSid: THandle   // PSID
): BOOL; stdcall;
  external 'ADVAPI32.dll' name 'CopySid';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "CopySid"
  c_CopySid :: Word32 -> Ptr () -> Ptr () -> IO CInt
-- nDestinationSidLength : DWORD -> Word32
-- pDestinationSid : PSID out -> Ptr ()
-- pSourceSid : PSID -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let copysid =
  foreign "CopySid"
    (uint32_t @-> (ptr void) @-> (ptr void) @-> returning int32_t)
(* nDestinationSidLength : DWORD -> uint32_t *)
(* pDestinationSid : PSID out -> (ptr void) *)
(* pSourceSid : PSID -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library advapi32 (t "ADVAPI32.dll"))
(cffi:use-foreign-library advapi32)

(cffi:defcfun ("CopySid" copy-sid :convention :stdcall) :int32
  (n-destination-sid-length :uint32)   ; DWORD
  (p-destination-sid :pointer)   ; PSID out
  (p-source-sid :pointer))   ; PSID
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $CopySid = Win32::API::More->new('ADVAPI32',
    'BOOL CopySid(DWORD nDestinationSidLength, HANDLE pDestinationSid, HANDLE pSourceSid)');
# my $ret = $CopySid->Call($nDestinationSidLength, $pDestinationSid, $pSourceSid);
# nDestinationSidLength : DWORD -> DWORD
# pDestinationSid : PSID out -> HANDLE
# pSourceSid : PSID -> HANDLE
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

公式の関連項目