CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 25
  1. #1
    Join Date
    Nov 2011
    Location
    India
    Posts
    333

    [RESOLVED] wstring Conversion

    Hi,

    I was read a korean string and display the text.


    Code:
    std::wstring s2ws(const std::string& str)
    {
        int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0);
        std::wstring wstrTo( size_needed, 0 );
        MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), &wstrTo[0], size_needed);
    	
    	//LPCWSTR result = wstrTo.c_str();
    	//::AfxMessageBox((CString)result);
    
        return wstrTo;
    }
    
    
    void CApplicationDlg::Calling()
    {	
    	//s2ws(m_pLanguage->GetString(IDS_EXAMPLE).c_str());  //Read string from dll
    
    	s2ws("인도 남부의 맨체스터로 알려진 코임 바토르는 직물로 유명합니다.");
    }
    It display like wstrTo L"?? ??? ????? ??? ?? ???? ??? ?????." using watch window.

    reading english, dutch, german & french characters working good.
    Regards,

    SaraswathiSrinath

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: wstring Conversion

    What language was set for non-unicode programs?
    Victor Nijegorodov

  3. #3
    Join Date
    Nov 2011
    Location
    India
    Posts
    333

    Re: wstring Conversion

    Quote Originally Posted by VictorN View Post
    What language was set for non-unicode programs?
    Sets "Use Unicode Character Set" for all string inputs

    Code:
         m_lblExample.SetWindowText(L"인도 남부의 맨체스터로 알려진 코임 바토르는 직물로 유명합니다.");
    Its showing perfectly.
    Regards,

    SaraswathiSrinath

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: wstring Conversion

    Quote Originally Posted by saraswathisrinath View Post
    Sets "Use Unicode Character Set" for all string inputs

    Code:
         m_lblExample.SetWindowText(L"인도 남부의 맨체스터로 알려진 코임 바토르는 직물로 유명합니다.");
    Its showing perfectly.
    Now compare it with what you showed in your OP:
    Quote Originally Posted by saraswathisrinath View Post
    Code:
    ...
    	s2ws("인도 남부의 맨체스터로 알려진 코임 바토르는 직물로 유명합니다.");
    ...
    Victor Nijegorodov

  5. #5
    Join Date
    Nov 2011
    Location
    India
    Posts
    333

    Re: wstring Conversion

    Quote Originally Posted by VictorN View Post
    Now compare it with what you showed in your OP:
    In the below code, watch display like ip "?? ??? ????? ??? ?? ???? ??? ?????."
    Code:
    std::string ip =  "인도 남부의 맨체스터로 알려진 코임 바토르는 직물로 유명합니다.";
    need to convert my input from const std::string str in to LPCTSTR.


    This code works for English, Dutch, German & French, but not for Korean.
    If i change the code for string conversion then not work for all.
    Code:
    void CApplicationDlg::TranslateAllControls()
    {
    	//WORKS for all lang, not Korean
    	std::string ip = m_pLanguage->GetString(IDS_EXAMPLE).c_str();    //Read string from dll
    	std::wstring op = s2ws(ip);
    	LPCTSTR result=(LPCTSTR)op.c_str();	
    	m_lblExample.SetWindowText(result);
    }
    Last edited by saraswathisrinath; August 25th, 2020 at 04:12 AM.
    Regards,

    SaraswathiSrinath

  6. #6
    Join Date
    Nov 2011
    Location
    India
    Posts
    333

    Re: wstring Conversion

    Refered these links
    https://forums.codeguru.com/showthre...em-in-VS2005-C
    https://groups.google.com/g/microsof.../c/4bA_9a4U5Qw
    https://www.codeproject.com/Articles...ithin-that-DLL


    tried
    Code:
    setlocale( LC_ALL, "china");
    	_wsetlocale(LC_ALL, L"zh-CN");
            CString buf = LoadStringFromDLL(GetModuleHandle(NULL), IDS_EXAMPLE);
    	m_lblExample.SetWindowText(buf);
    Not working
    Regards,

    SaraswathiSrinath

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: wstring Conversion

    If you are trying to load a localized resource string from a resource library, this isnt the way to do it.

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: wstring Conversion

    Quote Originally Posted by saraswathisrinath View Post
    tried
    Code:
    setlocale( LC_ALL, "china");
    	_wsetlocale(LC_ALL, L"zh-CN");
            CString buf = LoadStringFromDLL(GetModuleHandle(NULL), IDS_EXAMPLE);
    	m_lblExample.SetWindowText(buf);
    Not working
    Why "china"? you wrote it is
    Quote Originally Posted by saraswathisrinath
    a korean string
    Victor Nijegorodov

  9. #9
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: wstring Conversion

    Quote Originally Posted by saraswathisrinath View Post
    Hi,

    I was read a korean string and display the text.


    Code:
    std::wstring s2ws(const std::string& str)
    {
        int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0);
        std::wstring wstrTo( size_needed, 0 );
        MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), &wstrTo[0], size_needed);
    	
    	//LPCWSTR result = wstrTo.c_str();
    	//::AfxMessageBox((CString)result);
    
        return wstrTo;
    }
    
    
    void CApplicationDlg::Calling()
    {	
    	//s2ws(m_pLanguage->GetString(IDS_EXAMPLE).c_str());  //Read string from dll
    
    	s2ws("인도 남부의 맨체스터로 알려진 코임 바토르는 직물로 유명합니다.");
    }
    It display like wstrTo L"?? ??? ????? ??? ?? ???? ??? ?????." using watch window.
    But do you first convert this "korean string" to std::string and then try to convert it to std::wstring?
    Why not to assign it to the std::wstring fro the very begin?
    Victor Nijegorodov

  10. #10
    Join Date
    Nov 2011
    Location
    India
    Posts
    333

    Re: wstring Conversion

    Quote Originally Posted by Arjay View Post
    If you are trying to load a localized resource string from a resource library, this isnt the way to do it.
    First tried like this
    Code:
            std::string ip = m_pLanguage->GetString(IDS_EXAMPLE).c_str();
    	std::wstring op = s2ws(ip);
    	LPCTSTR result=(LPCTSTR)op.c_str();
    	//::AfxMessageBox(result);
    	m_lblExample.SetWindowText(result);
    then tried all possibilities.
    Regards,

    SaraswathiSrinath

  11. #11
    Join Date
    Nov 2011
    Location
    India
    Posts
    333

    Re: wstring Conversion

    Quote Originally Posted by VictorN View Post
    Why "china"? you wrote it is
    I have created chinese.dll to read a resource string from string table like korean.
    Sorry for the confusion.
    Regards,

    SaraswathiSrinath

  12. #12
    Join Date
    Nov 2011
    Location
    India
    Posts
    333

    Re: wstring Conversion

    Quote Originally Posted by VictorN View Post
    But do you first convert this "korean string" to std::string and then try to convert it to std::wstring?
    Why not to assign it to the std::wstring fro the very begin?
    return type is std::string, that's why assigned like below.
    Code:
    std::string ip = m_pLanguage->GetString(IDS_EXAMPLE).c_str();

    i was converted this for display purpose
    Code:
    std::wstring op = s2ws(ip);
    LPCTSTR result=(LPCTSTR)op.c_str();
    m_lblExample.SetWindowText(result);
    Regards,

    SaraswathiSrinath

  13. #13
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: wstring Conversion

    Quote Originally Posted by saraswathisrinath View Post
    First tried like this
    Code:
            std::string ip = m_pLanguage->GetString(IDS_EXAMPLE).c_str();
    	std::wstring op = s2ws(ip);
    	LPCTSTR result=(LPCTSTR)op.c_str();
    	//::AfxMessageBox(result);
    	m_lblExample.SetWindowText(result);
    then tried all possibilities.
    Have you tried the Win32 api LoadString function?

  14. #14
    Join Date
    Nov 2011
    Location
    India
    Posts
    333

    Re: wstring Conversion

    Quote Originally Posted by Arjay View Post
    Have you tried the Win32 api LoadString function?
    Its retrive data from local resource string table.
    Code:
    CStringW result;
    result.LoadString(IDS_EXAMPLE); 
    ::AfxMessageBox(result);
    How can i retrieve data from dll string table?
    Code:
    m_pLanguage->GetString(IDS_EXAMPLE)
    how can i pass this in to LoadString()?

    I was loaded a language DLL and extract the strings from resource table Ref: https://www.codeproject.com/Articles...93#xx5745393xx
    Code:
    // Init language object
    CLanguage * pLanguage = CLanguage::Instance();
    // Set text for static control
    m_lblExample.SetWindowText(m_pLanguage->GetString(IDS_EXAMPLE).c_str());
    Regards,

    SaraswathiSrinath

  15. #15
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: wstring Conversion

    What is
    Code:
    m_pLanguage->GetString(IDS_EXAMPLE)
    What class is m_pLanguage? How is its GetString method implemented?

Page 1 of 2 12 LastLast

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