FindExecutableA
関数シグネチャ
// SHELL32.dll (ANSI / -A)
#include <windows.h>
HINSTANCE FindExecutableA(
LPCSTR lpFile,
LPCSTR lpDirectory, // optional
LPSTR lpResult
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| lpFile | LPCSTR | in | ファイル名を指定する null 終端文字列のアドレス。このファイルはドキュメントである必要があります。 |
| lpDirectory | LPCSTR | inoptional | 既定のディレクトリを指定する null 終端文字列のアドレス。この値は NULL でもかまいません。 |
| lpResult | LPSTR | out | 関連付けられた実行可能ファイルのファイル名を受け取るバッファーのアドレス。このファイル名は null 終端文字列であり、lpFile パラメーターで指定されたファイルに対して関連付けによる ":::no-loc text="open":::" が実行されたときに起動される実行可能ファイルを指定します。簡単に言えば、これはドキュメントファイルを直接ダブルクリックしたとき、またはファイルのショートカットメニューから Open を選択したときに起動されるアプリケーションです。このパラメーターには有効な非 null 値を格納する必要があり、長さは MAX_PATH であると想定されます。値の検証はプログラマーの責任となります。 |
戻り値の型: HINSTANCE
公式ドキュメント
特定のドキュメントファイルに関連付けられた実行可能 (.exe) ファイルの名前とハンドルを取得します。(ANSI)
戻り値
型: 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: Dynamic Data Exchange) サーバーへのパスが格納される場合があります。
shellapi.h ヘッダーは FindExecutable をエイリアスとして定義しており、UNICODE プリプロセッサ定数の定義に基づいて、この関数の ANSI 版または Unicode 版が自動的に選択されます。エンコーディング中立のエイリアスを、エンコーディング中立でないコードと混在して使用すると、不一致が生じてコンパイルエラーまたは実行時エラーを引き起こす可能性があります。詳細については、Conventions for Function Prototypes を参照してください。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// SHELL32.dll (ANSI / -A)
#include <windows.h>
HINSTANCE FindExecutableA(
LPCSTR lpFile,
LPCSTR lpDirectory, // optional
LPSTR lpResult
);[DllImport("SHELL32.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
static extern IntPtr FindExecutableA(
[MarshalAs(UnmanagedType.LPStr)] string lpFile, // LPCSTR
[MarshalAs(UnmanagedType.LPStr)] string lpDirectory, // LPCSTR optional
[MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder lpResult // LPSTR out
);<DllImport("SHELL32.dll", CharSet:=CharSet.Ansi, ExactSpelling:=True)>
Public Shared Function FindExecutableA(
<MarshalAs(UnmanagedType.LPStr)> lpFile As String, ' LPCSTR
<MarshalAs(UnmanagedType.LPStr)> lpDirectory As String, ' LPCSTR optional
<MarshalAs(UnmanagedType.LPStr)> lpResult As System.Text.StringBuilder ' LPSTR out
) As IntPtr
End Function' lpFile : LPCSTR
' lpDirectory : LPCSTR optional
' lpResult : LPSTR out
Declare PtrSafe Function FindExecutableA Lib "shell32" ( _
ByVal lpFile As String, _
ByVal lpDirectory As String, _
ByVal lpResult As String) As LongPtr
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
FindExecutableA = ctypes.windll.shell32.FindExecutableA
FindExecutableA.restype = ctypes.c_void_p
FindExecutableA.argtypes = [
wintypes.LPCSTR, # lpFile : LPCSTR
wintypes.LPCSTR, # lpDirectory : LPCSTR optional
wintypes.LPSTR, # lpResult : LPSTR out
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('SHELL32.dll')
FindExecutableA = Fiddle::Function.new(
lib['FindExecutableA'],
[
Fiddle::TYPE_VOIDP, # lpFile : LPCSTR
Fiddle::TYPE_VOIDP, # lpDirectory : LPCSTR optional
Fiddle::TYPE_VOIDP, # lpResult : LPSTR out
],
Fiddle::TYPE_VOIDP)#[link(name = "shell32")]
extern "system" {
fn FindExecutableA(
lpFile: *const u8, // LPCSTR
lpDirectory: *const u8, // LPCSTR optional
lpResult: *mut u8 // LPSTR out
) -> *mut core::ffi::c_void;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("SHELL32.dll", CharSet = CharSet.Ansi)]
public static extern IntPtr FindExecutableA([MarshalAs(UnmanagedType.LPStr)] string lpFile, [MarshalAs(UnmanagedType.LPStr)] string lpDirectory, [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder lpResult);
"@
$api = Add-Type -MemberDefinition $sig -Name 'SHELL32_FindExecutableA' -Namespace Win32 -PassThru
# $api::FindExecutableA(lpFile, lpDirectory, lpResult)#uselib "SHELL32.dll"
#func global FindExecutableA "FindExecutableA" sptr, sptr, sptr
; FindExecutableA lpFile, lpDirectory, varptr(lpResult) ; 戻り値は stat
; lpFile : LPCSTR -> "sptr"
; lpDirectory : LPCSTR optional -> "sptr"
; lpResult : LPSTR out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "SHELL32.dll" #cfunc global FindExecutableA "FindExecutableA" str, str, var ; res = FindExecutableA(lpFile, lpDirectory, lpResult) ; lpFile : LPCSTR -> "str" ; lpDirectory : LPCSTR optional -> "str" ; lpResult : LPSTR out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "SHELL32.dll" #cfunc global FindExecutableA "FindExecutableA" str, str, sptr ; res = FindExecutableA(lpFile, lpDirectory, varptr(lpResult)) ; lpFile : LPCSTR -> "str" ; lpDirectory : LPCSTR optional -> "str" ; lpResult : LPSTR out -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
; HINSTANCE FindExecutableA(LPCSTR lpFile, LPCSTR lpDirectory, LPSTR lpResult) #uselib "SHELL32.dll" #cfunc global FindExecutableA "FindExecutableA" str, str, var ; res = FindExecutableA(lpFile, lpDirectory, lpResult) ; lpFile : LPCSTR -> "str" ; lpDirectory : LPCSTR optional -> "str" ; lpResult : LPSTR out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; HINSTANCE FindExecutableA(LPCSTR lpFile, LPCSTR lpDirectory, LPSTR lpResult) #uselib "SHELL32.dll" #cfunc global FindExecutableA "FindExecutableA" str, str, intptr ; res = FindExecutableA(lpFile, lpDirectory, varptr(lpResult)) ; lpFile : LPCSTR -> "str" ; lpDirectory : LPCSTR optional -> "str" ; lpResult : LPSTR out -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
shell32 = windows.NewLazySystemDLL("SHELL32.dll")
procFindExecutableA = shell32.NewProc("FindExecutableA")
)
// lpFile (LPCSTR), lpDirectory (LPCSTR optional), lpResult (LPSTR out)
r1, _, err := procFindExecutableA.Call(
uintptr(unsafe.Pointer(windows.BytePtrFromString(lpFile))),
uintptr(unsafe.Pointer(windows.BytePtrFromString(lpDirectory))),
uintptr(lpResult),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // HINSTANCEfunction FindExecutableA(
lpFile: PAnsiChar; // LPCSTR
lpDirectory: PAnsiChar; // LPCSTR optional
lpResult: PAnsiChar // LPSTR out
): THandle; stdcall;
external 'SHELL32.dll' name 'FindExecutableA';result := DllCall("SHELL32\FindExecutableA"
, "AStr", lpFile ; LPCSTR
, "AStr", lpDirectory ; LPCSTR optional
, "Ptr", lpResult ; LPSTR out
, "Ptr") ; return: HINSTANCE●FindExecutableA(lpFile, lpDirectory, lpResult) = DLL("SHELL32.dll", "void* FindExecutableA(char*, char*, char*)")
# 呼び出し: FindExecutableA(lpFile, lpDirectory, lpResult)
# lpFile : LPCSTR -> "char*"
# lpDirectory : LPCSTR optional -> "char*"
# lpResult : LPSTR out -> "char*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "shell32" fn FindExecutableA(
lpFile: [*c]const u8, // LPCSTR
lpDirectory: [*c]const u8, // LPCSTR optional
lpResult: [*c]u8 // LPSTR out
) callconv(std.os.windows.WINAPI) ?*anyopaque;proc FindExecutableA(
lpFile: cstring, # LPCSTR
lpDirectory: cstring, # LPCSTR optional
lpResult: ptr char # LPSTR out
): pointer {.importc: "FindExecutableA", stdcall, dynlib: "SHELL32.dll".}pragma(lib, "shell32");
extern(Windows)
void* FindExecutableA(
const(char)* lpFile, // LPCSTR
const(char)* lpDirectory, // LPCSTR optional
char* lpResult // LPSTR out
);ccall((:FindExecutableA, "SHELL32.dll"), stdcall, Ptr{Cvoid},
(Cstring, Cstring, Ptr{UInt8}),
lpFile, lpDirectory, lpResult)
# lpFile : LPCSTR -> Cstring
# lpDirectory : LPCSTR optional -> Cstring
# lpResult : LPSTR out -> Ptr{UInt8}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
void* FindExecutableA(
const char* lpFile,
const char* lpDirectory,
char* lpResult);
]]
local shell32 = ffi.load("shell32")
-- shell32.FindExecutableA(lpFile, lpDirectory, lpResult)
-- lpFile : LPCSTR
-- lpDirectory : LPCSTR optional
-- lpResult : LPSTR out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('SHELL32.dll');
const FindExecutableA = lib.func('__stdcall', 'FindExecutableA', 'void *', ['str', 'str', 'char *']);
// FindExecutableA(lpFile, lpDirectory, lpResult)
// lpFile : LPCSTR -> 'str'
// lpDirectory : LPCSTR optional -> 'str'
// lpResult : LPSTR out -> 'char *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("SHELL32.dll", {
FindExecutableA: { parameters: ["buffer", "buffer", "buffer"], result: "pointer" },
});
// lib.symbols.FindExecutableA(lpFile, lpDirectory, lpResult)
// lpFile : LPCSTR -> "buffer"
// lpDirectory : LPCSTR optional -> "buffer"
// lpResult : LPSTR out -> "buffer"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
void* FindExecutableA(
const char* lpFile,
const char* lpDirectory,
char* lpResult);
C, "SHELL32.dll");
// $ffi->FindExecutableA(lpFile, lpDirectory, lpResult);
// lpFile : LPCSTR
// lpDirectory : LPCSTR optional
// lpResult : LPSTR 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.ASCII_OPTIONS);
Pointer FindExecutableA(
String lpFile, // LPCSTR
String lpDirectory, // LPCSTR optional
byte[] lpResult // LPSTR out
);
}@[Link("shell32")]
lib LibSHELL32
fun FindExecutableA = FindExecutableA(
lpFile : UInt8*, # LPCSTR
lpDirectory : UInt8*, # LPCSTR optional
lpResult : UInt8* # LPSTR out
) : Void*
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef FindExecutableANative = Pointer<Void> Function(Pointer<Utf8>, Pointer<Utf8>, Pointer<Utf8>);
typedef FindExecutableADart = Pointer<Void> Function(Pointer<Utf8>, Pointer<Utf8>, Pointer<Utf8>);
final FindExecutableA = DynamicLibrary.open('SHELL32.dll')
.lookupFunction<FindExecutableANative, FindExecutableADart>('FindExecutableA');
// lpFile : LPCSTR -> Pointer<Utf8>
// lpDirectory : LPCSTR optional -> Pointer<Utf8>
// lpResult : LPSTR out -> Pointer<Utf8>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function FindExecutableA(
lpFile: PAnsiChar; // LPCSTR
lpDirectory: PAnsiChar; // LPCSTR optional
lpResult: PAnsiChar // LPSTR out
): THandle; stdcall;
external 'SHELL32.dll' name 'FindExecutableA';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "FindExecutableA"
c_FindExecutableA :: CString -> CString -> CString -> IO (Ptr ())
-- lpFile : LPCSTR -> CString
-- lpDirectory : LPCSTR optional -> CString
-- lpResult : LPSTR out -> CString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let findexecutablea =
foreign "FindExecutableA"
(string @-> string @-> string @-> returning (ptr void))
(* lpFile : LPCSTR -> string *)
(* lpDirectory : LPCSTR optional -> string *)
(* lpResult : LPSTR out -> string *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library shell32 (t "SHELL32.dll"))
(cffi:use-foreign-library shell32)
(cffi:defcfun ("FindExecutableA" find-executable-a :convention :stdcall) :pointer
(lp-file :string) ; LPCSTR
(lp-directory :string) ; LPCSTR optional
(lp-result :pointer)) ; LPSTR out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $FindExecutableA = Win32::API::More->new('SHELL32',
'HANDLE FindExecutableA(LPCSTR lpFile, LPCSTR lpDirectory, LPSTR lpResult)');
# my $ret = $FindExecutableA->Call($lpFile, $lpDirectory, $lpResult);
# lpFile : LPCSTR -> LPCSTR
# lpDirectory : LPCSTR optional -> LPCSTR
# lpResult : LPSTR out -> LPSTR
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
- f FindExecutableW (Unicode版) — ファイル(Unicode)に関連付けられた実行ファイルを検索する。
- f ShellExecuteA — ファイルやプログラム(ANSI)に対し操作を実行する。