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

_lwrite

関数
ファイルへ指定バイト数を書き込む旧式関数。
DLLKERNEL32.dll呼出規約winapiSetLastErrorあり

シグネチャ

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

DWORD _lwrite(
    INT hFile,
    LPCSTR lpBuffer,
    DWORD uBytes
);

パラメーター

名前方向説明
hFileINTin書き込み先のファイルハンドル。
lpBufferLPCSTRin書き込むデータが入ったバッファへのポインタ。
uBytesDWORDin書き込むバイト数。

戻り値の型: DWORD

各言語での呼び出し定義

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

DWORD _lwrite(
    INT hFile,
    LPCSTR lpBuffer,
    DWORD uBytes
);
[DllImport("KERNEL32.dll", SetLastError = true, ExactSpelling = true)]
static extern uint _lwrite(
    int hFile,   // INT
    [MarshalAs(UnmanagedType.LPStr)] string lpBuffer,   // LPCSTR
    uint uBytes   // DWORD
);
<DllImport("KERNEL32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function _lwrite(
    hFile As Integer,   ' INT
    <MarshalAs(UnmanagedType.LPStr)> lpBuffer As String,   ' LPCSTR
    uBytes As UInteger   ' DWORD
) As UInteger
End Function
' hFile : INT
' lpBuffer : LPCSTR
' uBytes : DWORD
Declare PtrSafe Function _lwrite Lib "kernel32" ( _
    ByVal hFile As Long, _
    ByVal lpBuffer As String, _
    ByVal uBytes As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

_lwrite = ctypes.windll.kernel32._lwrite
_lwrite.restype = wintypes.DWORD
_lwrite.argtypes = [
    ctypes.c_int,  # hFile : INT
    wintypes.LPCSTR,  # lpBuffer : LPCSTR
    wintypes.DWORD,  # uBytes : DWORD
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('KERNEL32.dll')
_lwrite = Fiddle::Function.new(
  lib['_lwrite'],
  [
    Fiddle::TYPE_INT,  # hFile : INT
    Fiddle::TYPE_VOIDP,  # lpBuffer : LPCSTR
    -Fiddle::TYPE_INT,  # uBytes : DWORD
  ],
  -Fiddle::TYPE_INT)
#[link(name = "kernel32")]
extern "system" {
    fn _lwrite(
        hFile: i32,  // INT
        lpBuffer: *const u8,  // LPCSTR
        uBytes: u32  // DWORD
    ) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("KERNEL32.dll", SetLastError = true)]
public static extern uint _lwrite(int hFile, [MarshalAs(UnmanagedType.LPStr)] string lpBuffer, uint uBytes);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32__lwrite' -Namespace Win32 -PassThru
# $api::_lwrite(hFile, lpBuffer, uBytes)
#uselib "KERNEL32.dll"
#func global _lwrite "_lwrite" sptr, sptr, sptr
; _lwrite hFile, lpBuffer, uBytes   ; 戻り値は stat
; hFile : INT -> "sptr"
; lpBuffer : LPCSTR -> "sptr"
; uBytes : DWORD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "KERNEL32.dll"
#cfunc global _lwrite "_lwrite" int, str, int
; res = _lwrite(hFile, lpBuffer, uBytes)
; hFile : INT -> "int"
; lpBuffer : LPCSTR -> "str"
; uBytes : DWORD -> "int"
; DWORD _lwrite(INT hFile, LPCSTR lpBuffer, DWORD uBytes)
#uselib "KERNEL32.dll"
#cfunc global _lwrite "_lwrite" int, str, int
; res = _lwrite(hFile, lpBuffer, uBytes)
; hFile : INT -> "int"
; lpBuffer : LPCSTR -> "str"
; uBytes : DWORD -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	proc_lwrite = kernel32.NewProc("_lwrite")
)

// hFile (INT), lpBuffer (LPCSTR), uBytes (DWORD)
r1, _, err := proc_lwrite.Call(
	uintptr(hFile),
	uintptr(unsafe.Pointer(windows.BytePtrFromString(lpBuffer))),
	uintptr(uBytes),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // DWORD
function _lwrite(
  hFile: Integer;   // INT
  lpBuffer: PAnsiChar;   // LPCSTR
  uBytes: DWORD   // DWORD
): DWORD; stdcall;
  external 'KERNEL32.dll' name '_lwrite';
result := DllCall("KERNEL32\_lwrite"
    , "Int", hFile   ; INT
    , "AStr", lpBuffer   ; LPCSTR
    , "UInt", uBytes   ; DWORD
    , "UInt")   ; return: DWORD
●_lwrite(hFile, lpBuffer, uBytes) = DLL("KERNEL32.dll", "dword _lwrite(int, char*, dword)")
# 呼び出し: _lwrite(hFile, lpBuffer, uBytes)
# hFile : INT -> "int"
# lpBuffer : LPCSTR -> "char*"
# uBytes : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "kernel32" fn _lwrite(
    hFile: i32, // INT
    lpBuffer: [*c]const u8, // LPCSTR
    uBytes: u32 // DWORD
) callconv(std.os.windows.WINAPI) u32;
proc _lwrite(
    hFile: int32,  # INT
    lpBuffer: cstring,  # LPCSTR
    uBytes: uint32  # DWORD
): uint32 {.importc: "_lwrite", stdcall, dynlib: "KERNEL32.dll".}
pragma(lib, "kernel32");
extern(Windows)
uint _lwrite(
    int hFile,   // INT
    const(char)* lpBuffer,   // LPCSTR
    uint uBytes   // DWORD
);
ccall((:_lwrite, "KERNEL32.dll"), stdcall, UInt32,
      (Int32, Cstring, UInt32),
      hFile, lpBuffer, uBytes)
# hFile : INT -> Int32
# lpBuffer : LPCSTR -> Cstring
# uBytes : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
uint32_t _lwrite(
    int32_t hFile,
    const char* lpBuffer,
    uint32_t uBytes);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32._lwrite(hFile, lpBuffer, uBytes)
-- hFile : INT
-- lpBuffer : LPCSTR
-- uBytes : DWORD
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('KERNEL32.dll');
const _lwrite = lib.func('__stdcall', '_lwrite', 'uint32_t', ['int32_t', 'str', 'uint32_t']);
// _lwrite(hFile, lpBuffer, uBytes)
// hFile : INT -> 'int32_t'
// lpBuffer : LPCSTR -> 'str'
// uBytes : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("KERNEL32.dll", {
  _lwrite: { parameters: ["i32", "buffer", "u32"], result: "u32" },
});
// lib.symbols._lwrite(hFile, lpBuffer, uBytes)
// hFile : INT -> "i32"
// lpBuffer : LPCSTR -> "buffer"
// uBytes : DWORD -> "u32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
uint32_t _lwrite(
    int32_t hFile,
    const char* lpBuffer,
    uint32_t uBytes);
C, "KERNEL32.dll");
// $ffi->_lwrite(hFile, lpBuffer, uBytes);
// hFile : INT
// lpBuffer : LPCSTR
// uBytes : DWORD
// 構造体/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);
    int _lwrite(
        int hFile,   // INT
        String lpBuffer,   // LPCSTR
        int uBytes   // DWORD
    );
}
@[Link("kernel32")]
lib LibKERNEL32
  fun _lwrite = _lwrite(
    hFile : Int32,   # INT
    lpBuffer : UInt8*,   # LPCSTR
    uBytes : UInt32   # DWORD
  ) : UInt32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef _lwriteNative = Uint32 Function(Int32, Pointer<Utf8>, Uint32);
typedef _lwriteDart = int Function(int, Pointer<Utf8>, int);
final _lwrite = DynamicLibrary.open('KERNEL32.dll')
    .lookupFunction<_lwriteNative, _lwriteDart>('_lwrite');
// hFile : INT -> Int32
// lpBuffer : LPCSTR -> Pointer<Utf8>
// uBytes : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function _lwrite(
  hFile: Integer;   // INT
  lpBuffer: PAnsiChar;   // LPCSTR
  uBytes: DWORD   // DWORD
): DWORD; stdcall;
  external 'KERNEL32.dll' name '_lwrite';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "_lwrite"
  c__lwrite :: Int32 -> CString -> Word32 -> IO Word32
-- hFile : INT -> Int32
-- lpBuffer : LPCSTR -> CString
-- uBytes : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let _lwrite =
  foreign "_lwrite"
    (int32_t @-> string @-> uint32_t @-> returning uint32_t)
(* hFile : INT -> int32_t *)
(* lpBuffer : LPCSTR -> string *)
(* uBytes : DWORD -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)

(cffi:defcfun ("_lwrite" -lwrite :convention :stdcall) :uint32
  (h-file :int32)   ; INT
  (lp-buffer :string)   ; LPCSTR
  (u-bytes :uint32))   ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $_lwrite = Win32::API::More->new('KERNEL32',
    'DWORD _lwrite(int hFile, LPCSTR lpBuffer, DWORD uBytes)');
# my $ret = $_lwrite->Call($hFile, $lpBuffer, $uBytes);
# hFile : INT -> int
# lpBuffer : LPCSTR -> LPCSTR
# uBytes : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。