ホーム › Security.Cryptography › CryptBinaryToStringW
CryptBinaryToStringW
関数バイナリデータをBase64などのUnicode文字列に変換する。
シグネチャ
// 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 |
| dwFlags | CRYPT_STRING | in |
| pszString | LPWSTR | outoptional |
| pcchString | DWORD* | inout |
戻り値の型: BOOL
各言語での呼び出し定義
// 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 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 版の利用を推奨。