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

CredUnPackAuthenticationBufferA

関数
認証バッファからユーザー名・ドメイン・パスワードを取り出す。
DLLcredui.dll文字セットANSI (-A)呼出規約winapiSetLastErrorあり対応OSWindows Vista 以降

シグネチャ

// credui.dll  (ANSI / -A)
#include <windows.h>

BOOL CredUnPackAuthenticationBufferA(
    CRED_PACK_FLAGS dwFlags,
    void* pAuthBuffer,
    DWORD cbAuthBuffer,
    LPSTR pszUserName,   // optional
    DWORD* pcchlMaxUserName,
    LPSTR pszDomainName,   // optional
    DWORD* pcchMaxDomainName,   // optional
    LPSTR pszPassword,   // optional
    DWORD* pcchMaxPassword
);

パラメーター

名前方向説明
dwFlagsCRED_PACK_FLAGSin

このパラメーターの値を CRED_PACK_PROTECTED_CREDENTIALS に設定すると、関数は認証バッファー内の資格情報の復号を試みます。資格情報を復号できない場合、関数は FALSE を返し、GetLastError 関数の呼び出しは ERROR_NOT_CAPABLE の値を返します。

復号の方法は、認証バッファーの形式によって異なります。

認証バッファーが SEC_WINNT_AUTH_IDENTITY_EX2 構造体である場合、SEC_WINNT_AUTH_IDENTITY_ENCRYPT_SAME_LOGON オプションを指定した SspiEncryptAuthIdentityEx によって暗号化されていれば、関数はバッファーを復号できます。

認証バッファーがマーシャリングされた KERB_*_LOGON 構造体のいずれかである場合、関数はパスワードを復号してから pszPassword バッファーに返します。

pAuthBuffervoid*in

変換対象の認証バッファーへのポインターです。

このバッファーは通常、CredUIPromptForWindowsCredentials 関数または CredPackAuthenticationBuffer 関数の出力です。次のいずれかの型である必要があります。

cbAuthBufferDWORDinpAuthBuffer バッファーのサイズ (バイト単位) です。
pszUserNameLPSTRoutoptional

ユーザー名を受け取る、null で終わる文字列へのポインターです。

この文字列はマーシャリングされた資格情報の場合があります。「解説」を参照してください。

pcchlMaxUserNameDWORD*inoutpszUserName バッファーのサイズ (文字単位) を指定する DWORD 値へのポインターです。出力時、バッファーのサイズが不足している場合は、必要な pszUserName バッファーのサイズ (文字単位) が設定されます。このサイズには終端の null 文字が含まれます。
pszDomainNameLPSTRoutoptionalユーザーのドメイン名を受け取る、null で終わる文字列へのポインターです。
pcchMaxDomainNameDWORD*inoutoptionalpszDomainName バッファーのサイズ (文字単位) を指定する DWORD 値へのポインターです。出力時、バッファーのサイズが不足している場合は、必要な pszDomainName バッファーのサイズ (文字単位) が設定されます。このサイズには終端の null 文字が含まれます。ドメイン名が存在しない場合、必要なサイズは 0 になることがあります。
pszPasswordLPSTRoutoptionalパスワードを受け取る、null で終わる文字列へのポインターです。
pcchMaxPasswordDWORD*inout

pszPassword バッファーのサイズ (文字単位) を指定する DWORD 値へのポインターです。出力時、バッファーのサイズが不足している場合は、必要な pszPassword バッファーのサイズ (文字単位) が設定されます。このサイズには終端の null 文字が含まれます。

この文字列はマーシャリングされた資格情報の場合があります。「解説」を参照してください。

戻り値の型: BOOL

公式ドキュメント

CredUIPromptForWindowsCredentials 関数の呼び出しによって返された認証バッファーを、文字列形式のユーザー名とパスワードに変換します。(ANSI)

戻り値

関数が成功した場合は TRUE、それ以外の場合は FALSE を返します。

拡張エラー情報を取得するには、 GetLastError 関数を呼び出します。次の表に、GetLastError 関数の一般的な値を示します。

リターンコード/値 説明
ERROR_NOT_CAPABLE
dwFlags パラメーターの値として CRED_PACK_PROTECTED_CREDENTIALS が渡されましたが、パスワードの保護に使用されたセキュリティコンテキストが呼び出し元のセキュリティコンテキストと異なるため、この関数は資格情報を復号できません。
ERROR_INSUFFICIENT_BUFFER
出力バッファー pszUserNamepszDomainNamepszPassword のいずれかのサイズが不足していました。
ERROR_NOT_SUPPORTED
認証バッファーがサポートされていない型です。

解説(Remarks)

Windows 8 および Windows Server 2012 以降では、認証バッファーを SEC_WINNT_AUTH_IDENTITY_EX2 構造体にすることができます。この構造体は、SEC_WINNT_AUTH_IDENTITY_ENCRYPT_SAME_LOGON オプションを指定した SspiEncryptAuthIdentityEx 関数によって、必要に応じて暗号化できます。この資格情報形式は、CredUIPromptForWindowsCredentials 関数または SspiPromptForCredentials 関数を使用して、ID プロバイダーの資格情報プロバイダーによって返されます。この構造体は、CredPackAuthenticationBuffer 関数を使用して構築することもできます。 認証バッファー pAuthBufferKERB_CERTIFICATE_LOGONSEC_WINNT_AUTH_IDENTITY_EX2 などのパスワード以外の資格情報を表す場合、関数は資格情報を文字列としてマーシャリングし、ユーザー名、ドメイン名、パスワードの各文字列として返す必要があります。マーシャリングは特定の手順に従って行われます。dwFlagsCRED_PACK_PROTECTED_CREDENTIALS フラグが含まれる場合、呼び出し元は資格情報が暗号化されたのと同じログオンセッションで実行されている必要があります。

メモ

wincred.h ヘッダーは、CredUnPackAuthenticationBuffer を、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)

各言語での呼び出し定義

// credui.dll  (ANSI / -A)
#include <windows.h>

BOOL CredUnPackAuthenticationBufferA(
    CRED_PACK_FLAGS dwFlags,
    void* pAuthBuffer,
    DWORD cbAuthBuffer,
    LPSTR pszUserName,   // optional
    DWORD* pcchlMaxUserName,
    LPSTR pszDomainName,   // optional
    DWORD* pcchMaxDomainName,   // optional
    LPSTR pszPassword,   // optional
    DWORD* pcchMaxPassword
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("credui.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
static extern bool CredUnPackAuthenticationBufferA(
    uint dwFlags,   // CRED_PACK_FLAGS
    IntPtr pAuthBuffer,   // void*
    uint cbAuthBuffer,   // DWORD
    [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder pszUserName,   // LPSTR optional, out
    ref uint pcchlMaxUserName,   // DWORD* in/out
    [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder pszDomainName,   // LPSTR optional, out
    IntPtr pcchMaxDomainName,   // DWORD* optional, in/out
    [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder pszPassword,   // LPSTR optional, out
    ref uint pcchMaxPassword   // DWORD* in/out
);
<DllImport("credui.dll", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function CredUnPackAuthenticationBufferA(
    dwFlags As UInteger,   ' CRED_PACK_FLAGS
    pAuthBuffer As IntPtr,   ' void*
    cbAuthBuffer As UInteger,   ' DWORD
    <MarshalAs(UnmanagedType.LPStr)> pszUserName As System.Text.StringBuilder,   ' LPSTR optional, out
    ByRef pcchlMaxUserName As UInteger,   ' DWORD* in/out
    <MarshalAs(UnmanagedType.LPStr)> pszDomainName As System.Text.StringBuilder,   ' LPSTR optional, out
    pcchMaxDomainName As IntPtr,   ' DWORD* optional, in/out
    <MarshalAs(UnmanagedType.LPStr)> pszPassword As System.Text.StringBuilder,   ' LPSTR optional, out
    ByRef pcchMaxPassword As UInteger   ' DWORD* in/out
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' dwFlags : CRED_PACK_FLAGS
' pAuthBuffer : void*
' cbAuthBuffer : DWORD
' pszUserName : LPSTR optional, out
' pcchlMaxUserName : DWORD* in/out
' pszDomainName : LPSTR optional, out
' pcchMaxDomainName : DWORD* optional, in/out
' pszPassword : LPSTR optional, out
' pcchMaxPassword : DWORD* in/out
Declare PtrSafe Function CredUnPackAuthenticationBufferA Lib "credui" ( _
    ByVal dwFlags As Long, _
    ByVal pAuthBuffer As LongPtr, _
    ByVal cbAuthBuffer As Long, _
    ByVal pszUserName As String, _
    ByRef pcchlMaxUserName As Long, _
    ByVal pszDomainName As String, _
    ByVal pcchMaxDomainName As LongPtr, _
    ByVal pszPassword As String, _
    ByRef pcchMaxPassword As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

CredUnPackAuthenticationBufferA = ctypes.windll.credui.CredUnPackAuthenticationBufferA
CredUnPackAuthenticationBufferA.restype = wintypes.BOOL
CredUnPackAuthenticationBufferA.argtypes = [
    wintypes.DWORD,  # dwFlags : CRED_PACK_FLAGS
    ctypes.POINTER(None),  # pAuthBuffer : void*
    wintypes.DWORD,  # cbAuthBuffer : DWORD
    wintypes.LPSTR,  # pszUserName : LPSTR optional, out
    ctypes.POINTER(wintypes.DWORD),  # pcchlMaxUserName : DWORD* in/out
    wintypes.LPSTR,  # pszDomainName : LPSTR optional, out
    ctypes.POINTER(wintypes.DWORD),  # pcchMaxDomainName : DWORD* optional, in/out
    wintypes.LPSTR,  # pszPassword : LPSTR optional, out
    ctypes.POINTER(wintypes.DWORD),  # pcchMaxPassword : DWORD* in/out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('credui.dll')
CredUnPackAuthenticationBufferA = Fiddle::Function.new(
  lib['CredUnPackAuthenticationBufferA'],
  [
    -Fiddle::TYPE_INT,  # dwFlags : CRED_PACK_FLAGS
    Fiddle::TYPE_VOIDP,  # pAuthBuffer : void*
    -Fiddle::TYPE_INT,  # cbAuthBuffer : DWORD
    Fiddle::TYPE_VOIDP,  # pszUserName : LPSTR optional, out
    Fiddle::TYPE_VOIDP,  # pcchlMaxUserName : DWORD* in/out
    Fiddle::TYPE_VOIDP,  # pszDomainName : LPSTR optional, out
    Fiddle::TYPE_VOIDP,  # pcchMaxDomainName : DWORD* optional, in/out
    Fiddle::TYPE_VOIDP,  # pszPassword : LPSTR optional, out
    Fiddle::TYPE_VOIDP,  # pcchMaxPassword : DWORD* in/out
  ],
  Fiddle::TYPE_INT)
#[link(name = "credui")]
extern "system" {
    fn CredUnPackAuthenticationBufferA(
        dwFlags: u32,  // CRED_PACK_FLAGS
        pAuthBuffer: *mut (),  // void*
        cbAuthBuffer: u32,  // DWORD
        pszUserName: *mut u8,  // LPSTR optional, out
        pcchlMaxUserName: *mut u32,  // DWORD* in/out
        pszDomainName: *mut u8,  // LPSTR optional, out
        pcchMaxDomainName: *mut u32,  // DWORD* optional, in/out
        pszPassword: *mut u8,  // LPSTR optional, out
        pcchMaxPassword: *mut u32  // DWORD* in/out
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("credui.dll", CharSet = CharSet.Ansi, SetLastError = true)]
public static extern bool CredUnPackAuthenticationBufferA(uint dwFlags, IntPtr pAuthBuffer, uint cbAuthBuffer, [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder pszUserName, ref uint pcchlMaxUserName, [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder pszDomainName, IntPtr pcchMaxDomainName, [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder pszPassword, ref uint pcchMaxPassword);
"@
$api = Add-Type -MemberDefinition $sig -Name 'credui_CredUnPackAuthenticationBufferA' -Namespace Win32 -PassThru
# $api::CredUnPackAuthenticationBufferA(dwFlags, pAuthBuffer, cbAuthBuffer, pszUserName, pcchlMaxUserName, pszDomainName, pcchMaxDomainName, pszPassword, pcchMaxPassword)
#uselib "credui.dll"
#func global CredUnPackAuthenticationBufferA "CredUnPackAuthenticationBufferA" sptr, sptr, sptr, sptr, sptr, sptr, sptr, sptr, sptr
; CredUnPackAuthenticationBufferA dwFlags, pAuthBuffer, cbAuthBuffer, varptr(pszUserName), varptr(pcchlMaxUserName), varptr(pszDomainName), varptr(pcchMaxDomainName), varptr(pszPassword), varptr(pcchMaxPassword)   ; 戻り値は stat
; dwFlags : CRED_PACK_FLAGS -> "sptr"
; pAuthBuffer : void* -> "sptr"
; cbAuthBuffer : DWORD -> "sptr"
; pszUserName : LPSTR optional, out -> "sptr"
; pcchlMaxUserName : DWORD* in/out -> "sptr"
; pszDomainName : LPSTR optional, out -> "sptr"
; pcchMaxDomainName : DWORD* optional, in/out -> "sptr"
; pszPassword : LPSTR optional, out -> "sptr"
; pcchMaxPassword : DWORD* in/out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "credui.dll"
#cfunc global CredUnPackAuthenticationBufferA "CredUnPackAuthenticationBufferA" int, sptr, int, var, var, var, var, var, var
; res = CredUnPackAuthenticationBufferA(dwFlags, pAuthBuffer, cbAuthBuffer, pszUserName, pcchlMaxUserName, pszDomainName, pcchMaxDomainName, pszPassword, pcchMaxPassword)
; dwFlags : CRED_PACK_FLAGS -> "int"
; pAuthBuffer : void* -> "sptr"
; cbAuthBuffer : DWORD -> "int"
; pszUserName : LPSTR optional, out -> "var"
; pcchlMaxUserName : DWORD* in/out -> "var"
; pszDomainName : LPSTR optional, out -> "var"
; pcchMaxDomainName : DWORD* optional, in/out -> "var"
; pszPassword : LPSTR optional, out -> "var"
; pcchMaxPassword : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL CredUnPackAuthenticationBufferA(CRED_PACK_FLAGS dwFlags, void* pAuthBuffer, DWORD cbAuthBuffer, LPSTR pszUserName, DWORD* pcchlMaxUserName, LPSTR pszDomainName, DWORD* pcchMaxDomainName, LPSTR pszPassword, DWORD* pcchMaxPassword)
#uselib "credui.dll"
#cfunc global CredUnPackAuthenticationBufferA "CredUnPackAuthenticationBufferA" int, intptr, int, var, var, var, var, var, var
; res = CredUnPackAuthenticationBufferA(dwFlags, pAuthBuffer, cbAuthBuffer, pszUserName, pcchlMaxUserName, pszDomainName, pcchMaxDomainName, pszPassword, pcchMaxPassword)
; dwFlags : CRED_PACK_FLAGS -> "int"
; pAuthBuffer : void* -> "intptr"
; cbAuthBuffer : DWORD -> "int"
; pszUserName : LPSTR optional, out -> "var"
; pcchlMaxUserName : DWORD* in/out -> "var"
; pszDomainName : LPSTR optional, out -> "var"
; pcchMaxDomainName : DWORD* optional, in/out -> "var"
; pszPassword : LPSTR optional, out -> "var"
; pcchMaxPassword : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	credui = windows.NewLazySystemDLL("credui.dll")
	procCredUnPackAuthenticationBufferA = credui.NewProc("CredUnPackAuthenticationBufferA")
)

// dwFlags (CRED_PACK_FLAGS), pAuthBuffer (void*), cbAuthBuffer (DWORD), pszUserName (LPSTR optional, out), pcchlMaxUserName (DWORD* in/out), pszDomainName (LPSTR optional, out), pcchMaxDomainName (DWORD* optional, in/out), pszPassword (LPSTR optional, out), pcchMaxPassword (DWORD* in/out)
r1, _, err := procCredUnPackAuthenticationBufferA.Call(
	uintptr(dwFlags),
	uintptr(pAuthBuffer),
	uintptr(cbAuthBuffer),
	uintptr(pszUserName),
	uintptr(pcchlMaxUserName),
	uintptr(pszDomainName),
	uintptr(pcchMaxDomainName),
	uintptr(pszPassword),
	uintptr(pcchMaxPassword),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function CredUnPackAuthenticationBufferA(
  dwFlags: DWORD;   // CRED_PACK_FLAGS
  pAuthBuffer: Pointer;   // void*
  cbAuthBuffer: DWORD;   // DWORD
  pszUserName: PAnsiChar;   // LPSTR optional, out
  pcchlMaxUserName: Pointer;   // DWORD* in/out
  pszDomainName: PAnsiChar;   // LPSTR optional, out
  pcchMaxDomainName: Pointer;   // DWORD* optional, in/out
  pszPassword: PAnsiChar;   // LPSTR optional, out
  pcchMaxPassword: Pointer   // DWORD* in/out
): BOOL; stdcall;
  external 'credui.dll' name 'CredUnPackAuthenticationBufferA';
result := DllCall("credui\CredUnPackAuthenticationBufferA"
    , "UInt", dwFlags   ; CRED_PACK_FLAGS
    , "Ptr", pAuthBuffer   ; void*
    , "UInt", cbAuthBuffer   ; DWORD
    , "Ptr", pszUserName   ; LPSTR optional, out
    , "Ptr", pcchlMaxUserName   ; DWORD* in/out
    , "Ptr", pszDomainName   ; LPSTR optional, out
    , "Ptr", pcchMaxDomainName   ; DWORD* optional, in/out
    , "Ptr", pszPassword   ; LPSTR optional, out
    , "Ptr", pcchMaxPassword   ; DWORD* in/out
    , "Int")   ; return: BOOL
●CredUnPackAuthenticationBufferA(dwFlags, pAuthBuffer, cbAuthBuffer, pszUserName, pcchlMaxUserName, pszDomainName, pcchMaxDomainName, pszPassword, pcchMaxPassword) = DLL("credui.dll", "bool CredUnPackAuthenticationBufferA(dword, void*, dword, char*, void*, char*, void*, char*, void*)")
# 呼び出し: CredUnPackAuthenticationBufferA(dwFlags, pAuthBuffer, cbAuthBuffer, pszUserName, pcchlMaxUserName, pszDomainName, pcchMaxDomainName, pszPassword, pcchMaxPassword)
# dwFlags : CRED_PACK_FLAGS -> "dword"
# pAuthBuffer : void* -> "void*"
# cbAuthBuffer : DWORD -> "dword"
# pszUserName : LPSTR optional, out -> "char*"
# pcchlMaxUserName : DWORD* in/out -> "void*"
# pszDomainName : LPSTR optional, out -> "char*"
# pcchMaxDomainName : DWORD* optional, in/out -> "void*"
# pszPassword : LPSTR optional, out -> "char*"
# pcchMaxPassword : DWORD* in/out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "credui" fn CredUnPackAuthenticationBufferA(
    dwFlags: u32, // CRED_PACK_FLAGS
    pAuthBuffer: ?*anyopaque, // void*
    cbAuthBuffer: u32, // DWORD
    pszUserName: [*c]u8, // LPSTR optional, out
    pcchlMaxUserName: [*c]u32, // DWORD* in/out
    pszDomainName: [*c]u8, // LPSTR optional, out
    pcchMaxDomainName: [*c]u32, // DWORD* optional, in/out
    pszPassword: [*c]u8, // LPSTR optional, out
    pcchMaxPassword: [*c]u32 // DWORD* in/out
) callconv(std.os.windows.WINAPI) i32;
proc CredUnPackAuthenticationBufferA(
    dwFlags: uint32,  # CRED_PACK_FLAGS
    pAuthBuffer: pointer,  # void*
    cbAuthBuffer: uint32,  # DWORD
    pszUserName: ptr char,  # LPSTR optional, out
    pcchlMaxUserName: ptr uint32,  # DWORD* in/out
    pszDomainName: ptr char,  # LPSTR optional, out
    pcchMaxDomainName: ptr uint32,  # DWORD* optional, in/out
    pszPassword: ptr char,  # LPSTR optional, out
    pcchMaxPassword: ptr uint32  # DWORD* in/out
): int32 {.importc: "CredUnPackAuthenticationBufferA", stdcall, dynlib: "credui.dll".}
pragma(lib, "credui");
extern(Windows)
int CredUnPackAuthenticationBufferA(
    uint dwFlags,   // CRED_PACK_FLAGS
    void* pAuthBuffer,   // void*
    uint cbAuthBuffer,   // DWORD
    char* pszUserName,   // LPSTR optional, out
    uint* pcchlMaxUserName,   // DWORD* in/out
    char* pszDomainName,   // LPSTR optional, out
    uint* pcchMaxDomainName,   // DWORD* optional, in/out
    char* pszPassword,   // LPSTR optional, out
    uint* pcchMaxPassword   // DWORD* in/out
);
ccall((:CredUnPackAuthenticationBufferA, "credui.dll"), stdcall, Int32,
      (UInt32, Ptr{Cvoid}, UInt32, Ptr{UInt8}, Ptr{UInt32}, Ptr{UInt8}, Ptr{UInt32}, Ptr{UInt8}, Ptr{UInt32}),
      dwFlags, pAuthBuffer, cbAuthBuffer, pszUserName, pcchlMaxUserName, pszDomainName, pcchMaxDomainName, pszPassword, pcchMaxPassword)
# dwFlags : CRED_PACK_FLAGS -> UInt32
# pAuthBuffer : void* -> Ptr{Cvoid}
# cbAuthBuffer : DWORD -> UInt32
# pszUserName : LPSTR optional, out -> Ptr{UInt8}
# pcchlMaxUserName : DWORD* in/out -> Ptr{UInt32}
# pszDomainName : LPSTR optional, out -> Ptr{UInt8}
# pcchMaxDomainName : DWORD* optional, in/out -> Ptr{UInt32}
# pszPassword : LPSTR optional, out -> Ptr{UInt8}
# pcchMaxPassword : DWORD* in/out -> Ptr{UInt32}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t CredUnPackAuthenticationBufferA(
    uint32_t dwFlags,
    void* pAuthBuffer,
    uint32_t cbAuthBuffer,
    char* pszUserName,
    uint32_t* pcchlMaxUserName,
    char* pszDomainName,
    uint32_t* pcchMaxDomainName,
    char* pszPassword,
    uint32_t* pcchMaxPassword);
]]
local credui = ffi.load("credui")
-- credui.CredUnPackAuthenticationBufferA(dwFlags, pAuthBuffer, cbAuthBuffer, pszUserName, pcchlMaxUserName, pszDomainName, pcchMaxDomainName, pszPassword, pcchMaxPassword)
-- dwFlags : CRED_PACK_FLAGS
-- pAuthBuffer : void*
-- cbAuthBuffer : DWORD
-- pszUserName : LPSTR optional, out
-- pcchlMaxUserName : DWORD* in/out
-- pszDomainName : LPSTR optional, out
-- pcchMaxDomainName : DWORD* optional, in/out
-- pszPassword : LPSTR optional, out
-- pcchMaxPassword : DWORD* in/out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('credui.dll');
const CredUnPackAuthenticationBufferA = lib.func('__stdcall', 'CredUnPackAuthenticationBufferA', 'int32_t', ['uint32_t', 'void *', 'uint32_t', 'char *', 'uint32_t *', 'char *', 'uint32_t *', 'char *', 'uint32_t *']);
// CredUnPackAuthenticationBufferA(dwFlags, pAuthBuffer, cbAuthBuffer, pszUserName, pcchlMaxUserName, pszDomainName, pcchMaxDomainName, pszPassword, pcchMaxPassword)
// dwFlags : CRED_PACK_FLAGS -> 'uint32_t'
// pAuthBuffer : void* -> 'void *'
// cbAuthBuffer : DWORD -> 'uint32_t'
// pszUserName : LPSTR optional, out -> 'char *'
// pcchlMaxUserName : DWORD* in/out -> 'uint32_t *'
// pszDomainName : LPSTR optional, out -> 'char *'
// pcchMaxDomainName : DWORD* optional, in/out -> 'uint32_t *'
// pszPassword : LPSTR optional, out -> 'char *'
// pcchMaxPassword : DWORD* in/out -> 'uint32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("credui.dll", {
  CredUnPackAuthenticationBufferA: { parameters: ["u32", "pointer", "u32", "buffer", "pointer", "buffer", "pointer", "buffer", "pointer"], result: "i32" },
});
// lib.symbols.CredUnPackAuthenticationBufferA(dwFlags, pAuthBuffer, cbAuthBuffer, pszUserName, pcchlMaxUserName, pszDomainName, pcchMaxDomainName, pszPassword, pcchMaxPassword)
// dwFlags : CRED_PACK_FLAGS -> "u32"
// pAuthBuffer : void* -> "pointer"
// cbAuthBuffer : DWORD -> "u32"
// pszUserName : LPSTR optional, out -> "buffer"
// pcchlMaxUserName : DWORD* in/out -> "pointer"
// pszDomainName : LPSTR optional, out -> "buffer"
// pcchMaxDomainName : DWORD* optional, in/out -> "pointer"
// pszPassword : LPSTR optional, out -> "buffer"
// pcchMaxPassword : DWORD* in/out -> "pointer"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t CredUnPackAuthenticationBufferA(
    uint32_t dwFlags,
    void* pAuthBuffer,
    uint32_t cbAuthBuffer,
    char* pszUserName,
    uint32_t* pcchlMaxUserName,
    char* pszDomainName,
    uint32_t* pcchMaxDomainName,
    char* pszPassword,
    uint32_t* pcchMaxPassword);
C, "credui.dll");
// $ffi->CredUnPackAuthenticationBufferA(dwFlags, pAuthBuffer, cbAuthBuffer, pszUserName, pcchlMaxUserName, pszDomainName, pcchMaxDomainName, pszPassword, pcchMaxPassword);
// dwFlags : CRED_PACK_FLAGS
// pAuthBuffer : void*
// cbAuthBuffer : DWORD
// pszUserName : LPSTR optional, out
// pcchlMaxUserName : DWORD* in/out
// pszDomainName : LPSTR optional, out
// pcchMaxDomainName : DWORD* optional, in/out
// pszPassword : LPSTR optional, out
// pcchMaxPassword : DWORD* in/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 Credui extends StdCallLibrary {
    Credui INSTANCE = Native.load("credui", Credui.class, W32APIOptions.ASCII_OPTIONS);
    boolean CredUnPackAuthenticationBufferA(
        int dwFlags,   // CRED_PACK_FLAGS
        Pointer pAuthBuffer,   // void*
        int cbAuthBuffer,   // DWORD
        byte[] pszUserName,   // LPSTR optional, out
        IntByReference pcchlMaxUserName,   // DWORD* in/out
        byte[] pszDomainName,   // LPSTR optional, out
        IntByReference pcchMaxDomainName,   // DWORD* optional, in/out
        byte[] pszPassword,   // LPSTR optional, out
        IntByReference pcchMaxPassword   // DWORD* in/out
    );
}
@[Link("credui")]
lib Libcredui
  fun CredUnPackAuthenticationBufferA = CredUnPackAuthenticationBufferA(
    dwFlags : UInt32,   # CRED_PACK_FLAGS
    pAuthBuffer : Void*,   # void*
    cbAuthBuffer : UInt32,   # DWORD
    pszUserName : UInt8*,   # LPSTR optional, out
    pcchlMaxUserName : UInt32*,   # DWORD* in/out
    pszDomainName : UInt8*,   # LPSTR optional, out
    pcchMaxDomainName : UInt32*,   # DWORD* optional, in/out
    pszPassword : UInt8*,   # LPSTR optional, out
    pcchMaxPassword : UInt32*   # DWORD* in/out
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef CredUnPackAuthenticationBufferANative = Int32 Function(Uint32, Pointer<Void>, Uint32, Pointer<Utf8>, Pointer<Uint32>, Pointer<Utf8>, Pointer<Uint32>, Pointer<Utf8>, Pointer<Uint32>);
typedef CredUnPackAuthenticationBufferADart = int Function(int, Pointer<Void>, int, Pointer<Utf8>, Pointer<Uint32>, Pointer<Utf8>, Pointer<Uint32>, Pointer<Utf8>, Pointer<Uint32>);
final CredUnPackAuthenticationBufferA = DynamicLibrary.open('credui.dll')
    .lookupFunction<CredUnPackAuthenticationBufferANative, CredUnPackAuthenticationBufferADart>('CredUnPackAuthenticationBufferA');
// dwFlags : CRED_PACK_FLAGS -> Uint32
// pAuthBuffer : void* -> Pointer<Void>
// cbAuthBuffer : DWORD -> Uint32
// pszUserName : LPSTR optional, out -> Pointer<Utf8>
// pcchlMaxUserName : DWORD* in/out -> Pointer<Uint32>
// pszDomainName : LPSTR optional, out -> Pointer<Utf8>
// pcchMaxDomainName : DWORD* optional, in/out -> Pointer<Uint32>
// pszPassword : LPSTR optional, out -> Pointer<Utf8>
// pcchMaxPassword : DWORD* in/out -> Pointer<Uint32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function CredUnPackAuthenticationBufferA(
  dwFlags: DWORD;   // CRED_PACK_FLAGS
  pAuthBuffer: Pointer;   // void*
  cbAuthBuffer: DWORD;   // DWORD
  pszUserName: PAnsiChar;   // LPSTR optional, out
  pcchlMaxUserName: Pointer;   // DWORD* in/out
  pszDomainName: PAnsiChar;   // LPSTR optional, out
  pcchMaxDomainName: Pointer;   // DWORD* optional, in/out
  pszPassword: PAnsiChar;   // LPSTR optional, out
  pcchMaxPassword: Pointer   // DWORD* in/out
): BOOL; stdcall;
  external 'credui.dll' name 'CredUnPackAuthenticationBufferA';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "CredUnPackAuthenticationBufferA"
  c_CredUnPackAuthenticationBufferA :: Word32 -> Ptr () -> Word32 -> CString -> Ptr Word32 -> CString -> Ptr Word32 -> CString -> Ptr Word32 -> IO CInt
-- dwFlags : CRED_PACK_FLAGS -> Word32
-- pAuthBuffer : void* -> Ptr ()
-- cbAuthBuffer : DWORD -> Word32
-- pszUserName : LPSTR optional, out -> CString
-- pcchlMaxUserName : DWORD* in/out -> Ptr Word32
-- pszDomainName : LPSTR optional, out -> CString
-- pcchMaxDomainName : DWORD* optional, in/out -> Ptr Word32
-- pszPassword : LPSTR optional, out -> CString
-- pcchMaxPassword : DWORD* in/out -> Ptr Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let credunpackauthenticationbuffera =
  foreign "CredUnPackAuthenticationBufferA"
    (uint32_t @-> (ptr void) @-> uint32_t @-> string @-> (ptr uint32_t) @-> string @-> (ptr uint32_t) @-> string @-> (ptr uint32_t) @-> returning int32_t)
(* dwFlags : CRED_PACK_FLAGS -> uint32_t *)
(* pAuthBuffer : void* -> (ptr void) *)
(* cbAuthBuffer : DWORD -> uint32_t *)
(* pszUserName : LPSTR optional, out -> string *)
(* pcchlMaxUserName : DWORD* in/out -> (ptr uint32_t) *)
(* pszDomainName : LPSTR optional, out -> string *)
(* pcchMaxDomainName : DWORD* optional, in/out -> (ptr uint32_t) *)
(* pszPassword : LPSTR optional, out -> string *)
(* pcchMaxPassword : DWORD* in/out -> (ptr uint32_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library credui (t "credui.dll"))
(cffi:use-foreign-library credui)

(cffi:defcfun ("CredUnPackAuthenticationBufferA" cred-un-pack-authentication-buffer-a :convention :stdcall) :int32
  (dw-flags :uint32)   ; CRED_PACK_FLAGS
  (p-auth-buffer :pointer)   ; void*
  (cb-auth-buffer :uint32)   ; DWORD
  (psz-user-name :pointer)   ; LPSTR optional, out
  (pcchl-max-user-name :pointer)   ; DWORD* in/out
  (psz-domain-name :pointer)   ; LPSTR optional, out
  (pcch-max-domain-name :pointer)   ; DWORD* optional, in/out
  (psz-password :pointer)   ; LPSTR optional, out
  (pcch-max-password :pointer))   ; DWORD* in/out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $CredUnPackAuthenticationBufferA = Win32::API::More->new('credui',
    'BOOL CredUnPackAuthenticationBufferA(DWORD dwFlags, LPVOID pAuthBuffer, DWORD cbAuthBuffer, LPSTR pszUserName, LPVOID pcchlMaxUserName, LPSTR pszDomainName, LPVOID pcchMaxDomainName, LPSTR pszPassword, LPVOID pcchMaxPassword)');
# my $ret = $CredUnPackAuthenticationBufferA->Call($dwFlags, $pAuthBuffer, $cbAuthBuffer, $pszUserName, $pcchlMaxUserName, $pszDomainName, $pcchMaxDomainName, $pszPassword, $pcchMaxPassword);
# dwFlags : CRED_PACK_FLAGS -> DWORD
# pAuthBuffer : void* -> LPVOID
# cbAuthBuffer : DWORD -> DWORD
# pszUserName : LPSTR optional, out -> LPSTR
# pcchlMaxUserName : DWORD* in/out -> LPVOID
# pszDomainName : LPSTR optional, out -> LPSTR
# pcchMaxDomainName : DWORD* optional, in/out -> LPVOID
# pszPassword : LPSTR optional, out -> LPSTR
# pcchMaxPassword : DWORD* in/out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

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