Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ win32 loading strings from resource

Alright so I have recently made the decision to put every string in my application into a STRINGTABLE, so I can easily translate to different languages. I know how to use the LoadString() api, but this involves me having a different variable for every string I want to load, and if my application has 100 strings, thats alot of variables. Is this the best way to do this? Or should i create a global variable thats used as a buffer to load the strings as needed? Also since there's no way of knowing how big my string is should I just create a big enough buffer to hold any string i could possible have, or is there a better method of doing this?

Also is loading the strings as needed bad for performance? Is there any way i can preload them?

RE: Okay i've tried creating a buffer 256 bytes in size and loading the strings into that as needed, though i ran into a little problem...

Here's my code thats displaying an error message, the error is "Error allocating memory!"

LoadString(g_hInst, IDS_ERROR_MEMORY, szBuffer, sizeof(szBuffer)/sizeof(TCHAR));
MessageBox(NULL, szBuffer, TEXT("Error"), MB_OK | MB_ICONERROR);
ExitProcess(1);

And I have my buffer as a global variable: TCHAR szBuffer[256];

This works but, id like to also store the "Error" text into the string table and load that when I want to display the error, problem is that would require me to have 2 global variables to load the strings, and there are some places where i need to load even more then that at a time.

Is there a better solution then having multiple global variables?

like image 837
Josh Avatar asked May 20 '11 16:05

Josh


1 Answers

You can certainly preload them if you want. You'd just need to create an array of string pointers and load each string into that array. Or you could use a hash map or something similar.

Bad for performance? It depends. If you're displaying those strings as prompts in a user interface, I don't see how loading each string as it's needed is going to be a performance problem. The operating system is going to do some intelligent caching anyway, so it's not like you'll be hitting the disk for every string that you need to display. On the other hand, if you're going to be working with those strings in a tight loop, then it's probably best to preload them into memory so you don't have to call LoadString all the time.

As far as buffers go, I always allocated a buffer that was as large as the largest string I expected to have in my resource file. Considering that user interface strings are typically very small, a 256 byte buffer was more than sufficient. Anything larger than that, I'd either pre-load into memory at startup so I could keep it around, or I wrote a separate method that would allocate a string at load time rather than keeping a buffer around.

Additional info:

Rather than defining global variables for your strings, consider writing a function that loads a resource string, makes a copy of it, and returns that copy. That is:

char * LoadStringFromResource(uint id)
{
    // szBuffer is a globally pre-defined buffer of some maximum length
    LoadString(ghInst, id, szBuffer, bufferSize);
    // yes, I know that strdup has problems. But you get the idea.
    return strdup(szBuffer);
}

Your code, then becomes:

char* errMem = LoadStringFromResource(IDS_ERROR_MEMORY);
char* errText = LoadStringFromResource(IDS_ERROR_TEXT);
MessageBox(NULL, errMem, errText, MB_OK | MB_ICONERROR);
free(errMem);
free(errText);

The above is C code, but you can easily convert to C++. In particular, you probably want to to modify the wrapper function so that it returns a C++ string--something that will be automatically deallocated when it goes out of scope (using smart pointers or whatever the modern equivalent is).

like image 142
Jim Mischel Avatar answered Sep 22 '22 02:09

Jim Mischel