Thread: GlobalAlloc

  1. #1
    Master of Puppets rwmarsh's Avatar
    Join Date
    Feb 2006
    Location
    Texas
    Posts
    96

    GlobalAlloc

    I am going through the tutorial on TheForgers and I am at the part that talks about using files. I am having problems getting the GlobalAlloc function to work. Here is what I have :
    Code:
    BOOL LoadTextFileToEdit(HWND hEdit, LPCTSTR pszFileName)
    {
    	HANDLE hFile;
    	BOOL bSuccess = FALSE;
    
    	hFile = CreateFile(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
    		OPEN_EXISTING, 0, NULL);
    	if(hFile != INVALID_HANDLE_VALUE)
    	{
    		DWORD dwFileSize;
    
    		dwFileSize = GetFileSize(hFile, NULL);
    		if(dwFileSize != 0xFFFFFFFF)
    		{
    			LPSTR pszFileText;
    
    			pszFileText = GlobalAlloc(GPTR, dwFileSize + 1);
    			if(pszFileText != NULL)
    			{
    				DWORD dwRead;
    
    				if(ReadFile(hFile, pszFileText, dwFileSize, &dwRead, NULL))
    				{
    					pszFileText[dwFileSize] = 0; // Add null terminator
    					if(SetWindowText(hEdit, pszFileText))
    						bSuccess = TRUE; // It worked!
    				}
    				GlobalFree(pszFileText);
    			}
    		}
    		CloseHandle(hFile);
    	}
    	return bSuccess;
    }
    and the compiler is telling me : invalid conversion from "void*' to `CHAR*" for the hilighted line. Just a little confused as everything I have read indicates that this should work, But it dosn't.....
    Any help is appreciated
    Using DEV-C++ Under Windows XP
    +------------------------------+

    "No! Do, or Do Not. There is no Try..."

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Compile it as C. If you compile it as C++, you will need to add the cast.
    Code:
    pszFileText = (LPSTR)GlobalAlloc(GPTR, dwFileSize + 1);
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    Why are you using it?
    http://msdn.microsoft.com/msdnmag/issues/0700/hood/
    "GlobalAlloc still exists in Win32, but only to ease porting of Win16 code"
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Master of Puppets rwmarsh's Avatar
    Join Date
    Feb 2006
    Location
    Texas
    Posts
    96
    Well, I think I figured it out..... i changed
    Code:
    pszFileText = GlobalAlloc(GPTR, dwFileSize + 1);
    to
    Code:
    pszFileText = (LPSTR)GlobalAlloc(GPTR, dwFileSize + 1);
    and it worked just fine... I do not know if this is the best way to do this(????) but it is working now....
    Using DEV-C++ Under Windows XP
    +------------------------------+

    "No! Do, or Do Not. There is no Try..."

  5. #5
    Master of Puppets rwmarsh's Avatar
    Join Date
    Feb 2006
    Location
    Texas
    Posts
    96
    Why are you using it?
    Like I said, that is how it is done in the tutorial I am reading, And as I am just learning windows API and I do not know of any other way to do it.......
    Using DEV-C++ Under Windows XP
    +------------------------------+

    "No! Do, or Do Not. There is no Try..."

  6. #6
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    You might want to take a look at the HeapAlloc and HeapFree functions.

    The standard malloc and free functions can also be used.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    Perhaps you should consider the possibility that it's an old tutorial?
    There's plenty of TurboC tutorials still about, but that doesn't make them any good.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    >>Compile it as C. If you compile it as C++, you will need to add the cast

    You can force the compiler to compile as C by changing the file extension from .cpp to .c (.h are for both)
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GlobalAlloc() conversion from void to char*
    By scwizzo in forum Windows Programming
    Replies: 6
    Last Post: 06-12-2007, 01:00 PM
  2. Suggestions on this C style code
    By Joelito in forum C Programming
    Replies: 11
    Last Post: 06-07-2007, 03:22 AM
  3. globalalloc function pass
    By kryptkat in forum Windows Programming
    Replies: 1
    Last Post: 04-27-2005, 06:08 AM
  4. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM