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

CredUnprotectA

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

シグネチャ

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

BOOL CredUnprotectA(
    BOOL fAsSelf,
    LPSTR pszProtectedCredentials,
    DWORD cchProtectedCredentials,
    LPSTR pszCredentials,   // optional
    DWORD* pcchMaxChars
);

パラメーター

名前方向説明
fAsSelfBOOLin現在のプロセスのセキュリティコンテキストで資格情報が暗号化されたことを指定する場合は TRUE を設定します。呼び出し元スレッドのセキュリティコンテキストで資格情報が暗号化されたことを指定する場合は FALSE を設定します。
pszProtectedCredentialsLPSTRin暗号化された資格情報を指定する文字列へのポインターです。
cchProtectedCredentialsDWORDinpszProtectedCredentials バッファーのサイズ(文字単位)です。
pszCredentialsLPSTRoutoptional出力時に、復号された資格情報を受け取る文字列へのポインターです。
pcchMaxCharsDWORD*inoutpszCredentials バッファーのサイズ(文字単位)です。出力時に、pszCredentials が暗号化された資格情報を受け取るのに十分なサイズでない場合、このパラメーターには pszCredentials バッファーに必要なサイズ(文字単位)が設定されます。

戻り値の型: BOOL

公式ドキュメント

CredProtect 関数によって以前に暗号化された資格情報を復号します。(ANSI)

戻り値

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

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

戻り値/値 説明
ERROR_NOT_CAPABLE
資格情報の暗号化に使用されたセキュリティコンテキストが、復号に使用されたセキュリティコンテキストと異なります。
ERROR_INSUFFICIENT_BUFFER
pszCredentials バッファーのサイズが不十分でした。

解説(Remarks)

メモ

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

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

各言語での呼び出し定義

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

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

CredUnprotectA = ctypes.windll.advapi32.CredUnprotectA
CredUnprotectA.restype = wintypes.BOOL
CredUnprotectA.argtypes = [
    wintypes.BOOL,  # fAsSelf : BOOL
    wintypes.LPCSTR,  # pszProtectedCredentials : LPSTR
    wintypes.DWORD,  # cchProtectedCredentials : DWORD
    wintypes.LPSTR,  # pszCredentials : LPSTR optional, out
    ctypes.POINTER(wintypes.DWORD),  # pcchMaxChars : DWORD* in/out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

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

var (
	advapi32 = windows.NewLazySystemDLL("ADVAPI32.dll")
	procCredUnprotectA = advapi32.NewProc("CredUnprotectA")
)

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

extern "advapi32" fn CredUnprotectA(
    fAsSelf: i32, // BOOL
    pszProtectedCredentials: [*c]const u8, // LPSTR
    cchProtectedCredentials: u32, // DWORD
    pszCredentials: [*c]u8, // LPSTR optional, out
    pcchMaxChars: [*c]u32 // DWORD* in/out
) callconv(std.os.windows.WINAPI) i32;
proc CredUnprotectA(
    fAsSelf: int32,  # BOOL
    pszProtectedCredentials: cstring,  # LPSTR
    cchProtectedCredentials: uint32,  # DWORD
    pszCredentials: ptr char,  # LPSTR optional, out
    pcchMaxChars: ptr uint32  # DWORD* in/out
): int32 {.importc: "CredUnprotectA", stdcall, dynlib: "ADVAPI32.dll".}
pragma(lib, "advapi32");
extern(Windows)
int CredUnprotectA(
    int fAsSelf,   // BOOL
    const(char)* pszProtectedCredentials,   // LPSTR
    uint cchProtectedCredentials,   // DWORD
    char* pszCredentials,   // LPSTR optional, out
    uint* pcchMaxChars   // DWORD* in/out
);
ccall((:CredUnprotectA, "ADVAPI32.dll"), stdcall, Int32,
      (Int32, Cstring, UInt32, Ptr{UInt8}, Ptr{UInt32}),
      fAsSelf, pszProtectedCredentials, cchProtectedCredentials, pszCredentials, pcchMaxChars)
# fAsSelf : BOOL -> Int32
# pszProtectedCredentials : LPSTR -> Cstring
# cchProtectedCredentials : DWORD -> UInt32
# pszCredentials : LPSTR optional, out -> Ptr{UInt8}
# pcchMaxChars : DWORD* in/out -> Ptr{UInt32}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t CredUnprotectA(
    int32_t fAsSelf,
    const char* pszProtectedCredentials,
    uint32_t cchProtectedCredentials,
    char* pszCredentials,
    uint32_t* pcchMaxChars);
]]
local advapi32 = ffi.load("advapi32")
-- advapi32.CredUnprotectA(fAsSelf, pszProtectedCredentials, cchProtectedCredentials, pszCredentials, pcchMaxChars)
-- fAsSelf : BOOL
-- pszProtectedCredentials : LPSTR
-- cchProtectedCredentials : DWORD
-- pszCredentials : LPSTR optional, out
-- pcchMaxChars : DWORD* in/out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('ADVAPI32.dll');
const CredUnprotectA = lib.func('__stdcall', 'CredUnprotectA', 'int32_t', ['int32_t', 'str', 'uint32_t', 'char *', 'uint32_t *']);
// CredUnprotectA(fAsSelf, pszProtectedCredentials, cchProtectedCredentials, pszCredentials, pcchMaxChars)
// fAsSelf : BOOL -> 'int32_t'
// pszProtectedCredentials : LPSTR -> 'str'
// cchProtectedCredentials : DWORD -> 'uint32_t'
// pszCredentials : LPSTR optional, out -> 'char *'
// pcchMaxChars : DWORD* in/out -> 'uint32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("ADVAPI32.dll", {
  CredUnprotectA: { parameters: ["i32", "buffer", "u32", "buffer", "pointer"], result: "i32" },
});
// lib.symbols.CredUnprotectA(fAsSelf, pszProtectedCredentials, cchProtectedCredentials, pszCredentials, pcchMaxChars)
// fAsSelf : BOOL -> "i32"
// pszProtectedCredentials : LPSTR -> "buffer"
// cchProtectedCredentials : DWORD -> "u32"
// pszCredentials : LPSTR optional, out -> "buffer"
// pcchMaxChars : DWORD* in/out -> "pointer"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t CredUnprotectA(
    int32_t fAsSelf,
    const char* pszProtectedCredentials,
    uint32_t cchProtectedCredentials,
    char* pszCredentials,
    uint32_t* pcchMaxChars);
C, "ADVAPI32.dll");
// $ffi->CredUnprotectA(fAsSelf, pszProtectedCredentials, cchProtectedCredentials, pszCredentials, pcchMaxChars);
// fAsSelf : BOOL
// pszProtectedCredentials : LPSTR
// cchProtectedCredentials : DWORD
// pszCredentials : LPSTR optional, out
// pcchMaxChars : 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 Advapi32 extends StdCallLibrary {
    Advapi32 INSTANCE = Native.load("advapi32", Advapi32.class, W32APIOptions.ASCII_OPTIONS);
    boolean CredUnprotectA(
        boolean fAsSelf,   // BOOL
        String pszProtectedCredentials,   // LPSTR
        int cchProtectedCredentials,   // DWORD
        byte[] pszCredentials,   // LPSTR optional, out
        IntByReference pcchMaxChars   // DWORD* in/out
    );
}
@[Link("advapi32")]
lib LibADVAPI32
  fun CredUnprotectA = CredUnprotectA(
    fAsSelf : Int32,   # BOOL
    pszProtectedCredentials : UInt8*,   # LPSTR
    cchProtectedCredentials : UInt32,   # DWORD
    pszCredentials : UInt8*,   # LPSTR optional, out
    pcchMaxChars : 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 CredUnprotectANative = Int32 Function(Int32, Pointer<Utf8>, Uint32, Pointer<Utf8>, Pointer<Uint32>);
typedef CredUnprotectADart = int Function(int, Pointer<Utf8>, int, Pointer<Utf8>, Pointer<Uint32>);
final CredUnprotectA = DynamicLibrary.open('ADVAPI32.dll')
    .lookupFunction<CredUnprotectANative, CredUnprotectADart>('CredUnprotectA');
// fAsSelf : BOOL -> Int32
// pszProtectedCredentials : LPSTR -> Pointer<Utf8>
// cchProtectedCredentials : DWORD -> Uint32
// pszCredentials : LPSTR optional, out -> Pointer<Utf8>
// pcchMaxChars : DWORD* in/out -> Pointer<Uint32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function CredUnprotectA(
  fAsSelf: BOOL;   // BOOL
  pszProtectedCredentials: PAnsiChar;   // LPSTR
  cchProtectedCredentials: DWORD;   // DWORD
  pszCredentials: PAnsiChar;   // LPSTR optional, out
  pcchMaxChars: Pointer   // DWORD* in/out
): BOOL; stdcall;
  external 'ADVAPI32.dll' name 'CredUnprotectA';
import Foreign
import Foreign.C.Types
import Foreign.C.String

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

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

(cffi:defcfun ("CredUnprotectA" cred-unprotect-a :convention :stdcall) :int32
  (f-as-self :int32)   ; BOOL
  (psz-protected-credentials :string)   ; LPSTR
  (cch-protected-credentials :uint32)   ; DWORD
  (psz-credentials :pointer)   ; LPSTR optional, out
  (pcch-max-chars :pointer))   ; DWORD* in/out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $CredUnprotectA = Win32::API::More->new('ADVAPI32',
    'BOOL CredUnprotectA(BOOL fAsSelf, LPCSTR pszProtectedCredentials, DWORD cchProtectedCredentials, LPSTR pszCredentials, LPVOID pcchMaxChars)');
# my $ret = $CredUnprotectA->Call($fAsSelf, $pszProtectedCredentials, $cchProtectedCredentials, $pszCredentials, $pcchMaxChars);
# fAsSelf : BOOL -> BOOL
# pszProtectedCredentials : LPSTR -> LPCSTR
# cchProtectedCredentials : DWORD -> DWORD
# pszCredentials : LPSTR optional, out -> LPSTR
# pcchMaxChars : DWORD* in/out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

文字セット違い