Win32 API 日本語リファレンス
ホーム › Devices.BiometricFramework › WinBioSetCredential

WinBioSetCredential

関数
指定種別の生体認証資格情報を設定する。
DLLwinbio.dll呼出規約winapi対応OSWindows 8.1 以降

シグネチャ

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

HRESULT WinBioSetCredential(
    WINBIO_CREDENTIAL_TYPE Type,
    BYTE* Credential,
    UINT_PTR CredentialSize,
    WINBIO_CREDENTIAL_FORMAT Format
);

パラメーター

名前型方向説明
TypeWINBIO_CREDENTIAL_TYPEin資格情報の種類を指定する WINBIO_CREDENTIAL_TYPE 値。現在は WINBIO_CREDENTIAL_PASSWORD を指定できます。
CredentialBYTE*in資格情報を格納する可変長バイト配列へのポインター。形式は Type パラメーターと Format パラメーターによって決まります。
CredentialSizeUINT_PTRinCredential パラメーターで指定した値のサイズ (バイト単位)。
FormatWINBIO_CREDENTIAL_FORMATin

資格情報の形式を指定する WINBIO_CREDENTIAL_FORMAT 列挙値。Type パラメーターが WINBIO_CREDENTIAL_PASSWORD の場合、次のいずれかを指定できます。

値 意味
WINBIO_PASSWORD_GENERIC
資格情報は、プレーンテキストの NULL で終わる Unicode 文字列です。
WINBIO_PASSWORD_PACKED
資格情報は CredProtect 関数でラップされ、CredPackAuthenticationBuffer 関数でパックされています。この形式が推奨されます。
WINBIO_PASSWORD_PROTECTED
パスワード資格情報は CredProtect でラップされています。

戻り値の型: HRESULT

公式ドキュメント

現在のユーザーの生体認証ログオン資格情報を保存します。Windows 10 ビルド 1607 以降、この関数はモバイルイメージでも使用できます。

戻り値

関数が成功した場合は S_OK を返します。関数が失敗した場合は、エラーを示す HRESULT 値を返します。戻り値には次の表に示すものが含まれますが、これらに限定されません。一般的なエラーコードの一覧については、Common HRESULT Values を参照してください。

戻り値 説明
E_ACCESSDENIED
呼び出し元に資格情報を設定する権限がありません。
WINBIO_E_UNKNOWN_ID
ユーザーが生体認証サンプルを登録していません。
SEC_E_LOGON_DENIED
資格情報が現在のユーザーに対して有効ではありませんでした。
WINBIO_E_CRED_PROV_DISABLED
現在の管理ポリシーにより、資格情報プロバイダーの使用が禁止されています。

解説(Remarks)

現在のユーザーが指定した種類のログオン資格情報を既に持っている場合、WinBioSetCredential はそれを上書きします。 この関数はユーザーの資格情報と対話型ログオン特権の両方を検証し、検証に失敗した場合は失敗します。ログオン試行に関するイベントがイベントログに記録されます。ドメインアカウントの資格情報は、グループポリシーで許可されている場合にのみ保存できます。

Format パラメーターに WINBIO_PASSWORD_PACKED を渡した場合は、SecureZeroMemory を呼び出して資格情報を安全にゼロクリアしてください。

資格情報を設定できるのは、中 (medium) 以上の整合性レベルのプロセスだけです。それより低い整合性レベルのプロセスが資格情報を設定しようとすると、この関数は E_ACCESSDENIED を返します。

例

次の関数は、現在のユーザーの ID と資格情報を取得し、WinBioSetCredential を呼び出して資格情報を設定します。GetCredentials と GetCurrentUserIdentity という 2 つのヘルパー関数も含まれています。Winbio.lib スタティックライブラリにリンクし、次のヘッダーファイルをインクルードしてください。

HRESULT SetCredential()
{
    // 変数を宣言します。
    HRESULT hr = S_OK;
    PVOID   pvAuthBlob = NULL;
    ULONG   cbAuthBlob = 0;
    WINBIO_IDENTITY identity;
    PSID pSid = NULL;

    // ユーザーの ID を検索します。
    wprintf_s(L"\n Finding user identity.\n");
    hr = GetCurrentUserIdentity( &identity );
    if (FAILED(hr))
    {
        wprintf_s(L"\n User identity not found. hr = 0x%x\n", hr);
        return hr;
    }

    // ユーザーのセキュリティ記述子へのポインターを設定します。
    pSid = identity.Value.AccountSid.Data;

    // 資格情報を格納するバイト配列を取得します。
    hr = GetCredentials(pSid, &pvAuthBlob, &cbAuthBlob);
    if (FAILED(hr))
    {
        wprintf_s(L"\n GetCredentials failed. hr = 0x%x\n", hr);
        goto e_Exit;
    }

    // 資格情報を設定します。
    hr = WinBioSetCredential(
            WINBIO_CREDENTIAL_PASSWORD,     // 資格情報の種類
            (PUCHAR)pvAuthBlob,             // 資格情報のバイト配列
            cbAuthBlob,                     // 資格情報のサイズ
            WINBIO_PASSWORD_PACKED);        // 資格情報の形式

    if (FAILED(hr))
    {
        wprintf_s(L"\n WinBioSetCredential failed. hr = 0x%x\n", hr);
        goto e_Exit;
    }
    wprintf_s(L"\n Credentials successfully set.\n");

e_Exit:
    // 認証バイト配列を削除します。
    if (NULL != pvAuthBlob)
    {
        SecureZeroMemory(pvAuthBlob, cbAuthBlob);
        CoTaskMemFree(pvAuthBlob);
        pvAuthBlob = NULL;
    }

    wprintf_s(L"\n Press any key to exit...");
    _getch();

    return hr;


}

//------------------------------------------------------------------------
// 次の関数は、ユーザーに資格情報の入力を求めるダイアログボックスを
// 表示します。
//
HRESULT GetCredentials(PSID pSid, PVOID* ppvAuthBlob, ULONG* pcbAuthBlob)
{
    HRESULT hr = S_OK;
    DWORD   dwResult;
    WCHAR   szUsername[MAX_PATH] = {0};
    DWORD   cchUsername = ARRAYSIZE(szUsername);
    WCHAR   szPassword[MAX_PATH] = {0};
    WCHAR   szDomain[MAX_PATH] = {0};
    DWORD   cchDomain = ARRAYSIZE(szDomain);
    WCHAR   szDomainAndUser[MAX_PATH] = {0};
    DWORD   cchDomainAndUser = ARRAYSIZE(szDomainAndUser);
    PVOID   pvInAuthBlob = NULL;
    ULONG   cbInAuthBlob = 0;
    PVOID   pvAuthBlob = NULL;
    ULONG   cbAuthBlob = 0;
    CREDUI_INFOW ui;
    ULONG   ulAuthPackage = 0;
    BOOL    fSave = FALSE;

    static const WCHAR WINBIO_CREDPROV_TEST_PASSWORD_PROMPT_MESSAGE[] =
           L"Enter your current password to enable biometric logon.";

    static const WCHAR WINBIO_CREDPROV_TEST_PASSWORD_PROMPT_CAPTION[] =
           L"Biometric Log On Enrollment";

    if (NULL == pSid || NULL == ppvAuthBlob || NULL == pcbAuthBlob)
    {
        return E_INVALIDARG;
    }

    // ユーザー名とドメイン名を取得します。
    SID_NAME_USE    SidUse;
    DWORD           cchTmpUsername = cchUsername;
    DWORD           cchTmpDomain = cchDomain;

    if (!LookupAccountSidW(
              NULL,             // ローカルコンピューター
              pSid,             // ユーザーのセキュリティ識別子
              szUsername,       // ユーザー名
              &cchTmpUsername,  // ユーザー名のサイズ
              szDomain,         // ドメイン名
              &cchTmpDomain,    // ドメイン名のサイズ
              &SidUse))         // アカウントの種類
    {
        dwResult = GetLastError();
        hr = HRESULT_FROM_WIN32(dwResult);
        wprintf_s(L"\n LookupAccountSidLocalW failed: hr = 0x%x\n", hr);
        return hr;
    }

    // ドメイン名とユーザー名を結合します。
    swprintf_s(
        szDomainAndUser, 
        cchDomainAndUser, 
        L"%s\\%s", 
        szDomain, 
        szUsername);

    // 認証バッファーのサイズ (バイト単位) を確認するために、
    // CredPackAuthenticationBufferW を一度呼び出します。
    if (!CredPackAuthenticationBufferW(
              0,                // 予約済み
              szDomainAndUser,  // Domain\User 形式の名前
              szPassword,       // ユーザーのパスワード
              NULL,             // パックされた資格情報
              &cbInAuthBlob)    // 資格情報のサイズ (バイト単位)
        && GetLastError() != ERROR_INSUFFICIENT_BUFFER)
        {
            dwResult = GetLastError();
            hr = HRESULT_FROM_WIN32(dwResult);
            wprintf_s(L"\n CredPackAuthenticationBufferW (1) failed: ");
            wprintf_s(L"hr = 0x%x\n", hr);
        }

    // 入力バッファー用のメモリを割り当てます。
    pvInAuthBlob = CoTaskMemAlloc(cbInAuthBlob);
    if (!pvInAuthBlob)
    {
        cbInAuthBlob = 0;
         wprintf_s(L"\n CoTaskMemAlloc() Out of memory %d\n");
        return HRESULT_FROM_WIN32(ERROR_OUTOFMEMORY);
    }

    // 認証バッファーを取得するために、CredPackAuthenticationBufferW を
    // 再度呼び出します。
    if (!CredPackAuthenticationBufferW(
              0,
              szDomainAndUser,
              szPassword,
              (PBYTE)pvInAuthBlob,
              &cbInAuthBlob))
    {
        dwResult = GetLastError();
        hr = HRESULT_FROM_WIN32(dwResult);
        wprintf_s(L"\n CredPackAuthenticationBufferW (2) failed: ");
        wprintf_s(L"hr = 0x%x\n", hr);
        goto e_Exit;
    }

    // 資格情報の入力を求めるダイアログボックスを表示します。
    ui.cbSize = sizeof(ui);
    ui.hwndParent = GetConsoleWindow();
    ui.pszMessageText = WINBIO_CREDPROV_TEST_PASSWORD_PROMPT_MESSAGE;
    ui.pszCaptionText = WINBIO_CREDPROV_TEST_PASSWORD_PROMPT_CAPTION;
    ui.hbmBanner = NULL;

    dwResult = CredUIPromptForWindowsCredentialsW(
                   &ui,             // カスタマイズ情報
                   0,               // 表示するエラーコード
                   &ulAuthPackage,  // 承認パッケージ
                   pvInAuthBlob,    // 資格情報のバイト配列
                   cbInAuthBlob,    // 資格情報の入力バッファーのサイズ
                   &pvAuthBlob,     // 出力される資格情報のバイト配列
                   &cbAuthBlob,     // 資格情報のバイト配列のサイズ
                   &fSave,          // 保存チェックボックスを選択します。
                   CREDUIWIN_IN_CRED_ONLY |
                   CREDUIWIN_ENUMERATE_CURRENT_USER
                   );
    if (dwResult != NO_ERROR)
    {
        hr = HRESULT_FROM_WIN32(dwResult);
        wprintf_s(L"\n CredUIPromptForWindowsCredentials failed: ");
        wprintf_s(L"0x%08x\n", dwResult);
        goto e_Exit;
    }

    *ppvAuthBlob = pvAuthBlob;
    *pcbAuthBlob = cbAuthBlob;

e_Exit:
    // 入力用の認証バイト配列を削除します。
    if (pvInAuthBlob)
    {
        SecureZeroMemory(pvInAuthBlob, cbInAuthBlob);
        CoTaskMemFree(pvInAuthBlob);
        pvInAuthBlob = NULL;
    };

    return hr;
}


//------------------------------------------------------------------------
// 次の関数は、現在のユーザーの ID を取得します。
// これはヘルパー関数であり、Windows Biometric Framework API の
// 一部ではありません。
//
HRESULT GetCurrentUserIdentity(__inout PWINBIO_IDENTITY Identity)
{
    // 変数を宣言します。
    HRESULT hr = S_OK;
    HANDLE tokenHandle = NULL;
    DWORD bytesReturned = 0;
    struct{
        TOKEN_USER tokenUser;
        BYTE buffer[SECURITY_MAX_SID_SIZE];
    } tokenInfoBuffer;

    // 入力された ID をゼロで初期化し、種類を指定します。
    ZeroMemory( Identity, sizeof(WINBIO_IDENTITY));
    Identity->Type = WINBIO_ID_TYPE_NULL;

    // 現在のプロセスに関連付けられたアクセストークンを
    // 開きます。
    if (!OpenProcessToken(
            GetCurrentProcess(),            // プロセスハンドル
            TOKEN_READ,                     // 読み取りアクセスのみ
            &tokenHandle))                  // アクセストークンのハンドル
    {
        DWORD win32Status = GetLastError();
        wprintf_s(L"Cannot open token handle: %d\n", win32Status);
        hr = HRESULT_FROM_WIN32(win32Status);
        goto e_Exit;
    }

    // tokenInfoBuffer 構造体をゼロで初期化します。
    ZeroMemory(&tokenInfoBuffer, sizeof(tokenInfoBuffer));

    // アクセストークンに関する情報を取得します。ここでは SID を
    // 取得します。
    if (!GetTokenInformation(
            tokenHandle,                    // アクセストークンのハンドル
            TokenUser,                      // トークンのユーザー
            &tokenInfoBuffer.tokenUser,     // 格納先のバッファー
            sizeof(tokenInfoBuffer),        // バッファーのサイズ
            &bytesReturned))                // 必要なサイズ
    {
        DWORD win32Status = GetLastError();
        wprintf_s(L"Cannot query token information: %d\n", win32Status);
        hr = HRESULT_FROM_WIN32(win32Status);
        goto e_Exit;
    }

    // tokenInfoBuffer 構造体から WINBIO_IDENTITY 構造体へ SID を
    // コピーします。 
    CopySid(
        SECURITY_MAX_SID_SIZE,
        Identity->Value.AccountSid.Data,
        tokenInfoBuffer.tokenUser.User.Sid
        );

    // SID のサイズを指定し、WINBIO_IDENTITY 構造体の type メンバーに
    // WINBIO_ID_TYPE_SID を割り当てます。
    Identity->Value.AccountSid.Size = GetLengthSid(tokenInfoBuffer.tokenUser.User.Sid);
    Identity->Type = WINBIO_ID_TYPE_SID;

e_Exit:

    if (tokenHandle != NULL)
    {
        CloseHandle(tokenHandle);
    }

    return hr;
}

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

各言語での呼び出し定義

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

HRESULT WinBioSetCredential(
    WINBIO_CREDENTIAL_TYPE Type,
    BYTE* Credential,
    UINT_PTR CredentialSize,
    WINBIO_CREDENTIAL_FORMAT Format
);
[DllImport("winbio.dll", ExactSpelling = true)]
static extern int WinBioSetCredential(
    int Type,   // WINBIO_CREDENTIAL_TYPE
    IntPtr Credential,   // BYTE*
    UIntPtr CredentialSize,   // UINT_PTR
    int Format   // WINBIO_CREDENTIAL_FORMAT
);
<DllImport("winbio.dll", ExactSpelling:=True)>
Public Shared Function WinBioSetCredential(
    Type As Integer,   ' WINBIO_CREDENTIAL_TYPE
    Credential As IntPtr,   ' BYTE*
    CredentialSize As UIntPtr,   ' UINT_PTR
    Format As Integer   ' WINBIO_CREDENTIAL_FORMAT
) As Integer
End Function
' Type : WINBIO_CREDENTIAL_TYPE
' Credential : BYTE*
' CredentialSize : UINT_PTR
' Format : WINBIO_CREDENTIAL_FORMAT
Declare PtrSafe Function WinBioSetCredential Lib "winbio" ( _
    ByVal Type As Long, _
    ByVal Credential As LongPtr, _
    ByVal CredentialSize As LongPtr, _
    ByVal Format As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

WinBioSetCredential = ctypes.windll.winbio.WinBioSetCredential
WinBioSetCredential.restype = ctypes.c_int
WinBioSetCredential.argtypes = [
    ctypes.c_int,  # Type : WINBIO_CREDENTIAL_TYPE
    ctypes.POINTER(ctypes.c_ubyte),  # Credential : BYTE*
    ctypes.c_size_t,  # CredentialSize : UINT_PTR
    ctypes.c_int,  # Format : WINBIO_CREDENTIAL_FORMAT
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('winbio.dll')
WinBioSetCredential = Fiddle::Function.new(
  lib['WinBioSetCredential'],
  [
    Fiddle::TYPE_INT,  # Type : WINBIO_CREDENTIAL_TYPE
    Fiddle::TYPE_VOIDP,  # Credential : BYTE*
    Fiddle::TYPE_UINTPTR_T,  # CredentialSize : UINT_PTR
    Fiddle::TYPE_INT,  # Format : WINBIO_CREDENTIAL_FORMAT
  ],
  Fiddle::TYPE_INT)
#[link(name = "winbio")]
extern "system" {
    fn WinBioSetCredential(
        Type: i32,  // WINBIO_CREDENTIAL_TYPE
        Credential: *mut u8,  // BYTE*
        CredentialSize: usize,  // UINT_PTR
        Format: i32  // WINBIO_CREDENTIAL_FORMAT
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("winbio.dll")]
public static extern int WinBioSetCredential(int Type, IntPtr Credential, UIntPtr CredentialSize, int Format);
"@
$api = Add-Type -MemberDefinition $sig -Name 'winbio_WinBioSetCredential' -Namespace Win32 -PassThru
# $api::WinBioSetCredential(Type, Credential, CredentialSize, Format)
#uselib "winbio.dll"
#func global WinBioSetCredential "WinBioSetCredential" sptr, sptr, sptr, sptr
; WinBioSetCredential Type, varptr(Credential), CredentialSize, Format   ; 戻り値は stat
; Type : WINBIO_CREDENTIAL_TYPE -> "sptr"
; Credential : BYTE* -> "sptr"
; CredentialSize : UINT_PTR -> "sptr"
; Format : WINBIO_CREDENTIAL_FORMAT -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "winbio.dll"
#cfunc global WinBioSetCredential "WinBioSetCredential" int, var, sptr, int
; res = WinBioSetCredential(Type, Credential, CredentialSize, Format)
; Type : WINBIO_CREDENTIAL_TYPE -> "int"
; Credential : BYTE* -> "var"
; CredentialSize : UINT_PTR -> "sptr"
; Format : WINBIO_CREDENTIAL_FORMAT -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; HRESULT WinBioSetCredential(WINBIO_CREDENTIAL_TYPE Type, BYTE* Credential, UINT_PTR CredentialSize, WINBIO_CREDENTIAL_FORMAT Format)
#uselib "winbio.dll"
#cfunc global WinBioSetCredential "WinBioSetCredential" int, var, intptr, int
; res = WinBioSetCredential(Type, Credential, CredentialSize, Format)
; Type : WINBIO_CREDENTIAL_TYPE -> "int"
; Credential : BYTE* -> "var"
; CredentialSize : UINT_PTR -> "intptr"
; Format : WINBIO_CREDENTIAL_FORMAT -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	winbio = windows.NewLazySystemDLL("winbio.dll")
	procWinBioSetCredential = winbio.NewProc("WinBioSetCredential")
)

// Type (WINBIO_CREDENTIAL_TYPE), Credential (BYTE*), CredentialSize (UINT_PTR), Format (WINBIO_CREDENTIAL_FORMAT)
r1, _, err := procWinBioSetCredential.Call(
	uintptr(Type),
	uintptr(Credential),
	uintptr(CredentialSize),
	uintptr(Format),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // HRESULT
function WinBioSetCredential(
  Type: Integer;   // WINBIO_CREDENTIAL_TYPE
  Credential: Pointer;   // BYTE*
  CredentialSize: NativeUInt;   // UINT_PTR
  Format: Integer   // WINBIO_CREDENTIAL_FORMAT
): Integer; stdcall;
  external 'winbio.dll' name 'WinBioSetCredential';
result := DllCall("winbio\WinBioSetCredential"
    , "Int", Type   ; WINBIO_CREDENTIAL_TYPE
    , "Ptr", Credential   ; BYTE*
    , "UPtr", CredentialSize   ; UINT_PTR
    , "Int", Format   ; WINBIO_CREDENTIAL_FORMAT
    , "Int")   ; return: HRESULT
●WinBioSetCredential(Type, Credential, CredentialSize, Format) = DLL("winbio.dll", "int WinBioSetCredential(int, void*, int, int)")
# 呼び出し: WinBioSetCredential(Type, Credential, CredentialSize, Format)
# Type : WINBIO_CREDENTIAL_TYPE -> "int"
# Credential : BYTE* -> "void*"
# CredentialSize : UINT_PTR -> "int"
# Format : WINBIO_CREDENTIAL_FORMAT -> "int"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "winbio" fn WinBioSetCredential(
    Type: i32, // WINBIO_CREDENTIAL_TYPE
    Credential: [*c]u8, // BYTE*
    CredentialSize: usize, // UINT_PTR
    Format: i32 // WINBIO_CREDENTIAL_FORMAT
) callconv(std.os.windows.WINAPI) i32;
proc WinBioSetCredential(
    Type: int32,  # WINBIO_CREDENTIAL_TYPE
    Credential: ptr uint8,  # BYTE*
    CredentialSize: uint,  # UINT_PTR
    Format: int32  # WINBIO_CREDENTIAL_FORMAT
): int32 {.importc: "WinBioSetCredential", stdcall, dynlib: "winbio.dll".}
pragma(lib, "winbio");
extern(Windows)
int WinBioSetCredential(
    int Type,   // WINBIO_CREDENTIAL_TYPE
    ubyte* Credential,   // BYTE*
    size_t CredentialSize,   // UINT_PTR
    int Format   // WINBIO_CREDENTIAL_FORMAT
);
ccall((:WinBioSetCredential, "winbio.dll"), stdcall, Int32,
      (Int32, Ptr{UInt8}, Csize_t, Int32),
      Type, Credential, CredentialSize, Format)
# Type : WINBIO_CREDENTIAL_TYPE -> Int32
# Credential : BYTE* -> Ptr{UInt8}
# CredentialSize : UINT_PTR -> Csize_t
# Format : WINBIO_CREDENTIAL_FORMAT -> Int32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t WinBioSetCredential(
    int32_t Type,
    uint8_t* Credential,
    uintptr_t CredentialSize,
    int32_t Format);
]]
local winbio = ffi.load("winbio")
-- winbio.WinBioSetCredential(Type, Credential, CredentialSize, Format)
-- Type : WINBIO_CREDENTIAL_TYPE
-- Credential : BYTE*
-- CredentialSize : UINT_PTR
-- Format : WINBIO_CREDENTIAL_FORMAT
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('winbio.dll');
const WinBioSetCredential = lib.func('__stdcall', 'WinBioSetCredential', 'int32_t', ['int32_t', 'uint8_t *', 'uintptr_t', 'int32_t']);
// WinBioSetCredential(Type, Credential, CredentialSize, Format)
// Type : WINBIO_CREDENTIAL_TYPE -> 'int32_t'
// Credential : BYTE* -> 'uint8_t *'
// CredentialSize : UINT_PTR -> 'uintptr_t'
// Format : WINBIO_CREDENTIAL_FORMAT -> 'int32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("winbio.dll", {
  WinBioSetCredential: { parameters: ["i32", "pointer", "usize", "i32"], result: "i32" },
});
// lib.symbols.WinBioSetCredential(Type, Credential, CredentialSize, Format)
// Type : WINBIO_CREDENTIAL_TYPE -> "i32"
// Credential : BYTE* -> "pointer"
// CredentialSize : UINT_PTR -> "usize"
// Format : WINBIO_CREDENTIAL_FORMAT -> "i32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t WinBioSetCredential(
    int32_t Type,
    uint8_t* Credential,
    size_t CredentialSize,
    int32_t Format);
C, "winbio.dll");
// $ffi->WinBioSetCredential(Type, Credential, CredentialSize, Format);
// Type : WINBIO_CREDENTIAL_TYPE
// Credential : BYTE*
// CredentialSize : UINT_PTR
// Format : WINBIO_CREDENTIAL_FORMAT
// 構造体/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 Winbio extends StdCallLibrary {
    Winbio INSTANCE = Native.load("winbio", Winbio.class);
    int WinBioSetCredential(
        int Type,   // WINBIO_CREDENTIAL_TYPE
        byte[] Credential,   // BYTE*
        long CredentialSize,   // UINT_PTR
        int Format   // WINBIO_CREDENTIAL_FORMAT
    );
}
@[Link("winbio")]
lib Libwinbio
  fun WinBioSetCredential = WinBioSetCredential(
    Type : Int32,   # WINBIO_CREDENTIAL_TYPE
    Credential : UInt8*,   # BYTE*
    CredentialSize : LibC::SizeT,   # UINT_PTR
    Format : Int32   # WINBIO_CREDENTIAL_FORMAT
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef WinBioSetCredentialNative = Int32 Function(Int32, Pointer<Uint8>, UintPtr, Int32);
typedef WinBioSetCredentialDart = int Function(int, Pointer<Uint8>, int, int);
final WinBioSetCredential = DynamicLibrary.open('winbio.dll')
    .lookupFunction<WinBioSetCredentialNative, WinBioSetCredentialDart>('WinBioSetCredential');
// Type : WINBIO_CREDENTIAL_TYPE -> Int32
// Credential : BYTE* -> Pointer<Uint8>
// CredentialSize : UINT_PTR -> UintPtr
// Format : WINBIO_CREDENTIAL_FORMAT -> Int32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function WinBioSetCredential(
  Type: Integer;   // WINBIO_CREDENTIAL_TYPE
  Credential: Pointer;   // BYTE*
  CredentialSize: NativeUInt;   // UINT_PTR
  Format: Integer   // WINBIO_CREDENTIAL_FORMAT
): Integer; stdcall;
  external 'winbio.dll' name 'WinBioSetCredential';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "WinBioSetCredential"
  c_WinBioSetCredential :: Int32 -> Ptr Word8 -> CUIntPtr -> Int32 -> IO Int32
-- Type : WINBIO_CREDENTIAL_TYPE -> Int32
-- Credential : BYTE* -> Ptr Word8
-- CredentialSize : UINT_PTR -> CUIntPtr
-- Format : WINBIO_CREDENTIAL_FORMAT -> Int32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let winbiosetcredential =
  foreign "WinBioSetCredential"
    (int32_t @-> (ptr uint8_t) @-> size_t @-> int32_t @-> returning int32_t)
(* Type : WINBIO_CREDENTIAL_TYPE -> int32_t *)
(* Credential : BYTE* -> (ptr uint8_t) *)
(* CredentialSize : UINT_PTR -> size_t *)
(* Format : WINBIO_CREDENTIAL_FORMAT -> int32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library winbio (t "winbio.dll"))
(cffi:use-foreign-library winbio)

(cffi:defcfun ("WinBioSetCredential" win-bio-set-credential :convention :stdcall) :int32
  (type :int32)   ; WINBIO_CREDENTIAL_TYPE
  (credential :pointer)   ; BYTE*
  (credential-size :uint64)   ; UINT_PTR
  (format :int32))   ; WINBIO_CREDENTIAL_FORMAT
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $WinBioSetCredential = Win32::API::More->new('winbio',
    'int WinBioSetCredential(int Type, LPVOID Credential, WPARAM CredentialSize, int Format)');
# my $ret = $WinBioSetCredential->Call($Type, $Credential, $CredentialSize, $Format);
# Type : WINBIO_CREDENTIAL_TYPE -> int
# Credential : BYTE* -> LPVOID
# CredentialSize : UINT_PTR -> WPARAM
# Format : WINBIO_CREDENTIAL_FORMAT -> int
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

公式の関連項目
使用する型