Win32 API 日本語リファレンス
ホームUI.Shell › PathMatchSpecExW

PathMatchSpecExW

関数
フラグ指定付きでファイル名がパターンに一致するか判定する。
DLLSHLWAPI.dll文字セットUnicode (-W)呼出規約winapi対応OSWindows Vista 以降

シグネチャ

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

HRESULT PathMatchSpecExW(
    LPCWSTR pszFile,
    LPCWSTR pszSpec,
    DWORD dwFlags
);

パラメーター

名前方向説明
pszFileLPCWSTRin照合対象のファイル名を取り出すパスを格納した、null 終端文字列へのポインター。
pszSpecLPCWSTRin検索対象のファイル名パターンを格納した、null 終端文字列へのポインター。これは正確な名前を指定することも、ワイルドカード文字を含めることもできます。パターンを 1 つだけ指定する場合は、dwFlagsPMSF_NORMAL フラグを設定します。複数のパターンを指定する場合は、それらをセミコロンで区切り、PMSF_MULTIPLE フラグを設定します。
dwFlagsDWORDin

検索条件を変更します。次のフラグが有効です。

PMSF_NORMAL (0x00000000)

pszSpec パラメーターは、照合対象となる単一のファイル名パターンを指します。

PMSF_MULTIPLE (0x00000001)

pszSpec パラメーターは、照合対象となるセミコロン区切りのファイル名パターンの一覧を指します。

PMSF_DONT_STRIP_SPACES (0x00010000)

PMSF_NORMAL を使用する場合、pszSpec が指す文字列の先頭の空白を無視しません。PMSF_MULTIPLE を使用する場合、pszSpec が指す文字列に含まれる各ファイルタイプの先頭の空白を無視しません。このフラグは PMSF_NORMAL および PMSF_MULTIPLE と組み合わせて使用できます。

- dwFlags.PMSF_DONT_STRIP_SPACES (0x00010000)

PMSF_NORMAL を使用する場合、pszSpec が指す文字列の先頭の空白を無視しません。PMSF_MULTIPLE を使用する場合、pszSpec が指す文字列に含まれる各ファイルタイプの先頭の空白を無視しません。このフラグは PMSF_NORMAL および PMSF_MULTIPLE と組み合わせて使用できます。

- dwFlags.PMSF_MULTIPLE (0x00000001)

pszSpec パラメーターは、照合対象となるセミコロン区切りのファイル名パターンの一覧を指します。

- dwFlags.PMSF_NORMAL (0x00000000)

pszSpec パラメーターは、照合対象となる単一のファイル名パターンを指します。

戻り値の型: HRESULT

公式ドキュメント

パスから取り出したファイル名を、1 つ以上のファイル名パターンと照合します。(Unicode)

戻り値

型: HRESULT

次のいずれかの値を返します。

戻り値 説明
S_OK
pszSpec で指定したファイル名パターンが、pszFile が指す文字列内で見つかったファイル名と一致しました。
S_FALSE
pszSpec で指定したいずれのファイル名パターンも、pszFile が指す文字列内で見つかったファイル名と一致しませんでした。

解説(Remarks)

メモ

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

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

各言語での呼び出し定義

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

HRESULT PathMatchSpecExW(
    LPCWSTR pszFile,
    LPCWSTR pszSpec,
    DWORD dwFlags
);
[DllImport("SHLWAPI.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern int PathMatchSpecExW(
    [MarshalAs(UnmanagedType.LPWStr)] string pszFile,   // LPCWSTR
    [MarshalAs(UnmanagedType.LPWStr)] string pszSpec,   // LPCWSTR
    uint dwFlags   // DWORD
);
<DllImport("SHLWAPI.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function PathMatchSpecExW(
    <MarshalAs(UnmanagedType.LPWStr)> pszFile As String,   ' LPCWSTR
    <MarshalAs(UnmanagedType.LPWStr)> pszSpec As String,   ' LPCWSTR
    dwFlags As UInteger   ' DWORD
) As Integer
End Function
' pszFile : LPCWSTR
' pszSpec : LPCWSTR
' dwFlags : DWORD
Declare PtrSafe Function PathMatchSpecExW Lib "shlwapi" ( _
    ByVal pszFile As LongPtr, _
    ByVal pszSpec As LongPtr, _
    ByVal dwFlags 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

PathMatchSpecExW = ctypes.windll.shlwapi.PathMatchSpecExW
PathMatchSpecExW.restype = ctypes.c_int
PathMatchSpecExW.argtypes = [
    wintypes.LPCWSTR,  # pszFile : LPCWSTR
    wintypes.LPCWSTR,  # pszSpec : LPCWSTR
    wintypes.DWORD,  # dwFlags : DWORD
]
require 'fiddle'
require 'fiddle/import'

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

var (
	shlwapi = windows.NewLazySystemDLL("SHLWAPI.dll")
	procPathMatchSpecExW = shlwapi.NewProc("PathMatchSpecExW")
)

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

extern "shlwapi" fn PathMatchSpecExW(
    pszFile: [*c]const u16, // LPCWSTR
    pszSpec: [*c]const u16, // LPCWSTR
    dwFlags: u32 // DWORD
) callconv(std.os.windows.WINAPI) i32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。
proc PathMatchSpecExW(
    pszFile: WideCString,  # LPCWSTR
    pszSpec: WideCString,  # LPCWSTR
    dwFlags: uint32  # DWORD
): int32 {.importc: "PathMatchSpecExW", stdcall, dynlib: "SHLWAPI.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "shlwapi");
extern(Windows)
int PathMatchSpecExW(
    const(wchar)* pszFile,   // LPCWSTR
    const(wchar)* pszSpec,   // LPCWSTR
    uint dwFlags   // DWORD
);
ccall((:PathMatchSpecExW, "SHLWAPI.dll"), stdcall, Int32,
      (Cwstring, Cwstring, UInt32),
      pszFile, pszSpec, dwFlags)
# pszFile : LPCWSTR -> Cwstring
# pszSpec : LPCWSTR -> Cwstring
# dwFlags : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。
local ffi = require("ffi")
ffi.cdef[[
int32_t PathMatchSpecExW(
    const uint16_t* pszFile,
    const uint16_t* pszSpec,
    uint32_t dwFlags);
]]
local shlwapi = ffi.load("shlwapi")
-- shlwapi.PathMatchSpecExW(pszFile, pszSpec, dwFlags)
-- pszFile : LPCWSTR
-- pszSpec : LPCWSTR
-- dwFlags : DWORD
-- 構造体/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 PathMatchSpecExW = lib.func('__stdcall', 'PathMatchSpecExW', 'int32_t', ['str16', 'str16', 'uint32_t']);
// PathMatchSpecExW(pszFile, pszSpec, dwFlags)
// pszFile : LPCWSTR -> 'str16'
// pszSpec : LPCWSTR -> 'str16'
// dwFlags : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("SHLWAPI.dll", {
  PathMatchSpecExW: { parameters: ["buffer", "buffer", "u32"], result: "i32" },
});
// lib.symbols.PathMatchSpecExW(pszFile, pszSpec, dwFlags)
// pszFile : LPCWSTR -> "buffer"
// pszSpec : LPCWSTR -> "buffer"
// dwFlags : DWORD -> "u32"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t PathMatchSpecExW(
    const uint16_t* pszFile,
    const uint16_t* pszSpec,
    uint32_t dwFlags);
C, "SHLWAPI.dll");
// $ffi->PathMatchSpecExW(pszFile, pszSpec, dwFlags);
// pszFile : LPCWSTR
// pszSpec : LPCWSTR
// dwFlags : DWORD
// 構造体/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);
    int PathMatchSpecExW(
        WString pszFile,   // LPCWSTR
        WString pszSpec,   // LPCWSTR
        int dwFlags   // DWORD
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("shlwapi")]
lib LibSHLWAPI
  fun PathMatchSpecExW = PathMatchSpecExW(
    pszFile : UInt16*,   # LPCWSTR
    pszSpec : UInt16*,   # LPCWSTR
    dwFlags : UInt32   # DWORD
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef PathMatchSpecExWNative = Int32 Function(Pointer<Utf16>, Pointer<Utf16>, Uint32);
typedef PathMatchSpecExWDart = int Function(Pointer<Utf16>, Pointer<Utf16>, int);
final PathMatchSpecExW = DynamicLibrary.open('SHLWAPI.dll')
    .lookupFunction<PathMatchSpecExWNative, PathMatchSpecExWDart>('PathMatchSpecExW');
// pszFile : LPCWSTR -> Pointer<Utf16>
// pszSpec : LPCWSTR -> Pointer<Utf16>
// dwFlags : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function PathMatchSpecExW(
  pszFile: PWideChar;   // LPCWSTR
  pszSpec: PWideChar;   // LPCWSTR
  dwFlags: DWORD   // DWORD
): Integer; stdcall;
  external 'SHLWAPI.dll' name 'PathMatchSpecExW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "PathMatchSpecExW"
  c_PathMatchSpecExW :: CWString -> CWString -> Word32 -> IO Int32
-- pszFile : LPCWSTR -> CWString
-- pszSpec : LPCWSTR -> CWString
-- dwFlags : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let pathmatchspecexw =
  foreign "PathMatchSpecExW"
    ((ptr uint16_t) @-> (ptr uint16_t) @-> uint32_t @-> returning int32_t)
(* pszFile : LPCWSTR -> (ptr uint16_t) *)
(* pszSpec : LPCWSTR -> (ptr uint16_t) *)
(* dwFlags : DWORD -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library shlwapi (t "SHLWAPI.dll"))
(cffi:use-foreign-library shlwapi)

(cffi:defcfun ("PathMatchSpecExW" path-match-spec-ex-w :convention :stdcall) :int32
  (psz-file (:string :encoding :utf-16le))   ; LPCWSTR
  (psz-spec (:string :encoding :utf-16le))   ; LPCWSTR
  (dw-flags :uint32))   ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $PathMatchSpecExW = Win32::API::More->new('SHLWAPI',
    'int PathMatchSpecExW(LPCWSTR pszFile, LPCWSTR pszSpec, DWORD dwFlags)');
# my $ret = $PathMatchSpecExW->Call($pszFile, $pszSpec, $dwFlags);
# pszFile : LPCWSTR -> LPCWSTR
# pszSpec : LPCWSTR -> LPCWSTR
# dwFlags : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

文字セット違い
類似 API