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

CompareStringA

関数
ロケールに従い二つのANSI文字列を比較する。
DLLKERNEL32.dll文字セットANSI (-A)呼出規約winapi対応OSWindows 2000 以降

シグネチャ

// KERNEL32.dll  (ANSI / -A)
#include <windows.h>

COMPARESTRING_RESULT CompareStringA(
    DWORD Locale,
    DWORD dwCmpFlags,
    CHAR* lpString1,
    INT cchCount1,
    CHAR* lpString2,
    INT cchCount2
);

パラメーター

名前方向説明
LocaleDWORDin

比較に使用するロケールの ロケール識別子です。MAKELCID マクロを使用してロケール識別子を作成するか、次の定義済みの値のいずれかを使用できます。

dwCmpFlagsDWORDinこの関数が 2 つの文字列をどのように比較するかを示すフラグです。詳細な定義については、CompareStringExdwCmpFlags パラメーターを参照してください。
lpString1CHAR*in比較する 1 つ目の文字列へのポインターです。
cchCount1INTinlpString1 で示される文字列の長さです。終端の null 文字は含みません。この値は、関数の ANSI 版ではバイト数を、Unicode 版ではワイド文字数を表します。文字列が null 終端である場合、アプリケーションは負の値を指定できます。この場合、関数が長さを自動的に判定します。
lpString2CHAR*in比較する 2 つ目の文字列へのポインターです。
cchCount2INTinlpString2 で示される文字列の長さです。終端の null 文字は含みません。この値は、関数の ANSI 版ではバイト数を、Unicode 版ではワイド文字数を表します。文字列が null 終端である場合、アプリケーションは負の値を指定できます。この場合、関数が長さを自動的に判定します。

戻り値の型: COMPARESTRING_RESULT

公式ドキュメント

識別子で指定したロケールに従って、2 つの文字列を比較します。注意 CompareString を誤って使用すると、アプリケーションのセキュリティが損なわれる可能性があります。(CompareStringA)

戻り値

CompareStringEx について説明されている値を返します。

解説(Remarks)

「解説」については CompareStringEx を参照してください。

アプリケーションが CompareString の ANSI 版を呼び出している場合、関数は指定されたロケールの既定のコードページを介してパラメーターを変換します。したがって、アプリケーションは CompareString を使用して UTF-8 テキストを扱うことはできません。

通常、大文字小文字を区別しない比較では、CompareString は、ロケールがトルコ語またはアゼルバイジャン語であっても、小文字の "i" を大文字の "I" にマッピングします。NORM_LINGUISTIC_CASING フラグはこの動作をトルコ語またはアゼルバイジャン語について上書きします。このフラグがトルコ語またはアゼルバイジャン語と併せて指定された場合、LATIN SMALL LETTER DOTLESS I (U+0131) が LATIN CAPITAL LETTER I (U+0049) の小文字形となり、LATIN SMALL LETTER I (U+0069) が LATIN CAPITAL LETTER I WITH DOT ABOVE (U+0130) の小文字形となります。

Windows 8 以降: この関数の ANSI 版は Winnls.h で宣言され、Unicode 版は Stringapiset.h で宣言されています。Windows 8 より前は、両方の版が Winnls.h で宣言されていました。

メモ

winnls.h ヘッダーは、CompareString を、UNICODE プリプロセッサ定数の定義に基づいてこの関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして定義します。エンコーディング非依存のエイリアスの使用と、エンコーディング非依存ではないコードを混在させると、コンパイルエラーや実行時エラーを引き起こす不整合につながる可能性があります。詳細については、関数プロトタイプの規則 を参照してください。

出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

// KERNEL32.dll  (ANSI / -A)
#include <windows.h>

COMPARESTRING_RESULT CompareStringA(
    DWORD Locale,
    DWORD dwCmpFlags,
    CHAR* lpString1,
    INT cchCount1,
    CHAR* lpString2,
    INT cchCount2
);
[DllImport("KERNEL32.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
static extern int CompareStringA(
    uint Locale,   // DWORD
    uint dwCmpFlags,   // DWORD
    IntPtr lpString1,   // CHAR*
    int cchCount1,   // INT
    IntPtr lpString2,   // CHAR*
    int cchCount2   // INT
);
<DllImport("KERNEL32.dll", CharSet:=CharSet.Ansi, ExactSpelling:=True)>
Public Shared Function CompareStringA(
    Locale As UInteger,   ' DWORD
    dwCmpFlags As UInteger,   ' DWORD
    lpString1 As IntPtr,   ' CHAR*
    cchCount1 As Integer,   ' INT
    lpString2 As IntPtr,   ' CHAR*
    cchCount2 As Integer   ' INT
) As Integer
End Function
' Locale : DWORD
' dwCmpFlags : DWORD
' lpString1 : CHAR*
' cchCount1 : INT
' lpString2 : CHAR*
' cchCount2 : INT
Declare PtrSafe Function CompareStringA Lib "kernel32" ( _
    ByVal Locale As Long, _
    ByVal dwCmpFlags As Long, _
    ByVal lpString1 As LongPtr, _
    ByVal cchCount1 As Long, _
    ByVal lpString2 As LongPtr, _
    ByVal cchCount2 As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

CompareStringA = ctypes.windll.kernel32.CompareStringA
CompareStringA.restype = ctypes.c_int
CompareStringA.argtypes = [
    wintypes.DWORD,  # Locale : DWORD
    wintypes.DWORD,  # dwCmpFlags : DWORD
    ctypes.POINTER(ctypes.c_byte),  # lpString1 : CHAR*
    ctypes.c_int,  # cchCount1 : INT
    ctypes.POINTER(ctypes.c_byte),  # lpString2 : CHAR*
    ctypes.c_int,  # cchCount2 : INT
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('KERNEL32.dll')
CompareStringA = Fiddle::Function.new(
  lib['CompareStringA'],
  [
    -Fiddle::TYPE_INT,  # Locale : DWORD
    -Fiddle::TYPE_INT,  # dwCmpFlags : DWORD
    Fiddle::TYPE_VOIDP,  # lpString1 : CHAR*
    Fiddle::TYPE_INT,  # cchCount1 : INT
    Fiddle::TYPE_VOIDP,  # lpString2 : CHAR*
    Fiddle::TYPE_INT,  # cchCount2 : INT
  ],
  Fiddle::TYPE_INT)
#[link(name = "kernel32")]
extern "system" {
    fn CompareStringA(
        Locale: u32,  // DWORD
        dwCmpFlags: u32,  // DWORD
        lpString1: *mut i8,  // CHAR*
        cchCount1: i32,  // INT
        lpString2: *mut i8,  // CHAR*
        cchCount2: i32  // INT
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("KERNEL32.dll", CharSet = CharSet.Ansi)]
public static extern int CompareStringA(uint Locale, uint dwCmpFlags, IntPtr lpString1, int cchCount1, IntPtr lpString2, int cchCount2);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_CompareStringA' -Namespace Win32 -PassThru
# $api::CompareStringA(Locale, dwCmpFlags, lpString1, cchCount1, lpString2, cchCount2)
#uselib "KERNEL32.dll"
#func global CompareStringA "CompareStringA" sptr, sptr, sptr, sptr, sptr, sptr
; CompareStringA Locale, dwCmpFlags, varptr(lpString1), cchCount1, varptr(lpString2), cchCount2   ; 戻り値は stat
; Locale : DWORD -> "sptr"
; dwCmpFlags : DWORD -> "sptr"
; lpString1 : CHAR* -> "sptr"
; cchCount1 : INT -> "sptr"
; lpString2 : CHAR* -> "sptr"
; cchCount2 : INT -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "KERNEL32.dll"
#cfunc global CompareStringA "CompareStringA" int, int, var, int, var, int
; res = CompareStringA(Locale, dwCmpFlags, lpString1, cchCount1, lpString2, cchCount2)
; Locale : DWORD -> "int"
; dwCmpFlags : DWORD -> "int"
; lpString1 : CHAR* -> "var"
; cchCount1 : INT -> "int"
; lpString2 : CHAR* -> "var"
; cchCount2 : INT -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; COMPARESTRING_RESULT CompareStringA(DWORD Locale, DWORD dwCmpFlags, CHAR* lpString1, INT cchCount1, CHAR* lpString2, INT cchCount2)
#uselib "KERNEL32.dll"
#cfunc global CompareStringA "CompareStringA" int, int, var, int, var, int
; res = CompareStringA(Locale, dwCmpFlags, lpString1, cchCount1, lpString2, cchCount2)
; Locale : DWORD -> "int"
; dwCmpFlags : DWORD -> "int"
; lpString1 : CHAR* -> "var"
; cchCount1 : INT -> "int"
; lpString2 : CHAR* -> "var"
; cchCount2 : INT -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procCompareStringA = kernel32.NewProc("CompareStringA")
)

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

extern "kernel32" fn CompareStringA(
    Locale: u32, // DWORD
    dwCmpFlags: u32, // DWORD
    lpString1: [*c]i8, // CHAR*
    cchCount1: i32, // INT
    lpString2: [*c]i8, // CHAR*
    cchCount2: i32 // INT
) callconv(std.os.windows.WINAPI) i32;
proc CompareStringA(
    Locale: uint32,  # DWORD
    dwCmpFlags: uint32,  # DWORD
    lpString1: ptr int8,  # CHAR*
    cchCount1: int32,  # INT
    lpString2: ptr int8,  # CHAR*
    cchCount2: int32  # INT
): int32 {.importc: "CompareStringA", stdcall, dynlib: "KERNEL32.dll".}
pragma(lib, "kernel32");
extern(Windows)
int CompareStringA(
    uint Locale,   // DWORD
    uint dwCmpFlags,   // DWORD
    byte* lpString1,   // CHAR*
    int cchCount1,   // INT
    byte* lpString2,   // CHAR*
    int cchCount2   // INT
);
ccall((:CompareStringA, "KERNEL32.dll"), stdcall, Int32,
      (UInt32, UInt32, Ptr{Int8}, Int32, Ptr{Int8}, Int32),
      Locale, dwCmpFlags, lpString1, cchCount1, lpString2, cchCount2)
# Locale : DWORD -> UInt32
# dwCmpFlags : DWORD -> UInt32
# lpString1 : CHAR* -> Ptr{Int8}
# cchCount1 : INT -> Int32
# lpString2 : CHAR* -> Ptr{Int8}
# cchCount2 : INT -> Int32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t CompareStringA(
    uint32_t Locale,
    uint32_t dwCmpFlags,
    int8_t* lpString1,
    int32_t cchCount1,
    int8_t* lpString2,
    int32_t cchCount2);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.CompareStringA(Locale, dwCmpFlags, lpString1, cchCount1, lpString2, cchCount2)
-- Locale : DWORD
-- dwCmpFlags : DWORD
-- lpString1 : CHAR*
-- cchCount1 : INT
-- lpString2 : CHAR*
-- cchCount2 : INT
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('KERNEL32.dll');
const CompareStringA = lib.func('__stdcall', 'CompareStringA', 'int32_t', ['uint32_t', 'uint32_t', 'int8_t *', 'int32_t', 'int8_t *', 'int32_t']);
// CompareStringA(Locale, dwCmpFlags, lpString1, cchCount1, lpString2, cchCount2)
// Locale : DWORD -> 'uint32_t'
// dwCmpFlags : DWORD -> 'uint32_t'
// lpString1 : CHAR* -> 'int8_t *'
// cchCount1 : INT -> 'int32_t'
// lpString2 : CHAR* -> 'int8_t *'
// cchCount2 : INT -> 'int32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("KERNEL32.dll", {
  CompareStringA: { parameters: ["u32", "u32", "pointer", "i32", "pointer", "i32"], result: "i32" },
});
// lib.symbols.CompareStringA(Locale, dwCmpFlags, lpString1, cchCount1, lpString2, cchCount2)
// Locale : DWORD -> "u32"
// dwCmpFlags : DWORD -> "u32"
// lpString1 : CHAR* -> "pointer"
// cchCount1 : INT -> "i32"
// lpString2 : CHAR* -> "pointer"
// cchCount2 : INT -> "i32"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t CompareStringA(
    uint32_t Locale,
    uint32_t dwCmpFlags,
    int8_t* lpString1,
    int32_t cchCount1,
    int8_t* lpString2,
    int32_t cchCount2);
C, "KERNEL32.dll");
// $ffi->CompareStringA(Locale, dwCmpFlags, lpString1, cchCount1, lpString2, cchCount2);
// Locale : DWORD
// dwCmpFlags : DWORD
// lpString1 : CHAR*
// cchCount1 : INT
// lpString2 : CHAR*
// cchCount2 : 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, W32APIOptions.ASCII_OPTIONS);
    int CompareStringA(
        int Locale,   // DWORD
        int dwCmpFlags,   // DWORD
        byte[] lpString1,   // CHAR*
        int cchCount1,   // INT
        byte[] lpString2,   // CHAR*
        int cchCount2   // INT
    );
}
@[Link("kernel32")]
lib LibKERNEL32
  fun CompareStringA = CompareStringA(
    Locale : UInt32,   # DWORD
    dwCmpFlags : UInt32,   # DWORD
    lpString1 : Int8*,   # CHAR*
    cchCount1 : Int32,   # INT
    lpString2 : Int8*,   # CHAR*
    cchCount2 : 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 CompareStringANative = Int32 Function(Uint32, Uint32, Pointer<Int8>, Int32, Pointer<Int8>, Int32);
typedef CompareStringADart = int Function(int, int, Pointer<Int8>, int, Pointer<Int8>, int);
final CompareStringA = DynamicLibrary.open('KERNEL32.dll')
    .lookupFunction<CompareStringANative, CompareStringADart>('CompareStringA');
// Locale : DWORD -> Uint32
// dwCmpFlags : DWORD -> Uint32
// lpString1 : CHAR* -> Pointer<Int8>
// cchCount1 : INT -> Int32
// lpString2 : CHAR* -> Pointer<Int8>
// cchCount2 : INT -> Int32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function CompareStringA(
  Locale: DWORD;   // DWORD
  dwCmpFlags: DWORD;   // DWORD
  lpString1: Pointer;   // CHAR*
  cchCount1: Integer;   // INT
  lpString2: Pointer;   // CHAR*
  cchCount2: Integer   // INT
): Integer; stdcall;
  external 'KERNEL32.dll' name 'CompareStringA';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "CompareStringA"
  c_CompareStringA :: Word32 -> Word32 -> Ptr Int8 -> Int32 -> Ptr Int8 -> Int32 -> IO Int32
-- Locale : DWORD -> Word32
-- dwCmpFlags : DWORD -> Word32
-- lpString1 : CHAR* -> Ptr Int8
-- cchCount1 : INT -> Int32
-- lpString2 : CHAR* -> Ptr Int8
-- cchCount2 : INT -> Int32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let comparestringa =
  foreign "CompareStringA"
    (uint32_t @-> uint32_t @-> (ptr int8_t) @-> int32_t @-> (ptr int8_t) @-> int32_t @-> returning int32_t)
(* Locale : DWORD -> uint32_t *)
(* dwCmpFlags : DWORD -> uint32_t *)
(* lpString1 : CHAR* -> (ptr int8_t) *)
(* cchCount1 : INT -> int32_t *)
(* lpString2 : CHAR* -> (ptr int8_t) *)
(* cchCount2 : 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 ("CompareStringA" compare-string-a :convention :stdcall) :int32
  (locale :uint32)   ; DWORD
  (dw-cmp-flags :uint32)   ; DWORD
  (lp-string1 :pointer)   ; CHAR*
  (cch-count1 :int32)   ; INT
  (lp-string2 :pointer)   ; CHAR*
  (cch-count2 :int32))   ; INT
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $CompareStringA = Win32::API::More->new('KERNEL32',
    'int CompareStringA(DWORD Locale, DWORD dwCmpFlags, LPVOID lpString1, int cchCount1, LPVOID lpString2, int cchCount2)');
# my $ret = $CompareStringA->Call($Locale, $dwCmpFlags, $lpString1, $cchCount1, $lpString2, $cchCount2);
# Locale : DWORD -> DWORD
# dwCmpFlags : DWORD -> DWORD
# lpString1 : CHAR* -> LPVOID
# cchCount1 : INT -> int
# lpString2 : CHAR* -> LPVOID
# cchCount2 : INT -> int
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

文字セット違い
類似 API
使用する型