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
pszCredentialsLPWSTRin
cchCredentialsDWORDin
pszProtectedCredentialsLPWSTRout
pcchMaxCharsDWORD*inout
ProtectionTypeCRED_PROTECTION_TYPE*outoptional

戻り値の型: BOOL

各言語での呼び出し定義

// 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 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), 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, int
; 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 -> "int"
; ※出力/バッファ引数は 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, int
; 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 -> "int"
; ※出力/バッファ引数は 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 版の利用を推奨。