Win32 API 日本語リファレンス
ホームGlobalization › IsDBCSLeadByte

IsDBCSLeadByte

関数
指定バイトがDBCS先頭バイトか既定コードページで判定する。
DLLKERNEL32.dll呼出規約winapiSetLastErrorあり対応OSWindows 2000 以降

シグネチャ

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

BOOL IsDBCSLeadByte(
    BYTE TestChar
);

パラメーター

名前方向説明
TestCharBYTEinテストする文字。

戻り値の型: BOOL

公式ドキュメント

指定した文字が、システム既定の Windows ANSI コードページ (CP_ACP) における先行バイトかどうかを判定します。先行バイトとは、そのコードページのダブルバイト文字セット (DBCS) における 2 バイト文字の先頭バイトを指します。

戻り値

テスト対象の文字が先行バイトである可能性がある場合は、0 以外の値を返します。テスト対象の文字が先行バイトでない場合、または単一バイト文字である場合は 0 を返します。拡張エラー情報を取得するには、アプリケーションは GetLastError を呼び出すことができます。

解説(Remarks)

注意 この関数は後続バイトの有無や妥当性を検証しません。そのため、MultiByteToWideChar は、IsDBCSLeadByte を使用するアプリケーションが先行バイトとして報告したシーケンスを認識しない場合があります。アプリケーションは MultiByteToWideChar の結果と容易に同期がとれなくなり、予期しないエラーやバッファーサイズの不一致を引き起こす可能性があります。
一般に、コードページデータを低レベルで操作しようとするのではなく、アプリケーションは MultiByteToWideChar を使用してデータを UTF-16 に変換し、そのエンコーディングで処理すべきです。

先行バイトの値は、個々の DBCS ごとに固有です。一部のバイト値は、単一のコードページ内で DBCS 文字の先行バイトと後続バイトの両方として出現することがあります。

DBCS 文字列を解釈するには、アプリケーションは通常、文字列の先頭から開始して前方へスキャンし、先行バイトに遭遇したことを記録しながら、次のバイトを同じ文字の後続部分として扱います。アプリケーションが後戻りする必要がある場合は、独自のアルゴリズムを構築しようとするのではなく、CharPrev を使用すべきです。

出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

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

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

IsDBCSLeadByte = ctypes.windll.kernel32.IsDBCSLeadByte
IsDBCSLeadByte.restype = wintypes.BOOL
IsDBCSLeadByte.argtypes = [
    ctypes.c_ubyte,  # TestChar : BYTE
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('KERNEL32.dll')
IsDBCSLeadByte = Fiddle::Function.new(
  lib['IsDBCSLeadByte'],
  [
    -Fiddle::TYPE_CHAR,  # TestChar : BYTE
  ],
  Fiddle::TYPE_INT)
#[link(name = "kernel32")]
extern "system" {
    fn IsDBCSLeadByte(
        TestChar: u8  // BYTE
    ) -> 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 IsDBCSLeadByte(byte TestChar);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_IsDBCSLeadByte' -Namespace Win32 -PassThru
# $api::IsDBCSLeadByte(TestChar)
#uselib "KERNEL32.dll"
#func global IsDBCSLeadByte "IsDBCSLeadByte" sptr
; IsDBCSLeadByte TestChar   ; 戻り値は stat
; TestChar : BYTE -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "KERNEL32.dll"
#cfunc global IsDBCSLeadByte "IsDBCSLeadByte" int
; res = IsDBCSLeadByte(TestChar)
; TestChar : BYTE -> "int"
; BOOL IsDBCSLeadByte(BYTE TestChar)
#uselib "KERNEL32.dll"
#cfunc global IsDBCSLeadByte "IsDBCSLeadByte" int
; res = IsDBCSLeadByte(TestChar)
; TestChar : BYTE -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procIsDBCSLeadByte = kernel32.NewProc("IsDBCSLeadByte")
)

// TestChar (BYTE)
r1, _, err := procIsDBCSLeadByte.Call(
	uintptr(TestChar),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function IsDBCSLeadByte(
  TestChar: Byte   // BYTE
): BOOL; stdcall;
  external 'KERNEL32.dll' name 'IsDBCSLeadByte';
result := DllCall("KERNEL32\IsDBCSLeadByte"
    , "UChar", TestChar   ; BYTE
    , "Int")   ; return: BOOL
●IsDBCSLeadByte(TestChar) = DLL("KERNEL32.dll", "bool IsDBCSLeadByte(byte)")
# 呼び出し: IsDBCSLeadByte(TestChar)
# TestChar : BYTE -> "byte"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "kernel32" fn IsDBCSLeadByte(
    TestChar: u8 // BYTE
) callconv(std.os.windows.WINAPI) i32;
proc IsDBCSLeadByte(
    TestChar: uint8  # BYTE
): int32 {.importc: "IsDBCSLeadByte", stdcall, dynlib: "KERNEL32.dll".}
pragma(lib, "kernel32");
extern(Windows)
int IsDBCSLeadByte(
    ubyte TestChar   // BYTE
);
ccall((:IsDBCSLeadByte, "KERNEL32.dll"), stdcall, Int32,
      (UInt8,),
      TestChar)
# TestChar : BYTE -> UInt8
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t IsDBCSLeadByte(
    uint8_t TestChar);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.IsDBCSLeadByte(TestChar)
-- TestChar : BYTE
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('KERNEL32.dll');
const IsDBCSLeadByte = lib.func('__stdcall', 'IsDBCSLeadByte', 'int32_t', ['uint8_t']);
// IsDBCSLeadByte(TestChar)
// TestChar : BYTE -> 'uint8_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("KERNEL32.dll", {
  IsDBCSLeadByte: { parameters: ["u8"], result: "i32" },
});
// lib.symbols.IsDBCSLeadByte(TestChar)
// TestChar : BYTE -> "u8"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t IsDBCSLeadByte(
    uint8_t TestChar);
C, "KERNEL32.dll");
// $ffi->IsDBCSLeadByte(TestChar);
// TestChar : 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 Kernel32 extends StdCallLibrary {
    Kernel32 INSTANCE = Native.load("kernel32", Kernel32.class);
    boolean IsDBCSLeadByte(
        byte TestChar   // BYTE
    );
}
@[Link("kernel32")]
lib LibKERNEL32
  fun IsDBCSLeadByte = IsDBCSLeadByte(
    TestChar : 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 IsDBCSLeadByteNative = Int32 Function(Uint8);
typedef IsDBCSLeadByteDart = int Function(int);
final IsDBCSLeadByte = DynamicLibrary.open('KERNEL32.dll')
    .lookupFunction<IsDBCSLeadByteNative, IsDBCSLeadByteDart>('IsDBCSLeadByte');
// TestChar : BYTE -> Uint8
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function IsDBCSLeadByte(
  TestChar: Byte   // BYTE
): BOOL; stdcall;
  external 'KERNEL32.dll' name 'IsDBCSLeadByte';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "IsDBCSLeadByte"
  c_IsDBCSLeadByte :: Word8 -> IO CInt
-- TestChar : BYTE -> Word8
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let isdbcsleadbyte =
  foreign "IsDBCSLeadByte"
    (uint8_t @-> returning int32_t)
(* TestChar : BYTE -> uint8_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)

(cffi:defcfun ("IsDBCSLeadByte" is-dbcslead-byte :convention :stdcall) :int32
  (test-char :uint8))   ; BYTE
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $IsDBCSLeadByte = Win32::API::More->new('KERNEL32',
    'BOOL IsDBCSLeadByte(BYTE TestChar)');
# my $ret = $IsDBCSLeadByte->Call($TestChar);
# TestChar : BYTE -> BYTE
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

類似 API
公式の関連項目