ホーム › System.SubsystemForLinux › WslLaunchInteractive
WslLaunchInteractive
関数WSLディストリビューション上で対話的にコマンドを実行する。
シグネチャ
// Api-ms-win-wsl-api-l1-1-0.dll
#include <windows.h>
HRESULT WslLaunchInteractive(
LPCWSTR distributionName,
LPCWSTR command, // optional
BOOL useCurrentWorkingDirectory,
DWORD* exitCode
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| distributionName | LPCWSTR | in | コマンドを実行する WSL ディストリビューション名(Unicode)。 |
| command | LPCWSTR | inoptional | 実行するコマンド文字列(Unicode)。NULL で既定シェルを起動。 |
| useCurrentWorkingDirectory | BOOL | in | TRUE で現在の作業ディレクトリ、FALSE でユーザーのホームから実行する。 |
| exitCode | DWORD* | out | コマンドの終了コードを受け取るポインタ。 |
戻り値の型: HRESULT
各言語での呼び出し定義
// Api-ms-win-wsl-api-l1-1-0.dll
#include <windows.h>
HRESULT WslLaunchInteractive(
LPCWSTR distributionName,
LPCWSTR command, // optional
BOOL useCurrentWorkingDirectory,
DWORD* exitCode
);[DllImport("Api-ms-win-wsl-api-l1-1-0.dll", ExactSpelling = true)]
static extern int WslLaunchInteractive(
[MarshalAs(UnmanagedType.LPWStr)] string distributionName, // LPCWSTR
[MarshalAs(UnmanagedType.LPWStr)] string command, // LPCWSTR optional
bool useCurrentWorkingDirectory, // BOOL
out uint exitCode // DWORD* out
);<DllImport("Api-ms-win-wsl-api-l1-1-0.dll", ExactSpelling:=True)>
Public Shared Function WslLaunchInteractive(
<MarshalAs(UnmanagedType.LPWStr)> distributionName As String, ' LPCWSTR
<MarshalAs(UnmanagedType.LPWStr)> command As String, ' LPCWSTR optional
useCurrentWorkingDirectory As Boolean, ' BOOL
<Out> ByRef exitCode As UInteger ' DWORD* out
) As Integer
End Function' distributionName : LPCWSTR
' command : LPCWSTR optional
' useCurrentWorkingDirectory : BOOL
' exitCode : DWORD* out
Declare PtrSafe Function WslLaunchInteractive Lib "api-ms-win-wsl-api-l1-1-0" ( _
ByVal distributionName As LongPtr, _
ByVal command As LongPtr, _
ByVal useCurrentWorkingDirectory As Long, _
ByRef exitCode As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
WslLaunchInteractive = ctypes.windll.LoadLibrary("Api-ms-win-wsl-api-l1-1-0.dll").WslLaunchInteractive
WslLaunchInteractive.restype = ctypes.c_int
WslLaunchInteractive.argtypes = [
wintypes.LPCWSTR, # distributionName : LPCWSTR
wintypes.LPCWSTR, # command : LPCWSTR optional
wintypes.BOOL, # useCurrentWorkingDirectory : BOOL
ctypes.POINTER(wintypes.DWORD), # exitCode : DWORD* out
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('Api-ms-win-wsl-api-l1-1-0.dll')
WslLaunchInteractive = Fiddle::Function.new(
lib['WslLaunchInteractive'],
[
Fiddle::TYPE_VOIDP, # distributionName : LPCWSTR
Fiddle::TYPE_VOIDP, # command : LPCWSTR optional
Fiddle::TYPE_INT, # useCurrentWorkingDirectory : BOOL
Fiddle::TYPE_VOIDP, # exitCode : DWORD* out
],
Fiddle::TYPE_INT)#[link(name = "api-ms-win-wsl-api-l1-1-0")]
extern "system" {
fn WslLaunchInteractive(
distributionName: *const u16, // LPCWSTR
command: *const u16, // LPCWSTR optional
useCurrentWorkingDirectory: i32, // BOOL
exitCode: *mut u32 // DWORD* out
) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("Api-ms-win-wsl-api-l1-1-0.dll")]
public static extern int WslLaunchInteractive([MarshalAs(UnmanagedType.LPWStr)] string distributionName, [MarshalAs(UnmanagedType.LPWStr)] string command, bool useCurrentWorkingDirectory, out uint exitCode);
"@
$api = Add-Type -MemberDefinition $sig -Name 'Api-ms-win-wsl-api-l1-1-0_WslLaunchInteractive' -Namespace Win32 -PassThru
# $api::WslLaunchInteractive(distributionName, command, useCurrentWorkingDirectory, exitCode)#uselib "Api-ms-win-wsl-api-l1-1-0.dll"
#func global WslLaunchInteractive "WslLaunchInteractive" sptr, sptr, sptr, sptr
; WslLaunchInteractive distributionName, command, useCurrentWorkingDirectory, varptr(exitCode) ; 戻り値は stat
; distributionName : LPCWSTR -> "sptr"
; command : LPCWSTR optional -> "sptr"
; useCurrentWorkingDirectory : BOOL -> "sptr"
; exitCode : DWORD* out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "Api-ms-win-wsl-api-l1-1-0.dll" #cfunc global WslLaunchInteractive "WslLaunchInteractive" wstr, wstr, int, var ; res = WslLaunchInteractive(distributionName, command, useCurrentWorkingDirectory, exitCode) ; distributionName : LPCWSTR -> "wstr" ; command : LPCWSTR optional -> "wstr" ; useCurrentWorkingDirectory : BOOL -> "int" ; exitCode : DWORD* out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "Api-ms-win-wsl-api-l1-1-0.dll" #cfunc global WslLaunchInteractive "WslLaunchInteractive" wstr, wstr, int, sptr ; res = WslLaunchInteractive(distributionName, command, useCurrentWorkingDirectory, varptr(exitCode)) ; distributionName : LPCWSTR -> "wstr" ; command : LPCWSTR optional -> "wstr" ; useCurrentWorkingDirectory : BOOL -> "int" ; exitCode : DWORD* out -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; HRESULT WslLaunchInteractive(LPCWSTR distributionName, LPCWSTR command, BOOL useCurrentWorkingDirectory, DWORD* exitCode) #uselib "Api-ms-win-wsl-api-l1-1-0.dll" #cfunc global WslLaunchInteractive "WslLaunchInteractive" wstr, wstr, int, var ; res = WslLaunchInteractive(distributionName, command, useCurrentWorkingDirectory, exitCode) ; distributionName : LPCWSTR -> "wstr" ; command : LPCWSTR optional -> "wstr" ; useCurrentWorkingDirectory : BOOL -> "int" ; exitCode : DWORD* out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; HRESULT WslLaunchInteractive(LPCWSTR distributionName, LPCWSTR command, BOOL useCurrentWorkingDirectory, DWORD* exitCode) #uselib "Api-ms-win-wsl-api-l1-1-0.dll" #cfunc global WslLaunchInteractive "WslLaunchInteractive" wstr, wstr, int, intptr ; res = WslLaunchInteractive(distributionName, command, useCurrentWorkingDirectory, varptr(exitCode)) ; distributionName : LPCWSTR -> "wstr" ; command : LPCWSTR optional -> "wstr" ; useCurrentWorkingDirectory : BOOL -> "int" ; exitCode : DWORD* out -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
api_ms_win_wsl_api_l1_1_0 = windows.NewLazySystemDLL("Api-ms-win-wsl-api-l1-1-0.dll")
procWslLaunchInteractive = api_ms_win_wsl_api_l1_1_0.NewProc("WslLaunchInteractive")
)
// distributionName (LPCWSTR), command (LPCWSTR optional), useCurrentWorkingDirectory (BOOL), exitCode (DWORD* out)
r1, _, err := procWslLaunchInteractive.Call(
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(distributionName))),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(command))),
uintptr(useCurrentWorkingDirectory),
uintptr(exitCode),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // HRESULTfunction WslLaunchInteractive(
distributionName: PWideChar; // LPCWSTR
command: PWideChar; // LPCWSTR optional
useCurrentWorkingDirectory: BOOL; // BOOL
exitCode: Pointer // DWORD* out
): Integer; stdcall;
external 'Api-ms-win-wsl-api-l1-1-0.dll' name 'WslLaunchInteractive';result := DllCall("Api-ms-win-wsl-api-l1-1-0\WslLaunchInteractive"
, "WStr", distributionName ; LPCWSTR
, "WStr", command ; LPCWSTR optional
, "Int", useCurrentWorkingDirectory ; BOOL
, "Ptr", exitCode ; DWORD* out
, "Int") ; return: HRESULT●WslLaunchInteractive(distributionName, command, useCurrentWorkingDirectory, exitCode) = DLL("Api-ms-win-wsl-api-l1-1-0.dll", "int WslLaunchInteractive(char*, char*, bool, void*)")
# 呼び出し: WslLaunchInteractive(distributionName, command, useCurrentWorkingDirectory, exitCode)
# distributionName : LPCWSTR -> "char*"
# command : LPCWSTR optional -> "char*"
# useCurrentWorkingDirectory : BOOL -> "bool"
# exitCode : DWORD* out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "api-ms-win-wsl-api-l1-1-0" fn WslLaunchInteractive(
distributionName: [*c]const u16, // LPCWSTR
command: [*c]const u16, // LPCWSTR optional
useCurrentWorkingDirectory: i32, // BOOL
exitCode: [*c]u32 // DWORD* out
) callconv(std.os.windows.WINAPI) i32;proc WslLaunchInteractive(
distributionName: WideCString, # LPCWSTR
command: WideCString, # LPCWSTR optional
useCurrentWorkingDirectory: int32, # BOOL
exitCode: ptr uint32 # DWORD* out
): int32 {.importc: "WslLaunchInteractive", stdcall, dynlib: "Api-ms-win-wsl-api-l1-1-0.dll".}pragma(lib, "api-ms-win-wsl-api-l1-1-0");
extern(Windows)
int WslLaunchInteractive(
const(wchar)* distributionName, // LPCWSTR
const(wchar)* command, // LPCWSTR optional
int useCurrentWorkingDirectory, // BOOL
uint* exitCode // DWORD* out
);ccall((:WslLaunchInteractive, "Api-ms-win-wsl-api-l1-1-0.dll"), stdcall, Int32,
(Cwstring, Cwstring, Int32, Ptr{UInt32}),
distributionName, command, useCurrentWorkingDirectory, exitCode)
# distributionName : LPCWSTR -> Cwstring
# command : LPCWSTR optional -> Cwstring
# useCurrentWorkingDirectory : BOOL -> Int32
# exitCode : DWORD* out -> Ptr{UInt32}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int32_t WslLaunchInteractive(
const uint16_t* distributionName,
const uint16_t* command,
int32_t useCurrentWorkingDirectory,
uint32_t* exitCode);
]]
local api-ms-win-wsl-api-l1-1-0 = ffi.load("api-ms-win-wsl-api-l1-1-0")
-- api-ms-win-wsl-api-l1-1-0.WslLaunchInteractive(distributionName, command, useCurrentWorkingDirectory, exitCode)
-- distributionName : LPCWSTR
-- command : LPCWSTR optional
-- useCurrentWorkingDirectory : BOOL
-- exitCode : DWORD* out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('Api-ms-win-wsl-api-l1-1-0.dll');
const WslLaunchInteractive = lib.func('__stdcall', 'WslLaunchInteractive', 'int32_t', ['str16', 'str16', 'int32_t', 'uint32_t *']);
// WslLaunchInteractive(distributionName, command, useCurrentWorkingDirectory, exitCode)
// distributionName : LPCWSTR -> 'str16'
// command : LPCWSTR optional -> 'str16'
// useCurrentWorkingDirectory : BOOL -> 'int32_t'
// exitCode : DWORD* out -> 'uint32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("Api-ms-win-wsl-api-l1-1-0.dll", {
WslLaunchInteractive: { parameters: ["buffer", "buffer", "i32", "pointer"], result: "i32" },
});
// lib.symbols.WslLaunchInteractive(distributionName, command, useCurrentWorkingDirectory, exitCode)
// distributionName : LPCWSTR -> "buffer"
// command : LPCWSTR optional -> "buffer"
// useCurrentWorkingDirectory : BOOL -> "i32"
// exitCode : DWORD* out -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t WslLaunchInteractive(
const uint16_t* distributionName,
const uint16_t* command,
int32_t useCurrentWorkingDirectory,
uint32_t* exitCode);
C, "Api-ms-win-wsl-api-l1-1-0.dll");
// $ffi->WslLaunchInteractive(distributionName, command, useCurrentWorkingDirectory, exitCode);
// distributionName : LPCWSTR
// command : LPCWSTR optional
// useCurrentWorkingDirectory : BOOL
// exitCode : DWORD* 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 Api-ms-win-wsl-api-l1-1-0 extends StdCallLibrary {
Api-ms-win-wsl-api-l1-1-0 INSTANCE = Native.load("api-ms-win-wsl-api-l1-1-0", Api-ms-win-wsl-api-l1-1-0.class);
int WslLaunchInteractive(
WString distributionName, // LPCWSTR
WString command, // LPCWSTR optional
boolean useCurrentWorkingDirectory, // BOOL
IntByReference exitCode // DWORD* out
);
}@[Link("api-ms-win-wsl-api-l1-1-0")]
lib LibApi-ms-win-wsl-api-l1-1-0
fun WslLaunchInteractive = WslLaunchInteractive(
distributionName : UInt16*, # LPCWSTR
command : UInt16*, # LPCWSTR optional
useCurrentWorkingDirectory : Int32, # BOOL
exitCode : UInt32* # DWORD* out
) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef WslLaunchInteractiveNative = Int32 Function(Pointer<Utf16>, Pointer<Utf16>, Int32, Pointer<Uint32>);
typedef WslLaunchInteractiveDart = int Function(Pointer<Utf16>, Pointer<Utf16>, int, Pointer<Uint32>);
final WslLaunchInteractive = DynamicLibrary.open('Api-ms-win-wsl-api-l1-1-0.dll')
.lookupFunction<WslLaunchInteractiveNative, WslLaunchInteractiveDart>('WslLaunchInteractive');
// distributionName : LPCWSTR -> Pointer<Utf16>
// command : LPCWSTR optional -> Pointer<Utf16>
// useCurrentWorkingDirectory : BOOL -> Int32
// exitCode : DWORD* out -> Pointer<Uint32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function WslLaunchInteractive(
distributionName: PWideChar; // LPCWSTR
command: PWideChar; // LPCWSTR optional
useCurrentWorkingDirectory: BOOL; // BOOL
exitCode: Pointer // DWORD* out
): Integer; stdcall;
external 'Api-ms-win-wsl-api-l1-1-0.dll' name 'WslLaunchInteractive';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "WslLaunchInteractive"
c_WslLaunchInteractive :: CWString -> CWString -> CInt -> Ptr Word32 -> IO Int32
-- distributionName : LPCWSTR -> CWString
-- command : LPCWSTR optional -> CWString
-- useCurrentWorkingDirectory : BOOL -> CInt
-- exitCode : DWORD* out -> Ptr Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let wsllaunchinteractive =
foreign "WslLaunchInteractive"
((ptr uint16_t) @-> (ptr uint16_t) @-> int32_t @-> (ptr uint32_t) @-> returning int32_t)
(* distributionName : LPCWSTR -> (ptr uint16_t) *)
(* command : LPCWSTR optional -> (ptr uint16_t) *)
(* useCurrentWorkingDirectory : BOOL -> int32_t *)
(* exitCode : DWORD* out -> (ptr uint32_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library api-ms-win-wsl-api-l1-1-0 (t "Api-ms-win-wsl-api-l1-1-0.dll"))
(cffi:use-foreign-library api-ms-win-wsl-api-l1-1-0)
(cffi:defcfun ("WslLaunchInteractive" wsl-launch-interactive :convention :stdcall) :int32
(distribution-name (:string :encoding :utf-16le)) ; LPCWSTR
(command (:string :encoding :utf-16le)) ; LPCWSTR optional
(use-current-working-directory :int32) ; BOOL
(exit-code :pointer)) ; DWORD* out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $WslLaunchInteractive = Win32::API::More->new('Api-ms-win-wsl-api-l1-1-0',
'int WslLaunchInteractive(LPCWSTR distributionName, LPCWSTR command, BOOL useCurrentWorkingDirectory, LPVOID exitCode)');
# my $ret = $WslLaunchInteractive->Call($distributionName, $command, $useCurrentWorkingDirectory, $exitCode);
# distributionName : LPCWSTR -> LPCWSTR
# command : LPCWSTR optional -> LPCWSTR
# useCurrentWorkingDirectory : BOOL -> BOOL
# exitCode : DWORD* out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。