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

CompareStringW

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

シグネチャ

// KERNEL32.dll  (Unicode / -W)
#include <windows.h>

COMPARESTRING_RESULT CompareStringW(
    DWORD Locale,
    DWORD dwCmpFlags,
    LPCWSTR lpString1,
    INT cchCount1,
    LPCWSTR lpString2,
    INT cchCount2
);

パラメーター

名前方向
LocaleDWORDin
dwCmpFlagsDWORDin
lpString1LPCWSTRin
cchCount1INTin
lpString2LPCWSTRin
cchCount2INTin

戻り値の型: COMPARESTRING_RESULT

各言語での呼び出し定義

// KERNEL32.dll  (Unicode / -W)
#include <windows.h>

COMPARESTRING_RESULT CompareStringW(
    DWORD Locale,
    DWORD dwCmpFlags,
    LPCWSTR lpString1,
    INT cchCount1,
    LPCWSTR lpString2,
    INT cchCount2
);
[DllImport("KERNEL32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern int CompareStringW(
    uint Locale,   // DWORD
    uint dwCmpFlags,   // DWORD
    [MarshalAs(UnmanagedType.LPWStr)] string lpString1,   // LPCWSTR
    int cchCount1,   // INT
    [MarshalAs(UnmanagedType.LPWStr)] string lpString2,   // LPCWSTR
    int cchCount2   // INT
);
<DllImport("KERNEL32.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function CompareStringW(
    Locale As UInteger,   ' DWORD
    dwCmpFlags As UInteger,   ' DWORD
    <MarshalAs(UnmanagedType.LPWStr)> lpString1 As String,   ' LPCWSTR
    cchCount1 As Integer,   ' INT
    <MarshalAs(UnmanagedType.LPWStr)> lpString2 As String,   ' LPCWSTR
    cchCount2 As Integer   ' INT
) As Integer
End Function
' Locale : DWORD
' dwCmpFlags : DWORD
' lpString1 : LPCWSTR
' cchCount1 : INT
' lpString2 : LPCWSTR
' cchCount2 : INT
Declare PtrSafe Function CompareStringW 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
' Unicode(W): 文字列は ByVal As LongPtr とし StrPtr(unicodeStr) を渡す
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

CompareStringW = ctypes.windll.kernel32.CompareStringW
CompareStringW.restype = ctypes.c_int
CompareStringW.argtypes = [
    wintypes.DWORD,  # Locale : DWORD
    wintypes.DWORD,  # dwCmpFlags : DWORD
    wintypes.LPCWSTR,  # lpString1 : LPCWSTR
    ctypes.c_int,  # cchCount1 : INT
    wintypes.LPCWSTR,  # lpString2 : LPCWSTR
    ctypes.c_int,  # cchCount2 : INT
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('KERNEL32.dll')
CompareStringW = Fiddle::Function.new(
  lib['CompareStringW'],
  [
    -Fiddle::TYPE_INT,  # Locale : DWORD
    -Fiddle::TYPE_INT,  # dwCmpFlags : DWORD
    Fiddle::TYPE_VOIDP,  # lpString1 : LPCWSTR
    Fiddle::TYPE_INT,  # cchCount1 : INT
    Fiddle::TYPE_VOIDP,  # lpString2 : LPCWSTR
    Fiddle::TYPE_INT,  # cchCount2 : INT
  ],
  Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "kernel32")]
extern "system" {
    fn CompareStringW(
        Locale: u32,  // DWORD
        dwCmpFlags: u32,  // DWORD
        lpString1: *const u16,  // LPCWSTR
        cchCount1: i32,  // INT
        lpString2: *const u16,  // LPCWSTR
        cchCount2: i32  // INT
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("KERNEL32.dll", CharSet = CharSet.Unicode)]
public static extern int CompareStringW(uint Locale, uint dwCmpFlags, [MarshalAs(UnmanagedType.LPWStr)] string lpString1, int cchCount1, [MarshalAs(UnmanagedType.LPWStr)] string lpString2, int cchCount2);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_CompareStringW' -Namespace Win32 -PassThru
# $api::CompareStringW(Locale, dwCmpFlags, lpString1, cchCount1, lpString2, cchCount2)
#uselib "KERNEL32.dll"
#func global CompareStringW "CompareStringW" wptr, wptr, wptr, wptr, wptr, wptr
; CompareStringW Locale, dwCmpFlags, lpString1, cchCount1, lpString2, cchCount2   ; 戻り値は stat
; Locale : DWORD -> "wptr"
; dwCmpFlags : DWORD -> "wptr"
; lpString1 : LPCWSTR -> "wptr"
; cchCount1 : INT -> "wptr"
; lpString2 : LPCWSTR -> "wptr"
; cchCount2 : INT -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "KERNEL32.dll"
#cfunc global CompareStringW "CompareStringW" int, int, wstr, int, wstr, int
; res = CompareStringW(Locale, dwCmpFlags, lpString1, cchCount1, lpString2, cchCount2)
; Locale : DWORD -> "int"
; dwCmpFlags : DWORD -> "int"
; lpString1 : LPCWSTR -> "wstr"
; cchCount1 : INT -> "int"
; lpString2 : LPCWSTR -> "wstr"
; cchCount2 : INT -> "int"
; COMPARESTRING_RESULT CompareStringW(DWORD Locale, DWORD dwCmpFlags, LPCWSTR lpString1, INT cchCount1, LPCWSTR lpString2, INT cchCount2)
#uselib "KERNEL32.dll"
#cfunc global CompareStringW "CompareStringW" int, int, wstr, int, wstr, int
; res = CompareStringW(Locale, dwCmpFlags, lpString1, cchCount1, lpString2, cchCount2)
; Locale : DWORD -> "int"
; dwCmpFlags : DWORD -> "int"
; lpString1 : LPCWSTR -> "wstr"
; cchCount1 : INT -> "int"
; lpString2 : LPCWSTR -> "wstr"
; cchCount2 : INT -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procCompareStringW = kernel32.NewProc("CompareStringW")
)

// Locale (DWORD), dwCmpFlags (DWORD), lpString1 (LPCWSTR), cchCount1 (INT), lpString2 (LPCWSTR), cchCount2 (INT)
r1, _, err := procCompareStringW.Call(
	uintptr(Locale),
	uintptr(dwCmpFlags),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpString1))),
	uintptr(cchCount1),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpString2))),
	uintptr(cchCount2),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // COMPARESTRING_RESULT
function CompareStringW(
  Locale: DWORD;   // DWORD
  dwCmpFlags: DWORD;   // DWORD
  lpString1: PWideChar;   // LPCWSTR
  cchCount1: Integer;   // INT
  lpString2: PWideChar;   // LPCWSTR
  cchCount2: Integer   // INT
): Integer; stdcall;
  external 'KERNEL32.dll' name 'CompareStringW';
result := DllCall("KERNEL32\CompareStringW"
    , "UInt", Locale   ; DWORD
    , "UInt", dwCmpFlags   ; DWORD
    , "WStr", lpString1   ; LPCWSTR
    , "Int", cchCount1   ; INT
    , "WStr", lpString2   ; LPCWSTR
    , "Int", cchCount2   ; INT
    , "Int")   ; return: COMPARESTRING_RESULT
●CompareStringW(Locale, dwCmpFlags, lpString1, cchCount1, lpString2, cchCount2) = DLL("KERNEL32.dll", "int CompareStringW(dword, dword, char*, int, char*, int)")
# 呼び出し: CompareStringW(Locale, dwCmpFlags, lpString1, cchCount1, lpString2, cchCount2)
# Locale : DWORD -> "dword"
# dwCmpFlags : DWORD -> "dword"
# lpString1 : LPCWSTR -> "char*"
# cchCount1 : INT -> "int"
# lpString2 : LPCWSTR -> "char*"
# cchCount2 : INT -> "int"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。