Win32 API 日本語リファレンス
ホームSecurity › LookupPrivilegeValueW

LookupPrivilegeValueW

関数
特権名からローカル一意識別子LUIDを取得する(Unicode版)。
DLLADVAPI32.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

BOOL LookupPrivilegeValueW(
    LPCWSTR lpSystemName,   // optional
    LPCWSTR lpName,
    LUID* lpLuid
);

パラメーター

名前方向説明
lpSystemNameLPCWSTRinoptional特権名を取得する対象システムの名前を指定する、null で終わる文字列へのポインター。null 文字列を指定した場合、関数はローカルシステム上で特権名を検索しようとします。
lpNameLPCWSTRinWinnt.h ヘッダーファイルで定義されている特権の名前を指定する、null で終わる文字列へのポインター。たとえば、このパラメーターには定数 SE_SECURITY_NAME、またはそれに対応する文字列 "SeSecurityPrivilege" を指定できます。
lpLuidLUID*outlpSystemName パラメーターで指定したシステム上で特権を識別する LUID を受け取る変数へのポインター。

戻り値の型: BOOL

公式ドキュメント

指定したシステム上で、指定した特権名をローカルに表すために使用されるローカル一意識別子 (LUID) を取得します。(Unicode)

戻り値

関数が成功すると、0 以外の値を返します。

関数が失敗すると、0 を返します。拡張エラー情報を取得するには、 GetLastError を呼び出します。

解説(Remarks)

LookupPrivilegeValue 関数は、Winnt.h の Defined Privileges セクションで指定された特権のみをサポートします。値の一覧については、特権定数を参照してください。

この関数を使用する例については、特権の有効化と無効化を参照してください。

メモ

winbase.h ヘッダーは、LookupPrivilegeValue を、UNICODE プリプロセッサ定数の定義に基づいてこの関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして定義しています。エンコーディング非依存のエイリアスと、エンコーディング非依存ではないコードを混在して使用すると、不一致が生じてコンパイルエラーや実行時エラーの原因となる場合があります。詳細については、関数プロトタイプの規則を参照してください。

出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

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

BOOL LookupPrivilegeValueW(
    LPCWSTR lpSystemName,   // optional
    LPCWSTR lpName,
    LUID* lpLuid
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("ADVAPI32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern bool LookupPrivilegeValueW(
    [MarshalAs(UnmanagedType.LPWStr)] string lpSystemName,   // LPCWSTR optional
    [MarshalAs(UnmanagedType.LPWStr)] string lpName,   // LPCWSTR
    IntPtr lpLuid   // LUID* out
);
<DllImport("ADVAPI32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function LookupPrivilegeValueW(
    <MarshalAs(UnmanagedType.LPWStr)> lpSystemName As String,   ' LPCWSTR optional
    <MarshalAs(UnmanagedType.LPWStr)> lpName As String,   ' LPCWSTR
    lpLuid As IntPtr   ' LUID* out
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' lpSystemName : LPCWSTR optional
' lpName : LPCWSTR
' lpLuid : LUID* out
Declare PtrSafe Function LookupPrivilegeValueW Lib "advapi32" ( _
    ByVal lpSystemName As LongPtr, _
    ByVal lpName As LongPtr, _
    ByVal lpLuid As LongPtr) As Long
' 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

LookupPrivilegeValueW = ctypes.windll.advapi32.LookupPrivilegeValueW
LookupPrivilegeValueW.restype = wintypes.BOOL
LookupPrivilegeValueW.argtypes = [
    wintypes.LPCWSTR,  # lpSystemName : LPCWSTR optional
    wintypes.LPCWSTR,  # lpName : LPCWSTR
    ctypes.c_void_p,  # lpLuid : LUID* out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('ADVAPI32.dll')
LookupPrivilegeValueW = Fiddle::Function.new(
  lib['LookupPrivilegeValueW'],
  [
    Fiddle::TYPE_VOIDP,  # lpSystemName : LPCWSTR optional
    Fiddle::TYPE_VOIDP,  # lpName : LPCWSTR
    Fiddle::TYPE_VOIDP,  # lpLuid : LUID* out
  ],
  Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "advapi32")]
extern "system" {
    fn LookupPrivilegeValueW(
        lpSystemName: *const u16,  // LPCWSTR optional
        lpName: *const u16,  // LPCWSTR
        lpLuid: *mut LUID  // LUID* out
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("ADVAPI32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool LookupPrivilegeValueW([MarshalAs(UnmanagedType.LPWStr)] string lpSystemName, [MarshalAs(UnmanagedType.LPWStr)] string lpName, IntPtr lpLuid);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ADVAPI32_LookupPrivilegeValueW' -Namespace Win32 -PassThru
# $api::LookupPrivilegeValueW(lpSystemName, lpName, lpLuid)
#uselib "ADVAPI32.dll"
#func global LookupPrivilegeValueW "LookupPrivilegeValueW" wptr, wptr, wptr
; LookupPrivilegeValueW lpSystemName, lpName, varptr(lpLuid)   ; 戻り値は stat
; lpSystemName : LPCWSTR optional -> "wptr"
; lpName : LPCWSTR -> "wptr"
; lpLuid : LUID* out -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "ADVAPI32.dll"
#cfunc global LookupPrivilegeValueW "LookupPrivilegeValueW" wstr, wstr, var
; res = LookupPrivilegeValueW(lpSystemName, lpName, lpLuid)
; lpSystemName : LPCWSTR optional -> "wstr"
; lpName : LPCWSTR -> "wstr"
; lpLuid : LUID* out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL LookupPrivilegeValueW(LPCWSTR lpSystemName, LPCWSTR lpName, LUID* lpLuid)
#uselib "ADVAPI32.dll"
#cfunc global LookupPrivilegeValueW "LookupPrivilegeValueW" wstr, wstr, var
; res = LookupPrivilegeValueW(lpSystemName, lpName, lpLuid)
; lpSystemName : LPCWSTR optional -> "wstr"
; lpName : LPCWSTR -> "wstr"
; lpLuid : LUID* out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	advapi32 = windows.NewLazySystemDLL("ADVAPI32.dll")
	procLookupPrivilegeValueW = advapi32.NewProc("LookupPrivilegeValueW")
)

// lpSystemName (LPCWSTR optional), lpName (LPCWSTR), lpLuid (LUID* out)
r1, _, err := procLookupPrivilegeValueW.Call(
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpSystemName))),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpName))),
	uintptr(lpLuid),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function LookupPrivilegeValueW(
  lpSystemName: PWideChar;   // LPCWSTR optional
  lpName: PWideChar;   // LPCWSTR
  lpLuid: Pointer   // LUID* out
): BOOL; stdcall;
  external 'ADVAPI32.dll' name 'LookupPrivilegeValueW';
result := DllCall("ADVAPI32\LookupPrivilegeValueW"
    , "WStr", lpSystemName   ; LPCWSTR optional
    , "WStr", lpName   ; LPCWSTR
    , "Ptr", lpLuid   ; LUID* out
    , "Int")   ; return: BOOL
●LookupPrivilegeValueW(lpSystemName, lpName, lpLuid) = DLL("ADVAPI32.dll", "bool LookupPrivilegeValueW(char*, char*, void*)")
# 呼び出し: LookupPrivilegeValueW(lpSystemName, lpName, lpLuid)
# lpSystemName : LPCWSTR optional -> "char*"
# lpName : LPCWSTR -> "char*"
# lpLuid : LUID* out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。
const std = @import("std");

extern "advapi32" fn LookupPrivilegeValueW(
    lpSystemName: [*c]const u16, // LPCWSTR optional
    lpName: [*c]const u16, // LPCWSTR
    lpLuid: [*c]LUID // LUID* out
) callconv(std.os.windows.WINAPI) i32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。
proc LookupPrivilegeValueW(
    lpSystemName: WideCString,  # LPCWSTR optional
    lpName: WideCString,  # LPCWSTR
    lpLuid: ptr LUID  # LUID* out
): int32 {.importc: "LookupPrivilegeValueW", stdcall, dynlib: "ADVAPI32.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "advapi32");
extern(Windows)
int LookupPrivilegeValueW(
    const(wchar)* lpSystemName,   // LPCWSTR optional
    const(wchar)* lpName,   // LPCWSTR
    LUID* lpLuid   // LUID* out
);
ccall((:LookupPrivilegeValueW, "ADVAPI32.dll"), stdcall, Int32,
      (Cwstring, Cwstring, Ptr{LUID}),
      lpSystemName, lpName, lpLuid)
# lpSystemName : LPCWSTR optional -> Cwstring
# lpName : LPCWSTR -> Cwstring
# lpLuid : LUID* out -> Ptr{LUID}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。
local ffi = require("ffi")
ffi.cdef[[
int32_t LookupPrivilegeValueW(
    const uint16_t* lpSystemName,
    const uint16_t* lpName,
    void* lpLuid);
]]
local advapi32 = ffi.load("advapi32")
-- advapi32.LookupPrivilegeValueW(lpSystemName, lpName, lpLuid)
-- lpSystemName : LPCWSTR optional
-- lpName : LPCWSTR
-- lpLuid : LUID* out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。
const koffi = require('koffi');
const lib = koffi.load('ADVAPI32.dll');
const LookupPrivilegeValueW = lib.func('__stdcall', 'LookupPrivilegeValueW', 'int32_t', ['str16', 'str16', 'void *']);
// LookupPrivilegeValueW(lpSystemName, lpName, lpLuid)
// lpSystemName : LPCWSTR optional -> 'str16'
// lpName : LPCWSTR -> 'str16'
// lpLuid : LUID* out -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("ADVAPI32.dll", {
  LookupPrivilegeValueW: { parameters: ["buffer", "buffer", "pointer"], result: "i32" },
});
// lib.symbols.LookupPrivilegeValueW(lpSystemName, lpName, lpLuid)
// lpSystemName : LPCWSTR optional -> "buffer"
// lpName : LPCWSTR -> "buffer"
// lpLuid : LUID* out -> "pointer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t LookupPrivilegeValueW(
    const uint16_t* lpSystemName,
    const uint16_t* lpName,
    void* lpLuid);
C, "ADVAPI32.dll");
// $ffi->LookupPrivilegeValueW(lpSystemName, lpName, lpLuid);
// lpSystemName : LPCWSTR optional
// lpName : LPCWSTR
// lpLuid : LUID* 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 Advapi32 extends StdCallLibrary {
    Advapi32 INSTANCE = Native.load("advapi32", Advapi32.class, W32APIOptions.UNICODE_OPTIONS);
    boolean LookupPrivilegeValueW(
        WString lpSystemName,   // LPCWSTR optional
        WString lpName,   // LPCWSTR
        Pointer lpLuid   // LUID* out
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("advapi32")]
lib LibADVAPI32
  fun LookupPrivilegeValueW = LookupPrivilegeValueW(
    lpSystemName : UInt16*,   # LPCWSTR optional
    lpName : UInt16*,   # LPCWSTR
    lpLuid : LUID*   # LUID* out
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef LookupPrivilegeValueWNative = Int32 Function(Pointer<Utf16>, Pointer<Utf16>, Pointer<Void>);
typedef LookupPrivilegeValueWDart = int Function(Pointer<Utf16>, Pointer<Utf16>, Pointer<Void>);
final LookupPrivilegeValueW = DynamicLibrary.open('ADVAPI32.dll')
    .lookupFunction<LookupPrivilegeValueWNative, LookupPrivilegeValueWDart>('LookupPrivilegeValueW');
// lpSystemName : LPCWSTR optional -> Pointer<Utf16>
// lpName : LPCWSTR -> Pointer<Utf16>
// lpLuid : LUID* out -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function LookupPrivilegeValueW(
  lpSystemName: PWideChar;   // LPCWSTR optional
  lpName: PWideChar;   // LPCWSTR
  lpLuid: Pointer   // LUID* out
): BOOL; stdcall;
  external 'ADVAPI32.dll' name 'LookupPrivilegeValueW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "LookupPrivilegeValueW"
  c_LookupPrivilegeValueW :: CWString -> CWString -> Ptr () -> IO CInt
-- lpSystemName : LPCWSTR optional -> CWString
-- lpName : LPCWSTR -> CWString
-- lpLuid : LUID* out -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let lookupprivilegevaluew =
  foreign "LookupPrivilegeValueW"
    ((ptr uint16_t) @-> (ptr uint16_t) @-> (ptr void) @-> returning int32_t)
(* lpSystemName : LPCWSTR optional -> (ptr uint16_t) *)
(* lpName : LPCWSTR -> (ptr uint16_t) *)
(* lpLuid : LUID* out -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library advapi32 (t "ADVAPI32.dll"))
(cffi:use-foreign-library advapi32)

(cffi:defcfun ("LookupPrivilegeValueW" lookup-privilege-value-w :convention :stdcall) :int32
  (lp-system-name (:string :encoding :utf-16le))   ; LPCWSTR optional
  (lp-name (:string :encoding :utf-16le))   ; LPCWSTR
  (lp-luid :pointer))   ; LUID* out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $LookupPrivilegeValueW = Win32::API::More->new('ADVAPI32',
    'BOOL LookupPrivilegeValueW(LPCWSTR lpSystemName, LPCWSTR lpName, LPVOID lpLuid)');
# my $ret = $LookupPrivilegeValueW->Call($lpSystemName, $lpName, $lpLuid);
# lpSystemName : LPCWSTR optional -> LPCWSTR
# lpName : LPCWSTR -> LPCWSTR
# lpLuid : LUID* out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

文字セット違い
公式の関連項目
使用する型