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

ShellExecuteW

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

シグネチャ

// 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
);

パラメーター

名前方向
hwndHWNDinoptional
lpOperationLPCWSTRinoptional
lpFileLPCWSTRin
lpParametersLPCWSTRinoptional
lpDirectoryLPCWSTRinoptional
nShowCmdSHOW_WINDOW_CMDin

戻り値の型: HINSTANCE

各言語での呼び出し定義

// 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   // HINSTANCE
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';
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 版の利用を推奨。