I'm trying to read raw bytes from a removable drive using the WinAPI functions (_WinAPI_CreateFile, _WinAPI_SetFilePointer, _WinAPI_ReadFile).
Here is the MSDN reference for CreateFile and ReadFile.
My removable drive is mounted at Z:. (But for testing you can probably used a fixed drive like C:. It gives me the same results.)
After some trial and error, I was able to get the CreateFile function to return a (seemingly) valid handle. It seems to work correctly if I enter the drive like this: "\\.\Z:", provided that the share mode includes read/write. (If I don't set the share mode like that, then the returned handle is zero for some reason.) Entering "\\.\PhysicalDrive2" also seems to work, and is also a little more lenient on the share mode.
The problem is that when I pass it to the ReadFile function, it reports zero bytes read and the data seems to be just zeros.
Any ideas?
Here is the MSDN reference for CreateFile and ReadFile.
My removable drive is mounted at Z:. (But for testing you can probably used a fixed drive like C:. It gives me the same results.)
After some trial and error, I was able to get the CreateFile function to return a (seemingly) valid handle. It seems to work correctly if I enter the drive like this: "\\.\Z:", provided that the share mode includes read/write. (If I don't set the share mode like that, then the returned handle is zero for some reason.) Entering "\\.\PhysicalDrive2" also seems to work, and is also a little more lenient on the share mode.
The problem is that when I pass it to the ReadFile function, it reports zero bytes read and the data seems to be just zeros.
Any ideas?
[ autoit ]
#RequireAdmin #AutoIt3Wrapper_UseX64=n #include <WinAPI.au3> $sFileName = "\\.\C:"; $iCreation = 2; OPEN_EXISTING $iAccess = 2; GENERIC_READ $iShare = 2+4; FILE_SHARE_READ + FILE_SHARE_WRITE $hFile = _WinAPI_CreateFile($sFileName, $iCreation, $iAccess, $iShare) If @error Or $hFile = Ptr(0) Then $str = "Could not open file "&$sfilename&@CRLF $str &= "Error: "&@error&@CRLF $str &= "Handle: "&$hFile MsgBox(0,"Error: _WinAPI_CreateFile",$str) Exit EndIf MsgBox(0,"Success: _WinAPI_CreateFile ","Handle: "&$hFile) _WinAPI_SetFilePointer($hFile,0); Beginning of file If @error Then MsgBox(0,"Error: _WinAPI_SetFilePointer",StringFormat("Could not move pointer. (Error %d)\n",@error)) EndIf Global $nBytesReceived $tBuffer = DllStructCreate("byte[512]") _WinAPI_ReadFile($hFile, DllStructGetPtr($tBuffer,1), 512, $nBytesReceived) If @error Then MsgBox(0,"Error: _WinAPI_ReadFile",StringFormat("Could not read file. (Error %d)\n",@error)) EndIf If $nBytesReceived<512 Then MsgBox(0,"Error: _WinAPI_ReadFile",StringFormat("Only %d bytes read.\n",$nBytesReceived)) Else MsgBox(0,"Success: _WinAPI_ReadFile",StringFormat("Read %d bytes.\n",$nBytesReceived)) EndIf _WinAPI_CloseHandle($hFile) $sText = Hex(DllStructGetData($tBuffer,1,1),2);Just the first byte MsgBox(0,"Result",$sText)