<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="VB6Parse Library Reference - loadresdata - Resources">
<title>loadresdata - Resources - VB6Parse Library Reference</title>
<link rel="stylesheet" href="../../../assets/css/style.css">
<link rel="stylesheet" href="../../../assets/css/docs-style.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/github-dark.min.css">
<script src="../../../assets/js/theme-switcher.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/languages/vbnet.min.js"></script>
<script>hljs.highlightAll();</script>
</head>
<body>
<header class="docs-header">
<div class="container">
<h1><a href="../../../index.html">VB6Parse</a> / <a href="../../../library/index.html">Library</a> / <a href="../../../library/functions/resources/index.html">Resources</a> / loadresdata</h1>
<p class="tagline">VB6 Library Reference</p>
</div>
</header>
<nav class="docs-nav">
<div class="container">
<a href="../../../index.html">Home</a>
<a href="../../../library/index.html">Library Reference</a>
<a href="../../../documentation.html">Documentation</a>
<a href="https://docs.rs/vb6parse" target="_blank">API Docs</a>
<a href="https://github.com/scriptandcompile/vb6parse" target="_blank">GitHub</a>
<button id="theme-toggle" class="theme-toggle" aria-label="Toggle theme">
<span class="theme-icon">🌙</span>
</button>
</div>
</nav>
<main class="container">
<article class="library-item">
<h1 id="loadresdata-function">LoadResData Function</h1>
<p>Returns the data stored in a resource (.res) file.</p>
<h2 id="syntax">Syntax</h2>
<pre><code class="language-vbnet">LoadResData(index, format)</code></pre>
<h2 id="parameters">Parameters</h2>
<ul>
<li><code>index</code> (Required): <code>Integer</code> or <code>String</code> identifying the resource</li>
<li>Can be a numeric ID or string name</li>
<li>Must match the ID/name used when the resource was compiled</li>
<li><code>format</code> (Required): <code>Integer</code> specifying the format of the resource data</li>
<li>Custom user-defined format number (1-32767)</li>
<li>Cannot use predefined formats (use <code>LoadResPicture</code>/<code>LoadResString</code> instead)</li>
</ul>
<h2 id="return-value">Return Value</h2>
<p>Returns a Byte array (Variant containing Byte array):
- Contains the raw binary data from the resource file
- Array is zero-based
- Returns Empty if resource not found (in some VB versions)
- May raise error 32813 if resource not found
- Data is returned exactly as stored in .res file</p>
<h2 id="remarks">Remarks</h2>
<p>The <code>LoadResData</code> function loads custom binary data from resources:
- Loads custom data from compiled resource (.res) files
- Resource file must be linked to project at compile time
- Returns data as Byte array (<code>Variant</code>/<code>Byte()</code>)
- Used for custom binary resources (not bitmaps/icons/strings)
- For images, use <code>LoadResPicture</code> instead
- For strings, use <code>LoadResString</code> instead
- Resource files created with Resource Editor or RC.EXE
- Index can be numeric ID or string name
- Format must be custom type (not 1=Cursor, 2=Bitmap, 3=Icon, etc.)
- Useful for embedding files (sounds, data, etc.)
- Data embedded in compiled EXE (no external files needed)
- More secure than external files (can't be easily modified)
- Faster access than loading from disk
- Resource file added via Project > Add File or in .vbp
- Only one resource file per project
- Changes to .res file require recompile
- Error 32813: "Resource not found" if ID/format don't match
- Error 48: "Error loading from file" if resource file corrupt</p>
<h2 id="typical-uses">Typical Uses</h2>
<ol>
<li><strong>Load Binary Data</strong></li>
</ol>
<pre><code class="language-vbnet"> Dim data() As Byte
data = LoadResData(101, 256) ' Custom format 256
```
2. **Load Sound File**
```vb
Dim wavData() As Byte
wavData = LoadResData("STARTUP_SOUND", 257)
```
3. **Load Configuration Data**
```vb
Dim configData() As Byte
configData = LoadResData("CONFIG", 300)
```
4. **Load Binary Template**
```vb
Dim template() As Byte
template = LoadResData(1001, 400)
```
5. **Load Custom File Type**
```vb
Dim xmlData() As Byte
xmlData = LoadResData("SCHEMA", 500)
```
6. **Load Multiple Resources**
```vb
For i = 1 To 5
data = LoadResData(i, 256)
ProcessData data
Next i
```
7. **Load Named Resource**
```vb
Dim helpData() As Byte
helpData = LoadResData("HELP_FILE", 600)
```
8. **Conditional Loading**
```vb
If useCustomTheme Then
themeData = LoadResData("DARK_THEME", 700)
End If
```
## Basic Examples
### Example 1: Loading and Using Binary Data</code></pre>
<p>vb
' Load binary data from resource
Dim resourceData() As Byte
resourceData = LoadResData(101, 256)
' Use the data
Dim fileNum As Integer
fileNum = FreeFile
Open "C:\output.dat" For Binary As #fileNum
Put #fileNum, , resourceData
Close #fileNum</p>
<pre><code>### Example 2: Loading Sound Data</code></pre>
<p>vb
' Load WAV file from resources
Dim wavData() As Byte
wavData = LoadResData("BEEP", 257)
' Play using Windows API
Call sndPlaySound(wavData(0), SND_ASYNC Or SND_MEMORY)</p>
<pre><code>### Example 3: Error Handling</code></pre>
<p>vb
On Error Resume Next
Dim data() As Byte
data = LoadResData(999, 256)
If Err.Number = 32813 Then
MsgBox "Resource not found!", vbCritical
Err.Clear
ElseIf Err.Number <> 0 Then
MsgBox "Error loading resource: " & Err.Description, vbCritical
Err.Clear
End If</p>
<pre><code>### Example 4: Converting to String</code></pre>
<p>vb
' Load text data stored as binary
Dim textData() As Byte
textData = LoadResData("README", 300)
' Convert byte array to string
Dim content As String
content = StrConv(textData, vbUnicode)
MsgBox content</p>
<pre><code>## Common Patterns
### Pattern 1: `SafeLoadResData`</code></pre>
<p>vb
Function SafeLoadResData(ByVal resID As Variant, _
ByVal resFormat As Integer, _
ByRef outData() As Byte) As Boolean
On Error Resume Next
outData = LoadResData(resID, resFormat)
SafeLoadResData = (Err.Number = 0)
Err.Clear
End Function</p>
<pre><code>### Pattern 2: `LoadResDataToFile`</code></pre>
<p>vb
Sub LoadResDataToFile(ByVal resID As Variant, _
ByVal resFormat As Integer, _
ByVal filename As String)
Dim data() As Byte
Dim fileNum As Integer
data = LoadResData(resID, resFormat)
fileNum = FreeFile
Open filename For Binary As #fileNum
Put #fileNum, , data
Close #fileNum
End Sub</p>
<pre><code>### Pattern 3: `GetResDataSize`</code></pre>
<p>vb
Function GetResDataSize(ByVal resID As Variant, _
ByVal resFormat As Integer) As Long
On Error Resume Next
Dim data() As Byte
data = LoadResData(resID, resFormat)
If Err.Number = 0 Then
GetResDataSize = UBound(data) + 1
Else
GetResDataSize = 0
End If
End Function</p>
<pre><code>### Pattern 4: `LoadResDataAsString`</code></pre>
<p>vb
Function LoadResDataAsString(ByVal resID As Variant, _
ByVal resFormat As Integer) As String
Dim data() As Byte
data = LoadResData(resID, resFormat)
LoadResDataAsString = StrConv(data, vbUnicode)
End Function</p>
<pre><code>### Pattern 5: `ResDataExists`</code></pre>
<p>vb
Function ResDataExists(ByVal resID As Variant, _
ByVal resFormat As Integer) As Boolean
On Error Resume Next
Dim data() As Byte
data = LoadResData(resID, resFormat)
ResDataExists = (Err.Number = 0)
Err.Clear
End Function</p>
<pre><code>### Pattern 6: `LoadMultipleResources`</code></pre>
<p>vb
Function LoadMultipleResources(startID As Integer, _
endID As Integer, _
resFormat As Integer) As Collection
Dim col As New Collection
Dim i As Integer
Dim data() As Byte
On Error Resume Next
For i = startID To endID
data = LoadResData(i, resFormat)
If Err.Number = 0 Then
col.Add data
End If
Err.Clear
Next i
Set LoadMultipleResources = col
End Function</p>
<pre><code>### Pattern 7: `CompareResData`</code></pre>
<p>vb
Function CompareResData(ByVal resID1 As Variant, _
ByVal resID2 As Variant, _
ByVal resFormat As Integer) As Boolean
Dim data1() As Byte, data2() As Byte
Dim i As Long
data1 = LoadResData(resID1, resFormat)
data2 = LoadResData(resID2, resFormat)
If UBound(data1) <> UBound(data2) Then
CompareResData = False
Exit Function
End If
For i = 0 To UBound(data1)
If data1(i) <> data2(i) Then
CompareResData = False
Exit Function
End If
Next i
CompareResData = True
End Function</p>
<pre><code>### Pattern 8: `CopyResDataToArray`</code></pre>
<p>vb
Sub CopyResDataToArray(ByVal resID As Variant, _
ByVal resFormat As Integer, _
ByRef targetArray() As Byte)
Dim source() As Byte
Dim i As Long
source = LoadResData(resID, resFormat)
ReDim targetArray(LBound(source) To UBound(source))
For i = LBound(source) To UBound(source)
targetArray(i) = source(i)
Next i
End Sub</p>
<pre><code>### Pattern 9: `LoadResDataWithFallback`</code></pre>
<p>vb
Function LoadResDataWithFallback(ByVal primaryID As Variant, _
ByVal fallbackID As Variant, _
ByVal resFormat As Integer) As Byte()
On Error Resume Next
LoadResDataWithFallback = LoadResData(primaryID, resFormat)
If Err.Number <> 0 Then
Err.Clear
LoadResDataWithFallback = LoadResData(fallbackID, resFormat)
End If
End Function</p>
<pre><code>### Pattern 10: `CachedResDataLoader`</code></pre>
<p>vb
Dim resCache As New Collection
Function LoadResDataCached(ByVal resID As Variant, _
ByVal resFormat As Integer) As Byte()
Dim key As String
On Error Resume Next
key = CStr(resID) & "_" & CStr(resFormat)
' Try cache first
LoadResDataCached = resCache(key)
If Err.Number <> 0 Then
' Not in cache, load and cache it
Err.Clear
LoadResDataCached = LoadResData(resID, resFormat)
resCache.Add LoadResDataCached, key
End If
End Function</p>
<pre><code>## Advanced Examples
### Example 1: Resource Manager Class</code></pre>
<p>vb
' Class: ResourceManager
Private m_cache As Collection
Private Sub Class_Initialize()
Set m_cache = New Collection
End Sub
Public Function LoadData(ByVal resID As Variant, _
ByVal resFormat As Integer) As Byte()
Dim key As String
On Error Resume Next
key = CStr(resID) & "_" & CStr(resFormat)
LoadData = m_cache(key)
If Err.Number <> 0 Then
Err.Clear
LoadData = LoadResData(resID, resFormat)
If Err.Number = 0 Then
m_cache.Add LoadData, key
Else
Err.Raise vbObjectError + 1000, "ResourceManager", _
"Failed to load resource"
End If
End If
End Function
Public Function GetAsString(ByVal resID As Variant, _
ByVal resFormat As Integer) As String
Dim data() As Byte
data = LoadData(resID, resFormat)
GetAsString = StrConv(data, vbUnicode)
End Function
Public Sub SaveToFile(ByVal resID As Variant, _
ByVal resFormat As Integer, _
ByVal filename As String)
Dim data() As Byte
Dim fileNum As Integer
data = LoadData(resID, resFormat)
fileNum = FreeFile
Open filename For Binary As #fileNum
Put #fileNum, , data
Close #fileNum
End Sub
Public Sub ClearCache()
Set m_cache = New Collection
End Sub
Private Sub Class_Terminate()
Set m_cache = Nothing
End Sub</p>
<pre><code>### Example 2: Sound Player with Resources</code></pre>
<p>vb
' Module with API declarations
Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" ( _
lpszSoundName As Any, ByVal uFlags As Long) As Long
Private Const SND_ASYNC = &H1
Private Const SND_MEMORY = &H4
' Sound resource IDs
Private Const RES_SOUND_BEEP = 101
Private Const RES_SOUND_CLICK = 102
Private Const RES_SOUND_ERROR = 103
Private Const RES_FORMAT_WAVE = 257
Public Sub PlayResourceSound(ByVal soundID As Integer)
Dim soundData() As Byte
On Error Resume Next
soundData = LoadResData(soundID, RES_FORMAT_WAVE)
If Err.Number = 0 Then
Call sndPlaySound(soundData(0), SND_ASYNC Or SND_MEMORY)
Else
Debug.Print "Sound not found: " & soundID
End If
End Sub
Public Sub PlayBeep()
PlayResourceSound RES_SOUND_BEEP
End Sub
Public Sub PlayClick()
PlayResourceSound RES_SOUND_CLICK
End Sub
Public Sub PlayError()
PlayResourceSound RES_SOUND_ERROR
End Sub</p>
<pre><code>### Example 3: Configuration Manager</code></pre>
<p>vb
' Class: ConfigManager
Private m_config As String
Public Sub LoadConfig()
Dim configData() As Byte
On Error Resume Next
' Try to load from resource
configData = LoadResData("CONFIG", 300)
If Err.Number = 0 Then
m_config = StrConv(configData, vbUnicode)
Else
' Use default config
m_config = GetDefaultConfig()
End If
Err.Clear
End Sub
Public Function GetSetting(ByVal key As String) As String
Dim lines() As String
Dim i As Long
Dim pos As Long
lines = Split(m_config, vbCrLf)
For i = 0 To UBound(lines)
If Left(lines(i), Len(key) + 1) = key & "=" Then
GetSetting = Mid(lines(i), Len(key) + 2)
Exit Function
End If
Next i
End Function
Private Function GetDefaultConfig() As String
GetDefaultConfig = "Version=1.0" & vbCrLf & _
"Language=English" & vbCrLf & _
"Theme=Default"
End Function</p>
<pre><code>### Example 4: Template Engine</code></pre>
<p>vb
' Class: TemplateEngine
Private Const RES_FORMAT_TEMPLATE = 400
Public Function ProcessTemplate(ByVal templateID As Variant, _
ParamArray values()) As String
Dim template As String
Dim data() As Byte
Dim i As Long
Dim result As String
' Load template from resources
data = LoadResData(templateID, RES_FORMAT_TEMPLATE)
template = StrConv(data, vbUnicode)
result = template
' Replace placeholders with values
For i = LBound(values) To UBound(values)
result = Replace(result, "{" & i & "}", CStr(values(i)))
Next i
ProcessTemplate = result
End Function
Public Function LoadTemplate(ByVal templateID As Variant) As String
Dim data() As Byte
data = LoadResData(templateID, RES_FORMAT_TEMPLATE)
LoadTemplate = StrConv(data, vbUnicode)
End Function
Public Function HasTemplate(ByVal templateID As Variant) As Boolean
On Error Resume Next
Dim data() As Byte
data = LoadResData(templateID, RES_FORMAT_TEMPLATE)
HasTemplate = (Err.Number = 0)
Err.Clear
End Function</p>
<pre><code>## Error Handling</code></pre>
<p>vb
' Error 32813: Resource not found
On Error Resume Next
data = LoadResData(999, 256)
If Err.Number = 32813 Then
MsgBox "Resource ID 999 not found!"
End If
' Error 48: Error loading from file
data = LoadResData(101, 256)
If Err.Number = 48 Then
MsgBox "Resource file is corrupt or missing!"
End If
' Safe loading with error handling
Function TryLoadResData(ByVal resID As Variant, _
ByVal resFormat As Integer, _
ByRef outData() As Byte) As Boolean
On Error Resume Next
outData = LoadResData(resID, resFormat)
TryLoadResData = (Err.Number = 0)
If Err.Number <> 0 Then
Debug.Print "Error loading resource: " & Err.Description
End If
Err.Clear
End Function</p>
<pre><code>## Performance Considerations
- **Fast Access**: Resources are embedded in EXE (very fast loading)
- **Memory Overhead**: Data loaded into memory when accessed
- **No Caching**: Each call loads fresh copy (implement caching if needed)
- **Compile Time**: Large resources increase EXE size and compile time
- **One Resource File**: Only one .res file per project (combine all resources)
- **Array Copy**: Returns copy of data (not reference)
## Best Practices
1. **Always handle errors** - resource might not exist
2. **Use constants** for resource IDs and formats
3. **Cache frequently used data** to avoid repeated loading
4. **Use meaningful names** for string-based resource IDs
5. **Document resource IDs** in code or separate file
6. **Keep resources organized** by format number
7. **Test resource loading** during development
8. **Validate data size** after loading if needed
9. **Consider memory usage** for large resources
10. **Use appropriate formats** - `LoadResPicture` for images, `LoadResString` for text
## Comparison with Related Functions
| Function | Purpose | Return Type | Data Type |
|----------|---------|-------------|-----------|
| **`LoadResData`** | Load custom binary data | Byte array | Any binary data |
| **`LoadResPicture`** | Load image from resources | `StdPicture` | BMP, ICO, CUR |
| **`LoadResString`** | Load string from resources | String | Text strings |
| **`LoadPicture`** | Load image from file | `StdPicture` | External file |
## `LoadResData` vs `LoadResPicture` vs `LoadResString`</code></pre>
<p>vb
' LoadResData - custom binary data
Dim binData() As Byte
binData = LoadResData(101, 256)
' LoadResPicture - images
Picture1.Picture = LoadResPicture(101, vbResBitmap)
' LoadResString - strings
Dim msg As String
msg = LoadResString(101)
<code>``
**When to use each:**
- **</code>LoadResData<code>**: Custom binary files, sound files, configuration data
- **</code>LoadResPicture<code>**: Images (bitmaps, icons, cursors)
- **</code>LoadResString`**: Localized text strings</p>
<h2 id="platform-notes">Platform Notes</h2>
<ul>
<li>Available in VB6 (not in early VB versions)</li>
<li>Requires resource file (.res) linked to project</li>
<li>Resource file created with Resource Editor or RC.EXE</li>
<li>Only one resource file per project</li>
<li>Resources embedded in compiled EXE/DLL</li>
<li>Returns Variant containing Byte array</li>
<li>Array is always zero-based (0 to n-1)</li>
<li>Custom formats: 1-32767 (avoid predefined formats)</li>
<li>Maximum resource size limited by available memory</li>
</ul>
<h2 id="limitations">Limitations</h2>
<ul>
<li><strong>One resource file</strong>: Only one .res file per project</li>
<li><strong>Compile time</strong>: Must recompile to update resources</li>
<li><strong>No modification</strong>: Cannot modify resources at runtime</li>
<li><strong>Limited tools</strong>: VB6 Resource Editor is basic</li>
<li><strong>Format restrictions</strong>: Cannot use predefined formats (1-16)</li>
<li><strong>No compression</strong>: Resources stored uncompressed in EXE</li>
<li><strong>No encryption</strong>: Resources easily extractable from EXE</li>
<li><strong>Memory copy</strong>: Always returns copy of data (not reference)</li>
<li><strong>Error messages</strong>: Limited error information</li>
<li><strong>No streaming</strong>: Entire resource loaded into memory</li>
</ul>
<h2 id="related-functions">Related Functions</h2>
<ul>
<li><code>LoadResPicture</code>: Load picture from resource file</li>
<li><code>LoadResString</code>: Load string from resource file</li>
<li><code>LoadPicture</code>: Load picture from external file</li>
<li><code>StrConv</code>: Convert byte array to string</li>
<li><code>FreeFile</code>: Get file number for saving resource data</li>
</ul>
</article>
<div style="margin-top: 3rem; padding-top: 2rem; border-top: 1px solid var(--border-color);">
<p>
<a href="index.html">← Back to Resources</a> |
<a href="../index.html">View all functions</a>
</p>
</div>
</main>
<footer>
<div class="container">
<p>© 2024-2026 VB6Parse Contributors. Licensed under the MIT License.</p>
</div>
</footer>
</body>
</html>