3 out of 3 rated this helpful - Rate this topic

LockResource function

Applies to: desktop apps only

Retrieves a pointer to the specified resource in memory.

Syntax

LPVOID WINAPI LockResource(
  __in  HGLOBAL hResData
);

Parameters

hResData [in]

Type: HGLOBAL

A handle to the resource to be accessed. The LoadResource function returns this handle. Note that this parameter is listed as an HGLOBAL variable only for backward compatibility. Do not pass any value as a parameter other than a successful return value from the LoadResource function.

Return value

Type: LPVOID

If the loaded resource is available, the return value is a pointer to the first byte of the resource; otherwise, it is NULL.

Remarks

The pointer returned by LockResource is valid until the module containing the resource is unloaded. It is not necessary to unlock resources because the system automatically deletes them when the process that created them terminates.

Do not try to lock a resource by using the handle returned by the FindResource or FindResourceEx function. Such a handle points to random data.

Note  LockResource does not actually lock memory; it is just used to obtain a pointer to the memory containing the resource data. The name of the function comes from versions prior to Windows XP, when it was used to lock a global memory block allocated by LoadResource.

Examples

For an example, see Updating Resources.

Requirements

Minimum supported client

Windows 2000 Professional

Minimum supported server

Windows 2000 Server

Header

Winbase.h (include Windows.h)

Library

Kernel32.lib

DLL

Kernel32.dll

See also

Reference
FindResource
FindResourceEx
LoadResource
Conceptual
Resources

 

 

Send comments about this topic to Microsoft

Build date: 2/3/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
A sample code to retrieve version information - the code

The important here, is you can use the pointer returned by LockResource to see the info. But is easer parse it with VerQueryValue function. Thus you need first copy the content to a temporary buffer.


#include "stdafx.h"
#include <Strsafe.h>

const int strSize = 512;

struct LANGANDCODEPAGE{
        WORD wLanguage;
        WORD wCodePage;
};

CString GetVersionInfo(HMODULE hModule, CString resQuery)
{
        if(hModule==NULL) //don't really necessary, NULL is fine too
                hModule = AfxGetResourceHandle();

        HRSRC hResInfo = FindResource(hModule,MAKEINTRESOURCE(VS_VERSION_INFO),RT_VERSION);
        if(hResInfo==NULL){
                AfxMessageBox("FindResource failed");
                return "ERROR";
        }

        DWORD resSize = SizeofResource(hModule,hResInfo);
        if(resSize==0){
                AfxMessageBox("SizeofResource failed");
                return "ERROR";
        }

        HGLOBAL hResData = LoadResource(hModule,hResInfo);
        if(hResData==NULL){
                AfxMessageBox("LoadResource failed");
                return "ERROR";
        }

        LPVOID pFirstByte = LockResource(hResData);
        if(pFirstByte==NULL){
                AfxMessageBox("LockResource failed");
                return "ERROR";
        }

/*******************************************************************************
   At this point, you can directly manipulate the information by pointer
pFirstByte, but you can also parse it with VerQueryValue function. We try it
now.
*******************************************************************************/
        int retVal;
        char strTmp[strSize];
        struct LANGANDCODEPAGE *lpTranslate;
        HRESULT hr;
        UINT uLen;
        LPVOID pBlock;
        LPVOID lplpBuffer;

        resSize = 3*resSize; //Don't ask me about the triple?! But I think that with 2*resSize+4 works nicely too.
        pBlock = new BYTE[resSize];
        memset(pBlock,0,resSize);
        memcpy(pBlock,pFirstByte,resSize);

        retVal = VerQueryValue(pBlock,"\\VarFileInfo\\Translation",(LPVOID*)&lpTranslate,&uLen);
        if(retVal==0){
                AfxMessageBox("VerQueryValue(Translation): unsuccessful!");
                return "ERROR";
        }

        retVal = uLen/sizeof(struct LANGANDCODEPAGE);
        if(retVal!=1){ //langNumber always must be equal 1 (in my program only because it have only one lang info)
                hr = StringCchPrintf(strTmp,strSize,TEXT("Error! Languages number: %d."),retVal);
                if(FAILED(hr)){
                        AfxMessageBox("StringCchPrintf(Error! Languages number: %d.): unsuccessful!");
                        return "ERROR";
                }

                AfxMessageBox(strTmp);
                return "ERROR";
        }

        hr = StringCchPrintf(strTmp,strSize,TEXT("\\StringFileInfo\\%04x%04x\\%s"),
                        lpTranslate[0].wLanguage,lpTranslate[0].wCodePage,resQuery);
        if(FAILED(hr)){
                AfxMessageBox("StringCchPrintf(\\StringFileInfo\\%04x%04x\\%s): unsuccessful!");
                return "ERROR";
        }

        retVal = VerQueryValue(pBlock,strTmp,&lplpBuffer,&uLen);
        if(retVal==0){
                hr = StringCchPrintf(strTmp,strSize,
                        TEXT("VerQueryValue(StringFileInfo) - resQuery(%s): unsuccessful!"),resQuery);
                if(FAILED(hr)){
                        AfxMessageBox("StringCchPrintf(VerQueryValue-StringFileInfo): unsuccessful!");
                        return "ERROR";
                }

                AfxMessageBox(strTmp);
                return "ERROR";
        }

        CString csRet = (char*)lplpBuffer;
        csRet.Replace(", ","."); //for example: transform "1, 3, 4, 5" into "1.3.4.5"
        delete [] pBlock;
/*deprecated
        UnlockResource(hResData);
        FreeResource(hResData);
*/
        return csRet;
}

 

A sample code to retrieve version information
The important here, is you can use the pointer returned by LockResource to see the info. But is easer parse it with VerQueryValue function. Thus you need first copy the content to a temporary buffer.
vb.net syntax
<DllImport("kernel32.dll", SetLastError:=True)> _
Public Shared Function LockResource(ByVal hResData As IntPtr) As IntPtr
End Function
C# syntax
[DllImport("kernel32.dll", SetLastError=true)]
internal static extern IntPtr LockResource(IntPtr hResData);