Win32 API 日本語リファレンス
ホームGlobalization › CompareStringEx

CompareStringEx

関数
ロケール名を指定して二つの文字列を比較する。
DLLKERNEL32.dll呼出規約winapiSetLastErrorあり対応OSWindows Vista 以降

シグネチャ

// 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
);

パラメーター

名前方向説明
lpLocaleNameLPCWSTRinoptional

ロケール名へのポインター、または以下の定義済みの値のいずれか。

dwCmpFlagsCOMPARE_STRING_FLAGSin

関数が 2 つの文字列をどのように比較するかを示すフラグです。既定では、これらのフラグは設定されません。このパラメーターには、以下の値の任意の組み合わせを指定できます。既定の動作にするには 0 を設定します。

フラグ 意味
LINGUISTIC_IGNORECASE
言語的に適切な形で大文字と小文字を無視します。
LINGUISTIC_IGNOREDIACRITIC
言語的に適切な形で非スペーシング文字を無視します。
注意 このフラグは、分解された文字 (基底文字と 1 つ以上の非スペーシング文字がそれぞれ別個のコードポイント値を持つ文字) と共に使用した場合、必ずしも予測可能な結果を生成するとは限りません。
NORM_IGNORECASE
大文字と小文字を無視します。多くの文字体系 (特にラテン文字) では、NORM_IGNORECASELINGUISTIC_IGNORECASE と一致します。
注意 NORM_IGNORECASE は、それが実際に言語的な大文字小文字の区別であるかどうかにかかわらず、第 3 次の区別をすべて無視します。たとえばアラビア文字やインド系文字では、これは文字の異体字を区別しますが、その違いは言語的な大文字小文字の区別には対応しません。LINGUISTIC_IGNORECASE は、第 3 次のソート重みを無視するのではなく、実際の言語的な大文字小文字の区別のみを無視させます。
注意 このフラグを設定すると、関数は CJK 互換文字の全角形と半角形の区別を無視します。
NORM_IGNOREKANATYPE
ひらがなとカタカナを区別しません。対応するひらがなとカタカナは等しいものとして比較されます。
NORM_IGNORENONSPACE
非スペーシング文字を無視します。多くの文字体系 (特にラテン文字) では、NORM_IGNORENONSPACELINGUISTIC_IGNOREDIACRITIC と一致します。
注意 NORM_IGNORENONSPACE は、それがダイアクリティカルマークであるかどうかにかかわらず、第 2 次の区別をすべて無視します。韓国語、日本語、中国語、インド系言語などの文字体系では、この区別をダイアクリティカルマーク以外の目的で使用します。LINGUISTIC_IGNOREDIACRITIC は、第 2 次のソート重みを無視するのではなく、実際のダイアクリティカルマークのみを無視させます。
注意 NORM_IGNORENONSPACE は、アクセント付き文字を主文字とは別のパスでソートするロケールでのみ効果があります。通常、文字列内のすべての文字は最初にアクセントを考慮せずに比較され、文字列が等しい場合は、アクセントを比較するために 2 回目のパスが実行されます。このフラグはその 2 回目のパスを実行させないようにします。アクセント付き文字を最初のパスでソートするロケールでは、このフラグは効果がありません。
NORM_IGNORESYMBOLS
記号と句読点を無視します。
NORM_IGNOREWIDTH
半角文字と全角文字の違いを無視します。たとえば、C a t == cat となります。全角形は、中国語や日本語の文字体系で使用される書式上の区別です。
NORM_LINGUISTIC_CASING
ファイルシステムの規則ではなく、既定の言語的な規則を大文字小文字の処理に使用します。CompareStringEx のほとんどのシナリオでは、このフラグを使用することに注意してください。アプリケーションが CompareStringOrdinal を呼び出す場合は、このフラグを使用する必要はありません。
SORT_DIGITSASNUMBERS
Windows 7: ソート時に数字を数値として扱います。たとえば、"2" を "10" より前にソートします。
SORT_STRINGSORT
句読点を記号と同じように扱います。
lpString1LPCWSTRin比較する最初の文字列へのポインター。
cchCount1INTinlpString1 で示される文字列の長さ (終端の null 文字を除く)。文字列が null で終端されている場合、アプリケーションは負の値を指定できます。この場合、関数が長さを自動的に判別します。
lpString2LPCWSTRin比較する 2 番目の文字列へのポインター。
cchCount2INTinlpString2 で示される文字列の長さ (終端の null 文字を除く)。文字列が null で終端されている場合、アプリケーションは負の値を指定できます。この場合、関数が長さを自動的に判別します。
lpVersionInformationNLSVERSIONINFO*optional

関連する NLS 機能に関するバージョン情報を含む NLSVERSIONINFOEX 構造体へのポインター。通常は GetNLSVersionEx から取得します。

Windows Vista、Windows 7: 予約済み。NULL を設定する必要があります。

lpReservedvoid*optional予約済み。NULL を設定する必要があります。
lParamLPARAMoptional予約済み。0 を設定する必要があります。

戻り値の型: COMPARESTRING_RESULT

公式ドキュメント

名前で指定されたロケールに従って、2 つの Unicode (ワイド文字) 文字列を比較します。注意 CompareStringEx を誤って使用すると、アプリケーションのセキュリティが損なわれるおそれがあります。

戻り値

成功した場合、以下の値のいずれかを返します。文字列比較における C ランタイムの慣例を維持するために、0 以外の戻り値から 2 を減算できます。そうすると、<0、==0、>0 の意味が C ランタイムと一致します。

成功しなかった場合、関数は 0 を返します。拡張エラー情報を取得するには、アプリケーションは GetLastError を呼び出すことができます。これは以下のエラーコードのいずれかを返すことがあります。

解説(Remarks)

CompareStringCompareStringEx はいずれも、dwCmpFlags が 0 または NORM_IGNORECASE に設定され、cchCount1cchCount2 が -1 に設定され、かつ伝統的なスペイン語のソートが "ch" を 1 文字として扱うような言語的な圧縮をロケールがサポートしていない場合に、最高速で実行されるよう最適化されています。

CompareStringCompareStringEx はいずれも、比較中にアラビア語のカシダ (kashida) を無視します。したがって、2 つの文字列がカシダの有無を除いて同一である場合、関数は CSTR_EQUAL を返します。

アプリケーションがソート関数で NORM_IGNORENONSPACE フラグと NORM_IGNORECASE フラグを使用すると、これらのフラグが文字列比較を妨げることがあります。この状況は、非スペーシング文字や大文字小文字をサポートしていないが、他の重要な操作を処理するために同等の重みレベルを使用するロケールで発生する可能性があります。このような場合、アプリケーションは LINGUISTIC_IGNOREDIACRITIC フラグと LINGUISTIC_IGNORECASE フラグを使用する必要があります。これらのフラグは、大文字小文字やダイアクリティカルマークを使用するコードポイントのソートに対して言語的に適切な結果を提供し、他のコードポイントには影響を与えません。

Windows Vista 以降: CompareStringCompareStringEx はいずれも、カスタムロケールからデータを取得できます。データは、コンピューター間やアプリケーションの実行間で同じであることは保証されません。アプリケーションがデータを永続化または送信する必要がある場合は、永続的なロケールデータの使用を参照してください。

Windows 8 以降: アプリが Windows.Globalization 名前空間の言語タグをこの関数に渡す場合は、まず ResolveLocaleName を呼び出してタグを変換する必要があります。

Windows 8 以降: CompareStringEx は Stringapiset.h で宣言されています。Windows 8 より前では、Winnls.h で宣言されていました。

注意 ソートの動作は Windows のリリース間で変わる可能性があります。たとえば、新しい Unicode コードポイントが作成されることがあります。ソートのバージョンが変更されたかどうかを確認するには、GetNlsVersionEx を使用してください。

この関数の使用例は、NLS: Name-based APIs Sample にあります。

出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
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 方式にも切替可。
出力引数:
; 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 方式にも切替可。
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_RESULT
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';
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 を使用。

関連項目

類似 API
使用する型