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

CryptGetUserKey

関数
プロバイダーの永続的なユーザー鍵ペアのハンドルを取得する。
DLLADVAPI32.dll呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

BOOL CryptGetUserKey(
    UINT_PTR hProv,
    DWORD dwKeySpec,
    UINT_PTR* phUserKey
);

パラメーター

名前方向説明
hProvUINT_PTRinCryptAcquireContext の呼び出しによって作成された暗号化サービスプロバイダー (CSP) の HCRYPTPROV ハンドル。
dwKeySpecDWORDin

キーコンテナーから使用する秘密鍵を識別します。AT_KEYEXCHANGE または AT_SIGNATURE を指定できます。

さらに、一部のプロバイダーでは、この関数を通じてその他のユーザー固有のキーへのアクセスが許可される場合があります。詳細については、各プロバイダーのドキュメントを参照してください。

phUserKeyUINT_PTR*out取得したキーの HCRYPTKEY ハンドルへのポインター。キーの使用が終了したら、CryptDestroyKey 関数を呼び出してハンドルを削除してください。

戻り値の型: BOOL

公式ドキュメント

ユーザーが持つ 2 つの公開鍵/秘密鍵ペアのいずれか一方のハンドルを取得します。

戻り値

関数が成功した場合、戻り値は 0 以外 (TRUE) になります。

関数が失敗した場合、戻り値は 0 (FALSE) になります。拡張エラー情報を取得するには、GetLastError を呼び出します。

"NTE" で始まるエラーコードは、使用している特定の CSP によって生成されます。考えられるエラーコードの一部を次に示します。

戻り値コード 説明
ERROR_INVALID_HANDLE
いずれかのパラメーターに無効なハンドルが指定されています。
ERROR_INVALID_PARAMETER
いずれかのパラメーターに無効な値が含まれています。これは多くの場合、無効なポインターです。
NTE_BAD_KEY
dwKeySpec パラメーターに無効な値が含まれています。
NTE_BAD_UID
hProv パラメーターに有効なコンテキストハンドルが含まれていません。
NTE_NO_KEY
dwKeySpec パラメーターによって要求されたキーが存在しません。
出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

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

BOOL CryptGetUserKey(
    UINT_PTR hProv,
    DWORD dwKeySpec,
    UINT_PTR* phUserKey
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("ADVAPI32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool CryptGetUserKey(
    UIntPtr hProv,   // UINT_PTR
    uint dwKeySpec,   // DWORD
    out UIntPtr phUserKey   // UINT_PTR* out
);
<DllImport("ADVAPI32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function CryptGetUserKey(
    hProv As UIntPtr,   ' UINT_PTR
    dwKeySpec As UInteger,   ' DWORD
    <Out> ByRef phUserKey As UIntPtr   ' UINT_PTR* out
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' hProv : UINT_PTR
' dwKeySpec : DWORD
' phUserKey : UINT_PTR* out
Declare PtrSafe Function CryptGetUserKey Lib "advapi32" ( _
    ByVal hProv As LongPtr, _
    ByVal dwKeySpec As Long, _
    ByRef phUserKey As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

CryptGetUserKey = ctypes.windll.advapi32.CryptGetUserKey
CryptGetUserKey.restype = wintypes.BOOL
CryptGetUserKey.argtypes = [
    ctypes.c_size_t,  # hProv : UINT_PTR
    wintypes.DWORD,  # dwKeySpec : DWORD
    ctypes.POINTER(ctypes.c_size_t),  # phUserKey : UINT_PTR* out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

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

var (
	advapi32 = windows.NewLazySystemDLL("ADVAPI32.dll")
	procCryptGetUserKey = advapi32.NewProc("CryptGetUserKey")
)

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

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

typedef CryptGetUserKeyNative = Int32 Function(UintPtr, Uint32, Pointer<UintPtr>);
typedef CryptGetUserKeyDart = int Function(int, int, Pointer<UintPtr>);
final CryptGetUserKey = DynamicLibrary.open('ADVAPI32.dll')
    .lookupFunction<CryptGetUserKeyNative, CryptGetUserKeyDart>('CryptGetUserKey');
// hProv : UINT_PTR -> UintPtr
// dwKeySpec : DWORD -> Uint32
// phUserKey : UINT_PTR* out -> Pointer<UintPtr>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function CryptGetUserKey(
  hProv: NativeUInt;   // UINT_PTR
  dwKeySpec: DWORD;   // DWORD
  phUserKey: Pointer   // UINT_PTR* out
): BOOL; stdcall;
  external 'ADVAPI32.dll' name 'CryptGetUserKey';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "CryptGetUserKey"
  c_CryptGetUserKey :: CUIntPtr -> Word32 -> Ptr CUIntPtr -> IO CInt
-- hProv : UINT_PTR -> CUIntPtr
-- dwKeySpec : DWORD -> Word32
-- phUserKey : UINT_PTR* out -> Ptr CUIntPtr
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let cryptgetuserkey =
  foreign "CryptGetUserKey"
    (size_t @-> uint32_t @-> (ptr size_t) @-> returning int32_t)
(* hProv : UINT_PTR -> size_t *)
(* dwKeySpec : DWORD -> uint32_t *)
(* phUserKey : UINT_PTR* out -> (ptr size_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library advapi32 (t "ADVAPI32.dll"))
(cffi:use-foreign-library advapi32)

(cffi:defcfun ("CryptGetUserKey" crypt-get-user-key :convention :stdcall) :int32
  (h-prov :uint64)   ; UINT_PTR
  (dw-key-spec :uint32)   ; DWORD
  (ph-user-key :pointer))   ; UINT_PTR* out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $CryptGetUserKey = Win32::API::More->new('ADVAPI32',
    'BOOL CryptGetUserKey(WPARAM hProv, DWORD dwKeySpec, LPVOID phUserKey)');
# my $ret = $CryptGetUserKey->Call($hProv, $dwKeySpec, $phUserKey);
# hProv : UINT_PTR -> WPARAM
# dwKeySpec : DWORD -> DWORD
# phUserKey : UINT_PTR* out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

公式の関連項目