StrRChrW
関数範囲内で最後に出現する指定文字を検索する。
シグネチャ
// SHLWAPI.dll (Unicode / -W)
#include <windows.h>
LPWSTR StrRChrW(
LPCWSTR pszStart,
LPCWSTR pszEnd, // optional
WCHAR wMatch
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| pszStart | LPCWSTR | in | 検索対象となる null 終端文字列へのポインター。 |
| pszEnd | LPCWSTR | inoptional | 検索範囲を定義する、ソース文字列内へのポインター。pszEnd が文字列内のある文字を指すように設定すると、その直前の文字までで検索が停止します。pszEnd を NULL に設定すると、文字列全体が検索されます。 |
| wMatch | WCHAR | in | 検索する文字。 |
戻り値の型: LPWSTR
公式ドキュメント
文字列内で指定した文字が最後に出現する位置を検索します。比較では大文字と小文字が区別されます。(Unicode)
戻り値
型: PTSTR
成功した場合は文字列内でその文字が最後に出現する位置へのポインターを返します。見つからなかった場合は NULL を返します。
解説(Remarks)
この比較では、pszEnd が文字列の末尾を指していることを前提とします。
メモ
shlwapi.h ヘッダーは StrRChr をエイリアスとして定義しており、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 (Unicode / -W)
#include <windows.h>
LPWSTR StrRChrW(
LPCWSTR pszStart,
LPCWSTR pszEnd, // optional
WCHAR wMatch
);[DllImport("SHLWAPI.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern IntPtr StrRChrW(
[MarshalAs(UnmanagedType.LPWStr)] string pszStart, // LPCWSTR
[MarshalAs(UnmanagedType.LPWStr)] string pszEnd, // LPCWSTR optional
char wMatch // WCHAR
);<DllImport("SHLWAPI.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function StrRChrW(
<MarshalAs(UnmanagedType.LPWStr)> pszStart As String, ' LPCWSTR
<MarshalAs(UnmanagedType.LPWStr)> pszEnd As String, ' LPCWSTR optional
wMatch As Char ' WCHAR
) As IntPtr
End Function' pszStart : LPCWSTR
' pszEnd : LPCWSTR optional
' wMatch : WCHAR
Declare PtrSafe Function StrRChrW Lib "shlwapi" ( _
ByVal pszStart As LongPtr, _
ByVal pszEnd As LongPtr, _
ByVal wMatch As Integer) As LongPtr
' 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
StrRChrW = ctypes.windll.shlwapi.StrRChrW
StrRChrW.restype = wintypes.LPWSTR
StrRChrW.argtypes = [
wintypes.LPCWSTR, # pszStart : LPCWSTR
wintypes.LPCWSTR, # pszEnd : LPCWSTR optional
ctypes.c_wchar, # wMatch : WCHAR
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('SHLWAPI.dll')
StrRChrW = Fiddle::Function.new(
lib['StrRChrW'],
[
Fiddle::TYPE_VOIDP, # pszStart : LPCWSTR
Fiddle::TYPE_VOIDP, # pszEnd : LPCWSTR optional
Fiddle::TYPE_SHORT, # wMatch : WCHAR
],
Fiddle::TYPE_VOIDP)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"#[link(name = "shlwapi")]
extern "system" {
fn StrRChrW(
pszStart: *const u16, // LPCWSTR
pszEnd: *const u16, // LPCWSTR optional
wMatch: u16 // WCHAR
) -> *mut u16;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("SHLWAPI.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr StrRChrW([MarshalAs(UnmanagedType.LPWStr)] string pszStart, [MarshalAs(UnmanagedType.LPWStr)] string pszEnd, char wMatch);
"@
$api = Add-Type -MemberDefinition $sig -Name 'SHLWAPI_StrRChrW' -Namespace Win32 -PassThru
# $api::StrRChrW(pszStart, pszEnd, wMatch)#uselib "SHLWAPI.dll"
#func global StrRChrW "StrRChrW" wptr, wptr, wptr
; StrRChrW pszStart, pszEnd, wMatch ; 戻り値は stat
; pszStart : LPCWSTR -> "wptr"
; pszEnd : LPCWSTR optional -> "wptr"
; wMatch : WCHAR -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "SHLWAPI.dll"
#cfunc global StrRChrW "StrRChrW" wstr, wstr, int
; res = StrRChrW(pszStart, pszEnd, wMatch)
; pszStart : LPCWSTR -> "wstr"
; pszEnd : LPCWSTR optional -> "wstr"
; wMatch : WCHAR -> "int"; LPWSTR StrRChrW(LPCWSTR pszStart, LPCWSTR pszEnd, WCHAR wMatch)
#uselib "SHLWAPI.dll"
#cfunc global StrRChrW "StrRChrW" wstr, wstr, int
; res = StrRChrW(pszStart, pszEnd, wMatch)
; pszStart : LPCWSTR -> "wstr"
; pszEnd : LPCWSTR optional -> "wstr"
; wMatch : WCHAR -> "int"import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
shlwapi = windows.NewLazySystemDLL("SHLWAPI.dll")
procStrRChrW = shlwapi.NewProc("StrRChrW")
)
// pszStart (LPCWSTR), pszEnd (LPCWSTR optional), wMatch (WCHAR)
r1, _, err := procStrRChrW.Call(
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pszStart))),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pszEnd))),
uintptr(wMatch),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // LPWSTRfunction StrRChrW(
pszStart: PWideChar; // LPCWSTR
pszEnd: PWideChar; // LPCWSTR optional
wMatch: WideChar // WCHAR
): PWideChar; stdcall;
external 'SHLWAPI.dll' name 'StrRChrW';result := DllCall("SHLWAPI\StrRChrW"
, "WStr", pszStart ; LPCWSTR
, "WStr", pszEnd ; LPCWSTR optional
, "Short", wMatch ; WCHAR
, "Ptr") ; return: LPWSTR●StrRChrW(pszStart, pszEnd, wMatch) = DLL("SHLWAPI.dll", "char* StrRChrW(char*, char*, int)")
# 呼び出し: StrRChrW(pszStart, pszEnd, wMatch)
# pszStart : LPCWSTR -> "char*"
# pszEnd : LPCWSTR optional -> "char*"
# wMatch : WCHAR -> "int"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。const std = @import("std");
extern "shlwapi" fn StrRChrW(
pszStart: [*c]const u16, // LPCWSTR
pszEnd: [*c]const u16, // LPCWSTR optional
wMatch: u16 // WCHAR
) callconv(std.os.windows.WINAPI) [*c]const u16;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。proc StrRChrW(
pszStart: WideCString, # LPCWSTR
pszEnd: WideCString, # LPCWSTR optional
wMatch: uint16 # WCHAR
): WideCString {.importc: "StrRChrW", stdcall, dynlib: "SHLWAPI.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。pragma(lib, "shlwapi");
extern(Windows)
const(wchar)* StrRChrW(
const(wchar)* pszStart, // LPCWSTR
const(wchar)* pszEnd, // LPCWSTR optional
wchar wMatch // WCHAR
);ccall((:StrRChrW, "SHLWAPI.dll"), stdcall, Cwstring,
(Cwstring, Cwstring, UInt16),
pszStart, pszEnd, wMatch)
# pszStart : LPCWSTR -> Cwstring
# pszEnd : LPCWSTR optional -> Cwstring
# wMatch : WCHAR -> UInt16
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。local ffi = require("ffi")
ffi.cdef[[
const uint16_t* StrRChrW(
const uint16_t* pszStart,
const uint16_t* pszEnd,
uint16_t wMatch);
]]
local shlwapi = ffi.load("shlwapi")
-- shlwapi.StrRChrW(pszStart, pszEnd, wMatch)
-- pszStart : LPCWSTR
-- pszEnd : LPCWSTR optional
-- wMatch : WCHAR
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。const koffi = require('koffi');
const lib = koffi.load('SHLWAPI.dll');
const StrRChrW = lib.func('__stdcall', 'StrRChrW', 'void *', ['str16', 'str16', 'uint16_t']);
// StrRChrW(pszStart, pszEnd, wMatch)
// pszStart : LPCWSTR -> 'str16'
// pszEnd : LPCWSTR optional -> 'str16'
// wMatch : WCHAR -> 'uint16_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("SHLWAPI.dll", {
StrRChrW: { parameters: ["buffer", "buffer", "u16"], result: "pointer" },
});
// lib.symbols.StrRChrW(pszStart, pszEnd, wMatch)
// pszStart : LPCWSTR -> "buffer"
// pszEnd : LPCWSTR optional -> "buffer"
// wMatch : WCHAR -> "u16"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
const uint16_t* StrRChrW(
const uint16_t* pszStart,
const uint16_t* pszEnd,
uint16_t wMatch);
C, "SHLWAPI.dll");
// $ffi->StrRChrW(pszStart, pszEnd, wMatch);
// pszStart : LPCWSTR
// pszEnd : LPCWSTR optional
// wMatch : WCHAR
// 構造体/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.UNICODE_OPTIONS);
Pointer StrRChrW(
WString pszStart, // LPCWSTR
WString pszEnd, // LPCWSTR optional
char wMatch // WCHAR
);
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。@[Link("shlwapi")]
lib LibSHLWAPI
fun StrRChrW = StrRChrW(
pszStart : UInt16*, # LPCWSTR
pszEnd : UInt16*, # LPCWSTR optional
wMatch : UInt16 # WCHAR
) : UInt16*
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef StrRChrWNative = Pointer<Utf16> Function(Pointer<Utf16>, Pointer<Utf16>, Uint16);
typedef StrRChrWDart = Pointer<Utf16> Function(Pointer<Utf16>, Pointer<Utf16>, int);
final StrRChrW = DynamicLibrary.open('SHLWAPI.dll')
.lookupFunction<StrRChrWNative, StrRChrWDart>('StrRChrW');
// pszStart : LPCWSTR -> Pointer<Utf16>
// pszEnd : LPCWSTR optional -> Pointer<Utf16>
// wMatch : WCHAR -> Uint16
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function StrRChrW(
pszStart: PWideChar; // LPCWSTR
pszEnd: PWideChar; // LPCWSTR optional
wMatch: WideChar // WCHAR
): PWideChar; stdcall;
external 'SHLWAPI.dll' name 'StrRChrW';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "StrRChrW"
c_StrRChrW :: CWString -> CWString -> Word16 -> IO CWString
-- pszStart : LPCWSTR -> CWString
-- pszEnd : LPCWSTR optional -> CWString
-- wMatch : WCHAR -> Word16
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let strrchrw =
foreign "StrRChrW"
((ptr uint16_t) @-> (ptr uint16_t) @-> uint16_t @-> returning (ptr uint16_t))
(* pszStart : LPCWSTR -> (ptr uint16_t) *)
(* pszEnd : LPCWSTR optional -> (ptr uint16_t) *)
(* wMatch : WCHAR -> uint16_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library shlwapi (t "SHLWAPI.dll"))
(cffi:use-foreign-library shlwapi)
(cffi:defcfun ("StrRChrW" str-rchr-w :convention :stdcall) (:string :encoding :utf-16le)
(psz-start (:string :encoding :utf-16le)) ; LPCWSTR
(psz-end (:string :encoding :utf-16le)) ; LPCWSTR optional
(w-match :uint16)) ; WCHAR
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $StrRChrW = Win32::API::More->new('SHLWAPI',
'LPWSTR StrRChrW(LPCWSTR pszStart, LPCWSTR pszEnd, WORD wMatch)');
# my $ret = $StrRChrW->Call($pszStart, $pszEnd, $wMatch);
# pszStart : LPCWSTR -> LPCWSTR
# pszEnd : LPCWSTR optional -> LPCWSTR
# wMatch : WCHAR -> WORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。関連項目
文字セット違い
- f StrRChrA (ANSI版) — 範囲内で最後に出現する指定文字を検索する。