I got this simple code:

#include "stdio.h"
#include "stdlib.h"
#include "windows.h"

int main(int argc, CHAR* argv[])
{
    HANDLE hFile = NULL;
    HANDLE hMapFile = NULL;
    LPBYTE fileView;

    hFile = CreateFile(argv[1],GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
    if(hFile == INVALID_HANDLE_VALUE)
    {
        printf("CreateFile function failed\n");
        goto clean;
    }

***** hMapFile = (LPBYTE)CreateFileMapping(hFile,NULL,PAGE_READONLY,0,256,NULL);*****
if(hMapFile == NULL)
{
printf("CreateFileMapping function failed\n");
goto clean;
}
fileView = MapViewOfFile(hMapFile,FILE_MAP_READ,0,0,0);
if(fileView == NULL)
{
printf("CreateFileMapping function failed\n");
goto clean;
}

clean:
    CloseHandle(hFile);
    CloseHandle(hMapFile);
    system("PAUSE");
    return 0;
}

And for some reason in the line that is underlined it says that I cannot convert LPVOID to LPBYTE

You are trying to convert the return value of CreateFileMapping() to the wrong type. It already returns the correct type, so no typecasting is needed.

So how can I use this after I do this function?
I just have to cast the void pointer every time I want to use it as a byte type?

  1. Remove (LPBYTE) from CreateFileMapping(...)
  2. I'm guessing that you're wanting to do something with the mapped file, in which case you'd have:

    fileView = (LPBYTE)MapViewOfFile(hMapFile, FILE_MAP_READ, 0, 0, 0);

Edited 4 Years Ago by nullptr

I just have to cast the void pointer every time I want to use it as a byte type?

hMapFile is NOT a byte ptr -- its HANDLE which is already the type returned by CreateFileMaping(). And where do you see a void pointer??? I don't see that sort of object in the code you posed.

Edited 4 Years Ago by Ancient Dragon

Your primary problem is not with the following statement although it should not be typecasted as LPBYTE

> ***** hMapFile = (LPBYTE)CreateFileMapping(hFile,NULL,PAGE_READONLY,0,256,NULL);*****

The problem is caused by the following statement:

> fileView = MapViewOfFile(hMapFile,FILE_MAP_READ,0,0,0);

MapViewOfFile returns a LPVOID data type. fileView is a LPBYTE data type.

This article has been dead for over six months. Start a new discussion instead.