Backs up a file or directory, including the security information.
#Include <WinAPIEx.au3>
_WinAPI_BackupRead ( $hFile, $pBuffer, $iLength, ByRef $iBytes, ByRef $pContext [, $fSecurity] )
$hFile | Handle to the file or directory to be backed up. To obtain the handle, call the _WinAPI_CreateFileEx() function. The SACLs are not read unless the file handle was created with the $ACCESS_SYSTEM_SECURITY access right. |
$pBuffer | A pointer to a buffer that receives the data. |
$iLength | The size of the buffer, in bytes. The buffer size must be greater than the size of the $tagWIN32_STREAM_ID structure (查看MSDN得到更多信息). |
$iBytes | The number of bytes read. |
$pContext | A pointer to an internal data structure used by this function to maintain context information during a backup operation. You must set this variable to 0 before the first call to _WinAPI_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 this variable or the variable that it points to between calls to _WinAPI_BackupRead(). |
$fSecurity | [可选参数] Specifies whether the function will backup the access-control list (ACL) data, valid values: TRUE - The ACL data will be backed up. FALSE - The ACL data will be omitted. (Default) |
成功: | 返回 1. |
失败: | 返回 0 并设置 @error 标志为非 0 值. |
在MSDN中搜索
#Include <APIConstants.au3>
#Include <WinAPIEx.au3>
Opt('MustDeclareVars', 1)
Global Const $sFile = @ScriptFullPath
Global $aAdjust, $aPrivileges[2] = [$SE_BACKUP_NAME, $SE_RESTORE_NAME]
Global $Bytes = 4096 + FileGetSize($sFile)
Global $hFile, $pBuffer, $pContext
; Enable "SeBackupPrivilege" and "SeRestorePrivilege" privileges to perform backup and restore operation
$hToken = _WinAPI_OpenProcessToken(BitOR($TOKEN_ADJUST_PRIVILEGES, $TOKEN_QUERY))
_WinAPI_AdjustTokenPrivileges($hToken, $aPrivileges, $SE_PRIVILEGE_ENABLED, $aAdjust)
If @error Or @extended Then
MsgBox(16, 'Error', 'You do not have the required privileges.')
Exit
EndIf
; Create a memory buffer where to store the backup data
$pBuffer = _WinAPI_CreateBuffer($Bytes)
; Back up a file, including the security information
$pContext = 0
$hFile = _WinAPI_CreateFileEx($sFile, $OPEN_EXISTING, $GENERIC_READ)
_WinAPI_BackupRead($hFile, $pBuffer, $Bytes, $Bytes, $pContext, 1)
If @error Then
MsgBox(16, 'Error', 'Unable to back up a file.')
Exit
EndIf
_WinAPI_BackupReadAbort($pContext)
_WinAPI_CloseHandle($hFile)
; Restore a file (.bak) and the ACL data
$pContext = 0
$hFile = _WinAPI_CreateFileEx(_WinAPI_PathRenameExtension($sFile, '.bak'), $CREATE_ALWAYS, BitOR($GENERIC_WRITE, $WRITE_DAC, $WRITE_OWNER))
_WinAPI_BackupWrite($hFile, $pBuffer, $Bytes, $Bytes, $pContext, 1)
If @error Then
MsgBox(16, 'Error', 'Unable to restore a file.')
Exit
EndIf
_WinAPI_BackupWriteAbort($pContext)
_WinAPI_CloseHandle($hFile)
; Free the used memory
_WinAPI_FreeMemory($pBuffer)
; Restore "SeBackupPrivilege" and "SeRestorePrivilege" privileges by default
_WinAPI_AdjustTokenPrivileges($hToken, $aAdjust, 0, $aAdjust)
_WinAPI_CloseHandle($hToken)