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

CredReadDomainCredentialsW

関数
対象情報に一致するドメイン資格情報を読み取る。
DLLADVAPI32.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

BOOL CredReadDomainCredentialsW(
    CREDENTIAL_TARGET_INFORMATIONW* TargetInfo,
    DWORD Flags,
    DWORD* Count,
    CREDENTIALW*** Credential
);

パラメーター

名前方向説明
TargetInfoCREDENTIAL_TARGET_INFORMATIONW*in対象サーバーを識別するターゲット情報。次の名前指定メンバーのうち、少なくとも 1 つは NULL 以外でなければなりません。NetbiosServerName、DnsServerName、NetbiosDomainName、DnsDomainName、または DnsTreeName。
FlagsDWORDin

関数の動作を制御するフラグ。

次のフラグが定義されています。

CRED_CACHE_TARGET_INFORMATION

CredGetTargetInfo による後続の読み取りに備えて TargetInfo をキャッシュします。

CountDWORD*outCredentials 配列で返される資格情報の数。
CredentialCREDENTIALW***out

資格情報へのポインターの配列を指すポインター。TargetInfo に一致する、最も具体的な既存の資格情報が返されます。複数の種類の資格情報(例: CRED_TYPE_DOMAIN_PASSWORDCRED_TYPE_DOMAIN_CERTIFICATE の資格情報)が存在する場合は、各種類につき 1 つずつ返されます。指定したターゲットへの接続が行われる場合、この最も具体的な資格情報が使用されます。

TargetInfo.CredTypes 配列で指定された種類の資格情報のみが返されます。返される Credentials 配列は、TargetInfo.CredTypes 配列と同じ順序でソートされます。すなわち、認証パッケージは、優先する資格情報の種類を TargetInfo.CredTypes 配列内のより前の位置に指定することで指定します。TargetInfo.CredTypeCount が 0 の場合、Credentials 配列は次のソート順で返されます。

返されるバッファーは、単一の割り当てられたブロックです。バッファー内に含まれるポインターは、この単一の割り当てられたブロック内の位置を指すポインターです。返された単一のバッファーは、CredFree を呼び出して解放する必要があります。

戻り値の型: BOOL

公式ドキュメント

ユーザーの資格情報セットからドメイン資格情報を読み取ります。(Unicode)

戻り値

この関数は、成功した場合は TRUE を、失敗した場合は FALSE を返します。より具体的なステータス コードを取得するには、GetLastError 関数を呼び出すことができます。次のステータス コードが返される可能性があります。

解説(Remarks)

この関数は、名前指定パラメーターに一致する、最も具体的な資格情報を返します。たとえば、対象サーバー名に一致する資格情報と対象ドメイン名に一致する資格情報の両方が存在する場合、サーバー固有の資格情報のみが返されます。これは、実際に使用される資格情報です。

次の一覧は、複数が一致する場合にどの資格情報が返されるかの順序(最も具体的なものから最も具体的でないものへ)を示しています。

CredReadDomainCredentials は、ドメイン(CRED_TYPE_DOMAIN_PASSWORD または CRED_TYPE_DOMAIN_CERTIFICATE)資格情報の特性を処理する点で CredRead と異なります。ドメイン資格情報には、複数のターゲット メンバーが含まれます。

Credentials パラメーターで指定された CREDENTIAL 構造体の Type メンバーの値が CRED_TYPE_DOMAIN_EXTENDED の場合、ターゲット名に名前空間を指定する必要があります。この関数は、指定された種類の資格情報を 1 つだけ返すことができます。

この関数はこの種類の資格情報を複数返すことができますが、CRED_TYPE_DOMAIN_EXTENDED は、TargetInfo パラメーターで指定された CREDENTIAL_TARGET_INFORMATION 構造体の CredTypes メンバー内で他の種類と混在させることはできません。

メモ

wincred.h ヘッダーは、CredReadDomainCredentials を、UNICODE プリプロセッサ定数の定義に基づいてこの関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして定義します。エンコード非依存のエイリアスの使用を、エンコード非依存ではないコードと混在させると、コンパイル エラーや実行時エラーを引き起こす不一致が発生する可能性があります。詳細については、「Conventions for Function Prototypes」を参照してください。

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

各言語での呼び出し定義

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

BOOL CredReadDomainCredentialsW(
    CREDENTIAL_TARGET_INFORMATIONW* TargetInfo,
    DWORD Flags,
    DWORD* Count,
    CREDENTIALW*** Credential
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("ADVAPI32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern bool CredReadDomainCredentialsW(
    IntPtr TargetInfo,   // CREDENTIAL_TARGET_INFORMATIONW*
    uint Flags,   // DWORD
    out uint Count,   // DWORD* out
    IntPtr Credential   // CREDENTIALW*** out
);
<DllImport("ADVAPI32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function CredReadDomainCredentialsW(
    TargetInfo As IntPtr,   ' CREDENTIAL_TARGET_INFORMATIONW*
    Flags As UInteger,   ' DWORD
    <Out> ByRef Count As UInteger,   ' DWORD* out
    Credential As IntPtr   ' CREDENTIALW*** out
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' TargetInfo : CREDENTIAL_TARGET_INFORMATIONW*
' Flags : DWORD
' Count : DWORD* out
' Credential : CREDENTIALW*** out
Declare PtrSafe Function CredReadDomainCredentialsW Lib "advapi32" ( _
    ByVal TargetInfo As LongPtr, _
    ByVal Flags As Long, _
    ByRef Count As Long, _
    ByVal Credential 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

CredReadDomainCredentialsW = ctypes.windll.advapi32.CredReadDomainCredentialsW
CredReadDomainCredentialsW.restype = wintypes.BOOL
CredReadDomainCredentialsW.argtypes = [
    ctypes.c_void_p,  # TargetInfo : CREDENTIAL_TARGET_INFORMATIONW*
    wintypes.DWORD,  # Flags : DWORD
    ctypes.POINTER(wintypes.DWORD),  # Count : DWORD* out
    ctypes.c_void_p,  # Credential : CREDENTIALW*** out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('ADVAPI32.dll')
CredReadDomainCredentialsW = Fiddle::Function.new(
  lib['CredReadDomainCredentialsW'],
  [
    Fiddle::TYPE_VOIDP,  # TargetInfo : CREDENTIAL_TARGET_INFORMATIONW*
    -Fiddle::TYPE_INT,  # Flags : DWORD
    Fiddle::TYPE_VOIDP,  # Count : DWORD* out
    Fiddle::TYPE_VOIDP,  # Credential : CREDENTIALW*** out
  ],
  Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "advapi32")]
extern "system" {
    fn CredReadDomainCredentialsW(
        TargetInfo: *mut CREDENTIAL_TARGET_INFORMATIONW,  // CREDENTIAL_TARGET_INFORMATIONW*
        Flags: u32,  // DWORD
        Count: *mut u32,  // DWORD* out
        Credential: *mut *mut *mut CREDENTIALW  // CREDENTIALW*** out
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("ADVAPI32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool CredReadDomainCredentialsW(IntPtr TargetInfo, uint Flags, out uint Count, IntPtr Credential);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ADVAPI32_CredReadDomainCredentialsW' -Namespace Win32 -PassThru
# $api::CredReadDomainCredentialsW(TargetInfo, Flags, Count, Credential)
#uselib "ADVAPI32.dll"
#func global CredReadDomainCredentialsW "CredReadDomainCredentialsW" wptr, wptr, wptr, wptr
; CredReadDomainCredentialsW varptr(TargetInfo), Flags, varptr(Count), varptr(Credential)   ; 戻り値は stat
; TargetInfo : CREDENTIAL_TARGET_INFORMATIONW* -> "wptr"
; Flags : DWORD -> "wptr"
; Count : DWORD* out -> "wptr"
; Credential : CREDENTIALW*** out -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "ADVAPI32.dll"
#cfunc global CredReadDomainCredentialsW "CredReadDomainCredentialsW" var, int, var, var
; res = CredReadDomainCredentialsW(TargetInfo, Flags, Count, Credential)
; TargetInfo : CREDENTIAL_TARGET_INFORMATIONW* -> "var"
; Flags : DWORD -> "int"
; Count : DWORD* out -> "var"
; Credential : CREDENTIALW*** out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL CredReadDomainCredentialsW(CREDENTIAL_TARGET_INFORMATIONW* TargetInfo, DWORD Flags, DWORD* Count, CREDENTIALW*** Credential)
#uselib "ADVAPI32.dll"
#cfunc global CredReadDomainCredentialsW "CredReadDomainCredentialsW" var, int, var, var
; res = CredReadDomainCredentialsW(TargetInfo, Flags, Count, Credential)
; TargetInfo : CREDENTIAL_TARGET_INFORMATIONW* -> "var"
; Flags : DWORD -> "int"
; Count : DWORD* out -> "var"
; Credential : CREDENTIALW*** out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	advapi32 = windows.NewLazySystemDLL("ADVAPI32.dll")
	procCredReadDomainCredentialsW = advapi32.NewProc("CredReadDomainCredentialsW")
)

// TargetInfo (CREDENTIAL_TARGET_INFORMATIONW*), Flags (DWORD), Count (DWORD* out), Credential (CREDENTIALW*** out)
r1, _, err := procCredReadDomainCredentialsW.Call(
	uintptr(TargetInfo),
	uintptr(Flags),
	uintptr(Count),
	uintptr(Credential),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function CredReadDomainCredentialsW(
  TargetInfo: Pointer;   // CREDENTIAL_TARGET_INFORMATIONW*
  Flags: DWORD;   // DWORD
  Count: Pointer;   // DWORD* out
  Credential: Pointer   // CREDENTIALW*** out
): BOOL; stdcall;
  external 'ADVAPI32.dll' name 'CredReadDomainCredentialsW';
result := DllCall("ADVAPI32\CredReadDomainCredentialsW"
    , "Ptr", TargetInfo   ; CREDENTIAL_TARGET_INFORMATIONW*
    , "UInt", Flags   ; DWORD
    , "Ptr", Count   ; DWORD* out
    , "Ptr", Credential   ; CREDENTIALW*** out
    , "Int")   ; return: BOOL
●CredReadDomainCredentialsW(TargetInfo, Flags, Count, Credential) = DLL("ADVAPI32.dll", "bool CredReadDomainCredentialsW(void*, dword, void*, void*)")
# 呼び出し: CredReadDomainCredentialsW(TargetInfo, Flags, Count, Credential)
# TargetInfo : CREDENTIAL_TARGET_INFORMATIONW* -> "void*"
# Flags : DWORD -> "dword"
# Count : DWORD* out -> "void*"
# Credential : CREDENTIALW*** out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。
const std = @import("std");

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

typedef CredReadDomainCredentialsWNative = Int32 Function(Pointer<Void>, Uint32, Pointer<Uint32>, Pointer<Void>);
typedef CredReadDomainCredentialsWDart = int Function(Pointer<Void>, int, Pointer<Uint32>, Pointer<Void>);
final CredReadDomainCredentialsW = DynamicLibrary.open('ADVAPI32.dll')
    .lookupFunction<CredReadDomainCredentialsWNative, CredReadDomainCredentialsWDart>('CredReadDomainCredentialsW');
// TargetInfo : CREDENTIAL_TARGET_INFORMATIONW* -> Pointer<Void>
// Flags : DWORD -> Uint32
// Count : DWORD* out -> Pointer<Uint32>
// Credential : CREDENTIALW*** out -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function CredReadDomainCredentialsW(
  TargetInfo: Pointer;   // CREDENTIAL_TARGET_INFORMATIONW*
  Flags: DWORD;   // DWORD
  Count: Pointer;   // DWORD* out
  Credential: Pointer   // CREDENTIALW*** out
): BOOL; stdcall;
  external 'ADVAPI32.dll' name 'CredReadDomainCredentialsW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "CredReadDomainCredentialsW"
  c_CredReadDomainCredentialsW :: Ptr () -> Word32 -> Ptr Word32 -> Ptr () -> IO CInt
-- TargetInfo : CREDENTIAL_TARGET_INFORMATIONW* -> Ptr ()
-- Flags : DWORD -> Word32
-- Count : DWORD* out -> Ptr Word32
-- Credential : CREDENTIALW*** out -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let credreaddomaincredentialsw =
  foreign "CredReadDomainCredentialsW"
    ((ptr void) @-> uint32_t @-> (ptr uint32_t) @-> (ptr void) @-> returning int32_t)
(* TargetInfo : CREDENTIAL_TARGET_INFORMATIONW* -> (ptr void) *)
(* Flags : DWORD -> uint32_t *)
(* Count : DWORD* out -> (ptr uint32_t) *)
(* Credential : CREDENTIALW*** out -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library advapi32 (t "ADVAPI32.dll"))
(cffi:use-foreign-library advapi32)

(cffi:defcfun ("CredReadDomainCredentialsW" cred-read-domain-credentials-w :convention :stdcall) :int32
  (target-info :pointer)   ; CREDENTIAL_TARGET_INFORMATIONW*
  (flags :uint32)   ; DWORD
  (count :pointer)   ; DWORD* out
  (credential :pointer))   ; CREDENTIALW*** out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $CredReadDomainCredentialsW = Win32::API::More->new('ADVAPI32',
    'BOOL CredReadDomainCredentialsW(LPVOID TargetInfo, DWORD Flags, LPVOID Count, LPVOID Credential)');
# my $ret = $CredReadDomainCredentialsW->Call($TargetInfo, $Flags, $Count, $Credential);
# TargetInfo : CREDENTIAL_TARGET_INFORMATIONW* -> LPVOID
# Flags : DWORD -> DWORD
# Count : DWORD* out -> LPVOID
# Credential : CREDENTIALW*** out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

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