I tried to write a nt service for backup an small drive.

I put the following code in the main thread of service,but i can start the service.

Can anyone help me???

Thx very much!

backupDrive proc lpDrive:DWORD,lpImageFile:DWORD
LOCAL bBuff:BYTE
LOCAL szBuff:BYTE
LOCAL hFileW,hFileR:DWORD
LOCAL dwBytesR,dwBytesW:DWORD
invoke CreateFile,lpDrive,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL
.if eax!=INVALID_HANDLE_VALUE
mov hFileR,eax
invoke MessageBox,NULL,addr szRamdisk,NULL,MB_OK
invoke CreateFile,lpImageFile,GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL
.if eax!=INVALID_HANDLE_VALUE
mov hFileW,eax
mov eax,MAX_BLOCK
.while eax>0
invoke ReadFile,hFileR,addr bBuff,MAX_BLOCK,addr dwBytesR,NULL
invoke WriteFile,hFileW,addr bBuff,dwBytesR,addr dwBytesW,NULL
mov eax,dwBytesR
.endw
invoke CloseHandle,hFileW
.else
invoke GetLastError
;invoke wsprintf,addr szBuff,addr szErrWFile,lpImageFile,eax
;invoke MessageBox,NULL,addr szBuff,NULL,MB_ICONERROR or MB_OK
.endif
invoke CloseHandle,hFileR
.else
invoke GetLastError
;invoke wsprintf,addr szBuff,addr szErrRDisk,lpDrive,eax
;invoke MessageBox,NULL,addr szBuff,NULL,MB_ICONERROR or MB_OK
.endif
xor eax,eax
ret

backupDrive endp

Maybe nt service can't use CreateFile() to access harddisk?
Posted on 2005-05-18 11:32:21 by 13800
I tried the MAX_BLOCK less then 1024*8, it's ok, why?
Posted on 2005-05-18 11:47:46 by 13800
for wsprintf

lpOut
Pointer to a buffer to receive the formatted output. The maximum size of the buffer is 1024 bytes.

:)
Posted on 2005-05-18 12:52:08 by Webring
If you're accessing a drive directly (\\.\x: or \\.\PHYSICALDRIVEx), you need to allocate your buffer on a 512-byte aligned address (use VirtualAlloc instead of local storage), and you need to always read/write a multiple of 512 bytes at a time. (Well, actually not 512 bytes but "sector size", so you should query the device sector size if you want to be 100% foolproof).
Posted on 2005-05-19 07:58:00 by f0dder