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

SetServiceObjectSecurity

関数
サービスのセキュリティ記述子を設定する。
DLLADVAPI32.dll呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

// ADVAPI32.dll
#include <windows.h>

BOOL SetServiceObjectSecurity(
    SC_HANDLE hService,
    OBJECT_SECURITY_INFORMATION dwSecurityInformation,
    PSECURITY_DESCRIPTOR lpSecurityDescriptor
);

パラメーター

名前方向
hServiceSC_HANDLEin
dwSecurityInformationOBJECT_SECURITY_INFORMATIONin
lpSecurityDescriptorPSECURITY_DESCRIPTORin

戻り値の型: BOOL

各言語での呼び出し定義

// ADVAPI32.dll
#include <windows.h>

BOOL SetServiceObjectSecurity(
    SC_HANDLE hService,
    OBJECT_SECURITY_INFORMATION dwSecurityInformation,
    PSECURITY_DESCRIPTOR lpSecurityDescriptor
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("ADVAPI32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool SetServiceObjectSecurity(
    IntPtr hService,   // SC_HANDLE
    uint dwSecurityInformation,   // OBJECT_SECURITY_INFORMATION
    IntPtr lpSecurityDescriptor   // PSECURITY_DESCRIPTOR
);
<DllImport("ADVAPI32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function SetServiceObjectSecurity(
    hService As IntPtr,   ' SC_HANDLE
    dwSecurityInformation As UInteger,   ' OBJECT_SECURITY_INFORMATION
    lpSecurityDescriptor As IntPtr   ' PSECURITY_DESCRIPTOR
) As Boolean
End Function
' hService : SC_HANDLE
' dwSecurityInformation : OBJECT_SECURITY_INFORMATION
' lpSecurityDescriptor : PSECURITY_DESCRIPTOR
Declare PtrSafe Function SetServiceObjectSecurity Lib "advapi32" ( _
    ByVal hService As LongPtr, _
    ByVal dwSecurityInformation As Long, _
    ByVal lpSecurityDescriptor As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

SetServiceObjectSecurity = ctypes.windll.advapi32.SetServiceObjectSecurity
SetServiceObjectSecurity.restype = wintypes.BOOL
SetServiceObjectSecurity.argtypes = [
    wintypes.HANDLE,  # hService : SC_HANDLE
    wintypes.DWORD,  # dwSecurityInformation : OBJECT_SECURITY_INFORMATION
    wintypes.HANDLE,  # lpSecurityDescriptor : PSECURITY_DESCRIPTOR
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('ADVAPI32.dll')
SetServiceObjectSecurity = Fiddle::Function.new(
  lib['SetServiceObjectSecurity'],
  [
    Fiddle::TYPE_VOIDP,  # hService : SC_HANDLE
    -Fiddle::TYPE_INT,  # dwSecurityInformation : OBJECT_SECURITY_INFORMATION
    Fiddle::TYPE_VOIDP,  # lpSecurityDescriptor : PSECURITY_DESCRIPTOR
  ],
  Fiddle::TYPE_INT)
#[link(name = "advapi32")]
extern "system" {
    fn SetServiceObjectSecurity(
        hService: *mut core::ffi::c_void,  // SC_HANDLE
        dwSecurityInformation: u32,  // OBJECT_SECURITY_INFORMATION
        lpSecurityDescriptor: *mut core::ffi::c_void  // PSECURITY_DESCRIPTOR
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("ADVAPI32.dll", SetLastError = true)]
public static extern bool SetServiceObjectSecurity(IntPtr hService, uint dwSecurityInformation, IntPtr lpSecurityDescriptor);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ADVAPI32_SetServiceObjectSecurity' -Namespace Win32 -PassThru
# $api::SetServiceObjectSecurity(hService, dwSecurityInformation, lpSecurityDescriptor)
#uselib "ADVAPI32.dll"
#func global SetServiceObjectSecurity "SetServiceObjectSecurity" sptr, sptr, sptr
; SetServiceObjectSecurity hService, dwSecurityInformation, lpSecurityDescriptor   ; 戻り値は stat
; hService : SC_HANDLE -> "sptr"
; dwSecurityInformation : OBJECT_SECURITY_INFORMATION -> "sptr"
; lpSecurityDescriptor : PSECURITY_DESCRIPTOR -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "ADVAPI32.dll"
#cfunc global SetServiceObjectSecurity "SetServiceObjectSecurity" sptr, int, sptr
; res = SetServiceObjectSecurity(hService, dwSecurityInformation, lpSecurityDescriptor)
; hService : SC_HANDLE -> "sptr"
; dwSecurityInformation : OBJECT_SECURITY_INFORMATION -> "int"
; lpSecurityDescriptor : PSECURITY_DESCRIPTOR -> "sptr"
; BOOL SetServiceObjectSecurity(SC_HANDLE hService, OBJECT_SECURITY_INFORMATION dwSecurityInformation, PSECURITY_DESCRIPTOR lpSecurityDescriptor)
#uselib "ADVAPI32.dll"
#cfunc global SetServiceObjectSecurity "SetServiceObjectSecurity" intptr, int, intptr
; res = SetServiceObjectSecurity(hService, dwSecurityInformation, lpSecurityDescriptor)
; hService : SC_HANDLE -> "intptr"
; dwSecurityInformation : OBJECT_SECURITY_INFORMATION -> "int"
; lpSecurityDescriptor : PSECURITY_DESCRIPTOR -> "intptr"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	advapi32 = windows.NewLazySystemDLL("ADVAPI32.dll")
	procSetServiceObjectSecurity = advapi32.NewProc("SetServiceObjectSecurity")
)

// hService (SC_HANDLE), dwSecurityInformation (OBJECT_SECURITY_INFORMATION), lpSecurityDescriptor (PSECURITY_DESCRIPTOR)
r1, _, err := procSetServiceObjectSecurity.Call(
	uintptr(hService),
	uintptr(dwSecurityInformation),
	uintptr(lpSecurityDescriptor),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function SetServiceObjectSecurity(
  hService: THandle;   // SC_HANDLE
  dwSecurityInformation: DWORD;   // OBJECT_SECURITY_INFORMATION
  lpSecurityDescriptor: THandle   // PSECURITY_DESCRIPTOR
): BOOL; stdcall;
  external 'ADVAPI32.dll' name 'SetServiceObjectSecurity';
result := DllCall("ADVAPI32\SetServiceObjectSecurity"
    , "Ptr", hService   ; SC_HANDLE
    , "UInt", dwSecurityInformation   ; OBJECT_SECURITY_INFORMATION
    , "Ptr", lpSecurityDescriptor   ; PSECURITY_DESCRIPTOR
    , "Int")   ; return: BOOL
●SetServiceObjectSecurity(hService, dwSecurityInformation, lpSecurityDescriptor) = DLL("ADVAPI32.dll", "bool SetServiceObjectSecurity(void*, dword, void*)")
# 呼び出し: SetServiceObjectSecurity(hService, dwSecurityInformation, lpSecurityDescriptor)
# hService : SC_HANDLE -> "void*"
# dwSecurityInformation : OBJECT_SECURITY_INFORMATION -> "dword"
# lpSecurityDescriptor : PSECURITY_DESCRIPTOR -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。