Sign in to follow this  

Win32 LoadString alternative?

This topic is 3451 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 3451 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this