ホーム › Networking.Ldap › LdapUnicodeToUTF8
LdapUnicodeToUTF8
関数Unicode文字列をUTF-8文字列に変換する。
シグネチャ
// WLDAP32.dll
#include <windows.h>
INT LdapUnicodeToUTF8(
LPCWSTR lpSrcStr,
INT cchSrc,
LPSTR lpDestStr,
INT cchDest
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| lpSrcStr | LPCWSTR | in | 変換元のUnicode文字列。UTF-8へ変換する入力。 |
| cchSrc | INT | in | lpSrcStrの文字数。-1で自動的にNULL終端まで処理する。 |
| lpDestStr | LPSTR | out | 変換結果のUTF-8バイト列を受け取る出力バッファ。NULLで必要長を取得可。 |
| cchDest | INT | in | lpDestStrバッファのサイズ(バイト数)。0なら必要バイト数のみ返す。 |
戻り値の型: 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 方式にも切替可。#uselib "WLDAP32.dll" #cfunc global LdapUnicodeToUTF8 "LdapUnicodeToUTF8" wstr, int, sptr, int ; res = LdapUnicodeToUTF8(lpSrcStr, cchSrc, varptr(lpDestStr), cchDest) ; lpSrcStr : LPCWSTR -> "wstr" ; cchSrc : INT -> "int" ; lpDestStr : LPSTR out -> "sptr" ; cchDest : INT -> "int" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは 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 方式にも切替可。; INT LdapUnicodeToUTF8(LPCWSTR lpSrcStr, INT cchSrc, LPSTR lpDestStr, INT cchDest) #uselib "WLDAP32.dll" #cfunc global LdapUnicodeToUTF8 "LdapUnicodeToUTF8" wstr, int, intptr, int ; res = LdapUnicodeToUTF8(lpSrcStr, cchSrc, varptr(lpDestStr), cchDest) ; lpSrcStr : LPCWSTR -> "wstr" ; cchSrc : INT -> "int" ; lpDestStr : LPSTR out -> "intptr" ; cchDest : INT -> "int" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは 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 // INTfunction 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`,…) を使用。const std = @import("std");
extern "wldap32" fn LdapUnicodeToUTF8(
lpSrcStr: [*c]const u16, // LPCWSTR
cchSrc: i32, // INT
lpDestStr: [*c]u8, // LPSTR out
cchDest: i32 // INT
) callconv(.c) i32;proc LdapUnicodeToUTF8(
lpSrcStr: WideCString, # LPCWSTR
cchSrc: int32, # INT
lpDestStr: ptr char, # LPSTR out
cchDest: int32 # INT
): int32 {.importc: "LdapUnicodeToUTF8", cdecl, dynlib: "WLDAP32.dll".}pragma(lib, "wldap32");
extern(C)
int LdapUnicodeToUTF8(
const(wchar)* lpSrcStr, // LPCWSTR
int cchSrc, // INT
char* lpDestStr, // LPSTR out
int cchDest // INT
);ccall((:LdapUnicodeToUTF8, "WLDAP32.dll"), Int32,
(Cwstring, Int32, Ptr{UInt8}, Int32),
lpSrcStr, cchSrc, lpDestStr, cchDest)
# lpSrcStr : LPCWSTR -> Cwstring
# cchSrc : INT -> Int32
# lpDestStr : LPSTR out -> Ptr{UInt8}
# cchDest : INT -> Int32local ffi = require("ffi")
ffi.cdef[[
int32_t LdapUnicodeToUTF8(
const uint16_t* lpSrcStr,
int32_t cchSrc,
char* lpDestStr,
int32_t cchDest);
]]
local wldap32 = ffi.load("wldap32")
-- wldap32.LdapUnicodeToUTF8(lpSrcStr, cchSrc, lpDestStr, cchDest)
-- lpSrcStr : LPCWSTR
-- cchSrc : INT
-- lpDestStr : LPSTR out
-- cchDest : INT
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('WLDAP32.dll');
const LdapUnicodeToUTF8 = lib.func('__cdecl', 'LdapUnicodeToUTF8', 'int32_t', ['str16', 'int32_t', 'char *', 'int32_t']);
// LdapUnicodeToUTF8(lpSrcStr, cchSrc, lpDestStr, cchDest)
// lpSrcStr : LPCWSTR -> 'str16'
// cchSrc : INT -> 'int32_t'
// lpDestStr : LPSTR out -> 'char *'
// cchDest : INT -> 'int32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("WLDAP32.dll", {
LdapUnicodeToUTF8: { parameters: ["buffer", "i32", "buffer", "i32"], result: "i32" },
});
// lib.symbols.LdapUnicodeToUTF8(lpSrcStr, cchSrc, lpDestStr, cchDest)
// lpSrcStr : LPCWSTR -> "buffer"
// cchSrc : INT -> "i32"
// lpDestStr : LPSTR out -> "buffer"
// cchDest : INT -> "i32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t LdapUnicodeToUTF8(
const uint16_t* lpSrcStr,
int32_t cchSrc,
char* lpDestStr,
int32_t cchDest);
C, "WLDAP32.dll");
// $ffi->LdapUnicodeToUTF8(lpSrcStr, cchSrc, lpDestStr, cchDest);
// lpSrcStr : LPCWSTR
// cchSrc : INT
// lpDestStr : LPSTR out
// cchDest : INT
// 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。import com.sun.jna.*;
import com.sun.jna.ptr.*;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;
public interface Wldap32 extends Library {
Wldap32 INSTANCE = Native.load("wldap32", Wldap32.class);
int LdapUnicodeToUTF8(
WString lpSrcStr, // LPCWSTR
int cchSrc, // INT
byte[] lpDestStr, // LPSTR out
int cchDest // INT
);
}@[Link("wldap32")]
lib LibWLDAP32
fun LdapUnicodeToUTF8 = LdapUnicodeToUTF8(
lpSrcStr : UInt16*, # LPCWSTR
cchSrc : Int32, # INT
lpDestStr : UInt8*, # LPSTR out
cchDest : Int32 # INT
) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef LdapUnicodeToUTF8Native = Int32 Function(Pointer<Utf16>, Int32, Pointer<Utf8>, Int32);
typedef LdapUnicodeToUTF8Dart = int Function(Pointer<Utf16>, int, Pointer<Utf8>, int);
final LdapUnicodeToUTF8 = DynamicLibrary.open('WLDAP32.dll')
.lookupFunction<LdapUnicodeToUTF8Native, LdapUnicodeToUTF8Dart>('LdapUnicodeToUTF8');
// lpSrcStr : LPCWSTR -> Pointer<Utf16>
// cchSrc : INT -> Int32
// lpDestStr : LPSTR out -> Pointer<Utf8>
// cchDest : INT -> Int32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function LdapUnicodeToUTF8(
lpSrcStr: PWideChar; // LPCWSTR
cchSrc: Integer; // INT
lpDestStr: PAnsiChar; // LPSTR out
cchDest: Integer // INT
): Integer; cdecl;
external 'WLDAP32.dll' name 'LdapUnicodeToUTF8';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import ccall safe "LdapUnicodeToUTF8"
c_LdapUnicodeToUTF8 :: CWString -> Int32 -> CString -> Int32 -> IO Int32
-- lpSrcStr : LPCWSTR -> CWString
-- cchSrc : INT -> Int32
-- lpDestStr : LPSTR out -> CString
-- cchDest : INT -> Int32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let ldapunicodetoutf8 =
foreign "LdapUnicodeToUTF8"
((ptr uint16_t) @-> int32_t @-> string @-> int32_t @-> returning int32_t)
(* lpSrcStr : LPCWSTR -> (ptr uint16_t) *)
(* cchSrc : INT -> int32_t *)
(* lpDestStr : LPSTR out -> string *)
(* cchDest : INT -> int32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library wldap32 (t "WLDAP32.dll"))
(cffi:use-foreign-library wldap32)
(cffi:defcfun ("LdapUnicodeToUTF8" ldap-unicode-to-utf8 :convention :cdecl) :int32
(lp-src-str (:string :encoding :utf-16le)) ; LPCWSTR
(cch-src :int32) ; INT
(lp-dest-str :pointer) ; LPSTR out
(cch-dest :int32)) ; INT
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $LdapUnicodeToUTF8 = Win32::API::More->new('WLDAP32',
'int LdapUnicodeToUTF8(LPCWSTR lpSrcStr, int cchSrc, LPSTR lpDestStr, int cchDest)');
# my $ret = $LdapUnicodeToUTF8->Call($lpSrcStr, $cchSrc, $lpDestStr, $cchDest);
# lpSrcStr : LPCWSTR -> LPCWSTR
# cchSrc : INT -> int
# lpDestStr : LPSTR out -> LPSTR
# cchDest : INT -> int
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。