We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,267 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Cannot convert LPVOID to LPBYTE

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

4
Contributors
5
Replies
5 Hours
Discussion Span
1 Year Ago
Last Updated
7
Views
iPanda
Newbie Poster
16 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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.

Ancient Dragon
Achieved Level 70
Team Colleague
32,144 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,577
Skill Endorsements: 69

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?

iPanda
Newbie Poster
16 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
  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);

nullptr
Junior Poster
154 posts since Mar 2012
Reputation Points: 46
Solved Threads: 32
Skill Endorsements: 0

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.

Ancient Dragon
Achieved Level 70
Team Colleague
32,144 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,577
Skill Endorsements: 69

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.

BobS0327
Junior Poster
185 posts since Feb 2008
Reputation Points: 35
Solved Threads: 24
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0794 seconds using 2.69MB