ホーム › NetworkManagement.NetManagement › NetServiceControl
NetServiceControl
関数ネットワークサービスに制御コードを送信する。
シグネチャ
// NETAPI32.dll
#include <windows.h>
DWORD NetServiceControl(
LPCWSTR servername, // optional
LPCWSTR service,
DWORD opcode,
DWORD arg,
BYTE** bufptr
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| servername | LPCWSTR | inoptional | 操作対象サーバー名。NULLでローカルコンピュータを指す。 |
| service | LPCWSTR | in | 制御するネットワークサービス名。 |
| opcode | DWORD | in | 実行する制御操作コード(SERVICE_CTRL_PAUSE/CONTINUE等)。 |
| arg | DWORD | in | 制御操作に渡す追加引数。 |
| bufptr | BYTE** | out | サービス状態情報バッファへのポインタを受け取る出力引数。NetApiBufferFreeで解放する。 |
戻り値の型: DWORD
各言語での呼び出し定義
// NETAPI32.dll
#include <windows.h>
DWORD NetServiceControl(
LPCWSTR servername, // optional
LPCWSTR service,
DWORD opcode,
DWORD arg,
BYTE** bufptr
);[DllImport("NETAPI32.dll", ExactSpelling = true)]
static extern uint NetServiceControl(
[MarshalAs(UnmanagedType.LPWStr)] string servername, // LPCWSTR optional
[MarshalAs(UnmanagedType.LPWStr)] string service, // LPCWSTR
uint opcode, // DWORD
uint arg, // DWORD
IntPtr bufptr // BYTE** out
);<DllImport("NETAPI32.dll", ExactSpelling:=True)>
Public Shared Function NetServiceControl(
<MarshalAs(UnmanagedType.LPWStr)> servername As String, ' LPCWSTR optional
<MarshalAs(UnmanagedType.LPWStr)> service As String, ' LPCWSTR
opcode As UInteger, ' DWORD
arg As UInteger, ' DWORD
bufptr As IntPtr ' BYTE** out
) As UInteger
End Function' servername : LPCWSTR optional
' service : LPCWSTR
' opcode : DWORD
' arg : DWORD
' bufptr : BYTE** out
Declare PtrSafe Function NetServiceControl Lib "netapi32" ( _
ByVal servername As LongPtr, _
ByVal service As LongPtr, _
ByVal opcode As Long, _
ByVal arg As Long, _
ByVal bufptr As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
NetServiceControl = ctypes.windll.netapi32.NetServiceControl
NetServiceControl.restype = wintypes.DWORD
NetServiceControl.argtypes = [
wintypes.LPCWSTR, # servername : LPCWSTR optional
wintypes.LPCWSTR, # service : LPCWSTR
wintypes.DWORD, # opcode : DWORD
wintypes.DWORD, # arg : DWORD
ctypes.c_void_p, # bufptr : BYTE** out
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('NETAPI32.dll')
NetServiceControl = Fiddle::Function.new(
lib['NetServiceControl'],
[
Fiddle::TYPE_VOIDP, # servername : LPCWSTR optional
Fiddle::TYPE_VOIDP, # service : LPCWSTR
-Fiddle::TYPE_INT, # opcode : DWORD
-Fiddle::TYPE_INT, # arg : DWORD
Fiddle::TYPE_VOIDP, # bufptr : BYTE** out
],
-Fiddle::TYPE_INT)#[link(name = "netapi32")]
extern "system" {
fn NetServiceControl(
servername: *const u16, // LPCWSTR optional
service: *const u16, // LPCWSTR
opcode: u32, // DWORD
arg: u32, // DWORD
bufptr: *mut *mut u8 // BYTE** out
) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("NETAPI32.dll")]
public static extern uint NetServiceControl([MarshalAs(UnmanagedType.LPWStr)] string servername, [MarshalAs(UnmanagedType.LPWStr)] string service, uint opcode, uint arg, IntPtr bufptr);
"@
$api = Add-Type -MemberDefinition $sig -Name 'NETAPI32_NetServiceControl' -Namespace Win32 -PassThru
# $api::NetServiceControl(servername, service, opcode, arg, bufptr)#uselib "NETAPI32.dll"
#func global NetServiceControl "NetServiceControl" sptr, sptr, sptr, sptr, sptr
; NetServiceControl servername, service, opcode, arg, varptr(bufptr) ; 戻り値は stat
; servername : LPCWSTR optional -> "sptr"
; service : LPCWSTR -> "sptr"
; opcode : DWORD -> "sptr"
; arg : DWORD -> "sptr"
; bufptr : BYTE** out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "NETAPI32.dll" #cfunc global NetServiceControl "NetServiceControl" wstr, wstr, int, int, var ; res = NetServiceControl(servername, service, opcode, arg, bufptr) ; servername : LPCWSTR optional -> "wstr" ; service : LPCWSTR -> "wstr" ; opcode : DWORD -> "int" ; arg : DWORD -> "int" ; bufptr : BYTE** out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "NETAPI32.dll" #cfunc global NetServiceControl "NetServiceControl" wstr, wstr, int, int, sptr ; res = NetServiceControl(servername, service, opcode, arg, varptr(bufptr)) ; servername : LPCWSTR optional -> "wstr" ; service : LPCWSTR -> "wstr" ; opcode : DWORD -> "int" ; arg : DWORD -> "int" ; bufptr : BYTE** out -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; DWORD NetServiceControl(LPCWSTR servername, LPCWSTR service, DWORD opcode, DWORD arg, BYTE** bufptr) #uselib "NETAPI32.dll" #cfunc global NetServiceControl "NetServiceControl" wstr, wstr, int, int, var ; res = NetServiceControl(servername, service, opcode, arg, bufptr) ; servername : LPCWSTR optional -> "wstr" ; service : LPCWSTR -> "wstr" ; opcode : DWORD -> "int" ; arg : DWORD -> "int" ; bufptr : BYTE** out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; DWORD NetServiceControl(LPCWSTR servername, LPCWSTR service, DWORD opcode, DWORD arg, BYTE** bufptr) #uselib "NETAPI32.dll" #cfunc global NetServiceControl "NetServiceControl" wstr, wstr, int, int, intptr ; res = NetServiceControl(servername, service, opcode, arg, varptr(bufptr)) ; servername : LPCWSTR optional -> "wstr" ; service : LPCWSTR -> "wstr" ; opcode : DWORD -> "int" ; arg : DWORD -> "int" ; bufptr : BYTE** out -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
netapi32 = windows.NewLazySystemDLL("NETAPI32.dll")
procNetServiceControl = netapi32.NewProc("NetServiceControl")
)
// servername (LPCWSTR optional), service (LPCWSTR), opcode (DWORD), arg (DWORD), bufptr (BYTE** out)
r1, _, err := procNetServiceControl.Call(
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(servername))),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(service))),
uintptr(opcode),
uintptr(arg),
uintptr(bufptr),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // DWORDfunction NetServiceControl(
servername: PWideChar; // LPCWSTR optional
service: PWideChar; // LPCWSTR
opcode: DWORD; // DWORD
arg: DWORD; // DWORD
bufptr: Pointer // BYTE** out
): DWORD; stdcall;
external 'NETAPI32.dll' name 'NetServiceControl';result := DllCall("NETAPI32\NetServiceControl"
, "WStr", servername ; LPCWSTR optional
, "WStr", service ; LPCWSTR
, "UInt", opcode ; DWORD
, "UInt", arg ; DWORD
, "Ptr", bufptr ; BYTE** out
, "UInt") ; return: DWORD●NetServiceControl(servername, service, opcode, arg, bufptr) = DLL("NETAPI32.dll", "dword NetServiceControl(char*, char*, dword, dword, void*)")
# 呼び出し: NetServiceControl(servername, service, opcode, arg, bufptr)
# servername : LPCWSTR optional -> "char*"
# service : LPCWSTR -> "char*"
# opcode : DWORD -> "dword"
# arg : DWORD -> "dword"
# bufptr : BYTE** out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "netapi32" fn NetServiceControl(
servername: [*c]const u16, // LPCWSTR optional
service: [*c]const u16, // LPCWSTR
opcode: u32, // DWORD
arg: u32, // DWORD
bufptr: [*c][*c]u8 // BYTE** out
) callconv(std.os.windows.WINAPI) u32;proc NetServiceControl(
servername: WideCString, # LPCWSTR optional
service: WideCString, # LPCWSTR
opcode: uint32, # DWORD
arg: uint32, # DWORD
bufptr: ptr uint8 # BYTE** out
): uint32 {.importc: "NetServiceControl", stdcall, dynlib: "NETAPI32.dll".}pragma(lib, "netapi32");
extern(Windows)
uint NetServiceControl(
const(wchar)* servername, // LPCWSTR optional
const(wchar)* service, // LPCWSTR
uint opcode, // DWORD
uint arg, // DWORD
ubyte** bufptr // BYTE** out
);ccall((:NetServiceControl, "NETAPI32.dll"), stdcall, UInt32,
(Cwstring, Cwstring, UInt32, UInt32, Ptr{UInt8}),
servername, service, opcode, arg, bufptr)
# servername : LPCWSTR optional -> Cwstring
# service : LPCWSTR -> Cwstring
# opcode : DWORD -> UInt32
# arg : DWORD -> UInt32
# bufptr : BYTE** out -> Ptr{UInt8}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
uint32_t NetServiceControl(
const uint16_t* servername,
const uint16_t* service,
uint32_t opcode,
uint32_t arg,
uint8_t** bufptr);
]]
local netapi32 = ffi.load("netapi32")
-- netapi32.NetServiceControl(servername, service, opcode, arg, bufptr)
-- servername : LPCWSTR optional
-- service : LPCWSTR
-- opcode : DWORD
-- arg : DWORD
-- bufptr : BYTE** out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('NETAPI32.dll');
const NetServiceControl = lib.func('__stdcall', 'NetServiceControl', 'uint32_t', ['str16', 'str16', 'uint32_t', 'uint32_t', 'uint8_t *']);
// NetServiceControl(servername, service, opcode, arg, bufptr)
// servername : LPCWSTR optional -> 'str16'
// service : LPCWSTR -> 'str16'
// opcode : DWORD -> 'uint32_t'
// arg : DWORD -> 'uint32_t'
// bufptr : BYTE** out -> 'uint8_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("NETAPI32.dll", {
NetServiceControl: { parameters: ["buffer", "buffer", "u32", "u32", "pointer"], result: "u32" },
});
// lib.symbols.NetServiceControl(servername, service, opcode, arg, bufptr)
// servername : LPCWSTR optional -> "buffer"
// service : LPCWSTR -> "buffer"
// opcode : DWORD -> "u32"
// arg : DWORD -> "u32"
// bufptr : BYTE** out -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
uint32_t NetServiceControl(
const uint16_t* servername,
const uint16_t* service,
uint32_t opcode,
uint32_t arg,
uint8_t** bufptr);
C, "NETAPI32.dll");
// $ffi->NetServiceControl(servername, service, opcode, arg, bufptr);
// servername : LPCWSTR optional
// service : LPCWSTR
// opcode : DWORD
// arg : DWORD
// bufptr : BYTE** 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 Netapi32 extends StdCallLibrary {
Netapi32 INSTANCE = Native.load("netapi32", Netapi32.class);
int NetServiceControl(
WString servername, // LPCWSTR optional
WString service, // LPCWSTR
int opcode, // DWORD
int arg, // DWORD
Pointer bufptr // BYTE** out
);
}@[Link("netapi32")]
lib LibNETAPI32
fun NetServiceControl = NetServiceControl(
servername : UInt16*, # LPCWSTR optional
service : UInt16*, # LPCWSTR
opcode : UInt32, # DWORD
arg : UInt32, # DWORD
bufptr : UInt8** # BYTE** out
) : UInt32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef NetServiceControlNative = Uint32 Function(Pointer<Utf16>, Pointer<Utf16>, Uint32, Uint32, Pointer<Uint8>);
typedef NetServiceControlDart = int Function(Pointer<Utf16>, Pointer<Utf16>, int, int, Pointer<Uint8>);
final NetServiceControl = DynamicLibrary.open('NETAPI32.dll')
.lookupFunction<NetServiceControlNative, NetServiceControlDart>('NetServiceControl');
// servername : LPCWSTR optional -> Pointer<Utf16>
// service : LPCWSTR -> Pointer<Utf16>
// opcode : DWORD -> Uint32
// arg : DWORD -> Uint32
// bufptr : BYTE** out -> Pointer<Uint8>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function NetServiceControl(
servername: PWideChar; // LPCWSTR optional
service: PWideChar; // LPCWSTR
opcode: DWORD; // DWORD
arg: DWORD; // DWORD
bufptr: Pointer // BYTE** out
): DWORD; stdcall;
external 'NETAPI32.dll' name 'NetServiceControl';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "NetServiceControl"
c_NetServiceControl :: CWString -> CWString -> Word32 -> Word32 -> Ptr Word8 -> IO Word32
-- servername : LPCWSTR optional -> CWString
-- service : LPCWSTR -> CWString
-- opcode : DWORD -> Word32
-- arg : DWORD -> Word32
-- bufptr : BYTE** out -> Ptr Word8
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let netservicecontrol =
foreign "NetServiceControl"
((ptr uint16_t) @-> (ptr uint16_t) @-> uint32_t @-> uint32_t @-> (ptr uint8_t) @-> returning uint32_t)
(* servername : LPCWSTR optional -> (ptr uint16_t) *)
(* service : LPCWSTR -> (ptr uint16_t) *)
(* opcode : DWORD -> uint32_t *)
(* arg : DWORD -> uint32_t *)
(* bufptr : BYTE** out -> (ptr uint8_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library netapi32 (t "NETAPI32.dll"))
(cffi:use-foreign-library netapi32)
(cffi:defcfun ("NetServiceControl" net-service-control :convention :stdcall) :uint32
(servername (:string :encoding :utf-16le)) ; LPCWSTR optional
(service (:string :encoding :utf-16le)) ; LPCWSTR
(opcode :uint32) ; DWORD
(arg :uint32) ; DWORD
(bufptr :pointer)) ; BYTE** out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $NetServiceControl = Win32::API::More->new('NETAPI32',
'DWORD NetServiceControl(LPCWSTR servername, LPCWSTR service, DWORD opcode, DWORD arg, LPVOID bufptr)');
# my $ret = $NetServiceControl->Call($servername, $service, $opcode, $arg, $bufptr);
# servername : LPCWSTR optional -> LPCWSTR
# service : LPCWSTR -> LPCWSTR
# opcode : DWORD -> DWORD
# arg : DWORD -> DWORD
# bufptr : BYTE** out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。