Instant control over file and folder operations in Windows and .NET applications

CallbackFilter.OpenFile method


Pascal    C++ (Lib)    C++ (VCL)    C++ (.NET)    C#    VB.NET   

CallbackFilter     See also    


Opens a file from within the callback.


    function OpenFile(FileName: TCBString): THandle;
    type TCBString = {$ifdef UNICODE}UnicodeString{$else}WideString{$endif};

[C++ (Lib)]
    HANDLE OpenFile(LPWSTR FileName);

[C++ (VCL)]
    HANDLE __fastcall OpenFile(WideString FileName);

[C++ (.NET)]
    SafeFileHandle^ OpenFile(String^ FileName);

    SafeFileHandle OpenFile(string FileName);

    Function OpenFile(ByVal FileName As String) As SafeFileHandle


  • FileName - name of the file to be opened

Return values

Handle of the file if the function succeeded or INVALID_HANDLE_VALUE (-1L or 0xFFFFFFFF) if the function failed.


Use this method to open a file and obtain its handle instead of using CreateFile() Windows API function. If you use Windows API function CreateFile() to open the file and then call ReadFile() or WriteFile() Windows API functions from your callback handlers, you will get recursion and system crash. To avoid the crash, use this method to open the file. The obtained handle allows to read/write from/to the file using ReadFile() or WriteFile() functions and perform other operations on it when your application processes various callbacks.

This method should be called only from the handler of OnPostCreateFileC or OnPostOpenFileC callbacks and only for the file, for which this callback was called. If ReadWriteFilesInPreCreatePath property is set to true, this method can also be called from OnCreateFileC or OnOpenFileC callbacks.

The file is opened without buffering (see FILE_FLAG_NO_BUFFERING flag in the description of CreateFile() function of Windows API). This means that you should read from and write to such file with granularity equal to sector size of the device being read from or being written to. Other requirements specified in MSDN for FILE_FLAG_NO_BUFFERING flag also apply.

Use CloseHandle() function in Windows API to close the obtained handle.

See also

ReadWriteFileInPreCreatePath     OnPostCreateFileC    

Back to top