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

NCryptTranslateHandle

関数
レガシーCryptoAPIハンドルをCNGハンドルに変換する。
DLLncrypt.dll呼出規約winapi対応OSWindows Vista 以降

シグネチャ

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

HRESULT NCryptTranslateHandle(
    NCRYPT_PROV_HANDLE* phProvider,   // optional
    NCRYPT_KEY_HANDLE* phKey,
    UINT_PTR hLegacyProv,
    UINT_PTR hLegacyKey,   // optional
    CERT_KEY_SPEC dwLegacyKeySpec,   // optional
    DWORD dwFlags
);

パラメーター

名前方向説明
phProviderNCRYPT_PROV_HANDLE*outoptionalphKey パラメーターに格納される CNG キーを所有する CNG キーストレージプロバイダーのハンドルを受け取る NCRYPT_PROV_HANDLE 変数へのポインター。このハンドルが不要な場合、このパラメーターは NULL にできます。
phKeyNCRYPT_KEY_HANDLE*outCNG キーハンドルを受け取る NCRYPT_KEY_HANDLE 変数へのポインター。
hLegacyProvUINT_PTRin変換するキーを含む CryptoAPI プロバイダーのハンドル。この関数は、このプロバイダー内のコンテナーにある CryptoAPI キーを変換します。
hLegacyKeyUINT_PTRinoptional

返されるキーのキー仕様を判別するために使用する CryptoAPI キーのハンドル。このパラメーターは、dwLegacyKeySpec パラメーターに 0 以外の値が含まれている場合は無視されます。

hLegacyKeyNULLdwLegacyKeySpec が 0 の場合、この関数は hLegacyProv ハンドルからキー仕様を判別しようとします。

dwLegacyKeySpecCERT_KEY_SPECinoptional

キーのキー仕様を指定します。次のいずれかの値を指定できます。

意味
0
キーは以下のいずれの種類でもありません。
AT_KEYEXCHANGE
1
キーはキー交換キーです。
AT_SIGNATURE
2
キーは署名キーです。

hLegacyKeyNULLdwLegacyKeySpec が 0 の場合、この関数は hLegacyProv ハンドルからキー仕様を判別しようとします。

dwFlagsDWORDinこの関数の動作を変更するフラグのセット。この関数に対して定義されているフラグはありません。

戻り値の型: HRESULT

公式ドキュメント

CryptoAPI ハンドルを CNG キーハンドルに変換します。

戻り値

関数の成功または失敗を示すステータスコードを返します。

返される可能性のあるコードには、以下が含まれますが、これらに限定されません。

戻り値 説明
ERROR_SUCCESS
関数は成功しました。
NTE_BAD_FLAGS
dwFlags パラメーターに有効でない値が含まれています。
NTE_INVALID_PARAMETER
1 つ以上のパラメーターが有効ではありません。
NTE_NO_MEMORY
メモリ割り当てエラーが発生しました。

解説(Remarks)

これは、現在 CryptoAPI を使用しているアプリケーションやシステムコンポーネントが CNG の使用へ円滑に移行できるよう支援することを目的としたヘルパー関数です。

この関数は、hLegacyProv パラメーターで参照される暗号化サービスプロバイダー (CSP) の名前と同一の名前またはエイリアスを持つ CNG キーストレージプロバイダーが登録されている場合にのみ成功します。

この関数は、CSP ハンドルを CNG キーハンドルに変換するために、次の手順を実行します。

  1. hLegacyProv ハンドルから CSP の名前を取得します。
  2. CSP 名と同一の名前またはエイリアスを持つ CNG プロバイダーを開きます。
  3. CSP 内の現在のキーコンテナーの名前を取得します。
  4. CryptoAPI キーを取得し、それを CNG キーに変換して、phKey パラメーターで返します。
サービスは、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>

HRESULT NCryptTranslateHandle(
    NCRYPT_PROV_HANDLE* phProvider,   // optional
    NCRYPT_KEY_HANDLE* phKey,
    UINT_PTR hLegacyProv,
    UINT_PTR hLegacyKey,   // optional
    CERT_KEY_SPEC dwLegacyKeySpec,   // optional
    DWORD dwFlags
);
[DllImport("ncrypt.dll", ExactSpelling = true)]
static extern int NCryptTranslateHandle(
    IntPtr phProvider,   // NCRYPT_PROV_HANDLE* optional, out
    out UIntPtr phKey,   // NCRYPT_KEY_HANDLE* out
    UIntPtr hLegacyProv,   // UINT_PTR
    UIntPtr hLegacyKey,   // UINT_PTR optional
    uint dwLegacyKeySpec,   // CERT_KEY_SPEC optional
    uint dwFlags   // DWORD
);
<DllImport("ncrypt.dll", ExactSpelling:=True)>
Public Shared Function NCryptTranslateHandle(
    phProvider As IntPtr,   ' NCRYPT_PROV_HANDLE* optional, out
    <Out> ByRef phKey As UIntPtr,   ' NCRYPT_KEY_HANDLE* out
    hLegacyProv As UIntPtr,   ' UINT_PTR
    hLegacyKey As UIntPtr,   ' UINT_PTR optional
    dwLegacyKeySpec As UInteger,   ' CERT_KEY_SPEC optional
    dwFlags As UInteger   ' DWORD
) As Integer
End Function
' phProvider : NCRYPT_PROV_HANDLE* optional, out
' phKey : NCRYPT_KEY_HANDLE* out
' hLegacyProv : UINT_PTR
' hLegacyKey : UINT_PTR optional
' dwLegacyKeySpec : CERT_KEY_SPEC optional
' dwFlags : DWORD
Declare PtrSafe Function NCryptTranslateHandle Lib "ncrypt" ( _
    ByVal phProvider As LongPtr, _
    ByRef phKey As LongPtr, _
    ByVal hLegacyProv As LongPtr, _
    ByVal hLegacyKey As LongPtr, _
    ByVal dwLegacyKeySpec As Long, _
    ByVal dwFlags As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

NCryptTranslateHandle = ctypes.windll.ncrypt.NCryptTranslateHandle
NCryptTranslateHandle.restype = ctypes.c_int
NCryptTranslateHandle.argtypes = [
    ctypes.c_void_p,  # phProvider : NCRYPT_PROV_HANDLE* optional, out
    ctypes.c_void_p,  # phKey : NCRYPT_KEY_HANDLE* out
    ctypes.c_size_t,  # hLegacyProv : UINT_PTR
    ctypes.c_size_t,  # hLegacyKey : UINT_PTR optional
    wintypes.DWORD,  # dwLegacyKeySpec : CERT_KEY_SPEC optional
    wintypes.DWORD,  # dwFlags : DWORD
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('ncrypt.dll')
NCryptTranslateHandle = Fiddle::Function.new(
  lib['NCryptTranslateHandle'],
  [
    Fiddle::TYPE_VOIDP,  # phProvider : NCRYPT_PROV_HANDLE* optional, out
    Fiddle::TYPE_VOIDP,  # phKey : NCRYPT_KEY_HANDLE* out
    Fiddle::TYPE_UINTPTR_T,  # hLegacyProv : UINT_PTR
    Fiddle::TYPE_UINTPTR_T,  # hLegacyKey : UINT_PTR optional
    -Fiddle::TYPE_INT,  # dwLegacyKeySpec : CERT_KEY_SPEC optional
    -Fiddle::TYPE_INT,  # dwFlags : DWORD
  ],
  Fiddle::TYPE_INT)
#[link(name = "ncrypt")]
extern "system" {
    fn NCryptTranslateHandle(
        phProvider: *mut usize,  // NCRYPT_PROV_HANDLE* optional, out
        phKey: *mut usize,  // NCRYPT_KEY_HANDLE* out
        hLegacyProv: usize,  // UINT_PTR
        hLegacyKey: usize,  // UINT_PTR optional
        dwLegacyKeySpec: u32,  // CERT_KEY_SPEC optional
        dwFlags: u32  // DWORD
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("ncrypt.dll")]
public static extern int NCryptTranslateHandle(IntPtr phProvider, out UIntPtr phKey, UIntPtr hLegacyProv, UIntPtr hLegacyKey, uint dwLegacyKeySpec, uint dwFlags);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ncrypt_NCryptTranslateHandle' -Namespace Win32 -PassThru
# $api::NCryptTranslateHandle(phProvider, phKey, hLegacyProv, hLegacyKey, dwLegacyKeySpec, dwFlags)
#uselib "ncrypt.dll"
#func global NCryptTranslateHandle "NCryptTranslateHandle" sptr, sptr, sptr, sptr, sptr, sptr
; NCryptTranslateHandle varptr(phProvider), varptr(phKey), hLegacyProv, hLegacyKey, dwLegacyKeySpec, dwFlags   ; 戻り値は stat
; phProvider : NCRYPT_PROV_HANDLE* optional, out -> "sptr"
; phKey : NCRYPT_KEY_HANDLE* out -> "sptr"
; hLegacyProv : UINT_PTR -> "sptr"
; hLegacyKey : UINT_PTR optional -> "sptr"
; dwLegacyKeySpec : CERT_KEY_SPEC optional -> "sptr"
; dwFlags : DWORD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "ncrypt.dll"
#cfunc global NCryptTranslateHandle "NCryptTranslateHandle" var, var, sptr, sptr, int, int
; res = NCryptTranslateHandle(phProvider, phKey, hLegacyProv, hLegacyKey, dwLegacyKeySpec, dwFlags)
; phProvider : NCRYPT_PROV_HANDLE* optional, out -> "var"
; phKey : NCRYPT_KEY_HANDLE* out -> "var"
; hLegacyProv : UINT_PTR -> "sptr"
; hLegacyKey : UINT_PTR optional -> "sptr"
; dwLegacyKeySpec : CERT_KEY_SPEC optional -> "int"
; dwFlags : DWORD -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; HRESULT NCryptTranslateHandle(NCRYPT_PROV_HANDLE* phProvider, NCRYPT_KEY_HANDLE* phKey, UINT_PTR hLegacyProv, UINT_PTR hLegacyKey, CERT_KEY_SPEC dwLegacyKeySpec, DWORD dwFlags)
#uselib "ncrypt.dll"
#cfunc global NCryptTranslateHandle "NCryptTranslateHandle" var, var, intptr, intptr, int, int
; res = NCryptTranslateHandle(phProvider, phKey, hLegacyProv, hLegacyKey, dwLegacyKeySpec, dwFlags)
; phProvider : NCRYPT_PROV_HANDLE* optional, out -> "var"
; phKey : NCRYPT_KEY_HANDLE* out -> "var"
; hLegacyProv : UINT_PTR -> "intptr"
; hLegacyKey : UINT_PTR optional -> "intptr"
; dwLegacyKeySpec : CERT_KEY_SPEC optional -> "int"
; dwFlags : DWORD -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	ncrypt = windows.NewLazySystemDLL("ncrypt.dll")
	procNCryptTranslateHandle = ncrypt.NewProc("NCryptTranslateHandle")
)

// phProvider (NCRYPT_PROV_HANDLE* optional, out), phKey (NCRYPT_KEY_HANDLE* out), hLegacyProv (UINT_PTR), hLegacyKey (UINT_PTR optional), dwLegacyKeySpec (CERT_KEY_SPEC optional), dwFlags (DWORD)
r1, _, err := procNCryptTranslateHandle.Call(
	uintptr(phProvider),
	uintptr(phKey),
	uintptr(hLegacyProv),
	uintptr(hLegacyKey),
	uintptr(dwLegacyKeySpec),
	uintptr(dwFlags),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // HRESULT
function NCryptTranslateHandle(
  phProvider: Pointer;   // NCRYPT_PROV_HANDLE* optional, out
  phKey: Pointer;   // NCRYPT_KEY_HANDLE* out
  hLegacyProv: NativeUInt;   // UINT_PTR
  hLegacyKey: NativeUInt;   // UINT_PTR optional
  dwLegacyKeySpec: DWORD;   // CERT_KEY_SPEC optional
  dwFlags: DWORD   // DWORD
): Integer; stdcall;
  external 'ncrypt.dll' name 'NCryptTranslateHandle';
result := DllCall("ncrypt\NCryptTranslateHandle"
    , "Ptr", phProvider   ; NCRYPT_PROV_HANDLE* optional, out
    , "Ptr", phKey   ; NCRYPT_KEY_HANDLE* out
    , "UPtr", hLegacyProv   ; UINT_PTR
    , "UPtr", hLegacyKey   ; UINT_PTR optional
    , "UInt", dwLegacyKeySpec   ; CERT_KEY_SPEC optional
    , "UInt", dwFlags   ; DWORD
    , "Int")   ; return: HRESULT
●NCryptTranslateHandle(phProvider, phKey, hLegacyProv, hLegacyKey, dwLegacyKeySpec, dwFlags) = DLL("ncrypt.dll", "int NCryptTranslateHandle(void*, void*, int, int, dword, dword)")
# 呼び出し: NCryptTranslateHandle(phProvider, phKey, hLegacyProv, hLegacyKey, dwLegacyKeySpec, dwFlags)
# phProvider : NCRYPT_PROV_HANDLE* optional, out -> "void*"
# phKey : NCRYPT_KEY_HANDLE* out -> "void*"
# hLegacyProv : UINT_PTR -> "int"
# hLegacyKey : UINT_PTR optional -> "int"
# dwLegacyKeySpec : CERT_KEY_SPEC optional -> "dword"
# dwFlags : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "ncrypt" fn NCryptTranslateHandle(
    phProvider: [*c]usize, // NCRYPT_PROV_HANDLE* optional, out
    phKey: [*c]usize, // NCRYPT_KEY_HANDLE* out
    hLegacyProv: usize, // UINT_PTR
    hLegacyKey: usize, // UINT_PTR optional
    dwLegacyKeySpec: u32, // CERT_KEY_SPEC optional
    dwFlags: u32 // DWORD
) callconv(std.os.windows.WINAPI) i32;
proc NCryptTranslateHandle(
    phProvider: ptr uint,  # NCRYPT_PROV_HANDLE* optional, out
    phKey: ptr uint,  # NCRYPT_KEY_HANDLE* out
    hLegacyProv: uint,  # UINT_PTR
    hLegacyKey: uint,  # UINT_PTR optional
    dwLegacyKeySpec: uint32,  # CERT_KEY_SPEC optional
    dwFlags: uint32  # DWORD
): int32 {.importc: "NCryptTranslateHandle", stdcall, dynlib: "ncrypt.dll".}
pragma(lib, "ncrypt");
extern(Windows)
int NCryptTranslateHandle(
    size_t* phProvider,   // NCRYPT_PROV_HANDLE* optional, out
    size_t* phKey,   // NCRYPT_KEY_HANDLE* out
    size_t hLegacyProv,   // UINT_PTR
    size_t hLegacyKey,   // UINT_PTR optional
    uint dwLegacyKeySpec,   // CERT_KEY_SPEC optional
    uint dwFlags   // DWORD
);
ccall((:NCryptTranslateHandle, "ncrypt.dll"), stdcall, Int32,
      (Ptr{Csize_t}, Ptr{Csize_t}, Csize_t, Csize_t, UInt32, UInt32),
      phProvider, phKey, hLegacyProv, hLegacyKey, dwLegacyKeySpec, dwFlags)
# phProvider : NCRYPT_PROV_HANDLE* optional, out -> Ptr{Csize_t}
# phKey : NCRYPT_KEY_HANDLE* out -> Ptr{Csize_t}
# hLegacyProv : UINT_PTR -> Csize_t
# hLegacyKey : UINT_PTR optional -> Csize_t
# dwLegacyKeySpec : CERT_KEY_SPEC optional -> UInt32
# dwFlags : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t NCryptTranslateHandle(
    uintptr_t* phProvider,
    uintptr_t* phKey,
    uintptr_t hLegacyProv,
    uintptr_t hLegacyKey,
    uint32_t dwLegacyKeySpec,
    uint32_t dwFlags);
]]
local ncrypt = ffi.load("ncrypt")
-- ncrypt.NCryptTranslateHandle(phProvider, phKey, hLegacyProv, hLegacyKey, dwLegacyKeySpec, dwFlags)
-- phProvider : NCRYPT_PROV_HANDLE* optional, out
-- phKey : NCRYPT_KEY_HANDLE* out
-- hLegacyProv : UINT_PTR
-- hLegacyKey : UINT_PTR optional
-- dwLegacyKeySpec : CERT_KEY_SPEC optional
-- dwFlags : DWORD
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('ncrypt.dll');
const NCryptTranslateHandle = lib.func('__stdcall', 'NCryptTranslateHandle', 'int32_t', ['uintptr_t *', 'uintptr_t *', 'uintptr_t', 'uintptr_t', 'uint32_t', 'uint32_t']);
// NCryptTranslateHandle(phProvider, phKey, hLegacyProv, hLegacyKey, dwLegacyKeySpec, dwFlags)
// phProvider : NCRYPT_PROV_HANDLE* optional, out -> 'uintptr_t *'
// phKey : NCRYPT_KEY_HANDLE* out -> 'uintptr_t *'
// hLegacyProv : UINT_PTR -> 'uintptr_t'
// hLegacyKey : UINT_PTR optional -> 'uintptr_t'
// dwLegacyKeySpec : CERT_KEY_SPEC optional -> 'uint32_t'
// dwFlags : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("ncrypt.dll", {
  NCryptTranslateHandle: { parameters: ["pointer", "pointer", "usize", "usize", "u32", "u32"], result: "i32" },
});
// lib.symbols.NCryptTranslateHandle(phProvider, phKey, hLegacyProv, hLegacyKey, dwLegacyKeySpec, dwFlags)
// phProvider : NCRYPT_PROV_HANDLE* optional, out -> "pointer"
// phKey : NCRYPT_KEY_HANDLE* out -> "pointer"
// hLegacyProv : UINT_PTR -> "usize"
// hLegacyKey : UINT_PTR optional -> "usize"
// dwLegacyKeySpec : CERT_KEY_SPEC optional -> "u32"
// dwFlags : DWORD -> "u32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t NCryptTranslateHandle(
    size_t* phProvider,
    size_t* phKey,
    size_t hLegacyProv,
    size_t hLegacyKey,
    uint32_t dwLegacyKeySpec,
    uint32_t dwFlags);
C, "ncrypt.dll");
// $ffi->NCryptTranslateHandle(phProvider, phKey, hLegacyProv, hLegacyKey, dwLegacyKeySpec, dwFlags);
// phProvider : NCRYPT_PROV_HANDLE* optional, out
// phKey : NCRYPT_KEY_HANDLE* out
// hLegacyProv : UINT_PTR
// hLegacyKey : UINT_PTR optional
// dwLegacyKeySpec : CERT_KEY_SPEC 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 NCryptTranslateHandle(
        LongByReference phProvider,   // NCRYPT_PROV_HANDLE* optional, out
        LongByReference phKey,   // NCRYPT_KEY_HANDLE* out
        long hLegacyProv,   // UINT_PTR
        long hLegacyKey,   // UINT_PTR optional
        int dwLegacyKeySpec,   // CERT_KEY_SPEC optional
        int dwFlags   // DWORD
    );
}
@[Link("ncrypt")]
lib Libncrypt
  fun NCryptTranslateHandle = NCryptTranslateHandle(
    phProvider : LibC::SizeT*,   # NCRYPT_PROV_HANDLE* optional, out
    phKey : LibC::SizeT*,   # NCRYPT_KEY_HANDLE* out
    hLegacyProv : LibC::SizeT,   # UINT_PTR
    hLegacyKey : LibC::SizeT,   # UINT_PTR optional
    dwLegacyKeySpec : UInt32,   # CERT_KEY_SPEC 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 NCryptTranslateHandleNative = Int32 Function(Pointer<UintPtr>, Pointer<UintPtr>, UintPtr, UintPtr, Uint32, Uint32);
typedef NCryptTranslateHandleDart = int Function(Pointer<UintPtr>, Pointer<UintPtr>, int, int, int, int);
final NCryptTranslateHandle = DynamicLibrary.open('ncrypt.dll')
    .lookupFunction<NCryptTranslateHandleNative, NCryptTranslateHandleDart>('NCryptTranslateHandle');
// phProvider : NCRYPT_PROV_HANDLE* optional, out -> Pointer<UintPtr>
// phKey : NCRYPT_KEY_HANDLE* out -> Pointer<UintPtr>
// hLegacyProv : UINT_PTR -> UintPtr
// hLegacyKey : UINT_PTR optional -> UintPtr
// dwLegacyKeySpec : CERT_KEY_SPEC optional -> Uint32
// dwFlags : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function NCryptTranslateHandle(
  phProvider: Pointer;   // NCRYPT_PROV_HANDLE* optional, out
  phKey: Pointer;   // NCRYPT_KEY_HANDLE* out
  hLegacyProv: NativeUInt;   // UINT_PTR
  hLegacyKey: NativeUInt;   // UINT_PTR optional
  dwLegacyKeySpec: DWORD;   // CERT_KEY_SPEC optional
  dwFlags: DWORD   // DWORD
): Integer; stdcall;
  external 'ncrypt.dll' name 'NCryptTranslateHandle';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "NCryptTranslateHandle"
  c_NCryptTranslateHandle :: Ptr CUIntPtr -> Ptr CUIntPtr -> CUIntPtr -> CUIntPtr -> Word32 -> Word32 -> IO Int32
-- phProvider : NCRYPT_PROV_HANDLE* optional, out -> Ptr CUIntPtr
-- phKey : NCRYPT_KEY_HANDLE* out -> Ptr CUIntPtr
-- hLegacyProv : UINT_PTR -> CUIntPtr
-- hLegacyKey : UINT_PTR optional -> CUIntPtr
-- dwLegacyKeySpec : CERT_KEY_SPEC optional -> Word32
-- dwFlags : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let ncrypttranslatehandle =
  foreign "NCryptTranslateHandle"
    ((ptr size_t) @-> (ptr size_t) @-> size_t @-> size_t @-> uint32_t @-> uint32_t @-> returning int32_t)
(* phProvider : NCRYPT_PROV_HANDLE* optional, out -> (ptr size_t) *)
(* phKey : NCRYPT_KEY_HANDLE* out -> (ptr size_t) *)
(* hLegacyProv : UINT_PTR -> size_t *)
(* hLegacyKey : UINT_PTR optional -> size_t *)
(* dwLegacyKeySpec : CERT_KEY_SPEC optional -> uint32_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 ("NCryptTranslateHandle" ncrypt-translate-handle :convention :stdcall) :int32
  (ph-provider :pointer)   ; NCRYPT_PROV_HANDLE* optional, out
  (ph-key :pointer)   ; NCRYPT_KEY_HANDLE* out
  (h-legacy-prov :uint64)   ; UINT_PTR
  (h-legacy-key :uint64)   ; UINT_PTR optional
  (dw-legacy-key-spec :uint32)   ; CERT_KEY_SPEC optional
  (dw-flags :uint32))   ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $NCryptTranslateHandle = Win32::API::More->new('ncrypt',
    'int NCryptTranslateHandle(LPVOID phProvider, LPVOID phKey, WPARAM hLegacyProv, WPARAM hLegacyKey, DWORD dwLegacyKeySpec, DWORD dwFlags)');
# my $ret = $NCryptTranslateHandle->Call($phProvider, $phKey, $hLegacyProv, $hLegacyKey, $dwLegacyKeySpec, $dwFlags);
# phProvider : NCRYPT_PROV_HANDLE* optional, out -> LPVOID
# phKey : NCRYPT_KEY_HANDLE* out -> LPVOID
# hLegacyProv : UINT_PTR -> WPARAM
# hLegacyKey : UINT_PTR optional -> WPARAM
# dwLegacyKeySpec : CERT_KEY_SPEC optional -> DWORD
# dwFlags : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

使用する型