Click here to Skip to main content
Click here to Skip to main content

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

, 12 Jun 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
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).
 
#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)

Share

About the Author

Alain Rist

France France
No Biography provided

Comments and Discussions

 
GeneralIt was a constructive and useful discussion at least for me ... PinmemberAlain Rist12-Jun-10 0:51 
GeneralCool, looks more than the defacto standard, looks like it's ... PinmemberAescleal11-Jun-10 10:18 
GeneralIt's compiling here on VC2010 Warning level 4 (native C++ st... PinmemberAlain Rist10-Jun-10 23:45 
GeneralThis is well overcomplicated, and doesn't compile on any of ... PinmemberAescleal10-Jun-10 22:50 
GeneralRight, it was VC6 not C++ :) Now corrected. PinmemberAlain Rist8-Jun-10 15:43 
Generalnew doesn't return zero when it fails. PinmemberAescleal8-Jun-10 12:39 
GeneralAdded handling of (improbable but not impossible) new failur... PinmemberAlain Rist8-Jun-10 12:01 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 12 Jun 2010
Article Copyright 2010 by Alain Rist
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid