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

NCryptRegisterProtectionDescriptorName

関数
保護記述子の名前付きルールを登録する。
DLLncrypt.dll呼出規約winapi対応OSwindows8.0

シグネチャ

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

HRESULT NCryptRegisterProtectionDescriptorName(
    LPCWSTR pwszName,
    LPCWSTR pwszDescriptorString,   // optional
    DWORD dwFlags
);

パラメーター

名前方向説明
pwszNameLPCWSTRin登録する記述子の表示名を格納した、null 終端の Unicode 文字列へのポインターです。
pwszDescriptorStringLPCWSTRinoptional保護記述子のルールを格納した、null 終端の Unicode 文字列へのポインターです。このパラメーターが NULL の場合、または文字列が空の場合、pwszName パラメーターに対して以前に作成されたレジストリ値が削除されます。
dwFlagsDWORDin新しいエントリを登録するレジストリハイブを示す定数です。この値が 0 の場合、レジストリのルートは HKEY_CURRENT_USER です。この値が NCRYPT_MACHINE_KEY_FLAG の場合、ルートは HKEY_LOCAL_MACHINE です。

戻り値の型: HRESULT

公式ドキュメント

保護記述子(protection descriptor)の表示名と、それに関連付けられたルール文字列を登録します。

戻り値

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

戻り値 説明
ERROR_SUCCESS
関数は成功しました。
NTE_INVALID_PARAMETER
pwszName パラメーターを NULL にすることはできません。また、このパラメーターが指す値を空の文字列にすることもできません。
NTE_BAD_FLAGS
dwFlags パラメーターは、0 または NCRYPT_MACHINE_KEY_FLAG でなければなりません。

解説(Remarks)

この関数を使用して作成されるレジストリキーは揮発性ではありません。情報はファイルに格納され、コンピューターのシャットダウン時にも保持されます。

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

各言語での呼び出し定義

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

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

NCryptRegisterProtectionDescriptorName = ctypes.windll.ncrypt.NCryptRegisterProtectionDescriptorName
NCryptRegisterProtectionDescriptorName.restype = ctypes.c_int
NCryptRegisterProtectionDescriptorName.argtypes = [
    wintypes.LPCWSTR,  # pwszName : LPCWSTR
    wintypes.LPCWSTR,  # pwszDescriptorString : LPCWSTR optional
    wintypes.DWORD,  # dwFlags : DWORD
]
require 'fiddle'
require 'fiddle/import'

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

var (
	ncrypt = windows.NewLazySystemDLL("ncrypt.dll")
	procNCryptRegisterProtectionDescriptorName = ncrypt.NewProc("NCryptRegisterProtectionDescriptorName")
)

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

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

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

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

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

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

関連項目

公式の関連項目