GlobalAlloc

I am using C++ in Windows 3.1

The Win API provides the following example for GlobalAlloc:

HGLOBAL hglb;
void FAR* lpvBuffer;

hglb = GlobalAlloc(GPTR,1024);
lpvBuffer = GlobalLock(hglb);
.
.
.
GlobalUnlock(hglb);
GlobalFree(hglb);

What I would like is an example of the above in a coded example so that I can get a better idea how I can utilise GlobalAlloc in my programs.

your help is appreciated

Richard
aa941438Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RONSLOWCommented:
GlobalAlloc is pretty much like a malloc except for two things.  Firstly is where it gets its memory from, and second is how you access it.  Memory allocated with GlobalAlloc can potentially be moved about in memory without you knowing - and can even be swapped out to disk.  Because of this, if GlobalAlloc returned a pointer, it would be of little use to you as you cannot guarantee that it will point to your allocated data.  So instead you get a "handle".  Windows keeps a mapping between the handle and where the actual memory is in memory.  However, to get at the allocated data your program needs a pointer.  So, what you need to do is to call GlobalLock which locks that allocated memory so that windows wil not move it and returns to you a pointer to its current location.  When you are finished with the data, you unlock the handle, which then allows windows to manage the block of data again.

Hope this helps - let me know if you want more help.

0
aa941438Author Commented:
While I greatly appreciate the effort taken to provide a description of GlobalAlloc this was not really what I was after as I have much of this information at hand.

A coded example of GlobalAlloc was what I asked for as this is what would really help me. There is no substitute for an example.

 
0
alexxxCommented:
Here is an example (simple list implementation):

typedef struct tagLIST
{
   DWORD dwPayLoad; //data contained in a list element
   LPLIST lpNext;  //points to the next list element
} LIST, FAR* LPLIST;

main()
{
   LPLIST lpMyList;

   lpMyList = ListCreate();

   ListAdd(lpMyList, 1L);
   ListAdd(lpMyList, 2L);
   ListAdd(lpMyList, 3L);

   ListDestroy(lpMyList);  //deallocates all the memory used
}  //main()

LPLIST ListCreate(void)
{
   HGLOBAL hMem;
   LPLIST lpTmp;
   
   hMem = GlobalAlloc(GPTR, sizeof(LIST));
   lpTmp = GlobalLock(hMem);
   if(NULL != lpTmp)
      lpTmp->lpNext = NULL;
 
   return lpTmp;
}  //ListCreate()

BOOL ListAdd(LPLIST lpList, DWORD dwPayLoad)
{
   HGLOBAL hMem;
   LPLIST lpTmp;
   
   hMem = GlobalAlloc(GPTR, sizeof(LIST));
   lpTmp = GlobalLock(hMem);
   if(NULL != lpTmp)
   {
      lpTmp->lpNext = NULL;
      lpTmp->dwPayLoad = dwPayLoad;

      while(lpList->lpNext)
         lpList = lpList->lpNext;

      lpList->lpNext = lpTmp;
      return TRUE;
   }
   else
      return FALSE;
}   //ListAdd()

void ListDestroy(LPLIST lpList)
{
   LPLIST lpTmp;

   while(lpList)
   {
      lpTmp = lpList->lpNext;
      GlobalUnlock(HIWORD((DWORD)lpList));
      GlobalFree(HIWORD((DWORD)lpList));
      lpList = lpTmp;
   }
}   //ListDestroy()

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

RONSLOWCommented:
While the above is a use of GlobalAlloc, it is not a GOOD use of it.

You should not keep a handle locked for longer than you really need.

Now, of course, in this example, the whole program runs very quickly, but in a REAL example, you would not call GlobalLock immediately after GlobalAlloc and then GlobalUnlock immediately beforeGlobalFree.

You creation routine would allocate the handle and keep (in this case) only lpList.  Then whenever you wanted to add to the list, you would GlobalLock, get the pointer, do things to the list and then unlock it.

You destroy procedure would then just do a global free.

Here is improved code which also uses a class to encapulate access to the list.

// this is the data we will be putting in our global storage
typedef struct tagLIST {
      DWORD dwPayLoad;      // data contained in a list element
      HANDLE hNext;            // next list element
} LIST, FAR* LPLIST;

// this class encapulates access to the list
class List {
      HANDLE hFirst;            // first list element
      HANDLE hLast;            // last list element
public:
      List();
      ~List();
      BOOL Add(DWORD dwPayLoad);
}

main() {
      List myList;
      myList.Add(1L);
      myList.Add(2L);
      myList.Add(3L);
}

// construct the list - set handles to NULL
List::List()
: hFirst(NULL)
, hLast(NULL)
{}

// destruct the list by free all items
List::~List() {
      HANDLE hNext;
      // follow the chain for each item
      for (HANDLE hThis = hFirst; hThis; hThis = hNext) {
            // lock handle to get next pointer
            LPLIST p = GlobalLock(hThis);
            if (p) hNext = p->hNext;
            GlobalUnlock(hThis);
            // now free it
            GlobalFree(hThis);
      }
}

// add an item to the end of list
BOOL List::Add(DWORD dwPayLoad) {
      // allocate a new item
      hTmp = GlobalAlloc(GPTR, sizeof(LIST));
      if (! hTmp) return FALSE;
      // get a pointer to it
      pTmp = GlobalLock(hTmp);
      if (! lpTmp) return FALSE;
      // store the data
      pTmp.dwPayLoad = dwPayLoad;
      pTmp.hNext = NULL;
      GlobalUnlock(hTmp);
      // update old last item to point to new item
      if (hLast) {
            LPLIST pLast = GlobalLock(hLast);
            if (pLast) pLast->hNext = hTmp;
            GlobalUnlock(hLast);
      }
      // update pointer to end of list
      hLast = hTmp;
      // update pointer to start of list
      if (! hFirst) hFirst = hLast;
}


Please reject previous and accept this answer if you prefer it.

Roger

0
RONSLOWCommented:
PS: just found tried compiling the above - found a couple of little bugs.

Here is fixed code (which compiled with no warnings).  Also, note the use of static_cast to cast return from GlobalLock

#include <windows.h>

// this is the data we will be putting in our global storage
typedef struct tagLIST {
      DWORD dwPayLoad;      // data contained in a list element
      HANDLE hNext;            // next list element
} LIST, FAR* LPLIST;

// this class encapulates access to the list
class List {
      HANDLE hFirst;            // first list element
      HANDLE hLast;            // last list element
public:
      List();
      ~List();
      BOOL Add(DWORD dwPayLoad);
};

void main() {
      List myList;
      myList.Add(1L);
      myList.Add(2L);
      myList.Add(3L);
}

// construct the list - set handles to NULL
List::List()
: hFirst(NULL)
, hLast(NULL)
{}

// destruct the list by free all items
List::~List() {
      HANDLE hNext;
      // follow the chain for each item
      for (HANDLE hThis = hFirst; hThis; hThis = hNext) {
            // lock handle to get next pointer
            LPLIST p = static_cast<LPLIST>(GlobalLock(hThis));
            if (p) hNext = p->hNext;
            GlobalUnlock(hThis);
            // now free it
            GlobalFree(hThis);
      }
}

// add an item to the end of list
BOOL List::Add(DWORD dwPayLoad) {
      // allocate a new item
      HANDLE hTmp = GlobalAlloc(GPTR, sizeof(LIST));
      if (! hTmp) return FALSE;
      // get a pointer to it
      LPLIST pTmp = static_cast<LPLIST>(GlobalLock(hTmp));
      if (! pTmp) return FALSE;
      // store the data
      pTmp->dwPayLoad = dwPayLoad;
      pTmp->hNext = NULL;
      GlobalUnlock(hTmp);
      // update old last item to point to new item
      if (hLast) {
            LPLIST pLast = static_cast<LPLIST>(GlobalLock(hLast));
            if (pLast) pLast->hNext = hTmp;
            GlobalUnlock(hLast);
      }
      // update pointer to end of list
      hLast = hTmp;
      // update pointer to start of list
      if (! hFirst) hFirst = hLast;
      return TRUE;
}

Please reject previous and accept this answer if you prefer it.

Roger

0
aa941438Author Commented:
Many thanks, a great help much appreciated
0
alexxxCommented:
OK, I know this list implementation is ain't good at all, it's very bad actually, it wastes Windows global heap resources. But it IS a good example because it's simple.
0
RONSLOWCommented:
alexx - my problem with your example was NOT that it was simple, but that it did not show the correct way to use handles and to get access thru lock and unlock.

Your example allocated the space ok but it locked it and KEPT IT LOCKED until the end of the program.  This is NOT the way to use global alloced memory.

Also you stored the pointer returned by the lock and used that rather than the handle.  This is also bad because ,when you do start locking and unlocking, the pointer will not be valid.  Rather, you should keep the handle only and lock to get the pointer when you really need to get at the daat and then unlock when you are finished (and the pointer is then no longer valid).


But I DID appreciate the simplicity of your example and simply fixed it up a bit to make better use of handles and also (for the heck of it) put it in a class (as this is a C++ topic area).

Roger

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.