Win32 API 日本語リファレンス
ホームSystem.Services › CreateServiceW

CreateServiceW

関数
新しいサービスを作成しSCMデータベースに登録する。
DLLADVAPI32.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

// ADVAPI32.dll  (Unicode / -W)
#include <windows.h>

SC_HANDLE CreateServiceW(
    SC_HANDLE hSCManager,
    LPCWSTR lpServiceName,
    LPCWSTR lpDisplayName,   // optional
    DWORD dwDesiredAccess,
    ENUM_SERVICE_TYPE dwServiceType,
    SERVICE_START_TYPE dwStartType,
    SERVICE_ERROR dwErrorControl,
    LPCWSTR lpBinaryPathName,   // optional
    LPCWSTR lpLoadOrderGroup,   // optional
    DWORD* lpdwTagId,   // optional
    LPCWSTR lpDependencies,   // optional
    LPCWSTR lpServiceStartName,   // optional
    LPCWSTR lpPassword   // optional
);

パラメーター

名前方向
hSCManagerSC_HANDLEin
lpServiceNameLPCWSTRin
lpDisplayNameLPCWSTRinoptional
dwDesiredAccessDWORDin
dwServiceTypeENUM_SERVICE_TYPEin
dwStartTypeSERVICE_START_TYPEin
dwErrorControlSERVICE_ERRORin
lpBinaryPathNameLPCWSTRinoptional
lpLoadOrderGroupLPCWSTRinoptional
lpdwTagIdDWORD*outoptional
lpDependenciesLPCWSTRinoptional
lpServiceStartNameLPCWSTRinoptional
lpPasswordLPCWSTRinoptional

戻り値の型: SC_HANDLE

各言語での呼び出し定義

// ADVAPI32.dll  (Unicode / -W)
#include <windows.h>

SC_HANDLE CreateServiceW(
    SC_HANDLE hSCManager,
    LPCWSTR lpServiceName,
    LPCWSTR lpDisplayName,   // optional
    DWORD dwDesiredAccess,
    ENUM_SERVICE_TYPE dwServiceType,
    SERVICE_START_TYPE dwStartType,
    SERVICE_ERROR dwErrorControl,
    LPCWSTR lpBinaryPathName,   // optional
    LPCWSTR lpLoadOrderGroup,   // optional
    DWORD* lpdwTagId,   // optional
    LPCWSTR lpDependencies,   // optional
    LPCWSTR lpServiceStartName,   // optional
    LPCWSTR lpPassword   // optional
);
[DllImport("ADVAPI32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern IntPtr CreateServiceW(
    IntPtr hSCManager,   // SC_HANDLE
    [MarshalAs(UnmanagedType.LPWStr)] string lpServiceName,   // LPCWSTR
    [MarshalAs(UnmanagedType.LPWStr)] string lpDisplayName,   // LPCWSTR optional
    uint dwDesiredAccess,   // DWORD
    uint dwServiceType,   // ENUM_SERVICE_TYPE
    uint dwStartType,   // SERVICE_START_TYPE
    uint dwErrorControl,   // SERVICE_ERROR
    [MarshalAs(UnmanagedType.LPWStr)] string lpBinaryPathName,   // LPCWSTR optional
    [MarshalAs(UnmanagedType.LPWStr)] string lpLoadOrderGroup,   // LPCWSTR optional
    IntPtr lpdwTagId,   // DWORD* optional, out
    [MarshalAs(UnmanagedType.LPWStr)] string lpDependencies,   // LPCWSTR optional
    [MarshalAs(UnmanagedType.LPWStr)] string lpServiceStartName,   // LPCWSTR optional
    [MarshalAs(UnmanagedType.LPWStr)] string lpPassword   // LPCWSTR optional
);
<DllImport("ADVAPI32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function CreateServiceW(
    hSCManager As IntPtr,   ' SC_HANDLE
    <MarshalAs(UnmanagedType.LPWStr)> lpServiceName As String,   ' LPCWSTR
    <MarshalAs(UnmanagedType.LPWStr)> lpDisplayName As String,   ' LPCWSTR optional
    dwDesiredAccess As UInteger,   ' DWORD
    dwServiceType As UInteger,   ' ENUM_SERVICE_TYPE
    dwStartType As UInteger,   ' SERVICE_START_TYPE
    dwErrorControl As UInteger,   ' SERVICE_ERROR
    <MarshalAs(UnmanagedType.LPWStr)> lpBinaryPathName As String,   ' LPCWSTR optional
    <MarshalAs(UnmanagedType.LPWStr)> lpLoadOrderGroup As String,   ' LPCWSTR optional
    lpdwTagId As IntPtr,   ' DWORD* optional, out
    <MarshalAs(UnmanagedType.LPWStr)> lpDependencies As String,   ' LPCWSTR optional
    <MarshalAs(UnmanagedType.LPWStr)> lpServiceStartName As String,   ' LPCWSTR optional
    <MarshalAs(UnmanagedType.LPWStr)> lpPassword As String   ' LPCWSTR optional
) As IntPtr
End Function
' hSCManager : SC_HANDLE
' lpServiceName : LPCWSTR
' lpDisplayName : LPCWSTR optional
' dwDesiredAccess : DWORD
' dwServiceType : ENUM_SERVICE_TYPE
' dwStartType : SERVICE_START_TYPE
' dwErrorControl : SERVICE_ERROR
' lpBinaryPathName : LPCWSTR optional
' lpLoadOrderGroup : LPCWSTR optional
' lpdwTagId : DWORD* optional, out
' lpDependencies : LPCWSTR optional
' lpServiceStartName : LPCWSTR optional
' lpPassword : LPCWSTR optional
Declare PtrSafe Function CreateServiceW Lib "advapi32" ( _
    ByVal hSCManager As LongPtr, _
    ByVal lpServiceName As LongPtr, _
    ByVal lpDisplayName As LongPtr, _
    ByVal dwDesiredAccess As Long, _
    ByVal dwServiceType As Long, _
    ByVal dwStartType As Long, _
    ByVal dwErrorControl As Long, _
    ByVal lpBinaryPathName As LongPtr, _
    ByVal lpLoadOrderGroup As LongPtr, _
    ByVal lpdwTagId As LongPtr, _
    ByVal lpDependencies As LongPtr, _
    ByVal lpServiceStartName As LongPtr, _
    ByVal lpPassword As LongPtr) 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

CreateServiceW = ctypes.windll.advapi32.CreateServiceW
CreateServiceW.restype = ctypes.c_void_p
CreateServiceW.argtypes = [
    wintypes.HANDLE,  # hSCManager : SC_HANDLE
    wintypes.LPCWSTR,  # lpServiceName : LPCWSTR
    wintypes.LPCWSTR,  # lpDisplayName : LPCWSTR optional
    wintypes.DWORD,  # dwDesiredAccess : DWORD
    wintypes.DWORD,  # dwServiceType : ENUM_SERVICE_TYPE
    wintypes.DWORD,  # dwStartType : SERVICE_START_TYPE
    wintypes.DWORD,  # dwErrorControl : SERVICE_ERROR
    wintypes.LPCWSTR,  # lpBinaryPathName : LPCWSTR optional
    wintypes.LPCWSTR,  # lpLoadOrderGroup : LPCWSTR optional
    ctypes.POINTER(wintypes.DWORD),  # lpdwTagId : DWORD* optional, out
    wintypes.LPCWSTR,  # lpDependencies : LPCWSTR optional
    wintypes.LPCWSTR,  # lpServiceStartName : LPCWSTR optional
    wintypes.LPCWSTR,  # lpPassword : LPCWSTR optional
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('ADVAPI32.dll')
CreateServiceW = Fiddle::Function.new(
  lib['CreateServiceW'],
  [
    Fiddle::TYPE_VOIDP,  # hSCManager : SC_HANDLE
    Fiddle::TYPE_VOIDP,  # lpServiceName : LPCWSTR
    Fiddle::TYPE_VOIDP,  # lpDisplayName : LPCWSTR optional
    -Fiddle::TYPE_INT,  # dwDesiredAccess : DWORD
    -Fiddle::TYPE_INT,  # dwServiceType : ENUM_SERVICE_TYPE
    -Fiddle::TYPE_INT,  # dwStartType : SERVICE_START_TYPE
    -Fiddle::TYPE_INT,  # dwErrorControl : SERVICE_ERROR
    Fiddle::TYPE_VOIDP,  # lpBinaryPathName : LPCWSTR optional
    Fiddle::TYPE_VOIDP,  # lpLoadOrderGroup : LPCWSTR optional
    Fiddle::TYPE_VOIDP,  # lpdwTagId : DWORD* optional, out
    Fiddle::TYPE_VOIDP,  # lpDependencies : LPCWSTR optional
    Fiddle::TYPE_VOIDP,  # lpServiceStartName : LPCWSTR optional
    Fiddle::TYPE_VOIDP,  # lpPassword : LPCWSTR optional
  ],
  Fiddle::TYPE_VOIDP)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "advapi32")]
extern "system" {
    fn CreateServiceW(
        hSCManager: *mut core::ffi::c_void,  // SC_HANDLE
        lpServiceName: *const u16,  // LPCWSTR
        lpDisplayName: *const u16,  // LPCWSTR optional
        dwDesiredAccess: u32,  // DWORD
        dwServiceType: u32,  // ENUM_SERVICE_TYPE
        dwStartType: u32,  // SERVICE_START_TYPE
        dwErrorControl: u32,  // SERVICE_ERROR
        lpBinaryPathName: *const u16,  // LPCWSTR optional
        lpLoadOrderGroup: *const u16,  // LPCWSTR optional
        lpdwTagId: *mut u32,  // DWORD* optional, out
        lpDependencies: *const u16,  // LPCWSTR optional
        lpServiceStartName: *const u16,  // LPCWSTR optional
        lpPassword: *const u16  // LPCWSTR optional
    ) -> *mut core::ffi::c_void;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("ADVAPI32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern IntPtr CreateServiceW(IntPtr hSCManager, [MarshalAs(UnmanagedType.LPWStr)] string lpServiceName, [MarshalAs(UnmanagedType.LPWStr)] string lpDisplayName, uint dwDesiredAccess, uint dwServiceType, uint dwStartType, uint dwErrorControl, [MarshalAs(UnmanagedType.LPWStr)] string lpBinaryPathName, [MarshalAs(UnmanagedType.LPWStr)] string lpLoadOrderGroup, IntPtr lpdwTagId, [MarshalAs(UnmanagedType.LPWStr)] string lpDependencies, [MarshalAs(UnmanagedType.LPWStr)] string lpServiceStartName, [MarshalAs(UnmanagedType.LPWStr)] string lpPassword);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ADVAPI32_CreateServiceW' -Namespace Win32 -PassThru
# $api::CreateServiceW(hSCManager, lpServiceName, lpDisplayName, dwDesiredAccess, dwServiceType, dwStartType, dwErrorControl, lpBinaryPathName, lpLoadOrderGroup, lpdwTagId, lpDependencies, lpServiceStartName, lpPassword)
#uselib "ADVAPI32.dll"
#func global CreateServiceW "CreateServiceW" wptr, wptr, wptr, wptr, wptr, wptr, wptr, wptr, wptr, wptr, wptr, wptr, wptr
; CreateServiceW hSCManager, lpServiceName, lpDisplayName, dwDesiredAccess, dwServiceType, dwStartType, dwErrorControl, lpBinaryPathName, lpLoadOrderGroup, varptr(lpdwTagId), lpDependencies, lpServiceStartName, lpPassword   ; 戻り値は stat
; hSCManager : SC_HANDLE -> "wptr"
; lpServiceName : LPCWSTR -> "wptr"
; lpDisplayName : LPCWSTR optional -> "wptr"
; dwDesiredAccess : DWORD -> "wptr"
; dwServiceType : ENUM_SERVICE_TYPE -> "wptr"
; dwStartType : SERVICE_START_TYPE -> "wptr"
; dwErrorControl : SERVICE_ERROR -> "wptr"
; lpBinaryPathName : LPCWSTR optional -> "wptr"
; lpLoadOrderGroup : LPCWSTR optional -> "wptr"
; lpdwTagId : DWORD* optional, out -> "wptr"
; lpDependencies : LPCWSTR optional -> "wptr"
; lpServiceStartName : LPCWSTR optional -> "wptr"
; lpPassword : LPCWSTR optional -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "ADVAPI32.dll"
#cfunc global CreateServiceW "CreateServiceW" sptr, wstr, wstr, int, int, int, int, wstr, wstr, var, wstr, wstr, wstr
; res = CreateServiceW(hSCManager, lpServiceName, lpDisplayName, dwDesiredAccess, dwServiceType, dwStartType, dwErrorControl, lpBinaryPathName, lpLoadOrderGroup, lpdwTagId, lpDependencies, lpServiceStartName, lpPassword)
; hSCManager : SC_HANDLE -> "sptr"
; lpServiceName : LPCWSTR -> "wstr"
; lpDisplayName : LPCWSTR optional -> "wstr"
; dwDesiredAccess : DWORD -> "int"
; dwServiceType : ENUM_SERVICE_TYPE -> "int"
; dwStartType : SERVICE_START_TYPE -> "int"
; dwErrorControl : SERVICE_ERROR -> "int"
; lpBinaryPathName : LPCWSTR optional -> "wstr"
; lpLoadOrderGroup : LPCWSTR optional -> "wstr"
; lpdwTagId : DWORD* optional, out -> "var"
; lpDependencies : LPCWSTR optional -> "wstr"
; lpServiceStartName : LPCWSTR optional -> "wstr"
; lpPassword : LPCWSTR optional -> "wstr"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; SC_HANDLE CreateServiceW(SC_HANDLE hSCManager, LPCWSTR lpServiceName, LPCWSTR lpDisplayName, DWORD dwDesiredAccess, ENUM_SERVICE_TYPE dwServiceType, SERVICE_START_TYPE dwStartType, SERVICE_ERROR dwErrorControl, LPCWSTR lpBinaryPathName, LPCWSTR lpLoadOrderGroup, DWORD* lpdwTagId, LPCWSTR lpDependencies, LPCWSTR lpServiceStartName, LPCWSTR lpPassword)
#uselib "ADVAPI32.dll"
#cfunc global CreateServiceW "CreateServiceW" intptr, wstr, wstr, int, int, int, int, wstr, wstr, var, wstr, wstr, wstr
; res = CreateServiceW(hSCManager, lpServiceName, lpDisplayName, dwDesiredAccess, dwServiceType, dwStartType, dwErrorControl, lpBinaryPathName, lpLoadOrderGroup, lpdwTagId, lpDependencies, lpServiceStartName, lpPassword)
; hSCManager : SC_HANDLE -> "intptr"
; lpServiceName : LPCWSTR -> "wstr"
; lpDisplayName : LPCWSTR optional -> "wstr"
; dwDesiredAccess : DWORD -> "int"
; dwServiceType : ENUM_SERVICE_TYPE -> "int"
; dwStartType : SERVICE_START_TYPE -> "int"
; dwErrorControl : SERVICE_ERROR -> "int"
; lpBinaryPathName : LPCWSTR optional -> "wstr"
; lpLoadOrderGroup : LPCWSTR optional -> "wstr"
; lpdwTagId : DWORD* optional, out -> "var"
; lpDependencies : LPCWSTR optional -> "wstr"
; lpServiceStartName : LPCWSTR optional -> "wstr"
; lpPassword : LPCWSTR optional -> "wstr"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	advapi32 = windows.NewLazySystemDLL("ADVAPI32.dll")
	procCreateServiceW = advapi32.NewProc("CreateServiceW")
)

// hSCManager (SC_HANDLE), lpServiceName (LPCWSTR), lpDisplayName (LPCWSTR optional), dwDesiredAccess (DWORD), dwServiceType (ENUM_SERVICE_TYPE), dwStartType (SERVICE_START_TYPE), dwErrorControl (SERVICE_ERROR), lpBinaryPathName (LPCWSTR optional), lpLoadOrderGroup (LPCWSTR optional), lpdwTagId (DWORD* optional, out), lpDependencies (LPCWSTR optional), lpServiceStartName (LPCWSTR optional), lpPassword (LPCWSTR optional)
r1, _, err := procCreateServiceW.Call(
	uintptr(hSCManager),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpServiceName))),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpDisplayName))),
	uintptr(dwDesiredAccess),
	uintptr(dwServiceType),
	uintptr(dwStartType),
	uintptr(dwErrorControl),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpBinaryPathName))),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpLoadOrderGroup))),
	uintptr(lpdwTagId),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpDependencies))),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpServiceStartName))),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpPassword))),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // SC_HANDLE
function CreateServiceW(
  hSCManager: THandle;   // SC_HANDLE
  lpServiceName: PWideChar;   // LPCWSTR
  lpDisplayName: PWideChar;   // LPCWSTR optional
  dwDesiredAccess: DWORD;   // DWORD
  dwServiceType: DWORD;   // ENUM_SERVICE_TYPE
  dwStartType: DWORD;   // SERVICE_START_TYPE
  dwErrorControl: DWORD;   // SERVICE_ERROR
  lpBinaryPathName: PWideChar;   // LPCWSTR optional
  lpLoadOrderGroup: PWideChar;   // LPCWSTR optional
  lpdwTagId: Pointer;   // DWORD* optional, out
  lpDependencies: PWideChar;   // LPCWSTR optional
  lpServiceStartName: PWideChar;   // LPCWSTR optional
  lpPassword: PWideChar   // LPCWSTR optional
): THandle; stdcall;
  external 'ADVAPI32.dll' name 'CreateServiceW';
result := DllCall("ADVAPI32\CreateServiceW"
    , "Ptr", hSCManager   ; SC_HANDLE
    , "WStr", lpServiceName   ; LPCWSTR
    , "WStr", lpDisplayName   ; LPCWSTR optional
    , "UInt", dwDesiredAccess   ; DWORD
    , "UInt", dwServiceType   ; ENUM_SERVICE_TYPE
    , "UInt", dwStartType   ; SERVICE_START_TYPE
    , "UInt", dwErrorControl   ; SERVICE_ERROR
    , "WStr", lpBinaryPathName   ; LPCWSTR optional
    , "WStr", lpLoadOrderGroup   ; LPCWSTR optional
    , "Ptr", lpdwTagId   ; DWORD* optional, out
    , "WStr", lpDependencies   ; LPCWSTR optional
    , "WStr", lpServiceStartName   ; LPCWSTR optional
    , "WStr", lpPassword   ; LPCWSTR optional
    , "Ptr")   ; return: SC_HANDLE
●CreateServiceW(hSCManager, lpServiceName, lpDisplayName, dwDesiredAccess, dwServiceType, dwStartType, dwErrorControl, lpBinaryPathName, lpLoadOrderGroup, lpdwTagId, lpDependencies, lpServiceStartName, lpPassword) = DLL("ADVAPI32.dll", "void* CreateServiceW(void*, char*, char*, dword, dword, dword, dword, char*, char*, void*, char*, char*, char*)")
# 呼び出し: CreateServiceW(hSCManager, lpServiceName, lpDisplayName, dwDesiredAccess, dwServiceType, dwStartType, dwErrorControl, lpBinaryPathName, lpLoadOrderGroup, lpdwTagId, lpDependencies, lpServiceStartName, lpPassword)
# hSCManager : SC_HANDLE -> "void*"
# lpServiceName : LPCWSTR -> "char*"
# lpDisplayName : LPCWSTR optional -> "char*"
# dwDesiredAccess : DWORD -> "dword"
# dwServiceType : ENUM_SERVICE_TYPE -> "dword"
# dwStartType : SERVICE_START_TYPE -> "dword"
# dwErrorControl : SERVICE_ERROR -> "dword"
# lpBinaryPathName : LPCWSTR optional -> "char*"
# lpLoadOrderGroup : LPCWSTR optional -> "char*"
# lpdwTagId : DWORD* optional, out -> "void*"
# lpDependencies : LPCWSTR optional -> "char*"
# lpServiceStartName : LPCWSTR optional -> "char*"
# lpPassword : LPCWSTR optional -> "char*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。