Win32 API 日本語リファレンス
ホームStorage.FileSystem › SearchPathW

SearchPathW

関数
指定パスから条件に合うファイルを検索しフルパスを取得する。
DLLKERNEL32.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

DWORD SearchPathW(
    LPCWSTR lpPath,   // optional
    LPCWSTR lpFileName,
    LPCWSTR lpExtension,   // optional
    DWORD nBufferLength,
    LPWSTR lpBuffer,   // optional
    LPWSTR* lpFilePart   // optional
);

パラメーター

名前方向
lpPathLPCWSTRinoptional
lpFileNameLPCWSTRin
lpExtensionLPCWSTRinoptional
nBufferLengthDWORDin
lpBufferLPWSTRoutoptional
lpFilePartLPWSTR*outoptional

戻り値の型: DWORD

各言語での呼び出し定義

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

DWORD SearchPathW(
    LPCWSTR lpPath,   // optional
    LPCWSTR lpFileName,
    LPCWSTR lpExtension,   // optional
    DWORD nBufferLength,
    LPWSTR lpBuffer,   // optional
    LPWSTR* lpFilePart   // optional
);
[DllImport("KERNEL32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern uint SearchPathW(
    [MarshalAs(UnmanagedType.LPWStr)] string lpPath,   // LPCWSTR optional
    [MarshalAs(UnmanagedType.LPWStr)] string lpFileName,   // LPCWSTR
    [MarshalAs(UnmanagedType.LPWStr)] string lpExtension,   // LPCWSTR optional
    uint nBufferLength,   // DWORD
    [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder lpBuffer,   // LPWSTR optional, out
    IntPtr lpFilePart   // LPWSTR* optional, out
);
<DllImport("KERNEL32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function SearchPathW(
    <MarshalAs(UnmanagedType.LPWStr)> lpPath As String,   ' LPCWSTR optional
    <MarshalAs(UnmanagedType.LPWStr)> lpFileName As String,   ' LPCWSTR
    <MarshalAs(UnmanagedType.LPWStr)> lpExtension As String,   ' LPCWSTR optional
    nBufferLength As UInteger,   ' DWORD
    <MarshalAs(UnmanagedType.LPWStr)> lpBuffer As System.Text.StringBuilder,   ' LPWSTR optional, out
    lpFilePart As IntPtr   ' LPWSTR* optional, out
) As UInteger
End Function
' lpPath : LPCWSTR optional
' lpFileName : LPCWSTR
' lpExtension : LPCWSTR optional
' nBufferLength : DWORD
' lpBuffer : LPWSTR optional, out
' lpFilePart : LPWSTR* optional, out
Declare PtrSafe Function SearchPathW Lib "kernel32" ( _
    ByVal lpPath As LongPtr, _
    ByVal lpFileName As LongPtr, _
    ByVal lpExtension As LongPtr, _
    ByVal nBufferLength As Long, _
    ByVal lpBuffer As LongPtr, _
    ByVal lpFilePart As LongPtr) 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

SearchPathW = ctypes.windll.kernel32.SearchPathW
SearchPathW.restype = wintypes.DWORD
SearchPathW.argtypes = [
    wintypes.LPCWSTR,  # lpPath : LPCWSTR optional
    wintypes.LPCWSTR,  # lpFileName : LPCWSTR
    wintypes.LPCWSTR,  # lpExtension : LPCWSTR optional
    wintypes.DWORD,  # nBufferLength : DWORD
    wintypes.LPWSTR,  # lpBuffer : LPWSTR optional, out
    ctypes.c_void_p,  # lpFilePart : LPWSTR* optional, out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('KERNEL32.dll')
SearchPathW = Fiddle::Function.new(
  lib['SearchPathW'],
  [
    Fiddle::TYPE_VOIDP,  # lpPath : LPCWSTR optional
    Fiddle::TYPE_VOIDP,  # lpFileName : LPCWSTR
    Fiddle::TYPE_VOIDP,  # lpExtension : LPCWSTR optional
    -Fiddle::TYPE_INT,  # nBufferLength : DWORD
    Fiddle::TYPE_VOIDP,  # lpBuffer : LPWSTR optional, out
    Fiddle::TYPE_VOIDP,  # lpFilePart : LPWSTR* optional, out
  ],
  -Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "kernel32")]
extern "system" {
    fn SearchPathW(
        lpPath: *const u16,  // LPCWSTR optional
        lpFileName: *const u16,  // LPCWSTR
        lpExtension: *const u16,  // LPCWSTR optional
        nBufferLength: u32,  // DWORD
        lpBuffer: *mut u16,  // LPWSTR optional, out
        lpFilePart: *mut *mut u16  // LPWSTR* optional, out
    ) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("KERNEL32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern uint SearchPathW([MarshalAs(UnmanagedType.LPWStr)] string lpPath, [MarshalAs(UnmanagedType.LPWStr)] string lpFileName, [MarshalAs(UnmanagedType.LPWStr)] string lpExtension, uint nBufferLength, [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder lpBuffer, IntPtr lpFilePart);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_SearchPathW' -Namespace Win32 -PassThru
# $api::SearchPathW(lpPath, lpFileName, lpExtension, nBufferLength, lpBuffer, lpFilePart)
#uselib "KERNEL32.dll"
#func global SearchPathW "SearchPathW" wptr, wptr, wptr, wptr, wptr, wptr
; SearchPathW lpPath, lpFileName, lpExtension, nBufferLength, varptr(lpBuffer), varptr(lpFilePart)   ; 戻り値は stat
; lpPath : LPCWSTR optional -> "wptr"
; lpFileName : LPCWSTR -> "wptr"
; lpExtension : LPCWSTR optional -> "wptr"
; nBufferLength : DWORD -> "wptr"
; lpBuffer : LPWSTR optional, out -> "wptr"
; lpFilePart : LPWSTR* optional, out -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "KERNEL32.dll"
#cfunc global SearchPathW "SearchPathW" wstr, wstr, wstr, int, var, var
; res = SearchPathW(lpPath, lpFileName, lpExtension, nBufferLength, lpBuffer, lpFilePart)
; lpPath : LPCWSTR optional -> "wstr"
; lpFileName : LPCWSTR -> "wstr"
; lpExtension : LPCWSTR optional -> "wstr"
; nBufferLength : DWORD -> "int"
; lpBuffer : LPWSTR optional, out -> "var"
; lpFilePart : LPWSTR* optional, out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; DWORD SearchPathW(LPCWSTR lpPath, LPCWSTR lpFileName, LPCWSTR lpExtension, DWORD nBufferLength, LPWSTR lpBuffer, LPWSTR* lpFilePart)
#uselib "KERNEL32.dll"
#cfunc global SearchPathW "SearchPathW" wstr, wstr, wstr, int, var, var
; res = SearchPathW(lpPath, lpFileName, lpExtension, nBufferLength, lpBuffer, lpFilePart)
; lpPath : LPCWSTR optional -> "wstr"
; lpFileName : LPCWSTR -> "wstr"
; lpExtension : LPCWSTR optional -> "wstr"
; nBufferLength : DWORD -> "int"
; lpBuffer : LPWSTR optional, out -> "var"
; lpFilePart : LPWSTR* optional, out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procSearchPathW = kernel32.NewProc("SearchPathW")
)

// lpPath (LPCWSTR optional), lpFileName (LPCWSTR), lpExtension (LPCWSTR optional), nBufferLength (DWORD), lpBuffer (LPWSTR optional, out), lpFilePart (LPWSTR* optional, out)
r1, _, err := procSearchPathW.Call(
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpPath))),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpFileName))),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpExtension))),
	uintptr(nBufferLength),
	uintptr(lpBuffer),
	uintptr(lpFilePart),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // DWORD
function SearchPathW(
  lpPath: PWideChar;   // LPCWSTR optional
  lpFileName: PWideChar;   // LPCWSTR
  lpExtension: PWideChar;   // LPCWSTR optional
  nBufferLength: DWORD;   // DWORD
  lpBuffer: PWideChar;   // LPWSTR optional, out
  lpFilePart: PPWideChar   // LPWSTR* optional, out
): DWORD; stdcall;
  external 'KERNEL32.dll' name 'SearchPathW';
result := DllCall("KERNEL32\SearchPathW"
    , "WStr", lpPath   ; LPCWSTR optional
    , "WStr", lpFileName   ; LPCWSTR
    , "WStr", lpExtension   ; LPCWSTR optional
    , "UInt", nBufferLength   ; DWORD
    , "Ptr", lpBuffer   ; LPWSTR optional, out
    , "Ptr", lpFilePart   ; LPWSTR* optional, out
    , "UInt")   ; return: DWORD
●SearchPathW(lpPath, lpFileName, lpExtension, nBufferLength, lpBuffer, lpFilePart) = DLL("KERNEL32.dll", "dword SearchPathW(char*, char*, char*, dword, char*, void*)")
# 呼び出し: SearchPathW(lpPath, lpFileName, lpExtension, nBufferLength, lpBuffer, lpFilePart)
# lpPath : LPCWSTR optional -> "char*"
# lpFileName : LPCWSTR -> "char*"
# lpExtension : LPCWSTR optional -> "char*"
# nBufferLength : DWORD -> "dword"
# lpBuffer : LPWSTR optional, out -> "char*"
# lpFilePart : LPWSTR* optional, out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。