ShellExecuteW
関数シグネチャ
// SHELL32.dll (Unicode / -W)
#include <windows.h>
HINSTANCE ShellExecuteW(
HWND hwnd, // optional
LPCWSTR lpOperation, // optional
LPCWSTR lpFile,
LPCWSTR lpParameters, // optional
LPCWSTR lpDirectory, // optional
SHOW_WINDOW_CMD nShowCmd
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| hwnd | HWND | inoptional | UI やエラーメッセージの表示に使用される親ウィンドウへのハンドルです。操作がウィンドウに関連付けられていない場合、この値は NULL にできます。 |
| lpOperation | LPCWSTR | inoptional | 実行するアクションを指定する、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":::" 動詞が使用されます。どちらの動詞も利用できない場合、システムはレジストリに登録されている最初の動詞を使用します。 |
| lpFile | LPCWSTR | in | 指定した動詞を実行する対象のファイルまたはオブジェクトを指定する、null 終端文字列へのポインターです。シェル名前空間オブジェクトを指定するには、完全修飾の解析名 (parse name) を渡します。すべての動詞がすべてのオブジェクトでサポートされているわけではない点に注意してください。たとえば、すべてのドキュメント形式が ":::no-loc text="print":::" 動詞をサポートしているわけではありません。lpDirectory パラメーターに相対パスを使用する場合は、lpFile に相対パスを使用しないでください。 lpFile が実行可能ファイルを指定し、かつ lpOperation で ":::no-loc text="open":::" または "runas" 動詞が使用される場合、起動されるプロセスのコマンドライン文字列の末尾には空白文字が付加されます。 |
| lpParameters | LPCWSTR | inoptional | lpFile が実行可能ファイルを指定する場合、このパラメーターはアプリケーションに渡すパラメーターを指定する null 終端文字列へのポインターです。lpOperation で ":::no-loc text="open":::" または "runas" 動詞を使用して起動されたプロセスのコマンドライン文字列には末尾に空白文字が含まれ、パラメーターはそのコマンドライン文字列に付加されます。この文字列の形式は、呼び出される動詞によって決まります。lpFile がドキュメントファイルを指定する場合、lpParameters は NULL にする必要があります。 |
| lpDirectory | LPCWSTR | inoptional | アクションの既定 (作業) ディレクトリを指定する、null 終端文字列へのポインターです。この値が NULL の場合、現在の作業ディレクトリが使用されます。lpFile に相対パスを指定する場合は、lpDirectory に相対パスを使用しないでください。 |
| nShowCmd | SHOW_WINDOW_CMD | in | アプリケーションを開いたときの表示方法を指定するフラグです。lpFile がドキュメントファイルを指定する場合、このフラグは単に関連付けられたアプリケーションに渡されます。その扱い方を決定するのはアプリケーション側です。ShowWindow 関数の nCmdShow パラメーターに指定できる任意の値を使用できます。 |
戻り値の型: HINSTANCE
公式ドキュメント
指定されたファイルに対して操作を実行します。(ShellExecuteW)
戻り値
型: HINSTANCE
関数が成功すると、32 より大きい値を返します。関数が失敗すると、失敗の原因を示すエラー値を返します。この戻り値は、16 ビット Windows アプリケーションとの下位互換性のために HINSTANCE 型にキャストされていますが、実際の HINSTANCE ではありません。INT_PTR にのみキャストでき、32 または以下のエラーコードと比較できます。
| 戻り値 | 説明 |
|---|---|
|
オペレーティングシステムのメモリまたはリソースが不足しています。 |
| 指定されたファイルが見つかりませんでした。 | |
| 指定されたパスが見つかりませんでした。 | |
| .exe ファイルが無効です (Win32 以外の .exe であるか、.exe イメージにエラーがあります)。 | |
| オペレーティングシステムが指定されたファイルへのアクセスを拒否しました。 | |
| ファイル名の関連付けが不完全であるか無効です。 | |
| 他の DDE トランザクションが処理中であったため、DDE トランザクションを完了できませんでした。 | |
| DDE トランザクションが失敗しました。 | |
| 要求がタイムアウトしたため、DDE トランザクションを完了できませんでした。 | |
| 指定された DLL が見つかりませんでした。 | |
| 指定されたファイルが見つかりませんでした。 | |
| 指定されたファイル名拡張子に関連付けられたアプリケーションがありません。印刷できないファイルを印刷しようとした場合にも、このエラーが返されます。 | |
| 操作を完了するのに十分なメモリがありませんでした。 | |
| 指定されたパスが見つかりませんでした。 | |
| 共有違反が発生しました。 |
拡張エラー情報を取得するには 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);
lpOperation が NULL の場合、この関数は lpFile で指定されたファイルを開きます。lpOperation が ":::no-loc text="open":::" または ":::no-loc text=""::::::no-loc text="explore":::" の場合、この関数はフォルダーを開くか、エクスプローラーで開こうとします。
ShellExecute の呼び出しの結果として起動されたアプリケーションに関する情報を取得するには、ShellExecuteEx を使用してください。
shellapi.h ヘッダーは、UNICODE プリプロセッサ定数の定義に基づいてこの関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして ShellExecute を定義しています。エンコーディング非依存のエイリアスの使用を、エンコーディング非依存でないコードと混在させると、不整合が生じてコンパイルエラーや実行時エラーの原因となることがあります。詳細については、関数プロトタイプの規約を参照してください。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// SHELL32.dll (Unicode / -W)
#include <windows.h>
HINSTANCE ShellExecuteW(
HWND hwnd, // optional
LPCWSTR lpOperation, // optional
LPCWSTR lpFile,
LPCWSTR lpParameters, // optional
LPCWSTR lpDirectory, // optional
SHOW_WINDOW_CMD nShowCmd
);[DllImport("SHELL32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern IntPtr ShellExecuteW(
IntPtr hwnd, // HWND optional
[MarshalAs(UnmanagedType.LPWStr)] string lpOperation, // LPCWSTR optional
[MarshalAs(UnmanagedType.LPWStr)] string lpFile, // LPCWSTR
[MarshalAs(UnmanagedType.LPWStr)] string lpParameters, // LPCWSTR optional
[MarshalAs(UnmanagedType.LPWStr)] string lpDirectory, // LPCWSTR optional
int nShowCmd // SHOW_WINDOW_CMD
);<DllImport("SHELL32.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function ShellExecuteW(
hwnd As IntPtr, ' HWND optional
<MarshalAs(UnmanagedType.LPWStr)> lpOperation As String, ' LPCWSTR optional
<MarshalAs(UnmanagedType.LPWStr)> lpFile As String, ' LPCWSTR
<MarshalAs(UnmanagedType.LPWStr)> lpParameters As String, ' LPCWSTR optional
<MarshalAs(UnmanagedType.LPWStr)> lpDirectory As String, ' LPCWSTR optional
nShowCmd As Integer ' SHOW_WINDOW_CMD
) As IntPtr
End Function' hwnd : HWND optional
' lpOperation : LPCWSTR optional
' lpFile : LPCWSTR
' lpParameters : LPCWSTR optional
' lpDirectory : LPCWSTR optional
' nShowCmd : SHOW_WINDOW_CMD
Declare PtrSafe Function ShellExecuteW Lib "shell32" ( _
ByVal hwnd As LongPtr, _
ByVal lpOperation As LongPtr, _
ByVal lpFile As LongPtr, _
ByVal lpParameters As LongPtr, _
ByVal lpDirectory As LongPtr, _
ByVal nShowCmd As Long) 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
ShellExecuteW = ctypes.windll.shell32.ShellExecuteW
ShellExecuteW.restype = ctypes.c_void_p
ShellExecuteW.argtypes = [
wintypes.HANDLE, # hwnd : HWND optional
wintypes.LPCWSTR, # lpOperation : LPCWSTR optional
wintypes.LPCWSTR, # lpFile : LPCWSTR
wintypes.LPCWSTR, # lpParameters : LPCWSTR optional
wintypes.LPCWSTR, # lpDirectory : LPCWSTR optional
ctypes.c_int, # nShowCmd : SHOW_WINDOW_CMD
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('SHELL32.dll')
ShellExecuteW = Fiddle::Function.new(
lib['ShellExecuteW'],
[
Fiddle::TYPE_VOIDP, # hwnd : HWND optional
Fiddle::TYPE_VOIDP, # lpOperation : LPCWSTR optional
Fiddle::TYPE_VOIDP, # lpFile : LPCWSTR
Fiddle::TYPE_VOIDP, # lpParameters : LPCWSTR optional
Fiddle::TYPE_VOIDP, # lpDirectory : LPCWSTR optional
Fiddle::TYPE_INT, # nShowCmd : SHOW_WINDOW_CMD
],
Fiddle::TYPE_VOIDP)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"#[link(name = "shell32")]
extern "system" {
fn ShellExecuteW(
hwnd: *mut core::ffi::c_void, // HWND optional
lpOperation: *const u16, // LPCWSTR optional
lpFile: *const u16, // LPCWSTR
lpParameters: *const u16, // LPCWSTR optional
lpDirectory: *const u16, // LPCWSTR 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.Unicode)]
public static extern IntPtr ShellExecuteW(IntPtr hwnd, [MarshalAs(UnmanagedType.LPWStr)] string lpOperation, [MarshalAs(UnmanagedType.LPWStr)] string lpFile, [MarshalAs(UnmanagedType.LPWStr)] string lpParameters, [MarshalAs(UnmanagedType.LPWStr)] string lpDirectory, int nShowCmd);
"@
$api = Add-Type -MemberDefinition $sig -Name 'SHELL32_ShellExecuteW' -Namespace Win32 -PassThru
# $api::ShellExecuteW(hwnd, lpOperation, lpFile, lpParameters, lpDirectory, nShowCmd)#uselib "SHELL32.dll"
#func global ShellExecuteW "ShellExecuteW" wptr, wptr, wptr, wptr, wptr, wptr
; ShellExecuteW hwnd, lpOperation, lpFile, lpParameters, lpDirectory, nShowCmd ; 戻り値は stat
; hwnd : HWND optional -> "wptr"
; lpOperation : LPCWSTR optional -> "wptr"
; lpFile : LPCWSTR -> "wptr"
; lpParameters : LPCWSTR optional -> "wptr"
; lpDirectory : LPCWSTR optional -> "wptr"
; nShowCmd : SHOW_WINDOW_CMD -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "SHELL32.dll"
#cfunc global ShellExecuteW "ShellExecuteW" sptr, wstr, wstr, wstr, wstr, int
; res = ShellExecuteW(hwnd, lpOperation, lpFile, lpParameters, lpDirectory, nShowCmd)
; hwnd : HWND optional -> "sptr"
; lpOperation : LPCWSTR optional -> "wstr"
; lpFile : LPCWSTR -> "wstr"
; lpParameters : LPCWSTR optional -> "wstr"
; lpDirectory : LPCWSTR optional -> "wstr"
; nShowCmd : SHOW_WINDOW_CMD -> "int"; HINSTANCE ShellExecuteW(HWND hwnd, LPCWSTR lpOperation, LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, SHOW_WINDOW_CMD nShowCmd)
#uselib "SHELL32.dll"
#cfunc global ShellExecuteW "ShellExecuteW" intptr, wstr, wstr, wstr, wstr, int
; res = ShellExecuteW(hwnd, lpOperation, lpFile, lpParameters, lpDirectory, nShowCmd)
; hwnd : HWND optional -> "intptr"
; lpOperation : LPCWSTR optional -> "wstr"
; lpFile : LPCWSTR -> "wstr"
; lpParameters : LPCWSTR optional -> "wstr"
; lpDirectory : LPCWSTR optional -> "wstr"
; nShowCmd : SHOW_WINDOW_CMD -> "int"import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
shell32 = windows.NewLazySystemDLL("SHELL32.dll")
procShellExecuteW = shell32.NewProc("ShellExecuteW")
)
// hwnd (HWND optional), lpOperation (LPCWSTR optional), lpFile (LPCWSTR), lpParameters (LPCWSTR optional), lpDirectory (LPCWSTR optional), nShowCmd (SHOW_WINDOW_CMD)
r1, _, err := procShellExecuteW.Call(
uintptr(hwnd),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpOperation))),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpFile))),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpParameters))),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpDirectory))),
uintptr(nShowCmd),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // HINSTANCEfunction ShellExecuteW(
hwnd: THandle; // HWND optional
lpOperation: PWideChar; // LPCWSTR optional
lpFile: PWideChar; // LPCWSTR
lpParameters: PWideChar; // LPCWSTR optional
lpDirectory: PWideChar; // LPCWSTR optional
nShowCmd: Integer // SHOW_WINDOW_CMD
): THandle; stdcall;
external 'SHELL32.dll' name 'ShellExecuteW';result := DllCall("SHELL32\ShellExecuteW"
, "Ptr", hwnd ; HWND optional
, "WStr", lpOperation ; LPCWSTR optional
, "WStr", lpFile ; LPCWSTR
, "WStr", lpParameters ; LPCWSTR optional
, "WStr", lpDirectory ; LPCWSTR optional
, "Int", nShowCmd ; SHOW_WINDOW_CMD
, "Ptr") ; return: HINSTANCE●ShellExecuteW(hwnd, lpOperation, lpFile, lpParameters, lpDirectory, nShowCmd) = DLL("SHELL32.dll", "void* ShellExecuteW(void*, char*, char*, char*, char*, int)")
# 呼び出し: ShellExecuteW(hwnd, lpOperation, lpFile, lpParameters, lpDirectory, nShowCmd)
# hwnd : HWND optional -> "void*"
# lpOperation : LPCWSTR optional -> "char*"
# lpFile : LPCWSTR -> "char*"
# lpParameters : LPCWSTR optional -> "char*"
# lpDirectory : LPCWSTR optional -> "char*"
# nShowCmd : SHOW_WINDOW_CMD -> "int"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。const std = @import("std");
extern "shell32" fn ShellExecuteW(
hwnd: ?*anyopaque, // HWND optional
lpOperation: [*c]const u16, // LPCWSTR optional
lpFile: [*c]const u16, // LPCWSTR
lpParameters: [*c]const u16, // LPCWSTR optional
lpDirectory: [*c]const u16, // LPCWSTR optional
nShowCmd: i32 // SHOW_WINDOW_CMD
) callconv(std.os.windows.WINAPI) ?*anyopaque;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。proc ShellExecuteW(
hwnd: pointer, # HWND optional
lpOperation: WideCString, # LPCWSTR optional
lpFile: WideCString, # LPCWSTR
lpParameters: WideCString, # LPCWSTR optional
lpDirectory: WideCString, # LPCWSTR optional
nShowCmd: int32 # SHOW_WINDOW_CMD
): pointer {.importc: "ShellExecuteW", stdcall, dynlib: "SHELL32.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。pragma(lib, "shell32");
extern(Windows)
void* ShellExecuteW(
void* hwnd, // HWND optional
const(wchar)* lpOperation, // LPCWSTR optional
const(wchar)* lpFile, // LPCWSTR
const(wchar)* lpParameters, // LPCWSTR optional
const(wchar)* lpDirectory, // LPCWSTR optional
int nShowCmd // SHOW_WINDOW_CMD
);ccall((:ShellExecuteW, "SHELL32.dll"), stdcall, Ptr{Cvoid},
(Ptr{Cvoid}, Cwstring, Cwstring, Cwstring, Cwstring, Int32),
hwnd, lpOperation, lpFile, lpParameters, lpDirectory, nShowCmd)
# hwnd : HWND optional -> Ptr{Cvoid}
# lpOperation : LPCWSTR optional -> Cwstring
# lpFile : LPCWSTR -> Cwstring
# lpParameters : LPCWSTR optional -> Cwstring
# lpDirectory : LPCWSTR optional -> Cwstring
# nShowCmd : SHOW_WINDOW_CMD -> Int32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。local ffi = require("ffi")
ffi.cdef[[
void* ShellExecuteW(
void* hwnd,
const uint16_t* lpOperation,
const uint16_t* lpFile,
const uint16_t* lpParameters,
const uint16_t* lpDirectory,
int32_t nShowCmd);
]]
local shell32 = ffi.load("shell32")
-- shell32.ShellExecuteW(hwnd, lpOperation, lpFile, lpParameters, lpDirectory, nShowCmd)
-- hwnd : HWND optional
-- lpOperation : LPCWSTR optional
-- lpFile : LPCWSTR
-- lpParameters : LPCWSTR optional
-- lpDirectory : LPCWSTR optional
-- nShowCmd : SHOW_WINDOW_CMD
-- 構造体/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 ShellExecuteW = lib.func('__stdcall', 'ShellExecuteW', 'void *', ['void *', 'str16', 'str16', 'str16', 'str16', 'int32_t']);
// ShellExecuteW(hwnd, lpOperation, lpFile, lpParameters, lpDirectory, nShowCmd)
// hwnd : HWND optional -> 'void *'
// lpOperation : LPCWSTR optional -> 'str16'
// lpFile : LPCWSTR -> 'str16'
// lpParameters : LPCWSTR optional -> 'str16'
// lpDirectory : LPCWSTR optional -> 'str16'
// nShowCmd : SHOW_WINDOW_CMD -> 'int32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("SHELL32.dll", {
ShellExecuteW: { parameters: ["pointer", "buffer", "buffer", "buffer", "buffer", "i32"], result: "pointer" },
});
// lib.symbols.ShellExecuteW(hwnd, lpOperation, lpFile, lpParameters, lpDirectory, nShowCmd)
// hwnd : HWND optional -> "pointer"
// lpOperation : LPCWSTR optional -> "buffer"
// lpFile : LPCWSTR -> "buffer"
// lpParameters : LPCWSTR optional -> "buffer"
// lpDirectory : LPCWSTR optional -> "buffer"
// nShowCmd : SHOW_WINDOW_CMD -> "i32"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
void* ShellExecuteW(
void* hwnd,
const uint16_t* lpOperation,
const uint16_t* lpFile,
const uint16_t* lpParameters,
const uint16_t* lpDirectory,
int32_t nShowCmd);
C, "SHELL32.dll");
// $ffi->ShellExecuteW(hwnd, lpOperation, lpFile, lpParameters, lpDirectory, nShowCmd);
// hwnd : HWND optional
// lpOperation : LPCWSTR optional
// lpFile : LPCWSTR
// lpParameters : LPCWSTR optional
// lpDirectory : LPCWSTR 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.UNICODE_OPTIONS);
Pointer ShellExecuteW(
Pointer hwnd, // HWND optional
WString lpOperation, // LPCWSTR optional
WString lpFile, // LPCWSTR
WString lpParameters, // LPCWSTR optional
WString lpDirectory, // LPCWSTR optional
int nShowCmd // SHOW_WINDOW_CMD
);
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。@[Link("shell32")]
lib LibSHELL32
fun ShellExecuteW = ShellExecuteW(
hwnd : Void*, # HWND optional
lpOperation : UInt16*, # LPCWSTR optional
lpFile : UInt16*, # LPCWSTR
lpParameters : UInt16*, # LPCWSTR optional
lpDirectory : UInt16*, # LPCWSTR 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 ShellExecuteWNative = Pointer<Void> Function(Pointer<Void>, Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>, Int32);
typedef ShellExecuteWDart = Pointer<Void> Function(Pointer<Void>, Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>, int);
final ShellExecuteW = DynamicLibrary.open('SHELL32.dll')
.lookupFunction<ShellExecuteWNative, ShellExecuteWDart>('ShellExecuteW');
// hwnd : HWND optional -> Pointer<Void>
// lpOperation : LPCWSTR optional -> Pointer<Utf16>
// lpFile : LPCWSTR -> Pointer<Utf16>
// lpParameters : LPCWSTR optional -> Pointer<Utf16>
// lpDirectory : LPCWSTR optional -> Pointer<Utf16>
// nShowCmd : SHOW_WINDOW_CMD -> Int32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function ShellExecuteW(
hwnd: THandle; // HWND optional
lpOperation: PWideChar; // LPCWSTR optional
lpFile: PWideChar; // LPCWSTR
lpParameters: PWideChar; // LPCWSTR optional
lpDirectory: PWideChar; // LPCWSTR optional
nShowCmd: Integer // SHOW_WINDOW_CMD
): THandle; stdcall;
external 'SHELL32.dll' name 'ShellExecuteW';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "ShellExecuteW"
c_ShellExecuteW :: Ptr () -> CWString -> CWString -> CWString -> CWString -> Int32 -> IO (Ptr ())
-- hwnd : HWND optional -> Ptr ()
-- lpOperation : LPCWSTR optional -> CWString
-- lpFile : LPCWSTR -> CWString
-- lpParameters : LPCWSTR optional -> CWString
-- lpDirectory : LPCWSTR optional -> CWString
-- nShowCmd : SHOW_WINDOW_CMD -> Int32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let shellexecutew =
foreign "ShellExecuteW"
((ptr void) @-> (ptr uint16_t) @-> (ptr uint16_t) @-> (ptr uint16_t) @-> (ptr uint16_t) @-> int32_t @-> returning (ptr void))
(* hwnd : HWND optional -> (ptr void) *)
(* lpOperation : LPCWSTR optional -> (ptr uint16_t) *)
(* lpFile : LPCWSTR -> (ptr uint16_t) *)
(* lpParameters : LPCWSTR optional -> (ptr uint16_t) *)
(* lpDirectory : LPCWSTR optional -> (ptr uint16_t) *)
(* 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 ("ShellExecuteW" shell-execute-w :convention :stdcall) :pointer
(hwnd :pointer) ; HWND optional
(lp-operation (:string :encoding :utf-16le)) ; LPCWSTR optional
(lp-file (:string :encoding :utf-16le)) ; LPCWSTR
(lp-parameters (:string :encoding :utf-16le)) ; LPCWSTR optional
(lp-directory (:string :encoding :utf-16le)) ; LPCWSTR optional
(n-show-cmd :int32)) ; SHOW_WINDOW_CMD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $ShellExecuteW = Win32::API::More->new('SHELL32',
'HANDLE ShellExecuteW(HANDLE hwnd, LPCWSTR lpOperation, LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, int nShowCmd)');
# my $ret = $ShellExecuteW->Call($hwnd, $lpOperation, $lpFile, $lpParameters, $lpDirectory, $nShowCmd);
# hwnd : HWND optional -> HANDLE
# lpOperation : LPCWSTR optional -> LPCWSTR
# lpFile : LPCWSTR -> LPCWSTR
# lpParameters : LPCWSTR optional -> LPCWSTR
# lpDirectory : LPCWSTR optional -> LPCWSTR
# nShowCmd : SHOW_WINDOW_CMD -> int
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。関連項目
- f ShellExecuteA (ANSI版) — ファイルやプログラム(ANSI)に対し操作を実行する。
- f ShellExecuteExW — 指定したファイルやプログラムをシェル経由で実行する。
- f CoInitializeEx — 指定した並行モデルでCOMライブラリを初期化する。
- f CreateProcessW — 新しいプロセスとそのプライマリスレッドを作成する(Unicode版)。