CompareStringEx
関数シグネチャ
// KERNEL32.dll
#include <windows.h>
COMPARESTRING_RESULT CompareStringEx(
LPCWSTR lpLocaleName, // optional
COMPARE_STRING_FLAGS dwCmpFlags,
LPCWSTR lpString1,
INT cchCount1,
LPCWSTR lpString2,
INT cchCount2,
NLSVERSIONINFO* lpVersionInformation, // optional
void* lpReserved, // optional
LPARAM lParam // optional
);パラメーター
| 名前 | 型 | 方向 | 説明 | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| lpLocaleName | LPCWSTR | inoptional | ロケール名へのポインター、または以下の定義済みの値のいずれか。 | ||||||||||||||||||||||
| dwCmpFlags | COMPARE_STRING_FLAGS | in | 関数が 2 つの文字列をどのように比較するかを示すフラグです。既定では、これらのフラグは設定されません。このパラメーターには、以下の値の任意の組み合わせを指定できます。既定の動作にするには 0 を設定します。
| ||||||||||||||||||||||
| lpString1 | LPCWSTR | in | 比較する最初の文字列へのポインター。 | ||||||||||||||||||||||
| cchCount1 | INT | in | lpString1 で示される文字列の長さ (終端の null 文字を除く)。文字列が null で終端されている場合、アプリケーションは負の値を指定できます。この場合、関数が長さを自動的に判別します。 | ||||||||||||||||||||||
| lpString2 | LPCWSTR | in | 比較する 2 番目の文字列へのポインター。 | ||||||||||||||||||||||
| cchCount2 | INT | in | lpString2 で示される文字列の長さ (終端の null 文字を除く)。文字列が null で終端されている場合、アプリケーションは負の値を指定できます。この場合、関数が長さを自動的に判別します。 | ||||||||||||||||||||||
| lpVersionInformation | NLSVERSIONINFO* | optional | 関連する NLS 機能に関するバージョン情報を含む NLSVERSIONINFOEX 構造体へのポインター。通常は GetNLSVersionEx から取得します。 Windows Vista、Windows 7: 予約済み。NULL を設定する必要があります。 | ||||||||||||||||||||||
| lpReserved | void* | optional | 予約済み。NULL を設定する必要があります。 | ||||||||||||||||||||||
| lParam | LPARAM | optional | 予約済み。0 を設定する必要があります。 |
戻り値の型: COMPARESTRING_RESULT
公式ドキュメント
名前で指定されたロケールに従って、2 つの Unicode (ワイド文字) 文字列を比較します。注意 CompareStringEx を誤って使用すると、アプリケーションのセキュリティが損なわれるおそれがあります。
戻り値
成功した場合、以下の値のいずれかを返します。文字列比較における C ランタイムの慣例を維持するために、0 以外の戻り値から 2 を減算できます。そうすると、<0、==0、>0 の意味が C ランタイムと一致します。
- CSTR_LESS_THAN。lpString1 で示される文字列は、lpString2 で示される文字列より辞書順の値が小さいことを示します。
- CSTR_EQUAL。lpString1 で示される文字列は、lpString2 で示される文字列と辞書順の値が等価であることを示します。2 つの文字列はソートの目的上は等価ですが、必ずしも同一とは限りません。
- CSTR_GREATER_THAN。lpString1 で示される文字列は、lpString2 で示される文字列より辞書順の値が大きいことを示します。
- ERROR_INVALID_FLAGS。フラグに指定された値が無効でした。
- ERROR_INVALID_PARAMETER。いずれかのパラメーター値が無効でした。
解説(Remarks)
CompareString と CompareStringEx はいずれも、dwCmpFlags が 0 または NORM_IGNORECASE に設定され、cchCount1 と cchCount2 が -1 に設定され、かつ伝統的なスペイン語のソートが "ch" を 1 文字として扱うような言語的な圧縮をロケールがサポートしていない場合に、最高速で実行されるよう最適化されています。
CompareString と CompareStringEx はいずれも、比較中にアラビア語のカシダ (kashida) を無視します。したがって、2 つの文字列がカシダの有無を除いて同一である場合、関数は CSTR_EQUAL を返します。
アプリケーションがソート関数で NORM_IGNORENONSPACE フラグと NORM_IGNORECASE フラグを使用すると、これらのフラグが文字列比較を妨げることがあります。この状況は、非スペーシング文字や大文字小文字をサポートしていないが、他の重要な操作を処理するために同等の重みレベルを使用するロケールで発生する可能性があります。このような場合、アプリケーションは LINGUISTIC_IGNOREDIACRITIC フラグと LINGUISTIC_IGNORECASE フラグを使用する必要があります。これらのフラグは、大文字小文字やダイアクリティカルマークを使用するコードポイントのソートに対して言語的に適切な結果を提供し、他のコードポイントには影響を与えません。
Windows Vista 以降: CompareString と CompareStringEx はいずれも、カスタムロケールからデータを取得できます。データは、コンピューター間やアプリケーションの実行間で同じであることは保証されません。アプリケーションがデータを永続化または送信する必要がある場合は、永続的なロケールデータの使用を参照してください。
Windows 8 以降: アプリが Windows.Globalization 名前空間の言語タグをこの関数に渡す場合は、まず ResolveLocaleName を呼び出してタグを変換する必要があります。
Windows 8 以降: CompareStringEx は Stringapiset.h で宣言されています。Windows 8 より前では、Winnls.h で宣言されていました。
例
この関数の使用例は、NLS: Name-based APIs Sample にあります。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// KERNEL32.dll
#include <windows.h>
COMPARESTRING_RESULT CompareStringEx(
LPCWSTR lpLocaleName, // optional
COMPARE_STRING_FLAGS dwCmpFlags,
LPCWSTR lpString1,
INT cchCount1,
LPCWSTR lpString2,
INT cchCount2,
NLSVERSIONINFO* lpVersionInformation, // optional
void* lpReserved, // optional
LPARAM lParam // optional
);[DllImport("KERNEL32.dll", SetLastError = true, ExactSpelling = true)]
static extern int CompareStringEx(
[MarshalAs(UnmanagedType.LPWStr)] string lpLocaleName, // LPCWSTR optional
uint dwCmpFlags, // COMPARE_STRING_FLAGS
[MarshalAs(UnmanagedType.LPWStr)] string lpString1, // LPCWSTR
int cchCount1, // INT
[MarshalAs(UnmanagedType.LPWStr)] string lpString2, // LPCWSTR
int cchCount2, // INT
IntPtr lpVersionInformation, // NLSVERSIONINFO* optional
IntPtr lpReserved, // void* optional
IntPtr lParam // LPARAM optional
);<DllImport("KERNEL32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function CompareStringEx(
<MarshalAs(UnmanagedType.LPWStr)> lpLocaleName As String, ' LPCWSTR optional
dwCmpFlags As UInteger, ' COMPARE_STRING_FLAGS
<MarshalAs(UnmanagedType.LPWStr)> lpString1 As String, ' LPCWSTR
cchCount1 As Integer, ' INT
<MarshalAs(UnmanagedType.LPWStr)> lpString2 As String, ' LPCWSTR
cchCount2 As Integer, ' INT
lpVersionInformation As IntPtr, ' NLSVERSIONINFO* optional
lpReserved As IntPtr, ' void* optional
lParam As IntPtr ' LPARAM optional
) As Integer
End Function' lpLocaleName : LPCWSTR optional
' dwCmpFlags : COMPARE_STRING_FLAGS
' lpString1 : LPCWSTR
' cchCount1 : INT
' lpString2 : LPCWSTR
' cchCount2 : INT
' lpVersionInformation : NLSVERSIONINFO* optional
' lpReserved : void* optional
' lParam : LPARAM optional
Declare PtrSafe Function CompareStringEx Lib "kernel32" ( _
ByVal lpLocaleName As LongPtr, _
ByVal dwCmpFlags As Long, _
ByVal lpString1 As LongPtr, _
ByVal cchCount1 As Long, _
ByVal lpString2 As LongPtr, _
ByVal cchCount2 As Long, _
ByVal lpVersionInformation As LongPtr, _
ByVal lpReserved As LongPtr, _
ByVal lParam As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
CompareStringEx = ctypes.windll.kernel32.CompareStringEx
CompareStringEx.restype = ctypes.c_int
CompareStringEx.argtypes = [
wintypes.LPCWSTR, # lpLocaleName : LPCWSTR optional
wintypes.DWORD, # dwCmpFlags : COMPARE_STRING_FLAGS
wintypes.LPCWSTR, # lpString1 : LPCWSTR
ctypes.c_int, # cchCount1 : INT
wintypes.LPCWSTR, # lpString2 : LPCWSTR
ctypes.c_int, # cchCount2 : INT
ctypes.c_void_p, # lpVersionInformation : NLSVERSIONINFO* optional
ctypes.POINTER(None), # lpReserved : void* optional
ctypes.c_ssize_t, # lParam : LPARAM optional
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('KERNEL32.dll')
CompareStringEx = Fiddle::Function.new(
lib['CompareStringEx'],
[
Fiddle::TYPE_VOIDP, # lpLocaleName : LPCWSTR optional
-Fiddle::TYPE_INT, # dwCmpFlags : COMPARE_STRING_FLAGS
Fiddle::TYPE_VOIDP, # lpString1 : LPCWSTR
Fiddle::TYPE_INT, # cchCount1 : INT
Fiddle::TYPE_VOIDP, # lpString2 : LPCWSTR
Fiddle::TYPE_INT, # cchCount2 : INT
Fiddle::TYPE_VOIDP, # lpVersionInformation : NLSVERSIONINFO* optional
Fiddle::TYPE_VOIDP, # lpReserved : void* optional
Fiddle::TYPE_INTPTR_T, # lParam : LPARAM optional
],
Fiddle::TYPE_INT)#[link(name = "kernel32")]
extern "system" {
fn CompareStringEx(
lpLocaleName: *const u16, // LPCWSTR optional
dwCmpFlags: u32, // COMPARE_STRING_FLAGS
lpString1: *const u16, // LPCWSTR
cchCount1: i32, // INT
lpString2: *const u16, // LPCWSTR
cchCount2: i32, // INT
lpVersionInformation: *mut NLSVERSIONINFO, // NLSVERSIONINFO* optional
lpReserved: *mut (), // void* optional
lParam: isize // LPARAM optional
) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("KERNEL32.dll", SetLastError = true)]
public static extern int CompareStringEx([MarshalAs(UnmanagedType.LPWStr)] string lpLocaleName, uint dwCmpFlags, [MarshalAs(UnmanagedType.LPWStr)] string lpString1, int cchCount1, [MarshalAs(UnmanagedType.LPWStr)] string lpString2, int cchCount2, IntPtr lpVersionInformation, IntPtr lpReserved, IntPtr lParam);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_CompareStringEx' -Namespace Win32 -PassThru
# $api::CompareStringEx(lpLocaleName, dwCmpFlags, lpString1, cchCount1, lpString2, cchCount2, lpVersionInformation, lpReserved, lParam)#uselib "KERNEL32.dll"
#func global CompareStringEx "CompareStringEx" sptr, sptr, sptr, sptr, sptr, sptr, sptr, sptr, sptr
; CompareStringEx lpLocaleName, dwCmpFlags, lpString1, cchCount1, lpString2, cchCount2, varptr(lpVersionInformation), lpReserved, lParam ; 戻り値は stat
; lpLocaleName : LPCWSTR optional -> "sptr"
; dwCmpFlags : COMPARE_STRING_FLAGS -> "sptr"
; lpString1 : LPCWSTR -> "sptr"
; cchCount1 : INT -> "sptr"
; lpString2 : LPCWSTR -> "sptr"
; cchCount2 : INT -> "sptr"
; lpVersionInformation : NLSVERSIONINFO* optional -> "sptr"
; lpReserved : void* optional -> "sptr"
; lParam : LPARAM optional -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "KERNEL32.dll" #cfunc global CompareStringEx "CompareStringEx" wstr, int, wstr, int, wstr, int, var, sptr, sptr ; res = CompareStringEx(lpLocaleName, dwCmpFlags, lpString1, cchCount1, lpString2, cchCount2, lpVersionInformation, lpReserved, lParam) ; lpLocaleName : LPCWSTR optional -> "wstr" ; dwCmpFlags : COMPARE_STRING_FLAGS -> "int" ; lpString1 : LPCWSTR -> "wstr" ; cchCount1 : INT -> "int" ; lpString2 : LPCWSTR -> "wstr" ; cchCount2 : INT -> "int" ; lpVersionInformation : NLSVERSIONINFO* optional -> "var" ; lpReserved : void* optional -> "sptr" ; lParam : LPARAM optional -> "sptr" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "KERNEL32.dll" #cfunc global CompareStringEx "CompareStringEx" wstr, int, wstr, int, wstr, int, sptr, sptr, sptr ; res = CompareStringEx(lpLocaleName, dwCmpFlags, lpString1, cchCount1, lpString2, cchCount2, varptr(lpVersionInformation), lpReserved, lParam) ; lpLocaleName : LPCWSTR optional -> "wstr" ; dwCmpFlags : COMPARE_STRING_FLAGS -> "int" ; lpString1 : LPCWSTR -> "wstr" ; cchCount1 : INT -> "int" ; lpString2 : LPCWSTR -> "wstr" ; cchCount2 : INT -> "int" ; lpVersionInformation : NLSVERSIONINFO* optional -> "sptr" ; lpReserved : void* optional -> "sptr" ; lParam : LPARAM optional -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
; COMPARESTRING_RESULT CompareStringEx(LPCWSTR lpLocaleName, COMPARE_STRING_FLAGS dwCmpFlags, LPCWSTR lpString1, INT cchCount1, LPCWSTR lpString2, INT cchCount2, NLSVERSIONINFO* lpVersionInformation, void* lpReserved, LPARAM lParam) #uselib "KERNEL32.dll" #cfunc global CompareStringEx "CompareStringEx" wstr, int, wstr, int, wstr, int, var, intptr, intptr ; res = CompareStringEx(lpLocaleName, dwCmpFlags, lpString1, cchCount1, lpString2, cchCount2, lpVersionInformation, lpReserved, lParam) ; lpLocaleName : LPCWSTR optional -> "wstr" ; dwCmpFlags : COMPARE_STRING_FLAGS -> "int" ; lpString1 : LPCWSTR -> "wstr" ; cchCount1 : INT -> "int" ; lpString2 : LPCWSTR -> "wstr" ; cchCount2 : INT -> "int" ; lpVersionInformation : NLSVERSIONINFO* optional -> "var" ; lpReserved : void* optional -> "intptr" ; lParam : LPARAM optional -> "intptr" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; COMPARESTRING_RESULT CompareStringEx(LPCWSTR lpLocaleName, COMPARE_STRING_FLAGS dwCmpFlags, LPCWSTR lpString1, INT cchCount1, LPCWSTR lpString2, INT cchCount2, NLSVERSIONINFO* lpVersionInformation, void* lpReserved, LPARAM lParam) #uselib "KERNEL32.dll" #cfunc global CompareStringEx "CompareStringEx" wstr, int, wstr, int, wstr, int, intptr, intptr, intptr ; res = CompareStringEx(lpLocaleName, dwCmpFlags, lpString1, cchCount1, lpString2, cchCount2, varptr(lpVersionInformation), lpReserved, lParam) ; lpLocaleName : LPCWSTR optional -> "wstr" ; dwCmpFlags : COMPARE_STRING_FLAGS -> "int" ; lpString1 : LPCWSTR -> "wstr" ; cchCount1 : INT -> "int" ; lpString2 : LPCWSTR -> "wstr" ; cchCount2 : INT -> "int" ; lpVersionInformation : NLSVERSIONINFO* optional -> "intptr" ; lpReserved : void* optional -> "intptr" ; lParam : LPARAM optional -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
procCompareStringEx = kernel32.NewProc("CompareStringEx")
)
// lpLocaleName (LPCWSTR optional), dwCmpFlags (COMPARE_STRING_FLAGS), lpString1 (LPCWSTR), cchCount1 (INT), lpString2 (LPCWSTR), cchCount2 (INT), lpVersionInformation (NLSVERSIONINFO* optional), lpReserved (void* optional), lParam (LPARAM optional)
r1, _, err := procCompareStringEx.Call(
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpLocaleName))),
uintptr(dwCmpFlags),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpString1))),
uintptr(cchCount1),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpString2))),
uintptr(cchCount2),
uintptr(lpVersionInformation),
uintptr(lpReserved),
uintptr(lParam),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // COMPARESTRING_RESULTfunction CompareStringEx(
lpLocaleName: PWideChar; // LPCWSTR optional
dwCmpFlags: DWORD; // COMPARE_STRING_FLAGS
lpString1: PWideChar; // LPCWSTR
cchCount1: Integer; // INT
lpString2: PWideChar; // LPCWSTR
cchCount2: Integer; // INT
lpVersionInformation: Pointer; // NLSVERSIONINFO* optional
lpReserved: Pointer; // void* optional
lParam: NativeInt // LPARAM optional
): Integer; stdcall;
external 'KERNEL32.dll' name 'CompareStringEx';result := DllCall("KERNEL32\CompareStringEx"
, "WStr", lpLocaleName ; LPCWSTR optional
, "UInt", dwCmpFlags ; COMPARE_STRING_FLAGS
, "WStr", lpString1 ; LPCWSTR
, "Int", cchCount1 ; INT
, "WStr", lpString2 ; LPCWSTR
, "Int", cchCount2 ; INT
, "Ptr", lpVersionInformation ; NLSVERSIONINFO* optional
, "Ptr", lpReserved ; void* optional
, "Ptr", lParam ; LPARAM optional
, "Int") ; return: COMPARESTRING_RESULT●CompareStringEx(lpLocaleName, dwCmpFlags, lpString1, cchCount1, lpString2, cchCount2, lpVersionInformation, lpReserved, lParam) = DLL("KERNEL32.dll", "int CompareStringEx(char*, dword, char*, int, char*, int, void*, void*, int)")
# 呼び出し: CompareStringEx(lpLocaleName, dwCmpFlags, lpString1, cchCount1, lpString2, cchCount2, lpVersionInformation, lpReserved, lParam)
# lpLocaleName : LPCWSTR optional -> "char*"
# dwCmpFlags : COMPARE_STRING_FLAGS -> "dword"
# lpString1 : LPCWSTR -> "char*"
# cchCount1 : INT -> "int"
# lpString2 : LPCWSTR -> "char*"
# cchCount2 : INT -> "int"
# lpVersionInformation : NLSVERSIONINFO* optional -> "void*"
# lpReserved : void* optional -> "void*"
# lParam : LPARAM optional -> "int"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "kernel32" fn CompareStringEx(
lpLocaleName: [*c]const u16, // LPCWSTR optional
dwCmpFlags: u32, // COMPARE_STRING_FLAGS
lpString1: [*c]const u16, // LPCWSTR
cchCount1: i32, // INT
lpString2: [*c]const u16, // LPCWSTR
cchCount2: i32, // INT
lpVersionInformation: [*c]NLSVERSIONINFO, // NLSVERSIONINFO* optional
lpReserved: ?*anyopaque, // void* optional
lParam: isize // LPARAM optional
) callconv(std.os.windows.WINAPI) i32;proc CompareStringEx(
lpLocaleName: WideCString, # LPCWSTR optional
dwCmpFlags: uint32, # COMPARE_STRING_FLAGS
lpString1: WideCString, # LPCWSTR
cchCount1: int32, # INT
lpString2: WideCString, # LPCWSTR
cchCount2: int32, # INT
lpVersionInformation: ptr NLSVERSIONINFO, # NLSVERSIONINFO* optional
lpReserved: pointer, # void* optional
lParam: int # LPARAM optional
): int32 {.importc: "CompareStringEx", stdcall, dynlib: "KERNEL32.dll".}pragma(lib, "kernel32");
extern(Windows)
int CompareStringEx(
const(wchar)* lpLocaleName, // LPCWSTR optional
uint dwCmpFlags, // COMPARE_STRING_FLAGS
const(wchar)* lpString1, // LPCWSTR
int cchCount1, // INT
const(wchar)* lpString2, // LPCWSTR
int cchCount2, // INT
NLSVERSIONINFO* lpVersionInformation, // NLSVERSIONINFO* optional
void* lpReserved, // void* optional
ptrdiff_t lParam // LPARAM optional
);ccall((:CompareStringEx, "KERNEL32.dll"), stdcall, Int32,
(Cwstring, UInt32, Cwstring, Int32, Cwstring, Int32, Ptr{NLSVERSIONINFO}, Ptr{Cvoid}, Int),
lpLocaleName, dwCmpFlags, lpString1, cchCount1, lpString2, cchCount2, lpVersionInformation, lpReserved, lParam)
# lpLocaleName : LPCWSTR optional -> Cwstring
# dwCmpFlags : COMPARE_STRING_FLAGS -> UInt32
# lpString1 : LPCWSTR -> Cwstring
# cchCount1 : INT -> Int32
# lpString2 : LPCWSTR -> Cwstring
# cchCount2 : INT -> Int32
# lpVersionInformation : NLSVERSIONINFO* optional -> Ptr{NLSVERSIONINFO}
# lpReserved : void* optional -> Ptr{Cvoid}
# lParam : LPARAM optional -> Int
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int32_t CompareStringEx(
const uint16_t* lpLocaleName,
uint32_t dwCmpFlags,
const uint16_t* lpString1,
int32_t cchCount1,
const uint16_t* lpString2,
int32_t cchCount2,
void* lpVersionInformation,
void* lpReserved,
intptr_t lParam);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.CompareStringEx(lpLocaleName, dwCmpFlags, lpString1, cchCount1, lpString2, cchCount2, lpVersionInformation, lpReserved, lParam)
-- lpLocaleName : LPCWSTR optional
-- dwCmpFlags : COMPARE_STRING_FLAGS
-- lpString1 : LPCWSTR
-- cchCount1 : INT
-- lpString2 : LPCWSTR
-- cchCount2 : INT
-- lpVersionInformation : NLSVERSIONINFO* optional
-- lpReserved : void* optional
-- lParam : LPARAM optional
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('KERNEL32.dll');
const CompareStringEx = lib.func('__stdcall', 'CompareStringEx', 'int32_t', ['str16', 'uint32_t', 'str16', 'int32_t', 'str16', 'int32_t', 'void *', 'void *', 'intptr_t']);
// CompareStringEx(lpLocaleName, dwCmpFlags, lpString1, cchCount1, lpString2, cchCount2, lpVersionInformation, lpReserved, lParam)
// lpLocaleName : LPCWSTR optional -> 'str16'
// dwCmpFlags : COMPARE_STRING_FLAGS -> 'uint32_t'
// lpString1 : LPCWSTR -> 'str16'
// cchCount1 : INT -> 'int32_t'
// lpString2 : LPCWSTR -> 'str16'
// cchCount2 : INT -> 'int32_t'
// lpVersionInformation : NLSVERSIONINFO* optional -> 'void *'
// lpReserved : void* optional -> 'void *'
// lParam : LPARAM optional -> 'intptr_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("KERNEL32.dll", {
CompareStringEx: { parameters: ["buffer", "u32", "buffer", "i32", "buffer", "i32", "pointer", "pointer", "isize"], result: "i32" },
});
// lib.symbols.CompareStringEx(lpLocaleName, dwCmpFlags, lpString1, cchCount1, lpString2, cchCount2, lpVersionInformation, lpReserved, lParam)
// lpLocaleName : LPCWSTR optional -> "buffer"
// dwCmpFlags : COMPARE_STRING_FLAGS -> "u32"
// lpString1 : LPCWSTR -> "buffer"
// cchCount1 : INT -> "i32"
// lpString2 : LPCWSTR -> "buffer"
// cchCount2 : INT -> "i32"
// lpVersionInformation : NLSVERSIONINFO* optional -> "pointer"
// lpReserved : void* optional -> "pointer"
// lParam : LPARAM optional -> "isize"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t CompareStringEx(
const uint16_t* lpLocaleName,
uint32_t dwCmpFlags,
const uint16_t* lpString1,
int32_t cchCount1,
const uint16_t* lpString2,
int32_t cchCount2,
void* lpVersionInformation,
void* lpReserved,
intptr_t lParam);
C, "KERNEL32.dll");
// $ffi->CompareStringEx(lpLocaleName, dwCmpFlags, lpString1, cchCount1, lpString2, cchCount2, lpVersionInformation, lpReserved, lParam);
// lpLocaleName : LPCWSTR optional
// dwCmpFlags : COMPARE_STRING_FLAGS
// lpString1 : LPCWSTR
// cchCount1 : INT
// lpString2 : LPCWSTR
// cchCount2 : INT
// lpVersionInformation : NLSVERSIONINFO* optional
// lpReserved : void* optional
// lParam : LPARAM optional
// 構造体/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 CompareStringEx(
WString lpLocaleName, // LPCWSTR optional
int dwCmpFlags, // COMPARE_STRING_FLAGS
WString lpString1, // LPCWSTR
int cchCount1, // INT
WString lpString2, // LPCWSTR
int cchCount2, // INT
Pointer lpVersionInformation, // NLSVERSIONINFO* optional
Pointer lpReserved, // void* optional
long lParam // LPARAM optional
);
}@[Link("kernel32")]
lib LibKERNEL32
fun CompareStringEx = CompareStringEx(
lpLocaleName : UInt16*, # LPCWSTR optional
dwCmpFlags : UInt32, # COMPARE_STRING_FLAGS
lpString1 : UInt16*, # LPCWSTR
cchCount1 : Int32, # INT
lpString2 : UInt16*, # LPCWSTR
cchCount2 : Int32, # INT
lpVersionInformation : NLSVERSIONINFO*, # NLSVERSIONINFO* optional
lpReserved : Void*, # void* optional
lParam : LibC::SSizeT # LPARAM optional
) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef CompareStringExNative = Int32 Function(Pointer<Utf16>, Uint32, Pointer<Utf16>, Int32, Pointer<Utf16>, Int32, Pointer<Void>, Pointer<Void>, IntPtr);
typedef CompareStringExDart = int Function(Pointer<Utf16>, int, Pointer<Utf16>, int, Pointer<Utf16>, int, Pointer<Void>, Pointer<Void>, int);
final CompareStringEx = DynamicLibrary.open('KERNEL32.dll')
.lookupFunction<CompareStringExNative, CompareStringExDart>('CompareStringEx');
// lpLocaleName : LPCWSTR optional -> Pointer<Utf16>
// dwCmpFlags : COMPARE_STRING_FLAGS -> Uint32
// lpString1 : LPCWSTR -> Pointer<Utf16>
// cchCount1 : INT -> Int32
// lpString2 : LPCWSTR -> Pointer<Utf16>
// cchCount2 : INT -> Int32
// lpVersionInformation : NLSVERSIONINFO* optional -> Pointer<Void>
// lpReserved : void* optional -> Pointer<Void>
// lParam : LPARAM optional -> IntPtr
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function CompareStringEx(
lpLocaleName: PWideChar; // LPCWSTR optional
dwCmpFlags: DWORD; // COMPARE_STRING_FLAGS
lpString1: PWideChar; // LPCWSTR
cchCount1: Integer; // INT
lpString2: PWideChar; // LPCWSTR
cchCount2: Integer; // INT
lpVersionInformation: Pointer; // NLSVERSIONINFO* optional
lpReserved: Pointer; // void* optional
lParam: NativeInt // LPARAM optional
): Integer; stdcall;
external 'KERNEL32.dll' name 'CompareStringEx';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "CompareStringEx"
c_CompareStringEx :: CWString -> Word32 -> CWString -> Int32 -> CWString -> Int32 -> Ptr () -> Ptr () -> CIntPtr -> IO Int32
-- lpLocaleName : LPCWSTR optional -> CWString
-- dwCmpFlags : COMPARE_STRING_FLAGS -> Word32
-- lpString1 : LPCWSTR -> CWString
-- cchCount1 : INT -> Int32
-- lpString2 : LPCWSTR -> CWString
-- cchCount2 : INT -> Int32
-- lpVersionInformation : NLSVERSIONINFO* optional -> Ptr ()
-- lpReserved : void* optional -> Ptr ()
-- lParam : LPARAM optional -> CIntPtr
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let comparestringex =
foreign "CompareStringEx"
((ptr uint16_t) @-> uint32_t @-> (ptr uint16_t) @-> int32_t @-> (ptr uint16_t) @-> int32_t @-> (ptr void) @-> (ptr void) @-> intptr_t @-> returning int32_t)
(* lpLocaleName : LPCWSTR optional -> (ptr uint16_t) *)
(* dwCmpFlags : COMPARE_STRING_FLAGS -> uint32_t *)
(* lpString1 : LPCWSTR -> (ptr uint16_t) *)
(* cchCount1 : INT -> int32_t *)
(* lpString2 : LPCWSTR -> (ptr uint16_t) *)
(* cchCount2 : INT -> int32_t *)
(* lpVersionInformation : NLSVERSIONINFO* optional -> (ptr void) *)
(* lpReserved : void* optional -> (ptr void) *)
(* lParam : LPARAM optional -> intptr_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)
(cffi:defcfun ("CompareStringEx" compare-string-ex :convention :stdcall) :int32
(lp-locale-name (:string :encoding :utf-16le)) ; LPCWSTR optional
(dw-cmp-flags :uint32) ; COMPARE_STRING_FLAGS
(lp-string1 (:string :encoding :utf-16le)) ; LPCWSTR
(cch-count1 :int32) ; INT
(lp-string2 (:string :encoding :utf-16le)) ; LPCWSTR
(cch-count2 :int32) ; INT
(lp-version-information :pointer) ; NLSVERSIONINFO* optional
(lp-reserved :pointer) ; void* optional
(l-param :int64)) ; LPARAM optional
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $CompareStringEx = Win32::API::More->new('KERNEL32',
'int CompareStringEx(LPCWSTR lpLocaleName, DWORD dwCmpFlags, LPCWSTR lpString1, int cchCount1, LPCWSTR lpString2, int cchCount2, LPVOID lpVersionInformation, LPVOID lpReserved, LPARAM lParam)');
# my $ret = $CompareStringEx->Call($lpLocaleName, $dwCmpFlags, $lpString1, $cchCount1, $lpString2, $cchCount2, $lpVersionInformation, $lpReserved, $lParam);
# lpLocaleName : LPCWSTR optional -> LPCWSTR
# dwCmpFlags : COMPARE_STRING_FLAGS -> DWORD
# lpString1 : LPCWSTR -> LPCWSTR
# cchCount1 : INT -> int
# lpString2 : LPCWSTR -> LPCWSTR
# cchCount2 : INT -> int
# lpVersionInformation : NLSVERSIONINFO* optional -> LPVOID
# lpReserved : void* optional -> LPVOID
# lParam : LPARAM optional -> LPARAM
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
- f CompareStringW — ロケールに従い二つのUnicode文字列を比較する。