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

CredUnprotectW

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

シグネチャ

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

BOOL CredUnprotectW(
    BOOL fAsSelf,
    LPWSTR pszProtectedCredentials,
    DWORD cchProtectedCredentials,
    LPWSTR pszCredentials,   // optional
    DWORD* pcchMaxChars
);

パラメーター

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

戻り値の型: BOOL

公式ドキュメント

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

戻り値

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

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

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

解説(Remarks)

メモ

wincred.h ヘッダーは CredUnprotect をエイリアスとして定義しており、UNICODE プリプロセッサ定数の定義に基づいて、この関数の ANSI 版または Unicode 版を自動的に選択します。エンコーディング非依存のエイリアスの使用を、エンコーディング非依存でないコードと混在させると、コンパイルエラーや実行時エラーを引き起こす不整合が生じる可能性があります。詳細については、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 CredUnprotectW(
    BOOL fAsSelf,
    LPWSTR pszProtectedCredentials,
    DWORD cchProtectedCredentials,
    LPWSTR pszCredentials,   // optional
    DWORD* pcchMaxChars
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("ADVAPI32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern bool CredUnprotectW(
    bool fAsSelf,   // BOOL
    [MarshalAs(UnmanagedType.LPWStr)] string pszProtectedCredentials,   // LPWSTR
    uint cchProtectedCredentials,   // DWORD
    [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszCredentials,   // LPWSTR optional, out
    ref uint pcchMaxChars   // DWORD* in/out
);
<DllImport("ADVAPI32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function CredUnprotectW(
    fAsSelf As Boolean,   ' BOOL
    <MarshalAs(UnmanagedType.LPWStr)> pszProtectedCredentials As String,   ' LPWSTR
    cchProtectedCredentials As UInteger,   ' DWORD
    <MarshalAs(UnmanagedType.LPWStr)> pszCredentials As System.Text.StringBuilder,   ' LPWSTR optional, out
    ByRef pcchMaxChars As UInteger   ' DWORD* in/out
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' fAsSelf : BOOL
' pszProtectedCredentials : LPWSTR
' cchProtectedCredentials : DWORD
' pszCredentials : LPWSTR optional, out
' pcchMaxChars : DWORD* in/out
Declare PtrSafe Function CredUnprotectW Lib "advapi32" ( _
    ByVal fAsSelf As Long, _
    ByVal pszProtectedCredentials As LongPtr, _
    ByVal cchProtectedCredentials As Long, _
    ByVal pszCredentials As LongPtr, _
    ByRef pcchMaxChars 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

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

var (
	advapi32 = windows.NewLazySystemDLL("ADVAPI32.dll")
	procCredUnprotectW = advapi32.NewProc("CredUnprotectW")
)

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

extern "advapi32" fn CredUnprotectW(
    fAsSelf: i32, // BOOL
    pszProtectedCredentials: [*c]const u16, // LPWSTR
    cchProtectedCredentials: u32, // DWORD
    pszCredentials: [*c]u16, // LPWSTR optional, out
    pcchMaxChars: [*c]u32 // DWORD* in/out
) callconv(std.os.windows.WINAPI) i32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。
proc CredUnprotectW(
    fAsSelf: int32,  # BOOL
    pszProtectedCredentials: WideCString,  # LPWSTR
    cchProtectedCredentials: uint32,  # DWORD
    pszCredentials: ptr uint16,  # LPWSTR optional, out
    pcchMaxChars: ptr uint32  # DWORD* in/out
): int32 {.importc: "CredUnprotectW", stdcall, dynlib: "ADVAPI32.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "advapi32");
extern(Windows)
int CredUnprotectW(
    int fAsSelf,   // BOOL
    const(wchar)* pszProtectedCredentials,   // LPWSTR
    uint cchProtectedCredentials,   // DWORD
    wchar* pszCredentials,   // LPWSTR optional, out
    uint* pcchMaxChars   // DWORD* in/out
);
ccall((:CredUnprotectW, "ADVAPI32.dll"), stdcall, Int32,
      (Int32, Cwstring, UInt32, Ptr{UInt16}, Ptr{UInt32}),
      fAsSelf, pszProtectedCredentials, cchProtectedCredentials, pszCredentials, pcchMaxChars)
# fAsSelf : BOOL -> Int32
# pszProtectedCredentials : LPWSTR -> Cwstring
# cchProtectedCredentials : DWORD -> UInt32
# pszCredentials : LPWSTR optional, out -> Ptr{UInt16}
# pcchMaxChars : DWORD* in/out -> Ptr{UInt32}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。
local ffi = require("ffi")
ffi.cdef[[
int32_t CredUnprotectW(
    int32_t fAsSelf,
    const uint16_t* pszProtectedCredentials,
    uint32_t cchProtectedCredentials,
    uint16_t* pszCredentials,
    uint32_t* pcchMaxChars);
]]
local advapi32 = ffi.load("advapi32")
-- advapi32.CredUnprotectW(fAsSelf, pszProtectedCredentials, cchProtectedCredentials, pszCredentials, pcchMaxChars)
-- fAsSelf : BOOL
-- pszProtectedCredentials : LPWSTR
-- cchProtectedCredentials : DWORD
-- pszCredentials : LPWSTR optional, out
-- pcchMaxChars : DWORD* in/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 CredUnprotectW = lib.func('__stdcall', 'CredUnprotectW', 'int32_t', ['int32_t', 'str16', 'uint32_t', 'uint16_t *', 'uint32_t *']);
// CredUnprotectW(fAsSelf, pszProtectedCredentials, cchProtectedCredentials, pszCredentials, pcchMaxChars)
// fAsSelf : BOOL -> 'int32_t'
// pszProtectedCredentials : LPWSTR -> 'str16'
// cchProtectedCredentials : DWORD -> 'uint32_t'
// pszCredentials : LPWSTR optional, out -> 'uint16_t *'
// pcchMaxChars : DWORD* in/out -> 'uint32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("ADVAPI32.dll", {
  CredUnprotectW: { parameters: ["i32", "buffer", "u32", "buffer", "pointer"], result: "i32" },
});
// lib.symbols.CredUnprotectW(fAsSelf, pszProtectedCredentials, cchProtectedCredentials, pszCredentials, pcchMaxChars)
// fAsSelf : BOOL -> "i32"
// pszProtectedCredentials : LPWSTR -> "buffer"
// cchProtectedCredentials : DWORD -> "u32"
// pszCredentials : LPWSTR optional, out -> "buffer"
// pcchMaxChars : DWORD* in/out -> "pointer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t CredUnprotectW(
    int32_t fAsSelf,
    const uint16_t* pszProtectedCredentials,
    uint32_t cchProtectedCredentials,
    uint16_t* pszCredentials,
    uint32_t* pcchMaxChars);
C, "ADVAPI32.dll");
// $ffi->CredUnprotectW(fAsSelf, pszProtectedCredentials, cchProtectedCredentials, pszCredentials, pcchMaxChars);
// fAsSelf : BOOL
// pszProtectedCredentials : LPWSTR
// cchProtectedCredentials : DWORD
// pszCredentials : LPWSTR 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.UNICODE_OPTIONS);
    boolean CredUnprotectW(
        boolean fAsSelf,   // BOOL
        WString pszProtectedCredentials,   // LPWSTR
        int cchProtectedCredentials,   // DWORD
        char[] pszCredentials,   // LPWSTR optional, out
        IntByReference pcchMaxChars   // DWORD* in/out
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("advapi32")]
lib LibADVAPI32
  fun CredUnprotectW = CredUnprotectW(
    fAsSelf : Int32,   # BOOL
    pszProtectedCredentials : UInt16*,   # LPWSTR
    cchProtectedCredentials : UInt32,   # DWORD
    pszCredentials : UInt16*,   # LPWSTR 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 CredUnprotectWNative = Int32 Function(Int32, Pointer<Utf16>, Uint32, Pointer<Utf16>, Pointer<Uint32>);
typedef CredUnprotectWDart = int Function(int, Pointer<Utf16>, int, Pointer<Utf16>, Pointer<Uint32>);
final CredUnprotectW = DynamicLibrary.open('ADVAPI32.dll')
    .lookupFunction<CredUnprotectWNative, CredUnprotectWDart>('CredUnprotectW');
// fAsSelf : BOOL -> Int32
// pszProtectedCredentials : LPWSTR -> Pointer<Utf16>
// cchProtectedCredentials : DWORD -> Uint32
// pszCredentials : LPWSTR optional, out -> Pointer<Utf16>
// pcchMaxChars : DWORD* in/out -> Pointer<Uint32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function CredUnprotectW(
  fAsSelf: BOOL;   // BOOL
  pszProtectedCredentials: PWideChar;   // LPWSTR
  cchProtectedCredentials: DWORD;   // DWORD
  pszCredentials: PWideChar;   // LPWSTR optional, out
  pcchMaxChars: Pointer   // DWORD* in/out
): BOOL; stdcall;
  external 'ADVAPI32.dll' name 'CredUnprotectW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

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

let credunprotectw =
  foreign "CredUnprotectW"
    (int32_t @-> (ptr uint16_t) @-> uint32_t @-> (ptr uint16_t) @-> (ptr uint32_t) @-> returning int32_t)
(* fAsSelf : BOOL -> int32_t *)
(* pszProtectedCredentials : LPWSTR -> (ptr uint16_t) *)
(* cchProtectedCredentials : DWORD -> uint32_t *)
(* pszCredentials : LPWSTR optional, out -> (ptr uint16_t) *)
(* 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 ("CredUnprotectW" cred-unprotect-w :convention :stdcall) :int32
  (f-as-self :int32)   ; BOOL
  (psz-protected-credentials (:string :encoding :utf-16le))   ; LPWSTR
  (cch-protected-credentials :uint32)   ; DWORD
  (psz-credentials :pointer)   ; LPWSTR 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 $CredUnprotectW = Win32::API::More->new('ADVAPI32',
    'BOOL CredUnprotectW(BOOL fAsSelf, LPCWSTR pszProtectedCredentials, DWORD cchProtectedCredentials, LPWSTR pszCredentials, LPVOID pcchMaxChars)');
# my $ret = $CredUnprotectW->Call($fAsSelf, $pszProtectedCredentials, $cchProtectedCredentials, $pszCredentials, $pcchMaxChars);
# fAsSelf : BOOL -> BOOL
# pszProtectedCredentials : LPWSTR -> LPCWSTR
# cchProtectedCredentials : DWORD -> DWORD
# pszCredentials : LPWSTR optional, out -> LPWSTR
# pcchMaxChars : DWORD* in/out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

文字セット違い