Click here to Skip to main content
12,509,899 members (52,658 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

17.2K views
6 bookmarked
Posted

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

You may also be interested in...

Comments and Discussions

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

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.

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