ホーム › System.RemoteManagement › WSManRunShellCommandEx
WSManRunShellCommandEx
関数コマンドIDを指定してリモートコマンドを実行する。
シグネチャ
// WsmSvc.dll
#include <windows.h>
void WSManRunShellCommandEx(
WSMAN_SHELL_HANDLE shell,
DWORD flags,
LPCWSTR commandId,
LPCWSTR commandLine,
WSMAN_COMMAND_ARG_SET* args, // optional
WSMAN_OPTION_SET* options, // optional
WSMAN_SHELL_ASYNC* async,
WSMAN_COMMAND_HANDLE* command
);パラメーター
| 名前 | 型 | 方向 |
|---|---|---|
| shell | WSMAN_SHELL_HANDLE | in |
| flags | DWORD | in |
| commandId | LPCWSTR | in |
| commandLine | LPCWSTR | in |
| args | WSMAN_COMMAND_ARG_SET* | inoptional |
| options | WSMAN_OPTION_SET* | inoptional |
| async | WSMAN_SHELL_ASYNC* | in |
| command | WSMAN_COMMAND_HANDLE* | out |
戻り値の型: void
各言語での呼び出し定義
// WsmSvc.dll
#include <windows.h>
void WSManRunShellCommandEx(
WSMAN_SHELL_HANDLE shell,
DWORD flags,
LPCWSTR commandId,
LPCWSTR commandLine,
WSMAN_COMMAND_ARG_SET* args, // optional
WSMAN_OPTION_SET* options, // optional
WSMAN_SHELL_ASYNC* async,
WSMAN_COMMAND_HANDLE* command
);[DllImport("WsmSvc.dll", ExactSpelling = true)]
static extern void WSManRunShellCommandEx(
IntPtr shell, // WSMAN_SHELL_HANDLE
uint flags, // DWORD
[MarshalAs(UnmanagedType.LPWStr)] string commandId, // LPCWSTR
[MarshalAs(UnmanagedType.LPWStr)] string commandLine, // LPCWSTR
IntPtr args, // WSMAN_COMMAND_ARG_SET* optional
IntPtr options, // WSMAN_OPTION_SET* optional
IntPtr async, // WSMAN_SHELL_ASYNC*
out IntPtr command // WSMAN_COMMAND_HANDLE* out
);<DllImport("WsmSvc.dll", ExactSpelling:=True)>
Public Shared Sub WSManRunShellCommandEx(
shell As IntPtr, ' WSMAN_SHELL_HANDLE
flags As UInteger, ' DWORD
<MarshalAs(UnmanagedType.LPWStr)> commandId As String, ' LPCWSTR
<MarshalAs(UnmanagedType.LPWStr)> commandLine As String, ' LPCWSTR
args As IntPtr, ' WSMAN_COMMAND_ARG_SET* optional
options As IntPtr, ' WSMAN_OPTION_SET* optional
async As IntPtr, ' WSMAN_SHELL_ASYNC*
<Out> ByRef command As IntPtr ' WSMAN_COMMAND_HANDLE* out
)
End Sub' shell : WSMAN_SHELL_HANDLE
' flags : DWORD
' commandId : LPCWSTR
' commandLine : LPCWSTR
' args : WSMAN_COMMAND_ARG_SET* optional
' options : WSMAN_OPTION_SET* optional
' async : WSMAN_SHELL_ASYNC*
' command : WSMAN_COMMAND_HANDLE* out
Declare PtrSafe Sub WSManRunShellCommandEx Lib "wsmsvc" ( _
ByVal shell As LongPtr, _
ByVal flags As Long, _
ByVal commandId As LongPtr, _
ByVal commandLine As LongPtr, _
ByVal args As LongPtr, _
ByVal options As LongPtr, _
ByVal async As LongPtr, _
ByRef command As LongPtr)
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
WSManRunShellCommandEx = ctypes.windll.wsmsvc.WSManRunShellCommandEx
WSManRunShellCommandEx.restype = None
WSManRunShellCommandEx.argtypes = [
ctypes.c_ssize_t, # shell : WSMAN_SHELL_HANDLE
wintypes.DWORD, # flags : DWORD
wintypes.LPCWSTR, # commandId : LPCWSTR
wintypes.LPCWSTR, # commandLine : LPCWSTR
ctypes.c_void_p, # args : WSMAN_COMMAND_ARG_SET* optional
ctypes.c_void_p, # options : WSMAN_OPTION_SET* optional
ctypes.c_void_p, # async : WSMAN_SHELL_ASYNC*
ctypes.c_void_p, # command : WSMAN_COMMAND_HANDLE* out
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('WsmSvc.dll')
WSManRunShellCommandEx = Fiddle::Function.new(
lib['WSManRunShellCommandEx'],
[
Fiddle::TYPE_INTPTR_T, # shell : WSMAN_SHELL_HANDLE
-Fiddle::TYPE_INT, # flags : DWORD
Fiddle::TYPE_VOIDP, # commandId : LPCWSTR
Fiddle::TYPE_VOIDP, # commandLine : LPCWSTR
Fiddle::TYPE_VOIDP, # args : WSMAN_COMMAND_ARG_SET* optional
Fiddle::TYPE_VOIDP, # options : WSMAN_OPTION_SET* optional
Fiddle::TYPE_VOIDP, # async : WSMAN_SHELL_ASYNC*
Fiddle::TYPE_VOIDP, # command : WSMAN_COMMAND_HANDLE* out
],
Fiddle::TYPE_VOID)#[link(name = "wsmsvc")]
extern "system" {
fn WSManRunShellCommandEx(
shell: isize, // WSMAN_SHELL_HANDLE
flags: u32, // DWORD
commandId: *const u16, // LPCWSTR
commandLine: *const u16, // LPCWSTR
args: *mut WSMAN_COMMAND_ARG_SET, // WSMAN_COMMAND_ARG_SET* optional
options: *mut WSMAN_OPTION_SET, // WSMAN_OPTION_SET* optional
async: *mut WSMAN_SHELL_ASYNC, // WSMAN_SHELL_ASYNC*
command: *mut isize // WSMAN_COMMAND_HANDLE* out
);
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("WsmSvc.dll")]
public static extern void WSManRunShellCommandEx(IntPtr shell, uint flags, [MarshalAs(UnmanagedType.LPWStr)] string commandId, [MarshalAs(UnmanagedType.LPWStr)] string commandLine, IntPtr args, IntPtr options, IntPtr async, out IntPtr command);
"@
$api = Add-Type -MemberDefinition $sig -Name 'WsmSvc_WSManRunShellCommandEx' -Namespace Win32 -PassThru
# $api::WSManRunShellCommandEx(shell, flags, commandId, commandLine, args, options, async, command)#uselib "WsmSvc.dll"
#func global WSManRunShellCommandEx "WSManRunShellCommandEx" sptr, sptr, sptr, sptr, sptr, sptr, sptr, sptr
; WSManRunShellCommandEx shell, flags, commandId, commandLine, varptr(args), varptr(options), varptr(async), command
; shell : WSMAN_SHELL_HANDLE -> "sptr"
; flags : DWORD -> "sptr"
; commandId : LPCWSTR -> "sptr"
; commandLine : LPCWSTR -> "sptr"
; args : WSMAN_COMMAND_ARG_SET* optional -> "sptr"
; options : WSMAN_OPTION_SET* optional -> "sptr"
; async : WSMAN_SHELL_ASYNC* -> "sptr"
; command : WSMAN_COMMAND_HANDLE* out -> "sptr"出力引数:
#uselib "WsmSvc.dll" #func global WSManRunShellCommandEx "WSManRunShellCommandEx" sptr, int, wstr, wstr, var, var, var, int ; WSManRunShellCommandEx shell, flags, commandId, commandLine, args, options, async, command ; shell : WSMAN_SHELL_HANDLE -> "sptr" ; flags : DWORD -> "int" ; commandId : LPCWSTR -> "wstr" ; commandLine : LPCWSTR -> "wstr" ; args : WSMAN_COMMAND_ARG_SET* optional -> "var" ; options : WSMAN_OPTION_SET* optional -> "var" ; async : WSMAN_SHELL_ASYNC* -> "var" ; command : WSMAN_COMMAND_HANDLE* out -> "int" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "WsmSvc.dll" #func global WSManRunShellCommandEx "WSManRunShellCommandEx" sptr, int, wstr, wstr, sptr, sptr, sptr, int ; WSManRunShellCommandEx shell, flags, commandId, commandLine, varptr(args), varptr(options), varptr(async), command ; shell : WSMAN_SHELL_HANDLE -> "sptr" ; flags : DWORD -> "int" ; commandId : LPCWSTR -> "wstr" ; commandLine : LPCWSTR -> "wstr" ; args : WSMAN_COMMAND_ARG_SET* optional -> "sptr" ; options : WSMAN_OPTION_SET* optional -> "sptr" ; async : WSMAN_SHELL_ASYNC* -> "sptr" ; command : WSMAN_COMMAND_HANDLE* out -> "int" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; void WSManRunShellCommandEx(WSMAN_SHELL_HANDLE shell, DWORD flags, LPCWSTR commandId, LPCWSTR commandLine, WSMAN_COMMAND_ARG_SET* args, WSMAN_OPTION_SET* options, WSMAN_SHELL_ASYNC* async, WSMAN_COMMAND_HANDLE* command) #uselib "WsmSvc.dll" #func global WSManRunShellCommandEx "WSManRunShellCommandEx" intptr, int, wstr, wstr, var, var, var, int ; WSManRunShellCommandEx shell, flags, commandId, commandLine, args, options, async, command ; shell : WSMAN_SHELL_HANDLE -> "intptr" ; flags : DWORD -> "int" ; commandId : LPCWSTR -> "wstr" ; commandLine : LPCWSTR -> "wstr" ; args : WSMAN_COMMAND_ARG_SET* optional -> "var" ; options : WSMAN_OPTION_SET* optional -> "var" ; async : WSMAN_SHELL_ASYNC* -> "var" ; command : WSMAN_COMMAND_HANDLE* out -> "int" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; void WSManRunShellCommandEx(WSMAN_SHELL_HANDLE shell, DWORD flags, LPCWSTR commandId, LPCWSTR commandLine, WSMAN_COMMAND_ARG_SET* args, WSMAN_OPTION_SET* options, WSMAN_SHELL_ASYNC* async, WSMAN_COMMAND_HANDLE* command) #uselib "WsmSvc.dll" #func global WSManRunShellCommandEx "WSManRunShellCommandEx" intptr, int, wstr, wstr, intptr, intptr, intptr, int ; WSManRunShellCommandEx shell, flags, commandId, commandLine, varptr(args), varptr(options), varptr(async), command ; shell : WSMAN_SHELL_HANDLE -> "intptr" ; flags : DWORD -> "int" ; commandId : LPCWSTR -> "wstr" ; commandLine : LPCWSTR -> "wstr" ; args : WSMAN_COMMAND_ARG_SET* optional -> "intptr" ; options : WSMAN_OPTION_SET* optional -> "intptr" ; async : WSMAN_SHELL_ASYNC* -> "intptr" ; command : WSMAN_COMMAND_HANDLE* out -> "int" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
wsmsvc = windows.NewLazySystemDLL("WsmSvc.dll")
procWSManRunShellCommandEx = wsmsvc.NewProc("WSManRunShellCommandEx")
)
// shell (WSMAN_SHELL_HANDLE), flags (DWORD), commandId (LPCWSTR), commandLine (LPCWSTR), args (WSMAN_COMMAND_ARG_SET* optional), options (WSMAN_OPTION_SET* optional), async (WSMAN_SHELL_ASYNC*), command (WSMAN_COMMAND_HANDLE* out)
r1, _, err := procWSManRunShellCommandEx.Call(
uintptr(shell),
uintptr(flags),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(commandId))),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(commandLine))),
uintptr(args),
uintptr(options),
uintptr(async),
uintptr(command),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // voidprocedure WSManRunShellCommandEx(
shell: NativeInt; // WSMAN_SHELL_HANDLE
flags: DWORD; // DWORD
commandId: PWideChar; // LPCWSTR
commandLine: PWideChar; // LPCWSTR
args: Pointer; // WSMAN_COMMAND_ARG_SET* optional
options: Pointer; // WSMAN_OPTION_SET* optional
async: Pointer; // WSMAN_SHELL_ASYNC*
command: Pointer // WSMAN_COMMAND_HANDLE* out
); stdcall;
external 'WsmSvc.dll' name 'WSManRunShellCommandEx';result := DllCall("WsmSvc\WSManRunShellCommandEx"
, "Ptr", shell ; WSMAN_SHELL_HANDLE
, "UInt", flags ; DWORD
, "WStr", commandId ; LPCWSTR
, "WStr", commandLine ; LPCWSTR
, "Ptr", args ; WSMAN_COMMAND_ARG_SET* optional
, "Ptr", options ; WSMAN_OPTION_SET* optional
, "Ptr", async ; WSMAN_SHELL_ASYNC*
, "Ptr", command ; WSMAN_COMMAND_HANDLE* out
, "Int") ; return: void●WSManRunShellCommandEx(shell, flags, commandId, commandLine, args, options, async, command) = DLL("WsmSvc.dll", "int WSManRunShellCommandEx(int, dword, char*, char*, void*, void*, void*, void*)")
# 呼び出し: WSManRunShellCommandEx(shell, flags, commandId, commandLine, args, options, async, command)
# shell : WSMAN_SHELL_HANDLE -> "int"
# flags : DWORD -> "dword"
# commandId : LPCWSTR -> "char*"
# commandLine : LPCWSTR -> "char*"
# args : WSMAN_COMMAND_ARG_SET* optional -> "void*"
# options : WSMAN_OPTION_SET* optional -> "void*"
# async : WSMAN_SHELL_ASYNC* -> "void*"
# command : WSMAN_COMMAND_HANDLE* out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。