Win32 API 日本語リファレンス
ホームUI.Shell › ShellExecuteA

ShellExecuteA

関数
ファイルやプログラム(ANSI)に対し操作を実行する。
DLLSHELL32.dll文字セットANSI (-A)呼出規約winapi対応OSWindows XP 以降

シグネチャ

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

HINSTANCE ShellExecuteA(
    HWND hwnd,   // optional
    LPCSTR lpOperation,   // optional
    LPCSTR lpFile,
    LPCSTR lpParameters,   // optional
    LPCSTR lpDirectory,   // optional
    SHOW_WINDOW_CMD nShowCmd
);

パラメーター

名前方向説明
hwndHWNDinoptionalUI やエラーメッセージの表示に使用する親ウィンドウのハンドルです。操作がウィンドウに関連付けられていない場合、この値は NULL にできます。
lpOperationLPCSTRinoptional

実行する操作を指定する null 終端文字列へのポインターで、この場合は動詞 (verb) と呼ばれます。利用可能な動詞のセットは、対象となるファイルやフォルダーによって異なります。一般に、オブジェクトのショートカットメニューから利用できる操作が、使用可能な動詞となります。次の動詞がよく使用されます。

:::no-loc text="edit":::

エディターを起動し、ドキュメントを編集用に開きます。lpFile がドキュメントファイルでない場合、この関数は失敗します。

:::no-loc text="explore":::

lpFile で指定されたフォルダーを参照します。

:::no-loc text="find":::

lpDirectory で指定されたディレクトリを起点として検索を開始します。

:::no-loc text="open":::

lpFile パラメーターで指定された項目を開きます。項目はファイルまたはフォルダーです。

:::no-loc text="print":::

lpFile で指定されたファイルを印刷します。lpFile がドキュメントファイルでない場合、この関数は失敗します。

runas

アプリケーションを管理者として起動します。ユーザーアカウント制御 (UAC) は、アプリケーションを昇格して実行するための同意をユーザーに求めるか、アプリケーションの実行に使用する管理者アカウントの資格情報の入力を求めます。

NULL

利用可能であれば既定の動詞が使用されます。利用できない場合は ":::no-loc text="open":::" 動詞が使用されます。いずれの動詞も利用できない場合、システムはレジストリに登録されている最初の動詞を使用します。

lpFileLPCSTRin指定した動詞を実行する対象のファイルまたはオブジェクトを指定する null 終端文字列へのポインターです。シェル名前空間オブジェクトを指定するには、完全修飾された解析名 (parse name) を渡します。すべてのオブジェクトですべての動詞がサポートされるわけではない点に注意してください。たとえば、すべてのドキュメントの種類が ":::no-loc text="print":::" 動詞をサポートするわけではありません。lpDirectory パラメーターに相対パスを使用する場合は、lpFile に相対パスを使用しないでください。
lpParametersLPCSTRinoptionallpFile が実行可能ファイルを指定している場合、このパラメーターはアプリケーションに渡すパラメーターを指定する null 終端文字列へのポインターです。この文字列の形式は、呼び出される動詞によって決まります。lpFile がドキュメントファイルを指定している場合、lpParametersNULL にする必要があります。
lpDirectoryLPCSTRinoptional操作の既定の (作業) ディレクトリを指定する null 終端文字列へのポインターです。この値が NULL の場合、現在の作業ディレクトリが使用されます。lpFile に相対パスを指定する場合は、lpDirectory に相対パスを使用しないでください。
nShowCmdSHOW_WINDOW_CMDinアプリケーションを開いたときの表示方法を指定するフラグです。lpFile がドキュメントファイルを指定している場合、フラグは単に関連付けられたアプリケーションに渡されます。これをどのように扱うかはアプリケーションが決定します。ShowWindow 関数の nCmdShow パラメーターに指定できる任意の値を使用できます。

戻り値の型: HINSTANCE

公式ドキュメント

指定したファイルに対して操作を実行します。(ShellExecuteA)

戻り値

型: HINSTANCE

関数が成功した場合、32 より大きい値を返します。関数が失敗した場合、失敗の原因を示すエラー値を返します。戻り値は 16 ビット Windows アプリケーションとの後方互換性のために HINSTANCE にキャストされていますが、真の HINSTANCE ではありません。INT_PTR にのみキャストでき、32 または以下のエラーコードと比較することができます。

戻りコード 説明
0
オペレーティングシステムのメモリまたはリソースが不足しています。
ERROR_FILE_NOT_FOUND
指定されたファイルが見つかりませんでした。
ERROR_PATH_NOT_FOUND
指定されたパスが見つかりませんでした。
ERROR_BAD_FORMAT
.exe ファイルが無効です (Win32 以外の .exe であるか、.exe イメージにエラーがあります)。
SE_ERR_ACCESSDENIED
オペレーティングシステムが指定されたファイルへのアクセスを拒否しました。
SE_ERR_ASSOCINCOMPLETE
ファイル名の関連付けが不完全であるか、無効です。
SE_ERR_DDEBUSY
他の DDE トランザクションが処理中であったため、DDE トランザクションを完了できませんでした。
SE_ERR_DDEFAIL
DDE トランザクションが失敗しました。
SE_ERR_DDETIMEOUT
要求がタイムアウトしたため、DDE トランザクションを完了できませんでした。
SE_ERR_DLLNOTFOUND
指定された DLL が見つかりませんでした。
SE_ERR_FNF
指定されたファイルが見つかりませんでした。
SE_ERR_NOASSOC
指定されたファイル名拡張子に関連付けられたアプリケーションがありません。印刷できないファイルを印刷しようとした場合にも、このエラーが返されます。
SE_ERR_OOM
操作を完了するのに十分なメモリがありませんでした。
SE_ERR_PNF
指定されたパスが見つかりませんでした。
SE_ERR_SHARE
共有違反が発生しました。

拡張エラー情報を取得するには GetLastError を呼び出します。

解説(Remarks)

ShellExecute は、Component Object Model (COM) を使用してアクティブ化されるシェル拡張 (データソース、コンテキストメニューハンドラー、動詞の実装) に実行を委譲できるため、ShellExecute を呼び出す前に COM を初期化しておく必要があります。一部のシェル拡張は、COM のシングルスレッドアパートメント (STA) 型を必要とします。その場合、COM は次のように初期化する必要があります。

CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE)

ShellExecute がこれらの種類のシェル拡張を使用せず、COM の初期化をまったく必要としないケースも確かに存在します。とはいえ、この関数を使用する前に常に COM を初期化しておくことが良い習慣です。

このメソッドを使用すると、フォルダーのショートカットメニューにあるコマンドや、レジストリに格納されているコマンドを実行できます。

フォルダーを開くには、次のいずれかの呼び出しを使用します。

ShellExecute(handle, NULL, <fully_qualified_path_to_folder>, NULL, NULL, SW_SHOWNORMAL);

または

ShellExecute(handle, "open", <fully_qualified_path_to_folder>, NULL, NULL, SW_SHOWNORMAL);

フォルダーを参照するには、次の呼び出しを使用します。

ShellExecute(handle, "explore", <fully_qualified_path_to_folder>, NULL, NULL, SW_SHOWNORMAL);

ディレクトリに対してシェルの検索ユーティリティを起動するには、次の呼び出しを使用します。

ShellExecute(handle, "find", <fully_qualified_path_to_folder>, NULL, NULL, 0);

lpOperationNULL の場合、この関数は lpFile で指定されたファイルを開きます。lpOperation が ":::no-loc text="open":::" または ":::no-loc text="explore":::" の場合、この関数はフォルダーを開くか参照しようとします。

ShellExecute の呼び出しによって起動されたアプリケーションに関する情報を取得するには、ShellExecuteEx を使用します。

注意 フォルダーオプションの 別のプロセスでフォルダーウィンドウを開く 設定は ShellExecute に影響します。このオプションが無効になっている場合 (既定の設定)、ShellExecute は新しいウィンドウを起動するのではなく、開いているエクスプローラーのウィンドウを使用します。エクスプローラーのウィンドウが開いていない場合、ShellExecute は新しいウィンドウを起動します。
メモ

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

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

各言語での呼び出し定義

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

HINSTANCE ShellExecuteA(
    HWND hwnd,   // optional
    LPCSTR lpOperation,   // optional
    LPCSTR lpFile,
    LPCSTR lpParameters,   // optional
    LPCSTR lpDirectory,   // optional
    SHOW_WINDOW_CMD nShowCmd
);
[DllImport("SHELL32.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
static extern IntPtr ShellExecuteA(
    IntPtr hwnd,   // HWND optional
    [MarshalAs(UnmanagedType.LPStr)] string lpOperation,   // LPCSTR optional
    [MarshalAs(UnmanagedType.LPStr)] string lpFile,   // LPCSTR
    [MarshalAs(UnmanagedType.LPStr)] string lpParameters,   // LPCSTR optional
    [MarshalAs(UnmanagedType.LPStr)] string lpDirectory,   // LPCSTR optional
    int nShowCmd   // SHOW_WINDOW_CMD
);
<DllImport("SHELL32.dll", CharSet:=CharSet.Ansi, ExactSpelling:=True)>
Public Shared Function ShellExecuteA(
    hwnd As IntPtr,   ' HWND optional
    <MarshalAs(UnmanagedType.LPStr)> lpOperation As String,   ' LPCSTR optional
    <MarshalAs(UnmanagedType.LPStr)> lpFile As String,   ' LPCSTR
    <MarshalAs(UnmanagedType.LPStr)> lpParameters As String,   ' LPCSTR optional
    <MarshalAs(UnmanagedType.LPStr)> lpDirectory As String,   ' LPCSTR optional
    nShowCmd As Integer   ' SHOW_WINDOW_CMD
) As IntPtr
End Function
' hwnd : HWND optional
' lpOperation : LPCSTR optional
' lpFile : LPCSTR
' lpParameters : LPCSTR optional
' lpDirectory : LPCSTR optional
' nShowCmd : SHOW_WINDOW_CMD
Declare PtrSafe Function ShellExecuteA Lib "shell32" ( _
    ByVal hwnd As LongPtr, _
    ByVal lpOperation As String, _
    ByVal lpFile As String, _
    ByVal lpParameters As String, _
    ByVal lpDirectory As String, _
    ByVal nShowCmd As Long) As LongPtr
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

ShellExecuteA = ctypes.windll.shell32.ShellExecuteA
ShellExecuteA.restype = ctypes.c_void_p
ShellExecuteA.argtypes = [
    wintypes.HANDLE,  # hwnd : HWND optional
    wintypes.LPCSTR,  # lpOperation : LPCSTR optional
    wintypes.LPCSTR,  # lpFile : LPCSTR
    wintypes.LPCSTR,  # lpParameters : LPCSTR optional
    wintypes.LPCSTR,  # lpDirectory : LPCSTR optional
    ctypes.c_int,  # nShowCmd : SHOW_WINDOW_CMD
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('SHELL32.dll')
ShellExecuteA = Fiddle::Function.new(
  lib['ShellExecuteA'],
  [
    Fiddle::TYPE_VOIDP,  # hwnd : HWND optional
    Fiddle::TYPE_VOIDP,  # lpOperation : LPCSTR optional
    Fiddle::TYPE_VOIDP,  # lpFile : LPCSTR
    Fiddle::TYPE_VOIDP,  # lpParameters : LPCSTR optional
    Fiddle::TYPE_VOIDP,  # lpDirectory : LPCSTR optional
    Fiddle::TYPE_INT,  # nShowCmd : SHOW_WINDOW_CMD
  ],
  Fiddle::TYPE_VOIDP)
#[link(name = "shell32")]
extern "system" {
    fn ShellExecuteA(
        hwnd: *mut core::ffi::c_void,  // HWND optional
        lpOperation: *const u8,  // LPCSTR optional
        lpFile: *const u8,  // LPCSTR
        lpParameters: *const u8,  // LPCSTR optional
        lpDirectory: *const u8,  // LPCSTR optional
        nShowCmd: i32  // SHOW_WINDOW_CMD
    ) -> *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 ShellExecuteA(IntPtr hwnd, [MarshalAs(UnmanagedType.LPStr)] string lpOperation, [MarshalAs(UnmanagedType.LPStr)] string lpFile, [MarshalAs(UnmanagedType.LPStr)] string lpParameters, [MarshalAs(UnmanagedType.LPStr)] string lpDirectory, int nShowCmd);
"@
$api = Add-Type -MemberDefinition $sig -Name 'SHELL32_ShellExecuteA' -Namespace Win32 -PassThru
# $api::ShellExecuteA(hwnd, lpOperation, lpFile, lpParameters, lpDirectory, nShowCmd)
#uselib "SHELL32.dll"
#func global ShellExecuteA "ShellExecuteA" sptr, sptr, sptr, sptr, sptr, sptr
; ShellExecuteA hwnd, lpOperation, lpFile, lpParameters, lpDirectory, nShowCmd   ; 戻り値は stat
; hwnd : HWND optional -> "sptr"
; lpOperation : LPCSTR optional -> "sptr"
; lpFile : LPCSTR -> "sptr"
; lpParameters : LPCSTR optional -> "sptr"
; lpDirectory : LPCSTR optional -> "sptr"
; nShowCmd : SHOW_WINDOW_CMD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "SHELL32.dll"
#cfunc global ShellExecuteA "ShellExecuteA" sptr, str, str, str, str, int
; res = ShellExecuteA(hwnd, lpOperation, lpFile, lpParameters, lpDirectory, nShowCmd)
; hwnd : HWND optional -> "sptr"
; lpOperation : LPCSTR optional -> "str"
; lpFile : LPCSTR -> "str"
; lpParameters : LPCSTR optional -> "str"
; lpDirectory : LPCSTR optional -> "str"
; nShowCmd : SHOW_WINDOW_CMD -> "int"
; HINSTANCE ShellExecuteA(HWND hwnd, LPCSTR lpOperation, LPCSTR lpFile, LPCSTR lpParameters, LPCSTR lpDirectory, SHOW_WINDOW_CMD nShowCmd)
#uselib "SHELL32.dll"
#cfunc global ShellExecuteA "ShellExecuteA" intptr, str, str, str, str, int
; res = ShellExecuteA(hwnd, lpOperation, lpFile, lpParameters, lpDirectory, nShowCmd)
; hwnd : HWND optional -> "intptr"
; lpOperation : LPCSTR optional -> "str"
; lpFile : LPCSTR -> "str"
; lpParameters : LPCSTR optional -> "str"
; lpDirectory : LPCSTR optional -> "str"
; nShowCmd : SHOW_WINDOW_CMD -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	shell32 = windows.NewLazySystemDLL("SHELL32.dll")
	procShellExecuteA = shell32.NewProc("ShellExecuteA")
)

// hwnd (HWND optional), lpOperation (LPCSTR optional), lpFile (LPCSTR), lpParameters (LPCSTR optional), lpDirectory (LPCSTR optional), nShowCmd (SHOW_WINDOW_CMD)
r1, _, err := procShellExecuteA.Call(
	uintptr(hwnd),
	uintptr(unsafe.Pointer(windows.BytePtrFromString(lpOperation))),
	uintptr(unsafe.Pointer(windows.BytePtrFromString(lpFile))),
	uintptr(unsafe.Pointer(windows.BytePtrFromString(lpParameters))),
	uintptr(unsafe.Pointer(windows.BytePtrFromString(lpDirectory))),
	uintptr(nShowCmd),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // HINSTANCE
function ShellExecuteA(
  hwnd: THandle;   // HWND optional
  lpOperation: PAnsiChar;   // LPCSTR optional
  lpFile: PAnsiChar;   // LPCSTR
  lpParameters: PAnsiChar;   // LPCSTR optional
  lpDirectory: PAnsiChar;   // LPCSTR optional
  nShowCmd: Integer   // SHOW_WINDOW_CMD
): THandle; stdcall;
  external 'SHELL32.dll' name 'ShellExecuteA';
result := DllCall("SHELL32\ShellExecuteA"
    , "Ptr", hwnd   ; HWND optional
    , "AStr", lpOperation   ; LPCSTR optional
    , "AStr", lpFile   ; LPCSTR
    , "AStr", lpParameters   ; LPCSTR optional
    , "AStr", lpDirectory   ; LPCSTR optional
    , "Int", nShowCmd   ; SHOW_WINDOW_CMD
    , "Ptr")   ; return: HINSTANCE
●ShellExecuteA(hwnd, lpOperation, lpFile, lpParameters, lpDirectory, nShowCmd) = DLL("SHELL32.dll", "void* ShellExecuteA(void*, char*, char*, char*, char*, int)")
# 呼び出し: ShellExecuteA(hwnd, lpOperation, lpFile, lpParameters, lpDirectory, nShowCmd)
# hwnd : HWND optional -> "void*"
# lpOperation : LPCSTR optional -> "char*"
# lpFile : LPCSTR -> "char*"
# lpParameters : LPCSTR optional -> "char*"
# lpDirectory : LPCSTR optional -> "char*"
# nShowCmd : SHOW_WINDOW_CMD -> "int"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "shell32" fn ShellExecuteA(
    hwnd: ?*anyopaque, // HWND optional
    lpOperation: [*c]const u8, // LPCSTR optional
    lpFile: [*c]const u8, // LPCSTR
    lpParameters: [*c]const u8, // LPCSTR optional
    lpDirectory: [*c]const u8, // LPCSTR optional
    nShowCmd: i32 // SHOW_WINDOW_CMD
) callconv(std.os.windows.WINAPI) ?*anyopaque;
proc ShellExecuteA(
    hwnd: pointer,  # HWND optional
    lpOperation: cstring,  # LPCSTR optional
    lpFile: cstring,  # LPCSTR
    lpParameters: cstring,  # LPCSTR optional
    lpDirectory: cstring,  # LPCSTR optional
    nShowCmd: int32  # SHOW_WINDOW_CMD
): pointer {.importc: "ShellExecuteA", stdcall, dynlib: "SHELL32.dll".}
pragma(lib, "shell32");
extern(Windows)
void* ShellExecuteA(
    void* hwnd,   // HWND optional
    const(char)* lpOperation,   // LPCSTR optional
    const(char)* lpFile,   // LPCSTR
    const(char)* lpParameters,   // LPCSTR optional
    const(char)* lpDirectory,   // LPCSTR optional
    int nShowCmd   // SHOW_WINDOW_CMD
);
ccall((:ShellExecuteA, "SHELL32.dll"), stdcall, Ptr{Cvoid},
      (Ptr{Cvoid}, Cstring, Cstring, Cstring, Cstring, Int32),
      hwnd, lpOperation, lpFile, lpParameters, lpDirectory, nShowCmd)
# hwnd : HWND optional -> Ptr{Cvoid}
# lpOperation : LPCSTR optional -> Cstring
# lpFile : LPCSTR -> Cstring
# lpParameters : LPCSTR optional -> Cstring
# lpDirectory : LPCSTR optional -> Cstring
# nShowCmd : SHOW_WINDOW_CMD -> Int32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
void* ShellExecuteA(
    void* hwnd,
    const char* lpOperation,
    const char* lpFile,
    const char* lpParameters,
    const char* lpDirectory,
    int32_t nShowCmd);
]]
local shell32 = ffi.load("shell32")
-- shell32.ShellExecuteA(hwnd, lpOperation, lpFile, lpParameters, lpDirectory, nShowCmd)
-- hwnd : HWND optional
-- lpOperation : LPCSTR optional
-- lpFile : LPCSTR
-- lpParameters : LPCSTR optional
-- lpDirectory : LPCSTR optional
-- nShowCmd : SHOW_WINDOW_CMD
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('SHELL32.dll');
const ShellExecuteA = lib.func('__stdcall', 'ShellExecuteA', 'void *', ['void *', 'str', 'str', 'str', 'str', 'int32_t']);
// ShellExecuteA(hwnd, lpOperation, lpFile, lpParameters, lpDirectory, nShowCmd)
// hwnd : HWND optional -> 'void *'
// lpOperation : LPCSTR optional -> 'str'
// lpFile : LPCSTR -> 'str'
// lpParameters : LPCSTR optional -> 'str'
// lpDirectory : LPCSTR optional -> 'str'
// nShowCmd : SHOW_WINDOW_CMD -> 'int32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("SHELL32.dll", {
  ShellExecuteA: { parameters: ["pointer", "buffer", "buffer", "buffer", "buffer", "i32"], result: "pointer" },
});
// lib.symbols.ShellExecuteA(hwnd, lpOperation, lpFile, lpParameters, lpDirectory, nShowCmd)
// hwnd : HWND optional -> "pointer"
// lpOperation : LPCSTR optional -> "buffer"
// lpFile : LPCSTR -> "buffer"
// lpParameters : LPCSTR optional -> "buffer"
// lpDirectory : LPCSTR optional -> "buffer"
// nShowCmd : SHOW_WINDOW_CMD -> "i32"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
void* ShellExecuteA(
    void* hwnd,
    const char* lpOperation,
    const char* lpFile,
    const char* lpParameters,
    const char* lpDirectory,
    int32_t nShowCmd);
C, "SHELL32.dll");
// $ffi->ShellExecuteA(hwnd, lpOperation, lpFile, lpParameters, lpDirectory, nShowCmd);
// hwnd : HWND optional
// lpOperation : LPCSTR optional
// lpFile : LPCSTR
// lpParameters : LPCSTR optional
// lpDirectory : LPCSTR optional
// nShowCmd : SHOW_WINDOW_CMD
// 構造体/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 ShellExecuteA(
        Pointer hwnd,   // HWND optional
        String lpOperation,   // LPCSTR optional
        String lpFile,   // LPCSTR
        String lpParameters,   // LPCSTR optional
        String lpDirectory,   // LPCSTR optional
        int nShowCmd   // SHOW_WINDOW_CMD
    );
}
@[Link("shell32")]
lib LibSHELL32
  fun ShellExecuteA = ShellExecuteA(
    hwnd : Void*,   # HWND optional
    lpOperation : UInt8*,   # LPCSTR optional
    lpFile : UInt8*,   # LPCSTR
    lpParameters : UInt8*,   # LPCSTR optional
    lpDirectory : UInt8*,   # LPCSTR optional
    nShowCmd : Int32   # SHOW_WINDOW_CMD
  ) : Void*
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef ShellExecuteANative = Pointer<Void> Function(Pointer<Void>, Pointer<Utf8>, Pointer<Utf8>, Pointer<Utf8>, Pointer<Utf8>, Int32);
typedef ShellExecuteADart = Pointer<Void> Function(Pointer<Void>, Pointer<Utf8>, Pointer<Utf8>, Pointer<Utf8>, Pointer<Utf8>, int);
final ShellExecuteA = DynamicLibrary.open('SHELL32.dll')
    .lookupFunction<ShellExecuteANative, ShellExecuteADart>('ShellExecuteA');
// hwnd : HWND optional -> Pointer<Void>
// lpOperation : LPCSTR optional -> Pointer<Utf8>
// lpFile : LPCSTR -> Pointer<Utf8>
// lpParameters : LPCSTR optional -> Pointer<Utf8>
// lpDirectory : LPCSTR optional -> Pointer<Utf8>
// nShowCmd : SHOW_WINDOW_CMD -> Int32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function ShellExecuteA(
  hwnd: THandle;   // HWND optional
  lpOperation: PAnsiChar;   // LPCSTR optional
  lpFile: PAnsiChar;   // LPCSTR
  lpParameters: PAnsiChar;   // LPCSTR optional
  lpDirectory: PAnsiChar;   // LPCSTR optional
  nShowCmd: Integer   // SHOW_WINDOW_CMD
): THandle; stdcall;
  external 'SHELL32.dll' name 'ShellExecuteA';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "ShellExecuteA"
  c_ShellExecuteA :: Ptr () -> CString -> CString -> CString -> CString -> Int32 -> IO (Ptr ())
-- hwnd : HWND optional -> Ptr ()
-- lpOperation : LPCSTR optional -> CString
-- lpFile : LPCSTR -> CString
-- lpParameters : LPCSTR optional -> CString
-- lpDirectory : LPCSTR optional -> CString
-- nShowCmd : SHOW_WINDOW_CMD -> Int32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let shellexecutea =
  foreign "ShellExecuteA"
    ((ptr void) @-> string @-> string @-> string @-> string @-> int32_t @-> returning (ptr void))
(* hwnd : HWND optional -> (ptr void) *)
(* lpOperation : LPCSTR optional -> string *)
(* lpFile : LPCSTR -> string *)
(* lpParameters : LPCSTR optional -> string *)
(* lpDirectory : LPCSTR optional -> string *)
(* nShowCmd : SHOW_WINDOW_CMD -> int32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library shell32 (t "SHELL32.dll"))
(cffi:use-foreign-library shell32)

(cffi:defcfun ("ShellExecuteA" shell-execute-a :convention :stdcall) :pointer
  (hwnd :pointer)   ; HWND optional
  (lp-operation :string)   ; LPCSTR optional
  (lp-file :string)   ; LPCSTR
  (lp-parameters :string)   ; LPCSTR optional
  (lp-directory :string)   ; LPCSTR optional
  (n-show-cmd :int32))   ; SHOW_WINDOW_CMD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $ShellExecuteA = Win32::API::More->new('SHELL32',
    'HANDLE ShellExecuteA(HANDLE hwnd, LPCSTR lpOperation, LPCSTR lpFile, LPCSTR lpParameters, LPCSTR lpDirectory, int nShowCmd)');
# my $ret = $ShellExecuteA->Call($hwnd, $lpOperation, $lpFile, $lpParameters, $lpDirectory, $nShowCmd);
# hwnd : HWND optional -> HANDLE
# lpOperation : LPCSTR optional -> LPCSTR
# lpFile : LPCSTR -> LPCSTR
# lpParameters : LPCSTR optional -> LPCSTR
# lpDirectory : LPCSTR optional -> LPCSTR
# nShowCmd : SHOW_WINDOW_CMD -> int
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

文字セット違い
類似 API
公式の関連項目
使用する型