-
January 26th, 2011, 10:52 AM
#1
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."
-
January 26th, 2011, 11:22 AM
#2
Re: Retrieving Arabic characters using GetWindowText?
Originally Posted by Ciralia
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
-
January 26th, 2011, 12:49 PM
#3
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."
-
January 26th, 2011, 01:02 PM
#4
Re: Retrieving Arabic characters using GetWindowText?
Originally Posted by Ciralia
I am not sure how to tell if the build is UNICODE or ANSI.
What preprocessor definitions were set:
_MBCS
or
UNICODE,_UNICODE?
Originally Posted by Ciralia
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
-
January 26th, 2011, 01:10 PM
#5
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."
-
January 26th, 2011, 01:21 PM
#6
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
-
January 26th, 2011, 04:46 PM
#7
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."
-
January 26th, 2011, 04:58 PM
#8
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
-
January 27th, 2011, 08:21 AM
#9
Re: Retrieving Arabic characters using GetWindowText?
Originally Posted by VictorN
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.
-
January 28th, 2011, 04:37 AM
#10
Re: Retrieving Arabic characters using GetWindowText?
Victor Nijegorodov
-
January 28th, 2011, 01:05 PM
#11
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.
-
January 28th, 2011, 01:26 PM
#12
Re: Retrieving Arabic characters using GetWindowText?
Originally Posted by Programming241444
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
-
January 28th, 2011, 02:16 PM
#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.
-
January 28th, 2011, 02:26 PM
#14
Re: Retrieving Arabic characters using GetWindowText?
Originally Posted by Programming241444
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
-
January 28th, 2011, 02:44 PM
#15
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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|