首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

复制SAM文件有关问题!

2012-03-30 
复制SAM文件问题!!!我根据http://blog.1x1y.com.cn/greatdong/entry/2007051419上面的示例程序二(http://w

复制SAM文件问题!!!
我根据http://blog.1x1y.com.cn/greatdong/entry/2007051419上面的示例程序二(http://wasm.ru/pub/21/files/lockfileswork/samcopy.rar)翻译出来了如下的VB程序,不过不能复制SAM文件,在获取   hDriver   时失败,而且示例程序的代码也不能复制,请教高手!!!若有复制的代码发到我邮箱tzwsoho@126.com则更好!!!

Option   Explicit

Private   Const   INVALID_HANDLE_VALUE   As   Long   =   -1

Private   Const   CREATE_NEW   As   Long   =   1
Private   Const   OPEN_EXISTING   As   Long   =   3

Private   Const   GENERIC_READ   As   Long   =   &H80000000
Private   Const   GENERIC_WRITE   As   Long   =   &H40000000

Private   Const   MEM_COMMIT   As   Long   =   &H1000
Private   Const   MEM_RELEASE   As   Long   =   &H8000
Private   Const   MEM_RESERVE   As   Long   =   &H2000
Private   Const   PAGE_READWRITE   As   Long   =   &H4

Private   Const   FILE_DEVICE_UNKNOWN   As   Long   =   &H22
Private   Const   FILE_READ_ACCESS   As   Long   =   &H1
Private   Const   FILE_READ_ATTRIBUTES   As   Long   =   &H80
Private   Const   FILE_SHARE_DELETE   As   Long   =   &H4
Private   Const   FILE_SHARE_READ   As   Long   =   &H1
Private   Const   FILE_SHARE_WRITE   As   Long   =   &H2

Private   Const   METHOD_BUFFERED   As   Long   =   0

'Private   Const   BASE_IOCTL   As   Long   =   &H224000
Private   Const   IOCTL1   As   Long   =   &H1004004

Private   Const   AC_GENERIC_READ                 As   Long   =   &H120089
Private   Const   AC_GENERIC_WRITE               As   Long   =   &H120196
Private   Const   AC_DELETE                             As   Long   =   &H110080
Private   Const   AC_READ_CONTROL                 As   Long   =   &H120080
Private   Const   AC_WRITE_DAC                       As   Long   =   &H140080
Private   Const   AC_WRITE_OWNER                   As   Long   =   &H180080
Private   Const   AC_GENERIC_ALL                   As   Long   =   &H1F01FF
Private   Const   AC_STANDARD_RIGHTS_ALL   As   Long   =   &H1F0080

Private   Declare   Function   DeviceIoControl   Lib   "kernel32.dll "   (ByVal   hDevice   As   Long,   ByVal   dwIoControlCode   As   Long,   ByRef   lpInBuffer   As   Any,   ByVal   nInBufferSize   As   Long,   ByRef   lpOutBuffer   As   Any,   ByVal   nOutBufferSize   As   Long,   ByRef   lpBytesReturned   As   Long,   ByRef   lpOverlapped   As   Any)   As   Long
Private   Declare   Function   CreateFile   Lib   "kernel32.dll "   Alias   "CreateFileA "   (ByVal   lpFileName   As   String,   ByVal   dwDesiredAccess   As   Long,   ByVal   dwShareMode   As   Long,   ByRef   lpSecurityAttributes   As   Any,   ByVal   dwCreationDisposition   As   Long,   ByVal   dwFlagsAndAttributes   As   Long,   ByVal   hTemplateFile   As   Long)   As   Long


Private   Declare   Function   CloseHandle   Lib   "kernel32.dll "   (ByVal   hObject   As   Long)   As   Long
Private   Declare   Function   lstrcat   Lib   "kernel32.dll "   Alias   "lstrcatA "   (ByVal   lpString1   As   String,   ByVal   lpString2   As   String)   As   Long
Private   Declare   Function   GetSystemDirectory   Lib   "kernel32.dll "   Alias   "GetSystemDirectoryA "   (ByVal   lpBuffer   As   String,   ByVal   nSize   As   Long)   As   Long
Private   Declare   Function   VirtualFree   Lib   "kernel32.dll "   (ByRef   lpAddress   As   Any,   ByVal   dwSize   As   Long,   ByVal   dwFreeType   As   Long)   As   Long
Private   Declare   Function   WriteFile   Lib   "kernel32.dll "   (ByVal   hFile   As   Long,   ByRef   lpBuffer   As   Any,   ByVal   nNumberOfBytesToWrite   As   Long,   ByRef   lpNumberOfBytesWritten   As   Long,   ByRef   lpOverlapped   As   Any)   As   Long
Private   Declare   Function   ReadFile   Lib   "kernel32.dll "   (ByVal   hFile   As   Long,   ByRef   lpBuffer   As   Any,   ByVal   nNumberOfBytesToRead   As   Long,   ByRef   lpNumberOfBytesRead   As   Long,   ByRef   lpOverlapped   As   Any)   As   Long
Private   Declare   Function   GetFileSize   Lib   "kernel32.dll "   (ByVal   hFile   As   Long,   ByRef   lpFileSizeHigh   As   Long)   As   Long
Private   Declare   Function   VirtualAlloc   Lib   "kernel32.dll "   (ByRef   lpAddress   As   Any,   ByVal   dwSize   As   Long,   ByVal   flAllocationType   As   Long,   ByVal   flProtect   As   Long)   As   Long

Private   Sub   cmdCopy_Click()
Dim   hFile   As   Long,   hDest   As   Long
Dim   Size   As   Long,   Bytes   As   Long
Dim   Data   As   Long
Dim   Name   As   String   *   256
Call   GetSystemDirectory(Name,   256)
Name   =   Replace$(Name,   "\ ",   "\\ ")
Call   lstrcat(Name,   "\\config\\SAM ")
hFile   =   CreateFile(Name,   FILE_READ_ATTRIBUTES,   FILE_SHARE_READ   Or   FILE_SHARE_WRITE   Or   FILE_SHARE_DELETE,   0,   OPEN_EXISTING,   0,   0)
If   hFile   <>   INVALID_HANDLE_VALUE   Then
        If   SetHandleAccess(hFile,   AC_GENERIC_READ)   Then
                Size   =   GetFileSize(hFile,   ByVal   0&)
                Data   =   VirtualAlloc(ByVal   0&,   Size,   MEM_COMMIT   Or   MEM_RESERVE,   PAGE_READWRITE)
                If   Data   Then
                        Call   ReadFile(hFile,   Data,   Size,   Bytes,   ByVal   0&)
                        hDest   =   CreateFile( "c:\SAM ",   GENERIC_WRITE,   0,   ByVal   0&,   CREATE_NEW,   0,   0)


                        If   hDest   <>   INVALID_HANDLE_VALUE   Then
                                Call   WriteFile(hDest,   Data,   Size,   Bytes,   ByVal   0&)
                                Call   CloseHandle(hDest)
                                MsgBox   "Success!!! "
                        End   If
                        Call   VirtualFree(Data,   0,   MEM_RELEASE)
                End   If
        End   If
        Call   CloseHandle(hFile)
End   If
End   Sub

Private   Function   SetHandleAccess(ByVal   Handle   As   Long,   ByVal   GrantedAccess   As   Long)   As   Boolean
Dim   hDriver   As   Long,   Result   As   Boolean
Dim   Bytes   As   Long,   Buff(1)   As   Long
hDriver   =   CreateFile( "\\\\.\\haccess ",   GENERIC_READ,   0,   0,   OPEN_EXISTING,   0,   0)
If   hDriver   <>   INVALID_HANDLE_VALUE   Then
        Buff(0)   =   Handle
        Buff(1)   =   GrantedAccess
        Result   =   DeviceIoControl(hDriver,   IOCTL1,   ByVal   VarPtr(Buff(0)),   8,   ByVal   0&,   0&,   Bytes,   ByVal   0&)
        Call   CloseHandle(hDriver)
        SetHandleAccess   =   Result
End   If
End   Function

[解决办法]
哇 啊啊 不行了...

#define BASE_IOCTL (FILE_DEVICE_UNKNOWN < < 16) | (FILE_READ_ACCESS < < 14) | METHOD_BUFFERED
#define IOCTL1 BASE_IOCTL | (1 < < 2)

BOOLEAN SetHandleAccess(
HANDLE Handle,
ACCESS_MASK GrantedAccess
)
{
HANDLE hDriver;
ULONG Bytes;
ULONG Buff[2];
BOOLEAN Result = FALSE;

hDriver = CreateFile( "\\\\.\\haccess ", GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0);

if (hDriver != INVALID_HANDLE_VALUE)
{
Buff[0] = (ULONG)Handle;
Buff[1] = GrantedAccess;

Result = DeviceIoControl(hDriver, IOCTL1, Buff, sizeof(Buff), NULL, 0, &Bytes, NULL);

CloseHandle(hDriver);
}
return Result;
}


void main()
{
HANDLE hFile, hDest;
ULONG Size, Bytes;
PVOID Data;
CHAR Name[MAX_PATH];

GetSystemDirectory(Name, MAX_PATH);

lstrcat(Name, "\\config\\SAM ");

hFile = CreateFile(Name, FILE_READ_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL, OPEN_EXISTING, 0, 0);

if (hFile != INVALID_HANDLE_VALUE)
{
if (SetHandleAccess(hFile, AC_GENERIC_READ))
{


Size = GetFileSize(hFile, NULL);

Data = VirtualAlloc(NULL, Size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);

if (Data)
{
ReadFile(hFile, Data, Size, &Bytes, NULL);

hDest = CreateFile( "c:\\SAM ", GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, 0);

if (hDest != INVALID_HANDLE_VALUE)
{
WriteFile(hDest, Data, Size, &Bytes, NULL);

CloseHandle(hDest);
}
VirtualFree(Data, 0, MEM_RELEASE);
}
}
CloseHandle(hFile);
}
}


上面是C中的代码
在C中可以成功复制 为什么翻译成VB后就不行了
下面是VB代码 LZ上的代码

Option Explicit

Private Const INVALID_HANDLE_VALUE As Long = -1
Private Const CREATE_NEW As Long = 1
Private Const OPEN_EXISTING As Long = 3

Private Const GENERIC_READ As Long = &H80000000
Private Const GENERIC_WRITE As Long = &H40000000

Private Const MEM_COMMIT As Long = &H1000
Private Const MEM_RELEASE As Long = &H8000
Private Const MEM_RESERVE As Long = &H2000
Private Const PAGE_READWRITE As Long = &H4

Private Const FILE_DEVICE_UNKNOWN As Long = &H22

Private Const FILE_READ_ATTRIBUTES As Long = &H80
Private Const FILE_SHARE_DELETE As Long = &H4
Private Const FILE_SHARE_READ As Long = &H1

Private Const FILE_READ_ACCESS As Long = &H1
Private Const FILE_SHARE_WRITE As Long = &H2

Private Const METHOD_BUFFERED As Long = 0


Private Const BASE_IOCTL As Long = 2244608
'BASE_IOCTL = FILE_DEVICE_UNKNOWN * (2 ^ 16) Or FILE_READ_ACCESS * (2 ^ 14) Or METHOD_BUFFERED

Private Const IOCTL1 As Long = 2244612 'LZ这里算错了
'IOCTL1 = BASE_IOCTL Or 1 * (2 ^ 2)


Private Const AC_GENERIC_READ As Long = &H120089
Private Const AC_GENERIC_WRITE As Long = &H120196
Private Const AC_DELETE As Long = &H110080
Private Const AC_READ_CONTROL As Long = &H120080
Private Const AC_WRITE_DAC As Long = &H140080
Private Const AC_WRITE_OWNER As Long = &H180080
Private Const AC_GENERIC_ALL As Long = &H1F01FF
Private Const AC_STANDARD_RIGHTS_ALL As Long = &H1F0080

Private Declare Function DeviceIoControl Lib "kernel32.dll " (ByVal hDevice As Long, ByVal dwIoControlCode As Long, ByRef lpInBuffer As Any, ByVal nInBufferSize As Long, ByRef lpOutBuffer As Any, ByVal nOutBufferSize As Long, ByRef lpBytesReturned As Long, ByRef lpOverlapped As Any) As Long
Private Declare Function CreateFile Lib "kernel32.dll " Alias "CreateFileA " (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByRef lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function CloseHandle Lib "kernel32.dll " (ByVal hObject As Long) As Long
Private Declare Function lstrcat Lib "kernel32.dll " Alias "lstrcatA " (ByVal lpString1 As String, ByVal lpString2 As String) As Long
Private Declare Function GetSystemDirectory Lib "kernel32.dll " Alias "GetSystemDirectoryA " (ByVal lpBuffer As String, ByVal nSize As Long) As Long
Private Declare Function VirtualFree Lib "kernel32.dll " (ByRef lpAddress As Any, ByVal dwSize As Long, ByVal dwFreeType As Long) As Long
Private Declare Function WriteFile Lib "kernel32.dll " (ByVal hFile As Long, ByRef lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, ByRef lpNumberOfBytesWritten As Long, ByRef lpOverlapped As Any) As Long


Private Declare Function ReadFile Lib "kernel32.dll " (ByVal hFile As Long, ByRef lpBuffer As Long, ByVal nNumberOfBytesToRead As Long, ByRef lpNumberOfBytesRead As Long, ByRef lpOverlapped As Any) As Long
Private Declare Function GetFileSize Lib "kernel32.dll " (ByVal hFile As Long, ByRef lpFileSizeHigh As Long) As Long
Private Declare Function VirtualAlloc Lib "kernel32.dll " (ByRef lpAddress As Any, ByVal dwSize As Long, ByVal flAllocationType As Long, ByVal flProtect As Long) As Long

Private Sub cmdCopy_Click()
Dim hFile As Long, hDest As Long
Dim Size As Long, Bytes As Long, Data As Long
Dim Name As String, hRet As Byte

Name = Space(255)
hRet = GetSystemDirectory(Name, 256)
Name = Left(Name, hRet) & "\config\SAM "

hFile = CreateFile(Name, FILE_READ_ATTRIBUTES, FILE_SHARE_READ Or FILE_SHARE_WRITE Or FILE_SHARE_DELETE, ByVal 0, OPEN_EXISTING, 0, 0)
If hFile Then
If SetHandleAccess(hFile, AC_GENERIC_READ) Then
Size = GetFileSize(hFile, ByVal 0)
Data = VirtualAlloc(ByVal 0, Size, MEM_COMMIT Or MEM_RESERVE, PAGE_READWRITE)
If Data Then
If ReadFile(hFile, Data, Size, Bytes, ByVal 0) Then '这里就执行不下去了没读权限...
hDest = CreateFile( "c:\SAM ", GENERIC_WRITE, 0, ByVal 0, CREATE_NEW, 0, 0)
If hDest Then
WriteFile hDest, Data, Size, Bytes, ByVal 0
CloseHandle hDest
MsgBox "Success "
End If
End If
VirtualFree Data, 0, MEM_RELEASE
End If
End If
CloseHandle hFile
End If

End Sub

Private Function SetHandleAccess(ByVal Handle As Long, ByVal GrantedAccess As Long) As Boolean
Dim hDriver As Long, Result As Boolean
Dim Bytes As Long, buff(1) As Long
hDriver = CreateFile( "\\.\haccess ", GENERIC_READ, 0, ByVal 0, OPEN_EXISTING, 0, 0)
If hDriver Then
buff(0) = Handle
buff(1) = GrantedAccess

SetHandleAccess = DeviceIoControl(hDriver, IOCTL1, buff(0), 8, ByVal 0, 0, Bytes, ByVal 0) '返回虽然是真 但还是怀疑是这里的问题
CloseHandle hDriver
End If
End Function

热点排行