DeviceIoControl
関数デバイスドライバに制御コードを送って操作する。
シグネチャ
// KERNEL32.dll
#include <windows.h>
BOOL DeviceIoControl(
HANDLE hDevice,
DWORD dwIoControlCode,
void* lpInBuffer, // optional
DWORD nInBufferSize,
void* lpOutBuffer, // optional
DWORD nOutBufferSize,
DWORD* lpBytesReturned, // optional
OVERLAPPED* lpOverlapped // optional
);パラメーター
| 名前 | 型 | 方向 |
|---|---|---|
| hDevice | HANDLE | in |
| dwIoControlCode | DWORD | in |
| lpInBuffer | void* | inoptional |
| nInBufferSize | DWORD | in |
| lpOutBuffer | void* | outoptional |
| nOutBufferSize | DWORD | in |
| lpBytesReturned | DWORD* | outoptional |
| lpOverlapped | OVERLAPPED* | inoutoptional |
戻り値の型: BOOL
各言語での呼び出し定義
// KERNEL32.dll
#include <windows.h>
BOOL DeviceIoControl(
HANDLE hDevice,
DWORD dwIoControlCode,
void* lpInBuffer, // optional
DWORD nInBufferSize,
void* lpOutBuffer, // optional
DWORD nOutBufferSize,
DWORD* lpBytesReturned, // optional
OVERLAPPED* lpOverlapped // optional
);[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool DeviceIoControl(
IntPtr hDevice, // HANDLE
uint dwIoControlCode, // DWORD
IntPtr lpInBuffer, // void* optional
uint nInBufferSize, // DWORD
IntPtr lpOutBuffer, // void* optional, out
uint nOutBufferSize, // DWORD
IntPtr lpBytesReturned, // DWORD* optional, out
IntPtr lpOverlapped // OVERLAPPED* optional, in/out
);<DllImport("KERNEL32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function DeviceIoControl(
hDevice As IntPtr, ' HANDLE
dwIoControlCode As UInteger, ' DWORD
lpInBuffer As IntPtr, ' void* optional
nInBufferSize As UInteger, ' DWORD
lpOutBuffer As IntPtr, ' void* optional, out
nOutBufferSize As UInteger, ' DWORD
lpBytesReturned As IntPtr, ' DWORD* optional, out
lpOverlapped As IntPtr ' OVERLAPPED* optional, in/out
) As Boolean
End Function' hDevice : HANDLE
' dwIoControlCode : DWORD
' lpInBuffer : void* optional
' nInBufferSize : DWORD
' lpOutBuffer : void* optional, out
' nOutBufferSize : DWORD
' lpBytesReturned : DWORD* optional, out
' lpOverlapped : OVERLAPPED* optional, in/out
Declare PtrSafe Function DeviceIoControl Lib "kernel32" ( _
ByVal hDevice As LongPtr, _
ByVal dwIoControlCode As Long, _
ByVal lpInBuffer As LongPtr, _
ByVal nInBufferSize As Long, _
ByVal lpOutBuffer As LongPtr, _
ByVal nOutBufferSize As Long, _
ByVal lpBytesReturned As LongPtr, _
ByVal lpOverlapped As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
DeviceIoControl = ctypes.windll.kernel32.DeviceIoControl
DeviceIoControl.restype = wintypes.BOOL
DeviceIoControl.argtypes = [
wintypes.HANDLE, # hDevice : HANDLE
wintypes.DWORD, # dwIoControlCode : DWORD
ctypes.POINTER(None), # lpInBuffer : void* optional
wintypes.DWORD, # nInBufferSize : DWORD
ctypes.POINTER(None), # lpOutBuffer : void* optional, out
wintypes.DWORD, # nOutBufferSize : DWORD
ctypes.POINTER(wintypes.DWORD), # lpBytesReturned : DWORD* optional, out
ctypes.c_void_p, # lpOverlapped : OVERLAPPED* optional, in/out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('KERNEL32.dll')
DeviceIoControl = Fiddle::Function.new(
lib['DeviceIoControl'],
[
Fiddle::TYPE_VOIDP, # hDevice : HANDLE
-Fiddle::TYPE_INT, # dwIoControlCode : DWORD
Fiddle::TYPE_VOIDP, # lpInBuffer : void* optional
-Fiddle::TYPE_INT, # nInBufferSize : DWORD
Fiddle::TYPE_VOIDP, # lpOutBuffer : void* optional, out
-Fiddle::TYPE_INT, # nOutBufferSize : DWORD
Fiddle::TYPE_VOIDP, # lpBytesReturned : DWORD* optional, out
Fiddle::TYPE_VOIDP, # lpOverlapped : OVERLAPPED* optional, in/out
],
Fiddle::TYPE_INT)#[link(name = "kernel32")]
extern "system" {
fn DeviceIoControl(
hDevice: *mut core::ffi::c_void, // HANDLE
dwIoControlCode: u32, // DWORD
lpInBuffer: *mut (), // void* optional
nInBufferSize: u32, // DWORD
lpOutBuffer: *mut (), // void* optional, out
nOutBufferSize: u32, // DWORD
lpBytesReturned: *mut u32, // DWORD* optional, out
lpOverlapped: *mut OVERLAPPED // OVERLAPPED* optional, in/out
) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", SetLastError = true)]
public static extern bool DeviceIoControl(IntPtr hDevice, uint dwIoControlCode, IntPtr lpInBuffer, uint nInBufferSize, IntPtr lpOutBuffer, uint nOutBufferSize, IntPtr lpBytesReturned, IntPtr lpOverlapped);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_DeviceIoControl' -Namespace Win32 -PassThru
# $api::DeviceIoControl(hDevice, dwIoControlCode, lpInBuffer, nInBufferSize, lpOutBuffer, nOutBufferSize, lpBytesReturned, lpOverlapped)#uselib "KERNEL32.dll"
#func global DeviceIoControl "DeviceIoControl" sptr, sptr, sptr, sptr, sptr, sptr, sptr, sptr
; DeviceIoControl hDevice, dwIoControlCode, lpInBuffer, nInBufferSize, lpOutBuffer, nOutBufferSize, varptr(lpBytesReturned), varptr(lpOverlapped) ; 戻り値は stat
; hDevice : HANDLE -> "sptr"
; dwIoControlCode : DWORD -> "sptr"
; lpInBuffer : void* optional -> "sptr"
; nInBufferSize : DWORD -> "sptr"
; lpOutBuffer : void* optional, out -> "sptr"
; nOutBufferSize : DWORD -> "sptr"
; lpBytesReturned : DWORD* optional, out -> "sptr"
; lpOverlapped : OVERLAPPED* optional, in/out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "KERNEL32.dll" #cfunc global DeviceIoControl "DeviceIoControl" sptr, int, sptr, int, sptr, int, var, var ; res = DeviceIoControl(hDevice, dwIoControlCode, lpInBuffer, nInBufferSize, lpOutBuffer, nOutBufferSize, lpBytesReturned, lpOverlapped) ; hDevice : HANDLE -> "sptr" ; dwIoControlCode : DWORD -> "int" ; lpInBuffer : void* optional -> "sptr" ; nInBufferSize : DWORD -> "int" ; lpOutBuffer : void* optional, out -> "sptr" ; nOutBufferSize : DWORD -> "int" ; lpBytesReturned : DWORD* optional, out -> "var" ; lpOverlapped : OVERLAPPED* optional, in/out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "KERNEL32.dll" #cfunc global DeviceIoControl "DeviceIoControl" sptr, int, sptr, int, sptr, int, sptr, sptr ; res = DeviceIoControl(hDevice, dwIoControlCode, lpInBuffer, nInBufferSize, lpOutBuffer, nOutBufferSize, varptr(lpBytesReturned), varptr(lpOverlapped)) ; hDevice : HANDLE -> "sptr" ; dwIoControlCode : DWORD -> "int" ; lpInBuffer : void* optional -> "sptr" ; nInBufferSize : DWORD -> "int" ; lpOutBuffer : void* optional, out -> "sptr" ; nOutBufferSize : DWORD -> "int" ; lpBytesReturned : DWORD* optional, out -> "sptr" ; lpOverlapped : OVERLAPPED* optional, in/out -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; BOOL DeviceIoControl(HANDLE hDevice, DWORD dwIoControlCode, void* lpInBuffer, DWORD nInBufferSize, void* lpOutBuffer, DWORD nOutBufferSize, DWORD* lpBytesReturned, OVERLAPPED* lpOverlapped) #uselib "KERNEL32.dll" #cfunc global DeviceIoControl "DeviceIoControl" intptr, int, intptr, int, intptr, int, var, var ; res = DeviceIoControl(hDevice, dwIoControlCode, lpInBuffer, nInBufferSize, lpOutBuffer, nOutBufferSize, lpBytesReturned, lpOverlapped) ; hDevice : HANDLE -> "intptr" ; dwIoControlCode : DWORD -> "int" ; lpInBuffer : void* optional -> "intptr" ; nInBufferSize : DWORD -> "int" ; lpOutBuffer : void* optional, out -> "intptr" ; nOutBufferSize : DWORD -> "int" ; lpBytesReturned : DWORD* optional, out -> "var" ; lpOverlapped : OVERLAPPED* optional, in/out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; BOOL DeviceIoControl(HANDLE hDevice, DWORD dwIoControlCode, void* lpInBuffer, DWORD nInBufferSize, void* lpOutBuffer, DWORD nOutBufferSize, DWORD* lpBytesReturned, OVERLAPPED* lpOverlapped) #uselib "KERNEL32.dll" #cfunc global DeviceIoControl "DeviceIoControl" intptr, int, intptr, int, intptr, int, intptr, intptr ; res = DeviceIoControl(hDevice, dwIoControlCode, lpInBuffer, nInBufferSize, lpOutBuffer, nOutBufferSize, varptr(lpBytesReturned), varptr(lpOverlapped)) ; hDevice : HANDLE -> "intptr" ; dwIoControlCode : DWORD -> "int" ; lpInBuffer : void* optional -> "intptr" ; nInBufferSize : DWORD -> "int" ; lpOutBuffer : void* optional, out -> "intptr" ; nOutBufferSize : DWORD -> "int" ; lpBytesReturned : DWORD* optional, out -> "intptr" ; lpOverlapped : OVERLAPPED* optional, in/out -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
procDeviceIoControl = kernel32.NewProc("DeviceIoControl")
)
// hDevice (HANDLE), dwIoControlCode (DWORD), lpInBuffer (void* optional), nInBufferSize (DWORD), lpOutBuffer (void* optional, out), nOutBufferSize (DWORD), lpBytesReturned (DWORD* optional, out), lpOverlapped (OVERLAPPED* optional, in/out)
r1, _, err := procDeviceIoControl.Call(
uintptr(hDevice),
uintptr(dwIoControlCode),
uintptr(lpInBuffer),
uintptr(nInBufferSize),
uintptr(lpOutBuffer),
uintptr(nOutBufferSize),
uintptr(lpBytesReturned),
uintptr(lpOverlapped),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // BOOLfunction DeviceIoControl(
hDevice: THandle; // HANDLE
dwIoControlCode: DWORD; // DWORD
lpInBuffer: Pointer; // void* optional
nInBufferSize: DWORD; // DWORD
lpOutBuffer: Pointer; // void* optional, out
nOutBufferSize: DWORD; // DWORD
lpBytesReturned: Pointer; // DWORD* optional, out
lpOverlapped: Pointer // OVERLAPPED* optional, in/out
): BOOL; stdcall;
external 'KERNEL32.dll' name 'DeviceIoControl';result := DllCall("KERNEL32\DeviceIoControl"
, "Ptr", hDevice ; HANDLE
, "UInt", dwIoControlCode ; DWORD
, "Ptr", lpInBuffer ; void* optional
, "UInt", nInBufferSize ; DWORD
, "Ptr", lpOutBuffer ; void* optional, out
, "UInt", nOutBufferSize ; DWORD
, "Ptr", lpBytesReturned ; DWORD* optional, out
, "Ptr", lpOverlapped ; OVERLAPPED* optional, in/out
, "Int") ; return: BOOL●DeviceIoControl(hDevice, dwIoControlCode, lpInBuffer, nInBufferSize, lpOutBuffer, nOutBufferSize, lpBytesReturned, lpOverlapped) = DLL("KERNEL32.dll", "bool DeviceIoControl(void*, dword, void*, dword, void*, dword, void*, void*)")
# 呼び出し: DeviceIoControl(hDevice, dwIoControlCode, lpInBuffer, nInBufferSize, lpOutBuffer, nOutBufferSize, lpBytesReturned, lpOverlapped)
# hDevice : HANDLE -> "void*"
# dwIoControlCode : DWORD -> "dword"
# lpInBuffer : void* optional -> "void*"
# nInBufferSize : DWORD -> "dword"
# lpOutBuffer : void* optional, out -> "void*"
# nOutBufferSize : DWORD -> "dword"
# lpBytesReturned : DWORD* optional, out -> "void*"
# lpOverlapped : OVERLAPPED* optional, in/out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。