how can i solve this error in my win32 reading image file code

user7242752

I couldnt solve this problem. I dont know why im getting this error message.

Exception thrown at 0x7642DEB5 (KernelBase.dll) in Project2.exe: 0xC0000005: Access violation writing location 0x00000000.

Error at ReadFile(file,lpBuffer, nNumberOfBytesToRead-1, NULL, NULL)

Here my code. I am trying to access a JPG file to read its header.

#include<Windows.h>
#include<iostream>

int main()
{
    LPCTSTR path = "jpg.jpg";
    DWORD nNumberOfBytesToRead;

    HANDLE file = CreateFile(path, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

    if (file == INVALID_HANDLE_VALUE)
    {
        std::cout << "The file couldnt be opened" << std::endl;
    }

    nNumberOfBytesToRead = GetFileSize(file, NULL);

    BYTE *lpBuffer = new BYTE[nNumberOfBytesToRead-1] ;

    if (ReadFile(file,lpBuffer, nNumberOfBytesToRead-1, NULL, NULL))
    {
        delete[] lpBuffer;
        CloseHandle(file);
        std::cout << "The file couldnt be read" << std::endl;
    }
    CloseHandle(file);
    delete[] lpBuffer;

    if (file != 0)
    {
        std::cout << "The file has been closed" << std::endl;
    }

    system("PAUSE");
    return 0;
}

Thank you i have solved this problem. I have an another problem

lpBuffer = 0xcccccccc Error reading characters of string.>

enter image description here

Here my new code.

#include<Windows.h>
#include<iostream>

int main()
{
LPCTSTR path = "jpg.jpg";
DWORD nNumberOfBytesToRead = 0;
DWORD nNumberOfBytesRead = 0;
HANDLE file = CreateFile(path, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

if (file == INVALID_HANDLE_VALUE)
{
    std::cout << "The file couldnt be opened" << std::endl;
}

nNumberOfBytesToRead = GetFileSize(file, NULL);

BYTE *lpBuffer = new BYTE[nNumberOfBytesToRead];

if (ReadFile(file, lpBuffer, nNumberOfBytesToRead, &nNumberOfBytesRead, NULL))
{
    std::cout << "The file couldnt be read" << std::endl;
}

CancelIo(file);
CloseHandle(file);
delete[] lpBuffer;

system("PAUSE");
return 0;
}
Remy Lebeau

The error message is telling you that the Access Violation is due to memory address 0x00000000 being written to.

This is because you are passing a NULL pointer to the lpNumberOfBytesRead parameter of ReadFile().

Per the ReadFile() documentation:

lpNumberOfBytesRead [out, optional]

A pointer to the variable that receives the number of bytes read when using a synchronous hFile parameter. ReadFile sets this value to zero before doing any work or error checking. Use NULL for this parameter if this is an asynchronous operation to avoid potentially erroneous results.

This parameter can be NULL only when the lpOverlapped parameter is not NULL.

You are passing NULL to lpOverlapped, so you CANNOT pass NULL to lpNumberOfBytesRead. You MUST pass a pointer to an allocated DWORD to receive the number of bytes actually read, eg:

DWORD nNumberOfBytesRead;
...
if (ReadFile(file, lpBuffer, nNumberOfBytesToRead-1, &nNumberOfBytesRead, NULL))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Error within my code how can I centre responsive image?

From Dev

My code is leaking. How can I solve it?

From Dev

How can I manually add a resource to a Win32 resource file

From Dev

C++ Win32 how can i put this functions on separate cpp file?

From Java

How can I solve my connection error in mysql

From Dev

How can i made my app run and solve this error

From Dev

How can I solve this stream reading problem?

From Dev

What are the capitalized text identifiers for Win32 error codes called, and how can they be determined programmatically, given an error code?

From Dev

How do I deal with placeholders for Win32 error messages?

From Dev

My image upload option is not working in my codeigniter application.How can i get solve this?

From Dev

How can I solve reading MQ messages resulting in error on commit/rollback?

From Dev

How can i solve my bootstrap grid?

From Dev

How to solve an error in reading a merged cell value from an excel file?

From Dev

How can i solve duplication of code in this?

From Dev

How can I solve the errors in this DirectX code?

From Dev

i have used image as navbar brand in my header but the image comes out of the header how can i solve this in bootstrap?

From Dev

how can i to redirect my code with the same file?

From Dev

How can I fix this error in my code for an assignment?

From Dev

How can i solve my code to check size of data type (is c language with gcc compiler) on linux mint

From Dev

win32 GetConsoleMode() error code 6

From Dev

win32 GetConsoleMode() error code 6

From Dev

How can I solve this error in Flash game?

From Dev

How can i solve this java lang error?

From Dev

How can i solve this type of mysql error

From Dev

how can i solve this toString() error?

From Dev

How can I solve ACPI error?

From Dev

How can I solve "Runtime Error" on OJ

From Dev

how can i add button to this code to apply this effect on my image i am making an image editor app

From Dev

How can I solve the error "'Node' object has no attribute 'output_masks" in my keras fine tunning?

Related Related

  1. 1

    Error within my code how can I centre responsive image?

  2. 2

    My code is leaking. How can I solve it?

  3. 3

    How can I manually add a resource to a Win32 resource file

  4. 4

    C++ Win32 how can i put this functions on separate cpp file?

  5. 5

    How can I solve my connection error in mysql

  6. 6

    How can i made my app run and solve this error

  7. 7

    How can I solve this stream reading problem?

  8. 8

    What are the capitalized text identifiers for Win32 error codes called, and how can they be determined programmatically, given an error code?

  9. 9

    How do I deal with placeholders for Win32 error messages?

  10. 10

    My image upload option is not working in my codeigniter application.How can i get solve this?

  11. 11

    How can I solve reading MQ messages resulting in error on commit/rollback?

  12. 12

    How can i solve my bootstrap grid?

  13. 13

    How to solve an error in reading a merged cell value from an excel file?

  14. 14

    How can i solve duplication of code in this?

  15. 15

    How can I solve the errors in this DirectX code?

  16. 16

    i have used image as navbar brand in my header but the image comes out of the header how can i solve this in bootstrap?

  17. 17

    how can i to redirect my code with the same file?

  18. 18

    How can I fix this error in my code for an assignment?

  19. 19

    How can i solve my code to check size of data type (is c language with gcc compiler) on linux mint

  20. 20

    win32 GetConsoleMode() error code 6

  21. 21

    win32 GetConsoleMode() error code 6

  22. 22

    How can I solve this error in Flash game?

  23. 23

    How can i solve this java lang error?

  24. 24

    How can i solve this type of mysql error

  25. 25

    how can i solve this toString() error?

  26. 26

    How can I solve ACPI error?

  27. 27

    How can I solve "Runtime Error" on OJ

  28. 28

    how can i add button to this code to apply this effect on my image i am making an image editor app

  29. 29

    How can I solve the error "'Node' object has no attribute 'output_masks" in my keras fine tunning?

HotTag

Archive