ReadFile giving "invalid access to memory location"

This is a discussion on ReadFile giving "invalid access to memory location" within the Windows Programming forums, part of the Platform Specific Boards category; I'm writing a plugin for another application. I have successfully been able to use WriteFile(), but I am getting errors ...

  1. #1
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640

    ReadFile giving "invalid access to memory location"

    I'm writing a plugin for another application. I have successfully been able to use WriteFile(), but I am getting errors with ReadFile().

    Code:
    void loadLayers()
    {
    	DWORD dwRead = 0;
    	hLayerFile = CreateFile("ltmp.dat", 
                                                         GENERIC_READ, 
                                                         FILE_SHARE_READ, 
                                                         NULL, 
                                                         OPEN_EXISTING, 
                                                         FILE_ATTRIBUTE_NORMAL, 
                                                         NULL);
    	char *urls = NULL;
    	DWORD fileSize = GetFileSize(hLayerFile, NULL);
    
    	if (ReadFile(hLayerFile, urls, fileSize, &dwRead, NULL) == 0)
    	{//ReadFile() does return 0 and here I call GetLastError()
    GetLastError() returns "invalid access to memory location". I'm confused, and have even tried subtracting and adding 1 to fileSize, but I can't see what the problem is. Any help?
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    You declare urls but don't allocate any memory for it (see ReadFile). Also, you should check the return value of CreateFile to ensure a successful operation.
    Code:
    DWORD dwRead;
    hLayerFile = CreateFile("ltmp.dat", GENERIC_READ, FILE_SHARE_READ, 
                             NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    if (hLayerFile!=INVALID_HANDLE_VALUE)
      {
      DWORD fileSize = GetFileSize(hLayerFile, NULL);
      char *urls = new char[filesize];  //don't forget to delete[] urls when done
      if (ReadFile(hLayerFile, urls, fileSize, &dwRead, NULL) == 0)
      //etc
      }
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Yea, I check CreateFile, but I took it out to size down the code to look at. I probably should have mentioned that. Thanks for the help.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  2. Onboard video card memory access
    By HermioneFan in forum Game Programming
    Replies: 1
    Last Post: 05-28-2003, 09:53 AM
  3. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM
  4. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM
  5. Memory Access Error when using Strcpy()
    By fgs3124 in forum C Programming
    Replies: 2
    Last Post: 03-15-2002, 02:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21