StrCmpNA
関数先頭から指定文字数だけ二つの文字列を比較する。
シグネチャ
// SHLWAPI.dll (ANSI / -A)
#include <windows.h>
INT StrCmpNA(
LPCSTR psz1,
LPCSTR psz2,
INT nChar
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| psz1 | LPCSTR | in | 比較対象となる 1 番目の null 終端文字列へのポインター。 |
| psz2 | LPCSTR | in | 比較対象となる 2 番目の null 終端文字列へのポインター。 |
| nChar | INT | in | 各文字列の先頭から比較する文字数。 |
戻り値の型: INT
公式ドキュメント
2 つの文字列の先頭から指定された数の文字を比較し、それらが同一かどうかを判定します。比較では大文字と小文字が区別されます。StrNCmp マクロは、名前が異なるだけでこの関数と同じです。(ANSI)
戻り値
型: int
文字列が同一の場合は 0 を返します。psz1 が指す文字列の先頭 nChar 文字が psz2 が指す文字列の先頭 nChar 文字より大きい場合は正の値を返します。psz1 が指す文字列の先頭 nChar 文字が psz2 が指す文字列の先頭 nChar 文字より小さい場合は負の値を返します。
解説(Remarks)
メモ
shlwapi.h ヘッダーは StrCmpN をエイリアスとして定義しており、UNICODE プリプロセッサ定数の定義に基づいて、この関数の ANSI 版または Unicode 版を自動的に選択します。エンコード非依存のエイリアスを、エンコード非依存ではないコードと混在して使用すると、不一致が生じ、コンパイルエラーまたは実行時エラーの原因となることがあります。詳細については、「Conventions for Function Prototypes」を参照してください。
出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// SHLWAPI.dll (ANSI / -A)
#include <windows.h>
INT StrCmpNA(
LPCSTR psz1,
LPCSTR psz2,
INT nChar
);[DllImport("SHLWAPI.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
static extern int StrCmpNA(
[MarshalAs(UnmanagedType.LPStr)] string psz1, // LPCSTR
[MarshalAs(UnmanagedType.LPStr)] string psz2, // LPCSTR
int nChar // INT
);<DllImport("SHLWAPI.dll", CharSet:=CharSet.Ansi, ExactSpelling:=True)>
Public Shared Function StrCmpNA(
<MarshalAs(UnmanagedType.LPStr)> psz1 As String, ' LPCSTR
<MarshalAs(UnmanagedType.LPStr)> psz2 As String, ' LPCSTR
nChar As Integer ' INT
) As Integer
End Function' psz1 : LPCSTR
' psz2 : LPCSTR
' nChar : INT
Declare PtrSafe Function StrCmpNA Lib "shlwapi" ( _
ByVal psz1 As String, _
ByVal psz2 As String, _
ByVal nChar As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
StrCmpNA = ctypes.windll.shlwapi.StrCmpNA
StrCmpNA.restype = ctypes.c_int
StrCmpNA.argtypes = [
wintypes.LPCSTR, # psz1 : LPCSTR
wintypes.LPCSTR, # psz2 : LPCSTR
ctypes.c_int, # nChar : INT
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('SHLWAPI.dll')
StrCmpNA = Fiddle::Function.new(
lib['StrCmpNA'],
[
Fiddle::TYPE_VOIDP, # psz1 : LPCSTR
Fiddle::TYPE_VOIDP, # psz2 : LPCSTR
Fiddle::TYPE_INT, # nChar : INT
],
Fiddle::TYPE_INT)#[link(name = "shlwapi")]
extern "system" {
fn StrCmpNA(
psz1: *const u8, // LPCSTR
psz2: *const u8, // LPCSTR
nChar: i32 // INT
) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("SHLWAPI.dll", CharSet = CharSet.Ansi)]
public static extern int StrCmpNA([MarshalAs(UnmanagedType.LPStr)] string psz1, [MarshalAs(UnmanagedType.LPStr)] string psz2, int nChar);
"@
$api = Add-Type -MemberDefinition $sig -Name 'SHLWAPI_StrCmpNA' -Namespace Win32 -PassThru
# $api::StrCmpNA(psz1, psz2, nChar)#uselib "SHLWAPI.dll"
#func global StrCmpNA "StrCmpNA" sptr, sptr, sptr
; StrCmpNA psz1, psz2, nChar ; 戻り値は stat
; psz1 : LPCSTR -> "sptr"
; psz2 : LPCSTR -> "sptr"
; nChar : INT -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "SHLWAPI.dll"
#cfunc global StrCmpNA "StrCmpNA" str, str, int
; res = StrCmpNA(psz1, psz2, nChar)
; psz1 : LPCSTR -> "str"
; psz2 : LPCSTR -> "str"
; nChar : INT -> "int"; INT StrCmpNA(LPCSTR psz1, LPCSTR psz2, INT nChar)
#uselib "SHLWAPI.dll"
#cfunc global StrCmpNA "StrCmpNA" str, str, int
; res = StrCmpNA(psz1, psz2, nChar)
; psz1 : LPCSTR -> "str"
; psz2 : LPCSTR -> "str"
; nChar : INT -> "int"import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
shlwapi = windows.NewLazySystemDLL("SHLWAPI.dll")
procStrCmpNA = shlwapi.NewProc("StrCmpNA")
)
// psz1 (LPCSTR), psz2 (LPCSTR), nChar (INT)
r1, _, err := procStrCmpNA.Call(
uintptr(unsafe.Pointer(windows.BytePtrFromString(psz1))),
uintptr(unsafe.Pointer(windows.BytePtrFromString(psz2))),
uintptr(nChar),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // INTfunction StrCmpNA(
psz1: PAnsiChar; // LPCSTR
psz2: PAnsiChar; // LPCSTR
nChar: Integer // INT
): Integer; stdcall;
external 'SHLWAPI.dll' name 'StrCmpNA';result := DllCall("SHLWAPI\StrCmpNA"
, "AStr", psz1 ; LPCSTR
, "AStr", psz2 ; LPCSTR
, "Int", nChar ; INT
, "Int") ; return: INT●StrCmpNA(psz1, psz2, nChar) = DLL("SHLWAPI.dll", "int StrCmpNA(char*, char*, int)")
# 呼び出し: StrCmpNA(psz1, psz2, nChar)
# psz1 : LPCSTR -> "char*"
# psz2 : LPCSTR -> "char*"
# nChar : INT -> "int"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "shlwapi" fn StrCmpNA(
psz1: [*c]const u8, // LPCSTR
psz2: [*c]const u8, // LPCSTR
nChar: i32 // INT
) callconv(std.os.windows.WINAPI) i32;proc StrCmpNA(
psz1: cstring, # LPCSTR
psz2: cstring, # LPCSTR
nChar: int32 # INT
): int32 {.importc: "StrCmpNA", stdcall, dynlib: "SHLWAPI.dll".}pragma(lib, "shlwapi");
extern(Windows)
int StrCmpNA(
const(char)* psz1, // LPCSTR
const(char)* psz2, // LPCSTR
int nChar // INT
);ccall((:StrCmpNA, "SHLWAPI.dll"), stdcall, Int32,
(Cstring, Cstring, Int32),
psz1, psz2, nChar)
# psz1 : LPCSTR -> Cstring
# psz2 : LPCSTR -> Cstring
# nChar : INT -> Int32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int32_t StrCmpNA(
const char* psz1,
const char* psz2,
int32_t nChar);
]]
local shlwapi = ffi.load("shlwapi")
-- shlwapi.StrCmpNA(psz1, psz2, nChar)
-- psz1 : LPCSTR
-- psz2 : LPCSTR
-- nChar : INT
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('SHLWAPI.dll');
const StrCmpNA = lib.func('__stdcall', 'StrCmpNA', 'int32_t', ['str', 'str', 'int32_t']);
// StrCmpNA(psz1, psz2, nChar)
// psz1 : LPCSTR -> 'str'
// psz2 : LPCSTR -> 'str'
// nChar : INT -> 'int32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("SHLWAPI.dll", {
StrCmpNA: { parameters: ["buffer", "buffer", "i32"], result: "i32" },
});
// lib.symbols.StrCmpNA(psz1, psz2, nChar)
// psz1 : LPCSTR -> "buffer"
// psz2 : LPCSTR -> "buffer"
// nChar : INT -> "i32"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t StrCmpNA(
const char* psz1,
const char* psz2,
int32_t nChar);
C, "SHLWAPI.dll");
// $ffi->StrCmpNA(psz1, psz2, nChar);
// psz1 : LPCSTR
// psz2 : LPCSTR
// nChar : 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 Shlwapi extends StdCallLibrary {
Shlwapi INSTANCE = Native.load("shlwapi", Shlwapi.class, W32APIOptions.ASCII_OPTIONS);
int StrCmpNA(
String psz1, // LPCSTR
String psz2, // LPCSTR
int nChar // INT
);
}@[Link("shlwapi")]
lib LibSHLWAPI
fun StrCmpNA = StrCmpNA(
psz1 : UInt8*, # LPCSTR
psz2 : UInt8*, # LPCSTR
nChar : 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 StrCmpNANative = Int32 Function(Pointer<Utf8>, Pointer<Utf8>, Int32);
typedef StrCmpNADart = int Function(Pointer<Utf8>, Pointer<Utf8>, int);
final StrCmpNA = DynamicLibrary.open('SHLWAPI.dll')
.lookupFunction<StrCmpNANative, StrCmpNADart>('StrCmpNA');
// psz1 : LPCSTR -> Pointer<Utf8>
// psz2 : LPCSTR -> Pointer<Utf8>
// nChar : INT -> Int32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function StrCmpNA(
psz1: PAnsiChar; // LPCSTR
psz2: PAnsiChar; // LPCSTR
nChar: Integer // INT
): Integer; stdcall;
external 'SHLWAPI.dll' name 'StrCmpNA';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "StrCmpNA"
c_StrCmpNA :: CString -> CString -> Int32 -> IO Int32
-- psz1 : LPCSTR -> CString
-- psz2 : LPCSTR -> CString
-- nChar : INT -> Int32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let strcmpna =
foreign "StrCmpNA"
(string @-> string @-> int32_t @-> returning int32_t)
(* psz1 : LPCSTR -> string *)
(* psz2 : LPCSTR -> string *)
(* nChar : INT -> int32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library shlwapi (t "SHLWAPI.dll"))
(cffi:use-foreign-library shlwapi)
(cffi:defcfun ("StrCmpNA" str-cmp-na :convention :stdcall) :int32
(psz1 :string) ; LPCSTR
(psz2 :string) ; LPCSTR
(n-char :int32)) ; INT
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $StrCmpNA = Win32::API::More->new('SHLWAPI',
'int StrCmpNA(LPCSTR psz1, LPCSTR psz2, int nChar)');
# my $ret = $StrCmpNA->Call($psz1, $psz2, $nChar);
# psz1 : LPCSTR -> LPCSTR
# psz2 : LPCSTR -> LPCSTR
# nChar : INT -> int
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
文字セット違い
- f StrCmpNW (Unicode版) — 先頭から指定文字数だけ二つの文字列を比較する。