Adrian
Adrian

Reputation: 10911

LoadResource() returning hGlobal that I can't interrogate?

The following chunk of code results in err1 = 0 (success) and err2 = 6 (invalid handle).

    HGLOBAL hGlobal = LoadResource(hInst, hrSrc);
    INT err1 = GetLastError();
    UINT gflags = GlobalFlags(hGlobal);
    INT err2 = GetLastError();

gflags has a value of 0x8000 which means GMEM_INVALID_HANDLE. I know that the resource exists and if I lock the memory, I get the data in the resource.

My question is why do I get an invalid handle result? Is the memory returned by LoadResource() a 'special' HGLOBAL that is really not what it seems?

Upvotes: 1

Views: 236

Answers (1)

David Heffernan
David Heffernan

Reputation: 613461

The value returned by LoadResource is not really an HGLOBAL.

From the LoadResource documentation:

The return type of LoadResource is HGLOBAL for backward compatibility, not because the function returns a handle to a global memory block. Do not pass this handle to the GlobalLock or GlobalFree function. To obtain a pointer to the first byte of the resource data, call the LockResource function; to obtain the size of the resource, call SizeofResource.

All you ever do with the value returned from LoadResource is pass it to LockResource and SizeofResource.

These functions are this way for reasons of backwards compatibility. Older versions of Windows did return real global memory blocks.

Upvotes: 4

Related Questions