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

CompareStringOrdinal

関数
二つの文字列を序数的に比較する。
DLLKERNEL32.dll呼出規約winapiSetLastErrorあり対応OSWindows Vista 以降

シグネチャ

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

COMPARESTRING_RESULT CompareStringOrdinal(
    LPCWSTR lpString1,
    INT cchCount1,
    LPCWSTR lpString2,
    INT cchCount2,
    BOOL bIgnoreCase
);

パラメーター

名前方向説明
lpString1LPCWSTRin比較する 1 番目の文字列へのポインター。
cchCount1INTinlpString1 が指す文字列の長さ。文字列が null で終端されている場合、アプリケーションは -1 を指定します。この場合、関数が長さを自動的に判定します。
lpString2LPCWSTRin比較する 2 番目の文字列へのポインター。
cchCount2INTinlpString2 が指す文字列の長さ。文字列が null で終端されている場合、アプリケーションは -1 を指定します。この場合、関数が長さを自動的に判定します。
bIgnoreCaseBOOLinオペレーティングシステムの大文字テーブル情報を使用して大文字と小文字を区別しない比較を行う場合は TRUE を指定します。渡されたとおりに文字列を厳密に比較する場合、アプリケーションはこのパラメーターに FALSE を設定します。なお、このブール型パラメーターで真の値を指定するために使用できる数値は 1 のみであり、それ以外の値を指定すると無効なパラメーターエラーになります。ブール値はこのパラメーターで想定どおりに機能します。

戻り値の型: COMPARESTRING_RESULT

公式ドキュメント

2 つの Unicode 文字列を比較し、バイナリとして等価かどうかをテストします。

戻り値

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

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

解説(Remarks)

この関数は、言語的な等価性ではなくバイナリの等価性をテストします。順序ソート (ordinal sorting) でのこの関数の使用については、Handling Sorting in Your Applications を参照してください。

言語的な等価性が問題となるアプリケーションでは、CompareStringCompareStringExlstrcmp、または lstrcmpi を使用してください。言語的なソートの詳細については、Handling Sorting in Your Applications を参照してください。

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

出典・ライセンス: 上記「公式ドキュメント」の内容は 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 CompareStringOrdinal(
    LPCWSTR lpString1,
    INT cchCount1,
    LPCWSTR lpString2,
    INT cchCount2,
    BOOL bIgnoreCase
);
[DllImport("KERNEL32.dll", SetLastError = true, ExactSpelling = true)]
static extern int CompareStringOrdinal(
    [MarshalAs(UnmanagedType.LPWStr)] string lpString1,   // LPCWSTR
    int cchCount1,   // INT
    [MarshalAs(UnmanagedType.LPWStr)] string lpString2,   // LPCWSTR
    int cchCount2,   // INT
    bool bIgnoreCase   // BOOL
);
<DllImport("KERNEL32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function CompareStringOrdinal(
    <MarshalAs(UnmanagedType.LPWStr)> lpString1 As String,   ' LPCWSTR
    cchCount1 As Integer,   ' INT
    <MarshalAs(UnmanagedType.LPWStr)> lpString2 As String,   ' LPCWSTR
    cchCount2 As Integer,   ' INT
    bIgnoreCase As Boolean   ' BOOL
) As Integer
End Function
' lpString1 : LPCWSTR
' cchCount1 : INT
' lpString2 : LPCWSTR
' cchCount2 : INT
' bIgnoreCase : BOOL
Declare PtrSafe Function CompareStringOrdinal Lib "kernel32" ( _
    ByVal lpString1 As LongPtr, _
    ByVal cchCount1 As Long, _
    ByVal lpString2 As LongPtr, _
    ByVal cchCount2 As Long, _
    ByVal bIgnoreCase As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

CompareStringOrdinal = ctypes.windll.kernel32.CompareStringOrdinal
CompareStringOrdinal.restype = ctypes.c_int
CompareStringOrdinal.argtypes = [
    wintypes.LPCWSTR,  # lpString1 : LPCWSTR
    ctypes.c_int,  # cchCount1 : INT
    wintypes.LPCWSTR,  # lpString2 : LPCWSTR
    ctypes.c_int,  # cchCount2 : INT
    wintypes.BOOL,  # bIgnoreCase : BOOL
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('KERNEL32.dll')
CompareStringOrdinal = Fiddle::Function.new(
  lib['CompareStringOrdinal'],
  [
    Fiddle::TYPE_VOIDP,  # lpString1 : LPCWSTR
    Fiddle::TYPE_INT,  # cchCount1 : INT
    Fiddle::TYPE_VOIDP,  # lpString2 : LPCWSTR
    Fiddle::TYPE_INT,  # cchCount2 : INT
    Fiddle::TYPE_INT,  # bIgnoreCase : BOOL
  ],
  Fiddle::TYPE_INT)
#[link(name = "kernel32")]
extern "system" {
    fn CompareStringOrdinal(
        lpString1: *const u16,  // LPCWSTR
        cchCount1: i32,  // INT
        lpString2: *const u16,  // LPCWSTR
        cchCount2: i32,  // INT
        bIgnoreCase: i32  // BOOL
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("KERNEL32.dll", SetLastError = true)]
public static extern int CompareStringOrdinal([MarshalAs(UnmanagedType.LPWStr)] string lpString1, int cchCount1, [MarshalAs(UnmanagedType.LPWStr)] string lpString2, int cchCount2, bool bIgnoreCase);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_CompareStringOrdinal' -Namespace Win32 -PassThru
# $api::CompareStringOrdinal(lpString1, cchCount1, lpString2, cchCount2, bIgnoreCase)
#uselib "KERNEL32.dll"
#func global CompareStringOrdinal "CompareStringOrdinal" sptr, sptr, sptr, sptr, sptr
; CompareStringOrdinal lpString1, cchCount1, lpString2, cchCount2, bIgnoreCase   ; 戻り値は stat
; lpString1 : LPCWSTR -> "sptr"
; cchCount1 : INT -> "sptr"
; lpString2 : LPCWSTR -> "sptr"
; cchCount2 : INT -> "sptr"
; bIgnoreCase : BOOL -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "KERNEL32.dll"
#cfunc global CompareStringOrdinal "CompareStringOrdinal" wstr, int, wstr, int, int
; res = CompareStringOrdinal(lpString1, cchCount1, lpString2, cchCount2, bIgnoreCase)
; lpString1 : LPCWSTR -> "wstr"
; cchCount1 : INT -> "int"
; lpString2 : LPCWSTR -> "wstr"
; cchCount2 : INT -> "int"
; bIgnoreCase : BOOL -> "int"
; COMPARESTRING_RESULT CompareStringOrdinal(LPCWSTR lpString1, INT cchCount1, LPCWSTR lpString2, INT cchCount2, BOOL bIgnoreCase)
#uselib "KERNEL32.dll"
#cfunc global CompareStringOrdinal "CompareStringOrdinal" wstr, int, wstr, int, int
; res = CompareStringOrdinal(lpString1, cchCount1, lpString2, cchCount2, bIgnoreCase)
; lpString1 : LPCWSTR -> "wstr"
; cchCount1 : INT -> "int"
; lpString2 : LPCWSTR -> "wstr"
; cchCount2 : INT -> "int"
; bIgnoreCase : BOOL -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procCompareStringOrdinal = kernel32.NewProc("CompareStringOrdinal")
)

// lpString1 (LPCWSTR), cchCount1 (INT), lpString2 (LPCWSTR), cchCount2 (INT), bIgnoreCase (BOOL)
r1, _, err := procCompareStringOrdinal.Call(
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpString1))),
	uintptr(cchCount1),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpString2))),
	uintptr(cchCount2),
	uintptr(bIgnoreCase),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // COMPARESTRING_RESULT
function CompareStringOrdinal(
  lpString1: PWideChar;   // LPCWSTR
  cchCount1: Integer;   // INT
  lpString2: PWideChar;   // LPCWSTR
  cchCount2: Integer;   // INT
  bIgnoreCase: BOOL   // BOOL
): Integer; stdcall;
  external 'KERNEL32.dll' name 'CompareStringOrdinal';
result := DllCall("KERNEL32\CompareStringOrdinal"
    , "WStr", lpString1   ; LPCWSTR
    , "Int", cchCount1   ; INT
    , "WStr", lpString2   ; LPCWSTR
    , "Int", cchCount2   ; INT
    , "Int", bIgnoreCase   ; BOOL
    , "Int")   ; return: COMPARESTRING_RESULT
●CompareStringOrdinal(lpString1, cchCount1, lpString2, cchCount2, bIgnoreCase) = DLL("KERNEL32.dll", "int CompareStringOrdinal(char*, int, char*, int, bool)")
# 呼び出し: CompareStringOrdinal(lpString1, cchCount1, lpString2, cchCount2, bIgnoreCase)
# lpString1 : LPCWSTR -> "char*"
# cchCount1 : INT -> "int"
# lpString2 : LPCWSTR -> "char*"
# cchCount2 : INT -> "int"
# bIgnoreCase : BOOL -> "bool"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "kernel32" fn CompareStringOrdinal(
    lpString1: [*c]const u16, // LPCWSTR
    cchCount1: i32, // INT
    lpString2: [*c]const u16, // LPCWSTR
    cchCount2: i32, // INT
    bIgnoreCase: i32 // BOOL
) callconv(std.os.windows.WINAPI) i32;
proc CompareStringOrdinal(
    lpString1: WideCString,  # LPCWSTR
    cchCount1: int32,  # INT
    lpString2: WideCString,  # LPCWSTR
    cchCount2: int32,  # INT
    bIgnoreCase: int32  # BOOL
): int32 {.importc: "CompareStringOrdinal", stdcall, dynlib: "KERNEL32.dll".}
pragma(lib, "kernel32");
extern(Windows)
int CompareStringOrdinal(
    const(wchar)* lpString1,   // LPCWSTR
    int cchCount1,   // INT
    const(wchar)* lpString2,   // LPCWSTR
    int cchCount2,   // INT
    int bIgnoreCase   // BOOL
);
ccall((:CompareStringOrdinal, "KERNEL32.dll"), stdcall, Int32,
      (Cwstring, Int32, Cwstring, Int32, Int32),
      lpString1, cchCount1, lpString2, cchCount2, bIgnoreCase)
# lpString1 : LPCWSTR -> Cwstring
# cchCount1 : INT -> Int32
# lpString2 : LPCWSTR -> Cwstring
# cchCount2 : INT -> Int32
# bIgnoreCase : BOOL -> Int32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t CompareStringOrdinal(
    const uint16_t* lpString1,
    int32_t cchCount1,
    const uint16_t* lpString2,
    int32_t cchCount2,
    int32_t bIgnoreCase);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.CompareStringOrdinal(lpString1, cchCount1, lpString2, cchCount2, bIgnoreCase)
-- lpString1 : LPCWSTR
-- cchCount1 : INT
-- lpString2 : LPCWSTR
-- cchCount2 : INT
-- bIgnoreCase : BOOL
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('KERNEL32.dll');
const CompareStringOrdinal = lib.func('__stdcall', 'CompareStringOrdinal', 'int32_t', ['str16', 'int32_t', 'str16', 'int32_t', 'int32_t']);
// CompareStringOrdinal(lpString1, cchCount1, lpString2, cchCount2, bIgnoreCase)
// lpString1 : LPCWSTR -> 'str16'
// cchCount1 : INT -> 'int32_t'
// lpString2 : LPCWSTR -> 'str16'
// cchCount2 : INT -> 'int32_t'
// bIgnoreCase : BOOL -> 'int32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("KERNEL32.dll", {
  CompareStringOrdinal: { parameters: ["buffer", "i32", "buffer", "i32", "i32"], result: "i32" },
});
// lib.symbols.CompareStringOrdinal(lpString1, cchCount1, lpString2, cchCount2, bIgnoreCase)
// lpString1 : LPCWSTR -> "buffer"
// cchCount1 : INT -> "i32"
// lpString2 : LPCWSTR -> "buffer"
// cchCount2 : INT -> "i32"
// bIgnoreCase : BOOL -> "i32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t CompareStringOrdinal(
    const uint16_t* lpString1,
    int32_t cchCount1,
    const uint16_t* lpString2,
    int32_t cchCount2,
    int32_t bIgnoreCase);
C, "KERNEL32.dll");
// $ffi->CompareStringOrdinal(lpString1, cchCount1, lpString2, cchCount2, bIgnoreCase);
// lpString1 : LPCWSTR
// cchCount1 : INT
// lpString2 : LPCWSTR
// cchCount2 : INT
// bIgnoreCase : BOOL
// 構造体/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 CompareStringOrdinal(
        WString lpString1,   // LPCWSTR
        int cchCount1,   // INT
        WString lpString2,   // LPCWSTR
        int cchCount2,   // INT
        boolean bIgnoreCase   // BOOL
    );
}
@[Link("kernel32")]
lib LibKERNEL32
  fun CompareStringOrdinal = CompareStringOrdinal(
    lpString1 : UInt16*,   # LPCWSTR
    cchCount1 : Int32,   # INT
    lpString2 : UInt16*,   # LPCWSTR
    cchCount2 : Int32,   # INT
    bIgnoreCase : Int32   # BOOL
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef CompareStringOrdinalNative = Int32 Function(Pointer<Utf16>, Int32, Pointer<Utf16>, Int32, Int32);
typedef CompareStringOrdinalDart = int Function(Pointer<Utf16>, int, Pointer<Utf16>, int, int);
final CompareStringOrdinal = DynamicLibrary.open('KERNEL32.dll')
    .lookupFunction<CompareStringOrdinalNative, CompareStringOrdinalDart>('CompareStringOrdinal');
// lpString1 : LPCWSTR -> Pointer<Utf16>
// cchCount1 : INT -> Int32
// lpString2 : LPCWSTR -> Pointer<Utf16>
// cchCount2 : INT -> Int32
// bIgnoreCase : BOOL -> Int32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function CompareStringOrdinal(
  lpString1: PWideChar;   // LPCWSTR
  cchCount1: Integer;   // INT
  lpString2: PWideChar;   // LPCWSTR
  cchCount2: Integer;   // INT
  bIgnoreCase: BOOL   // BOOL
): Integer; stdcall;
  external 'KERNEL32.dll' name 'CompareStringOrdinal';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "CompareStringOrdinal"
  c_CompareStringOrdinal :: CWString -> Int32 -> CWString -> Int32 -> CInt -> IO Int32
-- lpString1 : LPCWSTR -> CWString
-- cchCount1 : INT -> Int32
-- lpString2 : LPCWSTR -> CWString
-- cchCount2 : INT -> Int32
-- bIgnoreCase : BOOL -> CInt
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let comparestringordinal =
  foreign "CompareStringOrdinal"
    ((ptr uint16_t) @-> int32_t @-> (ptr uint16_t) @-> int32_t @-> int32_t @-> returning int32_t)
(* lpString1 : LPCWSTR -> (ptr uint16_t) *)
(* cchCount1 : INT -> int32_t *)
(* lpString2 : LPCWSTR -> (ptr uint16_t) *)
(* cchCount2 : INT -> int32_t *)
(* bIgnoreCase : BOOL -> int32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)

(cffi:defcfun ("CompareStringOrdinal" compare-string-ordinal :convention :stdcall) :int32
  (lp-string1 (:string :encoding :utf-16le))   ; LPCWSTR
  (cch-count1 :int32)   ; INT
  (lp-string2 (:string :encoding :utf-16le))   ; LPCWSTR
  (cch-count2 :int32)   ; INT
  (b-ignore-case :int32))   ; BOOL
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $CompareStringOrdinal = Win32::API::More->new('KERNEL32',
    'int CompareStringOrdinal(LPCWSTR lpString1, int cchCount1, LPCWSTR lpString2, int cchCount2, BOOL bIgnoreCase)');
# my $ret = $CompareStringOrdinal->Call($lpString1, $cchCount1, $lpString2, $cchCount2, $bIgnoreCase);
# lpString1 : LPCWSTR -> LPCWSTR
# cchCount1 : INT -> int
# lpString2 : LPCWSTR -> LPCWSTR
# cchCount2 : INT -> int
# bIgnoreCase : BOOL -> BOOL
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

公式の関連項目
使用する型