GlobalAlloc() conversion from void to char*

This is a discussion on GlobalAlloc() conversion from void to char* within the Windows Programming forums, part of the Platform Specific Boards category; Everytime I try to compile this it gives me this error code " invalid conversion from `void*' to `CHAR*' " ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    416

    GlobalAlloc() conversion from void to char*

    Everytime I try to compile this it gives me this error code

    " invalid conversion from `void*' to `CHAR*' "

    I changed the GlobalAlloc() function to an integer to see if it was trying to return void, but it would give me a "invalid conversion from void* to int" error, so something is wrong with the DWORD FileSize, but I can't figure out what. It's suppose to be the save as... part of the program, but this is the only spot I'm hung up on. It does the same thing for open...

    Code:
    	if(hFile != INVALID_HANDLE_VALUE){
    		LPSTR FileSize;
    		DWORD BufferSize;
    		BufferSize = GetWindowTextLength(hSave);
    
    		if(BufferSize > 0){
    			DWORD Buff;
    			Buff = BufferSize + 1;
    			FileSize = GlobalAlloc(GHND,Buff);  //<--- says that is the error

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    Yes, if you are compiling this as C++, then you need to cast your void* returning functions to the appropriate type.

    FileSize = (LPSTR)GlobalAlloc(GHND,Buff);

    But why are you using GlobalAlloc() in preference to the standard C++ 'new' operator for allocating memory, which does not need any casting.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Quote Originally Posted by Salem View Post
    But why are you using GlobalAlloc() in preference to the standard C++ 'new' operator for allocating memory, which does not need any casting.
    Mainly because I'm new to Win32/API/windows programming and do not know a lot of functions yet. I've been surfing around tutorials, books, and msdn to find everything so far. I will look in to other ways of allocating memory, thanks.
    Last edited by scwizzo; 06-10-2007 at 01:06 PM.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    If you want to use the Win32 API memory management functions, look into the Heap functions. Microsoft discourages usage of GlobalAlloc() and LocalAlloc() these days.

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    ok i tried the new and delete operators, way easier, thank you.

  6. #6
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Quote Originally Posted by Salem View Post
    But why are you using GlobalAlloc() in preference to the standard C++ 'new' operator for allocating memory, which does not need any casting.
    GlobalAlloc() is part of the API, and hence will support faster API features ( such as block transfers) if the user upgrades their system/os. whereas the functionality of 'new' is fixed at compile time.

    'new' wins on portability
    GlobalAlloc() wins on functionality

    for applications that only target Win32/Win64 systems, it makes sense to use GlobalAlloc();
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    As I already said, MSDN says to skip GlobalAlloc() and move to HeapAlloc(). From my understanding I would assume that GlobalAlloc() just calls HeapAlloc() on the default heap object assigned to the process, but for whatever reason, the warning is that GlobalAlloc() is actually slower....

    Note The global functions are slower than other memory management functions and do not provide as many features. Therefore, new applications should use the heap functions. However, the global functions are still used with DDE, the clipboard functions, and OLE data objects.
    So, make whatever you will of it....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  2. i cannot see the mouse arrow
    By peachchentao in forum C Programming
    Replies: 6
    Last Post: 12-10-2006, 04:14 AM
  3. need help with handelling multiple source files
    By DarkMortar in forum C++ Programming
    Replies: 38
    Last Post: 05-26-2006, 11:46 PM
  4. Can't Find Conio.h?
    By drdroid in forum C++ Programming
    Replies: 27
    Last Post: 09-26-2002, 05:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21