Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SaveText: error C2440: '=' : cannot convert from 'void *' to 'char *' 1

Status
Not open for further replies.

LPlates

Programmer
Jun 5, 2003
554
AU
Im using this function to save text but I get an error when allocating the size of the char... LPSTR pszText
(It worked fine in a different project??)
I know this is basic, Im new to c++
Thanks in advance for the help, I will respond to your reply(s).


BOOL SaveTextFileFromEdit(HWND edit, LPCTSTR pszFileName)
{
HANDLE hFile;
BOOL bSuccess = FALSE;

hFile = CreateFile(pszFileName, GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if(hFile != INVALID_HANDLE_VALUE)
{
DWORD dwTextLength;

dwTextLength = GetWindowTextLength(edit);
if(dwTextLength > 0)
{
LPSTR pszText;
DWORD dwBufferSize = dwTextLength + 1;

pszText = GlobalAlloc(GPTR, dwBufferSize); // <----------------------- ERROR IS HERE
if(pszText != NULL)
{
if(GetWindowText(edit, pszText, dwBufferSize))
{
DWORD dwWritten;

if(WriteFile(hFile, pszText, dwTextLength, &dwWritten, NULL))
bSuccess = TRUE;
}
GlobalFree(pszText);
}
}
CloseHandle(hFile);
}
return bSuccess;
}
 
use pointer type conversion:
pszText = (char *)GlobalAlloc(GPTR, dwBufferSize);

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
Thanks IonFilipski! Can you summize why this function worked in a different project without error?
 
I guess you was working using C and now you have switched to C++.

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
I simply pasted the function from one c++ project into another, worked fine in first project but raised the error above in second project, and I also did not change the function at all! Things that make you go Hmmmmm, lol.
 
You may have had different project settings in the other project. Poke throught the settings and you find something is different.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top