Click to Rate and Give Feedback
MSDN
MSDN Library
Windows Development
Backup
Backup
Backup Reference
Backup Functions
 BackupRead function
BackupRead function

Applies to: desktop apps only

The BackupRead function can be used to back up a file or directory, including the security information. The function reads data associated with a specified file or directory into a buffer, which can then be written to the backup medium using the WriteFile function.

Syntax

BOOL BackupRead(
  __in   HANDLE hFile,
  __out  LPBYTE lpBuffer,
  __in   DWORD nNumberOfBytesToRead,
  __out  LPDWORD lpNumberOfBytesRead,
  __in   BOOL bAbort,
  __in   BOOL bProcessSecurity,
  __out  LPVOID *lpContext
);

Parameters

hFile [in]

Handle to the file or directory to be backed up. To obtain the handle, call the CreateFile function. The SACLs are not read unless the file handle was created with the ACCESS_SYSTEM_SECURITY access right. For more information, see File Security and Access Rights.

The handle must be synchronous (nonoverlapped). This means that the FILE_FLAG_OVERLAPPED flag must not be set when CreateFile is called. This function does not validate that the handle it receives is synchronous, so it does not return an error code for a synchronous handle, but calling it with an asynchronous (overlapped) handle can result in subtle errors that are very difficult to debug.

The BackupRead function may fail if CreateFile was called with the flag FILE_FLAG_NO_BUFFERING. In this case, the GetLastError function returns the value ERROR_INVALID_PARAMETER.

lpBuffer [out]

Pointer to a buffer that receives the data.

nNumberOfBytesToRead [in]

Length of the buffer, in bytes. The buffer size must be greater than the size of a WIN32_STREAM_ID structure.

lpNumberOfBytesRead [out]

Pointer to a variable that receives the number of bytes read.

If the function returns a nonzero value, and the variable pointed to by lpNumberOfBytesRead is zero, then all the data associated with the file handle has been read.

bAbort [in]

Indicates whether you have finished using BackupRead on the handle. While you are backing up the file, specify this parameter as FALSE. Once you are done using BackupRead, you must call BackupRead one more time specifying TRUE for this parameter and passing the appropriate lpContext. lpContext must be passed when bAbort is TRUE; all other parameters are ignored.

bProcessSecurity [in]

Indicates whether the function will restore the access-control list (ACL) data for the file or directory.

If bProcessSecurity is TRUE, the ACL data will be backed up.

lpContext [out]

Pointer to a variable that receives a pointer to an internal data structure used by BackupRead to maintain context information during a backup operation.

You must set the variable pointed to by lpContext to NULL before the first call to BackupRead for the specified file or directory. The function allocates memory for the data structure, and then sets the variable to point to that structure. You must not change lpContext or the variable that it points to between calls to BackupRead.

To release the memory used by the data structure, call BackupRead with the bAbort parameter set to TRUE when the backup operation is complete.

Return value

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero, indicating that an I/O error occurred. To get extended error information, call GetLastError.

Remarks

This function is not intended for use in backing up files encrypted under the Encrypted File System. Use ReadEncryptedFileRaw for that purpose.

If an error occurs while BackupRead is reading data, the calling process can skip the bad data by calling the BackupSeek function.

The file or directory should be restored using the BackupWrite function.

Requirements

Minimum supported client

Windows 2000 Professional

Minimum supported server

Windows 2000 Server

Header

Winbase.h (include Windows.h)

Library

Kernel32.lib

DLL

Kernel32.dll

See also

BackupWrite
BackupSeek
Creating a Backup Application
ReadEncryptedFileRaw
WIN32_STREAM_ID

 

 

Send comments about this topic to Microsoft

Build date: 2/3/2012

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
VB.NET      eriklitze   |   Edit   |   Show History
  
<DllImport("kernel32.dll", CharSet:=CharSet.Auto, ExactSpelling:=True, SetLastError:=True)> _
Public Shared Function BackupRead(ByVal hFile As IntPtr, <Out()> ByVal lpBuffer As IntPtr, ByVal nNumberOfBytesToRead As Integer, <Out()> ByRef lpNumberOfBytesRead As Integer, ByVal bAbort As Boolean, ByVal bProcessSecurity As Boolean, <Out()> ByRef lpContext As IntPtr) As Boolean
End Function

<DllImport("kernel32.dll", CharSet:=CharSet.Auto, ExactSpelling:=True, SetLastError:=True)> _
Public Shared Function BackupRead(ByVal hFile As HandleRef, <Out()> ByVal lpBuffer As IntPtr, ByVal nNumberOfBytesToRead As Integer, <Out()> ByRef lpNumberOfBytesRead As Integer, ByVal bAbort As Boolean, ByVal bProcessSecurity As Boolean, <Out()> ByRef lpContext As IntPtr) As Boolean
End Function

<DllImport("kernel32.dll", CharSet:=CharSet.Auto, ExactSpelling:=True, SetLastError:=True)> _
Public Shared Function BackupRead(ByVal hFile As SafeFileHandle, <Out()> ByVal lpBuffer As IntPtr, ByVal nNumberOfBytesToRead As Integer, <Out()> ByRef lpNumberOfBytesRead As Integer, ByVal bAbort As Boolean, ByVal bProcessSecurity As Boolean, <Out()> ByRef lpContext As IntPtr) As Boolean
End Function

<DllImport("kernel32.dll", CharSet:=CharSet.Auto, ExactSpelling:=True, SetLastError:=True)> _
Public Shared Function BackupRead(ByVal hFile As IntPtr, <Out()> ByRef lpBuffer As WIN32_STREAM_ID, ByVal nNumberOfBytesToRead As Integer, <Out()> ByRef lpNumberOfBytesRead As Integer, ByVal bAbort As Boolean, ByVal bProcessSecurity As Boolean, <Out()> ByRef lpContext As IntPtr) As Boolean
End Function

<DllImport("kernel32.dll", CharSet:=CharSet.Auto, ExactSpelling:=True, SetLastError:=True)> _
Public Shared Function BackupRead(ByVal hFile As HandleRef, <Out()> ByRef lpBuffer As WIN32_STREAM_ID, ByVal nNumberOfBytesToRead As Integer, <Out()> ByRef lpNumberOfBytesRead As Integer, ByVal bAbort As Boolean, ByVal bProcessSecurity As Boolean, <Out()> ByRef lpContext As IntPtr) As Boolean
End Function

<DllImport("kernel32.dll", CharSet:=CharSet.Auto, ExactSpelling:=True, SetLastError:=True)> _
Public Shared Function BackupRead(ByVal hFile As SafeFileHandle, <Out()> ByRef lpBuffer As WIN32_STREAM_ID, ByVal nNumberOfBytesToRead As Integer, <Out()> ByRef lpNumberOfBytesRead As Integer, ByVal bAbort As Boolean, ByVal bProcessSecurity As Boolean, <Out()> ByRef lpContext As IntPtr) As Boolean
End Function
Tags What's this?: Add a tag
Flag as ContentBug
How to use CreateFile in combination with BackupRead - for backing-up files.      Frank Spierings   |   Edit   |   Show History
Make sure you have SeBackupPrivilege (read http://msdn.microsoft.com/en-us/library/aa446619%28VS.85%29.aspx)
Retreive a file handle using something like the following:

char *fileName="C:\\example.txt"    
HANDLE hFile = CreateFile(
fileName, // lpFileName
GENERIC_READ, // dwDesiredAccess
0, // dwShareMode
NULL, // lpSecurityAttributes
OPEN_EXISTING, // dwCreationDisposition
FILE_FLAG_BACKUP_SEMANTICS, // dwFlagsAndAttributes
NULL // hTemplateFile
);


You can now use this hFile in BackupRead()

(This took me far too long to figure out!)
How to open a file with BackupRead      Frank Spierings   |   Edit   |   Show History
Make sure you have SeBackupPrivilege (read http://msdn.microsoft.com/en-us/library/aa446619%28VS.85%29.aspx)
Retreive a file handle using something like the following:

char *fileName="C:\\example.txt"
HANDLE hFile = CreateFile(
fileName, // lpFileName
GENERIC_READ, // dwDesiredAccess
0, // dwShareMode
NULL, // lpSecurityAttributes
OPEN_EXISTING, // dwCreationDisposition
FILE_FLAG_BACKUP_SEMANTICS, // dwFlagsAndAttributes
NULL // hTemplateFile
);

You can now use this hFile in BackupRead()

(This took me far too long to figure out!)
Tags What's this?: Add a tag
Flag as ContentBug
Requirements, corrected      KodeKyk   |   Edit   |   Show History

Client (Classic Windows): Not available on Windows 9x and Win32s
Client (NT-based): Version 3.1 and later (DocError: Above document says Version 5.0 and later)
Server (NT-based): Version 3.1 and later (DocError: Above document says Version 5.0 and later)
Mobile (CE-based): Not available on Windows Mobile
Redistributable: None
Header: Declared in Winbase.h; include Windows.h
Library: Use Kernel32.lib.
DLL: Exported from Kernel32.dll.

(Source for above details: Older versions of this documentation, plus a clearer notation for common aspects).

How to back up the security info      Roland Illig   |   Edit   |   Show History
To get the security info using the BackupRead function, you have to pass the READ_CONTROL as the dwDesiredAccess parameter of CreateFile. See http://support.microsoft.com/kb/240184 for more details.

Under Windows XP SP 2, you have to pass the bProcessSecurity flag consistently among all calls to BackupRead. When you read the stream header with bProcessSecurity=TRUE and try to read the stream data (containing the security data) with bProcessSecurity=FALSE, the latter call will hang indefinitely.
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2012 Microsoft. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker