CryptEncodeObjectEx
関数シグネチャ
// CRYPT32.dll
#include <windows.h>
BOOL CryptEncodeObjectEx(
CERT_QUERY_ENCODING_TYPE dwCertEncodingType,
LPCSTR lpszStructType,
const void* pvStructInfo,
CRYPT_ENCODE_OBJECT_FLAGS dwFlags,
CRYPT_ENCODE_PARA* pEncodePara, // optional
void* pvEncoded, // optional
DWORD* pcbEncoded
);パラメーター
| 名前 | 型 | 方向 | 説明 | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| dwCertEncodingType | CERT_QUERY_ENCODING_TYPE | in | オブジェクトのエンコードに使用する 証明書エンコード型およびメッセージエンコード型です。このパラメーターには、次の値の 1 つ以上を組み合わせて指定できます。
| ||||||||||||||
| lpszStructType | LPCSTR | in | 構造体の型を定義するオブジェクト識別子 (OID) へのポインターです。lpszStructType パラメーターの上位ワードがゼロの場合、下位ワードが指定された構造体の型を表す整数識別子を示します。そうでない場合、このパラメーターは OID の文字列表現を含む null 終端文字列へのポインターです。 オブジェクト識別子文字列、定義済み定数、および対応する構造体の詳細については、 Constants for CryptEncodeObject and CryptDecodeObject を参照してください。 | ||||||||||||||
| pvStructInfo | void* | in | エンコードする構造体へのポインターです。この構造体は lpszStructType で指定された型でなければなりません。 | ||||||||||||||
| dwFlags | CRYPT_ENCODE_OBJECT_FLAGS | in | エンコードのオプションを指定します。このパラメーターには、ゼロ、または次の値の 1 つ以上の組み合わせを指定できます。
| ||||||||||||||
| pEncodePara | CRYPT_ENCODE_PARA* | inoptional | エンコード情報を含む CRYPT_ENCODE_PARA 構造体へのポインターです。このパラメーターは NULL でもかまいません。 pEncodePara または pEncodePara の pfnAlloc メンバーのいずれかが NULL の場合、割り当てには LocalAlloc が使用され、メモリの解放には LocalFree を呼び出す必要があります。 pEncodePara と pEncodePara の pfnAlloc メンバーの両方が NULL でない場合、割り当てには pEncodePara が指す CRYPT_ENCODE_PARA 構造体の pfnAlloc メンバーが指す関数が呼び出されます。メモリの解放には、pEncodePara の pfnFree メンバーが指す関数を呼び出す必要があります。 | ||||||||||||||
| pvEncoded | void* | outoptional | エンコードされた構造体を受け取るバッファーへのポインターです。このバッファーのサイズは pcbEncoded パラメーターで指定します。指定されたバッファーがデコードされた構造体を受け取るのに十分な大きさでない場合、関数は ERROR_MORE_DATA コードを設定し、必要なバッファーサイズ (バイト単位) を pcbEncoded が指す変数に格納します。 このパラメーターは、メモリ割り当てのためにバッファーのサイズを取得する目的で NULL を指定できます。詳細については、 Retrieving Data of Unknown Length を参照してください。 dwFlags に CRYPT_ENCODE_ALLOC_FLAG フラグが含まれている場合、pvEncoded はバッファーへのポインターではなく、バッファーへのポインターのアドレスとなります。メモリは関数内部で割り当てられ、ポインターは pvEncoded に格納されるため、pvEncoded を NULL にすることはできません。 | ||||||||||||||
| pcbEncoded | DWORD* | inout | pvEncoded パラメーターが指すバッファーのサイズ (バイト単位) を格納する DWORD 変数へのポインターです。関数が戻ると、pcbEncoded パラメーターが指す変数には、バッファーに格納された、割り当て済みのエンコードされたバイト数が格納されます。 dwFlags に CRYPT_ENCODE_ALLOC_FLAG フラグが含まれている場合、pcbEncoded は更新される DWORD 値へのポインターのアドレスとなります。 注意 バッファーに返されたデータを処理する際、アプリケーションは実際に返されたデータのサイズを使用する必要があります。実際のサイズは、入力時に指定したバッファーのサイズよりわずかに小さくなる場合があります (入力時には通常、出力データが最大の場合でもバッファーに収まるように十分大きなバッファーサイズが指定されます)。出力時には、このパラメーターが指す変数が、バッファーにコピーされたデータの実際のサイズを反映するように更新されます。
|
戻り値の型: BOOL
公式ドキュメント
lpszStructType パラメーターの値によって示される型の構造体をエンコードします。
戻り値
成功した場合は 0 以外、失敗した場合は 0 を返します。
拡張エラー情報を取得するには、 GetLastError を呼び出します。次の表は、CryptEncodeObjectEx が失敗したときに GetLastError から返される可能性のあるエラーコードの一部を示します。
| 戻りコード | 説明 |
|---|---|
| エンコード中にエラーが発生しました。 | |
| 指定された dwCertEncodingType および lpszStructType に対するエンコード関数が見つかりませんでした。 | |
| pvEncoded パラメーターで指定されたバッファーが、返されるデータを保持するのに十分な大きさでない場合、関数は ERROR_MORE_DATA コードを設定し、必要なバッファーサイズ (バイト単位) を pcbEncoded が指す変数に格納します。 |
関数が失敗した場合、GetLastError は Abstract Syntax Notation One (ASN.1) のエンコード/デコードエラーを返すことがあります。これらのエラーの詳細については、 ASN.1 Encoding/Decoding Return Values を参照してください。
解説(Remarks)
推奨される CryptEncodeObjectEx 関数を使用して暗号化オブジェクトをエンコードする場合、終端の NULL 文字が含まれます。推奨される CryptDecodeObjectEx 関数を使用してデコードする場合、終端の NULL 文字は保持されません。
CryptEncodeObjectEx はまず、インストール可能な拡張エンコード関数を検索します。拡張エンコード関数が見つからない場合は、従来の非拡張のインストール可能な関数が検索されます。
オブジェクトの直接的な IA5String エンコードができない場合、dwFlag パラメーターに CRYPT_ENCODE_ENABLE_PUNYCODE_FLAG 値を設定することで Punycode エンコードを指定できます。CRYPT_ENCODE_ENABLE_PUNYCODE_FLAG フラグを設定した場合の効果は、lpszStructType パラメーターの値で指定される、エンコードされる構造体の型によって異なります。
以下のリストの各定数には、pvStructInfo パラメーターが指す関連する構造体型があります。その指される構造体は、直接的または間接的に CERT_ALT_NAME_ENTRY 構造体への参照を持ちます。
- X509_ALTERNATE_NAME
- szOID_AUTHORITY_INFO_ACCESS
- X509_AUTHORITY_INFO_ACCESS
- X509_AUTHORITY_KEY_ID2
- szOID_AUTHORITY_KEY_IDENTIFIER2
- szOID_CRL_DIST_POINTS
- X509_CRL_DIST_POINTS
- szOID_CROSS_CERT_DIST_POINTS
- X509_CROSS_CERT_DIST_POINTS
- szOID_ISSUER_ALT_NAME
- szOID_ISSUER_ALT_NAME2
- szOID_ISSUING_DIST_POINT
- X509_ISSUING_DIST_POINT
- szOID_NAME_CONSTRAINTS
- X509_NAME_CONSTRAINTS
- szOID_NEXT_UPDATE_LOCATION
- OCSP_REQUEST
- zOID_SUBJECT_ALT_NAME
- szOID_SUBJECT_ALT_NAME2
| dwAltNameChoice | 効果 |
|---|---|
| CERT_ALT_NAME_DNS_NAME | ホスト名に ASCII 文字セット外の Unicode 文字が含まれている場合、ホスト名はまず Punycode でエンコードされ、その後 IA5String 文字列としてエンコードされます。 |
| CERT_ALT_NAME_RFC822_NAME | メールアドレスのホスト名部分に ASCII 文字セット外の Unicode 文字が含まれている場合、メールアドレスのホスト名部分が Punycode でエンコードされます。その結果得られるメールアドレスは、その後 IA5String 文字列としてエンコードされます。 |
| CERT_ALT_NAME_URL | URI のサーバーホスト名に ASCII 文字セット外の Unicode 文字が含まれている場合、URI のホスト名部分が Punycode でエンコードされます。その後、結果として得られる URI がエスケープされ、URL は IA5String 文字列としてエンコードされます。 |
以下のリストの各定数には、pvStructInfo パラメーターが指す関連する構造体型があります。その指される構造体は、直接的または間接的に CERT_HASHED_URL 構造体への参照を持ちます。
- szOID_BIOMETRIC_EXT
- X509_BIOMETRIC_EXT
- szOID_LOGOTYPE_EXT
- X509_LOGOTYPE_EXT
以下のリストの各 X509_UNICODE_NAME 定数には、pvStructInfo パラメーターが指す関連する構造体型があります。
CERT_RDN_ATTR 構造体の pszObjId メンバーが szOID_RSA_emailAddr に設定されており、Value メンバー内のメールアドレスに ASCII 文字セット外の Unicode 文字が含まれている場合、メールアドレスのホスト名部分が Punycode でエンコードされます。その結果得られるメールアドレスは、その後 IA5String 文字列としてエンコードされます。いずれの場合も、ホスト名の Punycode エンコードはラベルごとに実行されます。
例
次の例は、CryptEncodeObjectEx を使用して X509_NAME 構造体を初期化およびエンコードする方法を示します。この例の完全なコンテキストを含む例については、Example C Program: ASN.1 Encoding and Decoding を参照してください。
#include <windows.h>
#include <stdio.h>
#include <Wincrypt.h>
#pragma comment(lib, "crypt32.lib")
#define MY_TYPE (X509_ASN_ENCODING)
void main()
{
//#define moved
//--------------------------------------------------------------------
// Declare and initialize local variables.
char *Cert_Sub_Name ="Test User Name";
//--------------------------------------------------------------------
// Initialize a single RDN structure.
CERT_RDN_ATTR rgNameAttr =
{
"2.5.4.3", // The OID
CERT_RDN_PRINTABLE_STRING, // Type of string
(DWORD)strlen(Cert_Sub_Name)+1, // String length including
// the terminating null character
(BYTE *)Cert_Sub_Name // Pointer to the string
};
//--------------------------------------------------------------------
// Declare and initialize a structure to include
// the array of RDN structures.
CERT_RDN rgRDN[] =
{
1, // The number of elements in the array
&rgNameAttr // Pointer to the array
};
//--------------------------------------------------------------------
// Declare and initialize a CERT_NAME_INFO
// structure that includes a CERT_RND.
CERT_NAME_INFO CertName =
{
1, // The number of elements in the CERT_RND's array
rgRDN
};
//--------------------------------------------------------------------
// Declare additional variables.
DWORD cbEncoded; // Variable to hold the
// length of the encoded string
BYTE *pbEncoded; // Variable to hold a pointer to the
// encoded buffer
//--------------------------------------------------------------------
// Call CrypteEncodeObjectEx to get
// length to allocate for pbEncoded.
if( CryptEncodeObjectEx(
MY_TYPE, // The encoding/decoding type
X509_NAME,
&CertName,
0,
NULL,
NULL,
&cbEncoded)) // Fill in the length needed for
// the encoded buffer.
{
printf("The number of bytes needed is %d \n",cbEncoded);
}
else
{
printf("The first call to the function failed.\n");
exit(1);
}
if( pbEncoded = (BYTE*)malloc(cbEncoded))
{
printf("Memory for pvEncoded has been allocated.\n");
}
else
{
printf("Memory allocation failed.\n");
exit(1);
}
if(CryptEncodeObjectEx(
MY_TYPE,
X509_NAME,
&CertName,
0,
NULL,
pbEncoded,
&cbEncoded))
{
printf("The structure has been encoded.\n");
}
else
{
printf("Encoding failed.");
exit(1);
}
// Free allocated memory when done.
// ...
if(pbEncoded)
{
free(pbEncoded);
}
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// CRYPT32.dll
#include <windows.h>
BOOL CryptEncodeObjectEx(
CERT_QUERY_ENCODING_TYPE dwCertEncodingType,
LPCSTR lpszStructType,
const void* pvStructInfo,
CRYPT_ENCODE_OBJECT_FLAGS dwFlags,
CRYPT_ENCODE_PARA* pEncodePara, // optional
void* pvEncoded, // optional
DWORD* pcbEncoded
);[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("CRYPT32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool CryptEncodeObjectEx(
uint dwCertEncodingType, // CERT_QUERY_ENCODING_TYPE
[MarshalAs(UnmanagedType.LPStr)] string lpszStructType, // LPCSTR
IntPtr pvStructInfo, // void*
uint dwFlags, // CRYPT_ENCODE_OBJECT_FLAGS
IntPtr pEncodePara, // CRYPT_ENCODE_PARA* optional
IntPtr pvEncoded, // void* optional, out
ref uint pcbEncoded // DWORD* in/out
);<DllImport("CRYPT32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function CryptEncodeObjectEx(
dwCertEncodingType As UInteger, ' CERT_QUERY_ENCODING_TYPE
<MarshalAs(UnmanagedType.LPStr)> lpszStructType As String, ' LPCSTR
pvStructInfo As IntPtr, ' void*
dwFlags As UInteger, ' CRYPT_ENCODE_OBJECT_FLAGS
pEncodePara As IntPtr, ' CRYPT_ENCODE_PARA* optional
pvEncoded As IntPtr, ' void* optional, out
ByRef pcbEncoded As UInteger ' DWORD* in/out
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function' dwCertEncodingType : CERT_QUERY_ENCODING_TYPE
' lpszStructType : LPCSTR
' pvStructInfo : void*
' dwFlags : CRYPT_ENCODE_OBJECT_FLAGS
' pEncodePara : CRYPT_ENCODE_PARA* optional
' pvEncoded : void* optional, out
' pcbEncoded : DWORD* in/out
Declare PtrSafe Function CryptEncodeObjectEx Lib "crypt32" ( _
ByVal dwCertEncodingType As Long, _
ByVal lpszStructType As String, _
ByVal pvStructInfo As LongPtr, _
ByVal dwFlags As Long, _
ByVal pEncodePara As LongPtr, _
ByVal pvEncoded As LongPtr, _
ByRef pcbEncoded As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
CryptEncodeObjectEx = ctypes.windll.crypt32.CryptEncodeObjectEx
CryptEncodeObjectEx.restype = wintypes.BOOL
CryptEncodeObjectEx.argtypes = [
wintypes.DWORD, # dwCertEncodingType : CERT_QUERY_ENCODING_TYPE
wintypes.LPCSTR, # lpszStructType : LPCSTR
ctypes.POINTER(None), # pvStructInfo : void*
wintypes.DWORD, # dwFlags : CRYPT_ENCODE_OBJECT_FLAGS
ctypes.c_void_p, # pEncodePara : CRYPT_ENCODE_PARA* optional
ctypes.POINTER(None), # pvEncoded : void* optional, out
ctypes.POINTER(wintypes.DWORD), # pcbEncoded : DWORD* in/out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('CRYPT32.dll')
CryptEncodeObjectEx = Fiddle::Function.new(
lib['CryptEncodeObjectEx'],
[
-Fiddle::TYPE_INT, # dwCertEncodingType : CERT_QUERY_ENCODING_TYPE
Fiddle::TYPE_VOIDP, # lpszStructType : LPCSTR
Fiddle::TYPE_VOIDP, # pvStructInfo : void*
-Fiddle::TYPE_INT, # dwFlags : CRYPT_ENCODE_OBJECT_FLAGS
Fiddle::TYPE_VOIDP, # pEncodePara : CRYPT_ENCODE_PARA* optional
Fiddle::TYPE_VOIDP, # pvEncoded : void* optional, out
Fiddle::TYPE_VOIDP, # pcbEncoded : DWORD* in/out
],
Fiddle::TYPE_INT)#[link(name = "crypt32")]
extern "system" {
fn CryptEncodeObjectEx(
dwCertEncodingType: u32, // CERT_QUERY_ENCODING_TYPE
lpszStructType: *const u8, // LPCSTR
pvStructInfo: *const (), // void*
dwFlags: u32, // CRYPT_ENCODE_OBJECT_FLAGS
pEncodePara: *mut CRYPT_ENCODE_PARA, // CRYPT_ENCODE_PARA* optional
pvEncoded: *mut (), // void* optional, out
pcbEncoded: *mut u32 // DWORD* in/out
) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("CRYPT32.dll", SetLastError = true)]
public static extern bool CryptEncodeObjectEx(uint dwCertEncodingType, [MarshalAs(UnmanagedType.LPStr)] string lpszStructType, IntPtr pvStructInfo, uint dwFlags, IntPtr pEncodePara, IntPtr pvEncoded, ref uint pcbEncoded);
"@
$api = Add-Type -MemberDefinition $sig -Name 'CRYPT32_CryptEncodeObjectEx' -Namespace Win32 -PassThru
# $api::CryptEncodeObjectEx(dwCertEncodingType, lpszStructType, pvStructInfo, dwFlags, pEncodePara, pvEncoded, pcbEncoded)#uselib "CRYPT32.dll"
#func global CryptEncodeObjectEx "CryptEncodeObjectEx" sptr, sptr, sptr, sptr, sptr, sptr, sptr
; CryptEncodeObjectEx dwCertEncodingType, lpszStructType, pvStructInfo, dwFlags, varptr(pEncodePara), pvEncoded, varptr(pcbEncoded) ; 戻り値は stat
; dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> "sptr"
; lpszStructType : LPCSTR -> "sptr"
; pvStructInfo : void* -> "sptr"
; dwFlags : CRYPT_ENCODE_OBJECT_FLAGS -> "sptr"
; pEncodePara : CRYPT_ENCODE_PARA* optional -> "sptr"
; pvEncoded : void* optional, out -> "sptr"
; pcbEncoded : DWORD* in/out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "CRYPT32.dll" #cfunc global CryptEncodeObjectEx "CryptEncodeObjectEx" int, str, sptr, int, var, sptr, var ; res = CryptEncodeObjectEx(dwCertEncodingType, lpszStructType, pvStructInfo, dwFlags, pEncodePara, pvEncoded, pcbEncoded) ; dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> "int" ; lpszStructType : LPCSTR -> "str" ; pvStructInfo : void* -> "sptr" ; dwFlags : CRYPT_ENCODE_OBJECT_FLAGS -> "int" ; pEncodePara : CRYPT_ENCODE_PARA* optional -> "var" ; pvEncoded : void* optional, out -> "sptr" ; pcbEncoded : DWORD* in/out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "CRYPT32.dll" #cfunc global CryptEncodeObjectEx "CryptEncodeObjectEx" int, str, sptr, int, sptr, sptr, sptr ; res = CryptEncodeObjectEx(dwCertEncodingType, lpszStructType, pvStructInfo, dwFlags, varptr(pEncodePara), pvEncoded, varptr(pcbEncoded)) ; dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> "int" ; lpszStructType : LPCSTR -> "str" ; pvStructInfo : void* -> "sptr" ; dwFlags : CRYPT_ENCODE_OBJECT_FLAGS -> "int" ; pEncodePara : CRYPT_ENCODE_PARA* optional -> "sptr" ; pvEncoded : void* optional, out -> "sptr" ; pcbEncoded : DWORD* in/out -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
; BOOL CryptEncodeObjectEx(CERT_QUERY_ENCODING_TYPE dwCertEncodingType, LPCSTR lpszStructType, void* pvStructInfo, CRYPT_ENCODE_OBJECT_FLAGS dwFlags, CRYPT_ENCODE_PARA* pEncodePara, void* pvEncoded, DWORD* pcbEncoded) #uselib "CRYPT32.dll" #cfunc global CryptEncodeObjectEx "CryptEncodeObjectEx" int, str, intptr, int, var, intptr, var ; res = CryptEncodeObjectEx(dwCertEncodingType, lpszStructType, pvStructInfo, dwFlags, pEncodePara, pvEncoded, pcbEncoded) ; dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> "int" ; lpszStructType : LPCSTR -> "str" ; pvStructInfo : void* -> "intptr" ; dwFlags : CRYPT_ENCODE_OBJECT_FLAGS -> "int" ; pEncodePara : CRYPT_ENCODE_PARA* optional -> "var" ; pvEncoded : void* optional, out -> "intptr" ; pcbEncoded : DWORD* in/out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; BOOL CryptEncodeObjectEx(CERT_QUERY_ENCODING_TYPE dwCertEncodingType, LPCSTR lpszStructType, void* pvStructInfo, CRYPT_ENCODE_OBJECT_FLAGS dwFlags, CRYPT_ENCODE_PARA* pEncodePara, void* pvEncoded, DWORD* pcbEncoded) #uselib "CRYPT32.dll" #cfunc global CryptEncodeObjectEx "CryptEncodeObjectEx" int, str, intptr, int, intptr, intptr, intptr ; res = CryptEncodeObjectEx(dwCertEncodingType, lpszStructType, pvStructInfo, dwFlags, varptr(pEncodePara), pvEncoded, varptr(pcbEncoded)) ; dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> "int" ; lpszStructType : LPCSTR -> "str" ; pvStructInfo : void* -> "intptr" ; dwFlags : CRYPT_ENCODE_OBJECT_FLAGS -> "int" ; pEncodePara : CRYPT_ENCODE_PARA* optional -> "intptr" ; pvEncoded : void* optional, out -> "intptr" ; pcbEncoded : DWORD* in/out -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
crypt32 = windows.NewLazySystemDLL("CRYPT32.dll")
procCryptEncodeObjectEx = crypt32.NewProc("CryptEncodeObjectEx")
)
// dwCertEncodingType (CERT_QUERY_ENCODING_TYPE), lpszStructType (LPCSTR), pvStructInfo (void*), dwFlags (CRYPT_ENCODE_OBJECT_FLAGS), pEncodePara (CRYPT_ENCODE_PARA* optional), pvEncoded (void* optional, out), pcbEncoded (DWORD* in/out)
r1, _, err := procCryptEncodeObjectEx.Call(
uintptr(dwCertEncodingType),
uintptr(unsafe.Pointer(windows.BytePtrFromString(lpszStructType))),
uintptr(pvStructInfo),
uintptr(dwFlags),
uintptr(pEncodePara),
uintptr(pvEncoded),
uintptr(pcbEncoded),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // BOOLfunction CryptEncodeObjectEx(
dwCertEncodingType: DWORD; // CERT_QUERY_ENCODING_TYPE
lpszStructType: PAnsiChar; // LPCSTR
pvStructInfo: Pointer; // void*
dwFlags: DWORD; // CRYPT_ENCODE_OBJECT_FLAGS
pEncodePara: Pointer; // CRYPT_ENCODE_PARA* optional
pvEncoded: Pointer; // void* optional, out
pcbEncoded: Pointer // DWORD* in/out
): BOOL; stdcall;
external 'CRYPT32.dll' name 'CryptEncodeObjectEx';result := DllCall("CRYPT32\CryptEncodeObjectEx"
, "UInt", dwCertEncodingType ; CERT_QUERY_ENCODING_TYPE
, "AStr", lpszStructType ; LPCSTR
, "Ptr", pvStructInfo ; void*
, "UInt", dwFlags ; CRYPT_ENCODE_OBJECT_FLAGS
, "Ptr", pEncodePara ; CRYPT_ENCODE_PARA* optional
, "Ptr", pvEncoded ; void* optional, out
, "Ptr", pcbEncoded ; DWORD* in/out
, "Int") ; return: BOOL●CryptEncodeObjectEx(dwCertEncodingType, lpszStructType, pvStructInfo, dwFlags, pEncodePara, pvEncoded, pcbEncoded) = DLL("CRYPT32.dll", "bool CryptEncodeObjectEx(dword, char*, void*, dword, void*, void*, void*)")
# 呼び出し: CryptEncodeObjectEx(dwCertEncodingType, lpszStructType, pvStructInfo, dwFlags, pEncodePara, pvEncoded, pcbEncoded)
# dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> "dword"
# lpszStructType : LPCSTR -> "char*"
# pvStructInfo : void* -> "void*"
# dwFlags : CRYPT_ENCODE_OBJECT_FLAGS -> "dword"
# pEncodePara : CRYPT_ENCODE_PARA* optional -> "void*"
# pvEncoded : void* optional, out -> "void*"
# pcbEncoded : DWORD* in/out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "crypt32" fn CryptEncodeObjectEx(
dwCertEncodingType: u32, // CERT_QUERY_ENCODING_TYPE
lpszStructType: [*c]const u8, // LPCSTR
pvStructInfo: ?*anyopaque, // void*
dwFlags: u32, // CRYPT_ENCODE_OBJECT_FLAGS
pEncodePara: [*c]CRYPT_ENCODE_PARA, // CRYPT_ENCODE_PARA* optional
pvEncoded: ?*anyopaque, // void* optional, out
pcbEncoded: [*c]u32 // DWORD* in/out
) callconv(std.os.windows.WINAPI) i32;proc CryptEncodeObjectEx(
dwCertEncodingType: uint32, # CERT_QUERY_ENCODING_TYPE
lpszStructType: cstring, # LPCSTR
pvStructInfo: pointer, # void*
dwFlags: uint32, # CRYPT_ENCODE_OBJECT_FLAGS
pEncodePara: ptr CRYPT_ENCODE_PARA, # CRYPT_ENCODE_PARA* optional
pvEncoded: pointer, # void* optional, out
pcbEncoded: ptr uint32 # DWORD* in/out
): int32 {.importc: "CryptEncodeObjectEx", stdcall, dynlib: "CRYPT32.dll".}pragma(lib, "crypt32");
extern(Windows)
int CryptEncodeObjectEx(
uint dwCertEncodingType, // CERT_QUERY_ENCODING_TYPE
const(char)* lpszStructType, // LPCSTR
void* pvStructInfo, // void*
uint dwFlags, // CRYPT_ENCODE_OBJECT_FLAGS
CRYPT_ENCODE_PARA* pEncodePara, // CRYPT_ENCODE_PARA* optional
void* pvEncoded, // void* optional, out
uint* pcbEncoded // DWORD* in/out
);ccall((:CryptEncodeObjectEx, "CRYPT32.dll"), stdcall, Int32,
(UInt32, Cstring, Ptr{Cvoid}, UInt32, Ptr{CRYPT_ENCODE_PARA}, Ptr{Cvoid}, Ptr{UInt32}),
dwCertEncodingType, lpszStructType, pvStructInfo, dwFlags, pEncodePara, pvEncoded, pcbEncoded)
# dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> UInt32
# lpszStructType : LPCSTR -> Cstring
# pvStructInfo : void* -> Ptr{Cvoid}
# dwFlags : CRYPT_ENCODE_OBJECT_FLAGS -> UInt32
# pEncodePara : CRYPT_ENCODE_PARA* optional -> Ptr{CRYPT_ENCODE_PARA}
# pvEncoded : void* optional, out -> Ptr{Cvoid}
# pcbEncoded : DWORD* in/out -> Ptr{UInt32}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int32_t CryptEncodeObjectEx(
uint32_t dwCertEncodingType,
const char* lpszStructType,
void* pvStructInfo,
uint32_t dwFlags,
void* pEncodePara,
void* pvEncoded,
uint32_t* pcbEncoded);
]]
local crypt32 = ffi.load("crypt32")
-- crypt32.CryptEncodeObjectEx(dwCertEncodingType, lpszStructType, pvStructInfo, dwFlags, pEncodePara, pvEncoded, pcbEncoded)
-- dwCertEncodingType : CERT_QUERY_ENCODING_TYPE
-- lpszStructType : LPCSTR
-- pvStructInfo : void*
-- dwFlags : CRYPT_ENCODE_OBJECT_FLAGS
-- pEncodePara : CRYPT_ENCODE_PARA* optional
-- pvEncoded : void* optional, out
-- pcbEncoded : DWORD* in/out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('CRYPT32.dll');
const CryptEncodeObjectEx = lib.func('__stdcall', 'CryptEncodeObjectEx', 'int32_t', ['uint32_t', 'str', 'void *', 'uint32_t', 'void *', 'void *', 'uint32_t *']);
// CryptEncodeObjectEx(dwCertEncodingType, lpszStructType, pvStructInfo, dwFlags, pEncodePara, pvEncoded, pcbEncoded)
// dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> 'uint32_t'
// lpszStructType : LPCSTR -> 'str'
// pvStructInfo : void* -> 'void *'
// dwFlags : CRYPT_ENCODE_OBJECT_FLAGS -> 'uint32_t'
// pEncodePara : CRYPT_ENCODE_PARA* optional -> 'void *'
// pvEncoded : void* optional, out -> 'void *'
// pcbEncoded : DWORD* in/out -> 'uint32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("CRYPT32.dll", {
CryptEncodeObjectEx: { parameters: ["u32", "buffer", "pointer", "u32", "pointer", "pointer", "pointer"], result: "i32" },
});
// lib.symbols.CryptEncodeObjectEx(dwCertEncodingType, lpszStructType, pvStructInfo, dwFlags, pEncodePara, pvEncoded, pcbEncoded)
// dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> "u32"
// lpszStructType : LPCSTR -> "buffer"
// pvStructInfo : void* -> "pointer"
// dwFlags : CRYPT_ENCODE_OBJECT_FLAGS -> "u32"
// pEncodePara : CRYPT_ENCODE_PARA* optional -> "pointer"
// pvEncoded : void* optional, out -> "pointer"
// pcbEncoded : DWORD* in/out -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t CryptEncodeObjectEx(
uint32_t dwCertEncodingType,
const char* lpszStructType,
void* pvStructInfo,
uint32_t dwFlags,
void* pEncodePara,
void* pvEncoded,
uint32_t* pcbEncoded);
C, "CRYPT32.dll");
// $ffi->CryptEncodeObjectEx(dwCertEncodingType, lpszStructType, pvStructInfo, dwFlags, pEncodePara, pvEncoded, pcbEncoded);
// dwCertEncodingType : CERT_QUERY_ENCODING_TYPE
// lpszStructType : LPCSTR
// pvStructInfo : void*
// dwFlags : CRYPT_ENCODE_OBJECT_FLAGS
// pEncodePara : CRYPT_ENCODE_PARA* optional
// pvEncoded : void* optional, out
// pcbEncoded : DWORD* in/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 Crypt32 extends StdCallLibrary {
Crypt32 INSTANCE = Native.load("crypt32", Crypt32.class);
boolean CryptEncodeObjectEx(
int dwCertEncodingType, // CERT_QUERY_ENCODING_TYPE
String lpszStructType, // LPCSTR
Pointer pvStructInfo, // void*
int dwFlags, // CRYPT_ENCODE_OBJECT_FLAGS
Pointer pEncodePara, // CRYPT_ENCODE_PARA* optional
Pointer pvEncoded, // void* optional, out
IntByReference pcbEncoded // DWORD* in/out
);
}@[Link("crypt32")]
lib LibCRYPT32
fun CryptEncodeObjectEx = CryptEncodeObjectEx(
dwCertEncodingType : UInt32, # CERT_QUERY_ENCODING_TYPE
lpszStructType : UInt8*, # LPCSTR
pvStructInfo : Void*, # void*
dwFlags : UInt32, # CRYPT_ENCODE_OBJECT_FLAGS
pEncodePara : CRYPT_ENCODE_PARA*, # CRYPT_ENCODE_PARA* optional
pvEncoded : Void*, # void* optional, out
pcbEncoded : UInt32* # DWORD* in/out
) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef CryptEncodeObjectExNative = Int32 Function(Uint32, Pointer<Utf8>, Pointer<Void>, Uint32, Pointer<Void>, Pointer<Void>, Pointer<Uint32>);
typedef CryptEncodeObjectExDart = int Function(int, Pointer<Utf8>, Pointer<Void>, int, Pointer<Void>, Pointer<Void>, Pointer<Uint32>);
final CryptEncodeObjectEx = DynamicLibrary.open('CRYPT32.dll')
.lookupFunction<CryptEncodeObjectExNative, CryptEncodeObjectExDart>('CryptEncodeObjectEx');
// dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> Uint32
// lpszStructType : LPCSTR -> Pointer<Utf8>
// pvStructInfo : void* -> Pointer<Void>
// dwFlags : CRYPT_ENCODE_OBJECT_FLAGS -> Uint32
// pEncodePara : CRYPT_ENCODE_PARA* optional -> Pointer<Void>
// pvEncoded : void* optional, out -> Pointer<Void>
// pcbEncoded : DWORD* in/out -> Pointer<Uint32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function CryptEncodeObjectEx(
dwCertEncodingType: DWORD; // CERT_QUERY_ENCODING_TYPE
lpszStructType: PAnsiChar; // LPCSTR
pvStructInfo: Pointer; // void*
dwFlags: DWORD; // CRYPT_ENCODE_OBJECT_FLAGS
pEncodePara: Pointer; // CRYPT_ENCODE_PARA* optional
pvEncoded: Pointer; // void* optional, out
pcbEncoded: Pointer // DWORD* in/out
): BOOL; stdcall;
external 'CRYPT32.dll' name 'CryptEncodeObjectEx';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "CryptEncodeObjectEx"
c_CryptEncodeObjectEx :: Word32 -> CString -> Ptr () -> Word32 -> Ptr () -> Ptr () -> Ptr Word32 -> IO CInt
-- dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> Word32
-- lpszStructType : LPCSTR -> CString
-- pvStructInfo : void* -> Ptr ()
-- dwFlags : CRYPT_ENCODE_OBJECT_FLAGS -> Word32
-- pEncodePara : CRYPT_ENCODE_PARA* optional -> Ptr ()
-- pvEncoded : void* optional, out -> Ptr ()
-- pcbEncoded : DWORD* in/out -> Ptr Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let cryptencodeobjectex =
foreign "CryptEncodeObjectEx"
(uint32_t @-> string @-> (ptr void) @-> uint32_t @-> (ptr void) @-> (ptr void) @-> (ptr uint32_t) @-> returning int32_t)
(* dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> uint32_t *)
(* lpszStructType : LPCSTR -> string *)
(* pvStructInfo : void* -> (ptr void) *)
(* dwFlags : CRYPT_ENCODE_OBJECT_FLAGS -> uint32_t *)
(* pEncodePara : CRYPT_ENCODE_PARA* optional -> (ptr void) *)
(* pvEncoded : void* optional, out -> (ptr void) *)
(* pcbEncoded : DWORD* in/out -> (ptr uint32_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library crypt32 (t "CRYPT32.dll"))
(cffi:use-foreign-library crypt32)
(cffi:defcfun ("CryptEncodeObjectEx" crypt-encode-object-ex :convention :stdcall) :int32
(dw-cert-encoding-type :uint32) ; CERT_QUERY_ENCODING_TYPE
(lpsz-struct-type :string) ; LPCSTR
(pv-struct-info :pointer) ; void*
(dw-flags :uint32) ; CRYPT_ENCODE_OBJECT_FLAGS
(p-encode-para :pointer) ; CRYPT_ENCODE_PARA* optional
(pv-encoded :pointer) ; void* optional, out
(pcb-encoded :pointer)) ; DWORD* in/out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $CryptEncodeObjectEx = Win32::API::More->new('CRYPT32',
'BOOL CryptEncodeObjectEx(DWORD dwCertEncodingType, LPCSTR lpszStructType, LPVOID pvStructInfo, DWORD dwFlags, LPVOID pEncodePara, LPVOID pvEncoded, LPVOID pcbEncoded)');
# my $ret = $CryptEncodeObjectEx->Call($dwCertEncodingType, $lpszStructType, $pvStructInfo, $dwFlags, $pEncodePara, $pvEncoded, $pcbEncoded);
# dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> DWORD
# lpszStructType : LPCSTR -> LPCSTR
# pvStructInfo : void* -> LPVOID
# dwFlags : CRYPT_ENCODE_OBJECT_FLAGS -> DWORD
# pEncodePara : CRYPT_ENCODE_PARA* optional -> LPVOID
# pvEncoded : void* optional, out -> LPVOID
# pcbEncoded : DWORD* in/out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
- f CryptEncodeObject — 暗号構造体を指定したエンコード形式に符号化する。
- f CryptDecodeObject — 符号化されたデータを暗号構造体に復号する。
- f CryptDecodeObjectEx — 符号化データを構造体に復号する拡張版関数。