Cannot convert from 'void *' to 'BITMAPFILEHEADER *

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
error C2440: = : cannot convert from void * to BITMAPFILEHEADER *
1> Conversion from void* to pointer to non-void requires an explicit cast
You can see the error above.I coppied the code from the Programming Windows 5th edition book(I really copy pasted it).Now I dont have any ideea how to fix this error. The code and the line that generates the error is here:

<div style="color:black; background-color:white
<pre>#include <span style="color:#a31515 "stdafx.h"
#include <windows.h>
#include <commdlg.h>
#include <span style="color:#a31515 "dibfile.h"

<span style="color:blue static OPENFILENAME ofn ;

<span style="color:blue void DibFileInitialize (HWND hwnd)
{
<span style="color:blue static TCHAR szFilter[] = TEXT (<span style="color:#a31515 "Bitmap Files (*.BMP)*.bmp")
TEXT (<span style="color:#a31515 "All Files (*.*)*.*") ;

ofn.lStructSize = <span style="color:blue sizeof (OPENFILENAME) ;
ofn.hwndOwner = hwnd ;
ofn.hInstance = NULL ;
ofn.lpstrFilter = szFilter ;
ofn.lpstrCustomFilter = NULL ;
ofn.nMaxCustFilter = 0 ;
ofn.nFilterIndex = 0 ;
ofn.lpstrFile = NULL ; <span style="color:green // Set in Open and Close functions
ofn.nMaxFile = MAX_PATH ;
ofn.lpstrFileTitle = NULL ; <span style="color:green // Set in Open and Close functions
ofn.nMaxFileTitle = MAX_PATH ;
ofn.lpstrInitialDir = NULL ;
ofn.lpstrTitle = NULL ;
ofn.Flags = 0 ; <span style="color:green // Set in Open and Close functions
ofn.nFileOffset = 0 ;
ofn.nFileExtension = 0 ;
ofn.lpstrDefExt = TEXT (<span style="color:#a31515 "bmp") ;
ofn.lCustData = 0 ;
ofn.lpfnHook = NULL ;
ofn.lpTemplateName = NULL ;
}
BOOL DibFileOpenDlg (HWND hwnd, PTSTR pstrFileName, PTSTR pstrTitleName)
{
ofn.hwndOwner = hwnd ;
ofn.lpstrFile = pstrFileName ;
ofn.lpstrFileTitle = pstrTitleName ;
ofn.Flags = 0 ;

<span style="color:blue return GetOpenFileName (&ofn) ;
}

BOOL DibFileSaveDlg (HWND hwnd, PTSTR pstrFileName, PTSTR pstrTitleName)
{
ofn.hwndOwner = hwnd ;
ofn.lpstrFile = pstrFileName ;
ofn.lpstrFileTitle = pstrTitleName ;
ofn.Flags = OFN_OVERWRITEPROMPT ;

<span style="color:blue return GetSaveFileName (&ofn) ;
}

BITMAPFILEHEADER * DibLoadImage (PTSTR pstrFileName)
{
BOOL bSuccess ;
DWORD dwFileSize, dwHighSize, dwBytesRead ;
HANDLE hFile ;
BITMAPFILEHEADER * pbmfh ;

hFile = CreateFile (pstrFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL) ;

<span style="color:blue if (hFile == INVALID_HANDLE_VALUE)
<span style="color:blue return NULL ;

dwFileSize = GetFileSize (hFile, &dwHighSize) ;

<span style="color:blue if (dwHighSize)
{
CloseHandle (hFile) ;
<span style="color:blue return NULL ;
}

pbmfh = malloc (dwFileSize) ; <span style="color:green //<<<<-------ERROR HERE!!!!
<span style="color:blue if (!pbmfh)
{
CloseHandle (hFile) ;
<span style="color:blue return NULL ;
}

bSuccess = ReadFile (hFile, pbmfh, dwFileSize, &dwBytesRead, NULL) ;
CloseHandle (hFile) ;
<span style="color:blue if (!bSuccess || (dwBytesRead != dwFileSize)
|| (pbmfh->bfType != * (WORD *) <span style="color:#a31515 "BM")
|| (pbmfh->bfSize != dwFileSize))
{
free (pbmfh) ;
<span style="color:blue return NULL ;
}
<span style="color:blue return pbmfh ;
}

BOOL DibSaveImage (PTSTR pstrFileName, BITMAPFILEHEADER * pbmfh)
{
BOOL bSuccess ;
DWORD dwBytesWritten ;
HANDLE hFile ;

hFile = CreateFile (pstrFileName, GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL) ;

<span style="color:blue if (hFile == INVALID_HANDLE_VALUE)
<span style="color:blue return FALSE ;

bSuccess = WriteFile (hFile, pbmfh, pbmfh->bfSize, &dwBytesWritten, NULL) ;
CloseHandle (hFile) ;

<span style="color:blue if (!bSuccess || (dwBytesWritten != pbmfh->bfSize))
{
DeleteFile (pstrFileName) ;
<span style="color:blue return FALSE ;
}
<span style="color:blue return TRUE ;
}



[/code]


<br/>
In case you didnt notice it,the line is:
pbmfh = malloc (dwFileSize) ;
I know i have to cast the return value,but cast it into what??

View the full article
 
Back
Top