Win32 LoadString alternative?

Started by
9 comments, last by Noods 15 years, 9 months ago
I am trying to move away from character strings to std::string. Does anyone know of an alternative to the Win32 LoadString for which I can use std::string instead of a character string?
Advertisement
I don't think there is such a function, but it's easy to write it yourself.

LoadString can return a read-only pointer to the in-memory resource, so just construct a std::string from that. No mess with new, delete and exception safety either.

I haven't tried it, but something like this might work:

std::string LoadString(UINT uID){    LPCTSTR str = _T("");    LoadString(NULL, uID, (LPTSTR)&str, 0);    return str;}


Change to std::wstring if you have a Unicode application.
Million-to-one chances occur nine times out of ten!
Ahh sorry, I forgot to mention that I am trying to stay away from casts as well.
ANSI
std::string LoadString(UINT uID){    PSTR str;    LoadString(GetModuleHandle(NULL), uID, &str, 0);    return str;}


Unicode
std::wstring LoadString(UINT uID){    PWSTR str;    LoadString(GetModuleHandle(NULL), uID, &str, 0);    return str;}
Quote:Original post by Noods
Ahh sorry, I forgot to mention that I am trying to stay away from casts as well.

In this case you'll have to. When working with Win32 it's sometimes necessary.

Quote:Original post by Colin Jeanne
ANSI
*** Source Snippet Removed ***

Which is essentially what I posted, except:

* If LoadString fails, you're constructing a string from an uninitialized pointer. That's why I initialize it to the empty string.

* You're passing a char** to a function expecting a char*. That's why you need the cast. Normally LoadString expects a pointer (to memory to fill) and a size. If the size is zero, the argument should point to a pointer, so that the pointer will get changed to the address of the read-only string in the loaded executable image.

At least, if I'm reading the documentation correctly. Haven't tested this.
Million-to-one chances occur nine times out of ten!
I've been using LoadString for many years, and never did MSDN say anything about getting a read-only string pointer!

That would have saved me days of writing an algorithm that initially allocates 512 bytes, then calls LoadString repeatedly, each time increasing buffer size until it is sure that the whole string has been loaded into buffer.

In fact, this did NOT use to be in MSDN6. Absolutely unbelievable. I guess I learned something today.
Quote:Original post by Mike nl
* If LoadString fails, you're constructing a string from an uninitialized pointer. That's why I initialize it to the empty string.

* You're passing a char** to a function expecting a char*. That's why you need the cast. Normally LoadString expects a pointer (to memory to fill) and a size. If the size is zero, the argument should point to a pointer, so that the pointer will get changed to the address of the read-only string in the loaded executable image.

At least, if I'm reading the documentation correctly. Haven't tested this.

My mistakes. I should have read over the code more carefully.
Just for completeness, after testing, the final code should be:

wstring LoadResourceString(UINT uID){    LPCTSTR str = TEXT("");    int n = LoadString(NULL, uID, (LPTSTR)&str, 0);    return wstring(str, n);}

Important points:
* Can't use the name LoadString. Thanks to stupid windows.h defines.
* The macro is TEXT and not _T
* The number of chars returned by LoadString has to be used when constructing the wstring. Strings in the resources are not zero-delimited.
Million-to-one chances occur nine times out of ten!
In your opinion(s), is it better to avoid the use of a character string or avoid the use of a cast?
Avoid a character string? Any string is typically a string of characters.

I assume you mean avoid the use of char*. In that case, yes, avoid those where possible. This cast in particular is pretty harmless and as portable as they get. Keep in mind this is Windows-specific code to begin with.

And besides, even with char*, you need the cast with this technique. If you really wanted to avoid it, you'd have to do something like ValMan said: keep reallocating a buffer until LoadString fills it.

Getting the read-only pointer with a cast is a far better solution, IMO.
Million-to-one chances occur nine times out of ten!

This topic is closed to new replies.

Advertisement