CryptMsgCalculateEncodedLength
関数シグネチャ
// CRYPT32.dll
#include <windows.h>
DWORD CryptMsgCalculateEncodedLength(
DWORD dwMsgEncodingType,
DWORD dwFlags,
DWORD dwMsgType,
const void* pvMsgEncodeInfo,
LPSTR pszInnerContentObjID, // optional
DWORD cbData
);パラメーター
| 名前 | 型 | 方向 | 説明 | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| dwMsgEncodingType | DWORD | in | 使用するエンコードの種類を指定します。証明書のエンコードの種類とメッセージのエンコードの種類の両方を、次の例のようにビットごとの OR 演算で組み合わせて指定することも常に可能です。 X509_ASN_ENCODING | PKCS_7_ASN_ENCODING 現在定義されているエンコードの種類は次のとおりです。 | ||||||||||||||
| dwFlags | DWORD | in | 現在定義されているフラグを次の表に示します。
| ||||||||||||||
| dwMsgType | DWORD | in | 現在定義されているメッセージの種類を次の表に示します。
| ||||||||||||||
| pvMsgEncodeInfo | void* | in | エンコード対象のデータへのポインター。指し示すデータの型は dwMsgType の値によって異なります。詳細は dwMsgType の表を参照してください。 | ||||||||||||||
| pszInnerContentObjID | LPSTR | inoptional | CryptMsgUpdate に渡すデータが既にエンコードされている状態で CryptMsgCalculateEncodedLength を呼び出す場合は、適切なオブジェクト識別子を pszInnerContentObjID に渡します。pszInnerContentObjID が NULL の場合、内部コンテンツの型は事前にエンコードされていないものとみなされ、オクテット文字列としてエンコードされ、型 CMSG_DATA が与えられます。 ストリーミングを使用している場合、pszInnerContentObjID は NULL または szOID_RSA_data のいずれかである必要があります。 次のアルゴリズムのオブジェクト識別子がよく使用されます。
| ||||||||||||||
| cbData | DWORD | in | コンテンツのサイズ(バイト単位)。 |
戻り値の型: DWORD
公式ドキュメント
メッセージの種類、エンコードパラメーター、およびエンコード対象データの合計長から、エンコードされた暗号化メッセージに必要な最大バイト数を計算します。
戻り値
エンコードされた暗号化メッセージに必要な長さを返します。この長さは正確な長さではない場合がありますが、必要な長さを下回ることはありません。関数が失敗した場合は 0 が返されます。
拡張エラー情報を取得するには、GetLastError 関数を使用します。次の表に、最もよく返されるエラーコードを示します。
| 戻り値コード | 説明 |
|---|---|
| メッセージの種類が無効です。 | |
| 暗号アルゴリズムが不明です。 | |
| 1 つ以上の引数が無効です。 |
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// CRYPT32.dll
#include <windows.h>
DWORD CryptMsgCalculateEncodedLength(
DWORD dwMsgEncodingType,
DWORD dwFlags,
DWORD dwMsgType,
const void* pvMsgEncodeInfo,
LPSTR pszInnerContentObjID, // optional
DWORD cbData
);[DllImport("CRYPT32.dll", SetLastError = true, ExactSpelling = true)]
static extern uint CryptMsgCalculateEncodedLength(
uint dwMsgEncodingType, // DWORD
uint dwFlags, // DWORD
uint dwMsgType, // DWORD
IntPtr pvMsgEncodeInfo, // void*
[MarshalAs(UnmanagedType.LPStr)] string pszInnerContentObjID, // LPSTR optional
uint cbData // DWORD
);<DllImport("CRYPT32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function CryptMsgCalculateEncodedLength(
dwMsgEncodingType As UInteger, ' DWORD
dwFlags As UInteger, ' DWORD
dwMsgType As UInteger, ' DWORD
pvMsgEncodeInfo As IntPtr, ' void*
<MarshalAs(UnmanagedType.LPStr)> pszInnerContentObjID As String, ' LPSTR optional
cbData As UInteger ' DWORD
) As UInteger
End Function' dwMsgEncodingType : DWORD
' dwFlags : DWORD
' dwMsgType : DWORD
' pvMsgEncodeInfo : void*
' pszInnerContentObjID : LPSTR optional
' cbData : DWORD
Declare PtrSafe Function CryptMsgCalculateEncodedLength Lib "crypt32" ( _
ByVal dwMsgEncodingType As Long, _
ByVal dwFlags As Long, _
ByVal dwMsgType As Long, _
ByVal pvMsgEncodeInfo As LongPtr, _
ByVal pszInnerContentObjID As String, _
ByVal cbData As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
CryptMsgCalculateEncodedLength = ctypes.windll.crypt32.CryptMsgCalculateEncodedLength
CryptMsgCalculateEncodedLength.restype = wintypes.DWORD
CryptMsgCalculateEncodedLength.argtypes = [
wintypes.DWORD, # dwMsgEncodingType : DWORD
wintypes.DWORD, # dwFlags : DWORD
wintypes.DWORD, # dwMsgType : DWORD
ctypes.POINTER(None), # pvMsgEncodeInfo : void*
wintypes.LPCSTR, # pszInnerContentObjID : LPSTR optional
wintypes.DWORD, # cbData : DWORD
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('CRYPT32.dll')
CryptMsgCalculateEncodedLength = Fiddle::Function.new(
lib['CryptMsgCalculateEncodedLength'],
[
-Fiddle::TYPE_INT, # dwMsgEncodingType : DWORD
-Fiddle::TYPE_INT, # dwFlags : DWORD
-Fiddle::TYPE_INT, # dwMsgType : DWORD
Fiddle::TYPE_VOIDP, # pvMsgEncodeInfo : void*
Fiddle::TYPE_VOIDP, # pszInnerContentObjID : LPSTR optional
-Fiddle::TYPE_INT, # cbData : DWORD
],
-Fiddle::TYPE_INT)#[link(name = "crypt32")]
extern "system" {
fn CryptMsgCalculateEncodedLength(
dwMsgEncodingType: u32, // DWORD
dwFlags: u32, // DWORD
dwMsgType: u32, // DWORD
pvMsgEncodeInfo: *const (), // void*
pszInnerContentObjID: *mut u8, // LPSTR optional
cbData: u32 // DWORD
) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("CRYPT32.dll", SetLastError = true)]
public static extern uint CryptMsgCalculateEncodedLength(uint dwMsgEncodingType, uint dwFlags, uint dwMsgType, IntPtr pvMsgEncodeInfo, [MarshalAs(UnmanagedType.LPStr)] string pszInnerContentObjID, uint cbData);
"@
$api = Add-Type -MemberDefinition $sig -Name 'CRYPT32_CryptMsgCalculateEncodedLength' -Namespace Win32 -PassThru
# $api::CryptMsgCalculateEncodedLength(dwMsgEncodingType, dwFlags, dwMsgType, pvMsgEncodeInfo, pszInnerContentObjID, cbData)#uselib "CRYPT32.dll"
#func global CryptMsgCalculateEncodedLength "CryptMsgCalculateEncodedLength" sptr, sptr, sptr, sptr, sptr, sptr
; CryptMsgCalculateEncodedLength dwMsgEncodingType, dwFlags, dwMsgType, pvMsgEncodeInfo, pszInnerContentObjID, cbData ; 戻り値は stat
; dwMsgEncodingType : DWORD -> "sptr"
; dwFlags : DWORD -> "sptr"
; dwMsgType : DWORD -> "sptr"
; pvMsgEncodeInfo : void* -> "sptr"
; pszInnerContentObjID : LPSTR optional -> "sptr"
; cbData : DWORD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "CRYPT32.dll"
#cfunc global CryptMsgCalculateEncodedLength "CryptMsgCalculateEncodedLength" int, int, int, sptr, str, int
; res = CryptMsgCalculateEncodedLength(dwMsgEncodingType, dwFlags, dwMsgType, pvMsgEncodeInfo, pszInnerContentObjID, cbData)
; dwMsgEncodingType : DWORD -> "int"
; dwFlags : DWORD -> "int"
; dwMsgType : DWORD -> "int"
; pvMsgEncodeInfo : void* -> "sptr"
; pszInnerContentObjID : LPSTR optional -> "str"
; cbData : DWORD -> "int"; DWORD CryptMsgCalculateEncodedLength(DWORD dwMsgEncodingType, DWORD dwFlags, DWORD dwMsgType, void* pvMsgEncodeInfo, LPSTR pszInnerContentObjID, DWORD cbData)
#uselib "CRYPT32.dll"
#cfunc global CryptMsgCalculateEncodedLength "CryptMsgCalculateEncodedLength" int, int, int, intptr, str, int
; res = CryptMsgCalculateEncodedLength(dwMsgEncodingType, dwFlags, dwMsgType, pvMsgEncodeInfo, pszInnerContentObjID, cbData)
; dwMsgEncodingType : DWORD -> "int"
; dwFlags : DWORD -> "int"
; dwMsgType : DWORD -> "int"
; pvMsgEncodeInfo : void* -> "intptr"
; pszInnerContentObjID : LPSTR optional -> "str"
; cbData : DWORD -> "int"import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
crypt32 = windows.NewLazySystemDLL("CRYPT32.dll")
procCryptMsgCalculateEncodedLength = crypt32.NewProc("CryptMsgCalculateEncodedLength")
)
// dwMsgEncodingType (DWORD), dwFlags (DWORD), dwMsgType (DWORD), pvMsgEncodeInfo (void*), pszInnerContentObjID (LPSTR optional), cbData (DWORD)
r1, _, err := procCryptMsgCalculateEncodedLength.Call(
uintptr(dwMsgEncodingType),
uintptr(dwFlags),
uintptr(dwMsgType),
uintptr(pvMsgEncodeInfo),
uintptr(unsafe.Pointer(windows.BytePtrFromString(pszInnerContentObjID))),
uintptr(cbData),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // DWORDfunction CryptMsgCalculateEncodedLength(
dwMsgEncodingType: DWORD; // DWORD
dwFlags: DWORD; // DWORD
dwMsgType: DWORD; // DWORD
pvMsgEncodeInfo: Pointer; // void*
pszInnerContentObjID: PAnsiChar; // LPSTR optional
cbData: DWORD // DWORD
): DWORD; stdcall;
external 'CRYPT32.dll' name 'CryptMsgCalculateEncodedLength';result := DllCall("CRYPT32\CryptMsgCalculateEncodedLength"
, "UInt", dwMsgEncodingType ; DWORD
, "UInt", dwFlags ; DWORD
, "UInt", dwMsgType ; DWORD
, "Ptr", pvMsgEncodeInfo ; void*
, "AStr", pszInnerContentObjID ; LPSTR optional
, "UInt", cbData ; DWORD
, "UInt") ; return: DWORD●CryptMsgCalculateEncodedLength(dwMsgEncodingType, dwFlags, dwMsgType, pvMsgEncodeInfo, pszInnerContentObjID, cbData) = DLL("CRYPT32.dll", "dword CryptMsgCalculateEncodedLength(dword, dword, dword, void*, char*, dword)")
# 呼び出し: CryptMsgCalculateEncodedLength(dwMsgEncodingType, dwFlags, dwMsgType, pvMsgEncodeInfo, pszInnerContentObjID, cbData)
# dwMsgEncodingType : DWORD -> "dword"
# dwFlags : DWORD -> "dword"
# dwMsgType : DWORD -> "dword"
# pvMsgEncodeInfo : void* -> "void*"
# pszInnerContentObjID : LPSTR optional -> "char*"
# cbData : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "crypt32" fn CryptMsgCalculateEncodedLength(
dwMsgEncodingType: u32, // DWORD
dwFlags: u32, // DWORD
dwMsgType: u32, // DWORD
pvMsgEncodeInfo: ?*anyopaque, // void*
pszInnerContentObjID: [*c]const u8, // LPSTR optional
cbData: u32 // DWORD
) callconv(std.os.windows.WINAPI) u32;proc CryptMsgCalculateEncodedLength(
dwMsgEncodingType: uint32, # DWORD
dwFlags: uint32, # DWORD
dwMsgType: uint32, # DWORD
pvMsgEncodeInfo: pointer, # void*
pszInnerContentObjID: cstring, # LPSTR optional
cbData: uint32 # DWORD
): uint32 {.importc: "CryptMsgCalculateEncodedLength", stdcall, dynlib: "CRYPT32.dll".}pragma(lib, "crypt32");
extern(Windows)
uint CryptMsgCalculateEncodedLength(
uint dwMsgEncodingType, // DWORD
uint dwFlags, // DWORD
uint dwMsgType, // DWORD
void* pvMsgEncodeInfo, // void*
const(char)* pszInnerContentObjID, // LPSTR optional
uint cbData // DWORD
);ccall((:CryptMsgCalculateEncodedLength, "CRYPT32.dll"), stdcall, UInt32,
(UInt32, UInt32, UInt32, Ptr{Cvoid}, Cstring, UInt32),
dwMsgEncodingType, dwFlags, dwMsgType, pvMsgEncodeInfo, pszInnerContentObjID, cbData)
# dwMsgEncodingType : DWORD -> UInt32
# dwFlags : DWORD -> UInt32
# dwMsgType : DWORD -> UInt32
# pvMsgEncodeInfo : void* -> Ptr{Cvoid}
# pszInnerContentObjID : LPSTR optional -> Cstring
# cbData : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
uint32_t CryptMsgCalculateEncodedLength(
uint32_t dwMsgEncodingType,
uint32_t dwFlags,
uint32_t dwMsgType,
void* pvMsgEncodeInfo,
const char* pszInnerContentObjID,
uint32_t cbData);
]]
local crypt32 = ffi.load("crypt32")
-- crypt32.CryptMsgCalculateEncodedLength(dwMsgEncodingType, dwFlags, dwMsgType, pvMsgEncodeInfo, pszInnerContentObjID, cbData)
-- dwMsgEncodingType : DWORD
-- dwFlags : DWORD
-- dwMsgType : DWORD
-- pvMsgEncodeInfo : void*
-- pszInnerContentObjID : LPSTR optional
-- cbData : DWORD
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('CRYPT32.dll');
const CryptMsgCalculateEncodedLength = lib.func('__stdcall', 'CryptMsgCalculateEncodedLength', 'uint32_t', ['uint32_t', 'uint32_t', 'uint32_t', 'void *', 'str', 'uint32_t']);
// CryptMsgCalculateEncodedLength(dwMsgEncodingType, dwFlags, dwMsgType, pvMsgEncodeInfo, pszInnerContentObjID, cbData)
// dwMsgEncodingType : DWORD -> 'uint32_t'
// dwFlags : DWORD -> 'uint32_t'
// dwMsgType : DWORD -> 'uint32_t'
// pvMsgEncodeInfo : void* -> 'void *'
// pszInnerContentObjID : LPSTR optional -> 'str'
// cbData : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("CRYPT32.dll", {
CryptMsgCalculateEncodedLength: { parameters: ["u32", "u32", "u32", "pointer", "buffer", "u32"], result: "u32" },
});
// lib.symbols.CryptMsgCalculateEncodedLength(dwMsgEncodingType, dwFlags, dwMsgType, pvMsgEncodeInfo, pszInnerContentObjID, cbData)
// dwMsgEncodingType : DWORD -> "u32"
// dwFlags : DWORD -> "u32"
// dwMsgType : DWORD -> "u32"
// pvMsgEncodeInfo : void* -> "pointer"
// pszInnerContentObjID : LPSTR optional -> "buffer"
// cbData : DWORD -> "u32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
uint32_t CryptMsgCalculateEncodedLength(
uint32_t dwMsgEncodingType,
uint32_t dwFlags,
uint32_t dwMsgType,
void* pvMsgEncodeInfo,
const char* pszInnerContentObjID,
uint32_t cbData);
C, "CRYPT32.dll");
// $ffi->CryptMsgCalculateEncodedLength(dwMsgEncodingType, dwFlags, dwMsgType, pvMsgEncodeInfo, pszInnerContentObjID, cbData);
// dwMsgEncodingType : DWORD
// dwFlags : DWORD
// dwMsgType : DWORD
// pvMsgEncodeInfo : void*
// pszInnerContentObjID : LPSTR optional
// cbData : 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 Crypt32 extends StdCallLibrary {
Crypt32 INSTANCE = Native.load("crypt32", Crypt32.class);
int CryptMsgCalculateEncodedLength(
int dwMsgEncodingType, // DWORD
int dwFlags, // DWORD
int dwMsgType, // DWORD
Pointer pvMsgEncodeInfo, // void*
String pszInnerContentObjID, // LPSTR optional
int cbData // DWORD
);
}@[Link("crypt32")]
lib LibCRYPT32
fun CryptMsgCalculateEncodedLength = CryptMsgCalculateEncodedLength(
dwMsgEncodingType : UInt32, # DWORD
dwFlags : UInt32, # DWORD
dwMsgType : UInt32, # DWORD
pvMsgEncodeInfo : Void*, # void*
pszInnerContentObjID : UInt8*, # LPSTR optional
cbData : UInt32 # DWORD
) : UInt32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef CryptMsgCalculateEncodedLengthNative = Uint32 Function(Uint32, Uint32, Uint32, Pointer<Void>, Pointer<Utf8>, Uint32);
typedef CryptMsgCalculateEncodedLengthDart = int Function(int, int, int, Pointer<Void>, Pointer<Utf8>, int);
final CryptMsgCalculateEncodedLength = DynamicLibrary.open('CRYPT32.dll')
.lookupFunction<CryptMsgCalculateEncodedLengthNative, CryptMsgCalculateEncodedLengthDart>('CryptMsgCalculateEncodedLength');
// dwMsgEncodingType : DWORD -> Uint32
// dwFlags : DWORD -> Uint32
// dwMsgType : DWORD -> Uint32
// pvMsgEncodeInfo : void* -> Pointer<Void>
// pszInnerContentObjID : LPSTR optional -> Pointer<Utf8>
// cbData : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function CryptMsgCalculateEncodedLength(
dwMsgEncodingType: DWORD; // DWORD
dwFlags: DWORD; // DWORD
dwMsgType: DWORD; // DWORD
pvMsgEncodeInfo: Pointer; // void*
pszInnerContentObjID: PAnsiChar; // LPSTR optional
cbData: DWORD // DWORD
): DWORD; stdcall;
external 'CRYPT32.dll' name 'CryptMsgCalculateEncodedLength';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "CryptMsgCalculateEncodedLength"
c_CryptMsgCalculateEncodedLength :: Word32 -> Word32 -> Word32 -> Ptr () -> CString -> Word32 -> IO Word32
-- dwMsgEncodingType : DWORD -> Word32
-- dwFlags : DWORD -> Word32
-- dwMsgType : DWORD -> Word32
-- pvMsgEncodeInfo : void* -> Ptr ()
-- pszInnerContentObjID : LPSTR optional -> CString
-- cbData : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let cryptmsgcalculateencodedlength =
foreign "CryptMsgCalculateEncodedLength"
(uint32_t @-> uint32_t @-> uint32_t @-> (ptr void) @-> string @-> uint32_t @-> returning uint32_t)
(* dwMsgEncodingType : DWORD -> uint32_t *)
(* dwFlags : DWORD -> uint32_t *)
(* dwMsgType : DWORD -> uint32_t *)
(* pvMsgEncodeInfo : void* -> (ptr void) *)
(* pszInnerContentObjID : LPSTR optional -> string *)
(* cbData : DWORD -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library crypt32 (t "CRYPT32.dll"))
(cffi:use-foreign-library crypt32)
(cffi:defcfun ("CryptMsgCalculateEncodedLength" crypt-msg-calculate-encoded-length :convention :stdcall) :uint32
(dw-msg-encoding-type :uint32) ; DWORD
(dw-flags :uint32) ; DWORD
(dw-msg-type :uint32) ; DWORD
(pv-msg-encode-info :pointer) ; void*
(psz-inner-content-obj-id :string) ; LPSTR optional
(cb-data :uint32)) ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $CryptMsgCalculateEncodedLength = Win32::API::More->new('CRYPT32',
'DWORD CryptMsgCalculateEncodedLength(DWORD dwMsgEncodingType, DWORD dwFlags, DWORD dwMsgType, LPVOID pvMsgEncodeInfo, LPCSTR pszInnerContentObjID, DWORD cbData)');
# my $ret = $CryptMsgCalculateEncodedLength->Call($dwMsgEncodingType, $dwFlags, $dwMsgType, $pvMsgEncodeInfo, $pszInnerContentObjID, $cbData);
# dwMsgEncodingType : DWORD -> DWORD
# dwFlags : DWORD -> DWORD
# dwMsgType : DWORD -> DWORD
# pvMsgEncodeInfo : void* -> LPVOID
# pszInnerContentObjID : LPSTR optional -> LPCSTR
# cbData : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
- f CryptMsgOpenToEncode — エンコード用の暗号メッセージ(PKCS#7/CMS)を開く。