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)。