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

NCryptCreateProtectionDescriptor

関数
記述子文字列から保護記述子ハンドルを作成する。
DLLncrypt.dll呼出規約winapi対応OSwindows8.0

シグネチャ

// ncrypt.dll
#include <windows.h>

HRESULT NCryptCreateProtectionDescriptor(
    LPCWSTR pwszDescriptorString,
    DWORD dwFlags,
    NCRYPT_DESCRIPTOR_HANDLE* phDescriptor
);

パラメーター

名前方向説明
pwszDescriptorStringLPCWSTRin

保護記述子のルール文字列、またはそのルールに登録された表示名を含む、null 終端の Unicode 文字列です。

表示名を指定し、それに関連付けられた保護記述子のルール文字列をこの関数にレジストリ内で検索させたい場合は、dwFlags パラメーターを NCRYPT_NAMED_DESCRIPTOR_FLAG に設定する必要があります。

dwFlagsDWORDin

pwszDescriptorString 内の文字列が保護記述子の表示名を表すかどうか、表す場合は関数が関連する保護ルール文字列をレジストリ内のどこで検索すべきかを指定するフラグです。次の値の組み合わせを設定できます。

  • pwszDescriptorString パラメーターに設定された値が表示名ではなく完全な保護記述子のルール文字列であることを示すには、dwFlags パラメーターをゼロ (0) に設定します。
  • 文字列が表示名であり、それに関連付けられた記述子ルール文字列とともに HKEY_LOCAL_MACHINE レジストリ ハイブに保存されていることを示すには、NCRYPT_NAMED_DESCRIPTOR_FLAG 値と NCRYPT_MACHINE_KEY_FLAG 値をビットごとの OR で結合します。
  • 文字列が表示名であり、それに関連付けられた記述子ルール文字列とともに HKEY_CURRENT_USER レジストリ ハイブに保存されていることを示すには、NCRYPT_NAMED_DESCRIPTOR_FLAG 値のみを設定します。つまり、現在のユーザーのレジストリ ハイブを指定する固有のフラグはありません。
注意 記述子ルールを表示名に関連付けて両方をレジストリに保存するには、NCryptRegisterProtectionDescriptorName 関数を呼び出します。
phDescriptorNCRYPT_DESCRIPTOR_HANDLE*out保護記述子オブジェクトのハンドルへのポインターです。

戻り値の型: HRESULT

公式ドキュメント

保護記述子オブジェクトへのハンドルを取得します。

戻り値

関数の成否を示すステータス コードを返します。返される可能性のあるコードには、次のものが含まれますが、これらに限定されません。

戻り値 説明
ERROR_SUCCESS
関数は成功しました。
NTE_INVALID_PARAMETER
phDescriptor パラメーターを NULL にすることはできません。

pwszDescriptorString パラメーターを NULL にすることはできず、空の文字列にすることもできません。

NTE_BAD_FLAGS
dwFlags パラメーターは NCRYPT_MACHINE_KEY_FLAG または NCRYPT_NAMED_DESCRIPTOR_FLAG でなければなりません。
NTE_NO_MEMORY
登録された保護記述子文字列を取得するためのメモリを割り当てられませんでした。
NTE_NOT_FOUND
pwszDescriptorString パラメーターで指定された保護記述子の名前が見つかりませんでした。

解説(Remarks)

この関数によって作成される保護記述子オブジェクトは、記述子に関する情報を含む内部データ構造です。直接使用することはできません。ただし、アプリケーションは返されたハンドルを次の関数で使用できます。

次の例は、保護記述子のルール文字列を示しています。 NCryptRegisterProtectionDescriptorName 関数を使用すると、表示名をルール文字列に関連付けて、両方をレジストリに保存できます。
出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

// ncrypt.dll
#include <windows.h>

HRESULT NCryptCreateProtectionDescriptor(
    LPCWSTR pwszDescriptorString,
    DWORD dwFlags,
    NCRYPT_DESCRIPTOR_HANDLE* phDescriptor
);
[DllImport("ncrypt.dll", ExactSpelling = true)]
static extern int NCryptCreateProtectionDescriptor(
    [MarshalAs(UnmanagedType.LPWStr)] string pwszDescriptorString,   // LPCWSTR
    uint dwFlags,   // DWORD
    IntPtr phDescriptor   // NCRYPT_DESCRIPTOR_HANDLE* out
);
<DllImport("ncrypt.dll", ExactSpelling:=True)>
Public Shared Function NCryptCreateProtectionDescriptor(
    <MarshalAs(UnmanagedType.LPWStr)> pwszDescriptorString As String,   ' LPCWSTR
    dwFlags As UInteger,   ' DWORD
    phDescriptor As IntPtr   ' NCRYPT_DESCRIPTOR_HANDLE* out
) As Integer
End Function
' pwszDescriptorString : LPCWSTR
' dwFlags : DWORD
' phDescriptor : NCRYPT_DESCRIPTOR_HANDLE* out
Declare PtrSafe Function NCryptCreateProtectionDescriptor Lib "ncrypt" ( _
    ByVal pwszDescriptorString As LongPtr, _
    ByVal dwFlags As Long, _
    ByVal phDescriptor As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

NCryptCreateProtectionDescriptor = ctypes.windll.ncrypt.NCryptCreateProtectionDescriptor
NCryptCreateProtectionDescriptor.restype = ctypes.c_int
NCryptCreateProtectionDescriptor.argtypes = [
    wintypes.LPCWSTR,  # pwszDescriptorString : LPCWSTR
    wintypes.DWORD,  # dwFlags : DWORD
    ctypes.c_void_p,  # phDescriptor : NCRYPT_DESCRIPTOR_HANDLE* out
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('ncrypt.dll')
NCryptCreateProtectionDescriptor = Fiddle::Function.new(
  lib['NCryptCreateProtectionDescriptor'],
  [
    Fiddle::TYPE_VOIDP,  # pwszDescriptorString : LPCWSTR
    -Fiddle::TYPE_INT,  # dwFlags : DWORD
    Fiddle::TYPE_VOIDP,  # phDescriptor : NCRYPT_DESCRIPTOR_HANDLE* out
  ],
  Fiddle::TYPE_INT)
#[link(name = "ncrypt")]
extern "system" {
    fn NCryptCreateProtectionDescriptor(
        pwszDescriptorString: *const u16,  // LPCWSTR
        dwFlags: u32,  // DWORD
        phDescriptor: *mut *mut core::ffi::c_void  // NCRYPT_DESCRIPTOR_HANDLE* out
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("ncrypt.dll")]
public static extern int NCryptCreateProtectionDescriptor([MarshalAs(UnmanagedType.LPWStr)] string pwszDescriptorString, uint dwFlags, IntPtr phDescriptor);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ncrypt_NCryptCreateProtectionDescriptor' -Namespace Win32 -PassThru
# $api::NCryptCreateProtectionDescriptor(pwszDescriptorString, dwFlags, phDescriptor)
#uselib "ncrypt.dll"
#func global NCryptCreateProtectionDescriptor "NCryptCreateProtectionDescriptor" sptr, sptr, sptr
; NCryptCreateProtectionDescriptor pwszDescriptorString, dwFlags, phDescriptor   ; 戻り値は stat
; pwszDescriptorString : LPCWSTR -> "sptr"
; dwFlags : DWORD -> "sptr"
; phDescriptor : NCRYPT_DESCRIPTOR_HANDLE* out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "ncrypt.dll"
#cfunc global NCryptCreateProtectionDescriptor "NCryptCreateProtectionDescriptor" wstr, int, sptr
; res = NCryptCreateProtectionDescriptor(pwszDescriptorString, dwFlags, phDescriptor)
; pwszDescriptorString : LPCWSTR -> "wstr"
; dwFlags : DWORD -> "int"
; phDescriptor : NCRYPT_DESCRIPTOR_HANDLE* out -> "sptr"
; HRESULT NCryptCreateProtectionDescriptor(LPCWSTR pwszDescriptorString, DWORD dwFlags, NCRYPT_DESCRIPTOR_HANDLE* phDescriptor)
#uselib "ncrypt.dll"
#cfunc global NCryptCreateProtectionDescriptor "NCryptCreateProtectionDescriptor" wstr, int, intptr
; res = NCryptCreateProtectionDescriptor(pwszDescriptorString, dwFlags, phDescriptor)
; pwszDescriptorString : LPCWSTR -> "wstr"
; dwFlags : DWORD -> "int"
; phDescriptor : NCRYPT_DESCRIPTOR_HANDLE* out -> "intptr"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	ncrypt = windows.NewLazySystemDLL("ncrypt.dll")
	procNCryptCreateProtectionDescriptor = ncrypt.NewProc("NCryptCreateProtectionDescriptor")
)

// pwszDescriptorString (LPCWSTR), dwFlags (DWORD), phDescriptor (NCRYPT_DESCRIPTOR_HANDLE* out)
r1, _, err := procNCryptCreateProtectionDescriptor.Call(
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pwszDescriptorString))),
	uintptr(dwFlags),
	uintptr(phDescriptor),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // HRESULT
function NCryptCreateProtectionDescriptor(
  pwszDescriptorString: PWideChar;   // LPCWSTR
  dwFlags: DWORD;   // DWORD
  phDescriptor: Pointer   // NCRYPT_DESCRIPTOR_HANDLE* out
): Integer; stdcall;
  external 'ncrypt.dll' name 'NCryptCreateProtectionDescriptor';
result := DllCall("ncrypt\NCryptCreateProtectionDescriptor"
    , "WStr", pwszDescriptorString   ; LPCWSTR
    , "UInt", dwFlags   ; DWORD
    , "Ptr", phDescriptor   ; NCRYPT_DESCRIPTOR_HANDLE* out
    , "Int")   ; return: HRESULT
●NCryptCreateProtectionDescriptor(pwszDescriptorString, dwFlags, phDescriptor) = DLL("ncrypt.dll", "int NCryptCreateProtectionDescriptor(char*, dword, void*)")
# 呼び出し: NCryptCreateProtectionDescriptor(pwszDescriptorString, dwFlags, phDescriptor)
# pwszDescriptorString : LPCWSTR -> "char*"
# dwFlags : DWORD -> "dword"
# phDescriptor : NCRYPT_DESCRIPTOR_HANDLE* out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "ncrypt" fn NCryptCreateProtectionDescriptor(
    pwszDescriptorString: [*c]const u16, // LPCWSTR
    dwFlags: u32, // DWORD
    phDescriptor: ?*anyopaque // NCRYPT_DESCRIPTOR_HANDLE* out
) callconv(std.os.windows.WINAPI) i32;
proc NCryptCreateProtectionDescriptor(
    pwszDescriptorString: WideCString,  # LPCWSTR
    dwFlags: uint32,  # DWORD
    phDescriptor: pointer  # NCRYPT_DESCRIPTOR_HANDLE* out
): int32 {.importc: "NCryptCreateProtectionDescriptor", stdcall, dynlib: "ncrypt.dll".}
pragma(lib, "ncrypt");
extern(Windows)
int NCryptCreateProtectionDescriptor(
    const(wchar)* pwszDescriptorString,   // LPCWSTR
    uint dwFlags,   // DWORD
    void* phDescriptor   // NCRYPT_DESCRIPTOR_HANDLE* out
);
ccall((:NCryptCreateProtectionDescriptor, "ncrypt.dll"), stdcall, Int32,
      (Cwstring, UInt32, Ptr{Cvoid}),
      pwszDescriptorString, dwFlags, phDescriptor)
# pwszDescriptorString : LPCWSTR -> Cwstring
# dwFlags : DWORD -> UInt32
# phDescriptor : NCRYPT_DESCRIPTOR_HANDLE* out -> Ptr{Cvoid}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t NCryptCreateProtectionDescriptor(
    const uint16_t* pwszDescriptorString,
    uint32_t dwFlags,
    void* phDescriptor);
]]
local ncrypt = ffi.load("ncrypt")
-- ncrypt.NCryptCreateProtectionDescriptor(pwszDescriptorString, dwFlags, phDescriptor)
-- pwszDescriptorString : LPCWSTR
-- dwFlags : DWORD
-- phDescriptor : NCRYPT_DESCRIPTOR_HANDLE* out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('ncrypt.dll');
const NCryptCreateProtectionDescriptor = lib.func('__stdcall', 'NCryptCreateProtectionDescriptor', 'int32_t', ['str16', 'uint32_t', 'void *']);
// NCryptCreateProtectionDescriptor(pwszDescriptorString, dwFlags, phDescriptor)
// pwszDescriptorString : LPCWSTR -> 'str16'
// dwFlags : DWORD -> 'uint32_t'
// phDescriptor : NCRYPT_DESCRIPTOR_HANDLE* out -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("ncrypt.dll", {
  NCryptCreateProtectionDescriptor: { parameters: ["buffer", "u32", "pointer"], result: "i32" },
});
// lib.symbols.NCryptCreateProtectionDescriptor(pwszDescriptorString, dwFlags, phDescriptor)
// pwszDescriptorString : LPCWSTR -> "buffer"
// dwFlags : DWORD -> "u32"
// phDescriptor : NCRYPT_DESCRIPTOR_HANDLE* out -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t NCryptCreateProtectionDescriptor(
    const uint16_t* pwszDescriptorString,
    uint32_t dwFlags,
    void* phDescriptor);
C, "ncrypt.dll");
// $ffi->NCryptCreateProtectionDescriptor(pwszDescriptorString, dwFlags, phDescriptor);
// pwszDescriptorString : LPCWSTR
// dwFlags : DWORD
// phDescriptor : NCRYPT_DESCRIPTOR_HANDLE* 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 Ncrypt extends StdCallLibrary {
    Ncrypt INSTANCE = Native.load("ncrypt", Ncrypt.class);
    int NCryptCreateProtectionDescriptor(
        WString pwszDescriptorString,   // LPCWSTR
        int dwFlags,   // DWORD
        Pointer phDescriptor   // NCRYPT_DESCRIPTOR_HANDLE* out
    );
}
@[Link("ncrypt")]
lib Libncrypt
  fun NCryptCreateProtectionDescriptor = NCryptCreateProtectionDescriptor(
    pwszDescriptorString : UInt16*,   # LPCWSTR
    dwFlags : UInt32,   # DWORD
    phDescriptor : Void*   # NCRYPT_DESCRIPTOR_HANDLE* out
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef NCryptCreateProtectionDescriptorNative = Int32 Function(Pointer<Utf16>, Uint32, Pointer<Void>);
typedef NCryptCreateProtectionDescriptorDart = int Function(Pointer<Utf16>, int, Pointer<Void>);
final NCryptCreateProtectionDescriptor = DynamicLibrary.open('ncrypt.dll')
    .lookupFunction<NCryptCreateProtectionDescriptorNative, NCryptCreateProtectionDescriptorDart>('NCryptCreateProtectionDescriptor');
// pwszDescriptorString : LPCWSTR -> Pointer<Utf16>
// dwFlags : DWORD -> Uint32
// phDescriptor : NCRYPT_DESCRIPTOR_HANDLE* out -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function NCryptCreateProtectionDescriptor(
  pwszDescriptorString: PWideChar;   // LPCWSTR
  dwFlags: DWORD;   // DWORD
  phDescriptor: Pointer   // NCRYPT_DESCRIPTOR_HANDLE* out
): Integer; stdcall;
  external 'ncrypt.dll' name 'NCryptCreateProtectionDescriptor';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "NCryptCreateProtectionDescriptor"
  c_NCryptCreateProtectionDescriptor :: CWString -> Word32 -> Ptr () -> IO Int32
-- pwszDescriptorString : LPCWSTR -> CWString
-- dwFlags : DWORD -> Word32
-- phDescriptor : NCRYPT_DESCRIPTOR_HANDLE* out -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let ncryptcreateprotectiondescriptor =
  foreign "NCryptCreateProtectionDescriptor"
    ((ptr uint16_t) @-> uint32_t @-> (ptr void) @-> returning int32_t)
(* pwszDescriptorString : LPCWSTR -> (ptr uint16_t) *)
(* dwFlags : DWORD -> uint32_t *)
(* phDescriptor : NCRYPT_DESCRIPTOR_HANDLE* out -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library ncrypt (t "ncrypt.dll"))
(cffi:use-foreign-library ncrypt)

(cffi:defcfun ("NCryptCreateProtectionDescriptor" ncrypt-create-protection-descriptor :convention :stdcall) :int32
  (pwsz-descriptor-string (:string :encoding :utf-16le))   ; LPCWSTR
  (dw-flags :uint32)   ; DWORD
  (ph-descriptor :pointer))   ; NCRYPT_DESCRIPTOR_HANDLE* out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $NCryptCreateProtectionDescriptor = Win32::API::More->new('ncrypt',
    'int NCryptCreateProtectionDescriptor(LPCWSTR pwszDescriptorString, DWORD dwFlags, HANDLE phDescriptor)');
# my $ret = $NCryptCreateProtectionDescriptor->Call($pwszDescriptorString, $dwFlags, $phDescriptor);
# pwszDescriptorString : LPCWSTR -> LPCWSTR
# dwFlags : DWORD -> DWORD
# phDescriptor : NCRYPT_DESCRIPTOR_HANDLE* out -> HANDLE
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。