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

GetCommState

関数
通信デバイスの現在の制御設定をDCB構造体に取得する。
DLLKERNEL32.dll呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

BOOL GetCommState(
    HANDLE hFile,
    DCB* lpDCB
);

パラメーター

名前方向
hFileHANDLEin
lpDCBDCB*out

戻り値の型: BOOL

各言語での呼び出し定義

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

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

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

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

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procGetCommState = kernel32.NewProc("GetCommState")
)

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