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

CredUnPackAuthenticationBufferW

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

シグネチャ

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

BOOL CredUnPackAuthenticationBufferW(
    CRED_PACK_FLAGS dwFlags,
    void* pAuthBuffer,
    DWORD cbAuthBuffer,
    LPWSTR pszUserName,   // optional
    DWORD* pcchMaxUserName,
    LPWSTR pszDomainName,   // optional
    DWORD* pcchMaxDomainName,   // optional
    LPWSTR 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 バッファのサイズ(バイト単位)。
pszUserNameLPWSTRoutoptional

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

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

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

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

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

- pcchlMaxUserName [in, out]

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

戻り値の型: BOOL

公式ドキュメント

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

戻り値

関数が成功した場合は TRUE、それ以外の場合は FALSE

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

戻り値/値 説明
ERROR_NOT_CAPABLE
dwFlags パラメーターの値として CRED_PACK_PROTECTED_CREDENTIALS が渡されましたが、パスワードの保護に使用されたセキュリティコンテキストが呼び出し元のセキュリティコンテキストと異なるため、この関数は資格情報を復号できません。
ERROR_INSUFFICIENT_BUFFER
出力バッファ pszUserNamepszDomainName、または pszPassword のいずれかのサイズが十分ではありませんでした。
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 関数を使用して構築することもできます。 認証バッファ pAuthBuffer が、KERB_CERTIFICATE_LOGONSEC_WINNT_AUTH_IDENTITY_EX2 などのパスワード以外の資格情報を表す場合、関数は資格情報を文字列としてマーシャリングし、ユーザー名、ドメイン名、パスワードの各文字列として返す必要があります。マーシャリングは特定の手順に従って行われます。dwFlagsCRED_PACK_PROTECTED_CREDENTIALS フラグが含まれている場合、呼び出し元は資格情報が暗号化されたのと同じログオンセッション内で実行されている必要があります。

メモ

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

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

各言語での呼び出し定義

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

BOOL CredUnPackAuthenticationBufferW(
    CRED_PACK_FLAGS dwFlags,
    void* pAuthBuffer,
    DWORD cbAuthBuffer,
    LPWSTR pszUserName,   // optional
    DWORD* pcchMaxUserName,
    LPWSTR pszDomainName,   // optional
    DWORD* pcchMaxDomainName,   // optional
    LPWSTR pszPassword,   // optional
    DWORD* pcchMaxPassword
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("credui.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern bool CredUnPackAuthenticationBufferW(
    uint dwFlags,   // CRED_PACK_FLAGS
    IntPtr pAuthBuffer,   // void*
    uint cbAuthBuffer,   // DWORD
    [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszUserName,   // LPWSTR optional, out
    ref uint pcchMaxUserName,   // DWORD* in/out
    [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszDomainName,   // LPWSTR optional, out
    IntPtr pcchMaxDomainName,   // DWORD* optional, in/out
    [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszPassword,   // LPWSTR optional, out
    ref uint pcchMaxPassword   // DWORD* in/out
);
<DllImport("credui.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function CredUnPackAuthenticationBufferW(
    dwFlags As UInteger,   ' CRED_PACK_FLAGS
    pAuthBuffer As IntPtr,   ' void*
    cbAuthBuffer As UInteger,   ' DWORD
    <MarshalAs(UnmanagedType.LPWStr)> pszUserName As System.Text.StringBuilder,   ' LPWSTR optional, out
    ByRef pcchMaxUserName As UInteger,   ' DWORD* in/out
    <MarshalAs(UnmanagedType.LPWStr)> pszDomainName As System.Text.StringBuilder,   ' LPWSTR optional, out
    pcchMaxDomainName As IntPtr,   ' DWORD* optional, in/out
    <MarshalAs(UnmanagedType.LPWStr)> pszPassword As System.Text.StringBuilder,   ' LPWSTR 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 : LPWSTR optional, out
' pcchMaxUserName : DWORD* in/out
' pszDomainName : LPWSTR optional, out
' pcchMaxDomainName : DWORD* optional, in/out
' pszPassword : LPWSTR optional, out
' pcchMaxPassword : DWORD* in/out
Declare PtrSafe Function CredUnPackAuthenticationBufferW Lib "credui" ( _
    ByVal dwFlags As Long, _
    ByVal pAuthBuffer As LongPtr, _
    ByVal cbAuthBuffer As Long, _
    ByVal pszUserName As LongPtr, _
    ByRef pcchMaxUserName As Long, _
    ByVal pszDomainName As LongPtr, _
    ByVal pcchMaxDomainName As LongPtr, _
    ByVal pszPassword As LongPtr, _
    ByRef pcchMaxPassword As Long) 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

CredUnPackAuthenticationBufferW = ctypes.windll.credui.CredUnPackAuthenticationBufferW
CredUnPackAuthenticationBufferW.restype = wintypes.BOOL
CredUnPackAuthenticationBufferW.argtypes = [
    wintypes.DWORD,  # dwFlags : CRED_PACK_FLAGS
    ctypes.POINTER(None),  # pAuthBuffer : void*
    wintypes.DWORD,  # cbAuthBuffer : DWORD
    wintypes.LPWSTR,  # pszUserName : LPWSTR optional, out
    ctypes.POINTER(wintypes.DWORD),  # pcchMaxUserName : DWORD* in/out
    wintypes.LPWSTR,  # pszDomainName : LPWSTR optional, out
    ctypes.POINTER(wintypes.DWORD),  # pcchMaxDomainName : DWORD* optional, in/out
    wintypes.LPWSTR,  # pszPassword : LPWSTR 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')
CredUnPackAuthenticationBufferW = Fiddle::Function.new(
  lib['CredUnPackAuthenticationBufferW'],
  [
    -Fiddle::TYPE_INT,  # dwFlags : CRED_PACK_FLAGS
    Fiddle::TYPE_VOIDP,  # pAuthBuffer : void*
    -Fiddle::TYPE_INT,  # cbAuthBuffer : DWORD
    Fiddle::TYPE_VOIDP,  # pszUserName : LPWSTR optional, out
    Fiddle::TYPE_VOIDP,  # pcchMaxUserName : DWORD* in/out
    Fiddle::TYPE_VOIDP,  # pszDomainName : LPWSTR optional, out
    Fiddle::TYPE_VOIDP,  # pcchMaxDomainName : DWORD* optional, in/out
    Fiddle::TYPE_VOIDP,  # pszPassword : LPWSTR optional, out
    Fiddle::TYPE_VOIDP,  # pcchMaxPassword : DWORD* in/out
  ],
  Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "credui")]
extern "system" {
    fn CredUnPackAuthenticationBufferW(
        dwFlags: u32,  // CRED_PACK_FLAGS
        pAuthBuffer: *mut (),  // void*
        cbAuthBuffer: u32,  // DWORD
        pszUserName: *mut u16,  // LPWSTR optional, out
        pcchMaxUserName: *mut u32,  // DWORD* in/out
        pszDomainName: *mut u16,  // LPWSTR optional, out
        pcchMaxDomainName: *mut u32,  // DWORD* optional, in/out
        pszPassword: *mut u16,  // LPWSTR 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.Unicode, SetLastError = true)]
public static extern bool CredUnPackAuthenticationBufferW(uint dwFlags, IntPtr pAuthBuffer, uint cbAuthBuffer, [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszUserName, ref uint pcchMaxUserName, [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszDomainName, IntPtr pcchMaxDomainName, [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszPassword, ref uint pcchMaxPassword);
"@
$api = Add-Type -MemberDefinition $sig -Name 'credui_CredUnPackAuthenticationBufferW' -Namespace Win32 -PassThru
# $api::CredUnPackAuthenticationBufferW(dwFlags, pAuthBuffer, cbAuthBuffer, pszUserName, pcchMaxUserName, pszDomainName, pcchMaxDomainName, pszPassword, pcchMaxPassword)
#uselib "credui.dll"
#func global CredUnPackAuthenticationBufferW "CredUnPackAuthenticationBufferW" wptr, wptr, wptr, wptr, wptr, wptr, wptr, wptr, wptr
; CredUnPackAuthenticationBufferW dwFlags, pAuthBuffer, cbAuthBuffer, varptr(pszUserName), varptr(pcchMaxUserName), varptr(pszDomainName), varptr(pcchMaxDomainName), varptr(pszPassword), varptr(pcchMaxPassword)   ; 戻り値は stat
; dwFlags : CRED_PACK_FLAGS -> "wptr"
; pAuthBuffer : void* -> "wptr"
; cbAuthBuffer : DWORD -> "wptr"
; pszUserName : LPWSTR optional, out -> "wptr"
; pcchMaxUserName : DWORD* in/out -> "wptr"
; pszDomainName : LPWSTR optional, out -> "wptr"
; pcchMaxDomainName : DWORD* optional, in/out -> "wptr"
; pszPassword : LPWSTR optional, out -> "wptr"
; pcchMaxPassword : DWORD* in/out -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "credui.dll"
#cfunc global CredUnPackAuthenticationBufferW "CredUnPackAuthenticationBufferW" int, sptr, int, var, var, var, var, var, var
; res = CredUnPackAuthenticationBufferW(dwFlags, pAuthBuffer, cbAuthBuffer, pszUserName, pcchMaxUserName, pszDomainName, pcchMaxDomainName, pszPassword, pcchMaxPassword)
; dwFlags : CRED_PACK_FLAGS -> "int"
; pAuthBuffer : void* -> "sptr"
; cbAuthBuffer : DWORD -> "int"
; pszUserName : LPWSTR optional, out -> "var"
; pcchMaxUserName : DWORD* in/out -> "var"
; pszDomainName : LPWSTR optional, out -> "var"
; pcchMaxDomainName : DWORD* optional, in/out -> "var"
; pszPassword : LPWSTR optional, out -> "var"
; pcchMaxPassword : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL CredUnPackAuthenticationBufferW(CRED_PACK_FLAGS dwFlags, void* pAuthBuffer, DWORD cbAuthBuffer, LPWSTR pszUserName, DWORD* pcchMaxUserName, LPWSTR pszDomainName, DWORD* pcchMaxDomainName, LPWSTR pszPassword, DWORD* pcchMaxPassword)
#uselib "credui.dll"
#cfunc global CredUnPackAuthenticationBufferW "CredUnPackAuthenticationBufferW" int, intptr, int, var, var, var, var, var, var
; res = CredUnPackAuthenticationBufferW(dwFlags, pAuthBuffer, cbAuthBuffer, pszUserName, pcchMaxUserName, pszDomainName, pcchMaxDomainName, pszPassword, pcchMaxPassword)
; dwFlags : CRED_PACK_FLAGS -> "int"
; pAuthBuffer : void* -> "intptr"
; cbAuthBuffer : DWORD -> "int"
; pszUserName : LPWSTR optional, out -> "var"
; pcchMaxUserName : DWORD* in/out -> "var"
; pszDomainName : LPWSTR optional, out -> "var"
; pcchMaxDomainName : DWORD* optional, in/out -> "var"
; pszPassword : LPWSTR optional, out -> "var"
; pcchMaxPassword : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	credui = windows.NewLazySystemDLL("credui.dll")
	procCredUnPackAuthenticationBufferW = credui.NewProc("CredUnPackAuthenticationBufferW")
)

// dwFlags (CRED_PACK_FLAGS), pAuthBuffer (void*), cbAuthBuffer (DWORD), pszUserName (LPWSTR optional, out), pcchMaxUserName (DWORD* in/out), pszDomainName (LPWSTR optional, out), pcchMaxDomainName (DWORD* optional, in/out), pszPassword (LPWSTR optional, out), pcchMaxPassword (DWORD* in/out)
r1, _, err := procCredUnPackAuthenticationBufferW.Call(
	uintptr(dwFlags),
	uintptr(pAuthBuffer),
	uintptr(cbAuthBuffer),
	uintptr(pszUserName),
	uintptr(pcchMaxUserName),
	uintptr(pszDomainName),
	uintptr(pcchMaxDomainName),
	uintptr(pszPassword),
	uintptr(pcchMaxPassword),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function CredUnPackAuthenticationBufferW(
  dwFlags: DWORD;   // CRED_PACK_FLAGS
  pAuthBuffer: Pointer;   // void*
  cbAuthBuffer: DWORD;   // DWORD
  pszUserName: PWideChar;   // LPWSTR optional, out
  pcchMaxUserName: Pointer;   // DWORD* in/out
  pszDomainName: PWideChar;   // LPWSTR optional, out
  pcchMaxDomainName: Pointer;   // DWORD* optional, in/out
  pszPassword: PWideChar;   // LPWSTR optional, out
  pcchMaxPassword: Pointer   // DWORD* in/out
): BOOL; stdcall;
  external 'credui.dll' name 'CredUnPackAuthenticationBufferW';
result := DllCall("credui\CredUnPackAuthenticationBufferW"
    , "UInt", dwFlags   ; CRED_PACK_FLAGS
    , "Ptr", pAuthBuffer   ; void*
    , "UInt", cbAuthBuffer   ; DWORD
    , "Ptr", pszUserName   ; LPWSTR optional, out
    , "Ptr", pcchMaxUserName   ; DWORD* in/out
    , "Ptr", pszDomainName   ; LPWSTR optional, out
    , "Ptr", pcchMaxDomainName   ; DWORD* optional, in/out
    , "Ptr", pszPassword   ; LPWSTR optional, out
    , "Ptr", pcchMaxPassword   ; DWORD* in/out
    , "Int")   ; return: BOOL
●CredUnPackAuthenticationBufferW(dwFlags, pAuthBuffer, cbAuthBuffer, pszUserName, pcchMaxUserName, pszDomainName, pcchMaxDomainName, pszPassword, pcchMaxPassword) = DLL("credui.dll", "bool CredUnPackAuthenticationBufferW(dword, void*, dword, char*, void*, char*, void*, char*, void*)")
# 呼び出し: CredUnPackAuthenticationBufferW(dwFlags, pAuthBuffer, cbAuthBuffer, pszUserName, pcchMaxUserName, pszDomainName, pcchMaxDomainName, pszPassword, pcchMaxPassword)
# dwFlags : CRED_PACK_FLAGS -> "dword"
# pAuthBuffer : void* -> "void*"
# cbAuthBuffer : DWORD -> "dword"
# pszUserName : LPWSTR optional, out -> "char*"
# pcchMaxUserName : DWORD* in/out -> "void*"
# pszDomainName : LPWSTR optional, out -> "char*"
# pcchMaxDomainName : DWORD* optional, in/out -> "void*"
# pszPassword : LPWSTR optional, out -> "char*"
# pcchMaxPassword : DWORD* in/out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。
const std = @import("std");

extern "credui" fn CredUnPackAuthenticationBufferW(
    dwFlags: u32, // CRED_PACK_FLAGS
    pAuthBuffer: ?*anyopaque, // void*
    cbAuthBuffer: u32, // DWORD
    pszUserName: [*c]u16, // LPWSTR optional, out
    pcchMaxUserName: [*c]u32, // DWORD* in/out
    pszDomainName: [*c]u16, // LPWSTR optional, out
    pcchMaxDomainName: [*c]u32, // DWORD* optional, in/out
    pszPassword: [*c]u16, // LPWSTR optional, out
    pcchMaxPassword: [*c]u32 // DWORD* in/out
) callconv(std.os.windows.WINAPI) i32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。
proc CredUnPackAuthenticationBufferW(
    dwFlags: uint32,  # CRED_PACK_FLAGS
    pAuthBuffer: pointer,  # void*
    cbAuthBuffer: uint32,  # DWORD
    pszUserName: ptr uint16,  # LPWSTR optional, out
    pcchMaxUserName: ptr uint32,  # DWORD* in/out
    pszDomainName: ptr uint16,  # LPWSTR optional, out
    pcchMaxDomainName: ptr uint32,  # DWORD* optional, in/out
    pszPassword: ptr uint16,  # LPWSTR optional, out
    pcchMaxPassword: ptr uint32  # DWORD* in/out
): int32 {.importc: "CredUnPackAuthenticationBufferW", stdcall, dynlib: "credui.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "credui");
extern(Windows)
int CredUnPackAuthenticationBufferW(
    uint dwFlags,   // CRED_PACK_FLAGS
    void* pAuthBuffer,   // void*
    uint cbAuthBuffer,   // DWORD
    wchar* pszUserName,   // LPWSTR optional, out
    uint* pcchMaxUserName,   // DWORD* in/out
    wchar* pszDomainName,   // LPWSTR optional, out
    uint* pcchMaxDomainName,   // DWORD* optional, in/out
    wchar* pszPassword,   // LPWSTR optional, out
    uint* pcchMaxPassword   // DWORD* in/out
);
ccall((:CredUnPackAuthenticationBufferW, "credui.dll"), stdcall, Int32,
      (UInt32, Ptr{Cvoid}, UInt32, Ptr{UInt16}, Ptr{UInt32}, Ptr{UInt16}, Ptr{UInt32}, Ptr{UInt16}, Ptr{UInt32}),
      dwFlags, pAuthBuffer, cbAuthBuffer, pszUserName, pcchMaxUserName, pszDomainName, pcchMaxDomainName, pszPassword, pcchMaxPassword)
# dwFlags : CRED_PACK_FLAGS -> UInt32
# pAuthBuffer : void* -> Ptr{Cvoid}
# cbAuthBuffer : DWORD -> UInt32
# pszUserName : LPWSTR optional, out -> Ptr{UInt16}
# pcchMaxUserName : DWORD* in/out -> Ptr{UInt32}
# pszDomainName : LPWSTR optional, out -> Ptr{UInt16}
# pcchMaxDomainName : DWORD* optional, in/out -> Ptr{UInt32}
# pszPassword : LPWSTR optional, out -> Ptr{UInt16}
# pcchMaxPassword : DWORD* in/out -> Ptr{UInt32}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。
local ffi = require("ffi")
ffi.cdef[[
int32_t CredUnPackAuthenticationBufferW(
    uint32_t dwFlags,
    void* pAuthBuffer,
    uint32_t cbAuthBuffer,
    uint16_t* pszUserName,
    uint32_t* pcchMaxUserName,
    uint16_t* pszDomainName,
    uint32_t* pcchMaxDomainName,
    uint16_t* pszPassword,
    uint32_t* pcchMaxPassword);
]]
local credui = ffi.load("credui")
-- credui.CredUnPackAuthenticationBufferW(dwFlags, pAuthBuffer, cbAuthBuffer, pszUserName, pcchMaxUserName, pszDomainName, pcchMaxDomainName, pszPassword, pcchMaxPassword)
-- dwFlags : CRED_PACK_FLAGS
-- pAuthBuffer : void*
-- cbAuthBuffer : DWORD
-- pszUserName : LPWSTR optional, out
-- pcchMaxUserName : DWORD* in/out
-- pszDomainName : LPWSTR optional, out
-- pcchMaxDomainName : DWORD* optional, in/out
-- pszPassword : LPWSTR optional, out
-- pcchMaxPassword : DWORD* in/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('credui.dll');
const CredUnPackAuthenticationBufferW = lib.func('__stdcall', 'CredUnPackAuthenticationBufferW', 'int32_t', ['uint32_t', 'void *', 'uint32_t', 'uint16_t *', 'uint32_t *', 'uint16_t *', 'uint32_t *', 'uint16_t *', 'uint32_t *']);
// CredUnPackAuthenticationBufferW(dwFlags, pAuthBuffer, cbAuthBuffer, pszUserName, pcchMaxUserName, pszDomainName, pcchMaxDomainName, pszPassword, pcchMaxPassword)
// dwFlags : CRED_PACK_FLAGS -> 'uint32_t'
// pAuthBuffer : void* -> 'void *'
// cbAuthBuffer : DWORD -> 'uint32_t'
// pszUserName : LPWSTR optional, out -> 'uint16_t *'
// pcchMaxUserName : DWORD* in/out -> 'uint32_t *'
// pszDomainName : LPWSTR optional, out -> 'uint16_t *'
// pcchMaxDomainName : DWORD* optional, in/out -> 'uint32_t *'
// pszPassword : LPWSTR optional, out -> 'uint16_t *'
// pcchMaxPassword : DWORD* in/out -> 'uint32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("credui.dll", {
  CredUnPackAuthenticationBufferW: { parameters: ["u32", "pointer", "u32", "buffer", "pointer", "buffer", "pointer", "buffer", "pointer"], result: "i32" },
});
// lib.symbols.CredUnPackAuthenticationBufferW(dwFlags, pAuthBuffer, cbAuthBuffer, pszUserName, pcchMaxUserName, pszDomainName, pcchMaxDomainName, pszPassword, pcchMaxPassword)
// dwFlags : CRED_PACK_FLAGS -> "u32"
// pAuthBuffer : void* -> "pointer"
// cbAuthBuffer : DWORD -> "u32"
// pszUserName : LPWSTR optional, out -> "buffer"
// pcchMaxUserName : DWORD* in/out -> "pointer"
// pszDomainName : LPWSTR optional, out -> "buffer"
// pcchMaxDomainName : DWORD* optional, in/out -> "pointer"
// pszPassword : LPWSTR optional, out -> "buffer"
// pcchMaxPassword : DWORD* in/out -> "pointer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t CredUnPackAuthenticationBufferW(
    uint32_t dwFlags,
    void* pAuthBuffer,
    uint32_t cbAuthBuffer,
    uint16_t* pszUserName,
    uint32_t* pcchMaxUserName,
    uint16_t* pszDomainName,
    uint32_t* pcchMaxDomainName,
    uint16_t* pszPassword,
    uint32_t* pcchMaxPassword);
C, "credui.dll");
// $ffi->CredUnPackAuthenticationBufferW(dwFlags, pAuthBuffer, cbAuthBuffer, pszUserName, pcchMaxUserName, pszDomainName, pcchMaxDomainName, pszPassword, pcchMaxPassword);
// dwFlags : CRED_PACK_FLAGS
// pAuthBuffer : void*
// cbAuthBuffer : DWORD
// pszUserName : LPWSTR optional, out
// pcchMaxUserName : DWORD* in/out
// pszDomainName : LPWSTR optional, out
// pcchMaxDomainName : DWORD* optional, in/out
// pszPassword : LPWSTR 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.UNICODE_OPTIONS);
    boolean CredUnPackAuthenticationBufferW(
        int dwFlags,   // CRED_PACK_FLAGS
        Pointer pAuthBuffer,   // void*
        int cbAuthBuffer,   // DWORD
        char[] pszUserName,   // LPWSTR optional, out
        IntByReference pcchMaxUserName,   // DWORD* in/out
        char[] pszDomainName,   // LPWSTR optional, out
        IntByReference pcchMaxDomainName,   // DWORD* optional, in/out
        char[] pszPassword,   // LPWSTR optional, out
        IntByReference pcchMaxPassword   // DWORD* in/out
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("credui")]
lib Libcredui
  fun CredUnPackAuthenticationBufferW = CredUnPackAuthenticationBufferW(
    dwFlags : UInt32,   # CRED_PACK_FLAGS
    pAuthBuffer : Void*,   # void*
    cbAuthBuffer : UInt32,   # DWORD
    pszUserName : UInt16*,   # LPWSTR optional, out
    pcchMaxUserName : UInt32*,   # DWORD* in/out
    pszDomainName : UInt16*,   # LPWSTR optional, out
    pcchMaxDomainName : UInt32*,   # DWORD* optional, in/out
    pszPassword : UInt16*,   # LPWSTR 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 CredUnPackAuthenticationBufferWNative = Int32 Function(Uint32, Pointer<Void>, Uint32, Pointer<Utf16>, Pointer<Uint32>, Pointer<Utf16>, Pointer<Uint32>, Pointer<Utf16>, Pointer<Uint32>);
typedef CredUnPackAuthenticationBufferWDart = int Function(int, Pointer<Void>, int, Pointer<Utf16>, Pointer<Uint32>, Pointer<Utf16>, Pointer<Uint32>, Pointer<Utf16>, Pointer<Uint32>);
final CredUnPackAuthenticationBufferW = DynamicLibrary.open('credui.dll')
    .lookupFunction<CredUnPackAuthenticationBufferWNative, CredUnPackAuthenticationBufferWDart>('CredUnPackAuthenticationBufferW');
// dwFlags : CRED_PACK_FLAGS -> Uint32
// pAuthBuffer : void* -> Pointer<Void>
// cbAuthBuffer : DWORD -> Uint32
// pszUserName : LPWSTR optional, out -> Pointer<Utf16>
// pcchMaxUserName : DWORD* in/out -> Pointer<Uint32>
// pszDomainName : LPWSTR optional, out -> Pointer<Utf16>
// pcchMaxDomainName : DWORD* optional, in/out -> Pointer<Uint32>
// pszPassword : LPWSTR optional, out -> Pointer<Utf16>
// pcchMaxPassword : DWORD* in/out -> Pointer<Uint32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function CredUnPackAuthenticationBufferW(
  dwFlags: DWORD;   // CRED_PACK_FLAGS
  pAuthBuffer: Pointer;   // void*
  cbAuthBuffer: DWORD;   // DWORD
  pszUserName: PWideChar;   // LPWSTR optional, out
  pcchMaxUserName: Pointer;   // DWORD* in/out
  pszDomainName: PWideChar;   // LPWSTR optional, out
  pcchMaxDomainName: Pointer;   // DWORD* optional, in/out
  pszPassword: PWideChar;   // LPWSTR optional, out
  pcchMaxPassword: Pointer   // DWORD* in/out
): BOOL; stdcall;
  external 'credui.dll' name 'CredUnPackAuthenticationBufferW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

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

let credunpackauthenticationbufferw =
  foreign "CredUnPackAuthenticationBufferW"
    (uint32_t @-> (ptr void) @-> uint32_t @-> (ptr uint16_t) @-> (ptr uint32_t) @-> (ptr uint16_t) @-> (ptr uint32_t) @-> (ptr uint16_t) @-> (ptr uint32_t) @-> returning int32_t)
(* dwFlags : CRED_PACK_FLAGS -> uint32_t *)
(* pAuthBuffer : void* -> (ptr void) *)
(* cbAuthBuffer : DWORD -> uint32_t *)
(* pszUserName : LPWSTR optional, out -> (ptr uint16_t) *)
(* pcchMaxUserName : DWORD* in/out -> (ptr uint32_t) *)
(* pszDomainName : LPWSTR optional, out -> (ptr uint16_t) *)
(* pcchMaxDomainName : DWORD* optional, in/out -> (ptr uint32_t) *)
(* pszPassword : LPWSTR optional, out -> (ptr uint16_t) *)
(* 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 ("CredUnPackAuthenticationBufferW" cred-un-pack-authentication-buffer-w :convention :stdcall) :int32
  (dw-flags :uint32)   ; CRED_PACK_FLAGS
  (p-auth-buffer :pointer)   ; void*
  (cb-auth-buffer :uint32)   ; DWORD
  (psz-user-name :pointer)   ; LPWSTR optional, out
  (pcch-max-user-name :pointer)   ; DWORD* in/out
  (psz-domain-name :pointer)   ; LPWSTR optional, out
  (pcch-max-domain-name :pointer)   ; DWORD* optional, in/out
  (psz-password :pointer)   ; LPWSTR 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 $CredUnPackAuthenticationBufferW = Win32::API::More->new('credui',
    'BOOL CredUnPackAuthenticationBufferW(DWORD dwFlags, LPVOID pAuthBuffer, DWORD cbAuthBuffer, LPWSTR pszUserName, LPVOID pcchMaxUserName, LPWSTR pszDomainName, LPVOID pcchMaxDomainName, LPWSTR pszPassword, LPVOID pcchMaxPassword)');
# my $ret = $CredUnPackAuthenticationBufferW->Call($dwFlags, $pAuthBuffer, $cbAuthBuffer, $pszUserName, $pcchMaxUserName, $pszDomainName, $pcchMaxDomainName, $pszPassword, $pcchMaxPassword);
# dwFlags : CRED_PACK_FLAGS -> DWORD
# pAuthBuffer : void* -> LPVOID
# cbAuthBuffer : DWORD -> DWORD
# pszUserName : LPWSTR optional, out -> LPWSTR
# pcchMaxUserName : DWORD* in/out -> LPVOID
# pszDomainName : LPWSTR optional, out -> LPWSTR
# pcchMaxDomainName : DWORD* optional, in/out -> LPVOID
# pszPassword : LPWSTR optional, out -> LPWSTR
# pcchMaxPassword : DWORD* in/out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

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