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

CredProtectW

関数
資格情報文字列を暗号化して保護する。
DLLADVAPI32.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり対応OSWindows Vista 以降

シグネチャ

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

BOOL CredProtectW(
    BOOL fAsSelf,
    LPWSTR pszCredentials,
    DWORD cchCredentials,
    LPWSTR pszProtectedCredentials,
    DWORD* pcchMaxChars,
    CRED_PROTECTION_TYPE* ProtectionType   // optional
);

パラメーター

名前方向説明
fAsSelfBOOLin現在のプロセスのセキュリティコンテキストで資格情報を暗号化することを指定する場合は TRUE を設定します。呼び出し元スレッドのセキュリティコンテキストで資格情報を暗号化することを指定する場合は FALSE を設定します。
pszCredentialsLPWSTRin暗号化する資格情報を指定する文字列へのポインター。この関数は、cchCredentials パラメーターで指定された文字数を暗号化します。
cchCredentialsDWORDinpszCredentials バッファーのサイズ(文字単位)。
pszProtectedCredentialsLPWSTRout出力時に、暗号化された資格情報を受け取る文字列へのポインター。
pcchMaxCharsDWORD*inoutpszProtectedCredentials バッファーのサイズ(文字単位)。出力時に、pszProtectedCredentials が暗号化された資格情報を受け取るのに十分なサイズでない場合、このパラメーターには pszProtectedCredentials バッファーに必要なサイズ(文字単位)が設定されます。
ProtectionTypeCRED_PROTECTION_TYPE*outoptional出力時に、提供される保護の種類を指定する CRED_PROTECTION_TYPE 列挙型へのポインター。

戻り値の型: BOOL

公式ドキュメント

指定された資格情報を、現在のセキュリティコンテキストでのみ復号できるように暗号化します。(Unicode)

戻り値

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

拡張エラー情報を取得するには、 GetLastError 関数を呼び出します。

解説(Remarks)

CredProtect 関数の出力は整合性が保護されないため、出力が変更された場合、CredUnprotect 関数には反映されず、正しくない結果が生成される可能性があることに注意してください。

メモ

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

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

各言語での呼び出し定義

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

BOOL CredProtectW(
    BOOL fAsSelf,
    LPWSTR pszCredentials,
    DWORD cchCredentials,
    LPWSTR pszProtectedCredentials,
    DWORD* pcchMaxChars,
    CRED_PROTECTION_TYPE* ProtectionType   // optional
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("ADVAPI32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern bool CredProtectW(
    bool fAsSelf,   // BOOL
    [MarshalAs(UnmanagedType.LPWStr)] string pszCredentials,   // LPWSTR
    uint cchCredentials,   // DWORD
    [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszProtectedCredentials,   // LPWSTR out
    ref uint pcchMaxChars,   // DWORD* in/out
    IntPtr ProtectionType   // CRED_PROTECTION_TYPE* optional, out
);
<DllImport("ADVAPI32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function CredProtectW(
    fAsSelf As Boolean,   ' BOOL
    <MarshalAs(UnmanagedType.LPWStr)> pszCredentials As String,   ' LPWSTR
    cchCredentials As UInteger,   ' DWORD
    <MarshalAs(UnmanagedType.LPWStr)> pszProtectedCredentials As System.Text.StringBuilder,   ' LPWSTR out
    ByRef pcchMaxChars As UInteger,   ' DWORD* in/out
    ProtectionType As IntPtr   ' CRED_PROTECTION_TYPE* optional, out
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' fAsSelf : BOOL
' pszCredentials : LPWSTR
' cchCredentials : DWORD
' pszProtectedCredentials : LPWSTR out
' pcchMaxChars : DWORD* in/out
' ProtectionType : CRED_PROTECTION_TYPE* optional, out
Declare PtrSafe Function CredProtectW Lib "advapi32" ( _
    ByVal fAsSelf As Long, _
    ByVal pszCredentials As LongPtr, _
    ByVal cchCredentials As Long, _
    ByVal pszProtectedCredentials As LongPtr, _
    ByRef pcchMaxChars As Long, _
    ByVal ProtectionType 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

CredProtectW = ctypes.windll.advapi32.CredProtectW
CredProtectW.restype = wintypes.BOOL
CredProtectW.argtypes = [
    wintypes.BOOL,  # fAsSelf : BOOL
    wintypes.LPCWSTR,  # pszCredentials : LPWSTR
    wintypes.DWORD,  # cchCredentials : DWORD
    wintypes.LPWSTR,  # pszProtectedCredentials : LPWSTR out
    ctypes.POINTER(wintypes.DWORD),  # pcchMaxChars : DWORD* in/out
    ctypes.c_void_p,  # ProtectionType : CRED_PROTECTION_TYPE* optional, out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

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

var (
	advapi32 = windows.NewLazySystemDLL("ADVAPI32.dll")
	procCredProtectW = advapi32.NewProc("CredProtectW")
)

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

extern "advapi32" fn CredProtectW(
    fAsSelf: i32, // BOOL
    pszCredentials: [*c]const u16, // LPWSTR
    cchCredentials: u32, // DWORD
    pszProtectedCredentials: [*c]u16, // LPWSTR out
    pcchMaxChars: [*c]u32, // DWORD* in/out
    ProtectionType: [*c]i32 // CRED_PROTECTION_TYPE* optional, out
) callconv(std.os.windows.WINAPI) i32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。
proc CredProtectW(
    fAsSelf: int32,  # BOOL
    pszCredentials: WideCString,  # LPWSTR
    cchCredentials: uint32,  # DWORD
    pszProtectedCredentials: ptr uint16,  # LPWSTR out
    pcchMaxChars: ptr uint32,  # DWORD* in/out
    ProtectionType: ptr int32  # CRED_PROTECTION_TYPE* optional, out
): int32 {.importc: "CredProtectW", stdcall, dynlib: "ADVAPI32.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "advapi32");
extern(Windows)
int CredProtectW(
    int fAsSelf,   // BOOL
    const(wchar)* pszCredentials,   // LPWSTR
    uint cchCredentials,   // DWORD
    wchar* pszProtectedCredentials,   // LPWSTR out
    uint* pcchMaxChars,   // DWORD* in/out
    int* ProtectionType   // CRED_PROTECTION_TYPE* optional, out
);
ccall((:CredProtectW, "ADVAPI32.dll"), stdcall, Int32,
      (Int32, Cwstring, UInt32, Ptr{UInt16}, Ptr{UInt32}, Ptr{Int32}),
      fAsSelf, pszCredentials, cchCredentials, pszProtectedCredentials, pcchMaxChars, ProtectionType)
# fAsSelf : BOOL -> Int32
# pszCredentials : LPWSTR -> Cwstring
# cchCredentials : DWORD -> UInt32
# pszProtectedCredentials : LPWSTR out -> Ptr{UInt16}
# pcchMaxChars : DWORD* in/out -> Ptr{UInt32}
# ProtectionType : CRED_PROTECTION_TYPE* optional, out -> Ptr{Int32}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。
local ffi = require("ffi")
ffi.cdef[[
int32_t CredProtectW(
    int32_t fAsSelf,
    const uint16_t* pszCredentials,
    uint32_t cchCredentials,
    uint16_t* pszProtectedCredentials,
    uint32_t* pcchMaxChars,
    int32_t* ProtectionType);
]]
local advapi32 = ffi.load("advapi32")
-- advapi32.CredProtectW(fAsSelf, pszCredentials, cchCredentials, pszProtectedCredentials, pcchMaxChars, ProtectionType)
-- fAsSelf : BOOL
-- pszCredentials : LPWSTR
-- cchCredentials : DWORD
-- pszProtectedCredentials : LPWSTR out
-- pcchMaxChars : DWORD* in/out
-- ProtectionType : CRED_PROTECTION_TYPE* optional, 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('ADVAPI32.dll');
const CredProtectW = lib.func('__stdcall', 'CredProtectW', 'int32_t', ['int32_t', 'str16', 'uint32_t', 'uint16_t *', 'uint32_t *', 'int32_t *']);
// CredProtectW(fAsSelf, pszCredentials, cchCredentials, pszProtectedCredentials, pcchMaxChars, ProtectionType)
// fAsSelf : BOOL -> 'int32_t'
// pszCredentials : LPWSTR -> 'str16'
// cchCredentials : DWORD -> 'uint32_t'
// pszProtectedCredentials : LPWSTR out -> 'uint16_t *'
// pcchMaxChars : DWORD* in/out -> 'uint32_t *'
// ProtectionType : CRED_PROTECTION_TYPE* optional, out -> 'int32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("ADVAPI32.dll", {
  CredProtectW: { parameters: ["i32", "buffer", "u32", "buffer", "pointer", "pointer"], result: "i32" },
});
// lib.symbols.CredProtectW(fAsSelf, pszCredentials, cchCredentials, pszProtectedCredentials, pcchMaxChars, ProtectionType)
// fAsSelf : BOOL -> "i32"
// pszCredentials : LPWSTR -> "buffer"
// cchCredentials : DWORD -> "u32"
// pszProtectedCredentials : LPWSTR out -> "buffer"
// pcchMaxChars : DWORD* in/out -> "pointer"
// ProtectionType : CRED_PROTECTION_TYPE* optional, out -> "pointer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t CredProtectW(
    int32_t fAsSelf,
    const uint16_t* pszCredentials,
    uint32_t cchCredentials,
    uint16_t* pszProtectedCredentials,
    uint32_t* pcchMaxChars,
    int32_t* ProtectionType);
C, "ADVAPI32.dll");
// $ffi->CredProtectW(fAsSelf, pszCredentials, cchCredentials, pszProtectedCredentials, pcchMaxChars, ProtectionType);
// fAsSelf : BOOL
// pszCredentials : LPWSTR
// cchCredentials : DWORD
// pszProtectedCredentials : LPWSTR out
// pcchMaxChars : DWORD* in/out
// ProtectionType : CRED_PROTECTION_TYPE* optional, 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 Advapi32 extends StdCallLibrary {
    Advapi32 INSTANCE = Native.load("advapi32", Advapi32.class, W32APIOptions.UNICODE_OPTIONS);
    boolean CredProtectW(
        boolean fAsSelf,   // BOOL
        WString pszCredentials,   // LPWSTR
        int cchCredentials,   // DWORD
        char[] pszProtectedCredentials,   // LPWSTR out
        IntByReference pcchMaxChars,   // DWORD* in/out
        IntByReference ProtectionType   // CRED_PROTECTION_TYPE* optional, out
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("advapi32")]
lib LibADVAPI32
  fun CredProtectW = CredProtectW(
    fAsSelf : Int32,   # BOOL
    pszCredentials : UInt16*,   # LPWSTR
    cchCredentials : UInt32,   # DWORD
    pszProtectedCredentials : UInt16*,   # LPWSTR out
    pcchMaxChars : UInt32*,   # DWORD* in/out
    ProtectionType : Int32*   # CRED_PROTECTION_TYPE* optional, out
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef CredProtectWNative = Int32 Function(Int32, Pointer<Utf16>, Uint32, Pointer<Utf16>, Pointer<Uint32>, Pointer<Int32>);
typedef CredProtectWDart = int Function(int, Pointer<Utf16>, int, Pointer<Utf16>, Pointer<Uint32>, Pointer<Int32>);
final CredProtectW = DynamicLibrary.open('ADVAPI32.dll')
    .lookupFunction<CredProtectWNative, CredProtectWDart>('CredProtectW');
// fAsSelf : BOOL -> Int32
// pszCredentials : LPWSTR -> Pointer<Utf16>
// cchCredentials : DWORD -> Uint32
// pszProtectedCredentials : LPWSTR out -> Pointer<Utf16>
// pcchMaxChars : DWORD* in/out -> Pointer<Uint32>
// ProtectionType : CRED_PROTECTION_TYPE* optional, out -> Pointer<Int32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function CredProtectW(
  fAsSelf: BOOL;   // BOOL
  pszCredentials: PWideChar;   // LPWSTR
  cchCredentials: DWORD;   // DWORD
  pszProtectedCredentials: PWideChar;   // LPWSTR out
  pcchMaxChars: Pointer;   // DWORD* in/out
  ProtectionType: Pointer   // CRED_PROTECTION_TYPE* optional, out
): BOOL; stdcall;
  external 'ADVAPI32.dll' name 'CredProtectW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "CredProtectW"
  c_CredProtectW :: CInt -> CWString -> Word32 -> CWString -> Ptr Word32 -> Ptr Int32 -> IO CInt
-- fAsSelf : BOOL -> CInt
-- pszCredentials : LPWSTR -> CWString
-- cchCredentials : DWORD -> Word32
-- pszProtectedCredentials : LPWSTR out -> CWString
-- pcchMaxChars : DWORD* in/out -> Ptr Word32
-- ProtectionType : CRED_PROTECTION_TYPE* optional, out -> Ptr Int32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let credprotectw =
  foreign "CredProtectW"
    (int32_t @-> (ptr uint16_t) @-> uint32_t @-> (ptr uint16_t) @-> (ptr uint32_t) @-> (ptr int32_t) @-> returning int32_t)
(* fAsSelf : BOOL -> int32_t *)
(* pszCredentials : LPWSTR -> (ptr uint16_t) *)
(* cchCredentials : DWORD -> uint32_t *)
(* pszProtectedCredentials : LPWSTR out -> (ptr uint16_t) *)
(* pcchMaxChars : DWORD* in/out -> (ptr uint32_t) *)
(* ProtectionType : CRED_PROTECTION_TYPE* optional, out -> (ptr int32_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library advapi32 (t "ADVAPI32.dll"))
(cffi:use-foreign-library advapi32)

(cffi:defcfun ("CredProtectW" cred-protect-w :convention :stdcall) :int32
  (f-as-self :int32)   ; BOOL
  (psz-credentials (:string :encoding :utf-16le))   ; LPWSTR
  (cch-credentials :uint32)   ; DWORD
  (psz-protected-credentials :pointer)   ; LPWSTR out
  (pcch-max-chars :pointer)   ; DWORD* in/out
  (protection-type :pointer))   ; CRED_PROTECTION_TYPE* optional, out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $CredProtectW = Win32::API::More->new('ADVAPI32',
    'BOOL CredProtectW(BOOL fAsSelf, LPCWSTR pszCredentials, DWORD cchCredentials, LPWSTR pszProtectedCredentials, LPVOID pcchMaxChars, LPVOID ProtectionType)');
# my $ret = $CredProtectW->Call($fAsSelf, $pszCredentials, $cchCredentials, $pszProtectedCredentials, $pcchMaxChars, $ProtectionType);
# fAsSelf : BOOL -> BOOL
# pszCredentials : LPWSTR -> LPCWSTR
# cchCredentials : DWORD -> DWORD
# pszProtectedCredentials : LPWSTR out -> LPWSTR
# pcchMaxChars : DWORD* in/out -> LPVOID
# ProtectionType : CRED_PROTECTION_TYPE* optional, out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

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