CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    Join Date
    Mar 2006
    Posts
    94

    Question Retrieving Arabic characters using GetWindowText?

    I am using Windows XP and Visual C++ and have the Arabic language pack installed so that I can switch from English to Arabic as needed. In my program I have a RichEditCtrl that allows a user to type in text on a single or multiple lines. My problem is, when I try to fetch the text from the RichEditCtrl using GetWindowText I get a "?" for every Arabic character. Here is the code I tried:

    Code:
    CRichEditCtrl* myEditCtrl = GetEdit( CtrlDefiniitions::COMPOSITION_EDIT );
    
    CString myString;
    myEditCtrl->GetWindowText(myString);
    I also read somewhere that perhaps my language settings were not correct, however, the article I read was 8 years old and I couldn't get the settings to match what they were talking about here: http://www.tek-tips.com/viewthread.cfm?qid=256666

    Another method I have tried is to use GetLine instead of GetWindowText, but I am unable to figure out how to get all of the lines from the RichEditCtrl into a single CString object. Here is the code I tried for that:

    Code:
    TCHAR tCharLine[4096];
    
    CRichEditCtrl* myEditCtrl = GetEdit( CtrlDefiniitions::COMPOSITION_EDIT );
    
    \\ This only gives me the first line from the RichEditCtrl, but I want all of the lines!
    int nBytesCopied = myEditCtrl->GetLine(0, tCharLine, sizeof(tCharline)/sizeof(tCharLine[0]));
    
    CString myString(tCharLine);
    The 2nd code snippet gives me the proper character output but only gives me the first line from the RichEditCtrl . Any ideas/suggestions?
    "My software doesn't have bugs, it just develops random features."

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

    Re: Retrieving Arabic characters using GetWindowText?

    Quote Originally Posted by Ciralia View Post
    I am using Windows XP and Visual C++ and have the Arabic language pack installed so that I can switch from English to Arabic as needed. In my program I have a RichEditCtrl that allows a user to type in text on a single or multiple lines. My problem is, when I try to fetch the text from the RichEditCtrl using GetWindowText I get a "?" for every Arabic character. Here is the code I tried:

    Code:
    CRichEditCtrl* myEditCtrl = GetEdit( CtrlDefiniitions::COMPOSITION_EDIT );
    
    CString myString;
    myEditCtrl->GetWindowText(myString);
    Is your build a UNICODE or ANSI?
    If the latter - what is the user locale: English or Arabic (it must be Arabic to recognize the Arabic letters)?
    Victor Nijegorodov

  3. #3
    Join Date
    Mar 2006
    Posts
    94

    Re: Retrieving Arabic characters using GetWindowText?

    Is your build a UNICODE or ANSI?
    If the latter - what is the user locale: English or Arabic (it must be Arabic to recognize the Arabic letters)?
    I am not sure how to tell if the build is UNICODE or ANSI. The user locale is set to English so that when my program starts it starts in English and I have to manually switch it to Arabic if I want Arabic characters.
    "My software doesn't have bugs, it just develops random features."

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

    Re: Retrieving Arabic characters using GetWindowText?

    Quote Originally Posted by Ciralia View Post
    I am not sure how to tell if the build is UNICODE or ANSI.
    What preprocessor definitions were set:
    _MBCS
    or
    UNICODE,_UNICODE?

    Quote Originally Posted by Ciralia View Post
    The user locale is set to English so that when my program starts it starts in English and I have to manually switch it to Arabic if I want Arabic characters.
    Well, what language is set "to match the language version of the non-Unicode programs" (it is in Control panel -> Regional... Optons -> Advance tab): if your app is not a unicode one then you have to set the Arabic as a language is set "to match the language version of the non-Unicode programs".
    Victor Nijegorodov

  5. #5
    Join Date
    Mar 2006
    Posts
    94

    Re: Retrieving Arabic characters using GetWindowText?

    My project has the _MBCS in its Preprocessor Definitions which means it isn't Unicode. Also in my language settings it is set to English for non-Unicode programs.

    That being said, is there a programmatic way around having to change the computer settings?
    "My software doesn't have bugs, it just develops random features."

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

    Re: Retrieving Arabic characters using GetWindowText?

    Either build your program as a Unicode
    or set the Arabic as a language is set "to match the language version of the non-Unicode programs".
    There is no other way.
    Victor Nijegorodov

  7. #7
    Join Date
    Mar 2006
    Posts
    94

    Re: Retrieving Arabic characters using GetWindowText?

    Is there a way to programmatically set the non-Unicode language setting under the Regional Settings area to Arabic?
    "My software doesn't have bugs, it just develops random features."

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

    Re: Retrieving Arabic characters using GetWindowText?

    No. Or may be but I don't know how: note that you have to restart your computer after let this new setting work.
    Victor Nijegorodov

  9. #9
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Retrieving Arabic characters using GetWindowText?

    Quote Originally Posted by VictorN View Post
    Either build your program as a Unicode
    or set the Arabic as a language is set "to match the language version of the non-Unicode programs".
    There is no other way.
    setlocale()

    However, you should really consider changing your application to be unicode. It'll relieve you of a LOT of hassle dealing with mbcs and locale changes.
    Note that locale changes may not be picked up corretly by all libraries out there, a lot of them are created with ansi=ascii in mind. This is particularly true for ansi-build libraries made by american companies.

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

    Re: Retrieving Arabic characters using GetWindowText?

    Victor Nijegorodov

  11. #11
    Join Date
    Jan 2011
    Posts
    13

    Re: Retrieving Arabic characters using GetWindowText?

    I took the suggestion to change my project to be Unicode (_UNICODE and UNICODE), so now the GetWindowText method retrieves the foreign characters without problems, however, I am running into an issue with the CRichEditCtrl in displaying the characters. I am using RichEditCtrl v2.0, but when I use StreamIn to populate it, it just comes out blank. Not even english characters are displayed. Here is the code:

    Code:
    void MyClass :: WriteToEditCtrl (CRichEditCtrl& myEditCtrl)
    {
         DWORD dwError = 0;
    
         //myMessage is a CString and has RTF code followed by a regular message "abc"
         EDITSTREAM es = {(DWORD) &myMessage, dwError, EditStreamCallBack};
    
         long err = myEditCtrl.StreamIn(SF_RTF | SFF_SELECTION, es);
    }
    
    DWORD CALLBACK EditStreamCallBack( DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
    {
    CString *pstr = (CString *)dwCookie;
    
        if(pstr->GetLength() < cb)
        {
             *pcb = pstr->GetLength();
             memcpy(pbBuff, (LPCTSTR)*pstr, *pcb);
             pstr->Empty();
        }
        else
        {
             *pcb = cb;
             memcpy(pbBuff, (LPCTSTR)*pstr, *pcb);
            *pstr = pstr->Right(pstr->GetLength() - cb);
        }
    
     return 0;
    }
    For the StreamIn method I have also tried SF_TEXT | SF_UNICODE, but that puts the RTF code in the box instead of the message, but all I want to see is "abc" which is after the RTF code.

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

    Re: Retrieving Arabic characters using GetWindowText?

    Quote Originally Posted by Programming241444 View Post
    Code:
    DWORD CALLBACK EditStreamCallBack( DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
    {
    CString *pstr = (CString *)dwCookie;
    
        if(pstr->GetLength() < cb)
        {
             *pcb = pstr->GetLength();
             memcpy(pbBuff, (LPCTSTR)*pstr, *pcb);
             pstr->Empty();
        }
        ...
    }
    This is incorrect. CString::GetLength returns the length in characters (in UNICODE build 1 character == 2 bytes) while *pcb is supposed to have the number of bytes transferred.
    Victor Nijegorodov

  13. #13
    Join Date
    Jan 2011
    Posts
    13

    Re: Retrieving Arabic characters using GetWindowText?

    Are you telling me to multiply my length by 2 or to use a different type for pcb? Sorry I'm not very well versed in C++.
    Last edited by Programming241444; January 28th, 2011 at 02:20 PM.

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

    Re: Retrieving Arabic characters using GetWindowText?

    Quote Originally Posted by Programming241444 View Post
    Are you telling me to multiply my length by 2 or to use a different type for pcb? Sorry I'm not very well versed in C++.
    You cannot use "a different type for pcb" because it was already defined by Microsoft who declared this
    Code:
    DWORD CALLBACK EditStreamCallback(
                                        DWORD_PTR dwCookie, // application-defined value
                                        LPBYTE pbBuff,      // data buffer
                                        LONG cb,            // number of bytes to read or write
                                        LONG *pcb           // number of bytes transferred
                                        );
    And you should multiply not by 2 but by sizeof(TCHAR)
    Victor Nijegorodov

  15. #15
    Join Date
    Jan 2011
    Posts
    13

    Re: Retrieving Arabic characters using GetWindowText?

    I updated the EditStreamCallback code to this but my CRichEditCtrl still shows up blank:

    Code:
    DWORD CALLBACK EditStreamCallBack( DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
    {
    CString *pstr = (CString *)dwCookie;
    int length = pstr->GetLength() * sizeof(TCHAR);
    
        if(length < cb)
        {
             *pcb = length;
             memcpy(pbBuff, (LPCTSTR)*pstr, *pcb);
             pstr->Empty();
        }
        else
        {
             *pcb = cb;
             memcpy(pbBuff, (LPCTSTR)*pstr, *pcb);
            *pstr = pstr->Right(length - (cb / sizeof(TCHAR)));
        }
    
     return 0;
    }
    Since the buffer size is large the 'else' statement is executed. Not sure what else could be causing the edit box to end up blank.
    Last edited by Programming241444; January 28th, 2011 at 02:46 PM.

Page 1 of 2 12 LastLast

Tags for this Thread

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