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

SearchPathA

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

シグネチャ

// KERNEL32.dll  (ANSI / -A)
#include <windows.h>

DWORD SearchPathA(
    LPCSTR lpPath,   // optional
    LPCSTR lpFileName,
    LPCSTR lpExtension,   // optional
    DWORD nBufferLength,
    LPSTR lpBuffer,   // optional
    LPSTR* lpFilePart   // optional
);

パラメーター

名前方向
lpPathLPCSTRinoptional
lpFileNameLPCSTRin
lpExtensionLPCSTRinoptional
nBufferLengthDWORDin
lpBufferLPSTRoutoptional
lpFilePartLPSTR*outoptional

戻り値の型: DWORD

各言語での呼び出し定義

// KERNEL32.dll  (ANSI / -A)
#include <windows.h>

DWORD SearchPathA(
    LPCSTR lpPath,   // optional
    LPCSTR lpFileName,
    LPCSTR lpExtension,   // optional
    DWORD nBufferLength,
    LPSTR lpBuffer,   // optional
    LPSTR* lpFilePart   // optional
);
[DllImport("KERNEL32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
static extern uint SearchPathA(
    [MarshalAs(UnmanagedType.LPStr)] string lpPath,   // LPCSTR optional
    [MarshalAs(UnmanagedType.LPStr)] string lpFileName,   // LPCSTR
    [MarshalAs(UnmanagedType.LPStr)] string lpExtension,   // LPCSTR optional
    uint nBufferLength,   // DWORD
    [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder lpBuffer,   // LPSTR optional, out
    IntPtr lpFilePart   // LPSTR* optional, out
);
<DllImport("KERNEL32.dll", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function SearchPathA(
    <MarshalAs(UnmanagedType.LPStr)> lpPath As String,   ' LPCSTR optional
    <MarshalAs(UnmanagedType.LPStr)> lpFileName As String,   ' LPCSTR
    <MarshalAs(UnmanagedType.LPStr)> lpExtension As String,   ' LPCSTR optional
    nBufferLength As UInteger,   ' DWORD
    <MarshalAs(UnmanagedType.LPStr)> lpBuffer As System.Text.StringBuilder,   ' LPSTR optional, out
    lpFilePart As IntPtr   ' LPSTR* optional, out
) As UInteger
End Function
' lpPath : LPCSTR optional
' lpFileName : LPCSTR
' lpExtension : LPCSTR optional
' nBufferLength : DWORD
' lpBuffer : LPSTR optional, out
' lpFilePart : LPSTR* optional, out
Declare PtrSafe Function SearchPathA Lib "kernel32" ( _
    ByVal lpPath As String, _
    ByVal lpFileName As String, _
    ByVal lpExtension As String, _
    ByVal nBufferLength As Long, _
    ByVal lpBuffer As String, _
    ByVal lpFilePart As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

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

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

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procSearchPathA = kernel32.NewProc("SearchPathA")
)

// lpPath (LPCSTR optional), lpFileName (LPCSTR), lpExtension (LPCSTR optional), nBufferLength (DWORD), lpBuffer (LPSTR optional, out), lpFilePart (LPSTR* optional, out)
r1, _, err := procSearchPathA.Call(
	uintptr(unsafe.Pointer(windows.BytePtrFromString(lpPath))),
	uintptr(unsafe.Pointer(windows.BytePtrFromString(lpFileName))),
	uintptr(unsafe.Pointer(windows.BytePtrFromString(lpExtension))),
	uintptr(nBufferLength),
	uintptr(lpBuffer),
	uintptr(lpFilePart),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // DWORD
function SearchPathA(
  lpPath: PAnsiChar;   // LPCSTR optional
  lpFileName: PAnsiChar;   // LPCSTR
  lpExtension: PAnsiChar;   // LPCSTR optional
  nBufferLength: DWORD;   // DWORD
  lpBuffer: PAnsiChar;   // LPSTR optional, out
  lpFilePart: PPAnsiChar   // LPSTR* optional, out
): DWORD; stdcall;
  external 'KERNEL32.dll' name 'SearchPathA';
result := DllCall("KERNEL32\SearchPathA"
    , "AStr", lpPath   ; LPCSTR optional
    , "AStr", lpFileName   ; LPCSTR
    , "AStr", lpExtension   ; LPCSTR optional
    , "UInt", nBufferLength   ; DWORD
    , "Ptr", lpBuffer   ; LPSTR optional, out
    , "Ptr", lpFilePart   ; LPSTR* optional, out
    , "UInt")   ; return: DWORD
●SearchPathA(lpPath, lpFileName, lpExtension, nBufferLength, lpBuffer, lpFilePart) = DLL("KERNEL32.dll", "dword SearchPathA(char*, char*, char*, dword, char*, void*)")
# 呼び出し: SearchPathA(lpPath, lpFileName, lpExtension, nBufferLength, lpBuffer, lpFilePart)
# lpPath : LPCSTR optional -> "char*"
# lpFileName : LPCSTR -> "char*"
# lpExtension : LPCSTR optional -> "char*"
# nBufferLength : DWORD -> "dword"
# lpBuffer : LPSTR optional, out -> "char*"
# lpFilePart : LPSTR* optional, out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。