-
August 6th, 2009, 06:58 AM
#1
What's wrong with this code?
Hello, I've trouble with this code:
Code:
#ifdef UNICODE
static std::wstring GetConfigDependentString(const std::string& str);
#else
static std::string GetConfigDependentString(const std::string& str);
#endif
...
#ifdef UNICODE
std::wstring CCoreUtils::GetConfigDependentString(const std::string& str)
{
return CCoreUtils::UTF8ToUTF16(str);
}
#else
const std::string CCoreUtils::GetConfigDependentString(const std::string& str)
{
return str;
}
#endif
...
void CCoreUtils::MyFunc(const std::string& dialogTitle)
{
OPENFILENAME ofn;
...
ofn.lpstrTitle = CCoreUtils::GetConfigDependentString(dialogTitle).c_str();
...
}
The project is configured to use the Multi-Byte Character Set (ie. UNICODE is not defined). However, when I call MyFunc(...), in ofn.lpstrTitle there is nonsense after the assignment. How is that possible? The class CCoreUtils is also used in another project built with UNICODE defined, but it gives me nonsense even if I choose Rebuild Solution for this (non-UNICODE) project.
-
August 6th, 2009, 08:11 AM
#2
Re: What's wrong with this code?
Originally Posted by FleetingGlimpse
Hello, I've trouble with this code:
So am I, given all of those dots and incomplete examples. No one knows what the data is supposed to be, and what the definition of "nonsense" at the end of the string is supposed to mean.
Secondly:
Code:
return CCoreUtils::UTF8ToUTF16(str);
If that return value can contain strings with embedded NULL characters, you cannot construct a std::(w)string this way by using the value. If you're using the std::(w)string as a buffer that can contain NULL characters, you have to use the proper constructors and functions (such as append()) to create and change your strings.
However, when I call MyFunc(...), in ofn.lpstrTitle there is nonsense after the assignment. How is that possible? The class CCoreUtils is also used in another project built with UNICODE defined, but it gives me nonsense even if I choose Rebuild Solution for this (non-UNICODE) project.
Give us a complete example, along with the data you're expecting to see.
Regards,
Paul McKenzie
-
August 7th, 2009, 02:17 AM
#3
Re: What's wrong with this code?
Originally Posted by Paul McKenzie
Give us a complete example, along with the data you're expecting to see.
Ok, here's the complete relevant code:
Code:
std::string CCoreUtils::GetConfigDependentString(const std::string& str)
{
return str;
}
bool CCoreUtils::OpenFileDialog(std::string& fileName, const TCHAR* filter, const TCHAR* defaultExtension, const std::string& dialogTitle, unsigned long flags)
{
OPENFILENAME ofn;
TCHAR szFileName[2048] = TEXT("");
TCHAR filenames[2048] = TEXT("");
ZeroMemory(&ofn, sizeof(ofn));
ofn.lpstrFileTitle = filenames;
ofn.nMaxFileTitle = 2048;
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = GetActiveWindow();
ofn.lpstrFilter = filter;
ofn.lpstrFile = szFileName;
ofn.nMaxFile = 2048;
ofn.Flags = flags;
ofn.lpstrDefExt = defaultExtension;
if (!dialogTitle.empty())
{
ofn.lpstrTitle = CCoreUtils::GetConfigDependentString(dialogTitle).c_str();
}
if (GetOpenFileName(&ofn))
{
if (ofn.lpstrFile)
{
fileName = CCoreUtils::GetString(ofn.lpstrFile);
return true;
}
}
return false;
}
void MyApplication::DoApplyDocument()
{
std::string translationPath, formattingPath;
if (CCoreUtils::OpenFileDialog(translationPath, CCoreUtils::FILEDLG_FILTER_RTF, CCoreUtils::FILEDLG_EXTENSION_RTF, "Select translation RTF document"))
{
// do something
}
}
The problem is in the bold line. After the assignment, it looks like this:
http://labts.troja.mff.cuni.cz/~kuris4am/watch.png
-
August 7th, 2009, 02:54 AM
#4
Re: What's wrong with this code?
Blame on me! The problem is in the fact that after leaving the block, the string pointed to by ofn.lpstrTitle gets invalid. Ooops...
-
August 7th, 2009, 03:16 AM
#5
Re: What's wrong with this code?
What you are doing in the bold line is assigning a pointer which as you mentioned may be invalid after some point, what you need to do is copy the dialogstring into the ofn.lpstrTitle( e.g. strcpy).
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
|