Win32 API 日本語リファレンス
ホームDevices.Communication › SetCommTimeouts

SetCommTimeouts

関数
通信デバイスの送受信タイムアウト設定を構成する。
DLLKERNEL32.dll呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

BOOL SetCommTimeouts(
    HANDLE hFile,
    COMMTIMEOUTS* lpCommTimeouts
);

パラメーター

名前方向
hFileHANDLEin
lpCommTimeoutsCOMMTIMEOUTS*in

戻り値の型: BOOL

各言語での呼び出し定義

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

BOOL SetCommTimeouts(
    HANDLE hFile,
    COMMTIMEOUTS* lpCommTimeouts
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool SetCommTimeouts(
    IntPtr hFile,   // HANDLE
    IntPtr lpCommTimeouts   // COMMTIMEOUTS*
);
<DllImport("KERNEL32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function SetCommTimeouts(
    hFile As IntPtr,   ' HANDLE
    lpCommTimeouts As IntPtr   ' COMMTIMEOUTS*
) As Boolean
End Function
' hFile : HANDLE
' lpCommTimeouts : COMMTIMEOUTS*
Declare PtrSafe Function SetCommTimeouts Lib "kernel32" ( _
    ByVal hFile As LongPtr, _
    ByVal lpCommTimeouts As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

SetCommTimeouts = ctypes.windll.kernel32.SetCommTimeouts
SetCommTimeouts.restype = wintypes.BOOL
SetCommTimeouts.argtypes = [
    wintypes.HANDLE,  # hFile : HANDLE
    ctypes.c_void_p,  # lpCommTimeouts : COMMTIMEOUTS*
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('KERNEL32.dll')
SetCommTimeouts = Fiddle::Function.new(
  lib['SetCommTimeouts'],
  [
    Fiddle::TYPE_VOIDP,  # hFile : HANDLE
    Fiddle::TYPE_VOIDP,  # lpCommTimeouts : COMMTIMEOUTS*
  ],
  Fiddle::TYPE_INT)
#[link(name = "kernel32")]
extern "system" {
    fn SetCommTimeouts(
        hFile: *mut core::ffi::c_void,  // HANDLE
        lpCommTimeouts: *mut COMMTIMEOUTS  // COMMTIMEOUTS*
    ) -> 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 SetCommTimeouts(IntPtr hFile, IntPtr lpCommTimeouts);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_SetCommTimeouts' -Namespace Win32 -PassThru
# $api::SetCommTimeouts(hFile, lpCommTimeouts)
#uselib "KERNEL32.dll"
#func global SetCommTimeouts "SetCommTimeouts" sptr, sptr
; SetCommTimeouts hFile, varptr(lpCommTimeouts)   ; 戻り値は stat
; hFile : HANDLE -> "sptr"
; lpCommTimeouts : COMMTIMEOUTS* -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "KERNEL32.dll"
#cfunc global SetCommTimeouts "SetCommTimeouts" sptr, var
; res = SetCommTimeouts(hFile, lpCommTimeouts)
; hFile : HANDLE -> "sptr"
; lpCommTimeouts : COMMTIMEOUTS* -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL SetCommTimeouts(HANDLE hFile, COMMTIMEOUTS* lpCommTimeouts)
#uselib "KERNEL32.dll"
#cfunc global SetCommTimeouts "SetCommTimeouts" intptr, var
; res = SetCommTimeouts(hFile, lpCommTimeouts)
; hFile : HANDLE -> "intptr"
; lpCommTimeouts : COMMTIMEOUTS* -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procSetCommTimeouts = kernel32.NewProc("SetCommTimeouts")
)

// hFile (HANDLE), lpCommTimeouts (COMMTIMEOUTS*)
r1, _, err := procSetCommTimeouts.Call(
	uintptr(hFile),
	uintptr(lpCommTimeouts),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function SetCommTimeouts(
  hFile: THandle;   // HANDLE
  lpCommTimeouts: Pointer   // COMMTIMEOUTS*
): BOOL; stdcall;
  external 'KERNEL32.dll' name 'SetCommTimeouts';
result := DllCall("KERNEL32\SetCommTimeouts"
    , "Ptr", hFile   ; HANDLE
    , "Ptr", lpCommTimeouts   ; COMMTIMEOUTS*
    , "Int")   ; return: BOOL
●SetCommTimeouts(hFile, lpCommTimeouts) = DLL("KERNEL32.dll", "bool SetCommTimeouts(void*, void*)")
# 呼び出し: SetCommTimeouts(hFile, lpCommTimeouts)
# hFile : HANDLE -> "void*"
# lpCommTimeouts : COMMTIMEOUTS* -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。