Well, there's a task to process text file using WinAPI functions and asm nested into C++. When I try to invoke Readfile I'm getting error
Unhandled exception at 0x7692DEB5 (KernelBase.dll) в XXX.exe: 0xC0000005: violation of access rights when writing to 0x00000000
#include "stdafx.h"
#include <Windows.h>
#include <fileapi.h>
#include <string>
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
char filename[256] = "text.txt"; //name of file with text
OFSTRUCT buffer;
HFILE pfile;
DWORD filesize; //size of file
BYTE * pbuf;
setlocale(0, "");
__asm
{
//File opening
push OF_READ
lea eax, buffer
push eax
lea eax, filename
push eax
call DWORD PTR OpenFile
cmp eax, -1
je fopen_error
mov pfile, eax
//Getting file size
push NULL
push pfile
call GetFileSize //executing function which calculates file size, result goes to ax
mov filesize, eax //Saving into variable
//Reserving memory for file reading into buffer
mov eax, filesize
inc eax
push eax
call malloc
mov pbuf, eax
add esp, 4
//Reading into buffer
push NULL
push NULL
PUSH filesize
push pbuf
push pfile
call ReadFile // At this point I'm getting an error
It runs nice on my friends PC (Win10 and VS 2017), I'm running on Win7 VS 2017 and I get this exception. Also we tried to launch same exe file with same code and it works good on my friend's PC and doesn't on mine.
Could you please check and tell me where I am wrong?
I think, that must be like this:
char filename[256] = "text.txt"; //name with text
OFSTRUCT buffer;
HFILE pfile;
DWORD filesize; //file size
BYTE * pbuf;
DWORD dwBytesRead; /* error because of absent link */
setlocale(0, "");
__asm
{
//opening file
push OF_READ
lea eax, buffer
push eax
lea eax, filename
push eax
call DWORD PTR OpenFile
cmp eax, -1
je fopen_error
mov pfile, eax
//Getting full size
push NULL
push pfile
call DWORD PTR GetFileSize //calling function via checking size
mov filesize, eax //recording
//storaging memory for reading
mov eax, filesize
inc eax
push eax
call DWORD PTR malloc
mov pbuf, eax
add esp, 4
//Reading into buffer
push NULL
lea eax,[dwBytesRead]
push eax
PUSH filesize
push pbuf
push pfile
call DWORD PTR ReadFile
User contributions licensed under CC BY-SA 3.0