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
TypeDWORDin
FlagsDWORDin
CredentialCREDENTIALW**out

戻り値の型: BOOL

各言語での呼び出し定義

// 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 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 版の利用を推奨。