Win32 API 日本語リファレンス
ホームSecurity.Authentication.Identity › RtlGenRandom

RtlGenRandom

関数
暗号品質の擬似乱数をバッファに生成する。
DLLADVAPI32.dllエントリSystemFunction036呼出規約winapi

シグネチャ

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

BOOLEAN RtlGenRandom(
    void* RandomBuffer,
    DWORD RandomBufferLength
);

パラメーター

名前方向
RandomBuffervoid*out
RandomBufferLengthDWORDin

戻り値の型: BOOLEAN

各言語での呼び出し定義

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

BOOLEAN RtlGenRandom(
    void* RandomBuffer,
    DWORD RandomBufferLength
);
[DllImport("ADVAPI32.dll", ExactSpelling = true)]
static extern byte RtlGenRandom(
    IntPtr RandomBuffer,   // void* out
    uint RandomBufferLength   // DWORD
);
<DllImport("ADVAPI32.dll", ExactSpelling:=True)>
Public Shared Function RtlGenRandom(
    RandomBuffer As IntPtr,   ' void* out
    RandomBufferLength As UInteger   ' DWORD
) As Byte
End Function
' RandomBuffer : void* out
' RandomBufferLength : DWORD
Declare PtrSafe Function RtlGenRandom Lib "advapi32" Alias "SystemFunction036" ( _
    ByVal RandomBuffer As LongPtr, _
    ByVal RandomBufferLength As Long) As Byte
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

RtlGenRandom = ctypes.windll.advapi32.RtlGenRandom
RtlGenRandom.restype = ctypes.c_byte
RtlGenRandom.argtypes = [
    ctypes.POINTER(None),  # RandomBuffer : void* out
    wintypes.DWORD,  # RandomBufferLength : DWORD
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('ADVAPI32.dll')
RtlGenRandom = Fiddle::Function.new(
  lib['SystemFunction036'],
  [
    Fiddle::TYPE_VOIDP,  # RandomBuffer : void* out
    -Fiddle::TYPE_INT,  # RandomBufferLength : DWORD
  ],
  Fiddle::TYPE_CHAR)
#[link(name = "advapi32")]
extern "system" {
    fn RtlGenRandom(
        RandomBuffer: *mut (),  // void* out
        RandomBufferLength: u32  // DWORD
    ) -> u8;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("ADVAPI32.dll")]
public static extern byte RtlGenRandom(IntPtr RandomBuffer, uint RandomBufferLength);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ADVAPI32_RtlGenRandom' -Namespace Win32 -PassThru
# $api::RtlGenRandom(RandomBuffer, RandomBufferLength)
#uselib "ADVAPI32.dll"
#func global RtlGenRandom "SystemFunction036" sptr, sptr
; RtlGenRandom RandomBuffer, RandomBufferLength   ; 戻り値は stat
; RandomBuffer : void* out -> "sptr"
; RandomBufferLength : DWORD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "ADVAPI32.dll"
#cfunc global RtlGenRandom "SystemFunction036" sptr, int
; res = RtlGenRandom(RandomBuffer, RandomBufferLength)
; RandomBuffer : void* out -> "sptr"
; RandomBufferLength : DWORD -> "int"
; BOOLEAN RtlGenRandom(void* RandomBuffer, DWORD RandomBufferLength)
#uselib "ADVAPI32.dll"
#cfunc global RtlGenRandom "SystemFunction036" intptr, int
; res = RtlGenRandom(RandomBuffer, RandomBufferLength)
; RandomBuffer : void* out -> "intptr"
; RandomBufferLength : DWORD -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	advapi32 = windows.NewLazySystemDLL("ADVAPI32.dll")
	procRtlGenRandom = advapi32.NewProc("SystemFunction036")
)

// RandomBuffer (void* out), RandomBufferLength (DWORD)
r1, _, err := procRtlGenRandom.Call(
	uintptr(RandomBuffer),
	uintptr(RandomBufferLength),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOLEAN
function RtlGenRandom(
  RandomBuffer: Pointer;   // void* out
  RandomBufferLength: DWORD   // DWORD
): ByteBool; stdcall;
  external 'ADVAPI32.dll' name 'SystemFunction036';
result := DllCall("ADVAPI32\SystemFunction036"
    , "Ptr", RandomBuffer   ; void* out
    , "UInt", RandomBufferLength   ; DWORD
    , "Char")   ; return: BOOLEAN
●RtlGenRandom(RandomBuffer, RandomBufferLength) = DLL("ADVAPI32.dll", "byte SystemFunction036(void*, dword)")
# 呼び出し: RtlGenRandom(RandomBuffer, RandomBufferLength)
# RandomBuffer : void* out -> "void*"
# RandomBufferLength : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。