FindStringOrdinal
関数シグネチャ
// KERNEL32.dll
#include <windows.h>
INT FindStringOrdinal(
DWORD dwFindStringOrdinalFlags,
LPCWSTR lpStringSource,
INT cchSource,
LPCWSTR lpStringValue,
INT cchValue,
BOOL bIgnoreCase
);パラメーター
| 名前 | 型 | 方向 | 説明 | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| dwFindStringOrdinalFlags | DWORD | in | 検索操作の詳細を指定するフラグ。これらのフラグは相互に排他的で、既定値は FIND_FROMSTART です。アプリケーションは検索フラグのいずれか 1 つのみを指定できます。
| ||||||||||
| lpStringSource | LPCWSTR | in | ソース文字列へのポインター。この文字列の中で、関数は lpStringValue で指定された文字列を検索します。 | ||||||||||
| cchSource | INT | in | lpStringSource で示される文字列の、終端の null 文字を除いた文字数単位のサイズ。アプリケーションは通常、正の数または 0 を指定する必要があります。ソース文字列が null 終端されており、関数にサイズを自動的に計算させたい場合は -1 を指定できます。 | ||||||||||
| lpStringValue | LPCWSTR | in | 検索文字列へのポインター。関数はこの文字列をソース文字列の中から検索します。 | ||||||||||
| cchValue | INT | in | lpStringValue で示される文字列の、終端の null 文字を除いた文字数単位のサイズ。アプリケーションは通常、正の数または 0 を指定する必要があります。文字列が null 終端されており、関数にサイズを自動的に計算させたい場合は -1 を指定できます。 | ||||||||||
| bIgnoreCase | BOOL | in | 関数で大文字と小文字を区別しない比較を行う場合は TRUE、それ以外の場合は FALSE を指定します。この比較は言語的な操作ではなく、すべてのロケールや言語に適しているわけではありません。その動作は英語の場合と同様です。 |
戻り値の型: INT
公式ドキュメント
非言語的な比較により、ある Unicode 文字列(ワイド文字)を別の Unicode 文字列の中から検索します。
戻り値
成功した場合、lpStringSource で示されるソース文字列に対する 0 から始まるインデックスを返します。関数が成功した場合、見つかった文字列は lpStringValue の値と同じサイズになります。戻り値 0 は、関数がソース文字列の先頭で一致を見つけたことを示します。
関数が成功しなかった場合、または検索文字列が見つからなかった場合は -1 を返します。拡張エラー情報を取得するには、アプリケーションは GetLastError を呼び出すことができ、次のいずれかのエラーコードが返されることがあります。
- ERROR_INVALID_FLAGS。フラグに指定された値が無効でした。
- ERROR_INVALID_PARAMETER。いずれかのパラメーター値が無効でした。
- ERROR_SUCCESS。アクションは正常に完了しましたが、結果が得られませんでした。
解説(Remarks)
FindStringOrdinal はバイナリ比較を行うため、言語的に適切な結果を返しません。順序による比較は英語のソート動作と誤解されることがあります。ただし、言語的に重要でない程度の差異がある文字については一致を検出しません。適切なソート関数の選択については、Sorting を参照してください。
失敗時に 0 を返す NLS 関数とは対照的に、この関数は失敗した場合に -1 を返します。成功時には 0 から始まるインデックスを返します。このインデックスを使用することで、関数は 1 つずれるエラー(off-by-one エラー)や 1 文字分のバッファーオーバーランを回避できます。
この関数は、成功した場合でも SetLastError を呼び出す数少ない NLS 関数の 1 つです。この呼び出しは、検索文字列の一致に失敗したときにスレッドの最後のエラーをクリアするために行われます。これにより、GetLastError が返す値がクリアされます。
Windows 8 以降: FindStringOrdinal は Libloaderapi.h で宣言されています。Windows 8 より前では、Winnls.h で宣言されていました。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// KERNEL32.dll
#include <windows.h>
INT FindStringOrdinal(
DWORD dwFindStringOrdinalFlags,
LPCWSTR lpStringSource,
INT cchSource,
LPCWSTR lpStringValue,
INT cchValue,
BOOL bIgnoreCase
);[DllImport("KERNEL32.dll", SetLastError = true, ExactSpelling = true)]
static extern int FindStringOrdinal(
uint dwFindStringOrdinalFlags, // DWORD
[MarshalAs(UnmanagedType.LPWStr)] string lpStringSource, // LPCWSTR
int cchSource, // INT
[MarshalAs(UnmanagedType.LPWStr)] string lpStringValue, // LPCWSTR
int cchValue, // INT
bool bIgnoreCase // BOOL
);<DllImport("KERNEL32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function FindStringOrdinal(
dwFindStringOrdinalFlags As UInteger, ' DWORD
<MarshalAs(UnmanagedType.LPWStr)> lpStringSource As String, ' LPCWSTR
cchSource As Integer, ' INT
<MarshalAs(UnmanagedType.LPWStr)> lpStringValue As String, ' LPCWSTR
cchValue As Integer, ' INT
bIgnoreCase As Boolean ' BOOL
) As Integer
End Function' dwFindStringOrdinalFlags : DWORD
' lpStringSource : LPCWSTR
' cchSource : INT
' lpStringValue : LPCWSTR
' cchValue : INT
' bIgnoreCase : BOOL
Declare PtrSafe Function FindStringOrdinal Lib "kernel32" ( _
ByVal dwFindStringOrdinalFlags As Long, _
ByVal lpStringSource As LongPtr, _
ByVal cchSource As Long, _
ByVal lpStringValue As LongPtr, _
ByVal cchValue 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
FindStringOrdinal = ctypes.windll.kernel32.FindStringOrdinal
FindStringOrdinal.restype = ctypes.c_int
FindStringOrdinal.argtypes = [
wintypes.DWORD, # dwFindStringOrdinalFlags : DWORD
wintypes.LPCWSTR, # lpStringSource : LPCWSTR
ctypes.c_int, # cchSource : INT
wintypes.LPCWSTR, # lpStringValue : LPCWSTR
ctypes.c_int, # cchValue : 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')
FindStringOrdinal = Fiddle::Function.new(
lib['FindStringOrdinal'],
[
-Fiddle::TYPE_INT, # dwFindStringOrdinalFlags : DWORD
Fiddle::TYPE_VOIDP, # lpStringSource : LPCWSTR
Fiddle::TYPE_INT, # cchSource : INT
Fiddle::TYPE_VOIDP, # lpStringValue : LPCWSTR
Fiddle::TYPE_INT, # cchValue : INT
Fiddle::TYPE_INT, # bIgnoreCase : BOOL
],
Fiddle::TYPE_INT)#[link(name = "kernel32")]
extern "system" {
fn FindStringOrdinal(
dwFindStringOrdinalFlags: u32, // DWORD
lpStringSource: *const u16, // LPCWSTR
cchSource: i32, // INT
lpStringValue: *const u16, // LPCWSTR
cchValue: 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 FindStringOrdinal(uint dwFindStringOrdinalFlags, [MarshalAs(UnmanagedType.LPWStr)] string lpStringSource, int cchSource, [MarshalAs(UnmanagedType.LPWStr)] string lpStringValue, int cchValue, bool bIgnoreCase);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_FindStringOrdinal' -Namespace Win32 -PassThru
# $api::FindStringOrdinal(dwFindStringOrdinalFlags, lpStringSource, cchSource, lpStringValue, cchValue, bIgnoreCase)#uselib "KERNEL32.dll"
#func global FindStringOrdinal "FindStringOrdinal" sptr, sptr, sptr, sptr, sptr, sptr
; FindStringOrdinal dwFindStringOrdinalFlags, lpStringSource, cchSource, lpStringValue, cchValue, bIgnoreCase ; 戻り値は stat
; dwFindStringOrdinalFlags : DWORD -> "sptr"
; lpStringSource : LPCWSTR -> "sptr"
; cchSource : INT -> "sptr"
; lpStringValue : LPCWSTR -> "sptr"
; cchValue : INT -> "sptr"
; bIgnoreCase : BOOL -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "KERNEL32.dll"
#cfunc global FindStringOrdinal "FindStringOrdinal" int, wstr, int, wstr, int, int
; res = FindStringOrdinal(dwFindStringOrdinalFlags, lpStringSource, cchSource, lpStringValue, cchValue, bIgnoreCase)
; dwFindStringOrdinalFlags : DWORD -> "int"
; lpStringSource : LPCWSTR -> "wstr"
; cchSource : INT -> "int"
; lpStringValue : LPCWSTR -> "wstr"
; cchValue : INT -> "int"
; bIgnoreCase : BOOL -> "int"; INT FindStringOrdinal(DWORD dwFindStringOrdinalFlags, LPCWSTR lpStringSource, INT cchSource, LPCWSTR lpStringValue, INT cchValue, BOOL bIgnoreCase)
#uselib "KERNEL32.dll"
#cfunc global FindStringOrdinal "FindStringOrdinal" int, wstr, int, wstr, int, int
; res = FindStringOrdinal(dwFindStringOrdinalFlags, lpStringSource, cchSource, lpStringValue, cchValue, bIgnoreCase)
; dwFindStringOrdinalFlags : DWORD -> "int"
; lpStringSource : LPCWSTR -> "wstr"
; cchSource : INT -> "int"
; lpStringValue : LPCWSTR -> "wstr"
; cchValue : INT -> "int"
; bIgnoreCase : BOOL -> "int"import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
procFindStringOrdinal = kernel32.NewProc("FindStringOrdinal")
)
// dwFindStringOrdinalFlags (DWORD), lpStringSource (LPCWSTR), cchSource (INT), lpStringValue (LPCWSTR), cchValue (INT), bIgnoreCase (BOOL)
r1, _, err := procFindStringOrdinal.Call(
uintptr(dwFindStringOrdinalFlags),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpStringSource))),
uintptr(cchSource),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpStringValue))),
uintptr(cchValue),
uintptr(bIgnoreCase),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // INTfunction FindStringOrdinal(
dwFindStringOrdinalFlags: DWORD; // DWORD
lpStringSource: PWideChar; // LPCWSTR
cchSource: Integer; // INT
lpStringValue: PWideChar; // LPCWSTR
cchValue: Integer; // INT
bIgnoreCase: BOOL // BOOL
): Integer; stdcall;
external 'KERNEL32.dll' name 'FindStringOrdinal';result := DllCall("KERNEL32\FindStringOrdinal"
, "UInt", dwFindStringOrdinalFlags ; DWORD
, "WStr", lpStringSource ; LPCWSTR
, "Int", cchSource ; INT
, "WStr", lpStringValue ; LPCWSTR
, "Int", cchValue ; INT
, "Int", bIgnoreCase ; BOOL
, "Int") ; return: INT●FindStringOrdinal(dwFindStringOrdinalFlags, lpStringSource, cchSource, lpStringValue, cchValue, bIgnoreCase) = DLL("KERNEL32.dll", "int FindStringOrdinal(dword, char*, int, char*, int, bool)")
# 呼び出し: FindStringOrdinal(dwFindStringOrdinalFlags, lpStringSource, cchSource, lpStringValue, cchValue, bIgnoreCase)
# dwFindStringOrdinalFlags : DWORD -> "dword"
# lpStringSource : LPCWSTR -> "char*"
# cchSource : INT -> "int"
# lpStringValue : LPCWSTR -> "char*"
# cchValue : INT -> "int"
# bIgnoreCase : BOOL -> "bool"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "kernel32" fn FindStringOrdinal(
dwFindStringOrdinalFlags: u32, // DWORD
lpStringSource: [*c]const u16, // LPCWSTR
cchSource: i32, // INT
lpStringValue: [*c]const u16, // LPCWSTR
cchValue: i32, // INT
bIgnoreCase: i32 // BOOL
) callconv(std.os.windows.WINAPI) i32;proc FindStringOrdinal(
dwFindStringOrdinalFlags: uint32, # DWORD
lpStringSource: WideCString, # LPCWSTR
cchSource: int32, # INT
lpStringValue: WideCString, # LPCWSTR
cchValue: int32, # INT
bIgnoreCase: int32 # BOOL
): int32 {.importc: "FindStringOrdinal", stdcall, dynlib: "KERNEL32.dll".}pragma(lib, "kernel32");
extern(Windows)
int FindStringOrdinal(
uint dwFindStringOrdinalFlags, // DWORD
const(wchar)* lpStringSource, // LPCWSTR
int cchSource, // INT
const(wchar)* lpStringValue, // LPCWSTR
int cchValue, // INT
int bIgnoreCase // BOOL
);ccall((:FindStringOrdinal, "KERNEL32.dll"), stdcall, Int32,
(UInt32, Cwstring, Int32, Cwstring, Int32, Int32),
dwFindStringOrdinalFlags, lpStringSource, cchSource, lpStringValue, cchValue, bIgnoreCase)
# dwFindStringOrdinalFlags : DWORD -> UInt32
# lpStringSource : LPCWSTR -> Cwstring
# cchSource : INT -> Int32
# lpStringValue : LPCWSTR -> Cwstring
# cchValue : INT -> Int32
# bIgnoreCase : BOOL -> Int32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int32_t FindStringOrdinal(
uint32_t dwFindStringOrdinalFlags,
const uint16_t* lpStringSource,
int32_t cchSource,
const uint16_t* lpStringValue,
int32_t cchValue,
int32_t bIgnoreCase);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.FindStringOrdinal(dwFindStringOrdinalFlags, lpStringSource, cchSource, lpStringValue, cchValue, bIgnoreCase)
-- dwFindStringOrdinalFlags : DWORD
-- lpStringSource : LPCWSTR
-- cchSource : INT
-- lpStringValue : LPCWSTR
-- cchValue : INT
-- bIgnoreCase : BOOL
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('KERNEL32.dll');
const FindStringOrdinal = lib.func('__stdcall', 'FindStringOrdinal', 'int32_t', ['uint32_t', 'str16', 'int32_t', 'str16', 'int32_t', 'int32_t']);
// FindStringOrdinal(dwFindStringOrdinalFlags, lpStringSource, cchSource, lpStringValue, cchValue, bIgnoreCase)
// dwFindStringOrdinalFlags : DWORD -> 'uint32_t'
// lpStringSource : LPCWSTR -> 'str16'
// cchSource : INT -> 'int32_t'
// lpStringValue : LPCWSTR -> 'str16'
// cchValue : INT -> 'int32_t'
// bIgnoreCase : BOOL -> 'int32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("KERNEL32.dll", {
FindStringOrdinal: { parameters: ["u32", "buffer", "i32", "buffer", "i32", "i32"], result: "i32" },
});
// lib.symbols.FindStringOrdinal(dwFindStringOrdinalFlags, lpStringSource, cchSource, lpStringValue, cchValue, bIgnoreCase)
// dwFindStringOrdinalFlags : DWORD -> "u32"
// lpStringSource : LPCWSTR -> "buffer"
// cchSource : INT -> "i32"
// lpStringValue : LPCWSTR -> "buffer"
// cchValue : INT -> "i32"
// bIgnoreCase : BOOL -> "i32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t FindStringOrdinal(
uint32_t dwFindStringOrdinalFlags,
const uint16_t* lpStringSource,
int32_t cchSource,
const uint16_t* lpStringValue,
int32_t cchValue,
int32_t bIgnoreCase);
C, "KERNEL32.dll");
// $ffi->FindStringOrdinal(dwFindStringOrdinalFlags, lpStringSource, cchSource, lpStringValue, cchValue, bIgnoreCase);
// dwFindStringOrdinalFlags : DWORD
// lpStringSource : LPCWSTR
// cchSource : INT
// lpStringValue : LPCWSTR
// cchValue : 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 FindStringOrdinal(
int dwFindStringOrdinalFlags, // DWORD
WString lpStringSource, // LPCWSTR
int cchSource, // INT
WString lpStringValue, // LPCWSTR
int cchValue, // INT
boolean bIgnoreCase // BOOL
);
}@[Link("kernel32")]
lib LibKERNEL32
fun FindStringOrdinal = FindStringOrdinal(
dwFindStringOrdinalFlags : UInt32, # DWORD
lpStringSource : UInt16*, # LPCWSTR
cchSource : Int32, # INT
lpStringValue : UInt16*, # LPCWSTR
cchValue : 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 FindStringOrdinalNative = Int32 Function(Uint32, Pointer<Utf16>, Int32, Pointer<Utf16>, Int32, Int32);
typedef FindStringOrdinalDart = int Function(int, Pointer<Utf16>, int, Pointer<Utf16>, int, int);
final FindStringOrdinal = DynamicLibrary.open('KERNEL32.dll')
.lookupFunction<FindStringOrdinalNative, FindStringOrdinalDart>('FindStringOrdinal');
// dwFindStringOrdinalFlags : DWORD -> Uint32
// lpStringSource : LPCWSTR -> Pointer<Utf16>
// cchSource : INT -> Int32
// lpStringValue : LPCWSTR -> Pointer<Utf16>
// cchValue : INT -> Int32
// bIgnoreCase : BOOL -> Int32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function FindStringOrdinal(
dwFindStringOrdinalFlags: DWORD; // DWORD
lpStringSource: PWideChar; // LPCWSTR
cchSource: Integer; // INT
lpStringValue: PWideChar; // LPCWSTR
cchValue: Integer; // INT
bIgnoreCase: BOOL // BOOL
): Integer; stdcall;
external 'KERNEL32.dll' name 'FindStringOrdinal';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "FindStringOrdinal"
c_FindStringOrdinal :: Word32 -> CWString -> Int32 -> CWString -> Int32 -> CInt -> IO Int32
-- dwFindStringOrdinalFlags : DWORD -> Word32
-- lpStringSource : LPCWSTR -> CWString
-- cchSource : INT -> Int32
-- lpStringValue : LPCWSTR -> CWString
-- cchValue : INT -> Int32
-- bIgnoreCase : BOOL -> CInt
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let findstringordinal =
foreign "FindStringOrdinal"
(uint32_t @-> (ptr uint16_t) @-> int32_t @-> (ptr uint16_t) @-> int32_t @-> int32_t @-> returning int32_t)
(* dwFindStringOrdinalFlags : DWORD -> uint32_t *)
(* lpStringSource : LPCWSTR -> (ptr uint16_t) *)
(* cchSource : INT -> int32_t *)
(* lpStringValue : LPCWSTR -> (ptr uint16_t) *)
(* cchValue : 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 ("FindStringOrdinal" find-string-ordinal :convention :stdcall) :int32
(dw-find-string-ordinal-flags :uint32) ; DWORD
(lp-string-source (:string :encoding :utf-16le)) ; LPCWSTR
(cch-source :int32) ; INT
(lp-string-value (:string :encoding :utf-16le)) ; LPCWSTR
(cch-value :int32) ; INT
(b-ignore-case :int32)) ; BOOL
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $FindStringOrdinal = Win32::API::More->new('KERNEL32',
'int FindStringOrdinal(DWORD dwFindStringOrdinalFlags, LPCWSTR lpStringSource, int cchSource, LPCWSTR lpStringValue, int cchValue, BOOL bIgnoreCase)');
# my $ret = $FindStringOrdinal->Call($dwFindStringOrdinalFlags, $lpStringSource, $cchSource, $lpStringValue, $cchValue, $bIgnoreCase);
# dwFindStringOrdinalFlags : DWORD -> DWORD
# lpStringSource : LPCWSTR -> LPCWSTR
# cchSource : INT -> int
# lpStringValue : LPCWSTR -> LPCWSTR
# cchValue : INT -> int
# bIgnoreCase : BOOL -> BOOL
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
- f FindNLSString — ロケールを考慮して文字列内から部分文字列を検索する。
- f FindNLSStringEx — ロケール名を指定して文字列内から部分文字列を検索する。