Function Reference


_WinAPI_FindResource

Determines the location of a resource with the specified type and name in the specified module

#include <WinAPIRes.au3>
_WinAPI_FindResource ( $hInstance, $sType, $sName )

Parameters

$hInstance Handle to the module whose executable file contains the resource.
A value of 0 specifies the module handle associated with the image file that the operating system used to create the current process.
$sType The type of the resource. This parameter can be string or integer value.
$sName The name of the resource. This parameter can be string or integer value.

Return Value

Success: Handle to the specified resource's information block.
To obtain a handle to the resource, pass this handle to the _WinAPI_LoadResource() function.
Failure: 0, call _WinAPI_GetLastError() to get extended error information.

Remarks

If the first character of the string of the $sName or $sType parameter is a pound sign (#), the remaining characters represent a decimal number that specifies the integer identifier of the resource's name or type.
For example, the string "#258" represents the integer identifier 258.

To reduce the amount of memory required for a resource, an application should refer to it by integer identifier instead of by name.

An application can use _WinAPI_FindResource() to find any type of resource, but this function should be used only if the application must access the binary resource data when making subsequent calls to _WinAPI_LockResource().

Related

_WinAPI_LoadResource, _WinAPI_LockResource

See Also

Search FindResource in MSDN Library.

Example

#include <APIMiscConstants.au3>
#include <APIResConstants.au3>
#include <GUIConstantsEx.au3>
#include <Memory.au3>
#include <MsgBoxConstants.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 $hInstance = _WinAPI_LoadLibraryEx(@ScriptDir & '\Extras\Resources.dll', $LOAD_LIBRARY_AS_DATAFILE)
If Not $hInstance Then
    MsgBox(BitOR($MB_ICONERROR, $MB_SYSTEMMODAL), 'Error', @ScriptDir & '\Extras\Resources.dll not found.')
    Exit
EndIf

; 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)