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

CredFindBestCredentialW

関数
対象名に最も適した資格情報を検索して取得する。
DLLADVAPI32.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり対応OSWindows Vista 以降

シグネチャ

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

BOOL CredFindBestCredentialW(
    LPCWSTR TargetName,
    DWORD Type,
    DWORD Flags,
    CREDENTIALW** Credential
);

パラメーター

名前方向説明
TargetNameLPCWSTRin資格情報を検索する対象のターゲット リソース名を格納した、null 終端文字列へのポインター。
TypeDWORDin検索する資格情報の種類。現在、この関数は CRED_TYPE_GENERIC のみをサポートします。
FlagsDWORDin予約済みです。
CredentialCREDENTIALW**out

この関数が取得した資格情報の組を指定する CREDENTIAL 構造体へのポインターのアドレス。

この構造体の使用を終えたら、CredFree 関数を呼び出して解放してください。

戻り値の型: BOOL

公式ドキュメント

資格情報管理 (CredMan) データベースを検索し、現在のログオン セッションに関連付けられた汎用資格情報のうち、指定したターゲット リソースに最も一致するものの組を取得します。(Unicode)

戻り値

関数が成功すると、TRUE を返します。

関数が失敗すると、FALSE を返します。拡張エラー情報を取得するには、 GetLastError を呼び出してください。

解説(Remarks)

メモ

wincred.h ヘッダーは、CredFindBestCredential を、UNICODE プリプロセッサ定数の定義に基づいてこの関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして定義します。エンコーディング非依存のエイリアスを、エンコーディング非依存でないコードと混在させて使用すると、不整合が生じ、コンパイル エラーや実行時エラーの原因となることがあります。詳細については、関数プロトタイプの規則を参照してください。

出典・ライセンス: 上記「公式ドキュメント」の内容は 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 CredFindBestCredentialW(
    LPCWSTR TargetName,
    DWORD Type,
    DWORD Flags,
    CREDENTIALW** Credential
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("ADVAPI32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern bool CredFindBestCredentialW(
    [MarshalAs(UnmanagedType.LPWStr)] string TargetName,   // LPCWSTR
    uint Type,   // DWORD
    uint Flags,   // DWORD
    IntPtr Credential   // CREDENTIALW** out
);
<DllImport("ADVAPI32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function CredFindBestCredentialW(
    <MarshalAs(UnmanagedType.LPWStr)> TargetName As String,   ' LPCWSTR
    Type As UInteger,   ' DWORD
    Flags As UInteger,   ' DWORD
    Credential As IntPtr   ' CREDENTIALW** out
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' TargetName : LPCWSTR
' Type : DWORD
' Flags : DWORD
' Credential : CREDENTIALW** out
Declare PtrSafe Function CredFindBestCredentialW Lib "advapi32" ( _
    ByVal TargetName As LongPtr, _
    ByVal Type As Long, _
    ByVal Flags 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

CredFindBestCredentialW = ctypes.windll.advapi32.CredFindBestCredentialW
CredFindBestCredentialW.restype = wintypes.BOOL
CredFindBestCredentialW.argtypes = [
    wintypes.LPCWSTR,  # TargetName : LPCWSTR
    wintypes.DWORD,  # Type : DWORD
    wintypes.DWORD,  # Flags : DWORD
    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')
CredFindBestCredentialW = Fiddle::Function.new(
  lib['CredFindBestCredentialW'],
  [
    Fiddle::TYPE_VOIDP,  # TargetName : LPCWSTR
    -Fiddle::TYPE_INT,  # Type : DWORD
    -Fiddle::TYPE_INT,  # Flags : DWORD
    Fiddle::TYPE_VOIDP,  # Credential : CREDENTIALW** out
  ],
  Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "advapi32")]
extern "system" {
    fn CredFindBestCredentialW(
        TargetName: *const u16,  // LPCWSTR
        Type: u32,  // DWORD
        Flags: u32,  // DWORD
        Credential: *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 CredFindBestCredentialW([MarshalAs(UnmanagedType.LPWStr)] string TargetName, uint Type, uint Flags, IntPtr Credential);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ADVAPI32_CredFindBestCredentialW' -Namespace Win32 -PassThru
# $api::CredFindBestCredentialW(TargetName, Type, Flags, Credential)
#uselib "ADVAPI32.dll"
#func global CredFindBestCredentialW "CredFindBestCredentialW" wptr, wptr, wptr, wptr
; CredFindBestCredentialW TargetName, Type, Flags, varptr(Credential)   ; 戻り値は stat
; TargetName : LPCWSTR -> "wptr"
; Type : DWORD -> "wptr"
; Flags : DWORD -> "wptr"
; Credential : CREDENTIALW** out -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "ADVAPI32.dll"
#cfunc global CredFindBestCredentialW "CredFindBestCredentialW" wstr, int, int, var
; res = CredFindBestCredentialW(TargetName, Type, Flags, Credential)
; TargetName : LPCWSTR -> "wstr"
; Type : DWORD -> "int"
; Flags : DWORD -> "int"
; Credential : CREDENTIALW** out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL CredFindBestCredentialW(LPCWSTR TargetName, DWORD Type, DWORD Flags, CREDENTIALW** Credential)
#uselib "ADVAPI32.dll"
#cfunc global CredFindBestCredentialW "CredFindBestCredentialW" wstr, int, int, var
; res = CredFindBestCredentialW(TargetName, Type, Flags, Credential)
; TargetName : LPCWSTR -> "wstr"
; Type : DWORD -> "int"
; Flags : DWORD -> "int"
; Credential : CREDENTIALW** out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	advapi32 = windows.NewLazySystemDLL("ADVAPI32.dll")
	procCredFindBestCredentialW = advapi32.NewProc("CredFindBestCredentialW")
)

// TargetName (LPCWSTR), Type (DWORD), Flags (DWORD), Credential (CREDENTIALW** out)
r1, _, err := procCredFindBestCredentialW.Call(
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(TargetName))),
	uintptr(Type),
	uintptr(Flags),
	uintptr(Credential),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function CredFindBestCredentialW(
  TargetName: PWideChar;   // LPCWSTR
  Type: DWORD;   // DWORD
  Flags: DWORD;   // DWORD
  Credential: Pointer   // CREDENTIALW** out
): BOOL; stdcall;
  external 'ADVAPI32.dll' name 'CredFindBestCredentialW';
result := DllCall("ADVAPI32\CredFindBestCredentialW"
    , "WStr", TargetName   ; LPCWSTR
    , "UInt", Type   ; DWORD
    , "UInt", Flags   ; DWORD
    , "Ptr", Credential   ; CREDENTIALW** out
    , "Int")   ; return: BOOL
●CredFindBestCredentialW(TargetName, Type, Flags, Credential) = DLL("ADVAPI32.dll", "bool CredFindBestCredentialW(char*, dword, dword, void*)")
# 呼び出し: CredFindBestCredentialW(TargetName, Type, Flags, Credential)
# TargetName : LPCWSTR -> "char*"
# Type : DWORD -> "dword"
# Flags : DWORD -> "dword"
# 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 CredFindBestCredentialW(
    TargetName: [*c]const u16, // LPCWSTR
    Type: u32, // DWORD
    Flags: u32, // DWORD
    Credential: [*c][*c]CREDENTIALW // CREDENTIALW** out
) callconv(std.os.windows.WINAPI) i32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。
proc CredFindBestCredentialW(
    TargetName: WideCString,  # LPCWSTR
    Type: uint32,  # DWORD
    Flags: uint32,  # DWORD
    Credential: ptr CREDENTIALW  # CREDENTIALW** out
): int32 {.importc: "CredFindBestCredentialW", stdcall, dynlib: "ADVAPI32.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "advapi32");
extern(Windows)
int CredFindBestCredentialW(
    const(wchar)* TargetName,   // LPCWSTR
    uint Type,   // DWORD
    uint Flags,   // DWORD
    CREDENTIALW** Credential   // CREDENTIALW** out
);
ccall((:CredFindBestCredentialW, "ADVAPI32.dll"), stdcall, Int32,
      (Cwstring, UInt32, UInt32, Ptr{CREDENTIALW}),
      TargetName, Type, Flags, Credential)
# TargetName : LPCWSTR -> Cwstring
# Type : DWORD -> UInt32
# Flags : DWORD -> UInt32
# Credential : CREDENTIALW** out -> Ptr{CREDENTIALW}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。
local ffi = require("ffi")
ffi.cdef[[
int32_t CredFindBestCredentialW(
    const uint16_t* TargetName,
    uint32_t Type,
    uint32_t Flags,
    void* Credential);
]]
local advapi32 = ffi.load("advapi32")
-- advapi32.CredFindBestCredentialW(TargetName, Type, Flags, Credential)
-- TargetName : LPCWSTR
-- Type : DWORD
-- Flags : DWORD
-- 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 CredFindBestCredentialW = lib.func('__stdcall', 'CredFindBestCredentialW', 'int32_t', ['str16', 'uint32_t', 'uint32_t', 'void *']);
// CredFindBestCredentialW(TargetName, Type, Flags, Credential)
// TargetName : LPCWSTR -> 'str16'
// Type : DWORD -> 'uint32_t'
// Flags : DWORD -> 'uint32_t'
// Credential : CREDENTIALW** out -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("ADVAPI32.dll", {
  CredFindBestCredentialW: { parameters: ["buffer", "u32", "u32", "pointer"], result: "i32" },
});
// lib.symbols.CredFindBestCredentialW(TargetName, Type, Flags, Credential)
// TargetName : LPCWSTR -> "buffer"
// Type : DWORD -> "u32"
// Flags : DWORD -> "u32"
// Credential : CREDENTIALW** out -> "pointer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t CredFindBestCredentialW(
    const uint16_t* TargetName,
    uint32_t Type,
    uint32_t Flags,
    void* Credential);
C, "ADVAPI32.dll");
// $ffi->CredFindBestCredentialW(TargetName, Type, Flags, Credential);
// TargetName : LPCWSTR
// Type : DWORD
// Flags : DWORD
// 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 CredFindBestCredentialW(
        WString TargetName,   // LPCWSTR
        int Type,   // DWORD
        int Flags,   // DWORD
        Pointer Credential   // CREDENTIALW** out
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("advapi32")]
lib LibADVAPI32
  fun CredFindBestCredentialW = CredFindBestCredentialW(
    TargetName : UInt16*,   # LPCWSTR
    Type : UInt32,   # DWORD
    Flags : UInt32,   # DWORD
    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 CredFindBestCredentialWNative = Int32 Function(Pointer<Utf16>, Uint32, Uint32, Pointer<Void>);
typedef CredFindBestCredentialWDart = int Function(Pointer<Utf16>, int, int, Pointer<Void>);
final CredFindBestCredentialW = DynamicLibrary.open('ADVAPI32.dll')
    .lookupFunction<CredFindBestCredentialWNative, CredFindBestCredentialWDart>('CredFindBestCredentialW');
// TargetName : LPCWSTR -> Pointer<Utf16>
// Type : DWORD -> Uint32
// Flags : DWORD -> Uint32
// Credential : CREDENTIALW** out -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function CredFindBestCredentialW(
  TargetName: PWideChar;   // LPCWSTR
  Type: DWORD;   // DWORD
  Flags: DWORD;   // DWORD
  Credential: Pointer   // CREDENTIALW** out
): BOOL; stdcall;
  external 'ADVAPI32.dll' name 'CredFindBestCredentialW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

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

let credfindbestcredentialw =
  foreign "CredFindBestCredentialW"
    ((ptr uint16_t) @-> uint32_t @-> uint32_t @-> (ptr void) @-> returning int32_t)
(* TargetName : LPCWSTR -> (ptr uint16_t) *)
(* Type : DWORD -> uint32_t *)
(* Flags : DWORD -> 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 ("CredFindBestCredentialW" cred-find-best-credential-w :convention :stdcall) :int32
  (target-name (:string :encoding :utf-16le))   ; LPCWSTR
  (type :uint32)   ; DWORD
  (flags :uint32)   ; DWORD
  (credential :pointer))   ; CREDENTIALW** out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $CredFindBestCredentialW = Win32::API::More->new('ADVAPI32',
    'BOOL CredFindBestCredentialW(LPCWSTR TargetName, DWORD Type, DWORD Flags, LPVOID Credential)');
# my $ret = $CredFindBestCredentialW->Call($TargetName, $Type, $Flags, $Credential);
# TargetName : LPCWSTR -> LPCWSTR
# Type : DWORD -> DWORD
# Flags : DWORD -> DWORD
# Credential : CREDENTIALW** out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

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