CfOpenFileWithOplock
関数シグネチャ
// cldapi.dll
#include <windows.h>
HRESULT CfOpenFileWithOplock(
LPCWSTR FilePath,
CF_OPEN_FILE_FLAGS Flags,
HANDLE* ProtectedHandle
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| FilePath | LPCWSTR | in | 開くファイルまたはディレクトリの完全修飾パス。 |
| Flags | CF_OPEN_FILE_FLAGS | in | ファイルを開く際のアクセス許可を指定するフラグ。Flags には次の値の組み合わせを設定できます。
|
| ProtectedHandle | HANDLE* | out | 開かれたばかりのファイルまたはディレクトリへの不透明なハンドル。これは通常の Win32 ハンドルではないため、CfApi 以外の Win32 API で直接使用することはできない点に注意してください。 |
戻り値の型: HRESULT
公式ドキュメント
通常のファイルとプレースホルダーファイルの両方について、ファイルまたはディレクトリへの非同期の不透明なハンドルを開き、オープンフラグに基づいて適切な oplock をそのファイルに設定します。
戻り値
この関数が成功した場合は S_OK を返します。それ以外の場合は HRESULT エラーコードを返します。
解説(Remarks)
oplock が解除されると、API は呼び出し元に代わって、アクティブなすべての要求を処理し切ってから、基になる Win32 ハンドルを閉じることで、解除の通知を自動的に処理します。
これは、oplock の使用に関連する複雑さを取り除くことを目的としています。呼び出し元は、CfOpenFileWithOplock が返したハンドルを CfCloseHandle で閉じる必要があります。
バックグラウンドアプリケーションは通常、ファイルに対して透過的に動作することを望みます。特に、他の (フォアグラウンドの) オープン元に共有違反を引き起こさないようにしたいと考えます。そのためには、CfOpenFileWithOplock で CF_OPEN_FILE_FLAG_EXCLUSIVE を使用した場合に付与されるような (OPLOCK_LEVEL_CACHE_READ | OPLOCK_LEVEL_CACHE_HANDLE) oplock を取得します。その後、要求する共有モード/アクセスモードがバックグラウンドアプリのものと競合する別のオープン元が現れると、バックグラウンドアプリの oplock が解除されます。これにより、バックグラウンドアプリはファイルハンドルを閉じるよう促されます (Cf ハンドルの場合、ハンドルは無効になります。実際の基になるハンドルは閉じられています)。バックグラウンドアプリがハンドルを閉じると、他のオープン元のオープンは共有違反に遭遇することなく続行されます。これらはすべて、oplock の OPLOCK_LEVEL_CACHE_HANDLE の部分によって機能します。CF_OPEN_FILE_FLAG_EXCLUSIVE を指定しない場合、oplock には OPLOCK_LEVEL_CACHE_READ の保護しかないため、ここで説明した共有違反に対する保護は行われません。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// cldapi.dll
#include <windows.h>
HRESULT CfOpenFileWithOplock(
LPCWSTR FilePath,
CF_OPEN_FILE_FLAGS Flags,
HANDLE* ProtectedHandle
);[DllImport("cldapi.dll", ExactSpelling = true)]
static extern int CfOpenFileWithOplock(
[MarshalAs(UnmanagedType.LPWStr)] string FilePath, // LPCWSTR
int Flags, // CF_OPEN_FILE_FLAGS
IntPtr ProtectedHandle // HANDLE* out
);<DllImport("cldapi.dll", ExactSpelling:=True)>
Public Shared Function CfOpenFileWithOplock(
<MarshalAs(UnmanagedType.LPWStr)> FilePath As String, ' LPCWSTR
Flags As Integer, ' CF_OPEN_FILE_FLAGS
ProtectedHandle As IntPtr ' HANDLE* out
) As Integer
End Function' FilePath : LPCWSTR
' Flags : CF_OPEN_FILE_FLAGS
' ProtectedHandle : HANDLE* out
Declare PtrSafe Function CfOpenFileWithOplock Lib "cldapi" ( _
ByVal FilePath As LongPtr, _
ByVal Flags As Long, _
ByVal ProtectedHandle As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
CfOpenFileWithOplock = ctypes.windll.cldapi.CfOpenFileWithOplock
CfOpenFileWithOplock.restype = ctypes.c_int
CfOpenFileWithOplock.argtypes = [
wintypes.LPCWSTR, # FilePath : LPCWSTR
ctypes.c_int, # Flags : CF_OPEN_FILE_FLAGS
ctypes.c_void_p, # ProtectedHandle : HANDLE* out
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('cldapi.dll')
CfOpenFileWithOplock = Fiddle::Function.new(
lib['CfOpenFileWithOplock'],
[
Fiddle::TYPE_VOIDP, # FilePath : LPCWSTR
Fiddle::TYPE_INT, # Flags : CF_OPEN_FILE_FLAGS
Fiddle::TYPE_VOIDP, # ProtectedHandle : HANDLE* out
],
Fiddle::TYPE_INT)#[link(name = "cldapi")]
extern "system" {
fn CfOpenFileWithOplock(
FilePath: *const u16, // LPCWSTR
Flags: i32, // CF_OPEN_FILE_FLAGS
ProtectedHandle: *mut *mut core::ffi::c_void // HANDLE* out
) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("cldapi.dll")]
public static extern int CfOpenFileWithOplock([MarshalAs(UnmanagedType.LPWStr)] string FilePath, int Flags, IntPtr ProtectedHandle);
"@
$api = Add-Type -MemberDefinition $sig -Name 'cldapi_CfOpenFileWithOplock' -Namespace Win32 -PassThru
# $api::CfOpenFileWithOplock(FilePath, Flags, ProtectedHandle)#uselib "cldapi.dll"
#func global CfOpenFileWithOplock "CfOpenFileWithOplock" sptr, sptr, sptr
; CfOpenFileWithOplock FilePath, Flags, ProtectedHandle ; 戻り値は stat
; FilePath : LPCWSTR -> "sptr"
; Flags : CF_OPEN_FILE_FLAGS -> "sptr"
; ProtectedHandle : HANDLE* out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "cldapi.dll"
#cfunc global CfOpenFileWithOplock "CfOpenFileWithOplock" wstr, int, sptr
; res = CfOpenFileWithOplock(FilePath, Flags, ProtectedHandle)
; FilePath : LPCWSTR -> "wstr"
; Flags : CF_OPEN_FILE_FLAGS -> "int"
; ProtectedHandle : HANDLE* out -> "sptr"; HRESULT CfOpenFileWithOplock(LPCWSTR FilePath, CF_OPEN_FILE_FLAGS Flags, HANDLE* ProtectedHandle)
#uselib "cldapi.dll"
#cfunc global CfOpenFileWithOplock "CfOpenFileWithOplock" wstr, int, intptr
; res = CfOpenFileWithOplock(FilePath, Flags, ProtectedHandle)
; FilePath : LPCWSTR -> "wstr"
; Flags : CF_OPEN_FILE_FLAGS -> "int"
; ProtectedHandle : HANDLE* out -> "intptr"import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
cldapi = windows.NewLazySystemDLL("cldapi.dll")
procCfOpenFileWithOplock = cldapi.NewProc("CfOpenFileWithOplock")
)
// FilePath (LPCWSTR), Flags (CF_OPEN_FILE_FLAGS), ProtectedHandle (HANDLE* out)
r1, _, err := procCfOpenFileWithOplock.Call(
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(FilePath))),
uintptr(Flags),
uintptr(ProtectedHandle),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // HRESULTfunction CfOpenFileWithOplock(
FilePath: PWideChar; // LPCWSTR
Flags: Integer; // CF_OPEN_FILE_FLAGS
ProtectedHandle: Pointer // HANDLE* out
): Integer; stdcall;
external 'cldapi.dll' name 'CfOpenFileWithOplock';result := DllCall("cldapi\CfOpenFileWithOplock"
, "WStr", FilePath ; LPCWSTR
, "Int", Flags ; CF_OPEN_FILE_FLAGS
, "Ptr", ProtectedHandle ; HANDLE* out
, "Int") ; return: HRESULT●CfOpenFileWithOplock(FilePath, Flags, ProtectedHandle) = DLL("cldapi.dll", "int CfOpenFileWithOplock(char*, int, void*)")
# 呼び出し: CfOpenFileWithOplock(FilePath, Flags, ProtectedHandle)
# FilePath : LPCWSTR -> "char*"
# Flags : CF_OPEN_FILE_FLAGS -> "int"
# ProtectedHandle : HANDLE* out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "cldapi" fn CfOpenFileWithOplock(
FilePath: [*c]const u16, // LPCWSTR
Flags: i32, // CF_OPEN_FILE_FLAGS
ProtectedHandle: ?*anyopaque // HANDLE* out
) callconv(std.os.windows.WINAPI) i32;proc CfOpenFileWithOplock(
FilePath: WideCString, # LPCWSTR
Flags: int32, # CF_OPEN_FILE_FLAGS
ProtectedHandle: pointer # HANDLE* out
): int32 {.importc: "CfOpenFileWithOplock", stdcall, dynlib: "cldapi.dll".}pragma(lib, "cldapi");
extern(Windows)
int CfOpenFileWithOplock(
const(wchar)* FilePath, // LPCWSTR
int Flags, // CF_OPEN_FILE_FLAGS
void* ProtectedHandle // HANDLE* out
);ccall((:CfOpenFileWithOplock, "cldapi.dll"), stdcall, Int32,
(Cwstring, Int32, Ptr{Cvoid}),
FilePath, Flags, ProtectedHandle)
# FilePath : LPCWSTR -> Cwstring
# Flags : CF_OPEN_FILE_FLAGS -> Int32
# ProtectedHandle : HANDLE* out -> Ptr{Cvoid}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int32_t CfOpenFileWithOplock(
const uint16_t* FilePath,
int32_t Flags,
void* ProtectedHandle);
]]
local cldapi = ffi.load("cldapi")
-- cldapi.CfOpenFileWithOplock(FilePath, Flags, ProtectedHandle)
-- FilePath : LPCWSTR
-- Flags : CF_OPEN_FILE_FLAGS
-- ProtectedHandle : HANDLE* out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('cldapi.dll');
const CfOpenFileWithOplock = lib.func('__stdcall', 'CfOpenFileWithOplock', 'int32_t', ['str16', 'int32_t', 'void *']);
// CfOpenFileWithOplock(FilePath, Flags, ProtectedHandle)
// FilePath : LPCWSTR -> 'str16'
// Flags : CF_OPEN_FILE_FLAGS -> 'int32_t'
// ProtectedHandle : HANDLE* out -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("cldapi.dll", {
CfOpenFileWithOplock: { parameters: ["buffer", "i32", "pointer"], result: "i32" },
});
// lib.symbols.CfOpenFileWithOplock(FilePath, Flags, ProtectedHandle)
// FilePath : LPCWSTR -> "buffer"
// Flags : CF_OPEN_FILE_FLAGS -> "i32"
// ProtectedHandle : HANDLE* out -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t CfOpenFileWithOplock(
const uint16_t* FilePath,
int32_t Flags,
void* ProtectedHandle);
C, "cldapi.dll");
// $ffi->CfOpenFileWithOplock(FilePath, Flags, ProtectedHandle);
// FilePath : LPCWSTR
// Flags : CF_OPEN_FILE_FLAGS
// ProtectedHandle : HANDLE* 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 Cldapi extends StdCallLibrary {
Cldapi INSTANCE = Native.load("cldapi", Cldapi.class);
int CfOpenFileWithOplock(
WString FilePath, // LPCWSTR
int Flags, // CF_OPEN_FILE_FLAGS
Pointer ProtectedHandle // HANDLE* out
);
}@[Link("cldapi")]
lib Libcldapi
fun CfOpenFileWithOplock = CfOpenFileWithOplock(
FilePath : UInt16*, # LPCWSTR
Flags : Int32, # CF_OPEN_FILE_FLAGS
ProtectedHandle : Void* # HANDLE* out
) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef CfOpenFileWithOplockNative = Int32 Function(Pointer<Utf16>, Int32, Pointer<Void>);
typedef CfOpenFileWithOplockDart = int Function(Pointer<Utf16>, int, Pointer<Void>);
final CfOpenFileWithOplock = DynamicLibrary.open('cldapi.dll')
.lookupFunction<CfOpenFileWithOplockNative, CfOpenFileWithOplockDart>('CfOpenFileWithOplock');
// FilePath : LPCWSTR -> Pointer<Utf16>
// Flags : CF_OPEN_FILE_FLAGS -> Int32
// ProtectedHandle : HANDLE* out -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function CfOpenFileWithOplock(
FilePath: PWideChar; // LPCWSTR
Flags: Integer; // CF_OPEN_FILE_FLAGS
ProtectedHandle: Pointer // HANDLE* out
): Integer; stdcall;
external 'cldapi.dll' name 'CfOpenFileWithOplock';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "CfOpenFileWithOplock"
c_CfOpenFileWithOplock :: CWString -> Int32 -> Ptr () -> IO Int32
-- FilePath : LPCWSTR -> CWString
-- Flags : CF_OPEN_FILE_FLAGS -> Int32
-- ProtectedHandle : HANDLE* out -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let cfopenfilewithoplock =
foreign "CfOpenFileWithOplock"
((ptr uint16_t) @-> int32_t @-> (ptr void) @-> returning int32_t)
(* FilePath : LPCWSTR -> (ptr uint16_t) *)
(* Flags : CF_OPEN_FILE_FLAGS -> int32_t *)
(* ProtectedHandle : HANDLE* out -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library cldapi (t "cldapi.dll"))
(cffi:use-foreign-library cldapi)
(cffi:defcfun ("CfOpenFileWithOplock" cf-open-file-with-oplock :convention :stdcall) :int32
(file-path (:string :encoding :utf-16le)) ; LPCWSTR
(flags :int32) ; CF_OPEN_FILE_FLAGS
(protected-handle :pointer)) ; HANDLE* out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $CfOpenFileWithOplock = Win32::API::More->new('cldapi',
'int CfOpenFileWithOplock(LPCWSTR FilePath, int Flags, HANDLE ProtectedHandle)');
# my $ret = $CfOpenFileWithOplock->Call($FilePath, $Flags, $ProtectedHandle);
# FilePath : LPCWSTR -> LPCWSTR
# Flags : CF_OPEN_FILE_FLAGS -> int
# ProtectedHandle : HANDLE* out -> HANDLE
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。