Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. SetFilePointerEx not moving pointer

SetFilePointerEx not moving pointer

Scheduled Pinned Locked Moved C#
csharpc++businessjsonquestion
13 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Chris Copeland
    wrote on last edited by
    #1

    Hey there, I'm working on an NTFS Master File Table reader as part of a project for University. I've already got a Console API working in C++ which reads the current $MFT file/table, and i've begun work on porting the code over to C# (as per my requirements), however my version of SetFilePointerEx doesn't seem to move the file buffer pointer. My C++ code:

    BOOL __stdcall SetFileBlockLocation(HANDLE address, LARGE_INTEGER position)
    {
    return SetFilePointerEx(address, position, NULL, FILE_BEGIN);
    }

    My C# code:

    public bool AssignPointerPosition(Int64 position, EFileMove movement)
    {
    if (this.IsBufferReady == false)
    return false;

    if (Win32API.SetFilePointerEx(this.block, (long)position, IntPtr.Zero, movement) == false)
    {
    	this.errorcode = Marshal.GetLastWin32Error();
                this.errorpos = MFTHaltPosition.MoveFile;
                return false;
    }
            
    return true;
    

    }

    Where EFileMove is set to EFileMove.FileBegin (aka. 0) When I run another ReadFile, it continues to read out the same block of data into the buffer (the first four bytes should be FILE, but instead reads the beginning of the NTFS block). Does anyone have an idea why it wouldn't move the pointer forward (position is a value far above 0), and is there a solution? Thank you, Chris

    J H L 3 Replies Last reply
    0
    • C Chris Copeland

      Hey there, I'm working on an NTFS Master File Table reader as part of a project for University. I've already got a Console API working in C++ which reads the current $MFT file/table, and i've begun work on porting the code over to C# (as per my requirements), however my version of SetFilePointerEx doesn't seem to move the file buffer pointer. My C++ code:

      BOOL __stdcall SetFileBlockLocation(HANDLE address, LARGE_INTEGER position)
      {
      return SetFilePointerEx(address, position, NULL, FILE_BEGIN);
      }

      My C# code:

      public bool AssignPointerPosition(Int64 position, EFileMove movement)
      {
      if (this.IsBufferReady == false)
      return false;

      if (Win32API.SetFilePointerEx(this.block, (long)position, IntPtr.Zero, movement) == false)
      {
      	this.errorcode = Marshal.GetLastWin32Error();
                  this.errorpos = MFTHaltPosition.MoveFile;
                  return false;
      }
              
      return true;
      

      }

      Where EFileMove is set to EFileMove.FileBegin (aka. 0) When I run another ReadFile, it continues to read out the same block of data into the buffer (the first four bytes should be FILE, but instead reads the beginning of the NTFS block). Does anyone have an idea why it wouldn't move the pointer forward (position is a value far above 0), and is there a solution? Thank you, Chris

      J Offline
      J Offline
      jchoover
      wrote on last edited by
      #2

      What does your import/p-invoke look like for SetFilePointerEx? What is this.block defined as?

      C 1 Reply Last reply
      0
      • J jchoover

        What does your import/p-invoke look like for SetFilePointerEx? What is this.block defined as?

        C Offline
        C Offline
        Chris Copeland
        wrote on last edited by
        #3

        My SetFilePointerEx import looks like this:

        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern bool SetFilePointerEx(IntPtr fHandle, long lDistance, IntPtr lpNewPointer, EFileMove dwMove);

        And the block attribute is an IntPtr:

        private IntPtr block;

        Which stores the file pointer created by CreateFile, and is used to read the data.

        J 1 Reply Last reply
        0
        • C Chris Copeland

          My SetFilePointerEx import looks like this:

          [DllImport("kernel32.dll", SetLastError = true)]
          public static extern bool SetFilePointerEx(IntPtr fHandle, long lDistance, IntPtr lpNewPointer, EFileMove dwMove);

          And the block attribute is an IntPtr:

          private IntPtr block;

          Which stores the file pointer created by CreateFile, and is used to read the data.

          J Offline
          J Offline
          jchoover
          wrote on last edited by
          #4

          Is SetFilePointerEx returning 0? If so, what is GetLastError reporting? I would recommend using SafeFileHandle instead of IntPtr for the file handle. I assume you are doing a verbatum translation of your C++ code to C#, so that you know the actual sequence of calls you are performing are valid.

          C 1 Reply Last reply
          0
          • C Chris Copeland

            Hey there, I'm working on an NTFS Master File Table reader as part of a project for University. I've already got a Console API working in C++ which reads the current $MFT file/table, and i've begun work on porting the code over to C# (as per my requirements), however my version of SetFilePointerEx doesn't seem to move the file buffer pointer. My C++ code:

            BOOL __stdcall SetFileBlockLocation(HANDLE address, LARGE_INTEGER position)
            {
            return SetFilePointerEx(address, position, NULL, FILE_BEGIN);
            }

            My C# code:

            public bool AssignPointerPosition(Int64 position, EFileMove movement)
            {
            if (this.IsBufferReady == false)
            return false;

            if (Win32API.SetFilePointerEx(this.block, (long)position, IntPtr.Zero, movement) == false)
            {
            	this.errorcode = Marshal.GetLastWin32Error();
                        this.errorpos = MFTHaltPosition.MoveFile;
                        return false;
            }
                    
            return true;
            

            }

            Where EFileMove is set to EFileMove.FileBegin (aka. 0) When I run another ReadFile, it continues to read out the same block of data into the buffer (the first four bytes should be FILE, but instead reads the beginning of the NTFS block). Does anyone have an idea why it wouldn't move the pointer forward (position is a value far above 0), and is there a solution? Thank you, Chris

            H Offline
            H Offline
            Henry Minute
            wrote on last edited by
            #5

            Not that it should make any difference, so far as I can tell, but just in case it triggers something in your brain cell. Is the type of your EFileMove set to uint?

            enum EFileMove : uint
            {
            FileBegin = 0,
            .................
            .................
            {

            The documentation I have read says the dwMoveNethod should be a uint.

            Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

            L 1 Reply Last reply
            0
            • J jchoover

              Is SetFilePointerEx returning 0? If so, what is GetLastError reporting? I would recommend using SafeFileHandle instead of IntPtr for the file handle. I assume you are doing a verbatum translation of your C++ code to C#, so that you know the actual sequence of calls you are performing are valid.

              C Offline
              C Offline
              Chris Copeland
              wrote on last edited by
              #6

              Yes, my EFileMove structure is inheriting uint. When I get chance, i'll try converting the code to use SafeFileHandle. When I run SetFilePointerEx and sending 0 as the distance to move, and setting the file movement to EFileMove.FileEnd, the resulting long value is always 0. It doesn't seem to adjust the pointer at all. I'm wondering if I should try passing a LARGE_INTEGER structure through it instead?

              J 1 Reply Last reply
              0
              • C Chris Copeland

                Yes, my EFileMove structure is inheriting uint. When I get chance, i'll try converting the code to use SafeFileHandle. When I run SetFilePointerEx and sending 0 as the distance to move, and setting the file movement to EFileMove.FileEnd, the resulting long value is always 0. It doesn't seem to adjust the pointer at all. I'm wondering if I should try passing a LARGE_INTEGER structure through it instead?

                J Offline
                J Offline
                jchoover
                wrote on last edited by
                #7

                if SetFilePointerEx is returning false, then you should look at the error code. if (!SetFilePointerEx(...)) { throw new Win32Exception(Marshal.GetLastWin32Error()); }

                C 1 Reply Last reply
                0
                • J jchoover

                  if SetFilePointerEx is returning false, then you should look at the error code. if (!SetFilePointerEx(...)) { throw new Win32Exception(Marshal.GetLastWin32Error()); }

                  C Offline
                  C Offline
                  Chris Copeland
                  wrote on last edited by
                  #8

                  It's not returning false, but the long output for the 3rd argument (if I change the argument type to ref long) returns 0 (which documented by MSDN, would return the position of the pointer, thus the pointer hasn't changed).

                  H 1 Reply Last reply
                  0
                  • C Chris Copeland

                    It's not returning false, but the long output for the 3rd argument (if I change the argument type to ref long) returns 0 (which documented by MSDN, would return the position of the pointer, thus the pointer hasn't changed).

                    H Offline
                    H Offline
                    Henry Minute
                    wrote on last edited by
                    #9

                    The third argument is actually 64 bit unsigned, so strictly speaking long is not the correct cast.

                    Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                    C 1 Reply Last reply
                    0
                    • H Henry Minute

                      The third argument is actually 64 bit unsigned, so strictly speaking long is not the correct cast.

                      Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                      C Offline
                      C Offline
                      Chris Copeland
                      wrote on last edited by
                      #10

                      Edit Nevermind. Seems passing Threading.NativeOverlapped to ReadFile was causing issues with the buffer. And I also changed all IntPtr to SafeFileHandle, and it's working now. Thanks! Previous It makes no difference if I convert all the Int64 types long, it still doesn't adjust the pointer and my SetFilePointerEx(this.block, 0, out pos, EFileMove.FileEnd); still assigns 0 to the pos variable (and the buffer is not changed).

                      modified on Saturday, January 1, 2011 7:41 AM

                      1 Reply Last reply
                      0
                      • H Henry Minute

                        Not that it should make any difference, so far as I can tell, but just in case it triggers something in your brain cell. Is the type of your EFileMove set to uint?

                        enum EFileMove : uint
                        {
                        FileBegin = 0,
                        .................
                        .................
                        {

                        The documentation I have read says the dwMoveNethod should be a uint.

                        Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                        L Offline
                        L Offline
                        Luc Pattyn
                        wrote on last edited by
                        #11

                        the boundary between managed and unmanaged code does not care about the exact numeric type, all it needs is the right amount of bits. Signed and unsigned are irrelevant at run-time, as it used to be in purely native code. :)

                        Luc Pattyn [Forum Guidelines] [My Articles] [My CP bug tracking] Nil Volentibus Arduum

                        Season's Greetings to all CPians.

                        H 1 Reply Last reply
                        0
                        • C Chris Copeland

                          Hey there, I'm working on an NTFS Master File Table reader as part of a project for University. I've already got a Console API working in C++ which reads the current $MFT file/table, and i've begun work on porting the code over to C# (as per my requirements), however my version of SetFilePointerEx doesn't seem to move the file buffer pointer. My C++ code:

                          BOOL __stdcall SetFileBlockLocation(HANDLE address, LARGE_INTEGER position)
                          {
                          return SetFilePointerEx(address, position, NULL, FILE_BEGIN);
                          }

                          My C# code:

                          public bool AssignPointerPosition(Int64 position, EFileMove movement)
                          {
                          if (this.IsBufferReady == false)
                          return false;

                          if (Win32API.SetFilePointerEx(this.block, (long)position, IntPtr.Zero, movement) == false)
                          {
                          	this.errorcode = Marshal.GetLastWin32Error();
                                      this.errorpos = MFTHaltPosition.MoveFile;
                                      return false;
                          }
                                  
                          return true;
                          

                          }

                          Where EFileMove is set to EFileMove.FileBegin (aka. 0) When I run another ReadFile, it continues to read out the same block of data into the buffer (the first four bytes should be FILE, but instead reads the beginning of the NTFS block). Does anyone have an idea why it wouldn't move the pointer forward (position is a value far above 0), and is there a solution? Thank you, Chris

                          L Offline
                          L Offline
                          Luc Pattyn
                          wrote on last edited by
                          #12

                          If you use P/Invoke sufficiently correctly (it does not care about the exact integer types, all that matters is the size of the variables), the C# version should behave the same as the C++ version. And then the MSDN documentation says: "The file pointer returned by this function is not used for overlapped read and write operations. To specify the offset for overlapped operations, use the Offset and OffsetHigh members of the OVERLAPPED structure." Maybe that explains some. :)

                          Luc Pattyn [Forum Guidelines] [My Articles] [My CP bug tracking] Nil Volentibus Arduum

                          Season's Greetings to all CPians.

                          1 Reply Last reply
                          0
                          • L Luc Pattyn

                            the boundary between managed and unmanaged code does not care about the exact numeric type, all it needs is the right amount of bits. Signed and unsigned are irrelevant at run-time, as it used to be in purely native code. :)

                            Luc Pattyn [Forum Guidelines] [My Articles] [My CP bug tracking] Nil Volentibus Arduum

                            Season's Greetings to all CPians.

                            H Offline
                            H Offline
                            Henry Minute
                            wrote on last edited by
                            #13

                            On the way in, I agree. Although I understood that if the unmanaged part filled those bits with what it considered an unsigned value and then on return to the managed code those same bits were treated as or cast to a signed value, there is a possibility of unpredictable results.

                            Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups