CryptBinaryToStringW
関数シグネチャ
// CRYPT32.dll (Unicode / -W)
#include <windows.h>
BOOL CryptBinaryToStringW(
const BYTE* pbBinary,
DWORD cbBinary,
CRYPT_STRING dwFlags,
LPWSTR pszString, // optional
DWORD* pcchString
);パラメーター
| 名前 | 型 | 方向 | 説明 | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| pbBinary | BYTE* | in | 文字列に変換するバイト配列へのポインター。 | ||||||||||||||||||||||||||||||||
| cbBinary | DWORD | in | pbBinary 配列の要素数。 | ||||||||||||||||||||||||||||||||
| dwFlags | CRYPT_STRING | in | 変換後の書式付き文字列の形式を指定します。このパラメーターには次のいずれかの値を指定できます。
上記の値に加えて、関数の動作を変更するために次の値を 1 つ以上指定できます。
| ||||||||||||||||||||||||||||||||
| pszString | LPWSTR | outoptional | 変換後の文字列を受け取るバッファーへのポインター。返される文字列を格納するために割り当てる必要がある文字数を計算するには、このパラメーターを NULL に設定します。関数は、終端の NULL 文字を含む必要な文字数を、pcchString が指す値に格納します。 | ||||||||||||||||||||||||||||||||
| pcchString | DWORD* | inout | pszString バッファーのサイズ (TCHAR 単位) を格納する DWORD 変数へのポインター。pszString が NULL の場合、関数は返される文字列の長さ (終端の null 文字を含む) を TCHAR 単位で計算し、このパラメーターに返します。pszString が NULL ではなく十分な大きさがある場合、関数はバイナリ データを指定された文字列形式に (終端の null 文字を含めて) 変換しますが、pcchString は終端の null 文字を含まない長さを TCHAR 単位で受け取ります。 |
戻り値の型: BOOL
公式ドキュメント
バイト配列を書式付き文字列に変換します。(Unicode)
戻り値
関数が成功した場合、関数は 0 以外の値 (TRUE) を返します。
関数が失敗した場合は、0 (FALSE) を返します。
解説(Remarks)
CRYPT_STRING_BINARY エンコードが使用される場合を除き、すべての文字列には改行シーケンスが付加されます。既定では、改行シーケンスは CR/LF のペア (0x0D/0x0A) です。dwFlags パラメーターに CRYPT_STRING_NOCR フラグが含まれている場合、改行シーケンスは LF 文字 (0x0A) になります。dwFlags パラメーターに CRYPT_STRING_NOCRLF フラグが含まれている場合、文字列に改行シーケンスは付加されません。
wincrypt.h ヘッダーは、CryptBinaryToString を、UNICODE プリプロセッサ定数の定義に基づいてこの関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして定義します。エンコーディング ニュートラルなエイリアスの使用を、エンコーディング ニュートラルではないコードと混在させると、不一致が生じ、コンパイル エラーまたは実行時エラーの原因となることがあります。詳細については、「関数プロトタイプの規則」を参照してください。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// CRYPT32.dll (Unicode / -W)
#include <windows.h>
BOOL CryptBinaryToStringW(
const BYTE* pbBinary,
DWORD cbBinary,
CRYPT_STRING dwFlags,
LPWSTR pszString, // optional
DWORD* pcchString
);[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("CRYPT32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern bool CryptBinaryToStringW(
IntPtr pbBinary, // BYTE*
uint cbBinary, // DWORD
uint dwFlags, // CRYPT_STRING
[MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszString, // LPWSTR optional, out
ref uint pcchString // DWORD* in/out
);<DllImport("CRYPT32.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function CryptBinaryToStringW(
pbBinary As IntPtr, ' BYTE*
cbBinary As UInteger, ' DWORD
dwFlags As UInteger, ' CRYPT_STRING
<MarshalAs(UnmanagedType.LPWStr)> pszString As System.Text.StringBuilder, ' LPWSTR optional, out
ByRef pcchString As UInteger ' DWORD* in/out
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function' pbBinary : BYTE*
' cbBinary : DWORD
' dwFlags : CRYPT_STRING
' pszString : LPWSTR optional, out
' pcchString : DWORD* in/out
Declare PtrSafe Function CryptBinaryToStringW Lib "crypt32" ( _
ByVal pbBinary As LongPtr, _
ByVal cbBinary As Long, _
ByVal dwFlags As Long, _
ByVal pszString As LongPtr, _
ByRef pcchString As Long) As Long
' Unicode(W): 文字列は ByVal As LongPtr とし StrPtr(unicodeStr) を渡す
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
CryptBinaryToStringW = ctypes.windll.crypt32.CryptBinaryToStringW
CryptBinaryToStringW.restype = wintypes.BOOL
CryptBinaryToStringW.argtypes = [
ctypes.POINTER(ctypes.c_ubyte), # pbBinary : BYTE*
wintypes.DWORD, # cbBinary : DWORD
wintypes.DWORD, # dwFlags : CRYPT_STRING
wintypes.LPWSTR, # pszString : LPWSTR optional, out
ctypes.POINTER(wintypes.DWORD), # pcchString : DWORD* in/out
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('CRYPT32.dll')
CryptBinaryToStringW = Fiddle::Function.new(
lib['CryptBinaryToStringW'],
[
Fiddle::TYPE_VOIDP, # pbBinary : BYTE*
-Fiddle::TYPE_INT, # cbBinary : DWORD
-Fiddle::TYPE_INT, # dwFlags : CRYPT_STRING
Fiddle::TYPE_VOIDP, # pszString : LPWSTR optional, out
Fiddle::TYPE_VOIDP, # pcchString : DWORD* in/out
],
Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"#[link(name = "crypt32")]
extern "system" {
fn CryptBinaryToStringW(
pbBinary: *const u8, // BYTE*
cbBinary: u32, // DWORD
dwFlags: u32, // CRYPT_STRING
pszString: *mut u16, // LPWSTR optional, out
pcchString: *mut u32 // DWORD* in/out
) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("CRYPT32.dll", CharSet = CharSet.Unicode)]
public static extern bool CryptBinaryToStringW(IntPtr pbBinary, uint cbBinary, uint dwFlags, [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszString, ref uint pcchString);
"@
$api = Add-Type -MemberDefinition $sig -Name 'CRYPT32_CryptBinaryToStringW' -Namespace Win32 -PassThru
# $api::CryptBinaryToStringW(pbBinary, cbBinary, dwFlags, pszString, pcchString)#uselib "CRYPT32.dll"
#func global CryptBinaryToStringW "CryptBinaryToStringW" wptr, wptr, wptr, wptr, wptr
; CryptBinaryToStringW varptr(pbBinary), cbBinary, dwFlags, varptr(pszString), varptr(pcchString) ; 戻り値は stat
; pbBinary : BYTE* -> "wptr"
; cbBinary : DWORD -> "wptr"
; dwFlags : CRYPT_STRING -> "wptr"
; pszString : LPWSTR optional, out -> "wptr"
; pcchString : DWORD* in/out -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "CRYPT32.dll" #cfunc global CryptBinaryToStringW "CryptBinaryToStringW" var, int, int, var, var ; res = CryptBinaryToStringW(pbBinary, cbBinary, dwFlags, pszString, pcchString) ; pbBinary : BYTE* -> "var" ; cbBinary : DWORD -> "int" ; dwFlags : CRYPT_STRING -> "int" ; pszString : LPWSTR optional, out -> "var" ; pcchString : DWORD* in/out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "CRYPT32.dll" #cfunc global CryptBinaryToStringW "CryptBinaryToStringW" sptr, int, int, sptr, sptr ; res = CryptBinaryToStringW(varptr(pbBinary), cbBinary, dwFlags, varptr(pszString), varptr(pcchString)) ; pbBinary : BYTE* -> "sptr" ; cbBinary : DWORD -> "int" ; dwFlags : CRYPT_STRING -> "int" ; pszString : LPWSTR optional, out -> "sptr" ; pcchString : DWORD* in/out -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
; BOOL CryptBinaryToStringW(BYTE* pbBinary, DWORD cbBinary, CRYPT_STRING dwFlags, LPWSTR pszString, DWORD* pcchString) #uselib "CRYPT32.dll" #cfunc global CryptBinaryToStringW "CryptBinaryToStringW" var, int, int, var, var ; res = CryptBinaryToStringW(pbBinary, cbBinary, dwFlags, pszString, pcchString) ; pbBinary : BYTE* -> "var" ; cbBinary : DWORD -> "int" ; dwFlags : CRYPT_STRING -> "int" ; pszString : LPWSTR optional, out -> "var" ; pcchString : DWORD* in/out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; BOOL CryptBinaryToStringW(BYTE* pbBinary, DWORD cbBinary, CRYPT_STRING dwFlags, LPWSTR pszString, DWORD* pcchString) #uselib "CRYPT32.dll" #cfunc global CryptBinaryToStringW "CryptBinaryToStringW" intptr, int, int, intptr, intptr ; res = CryptBinaryToStringW(varptr(pbBinary), cbBinary, dwFlags, varptr(pszString), varptr(pcchString)) ; pbBinary : BYTE* -> "intptr" ; cbBinary : DWORD -> "int" ; dwFlags : CRYPT_STRING -> "int" ; pszString : LPWSTR optional, out -> "intptr" ; pcchString : DWORD* in/out -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
crypt32 = windows.NewLazySystemDLL("CRYPT32.dll")
procCryptBinaryToStringW = crypt32.NewProc("CryptBinaryToStringW")
)
// pbBinary (BYTE*), cbBinary (DWORD), dwFlags (CRYPT_STRING), pszString (LPWSTR optional, out), pcchString (DWORD* in/out)
r1, _, err := procCryptBinaryToStringW.Call(
uintptr(pbBinary),
uintptr(cbBinary),
uintptr(dwFlags),
uintptr(pszString),
uintptr(pcchString),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // BOOLfunction CryptBinaryToStringW(
pbBinary: Pointer; // BYTE*
cbBinary: DWORD; // DWORD
dwFlags: DWORD; // CRYPT_STRING
pszString: PWideChar; // LPWSTR optional, out
pcchString: Pointer // DWORD* in/out
): BOOL; stdcall;
external 'CRYPT32.dll' name 'CryptBinaryToStringW';result := DllCall("CRYPT32\CryptBinaryToStringW"
, "Ptr", pbBinary ; BYTE*
, "UInt", cbBinary ; DWORD
, "UInt", dwFlags ; CRYPT_STRING
, "Ptr", pszString ; LPWSTR optional, out
, "Ptr", pcchString ; DWORD* in/out
, "Int") ; return: BOOL●CryptBinaryToStringW(pbBinary, cbBinary, dwFlags, pszString, pcchString) = DLL("CRYPT32.dll", "bool CryptBinaryToStringW(void*, dword, dword, char*, void*)")
# 呼び出し: CryptBinaryToStringW(pbBinary, cbBinary, dwFlags, pszString, pcchString)
# pbBinary : BYTE* -> "void*"
# cbBinary : DWORD -> "dword"
# dwFlags : CRYPT_STRING -> "dword"
# pszString : LPWSTR optional, out -> "char*"
# pcchString : DWORD* in/out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。const std = @import("std");
extern "crypt32" fn CryptBinaryToStringW(
pbBinary: [*c]u8, // BYTE*
cbBinary: u32, // DWORD
dwFlags: u32, // CRYPT_STRING
pszString: [*c]u16, // LPWSTR optional, out
pcchString: [*c]u32 // DWORD* in/out
) callconv(std.os.windows.WINAPI) i32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。proc CryptBinaryToStringW(
pbBinary: ptr uint8, # BYTE*
cbBinary: uint32, # DWORD
dwFlags: uint32, # CRYPT_STRING
pszString: ptr uint16, # LPWSTR optional, out
pcchString: ptr uint32 # DWORD* in/out
): int32 {.importc: "CryptBinaryToStringW", stdcall, dynlib: "CRYPT32.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。pragma(lib, "crypt32");
extern(Windows)
int CryptBinaryToStringW(
ubyte* pbBinary, // BYTE*
uint cbBinary, // DWORD
uint dwFlags, // CRYPT_STRING
wchar* pszString, // LPWSTR optional, out
uint* pcchString // DWORD* in/out
);ccall((:CryptBinaryToStringW, "CRYPT32.dll"), stdcall, Int32,
(Ptr{UInt8}, UInt32, UInt32, Ptr{UInt16}, Ptr{UInt32}),
pbBinary, cbBinary, dwFlags, pszString, pcchString)
# pbBinary : BYTE* -> Ptr{UInt8}
# cbBinary : DWORD -> UInt32
# dwFlags : CRYPT_STRING -> UInt32
# pszString : LPWSTR optional, out -> Ptr{UInt16}
# pcchString : DWORD* in/out -> Ptr{UInt32}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。local ffi = require("ffi")
ffi.cdef[[
int32_t CryptBinaryToStringW(
uint8_t* pbBinary,
uint32_t cbBinary,
uint32_t dwFlags,
uint16_t* pszString,
uint32_t* pcchString);
]]
local crypt32 = ffi.load("crypt32")
-- crypt32.CryptBinaryToStringW(pbBinary, cbBinary, dwFlags, pszString, pcchString)
-- pbBinary : BYTE*
-- cbBinary : DWORD
-- dwFlags : CRYPT_STRING
-- pszString : LPWSTR optional, out
-- pcchString : DWORD* in/out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。const koffi = require('koffi');
const lib = koffi.load('CRYPT32.dll');
const CryptBinaryToStringW = lib.func('__stdcall', 'CryptBinaryToStringW', 'int32_t', ['uint8_t *', 'uint32_t', 'uint32_t', 'uint16_t *', 'uint32_t *']);
// CryptBinaryToStringW(pbBinary, cbBinary, dwFlags, pszString, pcchString)
// pbBinary : BYTE* -> 'uint8_t *'
// cbBinary : DWORD -> 'uint32_t'
// dwFlags : CRYPT_STRING -> 'uint32_t'
// pszString : LPWSTR optional, out -> 'uint16_t *'
// pcchString : DWORD* in/out -> 'uint32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("CRYPT32.dll", {
CryptBinaryToStringW: { parameters: ["pointer", "u32", "u32", "buffer", "pointer"], result: "i32" },
});
// lib.symbols.CryptBinaryToStringW(pbBinary, cbBinary, dwFlags, pszString, pcchString)
// pbBinary : BYTE* -> "pointer"
// cbBinary : DWORD -> "u32"
// dwFlags : CRYPT_STRING -> "u32"
// pszString : LPWSTR optional, out -> "buffer"
// pcchString : DWORD* in/out -> "pointer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t CryptBinaryToStringW(
uint8_t* pbBinary,
uint32_t cbBinary,
uint32_t dwFlags,
uint16_t* pszString,
uint32_t* pcchString);
C, "CRYPT32.dll");
// $ffi->CryptBinaryToStringW(pbBinary, cbBinary, dwFlags, pszString, pcchString);
// pbBinary : BYTE*
// cbBinary : DWORD
// dwFlags : CRYPT_STRING
// pszString : LPWSTR optional, out
// pcchString : 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, W32APIOptions.UNICODE_OPTIONS);
boolean CryptBinaryToStringW(
byte[] pbBinary, // BYTE*
int cbBinary, // DWORD
int dwFlags, // CRYPT_STRING
char[] pszString, // LPWSTR optional, out
IntByReference pcchString // DWORD* in/out
);
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。@[Link("crypt32")]
lib LibCRYPT32
fun CryptBinaryToStringW = CryptBinaryToStringW(
pbBinary : UInt8*, # BYTE*
cbBinary : UInt32, # DWORD
dwFlags : UInt32, # CRYPT_STRING
pszString : UInt16*, # LPWSTR optional, out
pcchString : 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 CryptBinaryToStringWNative = Int32 Function(Pointer<Uint8>, Uint32, Uint32, Pointer<Utf16>, Pointer<Uint32>);
typedef CryptBinaryToStringWDart = int Function(Pointer<Uint8>, int, int, Pointer<Utf16>, Pointer<Uint32>);
final CryptBinaryToStringW = DynamicLibrary.open('CRYPT32.dll')
.lookupFunction<CryptBinaryToStringWNative, CryptBinaryToStringWDart>('CryptBinaryToStringW');
// pbBinary : BYTE* -> Pointer<Uint8>
// cbBinary : DWORD -> Uint32
// dwFlags : CRYPT_STRING -> Uint32
// pszString : LPWSTR optional, out -> Pointer<Utf16>
// pcchString : DWORD* in/out -> Pointer<Uint32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function CryptBinaryToStringW(
pbBinary: Pointer; // BYTE*
cbBinary: DWORD; // DWORD
dwFlags: DWORD; // CRYPT_STRING
pszString: PWideChar; // LPWSTR optional, out
pcchString: Pointer // DWORD* in/out
): BOOL; stdcall;
external 'CRYPT32.dll' name 'CryptBinaryToStringW';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "CryptBinaryToStringW"
c_CryptBinaryToStringW :: Ptr Word8 -> Word32 -> Word32 -> CWString -> Ptr Word32 -> IO CInt
-- pbBinary : BYTE* -> Ptr Word8
-- cbBinary : DWORD -> Word32
-- dwFlags : CRYPT_STRING -> Word32
-- pszString : LPWSTR optional, out -> CWString
-- pcchString : DWORD* in/out -> Ptr Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let cryptbinarytostringw =
foreign "CryptBinaryToStringW"
((ptr uint8_t) @-> uint32_t @-> uint32_t @-> (ptr uint16_t) @-> (ptr uint32_t) @-> returning int32_t)
(* pbBinary : BYTE* -> (ptr uint8_t) *)
(* cbBinary : DWORD -> uint32_t *)
(* dwFlags : CRYPT_STRING -> uint32_t *)
(* pszString : LPWSTR optional, out -> (ptr uint16_t) *)
(* pcchString : 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 ("CryptBinaryToStringW" crypt-binary-to-string-w :convention :stdcall) :int32
(pb-binary :pointer) ; BYTE*
(cb-binary :uint32) ; DWORD
(dw-flags :uint32) ; CRYPT_STRING
(psz-string :pointer) ; LPWSTR optional, out
(pcch-string :pointer)) ; DWORD* in/out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $CryptBinaryToStringW = Win32::API::More->new('CRYPT32',
'BOOL CryptBinaryToStringW(LPVOID pbBinary, DWORD cbBinary, DWORD dwFlags, LPWSTR pszString, LPVOID pcchString)');
# my $ret = $CryptBinaryToStringW->Call($pbBinary, $cbBinary, $dwFlags, $pszString, $pcchString);
# pbBinary : BYTE* -> LPVOID
# cbBinary : DWORD -> DWORD
# dwFlags : CRYPT_STRING -> DWORD
# pszString : LPWSTR optional, out -> LPWSTR
# pcchString : DWORD* in/out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。関連項目
- f CryptBinaryToStringA (ANSI版) — バイナリデータをBase64などのANSI文字列に変換する。
- f CryptStringToBinaryW — Base64などのUnicode文字列をバイナリデータに変換する。