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

NCryptIsKeyHandle

関数
指定したハンドルがCNGキーハンドルか判定する。
DLLncrypt.dll呼出規約winapi対応OSWindows Vista 以降

シグネチャ

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

BOOL NCryptIsKeyHandle(
    NCRYPT_KEY_HANDLE hKey
);

パラメーター

名前方向説明
hKeyNCRYPT_KEY_HANDLEinテストするキーのハンドル。

戻り値の型: BOOL

公式ドキュメント

指定されたハンドルが CNG キーハンドルかどうかを判定します。

戻り値

ハンドルがキーハンドルである場合は 0 以外の値を返し、それ以外の場合は 0 を返します。

解説(Remarks)

サービスは、この関数を StartService 関数から呼び出してはなりません。サービスが StartService 関数からこの関数を呼び出すと、デッドロックが発生し、サービスが応答しなくなる可能性があります。

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

各言語での呼び出し定義

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

BOOL NCryptIsKeyHandle(
    NCRYPT_KEY_HANDLE hKey
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("ncrypt.dll", ExactSpelling = true)]
static extern bool NCryptIsKeyHandle(
    UIntPtr hKey   // NCRYPT_KEY_HANDLE
);
<DllImport("ncrypt.dll", ExactSpelling:=True)>
Public Shared Function NCryptIsKeyHandle(
    hKey As UIntPtr   ' NCRYPT_KEY_HANDLE
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' hKey : NCRYPT_KEY_HANDLE
Declare PtrSafe Function NCryptIsKeyHandle Lib "ncrypt" ( _
    ByVal hKey As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

NCryptIsKeyHandle = ctypes.windll.ncrypt.NCryptIsKeyHandle
NCryptIsKeyHandle.restype = wintypes.BOOL
NCryptIsKeyHandle.argtypes = [
    ctypes.c_size_t,  # hKey : NCRYPT_KEY_HANDLE
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('ncrypt.dll')
NCryptIsKeyHandle = Fiddle::Function.new(
  lib['NCryptIsKeyHandle'],
  [
    Fiddle::TYPE_UINTPTR_T,  # hKey : NCRYPT_KEY_HANDLE
  ],
  Fiddle::TYPE_INT)
#[link(name = "ncrypt")]
extern "system" {
    fn NCryptIsKeyHandle(
        hKey: usize  // NCRYPT_KEY_HANDLE
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("ncrypt.dll")]
public static extern bool NCryptIsKeyHandle(UIntPtr hKey);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ncrypt_NCryptIsKeyHandle' -Namespace Win32 -PassThru
# $api::NCryptIsKeyHandle(hKey)
#uselib "ncrypt.dll"
#func global NCryptIsKeyHandle "NCryptIsKeyHandle" sptr
; NCryptIsKeyHandle hKey   ; 戻り値は stat
; hKey : NCRYPT_KEY_HANDLE -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "ncrypt.dll"
#cfunc global NCryptIsKeyHandle "NCryptIsKeyHandle" sptr
; res = NCryptIsKeyHandle(hKey)
; hKey : NCRYPT_KEY_HANDLE -> "sptr"
; BOOL NCryptIsKeyHandle(NCRYPT_KEY_HANDLE hKey)
#uselib "ncrypt.dll"
#cfunc global NCryptIsKeyHandle "NCryptIsKeyHandle" intptr
; res = NCryptIsKeyHandle(hKey)
; hKey : NCRYPT_KEY_HANDLE -> "intptr"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	ncrypt = windows.NewLazySystemDLL("ncrypt.dll")
	procNCryptIsKeyHandle = ncrypt.NewProc("NCryptIsKeyHandle")
)

// hKey (NCRYPT_KEY_HANDLE)
r1, _, err := procNCryptIsKeyHandle.Call(
	uintptr(hKey),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function NCryptIsKeyHandle(
  hKey: NativeUInt   // NCRYPT_KEY_HANDLE
): BOOL; stdcall;
  external 'ncrypt.dll' name 'NCryptIsKeyHandle';
result := DllCall("ncrypt\NCryptIsKeyHandle"
    , "UPtr", hKey   ; NCRYPT_KEY_HANDLE
    , "Int")   ; return: BOOL
●NCryptIsKeyHandle(hKey) = DLL("ncrypt.dll", "bool NCryptIsKeyHandle(int)")
# 呼び出し: NCryptIsKeyHandle(hKey)
# hKey : NCRYPT_KEY_HANDLE -> "int"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

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

typedef NCryptIsKeyHandleNative = Int32 Function(UintPtr);
typedef NCryptIsKeyHandleDart = int Function(int);
final NCryptIsKeyHandle = DynamicLibrary.open('ncrypt.dll')
    .lookupFunction<NCryptIsKeyHandleNative, NCryptIsKeyHandleDart>('NCryptIsKeyHandle');
// hKey : NCRYPT_KEY_HANDLE -> UintPtr
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function NCryptIsKeyHandle(
  hKey: NativeUInt   // NCRYPT_KEY_HANDLE
): BOOL; stdcall;
  external 'ncrypt.dll' name 'NCryptIsKeyHandle';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "NCryptIsKeyHandle"
  c_NCryptIsKeyHandle :: CUIntPtr -> IO CInt
-- hKey : NCRYPT_KEY_HANDLE -> CUIntPtr
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let ncryptiskeyhandle =
  foreign "NCryptIsKeyHandle"
    (size_t @-> returning int32_t)
(* hKey : NCRYPT_KEY_HANDLE -> size_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library ncrypt (t "ncrypt.dll"))
(cffi:use-foreign-library ncrypt)

(cffi:defcfun ("NCryptIsKeyHandle" ncrypt-is-key-handle :convention :stdcall) :int32
  (h-key :uint64))   ; NCRYPT_KEY_HANDLE
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $NCryptIsKeyHandle = Win32::API::More->new('ncrypt',
    'BOOL NCryptIsKeyHandle(WPARAM hKey)');
# my $ret = $NCryptIsKeyHandle->Call($hKey);
# hKey : NCRYPT_KEY_HANDLE -> WPARAM
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。