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?
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);
...
}
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));
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);
Re: Load a string resource automatically when app is started?
Quote:
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?
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.