简体   繁体   中英

How to assign value to LPVOID buffer of ReadFile

I am developing a win32 API hook program. Accordingly to my understanding, when a program calls ReadFile for a particular file, the content of that file is copied to lpBuffer(see the definition below),

ReadFile definition:

BOOL ReadFile(
  HANDLE       hFile,
  LPVOID       lpBuffer,
  DWORD        nNumberOfBytesToRead,
  LPDWORD      lpNumberOfBytesRead,
  LPOVERLAPPED lpOverlapped
);

Now, my target is to alter this lpBuffer and fill it with provided content by me. I am using EasyHook to hook ReadFile. I am not familiar with LPVOID type. I was able to alter the content for GetCurrentDirectory using the following code.

 string b = "C:\\my\\altered\\directory";
 DWORD returnLength  = b.length();
 int i;
 for (i = 0; i<b.length(); i++)
 {
    lpBuffer[i] = b[i];
 }
 lpBuffer[i++] = '\0';

GetCurrentDirectory definition:

DWORD GetCurrentDirectory(
  DWORD  nBufferLength,
  LPTSTR lpBuffer
);

How to do similar value assignment for ReadFile (LPVOID lpBuffer)?

Here's the LPVOID typedef:

#define far
typedef void far *LPVOID;

The far macro is defined as nothing, I guess it's because of some historical reasons (baggage). So you can almost directly treat the LPVOID as void* .

And now, suppose you have a std::vector<uint8_t> named FakeData , just:

if (nNumberOfBytesToRead < FakeData.size()) {
    SetLastError(ERROR_INSUFFICIENT_BUFFER);
    return FALSE;
}

memcpy(lpBuffer, FakeData.data(), FakeData.size());
*lpNumberOfBytesRead = FakeData.size();

SetLastError(ERROR_SUCCESS);
return TRUE;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM