CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2014
    Posts
    3

    Error with ReadFile and Overlapped

    Hi

    I have a problem with ReadFile and overlapped.

    first I use ReadFile with overlapped with 0

    Code:
    ZeroMemory(&overlapped ,sizeof(OVERLAPPED));
    
    hDevice = CreateFileW(zwpath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,  FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);
    
    if(hDevice != INVALID_HANDLE_VALUE){
    
    ret = ReadFile(hDevice, buff, 1024, &numerobyte, &overlapped);
    
    }
    with a for(), I can see the bytes using a printf()

    Code:
    for (int n=0; n<sizeof(buff); n++)  
           {  
               printf("0x%02X ", buff[n]);  
           }
    and now I have one variant with a large number

    Code:
    crbig = 322122547
    
        d1 = (DWORD*)crbig;
        overlapped.Offset = d1[1]; //22122547
        overlapped.OffsetHigh = d1[0];// 00000003
    I need use ReafFile twice, this second time is using overlapped() with offset and highoffset values

    Code:
     d1 = (DWORD*)&crbig;
    overlapped.Offset = d1[1];
    overlapped.OffsetHigh = d1[0];
    
    ret = ReadFile(hDevice, buff, 1024, &numerobyte, &overlapped);
    but the for() prints the same bytes that throws me the first ReadFile, I tried cleaning the buffer with memset() or use another buffer but always show me the same bytes

    Supposedly Overlapped() says to ReadFile where should go, and read 1024 bytes, and save it in a buffer, but nothing happen, buffer has the same first bytes that overlapped() = 0

    this is the complete code

    Code:
    int main(int argc, char *argv[]){
    
        HANDLE hDevice;
        OVERLAPPED overlapped;
        DWORD crbig;
        BYTE buff[1024] = {0};
        DWORD numerobyte = 0, nbytes = 0;
        UINT32 ret;
        DWORD *d1;
        int offset1 = 11, offset2 = 13, offset3 = 30;
        long MFTCluster = 0, bytespercluster = 0, sectperclusters = 0, offet = 0;
        unsigned long mult = 0;
    
        ZeroMemory(&overlapped ,sizeof(OVERLAPPED));
    
        hDevice = CreateFileW(zwpath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,  FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);
    
        if(hDevice != INVALID_HANDLE_VALUE){
    
            ret = ReadFile(hDevice, buff, 1024, &numerobyte, &overlapped);
    
        }else
        {
            return NULL;
        }
    
        if(ret == 0){
    
    
            ret = WaitForSingleObject(hDevice,INFINITE );
    
            switch (ret)
            {
            case WAIT_OBJECT_0:break;
            case WAIT_TIMEOUT:break;
            default:
                break;
            }
        }
        else
        {
            return NULL;
        }
    
    
        if((int)buff[3] == 'N' && (int)buff[4] == 'T' && (int)buff[5] == 'F' && (int)buff[6] == 'S' ){
            printf("Volumen es formato NTFS\n\n\n");
        }
    
        bytespercluster = endianhextodec(buff, offset1);
        sectperclusters = endianhextodec(buff, offset2);
        MFTCluster = endianhextodec(buff, offset3);
    
        crbig = (sectperclusters * (bytespercluster * MFTCluster));
    
        d1 = (DWORD*)&crbig;
        overlapped.Offset = d1[1];
        overlapped.OffsetHigh = d1[0];
    
        ret = ReadFile(hDevice, buff, 1024, &numerobyte, &overlapped); <<--- here's the error
    
        if(ret == 0){
    
    
            ret = WaitForSingleObject(hDevice,INFINITE );
    
            switch (ret)
            {
            case WAIT_OBJECT_0:break;
            default:
                break;
            }
        }
        else
        {
            return NULL;
        }
    
        for (int n=0; n<sizeof(buff); n++)  
        {  
            printf("0x%02X ", buff[n]);  
        } 
    
        CloseHandle(hDevice);
    
        getchar();
    }
    I try to convert VB.NET code to C code but fail overlapped

    Code:
    Private Sub SetReadFileOffset(ByRef NO As System.Threading.NativeOverlapped, ByRef curBig As Int64)
    
            Dim lowoffset() As Byte = BitConverter.GetBytes(curBig)
            Dim highoffset As Int32 = BitConverter.ToInt32(lowoffset, 0)
            Dim high As Int32
            Dim lastbytes(3) As Byte
            Array.Copy(lowoffset, 4, lastbytes, 0, 4)
            high = BitConverter.ToInt32(lastbytes, 0)
            NO.OffsetLow = highoffset
            NO.OffsetHigh = high
        End Sub
    
     SetReadFileOffset(NO, MFTCluster * SectperCluster * BytesPerSect)
            ret = ReadFile(Hnd, Buffer, 1024, nRead, NO)
            If ret = 0 Then
                ret = WaitForSingleObject(Hnd, INFINITE)
                Select Case ret
                    Case WAIT_OBJECT_0
                    Case Else
                End Select
            Else
                Return Nothing
            End If
    regards

  2. #2
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Error with ReadFile and Overlapped

    you aren't quite getting the concept I fear.

    overlapped io means "asynchronous".


    You issue a read and supply a buffer.
    The system returns from the read() immediately
    the contents of the buffer is "indeterminate" until the overlapped IO completes which is signalled (so you use WaitForSingleObject, or anyone of the other functions that allow you to be informed that the read has completed)

    Only when the object is signalled can you rely on the buffer having the value you intended (or having a read error).


    Overlapped IO only makes sense in a multithreading type environment where you launch a read request but don't immediately need the result but can go do other stuff while the disk is being read. You return to the data when the read has completed.

    if you have a situation where you Always end up with.
    - Read
    - Wait for read to complete
    - use buffer

    Then using overlapped IO is entirely pointless, you could just as well have used a regular (blocking) read instead.

  3. #3
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Error with ReadFile and Overlapped

    Quote Originally Posted by proxy_lainux View Post
    I try to convert VB.NET code to C code but fail overlapped

    Code:
    Private Sub SetReadFileOffset(ByRef NO As System.Threading.NativeOverlapped, ByRef curBig As Int64)
    
            Dim lowoffset() As Byte = BitConverter.GetBytes(curBig)
            Dim highoffset As Int32 = BitConverter.ToInt32(lowoffset, 0)
            Dim high As Int32
            Dim lastbytes(3) As Byte
            Array.Copy(lowoffset, 4, lastbytes, 0, 4)
            high = BitConverter.ToInt32(lastbytes, 0)
            NO.OffsetLow = highoffset
            NO.OffsetHigh = high
        End Sub
    
     SetReadFileOffset(NO, MFTCluster * SectperCluster * BytesPerSect)
            ret = ReadFile(Hnd, Buffer, 1024, nRead, NO)
            If ret = 0 Then
                ret = WaitForSingleObject(Hnd, INFINITE)
                Select Case ret
                    Case WAIT_OBJECT_0
                    Case Else
                End Select
            Else
                Return Nothing
            End If
    regards
    Please note, the wait function waits on file handle itself, but not on the overlapped structure handle. Therefore, the whole trick seems based on the fact that the file is written to, and written of as many bytes as your offset requires. I'm not sure how OVERLAPPED is expected to behave in the situation, i.e. I cannot predict what happens when the file is modified (WaitForSingleObject gets unblocked), but file size now is lesser than the specified read offset.

    There's one important aspect with overlapped reading with offset. The read device must support a concept of seeking.
    Last edited by Igor Vartanov; February 5th, 2014 at 06:52 AM.
    Best regards,
    Igor

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured