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

CredIsProtectedW

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

シグネチャ

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

BOOL CredIsProtectedW(
    LPWSTR pszProtectedCredentials,
    CRED_PROTECTION_TYPE* pProtectionType
);

パラメーター

名前方向説明
pszProtectedCredentialsLPWSTRinテスト対象の資格情報を指定する、null 終端文字列へのポインター。
pProtectionTypeCRED_PROTECTION_TYPE*outpszProtectedCredentials パラメーターで指定された資格情報が保護されているかどうかを指定する、CRED_PROTECTION_TYPE 列挙体の値へのポインター。

戻り値の型: BOOL

公式ドキュメント

指定された資格情報が、以前の CredProtect 関数の呼び出しによって暗号化されているかどうかを指定します。(Unicode)

戻り値

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

拡張エラー情報を取得するには、 GetLastError 関数を呼び出してください。

解説(Remarks)

メモ

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

CredIsProtectedW = ctypes.windll.advapi32.CredIsProtectedW
CredIsProtectedW.restype = wintypes.BOOL
CredIsProtectedW.argtypes = [
    wintypes.LPCWSTR,  # pszProtectedCredentials : LPWSTR
    ctypes.c_void_p,  # pProtectionType : CRED_PROTECTION_TYPE* out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

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

var (
	advapi32 = windows.NewLazySystemDLL("ADVAPI32.dll")
	procCredIsProtectedW = advapi32.NewProc("CredIsProtectedW")
)

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

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

typedef CredIsProtectedWNative = Int32 Function(Pointer<Utf16>, Pointer<Int32>);
typedef CredIsProtectedWDart = int Function(Pointer<Utf16>, Pointer<Int32>);
final CredIsProtectedW = DynamicLibrary.open('ADVAPI32.dll')
    .lookupFunction<CredIsProtectedWNative, CredIsProtectedWDart>('CredIsProtectedW');
// pszProtectedCredentials : LPWSTR -> Pointer<Utf16>
// pProtectionType : CRED_PROTECTION_TYPE* out -> Pointer<Int32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function CredIsProtectedW(
  pszProtectedCredentials: PWideChar;   // LPWSTR
  pProtectionType: Pointer   // CRED_PROTECTION_TYPE* out
): BOOL; stdcall;
  external 'ADVAPI32.dll' name 'CredIsProtectedW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "CredIsProtectedW"
  c_CredIsProtectedW :: CWString -> Ptr Int32 -> IO CInt
-- pszProtectedCredentials : LPWSTR -> CWString
-- pProtectionType : CRED_PROTECTION_TYPE* out -> Ptr Int32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let credisprotectedw =
  foreign "CredIsProtectedW"
    ((ptr uint16_t) @-> (ptr int32_t) @-> returning int32_t)
(* pszProtectedCredentials : LPWSTR -> (ptr uint16_t) *)
(* pProtectionType : CRED_PROTECTION_TYPE* 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 ("CredIsProtectedW" cred-is-protected-w :convention :stdcall) :int32
  (psz-protected-credentials (:string :encoding :utf-16le))   ; LPWSTR
  (p-protection-type :pointer))   ; CRED_PROTECTION_TYPE* out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $CredIsProtectedW = Win32::API::More->new('ADVAPI32',
    'BOOL CredIsProtectedW(LPCWSTR pszProtectedCredentials, LPVOID pProtectionType)');
# my $ret = $CredIsProtectedW->Call($pszProtectedCredentials, $pProtectionType);
# pszProtectedCredentials : LPWSTR -> LPCWSTR
# pProtectionType : CRED_PROTECTION_TYPE* out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

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