Win32 API 日本語リファレンス
ホームSystem.Diagnostics.Debug › SearchTreeForFileW

SearchTreeForFileW

関数
ディレクトリツリーを再帰的に検索して指定ファイルを探す。
DLLdbghelp.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり

シグネチャ

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

BOOL SearchTreeForFileW(
    LPCWSTR RootPath,
    LPCWSTR InputPathName,
    LPWSTR OutputPathBuffer
);

パラメーター

名前方向説明
RootPathLPCWSTRin関数がファイルの検索を開始するパス。
InputPathNameLPCWSTRin関数が検索するファイル。部分パスを使用できます。
OutputPathBufferLPWSTRout見つかったファイルへのフルパスを受け取るバッファーへのポインター。戻り値が FALSE の場合、この文字列は変更されません。

戻り値の型: BOOL

公式ドキュメント

SearchTreeForFileW (Unicode) 関数は、指定したファイルをディレクトリツリー内で検索します。

戻り値

関数が成功した場合、戻り値は TRUE です。

関数が失敗した場合、戻り値は FALSE です。拡張エラー情報を取得するには、 GetLastError を呼び出します。

解説(Remarks)

この関数は、RootPath パラメーターで指定したパスを起点として、InputPathName パラメーターで指定したファイルを検索します。RootPath に許容される最大パス深度は 32 ディレクトリです。関数がディレクトリツリー内でファイルを見つけると、そのファイルへのフルパスを OutputPathBuffer パラメーターで指定したバッファーに格納します。サブディレクトリの検索順序は、基盤となるファイルシステムによって決まります。

SymRegisterCallbackProc64 コールバック関数を登録すると、検索をキャンセルできます。検索する各ディレクトリについて、 SearchTreeForFile はこのコールバック関数を CBA_DEFERRED_SYMBOL_LOAD_CANCEL とともに呼び出します。コールバック関数が TRUE を返すと、 SearchTreeForFile は検索をキャンセルします。

この関数は、検索する各ディレクトリにつき 1 回の CBA_DEFERRED_SYMBOL_LOAD_CANCEL イベントを発生させます。これにより、呼び出し元は検索をキャンセルできます。

この関数を含むすべての DbgHelp 関数はシングルスレッドです。そのため、複数のスレッドからこの関数を呼び出すと、予期しない動作やメモリ破損を引き起こす可能性があります。これを回避するには、複数のスレッドからこの関数への同時呼び出しをすべて同期する必要があります。

この関数の Unicode バージョンを呼び出すには、DBGHELP_TRANSLATE_TCHAR を定義します。

メモ

dbghelp.h ヘッダーは、UNICODE プリプロセッサ定数の定義に基づいて、この関数の ANSI バージョンまたは Unicode バージョンを自動的に選択するエイリアスとして SearchTreeForFile を定義します。エンコーディング中立のエイリアスの使用を、エンコーディング中立でないコードと混在させると、コンパイルエラーや実行時エラーを引き起こす不整合が生じる可能性があります。詳細については、Conventions for Function Prototypes を参照してください。

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

各言語での呼び出し定義

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

BOOL SearchTreeForFileW(
    LPCWSTR RootPath,
    LPCWSTR InputPathName,
    LPWSTR OutputPathBuffer
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("dbghelp.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern bool SearchTreeForFileW(
    [MarshalAs(UnmanagedType.LPWStr)] string RootPath,   // LPCWSTR
    [MarshalAs(UnmanagedType.LPWStr)] string InputPathName,   // LPCWSTR
    [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder OutputPathBuffer   // LPWSTR out
);
<DllImport("dbghelp.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function SearchTreeForFileW(
    <MarshalAs(UnmanagedType.LPWStr)> RootPath As String,   ' LPCWSTR
    <MarshalAs(UnmanagedType.LPWStr)> InputPathName As String,   ' LPCWSTR
    <MarshalAs(UnmanagedType.LPWStr)> OutputPathBuffer As System.Text.StringBuilder   ' LPWSTR out
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' RootPath : LPCWSTR
' InputPathName : LPCWSTR
' OutputPathBuffer : LPWSTR out
Declare PtrSafe Function SearchTreeForFileW Lib "dbghelp" ( _
    ByVal RootPath As LongPtr, _
    ByVal InputPathName As LongPtr, _
    ByVal OutputPathBuffer 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

SearchTreeForFileW = ctypes.windll.dbghelp.SearchTreeForFileW
SearchTreeForFileW.restype = wintypes.BOOL
SearchTreeForFileW.argtypes = [
    wintypes.LPCWSTR,  # RootPath : LPCWSTR
    wintypes.LPCWSTR,  # InputPathName : LPCWSTR
    wintypes.LPWSTR,  # OutputPathBuffer : LPWSTR out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

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

var (
	dbghelp = windows.NewLazySystemDLL("dbghelp.dll")
	procSearchTreeForFileW = dbghelp.NewProc("SearchTreeForFileW")
)

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

extern "dbghelp" fn SearchTreeForFileW(
    RootPath: [*c]const u16, // LPCWSTR
    InputPathName: [*c]const u16, // LPCWSTR
    OutputPathBuffer: [*c]u16 // LPWSTR out
) callconv(std.os.windows.WINAPI) i32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。
proc SearchTreeForFileW(
    RootPath: WideCString,  # LPCWSTR
    InputPathName: WideCString,  # LPCWSTR
    OutputPathBuffer: ptr uint16  # LPWSTR out
): int32 {.importc: "SearchTreeForFileW", stdcall, dynlib: "dbghelp.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "dbghelp");
extern(Windows)
int SearchTreeForFileW(
    const(wchar)* RootPath,   // LPCWSTR
    const(wchar)* InputPathName,   // LPCWSTR
    wchar* OutputPathBuffer   // LPWSTR out
);
ccall((:SearchTreeForFileW, "dbghelp.dll"), stdcall, Int32,
      (Cwstring, Cwstring, Ptr{UInt16}),
      RootPath, InputPathName, OutputPathBuffer)
# RootPath : LPCWSTR -> Cwstring
# InputPathName : LPCWSTR -> Cwstring
# OutputPathBuffer : LPWSTR out -> Ptr{UInt16}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。
local ffi = require("ffi")
ffi.cdef[[
int32_t SearchTreeForFileW(
    const uint16_t* RootPath,
    const uint16_t* InputPathName,
    uint16_t* OutputPathBuffer);
]]
local dbghelp = ffi.load("dbghelp")
-- dbghelp.SearchTreeForFileW(RootPath, InputPathName, OutputPathBuffer)
-- RootPath : LPCWSTR
-- InputPathName : LPCWSTR
-- OutputPathBuffer : LPWSTR out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。
const koffi = require('koffi');
const lib = koffi.load('dbghelp.dll');
const SearchTreeForFileW = lib.func('__stdcall', 'SearchTreeForFileW', 'int32_t', ['str16', 'str16', 'uint16_t *']);
// SearchTreeForFileW(RootPath, InputPathName, OutputPathBuffer)
// RootPath : LPCWSTR -> 'str16'
// InputPathName : LPCWSTR -> 'str16'
// OutputPathBuffer : LPWSTR out -> 'uint16_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("dbghelp.dll", {
  SearchTreeForFileW: { parameters: ["buffer", "buffer", "buffer"], result: "i32" },
});
// lib.symbols.SearchTreeForFileW(RootPath, InputPathName, OutputPathBuffer)
// RootPath : LPCWSTR -> "buffer"
// InputPathName : LPCWSTR -> "buffer"
// OutputPathBuffer : LPWSTR out -> "buffer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t SearchTreeForFileW(
    const uint16_t* RootPath,
    const uint16_t* InputPathName,
    uint16_t* OutputPathBuffer);
C, "dbghelp.dll");
// $ffi->SearchTreeForFileW(RootPath, InputPathName, OutputPathBuffer);
// RootPath : LPCWSTR
// InputPathName : LPCWSTR
// OutputPathBuffer : LPWSTR out
// 構造体/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 Dbghelp extends StdCallLibrary {
    Dbghelp INSTANCE = Native.load("dbghelp", Dbghelp.class, W32APIOptions.UNICODE_OPTIONS);
    boolean SearchTreeForFileW(
        WString RootPath,   // LPCWSTR
        WString InputPathName,   // LPCWSTR
        char[] OutputPathBuffer   // LPWSTR out
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("dbghelp")]
lib Libdbghelp
  fun SearchTreeForFileW = SearchTreeForFileW(
    RootPath : UInt16*,   # LPCWSTR
    InputPathName : UInt16*,   # LPCWSTR
    OutputPathBuffer : UInt16*   # LPWSTR out
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef SearchTreeForFileWNative = Int32 Function(Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>);
typedef SearchTreeForFileWDart = int Function(Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>);
final SearchTreeForFileW = DynamicLibrary.open('dbghelp.dll')
    .lookupFunction<SearchTreeForFileWNative, SearchTreeForFileWDart>('SearchTreeForFileW');
// RootPath : LPCWSTR -> Pointer<Utf16>
// InputPathName : LPCWSTR -> Pointer<Utf16>
// OutputPathBuffer : LPWSTR out -> Pointer<Utf16>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function SearchTreeForFileW(
  RootPath: PWideChar;   // LPCWSTR
  InputPathName: PWideChar;   // LPCWSTR
  OutputPathBuffer: PWideChar   // LPWSTR out
): BOOL; stdcall;
  external 'dbghelp.dll' name 'SearchTreeForFileW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "SearchTreeForFileW"
  c_SearchTreeForFileW :: CWString -> CWString -> CWString -> IO CInt
-- RootPath : LPCWSTR -> CWString
-- InputPathName : LPCWSTR -> CWString
-- OutputPathBuffer : LPWSTR out -> CWString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let searchtreeforfilew =
  foreign "SearchTreeForFileW"
    ((ptr uint16_t) @-> (ptr uint16_t) @-> (ptr uint16_t) @-> returning int32_t)
(* RootPath : LPCWSTR -> (ptr uint16_t) *)
(* InputPathName : LPCWSTR -> (ptr uint16_t) *)
(* OutputPathBuffer : LPWSTR out -> (ptr uint16_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library dbghelp (t "dbghelp.dll"))
(cffi:use-foreign-library dbghelp)

(cffi:defcfun ("SearchTreeForFileW" search-tree-for-file-w :convention :stdcall) :int32
  (root-path (:string :encoding :utf-16le))   ; LPCWSTR
  (input-path-name (:string :encoding :utf-16le))   ; LPCWSTR
  (output-path-buffer :pointer))   ; LPWSTR out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $SearchTreeForFileW = Win32::API::More->new('dbghelp',
    'BOOL SearchTreeForFileW(LPCWSTR RootPath, LPCWSTR InputPathName, LPWSTR OutputPathBuffer)');
# my $ret = $SearchTreeForFileW->Call($RootPath, $InputPathName, $OutputPathBuffer);
# RootPath : LPCWSTR -> LPCWSTR
# InputPathName : LPCWSTR -> LPCWSTR
# OutputPathBuffer : LPWSTR out -> LPWSTR
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。