C++ Builder中从.res文件加载字符串与rc_data的示例求助
Awesome that your bitmap resource loading is working flawlessly already! Let's walk through solid, practical examples for loading string and RC_DATA resources in C++ Builder—these should fit right in with your existing setup.
Loading String Resources
First, double-check your .rc file includes a STRINGTABLE entry (this is what gets compiled into your .res file). Here's a sample definition:
STRINGTABLE BEGIN 100, "Welcome to C++ Builder Resource Strings" 101, "This is a test string pulled from resources" END
Option 1: Win32 API LoadString() (Direct & Flexible)
This is the standard Win32 approach, great for full control:
void LoadAndDisplayResourceString() { wchar_t strBuffer[256]; // Adjust buffer size based on your longest string // Load the string with resource ID 100 int stringLength = LoadStringW((HINSTANCE)HInstance, 100, strBuffer, sizeof(strBuffer)/sizeof(wchar_t)); if (stringLength > 0) { // Use the string however you need—display in a message, assign to a control, etc. ShowMessage(strBuffer); Edit1->Text = String(strBuffer); } else { ShowMessage("Oops: Failed to load the string resource!"); } }
Option 2: C++ Builder TResourceString (VCL-Native Simplicity)
If you prefer a more VCL-friendly method, declare a resource string directly and use it like any other String:
// Declare this in your header or implementation file TResourceString ResStr101 = {101}; void UseVCLResourceString() { String myLoadedString = ResStr101; Label1->Caption = "Loaded Resource String: " + myLoadedString; Memo1->Lines->Add(myLoadedString); }
Loading RC_DATA Resources
RC_DATA is ideal for embedding binary data (custom files, config blobs, raw bytes, etc.). First, define it in your .rc file—here are two common ways:
// Option 1: Embed an external file (e.g., a binary config or text file) MY_CUSTOM_BLOB RCDATA "my_external_file.bin" // Option 2: Embed raw binary data directly in the .rc file MY_RAW_BYTES RCDATA { 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, // ASCII "Hello " 0x57, 0x6F, 0x72, 0x6C, 0x64 // ASCII "World" }
Extracting RC_DATA in Code
Use the Win32 resource API to access the embedded data:
void LoadAndProcessRCData() { // Step 1: Locate the resource HRSRC resourceHandle = FindResourceW((HINSTANCE)HInstance, L"MY_CUSTOM_BLOB", RT_RCDATA); if (!resourceHandle) { ShowMessage("Failed to locate RC_DATA resource!"); return; } // Step 2: Load the resource into memory HGLOBAL resourceMemory = LoadResource((HINSTANCE)HInstance, resourceHandle); if (!resourceMemory) { ShowMessage("Failed to load RC_DATA into memory!"); return; } // Step 3: Lock to get a pointer to the raw data const void* dataPointer = LockResource(resourceMemory); DWORD dataSize = SizeofResource((HINSTANCE)HInstance, resourceHandle); if (dataPointer && dataSize > 0) { // Example 1: Save the data to an external file TFileStream* outputStream = new TFileStream("extracted_data.bin", fmCreate); outputStream->Write(dataPointer, dataSize); delete outputStream; ShowMessage("RC_DATA extracted to file successfully!"); // Example 2: Process as text (if the embedded data is a UTF-16 string) String textContent = String((wchar_t*)dataPointer, dataSize / sizeof(wchar_t)); Memo1->Lines->Add("Extracted Text: " + textContent); } // Note: No need to manually free the resource—Windows manages it automatically }
Quick Tips
- Match Resource Names/IDs Exactly: Resource names are case-sensitive when using string identifiers (like
MY_CUSTOM_BLOB), so double-check your code and .rc file match. - Unicode Awareness: Use the wide-character API functions (the
Wsuffix, likeLoadStringW) to avoid encoding issues with Unicode strings. - Error Handling: Always check the return values of resource functions—they'll help you debug if a resource isn't loading as expected.
内容的提问来源于stack exchange,提问作者Augusto Imperatore

