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

CredPackAuthenticationBufferW

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

シグネチャ

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

BOOL CredPackAuthenticationBufferW(
    CRED_PACK_FLAGS dwFlags,
    LPWSTR pszUserName,
    LPWSTR pszPassword,
    BYTE* pPackedCredentials,   // optional
    DWORD* pcbPackedCredentials
);

パラメーター

名前方向
dwFlagsCRED_PACK_FLAGSin
pszUserNameLPWSTRin
pszPasswordLPWSTRin
pPackedCredentialsBYTE*outoptional
pcbPackedCredentialsDWORD*inout

戻り値の型: BOOL

各言語での呼び出し定義

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

BOOL CredPackAuthenticationBufferW(
    CRED_PACK_FLAGS dwFlags,
    LPWSTR pszUserName,
    LPWSTR pszPassword,
    BYTE* pPackedCredentials,   // optional
    DWORD* pcbPackedCredentials
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("credui.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern bool CredPackAuthenticationBufferW(
    uint dwFlags,   // CRED_PACK_FLAGS
    [MarshalAs(UnmanagedType.LPWStr)] string pszUserName,   // LPWSTR
    [MarshalAs(UnmanagedType.LPWStr)] string pszPassword,   // LPWSTR
    IntPtr pPackedCredentials,   // BYTE* optional, out
    ref uint pcbPackedCredentials   // DWORD* in/out
);
<DllImport("credui.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function CredPackAuthenticationBufferW(
    dwFlags As UInteger,   ' CRED_PACK_FLAGS
    <MarshalAs(UnmanagedType.LPWStr)> pszUserName As String,   ' LPWSTR
    <MarshalAs(UnmanagedType.LPWStr)> pszPassword As String,   ' LPWSTR
    pPackedCredentials As IntPtr,   ' BYTE* optional, out
    ByRef pcbPackedCredentials As UInteger   ' DWORD* in/out
) As Boolean
End Function
' dwFlags : CRED_PACK_FLAGS
' pszUserName : LPWSTR
' pszPassword : LPWSTR
' pPackedCredentials : BYTE* optional, out
' pcbPackedCredentials : DWORD* in/out
Declare PtrSafe Function CredPackAuthenticationBufferW Lib "credui" ( _
    ByVal dwFlags As Long, _
    ByVal pszUserName As LongPtr, _
    ByVal pszPassword As LongPtr, _
    ByVal pPackedCredentials As LongPtr, _
    ByRef pcbPackedCredentials 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

CredPackAuthenticationBufferW = ctypes.windll.credui.CredPackAuthenticationBufferW
CredPackAuthenticationBufferW.restype = wintypes.BOOL
CredPackAuthenticationBufferW.argtypes = [
    wintypes.DWORD,  # dwFlags : CRED_PACK_FLAGS
    wintypes.LPCWSTR,  # pszUserName : LPWSTR
    wintypes.LPCWSTR,  # pszPassword : LPWSTR
    ctypes.POINTER(ctypes.c_ubyte),  # pPackedCredentials : BYTE* optional, out
    ctypes.POINTER(wintypes.DWORD),  # pcbPackedCredentials : DWORD* in/out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('credui.dll')
CredPackAuthenticationBufferW = Fiddle::Function.new(
  lib['CredPackAuthenticationBufferW'],
  [
    -Fiddle::TYPE_INT,  # dwFlags : CRED_PACK_FLAGS
    Fiddle::TYPE_VOIDP,  # pszUserName : LPWSTR
    Fiddle::TYPE_VOIDP,  # pszPassword : LPWSTR
    Fiddle::TYPE_VOIDP,  # pPackedCredentials : BYTE* optional, out
    Fiddle::TYPE_VOIDP,  # pcbPackedCredentials : DWORD* in/out
  ],
  Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "credui")]
extern "system" {
    fn CredPackAuthenticationBufferW(
        dwFlags: u32,  // CRED_PACK_FLAGS
        pszUserName: *mut u16,  // LPWSTR
        pszPassword: *mut u16,  // LPWSTR
        pPackedCredentials: *mut u8,  // BYTE* optional, out
        pcbPackedCredentials: *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 CredPackAuthenticationBufferW(uint dwFlags, [MarshalAs(UnmanagedType.LPWStr)] string pszUserName, [MarshalAs(UnmanagedType.LPWStr)] string pszPassword, IntPtr pPackedCredentials, ref uint pcbPackedCredentials);
"@
$api = Add-Type -MemberDefinition $sig -Name 'credui_CredPackAuthenticationBufferW' -Namespace Win32 -PassThru
# $api::CredPackAuthenticationBufferW(dwFlags, pszUserName, pszPassword, pPackedCredentials, pcbPackedCredentials)
#uselib "credui.dll"
#func global CredPackAuthenticationBufferW "CredPackAuthenticationBufferW" wptr, wptr, wptr, wptr, wptr
; CredPackAuthenticationBufferW dwFlags, pszUserName, pszPassword, varptr(pPackedCredentials), varptr(pcbPackedCredentials)   ; 戻り値は stat
; dwFlags : CRED_PACK_FLAGS -> "wptr"
; pszUserName : LPWSTR -> "wptr"
; pszPassword : LPWSTR -> "wptr"
; pPackedCredentials : BYTE* optional, out -> "wptr"
; pcbPackedCredentials : DWORD* in/out -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "credui.dll"
#cfunc global CredPackAuthenticationBufferW "CredPackAuthenticationBufferW" int, wstr, wstr, var, var
; res = CredPackAuthenticationBufferW(dwFlags, pszUserName, pszPassword, pPackedCredentials, pcbPackedCredentials)
; dwFlags : CRED_PACK_FLAGS -> "int"
; pszUserName : LPWSTR -> "wstr"
; pszPassword : LPWSTR -> "wstr"
; pPackedCredentials : BYTE* optional, out -> "var"
; pcbPackedCredentials : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL CredPackAuthenticationBufferW(CRED_PACK_FLAGS dwFlags, LPWSTR pszUserName, LPWSTR pszPassword, BYTE* pPackedCredentials, DWORD* pcbPackedCredentials)
#uselib "credui.dll"
#cfunc global CredPackAuthenticationBufferW "CredPackAuthenticationBufferW" int, wstr, wstr, var, var
; res = CredPackAuthenticationBufferW(dwFlags, pszUserName, pszPassword, pPackedCredentials, pcbPackedCredentials)
; dwFlags : CRED_PACK_FLAGS -> "int"
; pszUserName : LPWSTR -> "wstr"
; pszPassword : LPWSTR -> "wstr"
; pPackedCredentials : BYTE* optional, out -> "var"
; pcbPackedCredentials : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	credui = windows.NewLazySystemDLL("credui.dll")
	procCredPackAuthenticationBufferW = credui.NewProc("CredPackAuthenticationBufferW")
)

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