# Function Reference

# _WinAPI_LockResource

Locks the specified resource in memory

#include <WinAPIRes.au3>
_WinAPI_LockResource ( $hData )

## Parameters

$hData | Handle to the resource to be locked. The _WinAPI_LoadResource() function returns this handle. Do not pass any value as a parameter other than a successful return value from the LoadResource function.

## Return Value

Success: | Pointer to the first byte of the resource.
Failure: | 0.

## Remarks

The pointer returned by this function 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 _WinAPI_FindResource() or _WinAPI_FindResourceEx()
function. Such a handle points to random data.
Note _WinAPI_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 _WinAPI_LoadResource().

## Related

_WinAPI_FindResource, _WinAPI_FindResourceEx, _WinAPI_LoadResource, _WinAPI_UnlockFile

## See Also

Search LockResource in MSDN Library.

## Example

    #include "Extras\HelpFileInternals.au3"

    #include <GUIConstantsEx.au3>
    #include <Memory.au3>
    #include <StaticConstants.au3>
    #include <WinAPIGdi.au3>
    #include <WinAPIMisc.au3>
    #include <WinAPIRes.au3>

    Local Const $sJpg = @TempDir & '\~Tech.jpg'

    ; Load Resources.dll to memory
    Local $sDll = _Extras_PathFull('Resources.dll')
    Local $hInstance = _WinAPI_LoadLibraryEx($sDll, $LOAD_LIBRARY_AS_DATAFILE)

    ; Load JPEG resource from Resources.dll library
    Local $hResource = _WinAPI_FindResource($hInstance, 'JPEG', 1)
    Local $iSize = _WinAPI_SizeOfResource($hInstance, $hResource)
    Local $hData = _WinAPI_LoadResource($hInstance, $hResource)
    Local $pData = _WinAPI_LockResource($hData)

    ; Save resource to .jpg file
    Local $hFile = FileOpen($sJpg, 2 + 16)
    Local $tData = DllStructCreate('byte[' & $iSize & ']', $pData)
    FileWrite($hFile, DllStructGetData($tData, 1))
    FileClose($hFile)

    ; Load FONT resource from Resources.dll library
    $hResource = _WinAPI_FindResource($hInstance, $RT_FONT, 'TECHNOVIA_CAPS')
    $iSize = _WinAPI_SizeOfResource($hInstance, $hResource)
    $hData = _WinAPI_LoadResource($hInstance, $hResource)
    $pData = _WinAPI_LockResource($hData)

    ; Add font from a memory to the system
    Local $hFont = _WinAPI_AddFontMemResourceEx($pData, $iSize)

    ; Load SOUND resource from Resources.dll library
    $hResource = _WinAPI_FindResource($hInstance, 'SOUND', 1)
    $iSize = _WinAPI_SizeOfResource($hInstance, $hResource)
    $hData = _WinAPI_LoadResource($hInstance, $hResource)
    $pData = _WinAPI_LockResource($hData)

    ; Copy WAV to memory for use later
    Local $hWave = _MemGlobalAlloc($iSize, $GMEM_MOVEABLE)
    Local $pWave = _MemGlobalLock($hWave)
    _MemMoveMemory($pData, $pWave, $iSize)
    ; _MemGlobalUnlock($hWave)

    ; Load STRING resource from Resources.dll library
    Local $sText = _WinAPI_LoadString($hInstance, 1)

    ; Unload Resources.dll from memory
    _WinAPI_FreeLibrary($hInstance)

    ; Create GUI
    GUICreate('Test ' & StringReplace(@ScriptName, '.au3', '()'), 350, 350)
    GUICtrlCreatePic($sJpg, 0, 0, 350, 350)
    GUICtrlSetState(-1, $GUI_DISABLE)
    GUICtrlCreateLabel($sText, 10, 18, 330, 36, $SS_CENTER)
    GUICtrlSetFont(-1, 30, -1, -1, 'Technovia Caps')
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetColor(-1, 0xF06000)
    Local $idButton = GUICtrlCreateButton('Play Sound', 125, 316, 100, 23)
    GUISetState(@SW_SHOW)

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $idButton
                _WinAPI_PlaySound($pWave, BitOR($SND_ASYNC, $SND_MEMORY, $SND_NOWAIT))
        EndSwitch
    WEnd

    ; Free resources
    _WinAPI_RemoveFontMemResourceEx($hFont)
    FileDelete($sJpg)