C ++ win32 loading rows from a resource

Ok, so I recently decided to put every line in my application in STRINGTABLE, so I can easily translate to different languages. I know how to use the LoadString () api, but this is because I have a variable for every line that I want to load, and if my application has 100 lines, then this is a lot of variables. Is this the best way to do this? Or should I create a global variable that is used as a buffer to load strings as needed? Also, since there is no way to find out how big my line is, should I just create a large enough buffer to hold any line that could be, or is there a better way to do this?

Are strings also loaded if necessary for performance? Is there a way I can preload them?

RE: Well, I tried to create a 256 byte buffer and load lines into it as needed, although I ran into a small problem ...

Here, my code displaying the error message is "Memory Allocation Error!"

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 also likes to store the text “Error” in the row table and load it when I want to display the error, the problem is that I need two global variables to load the rows, and there are some places where I need to load more more than at a time.

Is there a better solution having several global variables?

+5
source share
1 answer

You can, of course, preload them if you want. You just need to create an array of row pointers and load each row into this array. Or you can use a hash map or something like that.

? . , , . - , , , . , , , , , LoadString.

, , , , . , , , , 256 . - , , , , , , .

:

, , . :

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

:

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

C, ++. , , , -, ++ - , , ( , ).

+4

All Articles