Load a string resource automatically when app is started?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Load a string resource automatically when app is started?

  1. #1
    Join Date
    Nov 2010
    Posts
    49

    Load a string resource automatically when app is started?

    I want to load a string resource when app is started. So I initialize a global variable like this

    CString g_strMyStr((LPCSTR)IDS_MYSTR); // IDS_MYSTR is a string resource ID

    But when starting debugging the codes, I will always get "Debug Assertion Failed!" error.

    How to solve this problem?

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,467

    Re: Load a string resource automatically when app is started?

    Global variables are created/initialized before the application (and resource) instances will be loaded.
    So, try to not initialize it in the ctor but use LoadString within InitInstance:
    Code:
    CString g_strMyStr;
    ...
    CMyApp::InitInstance()
    {
        ...
        g_strMyStr.LoadString(IDS_MYSTR);
        ...
    }
    Victor Nijegorodov

  3. #3
    Join Date
    Nov 2010
    Posts
    49

    Re: Load a string resource automatically when app is started?

    Can I use Windows API ::LoadString to load the string resource, as follows?

    const CString LoadStringResource(const UINT nID)
    {
    CString str;
    int nSize, nCopy;
    const int SIZE_INC = 256;

    // Initialization
    str.Empty();
    nSize = 0;

    // Keep looping until we have the whole string
    while (TRUE)
    {
    // Grow buffer by 256 bytes
    nSize += SIZE_INC;

    // Load string resource
    nCopy = ::LoadString(::GetModuleHandle(NULL), nID, str.GetBuffer(nSize), nSize + 1);

    // Check the result
    if ((nCopy == 0) || (nCopy < nSize))
    {
    str.ReleaseBuffer(nCopy);
    break;
    }
    }

    return str;
    }

    CString g_strMyStr(LoadStringResource(IDS_MYSTR));

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,467

    Re: Load a string resource automatically when app is started?

    If you want to use ::LoadString that please, have a look at the source code of Microsoft for CString::LoadString. You will see that AfxGetResourceHandle() must be passed in the ::LoadString(...), not the ::GetModuleHandle(NULL).

    And then this one:
    Quote Originally Posted by AlanCCC
    Code:
    CString g_strMyStr(LoadStringResource(IDS_MYSTR));
    will be exactly the same as
    Quote Originally Posted by AlanCCC
    Code:
    CString g_strMyStr((LPCSTR)IDS_MYSTR);
    Last edited by VictorN; February 7th, 2011 at 09:04 AM.
    Victor Nijegorodov

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,358

    Re: Load a string resource automatically when app is started?

    Quote Originally Posted by AlanCCC View Post
    Can I use Windows API ::LoadString to load the string resource, as follows?
    Why go through all the work, why not use CString::LoadString as Victor suggested?

  6. #6
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,966

    Re: Load a string resource automatically when app is started?

    The CString constructor accepts ID's on condition you use the macro to turn the ID into a "string pointer"
    CString str(MAKEINTRESOURCE(ID_MYSTRING));

    Casting it as a LPCSTR may work as well, but it could also fail for ID's > 32K in that case it could get sign extended rather than zero extended. Using MAKEINTRESOURCE is safer.

    However the above construction doesn't work for a global variable because when the string gets constructed, MCF's resource loading mechanism hasn't yet been initialised.
    It does work from the InitInstance and on.

    For the same reason. CString::LoadString() won't work, and Tryign to get AfxGetResourceHandle() won't work either. All of these rely on the MFC extention DLL resource "chain" to be initialised. Loading explicitely from the application handle may work though. But it won't properly handle resource DLL's once you try supporting multiple languages.
    Last edited by OReubens; February 7th, 2011 at 08:59 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center