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. VirualAlloc and ReadFile

VirualAlloc and ReadFile

Scheduled Pinned Locked Moved C / C++ / MFC
debuggingperformancequestion
13 Posts 3 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
    csrss
    wrote on last edited by
    #1

    So, got no idea why this is not working but it simply not working.

        SIZE\_T BASE\_BUFFER\_SIZE = 512;
    
    BYTE \* bInitialBuffer = NULL;
    bInitialBuffer = (BYTE \*)VirtualAlloc(0, 
    		BASE\_BUFFER\_SIZE, MEM\_RESERVE, PAGE\_READWRITE);
    
    if(bInitialBuffer == NULL)
    {
    	// handle this 
    	TRACE("VirtualAlloc failed!");
    	return NULL;
    }
    DWORD dwReadBytes;
    while (FALSE != ReadFile(hDevice, bInitialBuffer, BASE\_BUFFER\_SIZE, 
    	(LPDWORD)&dwReadBytes, 0) && dwReadBytes > 0)
    {
    	TRACE1("got buffer! %d",dwReadBytes );
        }
        if(dwReadBytes <= 0) TRACE("failed!");
    

    GetLAstError() returns Invalid access to memory location. I am dealing with virtual alloc for a first time :P What am i doing wrong? Thanks

    011011010110000101100011011010000110100101101110 0110010101110011

    R 1 Reply Last reply
    0
    • C csrss

      So, got no idea why this is not working but it simply not working.

          SIZE\_T BASE\_BUFFER\_SIZE = 512;
      
      BYTE \* bInitialBuffer = NULL;
      bInitialBuffer = (BYTE \*)VirtualAlloc(0, 
      		BASE\_BUFFER\_SIZE, MEM\_RESERVE, PAGE\_READWRITE);
      
      if(bInitialBuffer == NULL)
      {
      	// handle this 
      	TRACE("VirtualAlloc failed!");
      	return NULL;
      }
      DWORD dwReadBytes;
      while (FALSE != ReadFile(hDevice, bInitialBuffer, BASE\_BUFFER\_SIZE, 
      	(LPDWORD)&dwReadBytes, 0) && dwReadBytes > 0)
      {
      	TRACE1("got buffer! %d",dwReadBytes );
          }
          if(dwReadBytes <= 0) TRACE("failed!");
      

      GetLAstError() returns Invalid access to memory location. I am dealing with virtual alloc for a first time :P What am i doing wrong? Thanks

      011011010110000101100011011010000110100101101110 0110010101110011

      R Offline
      R Offline
      Rajesh R Subramanian
      wrote on last edited by
      #2

      csrss wrote:

      VirtualAlloc(0, BASE_BUFFER_SIZE, MEM_RESERVE, PAGE_READWRITE);

      Just a hunch. The doc says that MEM_RESERVE flag Reserves a range of the process's virtual address space without allocating any actual physical storage in memory or in the paging file on disk. If you're actually going to do something with the memory, MEM_COMMIT may be a good option (you could combine flags, like MEM_RESERVE | MEM_COMMIT). At least the doc[^] says so. You also failed to tell what's "not working". I'm assuming that your ReadFile call failed, and GetLastError returns invalid access to memory location. Reading that, I'm assuming that since you're just reserving memory with your VirtualAlloc call, and not committing it before using, you're getting an invalid access error.

      "Real men drive manual transmission" - Rajesh.

      C 1 Reply Last reply
      0
      • R Rajesh R Subramanian

        csrss wrote:

        VirtualAlloc(0, BASE_BUFFER_SIZE, MEM_RESERVE, PAGE_READWRITE);

        Just a hunch. The doc says that MEM_RESERVE flag Reserves a range of the process's virtual address space without allocating any actual physical storage in memory or in the paging file on disk. If you're actually going to do something with the memory, MEM_COMMIT may be a good option (you could combine flags, like MEM_RESERVE | MEM_COMMIT). At least the doc[^] says so. You also failed to tell what's "not working". I'm assuming that your ReadFile call failed, and GetLastError returns invalid access to memory location. Reading that, I'm assuming that since you're just reserving memory with your VirtualAlloc call, and not committing it before using, you're getting an invalid access error.

        "Real men drive manual transmission" - Rajesh.

        C Offline
        C Offline
        csrss
        wrote on last edited by
        #3

        Yep, you are right. But here with this stuff i got more and more problems. For example, i cannot return bytes from function, (this code is a function, which should read a file into BYTE array and return this array among with nr of bytes read).

        BYTE *ReadBytes(HANDLE hDevice, LPDWORD lpdwBytesRead)
        {
        SIZE_T BASE_BUFFER_SIZE = 512;

        BYTE \* bInitialBuffer = NULL;
        bInitialBuffer = (BYTE \*)VirtualAlloc(0, 
        		BASE\_BUFFER\_SIZE, MEM\_COMMIT | MEM\_RESERVE, PAGE\_READWRITE);
        
        if(bInitialBuffer == NULL)
        {
        	// handle this shit
        	TRACE("VirtualAlloc failed!");
        	return NULL;
        }
        DWORD dwReadBytes;
        while (FALSE != ReadFile(hDevice, bInitialBuffer, BASE\_BUFFER\_SIZE, 
        	(LPDWORD)&dwReadBytes, 0) && dwReadBytes > 0)
        {
        	TRACE1("got some buffer! %d", dwReadBytes);
        	lpdwBytesRead += dwReadBytes;
        }
        return bInitialBuffer;
        

        }

        and if i call it:

        DWORD read = 0;
        BYTE *DataBuffer = ReadBytes(hFile, &read);

        read is always zero, and i got no bytes returned from function... And i cannot use just defined size (skip memory allocation). Help ;(

        011011010110000101100011011010000110100101101110 0110010101110011

        R D 3 Replies Last reply
        0
        • C csrss

          Yep, you are right. But here with this stuff i got more and more problems. For example, i cannot return bytes from function, (this code is a function, which should read a file into BYTE array and return this array among with nr of bytes read).

          BYTE *ReadBytes(HANDLE hDevice, LPDWORD lpdwBytesRead)
          {
          SIZE_T BASE_BUFFER_SIZE = 512;

          BYTE \* bInitialBuffer = NULL;
          bInitialBuffer = (BYTE \*)VirtualAlloc(0, 
          		BASE\_BUFFER\_SIZE, MEM\_COMMIT | MEM\_RESERVE, PAGE\_READWRITE);
          
          if(bInitialBuffer == NULL)
          {
          	// handle this shit
          	TRACE("VirtualAlloc failed!");
          	return NULL;
          }
          DWORD dwReadBytes;
          while (FALSE != ReadFile(hDevice, bInitialBuffer, BASE\_BUFFER\_SIZE, 
          	(LPDWORD)&dwReadBytes, 0) && dwReadBytes > 0)
          {
          	TRACE1("got some buffer! %d", dwReadBytes);
          	lpdwBytesRead += dwReadBytes;
          }
          return bInitialBuffer;
          

          }

          and if i call it:

          DWORD read = 0;
          BYTE *DataBuffer = ReadBytes(hFile, &read);

          read is always zero, and i got no bytes returned from function... And i cannot use just defined size (skip memory allocation). Help ;(

          011011010110000101100011011010000110100101101110 0110010101110011

          R Offline
          R Offline
          Rajesh R Subramanian
          wrote on last edited by
          #4

          One step at a time. Have you progressed from your previous error, or not? With the MEM_RESERVE flag, do you still get an invalid access error while calling ReadFile?

          "Real men drive manual transmission" - Rajesh.

          C 1 Reply Last reply
          0
          • C csrss

            Yep, you are right. But here with this stuff i got more and more problems. For example, i cannot return bytes from function, (this code is a function, which should read a file into BYTE array and return this array among with nr of bytes read).

            BYTE *ReadBytes(HANDLE hDevice, LPDWORD lpdwBytesRead)
            {
            SIZE_T BASE_BUFFER_SIZE = 512;

            BYTE \* bInitialBuffer = NULL;
            bInitialBuffer = (BYTE \*)VirtualAlloc(0, 
            		BASE\_BUFFER\_SIZE, MEM\_COMMIT | MEM\_RESERVE, PAGE\_READWRITE);
            
            if(bInitialBuffer == NULL)
            {
            	// handle this shit
            	TRACE("VirtualAlloc failed!");
            	return NULL;
            }
            DWORD dwReadBytes;
            while (FALSE != ReadFile(hDevice, bInitialBuffer, BASE\_BUFFER\_SIZE, 
            	(LPDWORD)&dwReadBytes, 0) && dwReadBytes > 0)
            {
            	TRACE1("got some buffer! %d", dwReadBytes);
            	lpdwBytesRead += dwReadBytes;
            }
            return bInitialBuffer;
            

            }

            and if i call it:

            DWORD read = 0;
            BYTE *DataBuffer = ReadBytes(hFile, &read);

            read is always zero, and i got no bytes returned from function... And i cannot use just defined size (skip memory allocation). Help ;(

            011011010110000101100011011010000110100101101110 0110010101110011

            R Offline
            R Offline
            Rajesh R Subramanian
            wrote on last edited by
            #5

            csrss wrote:

            i cannot return bytes from function, (this code is a function, which should read a file into BYTE array and return this array among with nr of bytes read).

            This has nothing to do with VirtualAlloc.

            "Real men drive manual transmission" - Rajesh.

            C 1 Reply Last reply
            0
            • R Rajesh R Subramanian

              One step at a time. Have you progressed from your previous error, or not? With the MEM_RESERVE flag, do you still get an invalid access error while calling ReadFile?

              "Real men drive manual transmission" - Rajesh.

              C Offline
              C Offline
              csrss
              wrote on last edited by
              #6

              At this moment:

              while (FALSE != ReadFile(hDevice, bInitialBuffer, BASE_BUFFER_SIZE,
              (LPDWORD)&dwReadBytes, 0) && dwReadBytes > 0)
              {
              TRACE1("got some buffer! %d", dwReadBytes);

              dwReadBytes contains right value, which is 106 bytes, that is exactly the size of my file (just a text file). Here :

              lpdwBytesRead += dwReadBytes;

              It tries to append them, but lpdwBytesRead contain some trash, even if it has been initialized outside the function to zero:

              DWORD read = 0;
              BYTE *DataBuffer = ReadBytes(hFile, &read);

              When function returns, read is zero and there is no buffer returned :(

              011011010110000101100011011010000110100101101110 0110010101110011

              R 1 Reply Last reply
              0
              • R Rajesh R Subramanian

                csrss wrote:

                i cannot return bytes from function, (this code is a function, which should read a file into BYTE array and return this array among with nr of bytes read).

                This has nothing to do with VirtualAlloc.

                "Real men drive manual transmission" - Rajesh.

                C Offline
                C Offline
                csrss
                wrote on last edited by
                #7

                I know it is not perfect :P (the code) I have never worked with unsigned type allocation before. (the only proper way for returning unsigned type from function i know is to declare buffer size and make this, BYTE, static) If i pass a pointer to BYTE to this function from calling function, it still returns empty BYTE buffer. I am out of ideas. Somebody, help? :P

                011011010110000101100011011010000110100101101110 0110010101110011

                C 1 Reply Last reply
                0
                • C csrss

                  I know it is not perfect :P (the code) I have never worked with unsigned type allocation before. (the only proper way for returning unsigned type from function i know is to declare buffer size and make this, BYTE, static) If i pass a pointer to BYTE to this function from calling function, it still returns empty BYTE buffer. I am out of ideas. Somebody, help? :P

                  011011010110000101100011011010000110100101101110 0110010101110011

                  C Offline
                  C Offline
                  csrss
                  wrote on last edited by
                  #8

                  I got it!!! YEah!

                  *lpdwBytesRead += dwReadBytes;

                  Its just too much on my head, too much...

                  011011010110000101100011011010000110100101101110 0110010101110011

                  R 1 Reply Last reply
                  0
                  • C csrss

                    Yep, you are right. But here with this stuff i got more and more problems. For example, i cannot return bytes from function, (this code is a function, which should read a file into BYTE array and return this array among with nr of bytes read).

                    BYTE *ReadBytes(HANDLE hDevice, LPDWORD lpdwBytesRead)
                    {
                    SIZE_T BASE_BUFFER_SIZE = 512;

                    BYTE \* bInitialBuffer = NULL;
                    bInitialBuffer = (BYTE \*)VirtualAlloc(0, 
                    		BASE\_BUFFER\_SIZE, MEM\_COMMIT | MEM\_RESERVE, PAGE\_READWRITE);
                    
                    if(bInitialBuffer == NULL)
                    {
                    	// handle this shit
                    	TRACE("VirtualAlloc failed!");
                    	return NULL;
                    }
                    DWORD dwReadBytes;
                    while (FALSE != ReadFile(hDevice, bInitialBuffer, BASE\_BUFFER\_SIZE, 
                    	(LPDWORD)&dwReadBytes, 0) && dwReadBytes > 0)
                    {
                    	TRACE1("got some buffer! %d", dwReadBytes);
                    	lpdwBytesRead += dwReadBytes;
                    }
                    return bInitialBuffer;
                    

                    }

                    and if i call it:

                    DWORD read = 0;
                    BYTE *DataBuffer = ReadBytes(hFile, &read);

                    read is always zero, and i got no bytes returned from function... And i cannot use just defined size (skip memory allocation). Help ;(

                    011011010110000101100011011010000110100101101110 0110010101110011

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #9

                    csrss wrote:

                    lpdwBytesRead += dwReadBytes;

                    This looks suspect. You are incrementing the pointer instead of what the pointer points to. Is that your intent?

                    "One man's wage rise is another man's price increase." - Harold Wilson

                    "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                    "Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather

                    C 1 Reply Last reply
                    0
                    • C csrss

                      At this moment:

                      while (FALSE != ReadFile(hDevice, bInitialBuffer, BASE_BUFFER_SIZE,
                      (LPDWORD)&dwReadBytes, 0) && dwReadBytes > 0)
                      {
                      TRACE1("got some buffer! %d", dwReadBytes);

                      dwReadBytes contains right value, which is 106 bytes, that is exactly the size of my file (just a text file). Here :

                      lpdwBytesRead += dwReadBytes;

                      It tries to append them, but lpdwBytesRead contain some trash, even if it has been initialized outside the function to zero:

                      DWORD read = 0;
                      BYTE *DataBuffer = ReadBytes(hFile, &read);

                      When function returns, read is zero and there is no buffer returned :(

                      011011010110000101100011011010000110100101101110 0110010101110011

                      R Offline
                      R Offline
                      Rajesh R Subramanian
                      wrote on last edited by
                      #10

                      This piece of code works (you had trouble bringing the buffer to the calling function, which is resolved here)

                      #define BASE_BUFFER_SIZE 512

                      BOOL ReadBytes(HANDLE hFile, PBYTE bInitialBuffer)
                      {
                      DWORD dwReadBytes;
                      while (FALSE != ReadFile(hFile, bInitialBuffer, BASE_BUFFER_SIZE,
                      (LPDWORD)&dwReadBytes, 0) && dwReadBytes > 0)
                      {
                      /*CString str;
                      str.Format(L"got buffer! %d",dwReadBytes);
                      AfxMessageBox(str);*/
                      }
                      if(dwReadBytes <= 0)
                      {
                      return FALSE;
                      }
                      return TRUE;
                      }

                      void CMTest1Dlg::OnBnClickedOk()
                      {
                      HANDLE hFile = NULL;

                      hFile = CreateFile(L"C:\\\\temp\\\\myfile.txt", GENERIC\_READ, 0, 0, OPEN\_EXISTING, 0, 0);
                      if(INVALID\_HANDLE\_VALUE == hFile){
                      	AfxMessageBox(L"CreateFile failed");
                      	return;
                      }
                      
                      BYTE \*buffer = NULL;
                      buffer = (BYTE \*)VirtualAlloc(0, BASE\_BUFFER\_SIZE, MEM\_COMMIT | MEM\_RESERVE, PAGE\_READWRITE);
                      
                      if(NULL == buffer)
                      {
                      	AfxMessageBox(L"VirtualAlloc failed!");
                      	return;
                      }
                      BOOL bRead = ReadBytes(hFile, buffer);
                      //use buffer here
                      

                      }

                      "Real men drive manual transmission" - Rajesh.

                      1 Reply Last reply
                      0
                      • C csrss

                        I got it!!! YEah!

                        *lpdwBytesRead += dwReadBytes;

                        Its just too much on my head, too much...

                        011011010110000101100011011010000110100101101110 0110010101110011

                        R Offline
                        R Offline
                        Rajesh R Subramanian
                        wrote on last edited by
                        #11

                        Everything's good now? :) The piece of code I've given above in my other reply doesn't involve any pointer arithmetic.

                        "Real men drive manual transmission" - Rajesh.

                        C 1 Reply Last reply
                        0
                        • R Rajesh R Subramanian

                          Everything's good now? :) The piece of code I've given above in my other reply doesn't involve any pointer arithmetic.

                          "Real men drive manual transmission" - Rajesh.

                          C Offline
                          C Offline
                          csrss
                          wrote on last edited by
                          #12

                          For now i have solved the issue with simple text file and one time allocation :) Now, it is time to read mp4 file into buffer and make a copy of it :X

                          011011010110000101100011011010000110100101101110 0110010101110011

                          1 Reply Last reply
                          0
                          • D David Crow

                            csrss wrote:

                            lpdwBytesRead += dwReadBytes;

                            This looks suspect. You are incrementing the pointer instead of what the pointer points to. Is that your intent?

                            "One man's wage rise is another man's price increase." - Harold Wilson

                            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                            "Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather

                            C Offline
                            C Offline
                            csrss
                            wrote on last edited by
                            #13

                            It was very suspicious in fact! Yes, i have made a disaster mistake and it is corrected by now :)

                            011011010110000101100011011010000110100101101110 0110010101110011

                            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