NormalizeString
関数シグネチャ
// KERNEL32.dll
#include <windows.h>
INT NormalizeString(
NORM_FORM NormForm,
LPCWSTR lpSrcString,
INT cwSrcLength,
LPWSTR lpDstString, // optional
INT cwDstLength
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| NormForm | NORM_FORM | in | 使用する正規化形式。NORM_FORM は標準的な Unicode 正規化形式を指定します。 |
| lpSrcString | LPCWSTR | in | 正規化されていないソース文字列へのポインター。 |
| cwSrcLength | INT | in | ソース文字列を格納するバッファーの長さ(文字数)。文字列が null 終端であると関数に想定させ、長さを自動的に計算させる場合は、このパラメーターに -1 を設定できます。 |
| lpDstString | LPWSTR | outoptional | 関数が変換先文字列を取得するバッファーへのポインター。あるいは、cwDstLength が 0 に設定されている場合、このパラメーターには NULL を指定します。 Note 終端の null 文字を含めずに入力文字列の長さを明示的に指定した場合、関数は文字列を null 終端しません。出力文字列を null 終端するには、アプリケーションは -1 を指定するか、入力文字列の終端 null 文字を明示的に数に含める必要があります。
|
| cwDstLength | INT | in | 変換先文字列を格納するバッファーの長さ(文字数)。あるいは、変換先バッファーに必要なサイズを関数に返させるために、このパラメーターに 0 を設定することもできます。 |
戻り値の型: INT
公式ドキュメント
テキスト文字列の文字を Unicode 4.0 TR#15 に従って正規化します。詳細については、「Using Unicode Normalization to Represent Strings」を参照してください。
戻り値
変換先バッファー内の正規化された文字列の長さを返します。cwDstLength が 0 に設定されている場合、関数は実際の変換に必要なバッファー長の推定値を返します。
入力バッファー内の文字列が null 終端である場合、または cwSrcLength が -1 の場合、変換先バッファーに書き込まれる文字列は null 終端となり、返される文字列長には終端 null 文字が含まれます。
関数が成功しなかった場合は、0 以下の値を返します。拡張エラー情報を取得するには、アプリケーションは GetLastError を呼び出すことができ、次のいずれかのエラーコードが返されることがあります。
- ERROR_INSUFFICIENT_BUFFER. 指定されたバッファーサイズが十分な大きさでなかったか、誤って NULL に設定されていました。
- ERROR_INVALID_PARAMETER. いずれかのパラメーター値が無効でした。
- ERROR_NO_UNICODE_TRANSLATION. 文字列内に無効な Unicode が見つかりました。戻り値は、入力文字列内のエラー発生位置のインデックスの符号を反転した値です。
- ERROR_SUCCESS. 処理は正常に完了しましたが、結果は得られませんでした。
解説(Remarks)
一部の Unicode 文字には、結合文字や合成 Unicode 文字の集合からなる、複数の等価なバイナリ表現があります。Unicode 標準では、ある文字の等価なバイナリ表現のいずれかが与えられたときに、1 つのバイナリ表現を返す正規化と呼ばれる処理が定義されています。正規化は、異なる規則に従う正規化形式と呼ばれる複数のアルゴリズムで実行できます。これについては、「Using Unicode Normalization to Represent Strings」で説明されています。Win32 と .NET Framework は現在、Unicode Standard Annex #15: Unicode Normalization Forms で定義されている正規化形式 C、D、KC、KD をサポートしています。正規化された文字列は、通常、序数比較で評価されます。
次のコードは、バッファー長の推定値の使用方法を示しています。
const int maxIterations = 10;
LPWSTR strResult = NULL;
HANDLE hHeap = GetProcessHeap();
int iSizeEstimated = NormalizeString(form, strInput, -1, NULL, 0);
for (int i = 0; i < maxIterations; i++)
{
if (strResult)
HeapFree(hHeap, 0, strResult);
strResult = (LPWSTR)HeapAlloc(hHeap, 0, iSizeEstimated * sizeof (WCHAR));
iSizeEstimated = NormalizeString(form, strInput, -1, strResult, iSizeEstimated);
if (iSizeEstimated > 0)
break; // success
if (iSizeEstimated <= 0)
{
DWORD dwError = GetLastError();
if (dwError != ERROR_INSUFFICIENT_BUFFER) break; // Real error, not buffer error
// New guess is negative of the return value.
iSizeEstimated = -iSizeEstimated;
}
}
Windows XP、Windows Server 2003:
サポートされなくなりました。
必要なヘッダーファイルと DLL は、Microsoft Internationalized Domain Name (IDN) Mitigation API の一部であり、現在はダウンロードできません。
Examples
この関数の使用例は、「NLS: Unicode Normalization Sample」にあります。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// KERNEL32.dll
#include <windows.h>
INT NormalizeString(
NORM_FORM NormForm,
LPCWSTR lpSrcString,
INT cwSrcLength,
LPWSTR lpDstString, // optional
INT cwDstLength
);[DllImport("KERNEL32.dll", SetLastError = true, ExactSpelling = true)]
static extern int NormalizeString(
int NormForm, // NORM_FORM
[MarshalAs(UnmanagedType.LPWStr)] string lpSrcString, // LPCWSTR
int cwSrcLength, // INT
[MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder lpDstString, // LPWSTR optional, out
int cwDstLength // INT
);<DllImport("KERNEL32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function NormalizeString(
NormForm As Integer, ' NORM_FORM
<MarshalAs(UnmanagedType.LPWStr)> lpSrcString As String, ' LPCWSTR
cwSrcLength As Integer, ' INT
<MarshalAs(UnmanagedType.LPWStr)> lpDstString As System.Text.StringBuilder, ' LPWSTR optional, out
cwDstLength As Integer ' INT
) As Integer
End Function' NormForm : NORM_FORM
' lpSrcString : LPCWSTR
' cwSrcLength : INT
' lpDstString : LPWSTR optional, out
' cwDstLength : INT
Declare PtrSafe Function NormalizeString Lib "kernel32" ( _
ByVal NormForm As Long, _
ByVal lpSrcString As LongPtr, _
ByVal cwSrcLength As Long, _
ByVal lpDstString As LongPtr, _
ByVal cwDstLength As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
NormalizeString = ctypes.windll.kernel32.NormalizeString
NormalizeString.restype = ctypes.c_int
NormalizeString.argtypes = [
ctypes.c_int, # NormForm : NORM_FORM
wintypes.LPCWSTR, # lpSrcString : LPCWSTR
ctypes.c_int, # cwSrcLength : INT
wintypes.LPWSTR, # lpDstString : LPWSTR optional, out
ctypes.c_int, # cwDstLength : INT
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('KERNEL32.dll')
NormalizeString = Fiddle::Function.new(
lib['NormalizeString'],
[
Fiddle::TYPE_INT, # NormForm : NORM_FORM
Fiddle::TYPE_VOIDP, # lpSrcString : LPCWSTR
Fiddle::TYPE_INT, # cwSrcLength : INT
Fiddle::TYPE_VOIDP, # lpDstString : LPWSTR optional, out
Fiddle::TYPE_INT, # cwDstLength : INT
],
Fiddle::TYPE_INT)#[link(name = "kernel32")]
extern "system" {
fn NormalizeString(
NormForm: i32, // NORM_FORM
lpSrcString: *const u16, // LPCWSTR
cwSrcLength: i32, // INT
lpDstString: *mut u16, // LPWSTR optional, out
cwDstLength: i32 // INT
) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("KERNEL32.dll", SetLastError = true)]
public static extern int NormalizeString(int NormForm, [MarshalAs(UnmanagedType.LPWStr)] string lpSrcString, int cwSrcLength, [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder lpDstString, int cwDstLength);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_NormalizeString' -Namespace Win32 -PassThru
# $api::NormalizeString(NormForm, lpSrcString, cwSrcLength, lpDstString, cwDstLength)#uselib "KERNEL32.dll"
#func global NormalizeString "NormalizeString" sptr, sptr, sptr, sptr, sptr
; NormalizeString NormForm, lpSrcString, cwSrcLength, varptr(lpDstString), cwDstLength ; 戻り値は stat
; NormForm : NORM_FORM -> "sptr"
; lpSrcString : LPCWSTR -> "sptr"
; cwSrcLength : INT -> "sptr"
; lpDstString : LPWSTR optional, out -> "sptr"
; cwDstLength : INT -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "KERNEL32.dll" #cfunc global NormalizeString "NormalizeString" int, wstr, int, var, int ; res = NormalizeString(NormForm, lpSrcString, cwSrcLength, lpDstString, cwDstLength) ; NormForm : NORM_FORM -> "int" ; lpSrcString : LPCWSTR -> "wstr" ; cwSrcLength : INT -> "int" ; lpDstString : LPWSTR optional, out -> "var" ; cwDstLength : INT -> "int" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "KERNEL32.dll" #cfunc global NormalizeString "NormalizeString" int, wstr, int, sptr, int ; res = NormalizeString(NormForm, lpSrcString, cwSrcLength, varptr(lpDstString), cwDstLength) ; NormForm : NORM_FORM -> "int" ; lpSrcString : LPCWSTR -> "wstr" ; cwSrcLength : INT -> "int" ; lpDstString : LPWSTR optional, out -> "sptr" ; cwDstLength : INT -> "int" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
; INT NormalizeString(NORM_FORM NormForm, LPCWSTR lpSrcString, INT cwSrcLength, LPWSTR lpDstString, INT cwDstLength) #uselib "KERNEL32.dll" #cfunc global NormalizeString "NormalizeString" int, wstr, int, var, int ; res = NormalizeString(NormForm, lpSrcString, cwSrcLength, lpDstString, cwDstLength) ; NormForm : NORM_FORM -> "int" ; lpSrcString : LPCWSTR -> "wstr" ; cwSrcLength : INT -> "int" ; lpDstString : LPWSTR optional, out -> "var" ; cwDstLength : INT -> "int" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; INT NormalizeString(NORM_FORM NormForm, LPCWSTR lpSrcString, INT cwSrcLength, LPWSTR lpDstString, INT cwDstLength) #uselib "KERNEL32.dll" #cfunc global NormalizeString "NormalizeString" int, wstr, int, intptr, int ; res = NormalizeString(NormForm, lpSrcString, cwSrcLength, varptr(lpDstString), cwDstLength) ; NormForm : NORM_FORM -> "int" ; lpSrcString : LPCWSTR -> "wstr" ; cwSrcLength : INT -> "int" ; lpDstString : LPWSTR optional, out -> "intptr" ; cwDstLength : INT -> "int" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
procNormalizeString = kernel32.NewProc("NormalizeString")
)
// NormForm (NORM_FORM), lpSrcString (LPCWSTR), cwSrcLength (INT), lpDstString (LPWSTR optional, out), cwDstLength (INT)
r1, _, err := procNormalizeString.Call(
uintptr(NormForm),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpSrcString))),
uintptr(cwSrcLength),
uintptr(lpDstString),
uintptr(cwDstLength),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // INTfunction NormalizeString(
NormForm: Integer; // NORM_FORM
lpSrcString: PWideChar; // LPCWSTR
cwSrcLength: Integer; // INT
lpDstString: PWideChar; // LPWSTR optional, out
cwDstLength: Integer // INT
): Integer; stdcall;
external 'KERNEL32.dll' name 'NormalizeString';result := DllCall("KERNEL32\NormalizeString"
, "Int", NormForm ; NORM_FORM
, "WStr", lpSrcString ; LPCWSTR
, "Int", cwSrcLength ; INT
, "Ptr", lpDstString ; LPWSTR optional, out
, "Int", cwDstLength ; INT
, "Int") ; return: INT●NormalizeString(NormForm, lpSrcString, cwSrcLength, lpDstString, cwDstLength) = DLL("KERNEL32.dll", "int NormalizeString(int, char*, int, char*, int)")
# 呼び出し: NormalizeString(NormForm, lpSrcString, cwSrcLength, lpDstString, cwDstLength)
# NormForm : NORM_FORM -> "int"
# lpSrcString : LPCWSTR -> "char*"
# cwSrcLength : INT -> "int"
# lpDstString : LPWSTR optional, out -> "char*"
# cwDstLength : INT -> "int"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "kernel32" fn NormalizeString(
NormForm: i32, // NORM_FORM
lpSrcString: [*c]const u16, // LPCWSTR
cwSrcLength: i32, // INT
lpDstString: [*c]u16, // LPWSTR optional, out
cwDstLength: i32 // INT
) callconv(std.os.windows.WINAPI) i32;proc NormalizeString(
NormForm: int32, # NORM_FORM
lpSrcString: WideCString, # LPCWSTR
cwSrcLength: int32, # INT
lpDstString: ptr uint16, # LPWSTR optional, out
cwDstLength: int32 # INT
): int32 {.importc: "NormalizeString", stdcall, dynlib: "KERNEL32.dll".}pragma(lib, "kernel32");
extern(Windows)
int NormalizeString(
int NormForm, // NORM_FORM
const(wchar)* lpSrcString, // LPCWSTR
int cwSrcLength, // INT
wchar* lpDstString, // LPWSTR optional, out
int cwDstLength // INT
);ccall((:NormalizeString, "KERNEL32.dll"), stdcall, Int32,
(Int32, Cwstring, Int32, Ptr{UInt16}, Int32),
NormForm, lpSrcString, cwSrcLength, lpDstString, cwDstLength)
# NormForm : NORM_FORM -> Int32
# lpSrcString : LPCWSTR -> Cwstring
# cwSrcLength : INT -> Int32
# lpDstString : LPWSTR optional, out -> Ptr{UInt16}
# cwDstLength : INT -> Int32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int32_t NormalizeString(
int32_t NormForm,
const uint16_t* lpSrcString,
int32_t cwSrcLength,
uint16_t* lpDstString,
int32_t cwDstLength);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.NormalizeString(NormForm, lpSrcString, cwSrcLength, lpDstString, cwDstLength)
-- NormForm : NORM_FORM
-- lpSrcString : LPCWSTR
-- cwSrcLength : INT
-- lpDstString : LPWSTR optional, out
-- cwDstLength : INT
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('KERNEL32.dll');
const NormalizeString = lib.func('__stdcall', 'NormalizeString', 'int32_t', ['int32_t', 'str16', 'int32_t', 'uint16_t *', 'int32_t']);
// NormalizeString(NormForm, lpSrcString, cwSrcLength, lpDstString, cwDstLength)
// NormForm : NORM_FORM -> 'int32_t'
// lpSrcString : LPCWSTR -> 'str16'
// cwSrcLength : INT -> 'int32_t'
// lpDstString : LPWSTR optional, out -> 'uint16_t *'
// cwDstLength : INT -> 'int32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("KERNEL32.dll", {
NormalizeString: { parameters: ["i32", "buffer", "i32", "buffer", "i32"], result: "i32" },
});
// lib.symbols.NormalizeString(NormForm, lpSrcString, cwSrcLength, lpDstString, cwDstLength)
// NormForm : NORM_FORM -> "i32"
// lpSrcString : LPCWSTR -> "buffer"
// cwSrcLength : INT -> "i32"
// lpDstString : LPWSTR optional, out -> "buffer"
// cwDstLength : INT -> "i32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t NormalizeString(
int32_t NormForm,
const uint16_t* lpSrcString,
int32_t cwSrcLength,
uint16_t* lpDstString,
int32_t cwDstLength);
C, "KERNEL32.dll");
// $ffi->NormalizeString(NormForm, lpSrcString, cwSrcLength, lpDstString, cwDstLength);
// NormForm : NORM_FORM
// lpSrcString : LPCWSTR
// cwSrcLength : INT
// lpDstString : LPWSTR optional, out
// cwDstLength : INT
// 構造体/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 Kernel32 extends StdCallLibrary {
Kernel32 INSTANCE = Native.load("kernel32", Kernel32.class);
int NormalizeString(
int NormForm, // NORM_FORM
WString lpSrcString, // LPCWSTR
int cwSrcLength, // INT
char[] lpDstString, // LPWSTR optional, out
int cwDstLength // INT
);
}@[Link("kernel32")]
lib LibKERNEL32
fun NormalizeString = NormalizeString(
NormForm : Int32, # NORM_FORM
lpSrcString : UInt16*, # LPCWSTR
cwSrcLength : Int32, # INT
lpDstString : UInt16*, # LPWSTR optional, out
cwDstLength : Int32 # INT
) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef NormalizeStringNative = Int32 Function(Int32, Pointer<Utf16>, Int32, Pointer<Utf16>, Int32);
typedef NormalizeStringDart = int Function(int, Pointer<Utf16>, int, Pointer<Utf16>, int);
final NormalizeString = DynamicLibrary.open('KERNEL32.dll')
.lookupFunction<NormalizeStringNative, NormalizeStringDart>('NormalizeString');
// NormForm : NORM_FORM -> Int32
// lpSrcString : LPCWSTR -> Pointer<Utf16>
// cwSrcLength : INT -> Int32
// lpDstString : LPWSTR optional, out -> Pointer<Utf16>
// cwDstLength : INT -> Int32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function NormalizeString(
NormForm: Integer; // NORM_FORM
lpSrcString: PWideChar; // LPCWSTR
cwSrcLength: Integer; // INT
lpDstString: PWideChar; // LPWSTR optional, out
cwDstLength: Integer // INT
): Integer; stdcall;
external 'KERNEL32.dll' name 'NormalizeString';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "NormalizeString"
c_NormalizeString :: Int32 -> CWString -> Int32 -> CWString -> Int32 -> IO Int32
-- NormForm : NORM_FORM -> Int32
-- lpSrcString : LPCWSTR -> CWString
-- cwSrcLength : INT -> Int32
-- lpDstString : LPWSTR optional, out -> CWString
-- cwDstLength : INT -> Int32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let normalizestring =
foreign "NormalizeString"
(int32_t @-> (ptr uint16_t) @-> int32_t @-> (ptr uint16_t) @-> int32_t @-> returning int32_t)
(* NormForm : NORM_FORM -> int32_t *)
(* lpSrcString : LPCWSTR -> (ptr uint16_t) *)
(* cwSrcLength : INT -> int32_t *)
(* lpDstString : LPWSTR optional, out -> (ptr uint16_t) *)
(* cwDstLength : INT -> int32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)
(cffi:defcfun ("NormalizeString" normalize-string :convention :stdcall) :int32
(norm-form :int32) ; NORM_FORM
(lp-src-string (:string :encoding :utf-16le)) ; LPCWSTR
(cw-src-length :int32) ; INT
(lp-dst-string :pointer) ; LPWSTR optional, out
(cw-dst-length :int32)) ; INT
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $NormalizeString = Win32::API::More->new('KERNEL32',
'int NormalizeString(int NormForm, LPCWSTR lpSrcString, int cwSrcLength, LPWSTR lpDstString, int cwDstLength)');
# my $ret = $NormalizeString->Call($NormForm, $lpSrcString, $cwSrcLength, $lpDstString, $cwDstLength);
# NormForm : NORM_FORM -> int
# lpSrcString : LPCWSTR -> LPCWSTR
# cwSrcLength : INT -> int
# lpDstString : LPWSTR optional, out -> LPWSTR
# cwDstLength : INT -> int
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
- f IsNormalizedString — Unicode文字列が指定形式で正規化済みか判定する。