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

CfOpenFileWithOplock

関数
便宜ロック付きで保護されたファイルハンドルを開く。
DLLcldapi.dll呼出規約winapi対応OSWindows 10 以降

シグネチャ

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

HRESULT CfOpenFileWithOplock(
    LPCWSTR FilePath,
    CF_OPEN_FILE_FLAGS Flags,
    HANDLE* ProtectedHandle
);

パラメーター

名前方向説明
FilePathLPCWSTRinオプロック付きで開く対象ファイルのフルパスを示すワイド文字列。
FlagsCF_OPEN_FILE_FLAGSinオープン動作を制御するCF_OPEN_FILE_FLAGSフラグ。アクセス種別等を指定する。
ProtectedHandleHANDLE*out取得した保護付きハンドルを受け取るHANDLEへのポインタ。出力用。

戻り値の型: HRESULT

各言語での呼び出し定義

// 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   // HRESULT
function 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 を使用。

関連項目

使用する型