Click here to Skip to main content
15,881,139 members
Articles / General Programming / String
Tip/Trick

Load a Windows string resource into a std::string or std::wstring

Rate me:
Please Sign up or sign in to vote.
4.36/5 (7 votes)
12 Jun 2010CPOL 35.2K   6   7
A set of C++ functions to load the resource into an existing string or build the string from it
This contribution was first inspired by this thread's[^] question and Stephen Hewitt [^]answers.

It was heavily edited to address Aescleal's interesting concerns and remarks.

This (last) version compiles with VS2008 and VS2010 (native Standard C++ Library) without warning at level 4.

The two LoadString() functions are independant of the application character set and return the length of the destination string, as does ::LoadString().

template <class StringType> StringType LoadString_() is a helper class using the matching LoadString() version to build a StringType with uID string resource content.

LoadString_W() returns a std::wstring with uID string resource content (empty if no resource).
LoadString_S() returns a std::string with uID string resource content (empty if no resource).
LoadString_T() returns a UNICODE depending std::wstring or std::string, with uID string resource content (empty if no resource).

C++
#include <string>
#include <Windows.h>
#include "resource.h"

// Assigns the uID string resource to wsDest, returns length (0 if no resource)
inline int LoadString(std::wstring& wsDest, UINT uID, HINSTANCE hInstance = ::GetModuleHandle(NULL))
{
    PWCHAR wsBuf; // no need to initialize
    wsDest.clear();
    if (size_t len = ::LoadStringW(hInstance, uID, (PWCHAR)&wsBuf, 0))
        wsDest.assign(wsBuf, len);
    return wsDest.length();
}

// Assigns the uID string resource to sDest, returns length (0 if no resource)
inline int LoadString(std::string& sDest, UINT uID, HINSTANCE hInstance = ::GetModuleHandle(NULL))
{
    PWCHAR wsBuf; // no need to initialize
    sDest.clear();
    if (size_t len = ::LoadStringW(hInstance, uID, (PWCHAR)&wsBuf, 0) * sizeof WCHAR)
    {
        sDest.resize(++len); // make room for trailing '\0' in worst case
        sDest.resize(::LoadStringA(hInstance, uID, &*sDest.begin(), len));
    }
    return sDest.length();
}

// Returns a StringType with uID string resource content (empty if no resource)
template <class StringType>
inline StringType LoadString_(UINT uID, HINSTANCE hInstance)
{
    StringType sDest;
    return LoadString(sDest, uID, hInstance) ? sDest : StringType();
}

// Returns a std::string with uID string resource content (empty if no resource)
inline std::string LoadString_S(UINT uID, HINSTANCE hInstance = ::GetModuleHandle(NULL))
{
    return LoadString_<std::string>(uID, hInstance);
}

// Returns a std::wstring with uID string resource content (empty if no resource)
inline std::wstring LoadString_W(UINT uID, HINSTANCE hInstance = ::GetModuleHandle(NULL))
{
    return LoadString_<std::wstring>(uID, hInstance);
}

#ifdef UNICODE
    typedef std::wstring t_string;
#else
    typedef std::string t_string;
#endif

// Returns a UNICODE depending std::wstring or std::string, with uID string resource content (empty if no resource)
inline t_string LoadString_T(UINT uID, HINSTANCE hInstance = ::GetModuleHandle(NULL))
{
    return LoadString_<t_string>(uID, hInstance);
}



cheers,
AR

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
France France
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralIt was a constructive and useful discussion at least for me ... Pin
Alain Rist11-Jun-10 23:51
Alain Rist11-Jun-10 23:51 
GeneralCool, looks more than the defacto standard, looks like it's ... Pin
Aescleal11-Jun-10 9:18
Aescleal11-Jun-10 9:18 
GeneralIt's compiling here on VC2010 Warning level 4 (native C++ st... Pin
Alain Rist10-Jun-10 22:45
Alain Rist10-Jun-10 22:45 
GeneralThis is well overcomplicated, and doesn't compile on any of ... Pin
Aescleal10-Jun-10 21:50
Aescleal10-Jun-10 21:50 
GeneralRight, it was VC6 not C++ :) Now corrected. Pin
Alain Rist8-Jun-10 14:43
Alain Rist8-Jun-10 14:43 
Generalnew doesn't return zero when it fails. Pin
Aescleal8-Jun-10 11:39
Aescleal8-Jun-10 11:39 
GeneralAdded handling of (improbable but not impossible) new failur... Pin
Alain Rist8-Jun-10 11:01
Alain Rist8-Jun-10 11:01 
Added handling of (improbable but not impossible) new failure.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.