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

CryptBinaryToStringA

関数
バイナリデータをBase64などのANSI文字列に変換する。
DLLCRYPT32.dll文字セットANSI (-A)呼出規約winapi対応OSWindows XP 以降

シグネチャ

// CRYPT32.dll  (ANSI / -A)
#include <windows.h>

BOOL CryptBinaryToStringA(
    const BYTE* pbBinary,
    DWORD cbBinary,
    CRYPT_STRING dwFlags,
    LPSTR pszString,   // optional
    DWORD* pcchString
);

パラメーター

名前方向
pbBinaryBYTE*in
cbBinaryDWORDin
dwFlagsCRYPT_STRINGin
pszStringLPSTRoutoptional
pcchStringDWORD*inout

戻り値の型: BOOL

各言語での呼び出し定義

// CRYPT32.dll  (ANSI / -A)
#include <windows.h>

BOOL CryptBinaryToStringA(
    const BYTE* pbBinary,
    DWORD cbBinary,
    CRYPT_STRING dwFlags,
    LPSTR pszString,   // optional
    DWORD* pcchString
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("CRYPT32.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
static extern bool CryptBinaryToStringA(
    IntPtr pbBinary,   // BYTE*
    uint cbBinary,   // DWORD
    uint dwFlags,   // CRYPT_STRING
    [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder pszString,   // LPSTR optional, out
    ref uint pcchString   // DWORD* in/out
);
<DllImport("CRYPT32.dll", CharSet:=CharSet.Ansi, ExactSpelling:=True)>
Public Shared Function CryptBinaryToStringA(
    pbBinary As IntPtr,   ' BYTE*
    cbBinary As UInteger,   ' DWORD
    dwFlags As UInteger,   ' CRYPT_STRING
    <MarshalAs(UnmanagedType.LPStr)> pszString As System.Text.StringBuilder,   ' LPSTR optional, out
    ByRef pcchString As UInteger   ' DWORD* in/out
) As Boolean
End Function
' pbBinary : BYTE*
' cbBinary : DWORD
' dwFlags : CRYPT_STRING
' pszString : LPSTR optional, out
' pcchString : DWORD* in/out
Declare PtrSafe Function CryptBinaryToStringA Lib "crypt32" ( _
    ByVal pbBinary As LongPtr, _
    ByVal cbBinary As Long, _
    ByVal dwFlags As Long, _
    ByVal pszString As String, _
    ByRef pcchString As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

CryptBinaryToStringA = ctypes.windll.crypt32.CryptBinaryToStringA
CryptBinaryToStringA.restype = wintypes.BOOL
CryptBinaryToStringA.argtypes = [
    ctypes.POINTER(ctypes.c_ubyte),  # pbBinary : BYTE*
    wintypes.DWORD,  # cbBinary : DWORD
    wintypes.DWORD,  # dwFlags : CRYPT_STRING
    wintypes.LPSTR,  # pszString : LPSTR optional, out
    ctypes.POINTER(wintypes.DWORD),  # pcchString : DWORD* in/out
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('CRYPT32.dll')
CryptBinaryToStringA = Fiddle::Function.new(
  lib['CryptBinaryToStringA'],
  [
    Fiddle::TYPE_VOIDP,  # pbBinary : BYTE*
    -Fiddle::TYPE_INT,  # cbBinary : DWORD
    -Fiddle::TYPE_INT,  # dwFlags : CRYPT_STRING
    Fiddle::TYPE_VOIDP,  # pszString : LPSTR optional, out
    Fiddle::TYPE_VOIDP,  # pcchString : DWORD* in/out
  ],
  Fiddle::TYPE_INT)
#[link(name = "crypt32")]
extern "system" {
    fn CryptBinaryToStringA(
        pbBinary: *const u8,  // BYTE*
        cbBinary: u32,  // DWORD
        dwFlags: u32,  // CRYPT_STRING
        pszString: *mut u8,  // LPSTR 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.Ansi)]
public static extern bool CryptBinaryToStringA(IntPtr pbBinary, uint cbBinary, uint dwFlags, [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder pszString, ref uint pcchString);
"@
$api = Add-Type -MemberDefinition $sig -Name 'CRYPT32_CryptBinaryToStringA' -Namespace Win32 -PassThru
# $api::CryptBinaryToStringA(pbBinary, cbBinary, dwFlags, pszString, pcchString)
#uselib "CRYPT32.dll"
#func global CryptBinaryToStringA "CryptBinaryToStringA" sptr, sptr, sptr, sptr, sptr
; CryptBinaryToStringA varptr(pbBinary), cbBinary, dwFlags, varptr(pszString), varptr(pcchString)   ; 戻り値は stat
; pbBinary : BYTE* -> "sptr"
; cbBinary : DWORD -> "sptr"
; dwFlags : CRYPT_STRING -> "sptr"
; pszString : LPSTR optional, out -> "sptr"
; pcchString : DWORD* in/out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "CRYPT32.dll"
#cfunc global CryptBinaryToStringA "CryptBinaryToStringA" var, int, int, var, var
; res = CryptBinaryToStringA(pbBinary, cbBinary, dwFlags, pszString, pcchString)
; pbBinary : BYTE* -> "var"
; cbBinary : DWORD -> "int"
; dwFlags : CRYPT_STRING -> "int"
; pszString : LPSTR optional, out -> "var"
; pcchString : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL CryptBinaryToStringA(BYTE* pbBinary, DWORD cbBinary, CRYPT_STRING dwFlags, LPSTR pszString, DWORD* pcchString)
#uselib "CRYPT32.dll"
#cfunc global CryptBinaryToStringA "CryptBinaryToStringA" var, int, int, var, var
; res = CryptBinaryToStringA(pbBinary, cbBinary, dwFlags, pszString, pcchString)
; pbBinary : BYTE* -> "var"
; cbBinary : DWORD -> "int"
; dwFlags : CRYPT_STRING -> "int"
; pszString : LPSTR optional, out -> "var"
; pcchString : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	crypt32 = windows.NewLazySystemDLL("CRYPT32.dll")
	procCryptBinaryToStringA = crypt32.NewProc("CryptBinaryToStringA")
)

// pbBinary (BYTE*), cbBinary (DWORD), dwFlags (CRYPT_STRING), pszString (LPSTR optional, out), pcchString (DWORD* in/out)
r1, _, err := procCryptBinaryToStringA.Call(
	uintptr(pbBinary),
	uintptr(cbBinary),
	uintptr(dwFlags),
	uintptr(pszString),
	uintptr(pcchString),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function CryptBinaryToStringA(
  pbBinary: Pointer;   // BYTE*
  cbBinary: DWORD;   // DWORD
  dwFlags: DWORD;   // CRYPT_STRING
  pszString: PAnsiChar;   // LPSTR optional, out
  pcchString: Pointer   // DWORD* in/out
): BOOL; stdcall;
  external 'CRYPT32.dll' name 'CryptBinaryToStringA';
result := DllCall("CRYPT32\CryptBinaryToStringA"
    , "Ptr", pbBinary   ; BYTE*
    , "UInt", cbBinary   ; DWORD
    , "UInt", dwFlags   ; CRYPT_STRING
    , "Ptr", pszString   ; LPSTR optional, out
    , "Ptr", pcchString   ; DWORD* in/out
    , "Int")   ; return: BOOL
●CryptBinaryToStringA(pbBinary, cbBinary, dwFlags, pszString, pcchString) = DLL("CRYPT32.dll", "bool CryptBinaryToStringA(void*, dword, dword, char*, void*)")
# 呼び出し: CryptBinaryToStringA(pbBinary, cbBinary, dwFlags, pszString, pcchString)
# pbBinary : BYTE* -> "void*"
# cbBinary : DWORD -> "dword"
# dwFlags : CRYPT_STRING -> "dword"
# pszString : LPSTR optional, out -> "char*"
# pcchString : DWORD* in/out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。