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

ファイルを検索するパスです。

このパラメーターが NULL の場合、関数はレジストリに依存するシステム検索パスを使用して、一致するファイルを検索します。詳細については、「解説」セクションを参照してください。

lpFileNameLPCWSTRin検索対象のファイルの名前です。
lpExtensionLPCWSTRinoptional

ファイルを検索する際にファイル名に追加される拡張子です。ファイル名拡張子の最初の文字はピリオド (.) でなければなりません。拡張子は、指定されたファイル名が拡張子で終わっていない場合にのみ追加されます。

ファイル名拡張子が不要な場合、またはファイル名に拡張子が含まれている場合、このパラメーターは NULL にできます。

nBufferLengthDWORDin有効なパスとファイル名 (終端の null 文字を含む) を受け取るバッファーのサイズです。単位は TCHAR です。
lpBufferLPWSTRoutoptional見つかったファイルのパスとファイル名を受け取るバッファーへのポインターです。文字列は null で終端されます。
lpFilePartLPWSTR*outoptional有効なパスとファイル名の最後の要素のアドレス (lpBuffer 内) を受け取る変数へのポインターです。これはパス内の最後のバックスラッシュ (\) の直後の文字のアドレスです。

戻り値の型: DWORD

公式ドキュメント

指定されたパス内で指定されたファイルを検索します。(Unicode)

戻り値

関数が成功した場合、戻り値はバッファーにコピーされた文字列の長さ (終端の null 文字を含まない) で、単位は TCHAR です。戻り値が nBufferLength より大きい場合、戻り値はパスを保持するために必要なバッファーのサイズ (終端の null 文字を含む) です。

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

解説(Remarks)

lpPath パラメーターが NULL の場合、SearchPath は次のレジストリ値の現在の値に基づいて、一致するファイルを検索します。

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\SafeProcessSearchMode

この REG_DWORD レジストリ値が 1 に設定されている場合、SearchPath はまずシステムパスで指定されたフォルダーを検索し、その後に現在の作業フォルダーを検索します。このレジストリ値が 0 に設定されている場合、コンピューターはまず現在の作業フォルダーを検索し、その後にシステムパスで指定されたフォルダーを検索します。このレジストリキーのシステム既定値は 0 です。

SearchPath 関数が使用する検索モードは、SetSearchPathMode 関数を呼び出すことで、プロセスごとに設定することもできます。

出力を LoadLibrary 関数の呼び出しで使用する予定がある場合、.dll ファイルを見つける方法として SearchPath 関数は推奨されません。SearchPath 関数の検索順序は LoadLibrary 関数が使用する検索順序と異なるため、誤った .dll ファイルが見つかる可能性があります。.dll ファイルを見つけて読み込む必要がある場合は、LoadLibrary 関数を使用してください。

ヒント Windows 10 バージョン 1607 以降では、この関数の Unicode バージョン (SearchPathW) について、MAX_PATH の制限を解除するようにオプトインできます。詳細については、ファイル、パス、名前空間の名前付けの「最大パス長の制限」セクションを参照してください。
Windows 8 および Windows Server 2012 では、この関数は次のテクノロジでサポートされています。
テクノロジ サポート
Server Message Block (SMB) 3.0 プロトコル はい
SMB 3.0 Transparent Failover (TFO) はい
SMB 3.0 with Scale-out File Shares (SO) はい
Cluster Shared Volume File System (CsvFS) はい
Resilient File System (ReFS) はい
メモ

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

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

各言語での呼び出し定義

// 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 版の利用を推奨。
const std = @import("std");

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

typedef SearchPathWNative = Uint32 Function(Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>, Uint32, Pointer<Utf16>, Pointer<Pointer<Utf16>>);
typedef SearchPathWDart = int Function(Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>, int, Pointer<Utf16>, Pointer<Pointer<Utf16>>);
final SearchPathW = DynamicLibrary.open('KERNEL32.dll')
    .lookupFunction<SearchPathWNative, SearchPathWDart>('SearchPathW');
// lpPath : LPCWSTR optional -> Pointer<Utf16>
// lpFileName : LPCWSTR -> Pointer<Utf16>
// lpExtension : LPCWSTR optional -> Pointer<Utf16>
// nBufferLength : DWORD -> Uint32
// lpBuffer : LPWSTR optional, out -> Pointer<Utf16>
// lpFilePart : LPWSTR* optional, out -> Pointer<Pointer<Utf16>>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
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';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "SearchPathW"
  c_SearchPathW :: CWString -> CWString -> CWString -> Word32 -> CWString -> Ptr CWString -> IO Word32
-- lpPath : LPCWSTR optional -> CWString
-- lpFileName : LPCWSTR -> CWString
-- lpExtension : LPCWSTR optional -> CWString
-- nBufferLength : DWORD -> Word32
-- lpBuffer : LPWSTR optional, out -> CWString
-- lpFilePart : LPWSTR* optional, out -> Ptr CWString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let searchpathw =
  foreign "SearchPathW"
    ((ptr uint16_t) @-> (ptr uint16_t) @-> (ptr uint16_t) @-> uint32_t @-> (ptr uint16_t) @-> (ptr (ptr uint16_t)) @-> returning uint32_t)
(* lpPath : LPCWSTR optional -> (ptr uint16_t) *)
(* lpFileName : LPCWSTR -> (ptr uint16_t) *)
(* lpExtension : LPCWSTR optional -> (ptr uint16_t) *)
(* nBufferLength : DWORD -> uint32_t *)
(* lpBuffer : LPWSTR optional, out -> (ptr uint16_t) *)
(* lpFilePart : LPWSTR* optional, out -> (ptr (ptr uint16_t)) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)

(cffi:defcfun ("SearchPathW" search-path-w :convention :stdcall) :uint32
  (lp-path (:string :encoding :utf-16le))   ; LPCWSTR optional
  (lp-file-name (:string :encoding :utf-16le))   ; LPCWSTR
  (lp-extension (:string :encoding :utf-16le))   ; LPCWSTR optional
  (n-buffer-length :uint32)   ; DWORD
  (lp-buffer :pointer)   ; LPWSTR optional, out
  (lp-file-part :pointer))   ; LPWSTR* optional, out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $SearchPathW = Win32::API::More->new('KERNEL32',
    'DWORD SearchPathW(LPCWSTR lpPath, LPCWSTR lpFileName, LPCWSTR lpExtension, DWORD nBufferLength, LPWSTR lpBuffer, LPVOID lpFilePart)');
# my $ret = $SearchPathW->Call($lpPath, $lpFileName, $lpExtension, $nBufferLength, $lpBuffer, $lpFilePart);
# lpPath : LPCWSTR optional -> LPCWSTR
# lpFileName : LPCWSTR -> LPCWSTR
# lpExtension : LPCWSTR optional -> LPCWSTR
# nBufferLength : DWORD -> DWORD
# lpBuffer : LPWSTR optional, out -> LPWSTR
# lpFilePart : LPWSTR* optional, out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

文字セット違い
公式の関連項目