Win32 API 日本語リファレンス
ホームStorage.FileSystem › UnlockFileEx

UnlockFileEx

関数
LockFileExでロックした範囲を解除する。
DLLKERNEL32.dll呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

BOOL UnlockFileEx(
    HANDLE hFile,
    DWORD dwReserved,   // optional
    DWORD nNumberOfBytesToUnlockLow,
    DWORD nNumberOfBytesToUnlockHigh,
    OVERLAPPED* lpOverlapped
);

パラメーター

名前方向説明
hFileHANDLEinファイルへのハンドル。このハンドルは、 GENERIC_READ または GENERIC_WRITE アクセス権のいずれかで作成されている必要があります。詳細については、 File Security and Access Rights を参照してください。
dwReservedDWORDoptional予約済みパラメーター。0 にする必要があります。
nNumberOfBytesToUnlockLowDWORDinロックを解除するバイト範囲の長さの下位部分。
nNumberOfBytesToUnlockHighDWORDinロックを解除するバイト範囲の長さの上位部分。
lpOverlappedOVERLAPPED*inoutこの関数がロック解除要求に使用する OVERLAPPED 構造体へのポインター。この構造体には、ロック解除 範囲の先頭のファイルオフセットが含まれます。hEvent メンバーは、有効なハンドルまたは 0 で初期化する必要があります。詳細については、 Synchronous and Asynchronous I/O を参照してください。

戻り値の型: BOOL

公式ドキュメント

指定されたファイル内の領域のロックを解除します。この関数は同期的にも非同期的にも動作できます。

戻り値

関数が成功した場合、戻り値は 0 以外の値です。

関数が失敗した場合、戻り値は 0 または NULL です。拡張エラー 情報を取得するには、GetLastError を呼び出してください。

解説(Remarks)

ファイルの領域のロックを解除すると、そのファイルに対して以前に取得したロックが解放されます。ロックを解除する領域は、 既存のロックされた領域と正確に一致する必要があります。ファイルの隣接する 2 つの領域を別々にロックしてから、 両方のロックされた領域にまたがる単一の領域を使用してロックを解除することはできません。

ロックは、CloseHandle 関数の 処理が完了する前に解放されます。

Windows 8 および Windows Server 2012 では、この関数は次のテクノロジでサポートされています。

テクノロジ サポート
Server Message Block (SMB) 3.0 プロトコル はい
SMB 3.0 Transparent Failover (TFO) はい
SMB 3.0 with Scale-out File Shares (SO) はい
Cluster Shared Volume File System (CsvFS) はい
Resilient File System (ReFS) はい
出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

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

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

UnlockFileEx = ctypes.windll.kernel32.UnlockFileEx
UnlockFileEx.restype = wintypes.BOOL
UnlockFileEx.argtypes = [
    wintypes.HANDLE,  # hFile : HANDLE
    wintypes.DWORD,  # dwReserved : DWORD optional
    wintypes.DWORD,  # nNumberOfBytesToUnlockLow : DWORD
    wintypes.DWORD,  # nNumberOfBytesToUnlockHigh : DWORD
    ctypes.c_void_p,  # lpOverlapped : OVERLAPPED* in/out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

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

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procUnlockFileEx = kernel32.NewProc("UnlockFileEx")
)

// hFile (HANDLE), dwReserved (DWORD optional), nNumberOfBytesToUnlockLow (DWORD), nNumberOfBytesToUnlockHigh (DWORD), lpOverlapped (OVERLAPPED* in/out)
r1, _, err := procUnlockFileEx.Call(
	uintptr(hFile),
	uintptr(dwReserved),
	uintptr(nNumberOfBytesToUnlockLow),
	uintptr(nNumberOfBytesToUnlockHigh),
	uintptr(lpOverlapped),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function UnlockFileEx(
  hFile: THandle;   // HANDLE
  dwReserved: DWORD;   // DWORD optional
  nNumberOfBytesToUnlockLow: DWORD;   // DWORD
  nNumberOfBytesToUnlockHigh: DWORD;   // DWORD
  lpOverlapped: Pointer   // OVERLAPPED* in/out
): BOOL; stdcall;
  external 'KERNEL32.dll' name 'UnlockFileEx';
result := DllCall("KERNEL32\UnlockFileEx"
    , "Ptr", hFile   ; HANDLE
    , "UInt", dwReserved   ; DWORD optional
    , "UInt", nNumberOfBytesToUnlockLow   ; DWORD
    , "UInt", nNumberOfBytesToUnlockHigh   ; DWORD
    , "Ptr", lpOverlapped   ; OVERLAPPED* in/out
    , "Int")   ; return: BOOL
●UnlockFileEx(hFile, dwReserved, nNumberOfBytesToUnlockLow, nNumberOfBytesToUnlockHigh, lpOverlapped) = DLL("KERNEL32.dll", "bool UnlockFileEx(void*, dword, dword, dword, void*)")
# 呼び出し: UnlockFileEx(hFile, dwReserved, nNumberOfBytesToUnlockLow, nNumberOfBytesToUnlockHigh, lpOverlapped)
# hFile : HANDLE -> "void*"
# dwReserved : DWORD optional -> "dword"
# nNumberOfBytesToUnlockLow : DWORD -> "dword"
# nNumberOfBytesToUnlockHigh : DWORD -> "dword"
# lpOverlapped : OVERLAPPED* in/out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

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

typedef UnlockFileExNative = Int32 Function(Pointer<Void>, Uint32, Uint32, Uint32, Pointer<Void>);
typedef UnlockFileExDart = int Function(Pointer<Void>, int, int, int, Pointer<Void>);
final UnlockFileEx = DynamicLibrary.open('KERNEL32.dll')
    .lookupFunction<UnlockFileExNative, UnlockFileExDart>('UnlockFileEx');
// hFile : HANDLE -> Pointer<Void>
// dwReserved : DWORD optional -> Uint32
// nNumberOfBytesToUnlockLow : DWORD -> Uint32
// nNumberOfBytesToUnlockHigh : DWORD -> Uint32
// lpOverlapped : OVERLAPPED* in/out -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function UnlockFileEx(
  hFile: THandle;   // HANDLE
  dwReserved: DWORD;   // DWORD optional
  nNumberOfBytesToUnlockLow: DWORD;   // DWORD
  nNumberOfBytesToUnlockHigh: DWORD;   // DWORD
  lpOverlapped: Pointer   // OVERLAPPED* in/out
): BOOL; stdcall;
  external 'KERNEL32.dll' name 'UnlockFileEx';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "UnlockFileEx"
  c_UnlockFileEx :: Ptr () -> Word32 -> Word32 -> Word32 -> Ptr () -> IO CInt
-- hFile : HANDLE -> Ptr ()
-- dwReserved : DWORD optional -> Word32
-- nNumberOfBytesToUnlockLow : DWORD -> Word32
-- nNumberOfBytesToUnlockHigh : DWORD -> Word32
-- lpOverlapped : OVERLAPPED* in/out -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let unlockfileex =
  foreign "UnlockFileEx"
    ((ptr void) @-> uint32_t @-> uint32_t @-> uint32_t @-> (ptr void) @-> returning int32_t)
(* hFile : HANDLE -> (ptr void) *)
(* dwReserved : DWORD optional -> uint32_t *)
(* nNumberOfBytesToUnlockLow : DWORD -> uint32_t *)
(* nNumberOfBytesToUnlockHigh : DWORD -> uint32_t *)
(* lpOverlapped : OVERLAPPED* in/out -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)

(cffi:defcfun ("UnlockFileEx" unlock-file-ex :convention :stdcall) :int32
  (h-file :pointer)   ; HANDLE
  (dw-reserved :uint32)   ; DWORD optional
  (n-number-of-bytes-to-unlock-low :uint32)   ; DWORD
  (n-number-of-bytes-to-unlock-high :uint32)   ; DWORD
  (lp-overlapped :pointer))   ; OVERLAPPED* in/out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $UnlockFileEx = Win32::API::More->new('KERNEL32',
    'BOOL UnlockFileEx(HANDLE hFile, DWORD dwReserved, DWORD nNumberOfBytesToUnlockLow, DWORD nNumberOfBytesToUnlockHigh, LPVOID lpOverlapped)');
# my $ret = $UnlockFileEx->Call($hFile, $dwReserved, $nNumberOfBytesToUnlockLow, $nNumberOfBytesToUnlockHigh, $lpOverlapped);
# hFile : HANDLE -> HANDLE
# dwReserved : DWORD optional -> DWORD
# nNumberOfBytesToUnlockLow : DWORD -> DWORD
# nNumberOfBytesToUnlockHigh : DWORD -> DWORD
# lpOverlapped : OVERLAPPED* in/out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

類似 API
公式の関連項目
使用する型