Win32 API 日本語リファレンス
ホームNetworking.Ldap › LdapUnicodeToUTF8

LdapUnicodeToUTF8

関数
Unicode文字列をUTF-8文字列に変換する。
DLLWLDAP32.dll呼出規約cdeclSetLastErrorあり対応OSWindows Vista 以降

シグネチャ

// WLDAP32.dll
#include <windows.h>

INT LdapUnicodeToUTF8(
    LPCWSTR lpSrcStr,
    INT cchSrc,
    LPSTR lpDestStr,
    INT cchDest
);

パラメーター

名前方向
lpSrcStrLPCWSTRin
cchSrcINTin
lpDestStrLPSTRout
cchDestINTin

戻り値の型: INT

各言語での呼び出し定義

// WLDAP32.dll
#include <windows.h>

INT LdapUnicodeToUTF8(
    LPCWSTR lpSrcStr,
    INT cchSrc,
    LPSTR lpDestStr,
    INT cchDest
);
[DllImport("WLDAP32.dll", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
static extern int LdapUnicodeToUTF8(
    [MarshalAs(UnmanagedType.LPWStr)] string lpSrcStr,   // LPCWSTR
    int cchSrc,   // INT
    [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder lpDestStr,   // LPSTR out
    int cchDest   // INT
);
<DllImport("WLDAP32.dll", SetLastError:=True, ExactSpelling:=True, CallingConvention:=CallingConvention.Cdecl)>
Public Shared Function LdapUnicodeToUTF8(
    <MarshalAs(UnmanagedType.LPWStr)> lpSrcStr As String,   ' LPCWSTR
    cchSrc As Integer,   ' INT
    <MarshalAs(UnmanagedType.LPStr)> lpDestStr As System.Text.StringBuilder,   ' LPSTR out
    cchDest As Integer   ' INT
) As Integer
End Function
' lpSrcStr : LPCWSTR
' cchSrc : INT
' lpDestStr : LPSTR out
' cchDest : INT
Declare PtrSafe Function LdapUnicodeToUTF8 Lib "wldap32" ( _
    ByVal lpSrcStr As LongPtr, _
    ByVal cchSrc As Long, _
    ByVal lpDestStr As String, _
    ByVal cchDest As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

LdapUnicodeToUTF8 = ctypes.cdll.wldap32.LdapUnicodeToUTF8
LdapUnicodeToUTF8.restype = ctypes.c_int
LdapUnicodeToUTF8.argtypes = [
    wintypes.LPCWSTR,  # lpSrcStr : LPCWSTR
    ctypes.c_int,  # cchSrc : INT
    wintypes.LPSTR,  # lpDestStr : LPSTR out
    ctypes.c_int,  # cchDest : INT
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('WLDAP32.dll')
LdapUnicodeToUTF8 = Fiddle::Function.new(
  lib['LdapUnicodeToUTF8'],
  [
    Fiddle::TYPE_VOIDP,  # lpSrcStr : LPCWSTR
    Fiddle::TYPE_INT,  # cchSrc : INT
    Fiddle::TYPE_VOIDP,  # lpDestStr : LPSTR out
    Fiddle::TYPE_INT,  # cchDest : INT
  ],
  Fiddle::TYPE_INT, Fiddle::Function::CDECL)
#[link(name = "wldap32")]
extern "C" {
    fn LdapUnicodeToUTF8(
        lpSrcStr: *const u16,  // LPCWSTR
        cchSrc: i32,  // INT
        lpDestStr: *mut u8,  // LPSTR out
        cchDest: i32  // INT
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("WLDAP32.dll", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
public static extern int LdapUnicodeToUTF8([MarshalAs(UnmanagedType.LPWStr)] string lpSrcStr, int cchSrc, [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder lpDestStr, int cchDest);
"@
$api = Add-Type -MemberDefinition $sig -Name 'WLDAP32_LdapUnicodeToUTF8' -Namespace Win32 -PassThru
# $api::LdapUnicodeToUTF8(lpSrcStr, cchSrc, lpDestStr, cchDest)
#uselib "WLDAP32.dll"
#func global LdapUnicodeToUTF8 "LdapUnicodeToUTF8" sptr, sptr, sptr, sptr
; LdapUnicodeToUTF8 lpSrcStr, cchSrc, varptr(lpDestStr), cchDest   ; 戻り値は stat
; lpSrcStr : LPCWSTR -> "sptr"
; cchSrc : INT -> "sptr"
; lpDestStr : LPSTR out -> "sptr"
; cchDest : INT -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "WLDAP32.dll"
#cfunc global LdapUnicodeToUTF8 "LdapUnicodeToUTF8" wstr, int, var, int
; res = LdapUnicodeToUTF8(lpSrcStr, cchSrc, lpDestStr, cchDest)
; lpSrcStr : LPCWSTR -> "wstr"
; cchSrc : INT -> "int"
; lpDestStr : LPSTR out -> "var"
; cchDest : INT -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; INT LdapUnicodeToUTF8(LPCWSTR lpSrcStr, INT cchSrc, LPSTR lpDestStr, INT cchDest)
#uselib "WLDAP32.dll"
#cfunc global LdapUnicodeToUTF8 "LdapUnicodeToUTF8" wstr, int, var, int
; res = LdapUnicodeToUTF8(lpSrcStr, cchSrc, lpDestStr, cchDest)
; lpSrcStr : LPCWSTR -> "wstr"
; cchSrc : INT -> "int"
; lpDestStr : LPSTR out -> "var"
; cchDest : INT -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	wldap32 = windows.NewLazySystemDLL("WLDAP32.dll")
	procLdapUnicodeToUTF8 = wldap32.NewProc("LdapUnicodeToUTF8")
)

// lpSrcStr (LPCWSTR), cchSrc (INT), lpDestStr (LPSTR out), cchDest (INT)
r1, _, err := procLdapUnicodeToUTF8.Call(
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpSrcStr))),
	uintptr(cchSrc),
	uintptr(lpDestStr),
	uintptr(cchDest),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // INT
function LdapUnicodeToUTF8(
  lpSrcStr: PWideChar;   // LPCWSTR
  cchSrc: Integer;   // INT
  lpDestStr: PAnsiChar;   // LPSTR out
  cchDest: Integer   // INT
): Integer; cdecl;
  external 'WLDAP32.dll' name 'LdapUnicodeToUTF8';
result := DllCall("WLDAP32\LdapUnicodeToUTF8"
    , "WStr", lpSrcStr   ; LPCWSTR
    , "Int", cchSrc   ; INT
    , "Ptr", lpDestStr   ; LPSTR out
    , "Int", cchDest   ; INT
    , "Cdecl Int")   ; return: INT
●LdapUnicodeToUTF8(lpSrcStr, cchSrc, lpDestStr, cchDest) = DLL("WLDAP32.dll", "int LdapUnicodeToUTF8(char*, int, char*, int)")
# 呼び出し: LdapUnicodeToUTF8(lpSrcStr, cchSrc, lpDestStr, cchDest)
# lpSrcStr : LPCWSTR -> "char*"
# cchSrc : INT -> "int"
# lpDestStr : LPSTR out -> "char*"
# cchDest : INT -> "int"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※cdecl関数。DLL()宣言はstdcall前提。cdeclは EXEC_PTR(`cdecl`,…) を使用。