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

HdvWriteGuestMemory

関数
ゲスト物理メモリへデータを書き込む。
DLLvmdevicehost.dll呼出規約winapi

シグネチャ

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

HRESULT HdvWriteGuestMemory(
    void* requestor,
    ULONGLONG guestPhysicalAddress,
    DWORD byteCount,
    const BYTE* buffer
);

パラメーター

名前方向説明
requestorvoid*in操作を要求するデバイス等を表すリクエスタコンテキストへのポインタ。
guestPhysicalAddressULONGLONGin書き込みを開始するゲスト物理アドレスを指定する。
byteCountDWORDin書き込むバイト数を指定する。
bufferBYTE*in書き込むデータを格納した入力バッファへのポインタ。

戻り値の型: HRESULT

各言語での呼び出し定義

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

HRESULT HdvWriteGuestMemory(
    void* requestor,
    ULONGLONG guestPhysicalAddress,
    DWORD byteCount,
    const BYTE* buffer
);
[DllImport("vmdevicehost.dll", ExactSpelling = true)]
static extern int HdvWriteGuestMemory(
    IntPtr requestor,   // void*
    ulong guestPhysicalAddress,   // ULONGLONG
    uint byteCount,   // DWORD
    IntPtr buffer   // BYTE*
);
<DllImport("vmdevicehost.dll", ExactSpelling:=True)>
Public Shared Function HdvWriteGuestMemory(
    requestor As IntPtr,   ' void*
    guestPhysicalAddress As ULong,   ' ULONGLONG
    byteCount As UInteger,   ' DWORD
    buffer As IntPtr   ' BYTE*
) As Integer
End Function
' requestor : void*
' guestPhysicalAddress : ULONGLONG
' byteCount : DWORD
' buffer : BYTE*
Declare PtrSafe Function HdvWriteGuestMemory Lib "vmdevicehost" ( _
    ByVal requestor As LongPtr, _
    ByVal guestPhysicalAddress As LongLong, _
    ByVal byteCount As Long, _
    ByVal buffer As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

HdvWriteGuestMemory = ctypes.windll.vmdevicehost.HdvWriteGuestMemory
HdvWriteGuestMemory.restype = ctypes.c_int
HdvWriteGuestMemory.argtypes = [
    ctypes.POINTER(None),  # requestor : void*
    ctypes.c_ulonglong,  # guestPhysicalAddress : ULONGLONG
    wintypes.DWORD,  # byteCount : DWORD
    ctypes.POINTER(ctypes.c_ubyte),  # buffer : BYTE*
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('vmdevicehost.dll')
HdvWriteGuestMemory = Fiddle::Function.new(
  lib['HdvWriteGuestMemory'],
  [
    Fiddle::TYPE_VOIDP,  # requestor : void*
    -Fiddle::TYPE_LONG_LONG,  # guestPhysicalAddress : ULONGLONG
    -Fiddle::TYPE_INT,  # byteCount : DWORD
    Fiddle::TYPE_VOIDP,  # buffer : BYTE*
  ],
  Fiddle::TYPE_INT)
#[link(name = "vmdevicehost")]
extern "system" {
    fn HdvWriteGuestMemory(
        requestor: *mut (),  // void*
        guestPhysicalAddress: u64,  // ULONGLONG
        byteCount: u32,  // DWORD
        buffer: *const u8  // BYTE*
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("vmdevicehost.dll")]
public static extern int HdvWriteGuestMemory(IntPtr requestor, ulong guestPhysicalAddress, uint byteCount, IntPtr buffer);
"@
$api = Add-Type -MemberDefinition $sig -Name 'vmdevicehost_HdvWriteGuestMemory' -Namespace Win32 -PassThru
# $api::HdvWriteGuestMemory(requestor, guestPhysicalAddress, byteCount, buffer)
#uselib "vmdevicehost.dll"
#func global HdvWriteGuestMemory "HdvWriteGuestMemory" sptr, sptr, sptr, sptr
; HdvWriteGuestMemory requestor, guestPhysicalAddress, byteCount, varptr(buffer)   ; 戻り値は stat
; requestor : void* -> "sptr"
; guestPhysicalAddress : ULONGLONG -> "sptr"
; byteCount : DWORD -> "sptr"
; buffer : BYTE* -> "sptr"
; ※HSP3.7は int64 引数(64bit値渡し)に非対応です。
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "vmdevicehost.dll"
#cfunc global HdvWriteGuestMemory "HdvWriteGuestMemory" sptr, int64, int, var
; res = HdvWriteGuestMemory(requestor, guestPhysicalAddress, byteCount, buffer)
; requestor : void* -> "sptr"
; guestPhysicalAddress : ULONGLONG -> "int64"
; byteCount : DWORD -> "int"
; buffer : BYTE* -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
; ※int64 引数の DLL 値渡しは x64 ランタイム(hsp3_64)のみ対応(x86 は未対応)。
出力引数:
; HRESULT HdvWriteGuestMemory(void* requestor, ULONGLONG guestPhysicalAddress, DWORD byteCount, BYTE* buffer)
#uselib "vmdevicehost.dll"
#cfunc global HdvWriteGuestMemory "HdvWriteGuestMemory" intptr, int64, int, var
; res = HdvWriteGuestMemory(requestor, guestPhysicalAddress, byteCount, buffer)
; requestor : void* -> "intptr"
; guestPhysicalAddress : ULONGLONG -> "int64"
; byteCount : DWORD -> "int"
; buffer : BYTE* -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	vmdevicehost = windows.NewLazySystemDLL("vmdevicehost.dll")
	procHdvWriteGuestMemory = vmdevicehost.NewProc("HdvWriteGuestMemory")
)

// requestor (void*), guestPhysicalAddress (ULONGLONG), byteCount (DWORD), buffer (BYTE*)
r1, _, err := procHdvWriteGuestMemory.Call(
	uintptr(requestor),
	uintptr(guestPhysicalAddress),
	uintptr(byteCount),
	uintptr(buffer),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // HRESULT
function HdvWriteGuestMemory(
  requestor: Pointer;   // void*
  guestPhysicalAddress: UInt64;   // ULONGLONG
  byteCount: DWORD;   // DWORD
  buffer: Pointer   // BYTE*
): Integer; stdcall;
  external 'vmdevicehost.dll' name 'HdvWriteGuestMemory';
result := DllCall("vmdevicehost\HdvWriteGuestMemory"
    , "Ptr", requestor   ; void*
    , "Int64", guestPhysicalAddress   ; ULONGLONG
    , "UInt", byteCount   ; DWORD
    , "Ptr", buffer   ; BYTE*
    , "Int")   ; return: HRESULT
●HdvWriteGuestMemory(requestor, guestPhysicalAddress, byteCount, buffer) = DLL("vmdevicehost.dll", "int HdvWriteGuestMemory(void*, qword, dword, void*)")
# 呼び出し: HdvWriteGuestMemory(requestor, guestPhysicalAddress, byteCount, buffer)
# requestor : void* -> "void*"
# guestPhysicalAddress : ULONGLONG -> "qword"
# byteCount : DWORD -> "dword"
# buffer : BYTE* -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "vmdevicehost" fn HdvWriteGuestMemory(
    requestor: ?*anyopaque, // void*
    guestPhysicalAddress: u64, // ULONGLONG
    byteCount: u32, // DWORD
    buffer: [*c]u8 // BYTE*
) callconv(std.os.windows.WINAPI) i32;
proc HdvWriteGuestMemory(
    requestor: pointer,  # void*
    guestPhysicalAddress: uint64,  # ULONGLONG
    byteCount: uint32,  # DWORD
    buffer: ptr uint8  # BYTE*
): int32 {.importc: "HdvWriteGuestMemory", stdcall, dynlib: "vmdevicehost.dll".}
pragma(lib, "vmdevicehost");
extern(Windows)
int HdvWriteGuestMemory(
    void* requestor,   // void*
    ulong guestPhysicalAddress,   // ULONGLONG
    uint byteCount,   // DWORD
    ubyte* buffer   // BYTE*
);
ccall((:HdvWriteGuestMemory, "vmdevicehost.dll"), stdcall, Int32,
      (Ptr{Cvoid}, UInt64, UInt32, Ptr{UInt8}),
      requestor, guestPhysicalAddress, byteCount, buffer)
# requestor : void* -> Ptr{Cvoid}
# guestPhysicalAddress : ULONGLONG -> UInt64
# byteCount : DWORD -> UInt32
# buffer : BYTE* -> Ptr{UInt8}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t HdvWriteGuestMemory(
    void* requestor,
    uint64_t guestPhysicalAddress,
    uint32_t byteCount,
    uint8_t* buffer);
]]
local vmdevicehost = ffi.load("vmdevicehost")
-- vmdevicehost.HdvWriteGuestMemory(requestor, guestPhysicalAddress, byteCount, buffer)
-- requestor : void*
-- guestPhysicalAddress : ULONGLONG
-- byteCount : DWORD
-- buffer : BYTE*
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('vmdevicehost.dll');
const HdvWriteGuestMemory = lib.func('__stdcall', 'HdvWriteGuestMemory', 'int32_t', ['void *', 'uint64_t', 'uint32_t', 'uint8_t *']);
// HdvWriteGuestMemory(requestor, guestPhysicalAddress, byteCount, buffer)
// requestor : void* -> 'void *'
// guestPhysicalAddress : ULONGLONG -> 'uint64_t'
// byteCount : DWORD -> 'uint32_t'
// buffer : BYTE* -> 'uint8_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("vmdevicehost.dll", {
  HdvWriteGuestMemory: { parameters: ["pointer", "u64", "u32", "pointer"], result: "i32" },
});
// lib.symbols.HdvWriteGuestMemory(requestor, guestPhysicalAddress, byteCount, buffer)
// requestor : void* -> "pointer"
// guestPhysicalAddress : ULONGLONG -> "u64"
// byteCount : DWORD -> "u32"
// buffer : BYTE* -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t HdvWriteGuestMemory(
    void* requestor,
    uint64_t guestPhysicalAddress,
    uint32_t byteCount,
    uint8_t* buffer);
C, "vmdevicehost.dll");
// $ffi->HdvWriteGuestMemory(requestor, guestPhysicalAddress, byteCount, buffer);
// requestor : void*
// guestPhysicalAddress : ULONGLONG
// byteCount : DWORD
// buffer : BYTE*
// 構造体/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 Vmdevicehost extends StdCallLibrary {
    Vmdevicehost INSTANCE = Native.load("vmdevicehost", Vmdevicehost.class);
    int HdvWriteGuestMemory(
        Pointer requestor,   // void*
        long guestPhysicalAddress,   // ULONGLONG
        int byteCount,   // DWORD
        byte[] buffer   // BYTE*
    );
}
@[Link("vmdevicehost")]
lib Libvmdevicehost
  fun HdvWriteGuestMemory = HdvWriteGuestMemory(
    requestor : Void*,   # void*
    guestPhysicalAddress : UInt64,   # ULONGLONG
    byteCount : UInt32,   # DWORD
    buffer : UInt8*   # BYTE*
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef HdvWriteGuestMemoryNative = Int32 Function(Pointer<Void>, Uint64, Uint32, Pointer<Uint8>);
typedef HdvWriteGuestMemoryDart = int Function(Pointer<Void>, int, int, Pointer<Uint8>);
final HdvWriteGuestMemory = DynamicLibrary.open('vmdevicehost.dll')
    .lookupFunction<HdvWriteGuestMemoryNative, HdvWriteGuestMemoryDart>('HdvWriteGuestMemory');
// requestor : void* -> Pointer<Void>
// guestPhysicalAddress : ULONGLONG -> Uint64
// byteCount : DWORD -> Uint32
// buffer : BYTE* -> Pointer<Uint8>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function HdvWriteGuestMemory(
  requestor: Pointer;   // void*
  guestPhysicalAddress: UInt64;   // ULONGLONG
  byteCount: DWORD;   // DWORD
  buffer: Pointer   // BYTE*
): Integer; stdcall;
  external 'vmdevicehost.dll' name 'HdvWriteGuestMemory';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "HdvWriteGuestMemory"
  c_HdvWriteGuestMemory :: Ptr () -> Word64 -> Word32 -> Ptr Word8 -> IO Int32
-- requestor : void* -> Ptr ()
-- guestPhysicalAddress : ULONGLONG -> Word64
-- byteCount : DWORD -> Word32
-- buffer : BYTE* -> Ptr Word8
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let hdvwriteguestmemory =
  foreign "HdvWriteGuestMemory"
    ((ptr void) @-> uint64_t @-> uint32_t @-> (ptr uint8_t) @-> returning int32_t)
(* requestor : void* -> (ptr void) *)
(* guestPhysicalAddress : ULONGLONG -> uint64_t *)
(* byteCount : DWORD -> uint32_t *)
(* buffer : BYTE* -> (ptr uint8_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library vmdevicehost (t "vmdevicehost.dll"))
(cffi:use-foreign-library vmdevicehost)

(cffi:defcfun ("HdvWriteGuestMemory" hdv-write-guest-memory :convention :stdcall) :int32
  (requestor :pointer)   ; void*
  (guest-physical-address :uint64)   ; ULONGLONG
  (byte-count :uint32)   ; DWORD
  (buffer :pointer))   ; BYTE*
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $HdvWriteGuestMemory = Win32::API::More->new('vmdevicehost',
    'int HdvWriteGuestMemory(LPVOID requestor, UINT64 guestPhysicalAddress, DWORD byteCount, LPVOID buffer)');
# my $ret = $HdvWriteGuestMemory->Call($requestor, $guestPhysicalAddress, $byteCount, $buffer);
# requestor : void* -> LPVOID
# guestPhysicalAddress : ULONGLONG -> UINT64
# byteCount : DWORD -> DWORD
# buffer : BYTE* -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。