CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2007
    Posts
    13

    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.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: What's wrong with this code?

    Quote Originally Posted by FleetingGlimpse View Post
    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

  3. #3
    Join Date
    Jul 2007
    Posts
    13

    Re: What's wrong with this code?

    Quote Originally Posted by Paul McKenzie View Post
    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

  4. #4
    Join Date
    Jul 2007
    Posts
    13

    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...

  5. #5
    Join Date
    Jul 2005
    Posts
    266

    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
  •  





Click Here to Expand Forum to Full Width

Featured