FindExecutableW
関数シグネチャ
// SHELL32.dll (Unicode / -W)
#include <windows.h>
HINSTANCE FindExecutableW(
LPCWSTR lpFile,
LPCWSTR lpDirectory, // optional
LPWSTR lpResult
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| lpFile | LPCWSTR | in | ファイル名を指定する null 終端文字列のアドレスです。このファイルはドキュメントである必要があります。 |
| lpDirectory | LPCWSTR | inoptional | 既定のディレクトリを指定する null 終端文字列のアドレスです。この値は NULL にできます。 |
| lpResult | LPWSTR | out | 関連付けられた実行可能ファイルのファイル名を受け取るバッファーのアドレスです。このファイル名は null 終端文字列で、lpFile パラメーターで指定されたファイルに対して関連付けによる「":::no-loc text="open":::"」を実行したときに起動される実行可能ファイルを指定します。簡単に言えば、これはドキュメントファイルを直接ダブルクリックしたとき、またはファイルのショートカットメニューから Open を選択したときに起動されるアプリケーションです。このパラメーターには有効な非 null の値を格納する必要があり、長さは MAX_PATH であると想定されます。値の検証はプログラマーの責任です。 |
戻り値の型: HINSTANCE
公式ドキュメント
特定のドキュメントファイルに関連付けられた実行可能(.exe)ファイルの名前とハンドルを取得します。(Unicode)
戻り値
型: HINSTANCE
成功した場合は 32 より大きい値を返し、エラーの場合は 32 以下のエラーを表す値を返します。
次の表に、発生し得るエラー値を示します。
| 戻り値/値 | 説明 |
|---|---|
| 指定されたファイルが見つかりませんでした。 | |
| 指定されたパスが無効です。 | |
| 指定されたファイルにアクセスできません。 | |
| システムのメモリまたはリソースが不足しています。 | |
| 指定されたファイルタイプに対して、実行可能ファイルとの関連付けがありません。 |
解説(Remarks)
ドキュメントには FindExecutable を使用します。実行可能ファイルのパスを取得したい場合は、次の方法を使用してください。
AssocQueryString(ASSOCF_OPEN_BYEXENAME,
ASSOCSTR_EXECUTABLE,
pszExecutableName,
NULL,
pszPath,
pcchOut);
ここで、pszExecutableName は実行可能ファイルの名前を指定する null 終端文字列へのポインター、pszPath は実行可能ファイルへのパスを受け取る null 終端文字列バッファーへのポインター、pcchOut は pszPath バッファー内の文字数を指定する DWORD へのポインターです。関数が戻ると、pcchOut には実際にバッファーに格納された文字数が設定されます。詳細については、AssocQueryString を参照してください。
FindExecutable が戻るとき、DDE クライアントアプリケーションとの DDE 会話を開始する要求にサーバーが応答しなかった場合、lpResult パラメーターには起動された動的データ交換(DDE)サーバーのパスが格納されることがあります。
shellapi.h ヘッダーは、FindExecutable を、UNICODE プリプロセッサ定数の定義に基づいてこの関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして定義します。このエンコーディング中立のエイリアスの使用を、エンコーディング中立でないコードと混在させると、不一致が生じ、コンパイルエラーまたは実行時エラーを引き起こす可能性があります。詳細については、Conventions for Function Prototypes を参照してください。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// SHELL32.dll (Unicode / -W)
#include <windows.h>
HINSTANCE FindExecutableW(
LPCWSTR lpFile,
LPCWSTR lpDirectory, // optional
LPWSTR lpResult
);[DllImport("SHELL32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern IntPtr FindExecutableW(
[MarshalAs(UnmanagedType.LPWStr)] string lpFile, // LPCWSTR
[MarshalAs(UnmanagedType.LPWStr)] string lpDirectory, // LPCWSTR optional
[MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder lpResult // LPWSTR out
);<DllImport("SHELL32.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function FindExecutableW(
<MarshalAs(UnmanagedType.LPWStr)> lpFile As String, ' LPCWSTR
<MarshalAs(UnmanagedType.LPWStr)> lpDirectory As String, ' LPCWSTR optional
<MarshalAs(UnmanagedType.LPWStr)> lpResult As System.Text.StringBuilder ' LPWSTR out
) As IntPtr
End Function' lpFile : LPCWSTR
' lpDirectory : LPCWSTR optional
' lpResult : LPWSTR out
Declare PtrSafe Function FindExecutableW Lib "shell32" ( _
ByVal lpFile As LongPtr, _
ByVal lpDirectory As LongPtr, _
ByVal lpResult As LongPtr) As LongPtr
' 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
FindExecutableW = ctypes.windll.shell32.FindExecutableW
FindExecutableW.restype = ctypes.c_void_p
FindExecutableW.argtypes = [
wintypes.LPCWSTR, # lpFile : LPCWSTR
wintypes.LPCWSTR, # lpDirectory : LPCWSTR optional
wintypes.LPWSTR, # lpResult : LPWSTR out
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('SHELL32.dll')
FindExecutableW = Fiddle::Function.new(
lib['FindExecutableW'],
[
Fiddle::TYPE_VOIDP, # lpFile : LPCWSTR
Fiddle::TYPE_VOIDP, # lpDirectory : LPCWSTR optional
Fiddle::TYPE_VOIDP, # lpResult : LPWSTR out
],
Fiddle::TYPE_VOIDP)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"#[link(name = "shell32")]
extern "system" {
fn FindExecutableW(
lpFile: *const u16, // LPCWSTR
lpDirectory: *const u16, // LPCWSTR optional
lpResult: *mut u16 // LPWSTR out
) -> *mut core::ffi::c_void;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("SHELL32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr FindExecutableW([MarshalAs(UnmanagedType.LPWStr)] string lpFile, [MarshalAs(UnmanagedType.LPWStr)] string lpDirectory, [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder lpResult);
"@
$api = Add-Type -MemberDefinition $sig -Name 'SHELL32_FindExecutableW' -Namespace Win32 -PassThru
# $api::FindExecutableW(lpFile, lpDirectory, lpResult)#uselib "SHELL32.dll"
#func global FindExecutableW "FindExecutableW" wptr, wptr, wptr
; FindExecutableW lpFile, lpDirectory, varptr(lpResult) ; 戻り値は stat
; lpFile : LPCWSTR -> "wptr"
; lpDirectory : LPCWSTR optional -> "wptr"
; lpResult : LPWSTR out -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "SHELL32.dll" #cfunc global FindExecutableW "FindExecutableW" wstr, wstr, var ; res = FindExecutableW(lpFile, lpDirectory, lpResult) ; lpFile : LPCWSTR -> "wstr" ; lpDirectory : LPCWSTR optional -> "wstr" ; lpResult : LPWSTR out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "SHELL32.dll" #cfunc global FindExecutableW "FindExecutableW" wstr, wstr, sptr ; res = FindExecutableW(lpFile, lpDirectory, varptr(lpResult)) ; lpFile : LPCWSTR -> "wstr" ; lpDirectory : LPCWSTR optional -> "wstr" ; lpResult : LPWSTR out -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
; HINSTANCE FindExecutableW(LPCWSTR lpFile, LPCWSTR lpDirectory, LPWSTR lpResult) #uselib "SHELL32.dll" #cfunc global FindExecutableW "FindExecutableW" wstr, wstr, var ; res = FindExecutableW(lpFile, lpDirectory, lpResult) ; lpFile : LPCWSTR -> "wstr" ; lpDirectory : LPCWSTR optional -> "wstr" ; lpResult : LPWSTR out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; HINSTANCE FindExecutableW(LPCWSTR lpFile, LPCWSTR lpDirectory, LPWSTR lpResult) #uselib "SHELL32.dll" #cfunc global FindExecutableW "FindExecutableW" wstr, wstr, intptr ; res = FindExecutableW(lpFile, lpDirectory, varptr(lpResult)) ; lpFile : LPCWSTR -> "wstr" ; lpDirectory : LPCWSTR optional -> "wstr" ; lpResult : LPWSTR out -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
shell32 = windows.NewLazySystemDLL("SHELL32.dll")
procFindExecutableW = shell32.NewProc("FindExecutableW")
)
// lpFile (LPCWSTR), lpDirectory (LPCWSTR optional), lpResult (LPWSTR out)
r1, _, err := procFindExecutableW.Call(
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpFile))),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpDirectory))),
uintptr(lpResult),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // HINSTANCEfunction FindExecutableW(
lpFile: PWideChar; // LPCWSTR
lpDirectory: PWideChar; // LPCWSTR optional
lpResult: PWideChar // LPWSTR out
): THandle; stdcall;
external 'SHELL32.dll' name 'FindExecutableW';result := DllCall("SHELL32\FindExecutableW"
, "WStr", lpFile ; LPCWSTR
, "WStr", lpDirectory ; LPCWSTR optional
, "Ptr", lpResult ; LPWSTR out
, "Ptr") ; return: HINSTANCE●FindExecutableW(lpFile, lpDirectory, lpResult) = DLL("SHELL32.dll", "void* FindExecutableW(char*, char*, char*)")
# 呼び出し: FindExecutableW(lpFile, lpDirectory, lpResult)
# lpFile : LPCWSTR -> "char*"
# lpDirectory : LPCWSTR optional -> "char*"
# lpResult : LPWSTR out -> "char*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。const std = @import("std");
extern "shell32" fn FindExecutableW(
lpFile: [*c]const u16, // LPCWSTR
lpDirectory: [*c]const u16, // LPCWSTR optional
lpResult: [*c]u16 // LPWSTR out
) callconv(std.os.windows.WINAPI) ?*anyopaque;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。proc FindExecutableW(
lpFile: WideCString, # LPCWSTR
lpDirectory: WideCString, # LPCWSTR optional
lpResult: ptr uint16 # LPWSTR out
): pointer {.importc: "FindExecutableW", stdcall, dynlib: "SHELL32.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。pragma(lib, "shell32");
extern(Windows)
void* FindExecutableW(
const(wchar)* lpFile, // LPCWSTR
const(wchar)* lpDirectory, // LPCWSTR optional
wchar* lpResult // LPWSTR out
);ccall((:FindExecutableW, "SHELL32.dll"), stdcall, Ptr{Cvoid},
(Cwstring, Cwstring, Ptr{UInt16}),
lpFile, lpDirectory, lpResult)
# lpFile : LPCWSTR -> Cwstring
# lpDirectory : LPCWSTR optional -> Cwstring
# lpResult : LPWSTR out -> Ptr{UInt16}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。local ffi = require("ffi")
ffi.cdef[[
void* FindExecutableW(
const uint16_t* lpFile,
const uint16_t* lpDirectory,
uint16_t* lpResult);
]]
local shell32 = ffi.load("shell32")
-- shell32.FindExecutableW(lpFile, lpDirectory, lpResult)
-- lpFile : LPCWSTR
-- lpDirectory : LPCWSTR optional
-- lpResult : 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('SHELL32.dll');
const FindExecutableW = lib.func('__stdcall', 'FindExecutableW', 'void *', ['str16', 'str16', 'uint16_t *']);
// FindExecutableW(lpFile, lpDirectory, lpResult)
// lpFile : LPCWSTR -> 'str16'
// lpDirectory : LPCWSTR optional -> 'str16'
// lpResult : LPWSTR out -> 'uint16_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("SHELL32.dll", {
FindExecutableW: { parameters: ["buffer", "buffer", "buffer"], result: "pointer" },
});
// lib.symbols.FindExecutableW(lpFile, lpDirectory, lpResult)
// lpFile : LPCWSTR -> "buffer"
// lpDirectory : LPCWSTR optional -> "buffer"
// lpResult : LPWSTR out -> "buffer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
void* FindExecutableW(
const uint16_t* lpFile,
const uint16_t* lpDirectory,
uint16_t* lpResult);
C, "SHELL32.dll");
// $ffi->FindExecutableW(lpFile, lpDirectory, lpResult);
// lpFile : LPCWSTR
// lpDirectory : LPCWSTR optional
// lpResult : 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 Shell32 extends StdCallLibrary {
Shell32 INSTANCE = Native.load("shell32", Shell32.class, W32APIOptions.UNICODE_OPTIONS);
Pointer FindExecutableW(
WString lpFile, // LPCWSTR
WString lpDirectory, // LPCWSTR optional
char[] lpResult // LPWSTR out
);
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。@[Link("shell32")]
lib LibSHELL32
fun FindExecutableW = FindExecutableW(
lpFile : UInt16*, # LPCWSTR
lpDirectory : UInt16*, # LPCWSTR optional
lpResult : UInt16* # LPWSTR out
) : Void*
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef FindExecutableWNative = Pointer<Void> Function(Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>);
typedef FindExecutableWDart = Pointer<Void> Function(Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>);
final FindExecutableW = DynamicLibrary.open('SHELL32.dll')
.lookupFunction<FindExecutableWNative, FindExecutableWDart>('FindExecutableW');
// lpFile : LPCWSTR -> Pointer<Utf16>
// lpDirectory : LPCWSTR optional -> Pointer<Utf16>
// lpResult : LPWSTR out -> Pointer<Utf16>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function FindExecutableW(
lpFile: PWideChar; // LPCWSTR
lpDirectory: PWideChar; // LPCWSTR optional
lpResult: PWideChar // LPWSTR out
): THandle; stdcall;
external 'SHELL32.dll' name 'FindExecutableW';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "FindExecutableW"
c_FindExecutableW :: CWString -> CWString -> CWString -> IO (Ptr ())
-- lpFile : LPCWSTR -> CWString
-- lpDirectory : LPCWSTR optional -> CWString
-- lpResult : LPWSTR out -> CWString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let findexecutablew =
foreign "FindExecutableW"
((ptr uint16_t) @-> (ptr uint16_t) @-> (ptr uint16_t) @-> returning (ptr void))
(* lpFile : LPCWSTR -> (ptr uint16_t) *)
(* lpDirectory : LPCWSTR optional -> (ptr uint16_t) *)
(* lpResult : LPWSTR out -> (ptr uint16_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library shell32 (t "SHELL32.dll"))
(cffi:use-foreign-library shell32)
(cffi:defcfun ("FindExecutableW" find-executable-w :convention :stdcall) :pointer
(lp-file (:string :encoding :utf-16le)) ; LPCWSTR
(lp-directory (:string :encoding :utf-16le)) ; LPCWSTR optional
(lp-result :pointer)) ; LPWSTR out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $FindExecutableW = Win32::API::More->new('SHELL32',
'HANDLE FindExecutableW(LPCWSTR lpFile, LPCWSTR lpDirectory, LPWSTR lpResult)');
# my $ret = $FindExecutableW->Call($lpFile, $lpDirectory, $lpResult);
# lpFile : LPCWSTR -> LPCWSTR
# lpDirectory : LPCWSTR optional -> LPCWSTR
# lpResult : LPWSTR out -> LPWSTR
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。関連項目
- f FindExecutableA (ANSI版) — ファイル(ANSI)に関連付けられた実行ファイルを検索する。
- f ShellExecuteW — ファイルやプログラム(Unicode)に対し操作を実行する。