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 / C++ / MFC
  4. Reading sectors

Reading sectors

Scheduled Pinned Locked Moved C / C++ / MFC
debugginghelpquestionworkspace
23 Posts 5 Posters 4 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.
  • _ _Flaviu

    Is not accepted:

    unsigned char buffer[dwLen]; // error C2057: expected constant expression

    Of course, putting 512 is ok, but I need to allocate that length of buffer through a variable. I prefer this option, on stack, as far as I know, is faster than heap.

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #11

    You can fix that easily by doing it this way:

    #define SECTOR_SIZE 512
    DWORD dwLen = SECTOR_SIZE;
    DWORD dwNum = 0;
    char buffer[SECTOR_SIZE];
    ReadFile(hVolume, buffer, dwLen, &dwNum, NULL);

    If you are using C++ then instead of #define you can use this:

    const int SECTOR_SIZE = 512

    _ 1 Reply Last reply
    0
    • L Lost User

      You can fix that easily by doing it this way:

      #define SECTOR_SIZE 512
      DWORD dwLen = SECTOR_SIZE;
      DWORD dwNum = 0;
      char buffer[SECTOR_SIZE];
      ReadFile(hVolume, buffer, dwLen, &dwNum, NULL);

      If you are using C++ then instead of #define you can use this:

      const int SECTOR_SIZE = 512

      _ Offline
      _ Offline
      _Flaviu
      wrote on last edited by
      #12

      Good idea. I have tried in this way:

      CByteArray arrByte;
      arrByte.SetSize(512);
      BOOL bRet = ReadFile(hVolume, arrByte.GetData(), dwLen, &dwNum, NULL);
      

      and seem to go well.

      L 1 Reply Last reply
      0
      • _ _Flaviu

        It will interest me, not this time, but in the real program. I modified the code like this:

        LONG lValueLow = 0;
        LONG lValueHigh = 0;
        if (INVALID\_SET\_FILE\_POINTER == SetFilePointer(hVolume, lValueLow, &lValueHigh, FILE\_BEGIN) &&
        	NO\_ERROR != GetLastError())
        {
        	TRACE("Error: %d\\n", GetLastError());
        	return FALSE;
        }
        else
        {
        	TRACE("%d\\t%d\\n", lValueLow, lValueHigh);
        }
        

        Thank you Superman !

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #13

        You do not need lValueHigh unless the distance to move is a 64 bit value: just specify NULL instead. Also I suggest you study the difference between a variable and a pointer to a variable, you seem somewhat confused about it.

        _ 1 Reply Last reply
        0
        • L Lost User

          You do not need lValueHigh unless the distance to move is a 64 bit value: just specify NULL instead. Also I suggest you study the difference between a variable and a pointer to a variable, you seem somewhat confused about it.

          _ Offline
          _ Offline
          _Flaviu
          wrote on last edited by
          #14

          Understood now, I didn't paid attention on that function (lpDistanceToMoveHigh, is obviously now, or, at least I think is obviously).

          1 Reply Last reply
          0
          • _ _Flaviu

            Good idea. I have tried in this way:

            CByteArray arrByte;
            arrByte.SetSize(512);
            BOOL bRet = ReadFile(hVolume, arrByte.GetData(), dwLen, &dwNum, NULL);
            

            and seem to go well.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #15

            If you are writing MFC code then you should be using CFile to handle all your I/O.

            _ 2 Replies Last reply
            0
            • L Lost User

              If you are writing MFC code then you should be using CFile to handle all your I/O.

              _ Offline
              _ Offline
              _Flaviu
              wrote on last edited by
              #16

              I'll try to use CFile Class | Microsoft Docs[^] Can I replace SetFilePointer as well with something from MFC yard ?

              V L 2 Replies Last reply
              0
              • _ _Flaviu

                I'll try to use CFile Class | Microsoft Docs[^] Can I replace SetFilePointer as well with something from MFC yard ?

                V Offline
                V Offline
                Victor Nijegorodov
                wrote on last edited by
                #17

                [CFile Class | Microsoft Docs](https://docs.microsoft.com/en-us/cpp/mfc/reference/cfile-class?view=vs-2019#seek)

                1 Reply Last reply
                0
                • _ _Flaviu

                  I'll try to use CFile Class | Microsoft Docs[^] Can I replace SetFilePointer as well with something from MFC yard ?

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #18

                  CFile Class.Seek | Microsoft Docs[^]. You need to read through all the documentation for the class to see what is available.

                  _ 1 Reply Last reply
                  0
                  • L Lost User

                    CFile Class.Seek | Microsoft Docs[^]. You need to read through all the documentation for the class to see what is available.

                    _ Offline
                    _ Offline
                    _Flaviu
                    wrote on last edited by
                    #19

                    I should know that ... thank you.

                    1 Reply Last reply
                    0
                    • L Lost User

                      If you are writing MFC code then you should be using CFile to handle all your I/O.

                      _ Offline
                      _ Offline
                      _Flaviu
                      wrote on last edited by
                      #20

                      Good idea. I have few troubles with accessing USB drive with CFile, but once I'll solve it, the code will be simple. Here is my trial, none on them has worked:

                      file.Open(_T("\\\\.\\F:"), CFile::modeRead | CFile::osSequentialScan); // return FALSE

                      file.Open(_T("F:"), CFile::modeRead | CFile::osSequentialScan); // return FALSE

                      file.Open(_T("F:\\"), CFile::modeRead | CFile::osSequentialScan); // return FALSE

                      HANDLE hVolume = ::CreateFile(sVolume, GENERIC\_READ,
                      	FILE\_SHARE\_READ | FILE\_SHARE\_WRITE, 
                      	NULL, OPEN\_EXISTING, FILE\_FLAG\_SEQUENTIAL\_SCAN, NULL);
                      CFile file(hVolume);
                      CFileStatus status;
                      file.GetStatus(status); // return FALSE
                      
                      V L 2 Replies Last reply
                      0
                      • _ _Flaviu

                        Good idea. I have few troubles with accessing USB drive with CFile, but once I'll solve it, the code will be simple. Here is my trial, none on them has worked:

                        file.Open(_T("\\\\.\\F:"), CFile::modeRead | CFile::osSequentialScan); // return FALSE

                        file.Open(_T("F:"), CFile::modeRead | CFile::osSequentialScan); // return FALSE

                        file.Open(_T("F:\\"), CFile::modeRead | CFile::osSequentialScan); // return FALSE

                        HANDLE hVolume = ::CreateFile(sVolume, GENERIC\_READ,
                        	FILE\_SHARE\_READ | FILE\_SHARE\_WRITE, 
                        	NULL, OPEN\_EXISTING, FILE\_FLAG\_SEQUENTIAL\_SCAN, NULL);
                        CFile file(hVolume);
                        CFileStatus status;
                        file.GetStatus(status); // return FALSE
                        
                        V Offline
                        V Offline
                        Victor Nijegorodov
                        wrote on last edited by
                        #21

                        When using CFile::Open you should pass the CFileException* parameter to get the failure cause if CFile::Open fails. See the example in [CFile::Open](https://docs.microsoft.com/en-us/cpp/mfc/reference/cfile-class?view=vs-2019#open)

                        1 Reply Last reply
                        0
                        • _ _Flaviu

                          Good idea. I have few troubles with accessing USB drive with CFile, but once I'll solve it, the code will be simple. Here is my trial, none on them has worked:

                          file.Open(_T("\\\\.\\F:"), CFile::modeRead | CFile::osSequentialScan); // return FALSE

                          file.Open(_T("F:"), CFile::modeRead | CFile::osSequentialScan); // return FALSE

                          file.Open(_T("F:\\"), CFile::modeRead | CFile::osSequentialScan); // return FALSE

                          HANDLE hVolume = ::CreateFile(sVolume, GENERIC\_READ,
                          	FILE\_SHARE\_READ | FILE\_SHARE\_WRITE, 
                          	NULL, OPEN\_EXISTING, FILE\_FLAG\_SEQUENTIAL\_SCAN, NULL);
                          CFile file(hVolume);
                          CFileStatus status;
                          file.GetStatus(status); // return FALSE
                          
                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #22

                          It is just the same as OpenFile behind the scenes so nothing to do with CFile. Your call probably fails because you are not including the share flags in your call to CFile::Open(), which are required when trying to access a device.

                          _ 1 Reply Last reply
                          0
                          • L Lost User

                            It is just the same as OpenFile behind the scenes so nothing to do with CFile. Your call probably fails because you are not including the share flags in your call to CFile::Open(), which are required when trying to access a device.

                            _ Offline
                            _ Offline
                            _Flaviu
                            wrote on last edited by
                            #23

                            Thanks, solved.

                            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