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

CredProtectA

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

シグネチャ

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

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

パラメーター

名前方向
fAsSelfBOOLin
pszCredentialsLPSTRin
cchCredentialsDWORDin
pszProtectedCredentialsLPSTRout
pcchMaxCharsDWORD*inout
ProtectionTypeCRED_PROTECTION_TYPE*outoptional

戻り値の型: BOOL

各言語での呼び出し定義

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

BOOL CredProtectA(
    BOOL fAsSelf,
    LPSTR pszCredentials,
    DWORD cchCredentials,
    LPSTR pszProtectedCredentials,
    DWORD* pcchMaxChars,
    CRED_PROTECTION_TYPE* ProtectionType   // optional
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("ADVAPI32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
static extern bool CredProtectA(
    bool fAsSelf,   // BOOL
    [MarshalAs(UnmanagedType.LPStr)] string pszCredentials,   // LPSTR
    uint cchCredentials,   // DWORD
    [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder pszProtectedCredentials,   // LPSTR out
    ref uint pcchMaxChars,   // DWORD* in/out
    IntPtr ProtectionType   // CRED_PROTECTION_TYPE* optional, out
);
<DllImport("ADVAPI32.dll", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function CredProtectA(
    fAsSelf As Boolean,   ' BOOL
    <MarshalAs(UnmanagedType.LPStr)> pszCredentials As String,   ' LPSTR
    cchCredentials As UInteger,   ' DWORD
    <MarshalAs(UnmanagedType.LPStr)> pszProtectedCredentials As System.Text.StringBuilder,   ' LPSTR out
    ByRef pcchMaxChars As UInteger,   ' DWORD* in/out
    ProtectionType As IntPtr   ' CRED_PROTECTION_TYPE* optional, out
) As Boolean
End Function
' fAsSelf : BOOL
' pszCredentials : LPSTR
' cchCredentials : DWORD
' pszProtectedCredentials : LPSTR out
' pcchMaxChars : DWORD* in/out
' ProtectionType : CRED_PROTECTION_TYPE* optional, out
Declare PtrSafe Function CredProtectA Lib "advapi32" ( _
    ByVal fAsSelf As Long, _
    ByVal pszCredentials As String, _
    ByVal cchCredentials As Long, _
    ByVal pszProtectedCredentials As String, _
    ByRef pcchMaxChars As Long, _
    ByVal ProtectionType As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

CredProtectA = ctypes.windll.advapi32.CredProtectA
CredProtectA.restype = wintypes.BOOL
CredProtectA.argtypes = [
    wintypes.BOOL,  # fAsSelf : BOOL
    wintypes.LPCSTR,  # pszCredentials : LPSTR
    wintypes.DWORD,  # cchCredentials : DWORD
    wintypes.LPSTR,  # pszProtectedCredentials : LPSTR 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')
CredProtectA = Fiddle::Function.new(
  lib['CredProtectA'],
  [
    Fiddle::TYPE_INT,  # fAsSelf : BOOL
    Fiddle::TYPE_VOIDP,  # pszCredentials : LPSTR
    -Fiddle::TYPE_INT,  # cchCredentials : DWORD
    Fiddle::TYPE_VOIDP,  # pszProtectedCredentials : LPSTR out
    Fiddle::TYPE_VOIDP,  # pcchMaxChars : DWORD* in/out
    Fiddle::TYPE_VOIDP,  # ProtectionType : CRED_PROTECTION_TYPE* optional, out
  ],
  Fiddle::TYPE_INT)
#[link(name = "advapi32")]
extern "system" {
    fn CredProtectA(
        fAsSelf: i32,  // BOOL
        pszCredentials: *mut u8,  // LPSTR
        cchCredentials: u32,  // DWORD
        pszProtectedCredentials: *mut u8,  // LPSTR 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.Ansi, SetLastError = true)]
public static extern bool CredProtectA(bool fAsSelf, [MarshalAs(UnmanagedType.LPStr)] string pszCredentials, uint cchCredentials, [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder pszProtectedCredentials, ref uint pcchMaxChars, IntPtr ProtectionType);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ADVAPI32_CredProtectA' -Namespace Win32 -PassThru
# $api::CredProtectA(fAsSelf, pszCredentials, cchCredentials, pszProtectedCredentials, pcchMaxChars, ProtectionType)
#uselib "ADVAPI32.dll"
#func global CredProtectA "CredProtectA" sptr, sptr, sptr, sptr, sptr, sptr
; CredProtectA fAsSelf, pszCredentials, cchCredentials, varptr(pszProtectedCredentials), varptr(pcchMaxChars), ProtectionType   ; 戻り値は stat
; fAsSelf : BOOL -> "sptr"
; pszCredentials : LPSTR -> "sptr"
; cchCredentials : DWORD -> "sptr"
; pszProtectedCredentials : LPSTR out -> "sptr"
; pcchMaxChars : DWORD* in/out -> "sptr"
; ProtectionType : CRED_PROTECTION_TYPE* optional, out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "ADVAPI32.dll"
#cfunc global CredProtectA "CredProtectA" int, str, int, var, var, int
; res = CredProtectA(fAsSelf, pszCredentials, cchCredentials, pszProtectedCredentials, pcchMaxChars, ProtectionType)
; fAsSelf : BOOL -> "int"
; pszCredentials : LPSTR -> "str"
; cchCredentials : DWORD -> "int"
; pszProtectedCredentials : LPSTR out -> "var"
; pcchMaxChars : DWORD* in/out -> "var"
; ProtectionType : CRED_PROTECTION_TYPE* optional, out -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL CredProtectA(BOOL fAsSelf, LPSTR pszCredentials, DWORD cchCredentials, LPSTR pszProtectedCredentials, DWORD* pcchMaxChars, CRED_PROTECTION_TYPE* ProtectionType)
#uselib "ADVAPI32.dll"
#cfunc global CredProtectA "CredProtectA" int, str, int, var, var, int
; res = CredProtectA(fAsSelf, pszCredentials, cchCredentials, pszProtectedCredentials, pcchMaxChars, ProtectionType)
; fAsSelf : BOOL -> "int"
; pszCredentials : LPSTR -> "str"
; cchCredentials : DWORD -> "int"
; pszProtectedCredentials : LPSTR 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")
	procCredProtectA = advapi32.NewProc("CredProtectA")
)

// fAsSelf (BOOL), pszCredentials (LPSTR), cchCredentials (DWORD), pszProtectedCredentials (LPSTR out), pcchMaxChars (DWORD* in/out), ProtectionType (CRED_PROTECTION_TYPE* optional, out)
r1, _, err := procCredProtectA.Call(
	uintptr(fAsSelf),
	uintptr(unsafe.Pointer(windows.BytePtrFromString(pszCredentials))),
	uintptr(cchCredentials),
	uintptr(pszProtectedCredentials),
	uintptr(pcchMaxChars),
	uintptr(ProtectionType),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function CredProtectA(
  fAsSelf: BOOL;   // BOOL
  pszCredentials: PAnsiChar;   // LPSTR
  cchCredentials: DWORD;   // DWORD
  pszProtectedCredentials: PAnsiChar;   // LPSTR out
  pcchMaxChars: Pointer;   // DWORD* in/out
  ProtectionType: Pointer   // CRED_PROTECTION_TYPE* optional, out
): BOOL; stdcall;
  external 'ADVAPI32.dll' name 'CredProtectA';
result := DllCall("ADVAPI32\CredProtectA"
    , "Int", fAsSelf   ; BOOL
    , "AStr", pszCredentials   ; LPSTR
    , "UInt", cchCredentials   ; DWORD
    , "Ptr", pszProtectedCredentials   ; LPSTR out
    , "Ptr", pcchMaxChars   ; DWORD* in/out
    , "Ptr", ProtectionType   ; CRED_PROTECTION_TYPE* optional, out
    , "Int")   ; return: BOOL
●CredProtectA(fAsSelf, pszCredentials, cchCredentials, pszProtectedCredentials, pcchMaxChars, ProtectionType) = DLL("ADVAPI32.dll", "bool CredProtectA(bool, char*, dword, char*, void*, void*)")
# 呼び出し: CredProtectA(fAsSelf, pszCredentials, cchCredentials, pszProtectedCredentials, pcchMaxChars, ProtectionType)
# fAsSelf : BOOL -> "bool"
# pszCredentials : LPSTR -> "char*"
# cchCredentials : DWORD -> "dword"
# pszProtectedCredentials : LPSTR out -> "char*"
# pcchMaxChars : DWORD* in/out -> "void*"
# ProtectionType : CRED_PROTECTION_TYPE* optional, out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。