-
February 6th, 2011, 02:32 AM
#1
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?
-
February 6th, 2011, 03:46 AM
#2
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
-
February 6th, 2011, 05:09 AM
#3
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));
-
February 6th, 2011, 06:18 AM
#4
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:
Originally Posted by AlanCCC
Code:
CString g_strMyStr(LoadStringResource(IDS_MYSTR));
will be exactly the same as
Originally Posted by AlanCCC
Code:
CString g_strMyStr((LPCSTR)IDS_MYSTR);
Last edited by VictorN; February 7th, 2011 at 10:04 AM.
Victor Nijegorodov
-
February 7th, 2011, 02:10 AM
#5
Re: Load a string resource automatically when app is started?
Originally Posted by AlanCCC
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?
-
February 7th, 2011, 09:57 AM
#6
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 09: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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|