Thread: C++ - Trying to use SetFilePointer & SetFilePointerEx to read USB device

  1. #1
    Registered User
    Join Date
    May 2015
    Posts
    56

    Question C++ - Trying to use SetFilePointer & SetFilePointerEx to read USB device

    Hello,

    Windows 10
    gcc 5.3.0

    I'm opening a USB memory stick for read.
    I can read the beginning of the stick ok, then I try to reset
    the file pointer to another position and fail.
    What am I doing wrong?
    Thanks.

    Regards
    Code:
        DWORD dwCurrentFilePosition = SetFilePointer(
                    hdisk, // must have GENERIC_READ and/or GENERIC_WRITE
                    0,     // do not move pointer
                    NULL,  // hdisk is not large enough in needing this pointer
                    FILE_CURRENT); // provides offset from current position
        printf("Current file pointer position: %lu\n", dwCurrentFilePosition);
    Code:
        // position is a LARGE_INTEGER
        position.QuadPart = (__int64)1024;
        printf("LowPart:%lu HighPart:%lu\n", position.LowPart, position.HighPart);
    
        dwCurrentFilePosition = SetFilePointerEx(
                    hdisk,    // must have GENERIC_READ and/or GENERIC_WRITE
                    position, // new position
                    NULL,     // hdisk is not large enough to need this pointer
                    FILE_CURRENT); //provides offset from current position
        printf("New file pointer position: %lu\n", dwCurrentFilePosition);
    Code:
    Current file pointer position: 512 // this is correct, from previous read
    LowPart:1024 HighPart:0
    New file pointer position: 1 // should be 1024

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Second call is to a different function (ends in Ex) - see the return type for that function: SetFilePointerEx function (Windows)

    gg

  3. #3
    Registered User
    Join Date
    May 2015
    Posts
    56

    Post

    Hello Codeplug,

    Thanks for that.
    Reverted to the original method.

    Regards
    Code:
    int newPos = 2048; 
    
    dwCurrentFilePosition = SetFilePointer(
         hdisk, //must have GENERIC_READ and/or GENERIC_WRITE 
         newPos - dwCurrentFilePosition, //relative to current pointer
         NULL, //hdisk is not large enough to need this pointer      
         FILE_CURRENT); //provides offset from current position
    printf("Newfilepointerposition: %lu\n", dwCurrentFilePosition);
    Code:
    New file pointer position: 2048

  4. #4
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Using cl.exe is, like, the most fun part of Windows development though :P

    You'd be surprised at how loose gcc is with automatic conversions. cl.exe is actually pretty stringent.

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by MutantJohn View Post
    You'd be surprised at how loose gcc is with automatic conversions. cl.exe is actually pretty stringent.
    Is it possible that cl defaults to C++ mode? That would make it more stringent!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to read from i2c device with 2 byte internal address register
    By m.mogharabi in forum Networking/Device Communication
    Replies: 3
    Last Post: 02-28-2012, 11:14 PM
  2. Weird characters after device read
    By ojaro in forum Networking/Device Communication
    Replies: 10
    Last Post: 06-04-2010, 09:02 AM
  3. Read all Data from Device
    By haderach in forum C Programming
    Replies: 1
    Last Post: 05-04-2010, 07:40 AM
  4. ReadFile() and SetFilePointer Problems?
    By krishnampkkm in forum C++ Programming
    Replies: 12
    Last Post: 06-23-2009, 10:04 AM
  5. Undeclared Identifier Error for SetFilePointerEx
    By Kan in forum C++ Programming
    Replies: 2
    Last Post: 05-05-2003, 02:13 PM

Tags for this Thread