Read in Cyrillic, Arabic, Japanese Text with GetWindowText not working

Unanswered Read in Cyrillic, Arabic, Japanese Text with GetWindowText not working

  • Monday, January 11, 2010 4:14 PM
     
     
    I am trying to read text from an external application using GetWindowText. It works just fine for all of the other languages in the software I am trying to read from. (German, English, French) However, it fails when it tries to read in Cyrillic (Russian), Arabic, Japanese. All it gets is a bunch of question marks. Here is the code that I am using.

    //**********************************
    [DllImport("User32.dll" , CharSet = CharSet.Unicode)]
    public
    static extern Int32 GetWindowText(int hWnd, StringBuilder s, int nMaxCount);

    //formdetails is a StringBuilder
    Win32.GetWindowText(hWnd, formDetails, 512);
    //**********************************

    When I display the text using a textbox it is merely a bunch of "?????? ????? ?? ???????".
    I know it has nothing to do with the textboxes inability to display those languages because if I directly drop a string of Cyrillic into a variable in C# and display it in the box, it display just fine. What am I doing wrong?

    Thanks!
    -Elmernite
    • Edited by elmernite Monday, January 11, 2010 4:22 PM spelling mistakes
    •  

All Replies

  • Monday, January 11, 2010 5:58 PM
     
     
    Is required fonts installed?
    With best regards, Yasser Zamani
  • Monday, January 11, 2010 6:05 PM
     
     
    Yes, because if I drop the text directly into a variable in C#'
    IE.
    string = "Japanese Text here"
    It displays in a text box just fine.

    I'm guessing the GetWindowText or StringBuilder has problems reading in the aforementioned languages. (Or maybe translating it to the correct encoding?)

    -Elmernite

  • Monday, January 11, 2010 6:27 PM
     
      Has Code
    Please run following code and paste the result for me:
    byte[] bs = System.Text.Encoding.Unicode.GetBytes(/*"Japanese Text here"*/);
    string s = "";
    foreach (byte b in bs)
        s += String.Format("{0:X}", b) + ",";
    Clipboard.SetText(s);
    

    With best regards, Yasser Zamani
  • Monday, January 11, 2010 6:48 PM
     
     

    B9,30,AF,30,EA,30,FC,30,F3,30,B,4E,E8,90,6E,30,E1,30,CB,30,E5,30,FC,30,92,30,78,90,9E,62,57,30,66,30,4F,30,60,30,55,30,44,30,2,30,

    That was the result of manually entering this text.   "スクリーン下部のメニューを選択してください。" into the code you gave me.



    This was the result of entering formDetails.ToString(); From my code, into the code you gave me.
    3F,0,3F,0,3F,0,3F,0,3F,0,3F,0,3F,0,3F,0,3F,0,3F,0,3F,0,3F,0,3F,0,3F,0,3F,0,3F,0,3F,0,3F,0,3F,0,3F,0,3F,0,3F,0,


    ^^^ If everything worked like a wanted the formDetails.ToString() should have held the above Japanese.

    -Elmernite
  • Monday, January 11, 2010 7:08 PM
     
     
    Can you put more code, Please?
    With best regards, Yasser Zamani
  • Monday, January 11, 2010 8:06 PM
     
      Has Code
    Sorry, for the delay.


               
    StringBuilder formDetails = new StringBuilder(512);
               
                int txtValue;
                string editText = "";
    
                txtValue = Win32.GetClassName(hWnd, formDetails, 512); //gets the class name
                editText = formDetails.ToString().ToLower().Trim(); //converts it to string and trims
    
                if (editText.Contains("ccabledbibstatictext") == true) //makes sure I have the correct class before I read the info
                {
    
                    txtValue = Win32.GetWindowText(hWnd, formDetails, 512); // reads the info from the class
                    editText = formDetails.ToString().Trim(); // Converts to string and trims
    
                    MessageBox.Show(editText); // This is where is shows up as ????? instead of the proper text
                }

    -Elmernite
  • Tuesday, January 12, 2010 6:18 AM
     
     
    Yes, you are right, it seems there is a problem for example download following sample code and set the window text to a japanese string and run. it's not working probably!!!:
    http://www.artisticode.com/Downloads/138.aspx
    With best regards, Yasser Zamani
  • Tuesday, January 12, 2010 2:04 PM
     
     
    Yes, so it doesn't work, so here is my question. The problem must be in GetWindowText, since I know that stringbuilders support Unicode. Is there anyway I can make GetWindowText just fetch the information byte for byte and then I manually do the conversion once I get the bytes into c#?

    -Elmernite
  • Wednesday, January 13, 2010 8:32 AM
     
      Has Code
    Following function will help you to enumerate all possible encodings:
            class PossibleEncodingBytes
            {
                public EncodingInfo EI;
                public byte[] B;
                public PossibleEncodingBytes(EncodingInfo ei, byte[] b)
                {
                    EI = ei;
                    B = (byte[])b.Clone();
                }
            }
            IEnumerable<PossibleEncodingBytes> EnumerateEncodingsBytes(string s)
            {
                EncodingInfo[] eis = Encoding.GetEncodings();
                foreach (EncodingInfo ei in eis)
                {
                    yield return new PossibleEncodingBytes(ei, ei.GetEncoding().GetBytes(s));
                }
            }
    

    With best regards, Yasser Zamani
  • Thursday, January 14, 2010 9:04 AM
    Moderator
     
     
    Hi,

    Have you tried spy++ ? what is the value of the window?
    Please make sure the window you are working with is the one you want to retrieve text from.
    I suspect you are getting text from another window.

    Can I have a copy of the application or an application that can help us to see the problem?

    Harry


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
  • Thursday, January 14, 2010 6:59 PM
     
     
    Hi,

    Have you tried spy++ ? what is the value of the window?
    Please make sure the window you are working with is the one you want to retrieve text from.
    I suspect you are getting text from another window.

    Can I have a copy of the application or an application that can help us to see the problem?

    Harry


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.

    Hi Harry
    Please download http://www.artisticode.com/Downloads/138.aspx then set the startup form text to "スクリーン下部のメニューを選択してください。".
    If you run the sample it don't work for japanese text.
    But if you test it with Persian e.g. "به نام خداوند بخشنده و مهربان" it work!
    With best regards, Yasser Zamani
  • Friday, January 15, 2010 2:38 AM
    Moderator
     
     
    Hi,

    It seems the link is broken. I could not open it.

    Harry
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
  • Friday, January 15, 2010 3:00 PM
     
     
    Hi,

    It seems the link is broken. I could not open it.

    Harry
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.

    Hi Harry,
    But i can open it nevertherless i put it in my Sky Drive and you can get it from Active Window
    Thanks again.
    With best regards, Yasser Zamani
  • Monday, January 18, 2010 3:49 AM
    Moderator
     
     
    Hi,

    I just run the application , but the these charactors are retrieved , and displayed.
    What's the language of your OS?

    Harry
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
  • Monday, January 18, 2010 6:14 AM
     
     
    Hi,

    I just run the application , but the these charactors are retrieved , and displayed.
    What's the language of your OS?

    Harry
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.

    Hi Harry,
    Yes, it works if you just run it.
    But if you open the solution by Visual Studio, set the startup form text to "スクリーン下部のメニューを選択してください。" and run the sample it doesn't work for japanese text.
    But if you test it with Persian e.g. "به نام خداوند بخشنده و مهربان" it works!

    My OS is Microsoft Windows Vista Ultimate and it's language is en-US.

    Thanks.

    "Sweet are the uses of adversity,
     Which like the toad, ugly and venomous,
     Wears yet a precious jewel in his head;
     And this our life, exempt from public haunt,
     Finds tongues in trees,
     Books in the running brooks,
     Sermons in stones, and good in everything" - William Shakespeare

  • Monday, January 18, 2010 6:46 AM
    Moderator
     
     
    Hi,

    I'm not sure if you mean the text property of the form by "startup form text".
    If I set the text property , the application runs well , and the jananese characters are displayed.

    Harry
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
  • Monday, January 18, 2010 3:29 PM
     
     

    Are the window and the label using the same Font?

  • Tuesday, January 19, 2010 2:09 AM
    Moderator
     
     
    Hi,

    That's strange , you application works here,please take a look at the image:
    http://cid-219dc49fbdfffbd1.skydrive.live.com/self.aspx/Photos/New%20Bitmap%20Image.bmp

    Does Spy++ work on your machine?
    What is the value of StringBuilder Buff?
    What is the last error ?

    Harry
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
  • Tuesday, January 19, 2010 3:19 PM
     
     
    I'm not talking about the Font you defined in your Form class.
    I think Windows settings override that.
  • Tuesday, January 19, 2010 3:30 PM
     
     
    Hi Louis.fr
    Do you mean "Regional and Language Options" in "Control Panel"
    Following is my settings:
    Formats Tab-->Current Format-->English (United States)
    Location Tab-->Current Location-->United States

    "Sweet are the uses of adversity,
     Which like the toad, ugly and venomous,
     Wears yet a precious jewel in his head;
     And this our life, exempt from public haunt,
     Finds tongues in trees,
     Books in the running brooks,
     Sermons in stones, and good in everything" - William Shakespeare

  • Tuesday, January 19, 2010 4:06 PM
     
     

    Hard to tell you since I have the names in French.
    I think it must be Display settings, 4th tab page.

  • Tuesday, January 19, 2010 7:24 PM
     
     
    Great Louis.fr
    I do following and it figure out the problem less or more:
    Control Panel-->Regional and Language Options-->Administrative Tab(4th Tab)-->Change System Locale(Button)-->selecting Japanese language for text in programs which do not support unicode

    But why it woks with Persian - Persian is unicode and i did not select persian language for text in programs which do not support unicode?
    In other word
    Why GetTextWindow method don't return unicode for Japanese strings and return unicode for Persian strings?

    "Sweet are the uses of adversity,
     Which like the toad, ugly and venomous,
     Wears yet a precious jewel in his head;
     And this our life, exempt from public haunt,
     Finds tongues in trees,
     Books in the running brooks,
     Sermons in stones, and good in everything" - William Shakespeare

  • Wednesday, January 20, 2010 2:57 PM
     
     
    I am running Windows XP and do not have a fourth tab. (The Administrative Tab) Is there anyway I could still change this?

    BTW, I cannot get Persian text either.

    -Elmernite
  • Wednesday, January 20, 2010 4:21 PM
     
     

    The fourth tab I was talking about is in the Display settings.
    It's not the "administrative tab". In French, it's called "Apparence".
    It's where you can define the colors used by Windows.

    Note that I don't know if it's going to help you for your problem.

  • Wednesday, January 20, 2010 6:08 PM
     
     
    I was actually talking about where Yasser.Zamani.Iran says to click on a fourth tab which I cannot find.

    I don't think changing the font like you said would affect it because it displays just fine, I just cannot read from it.

    -Elmernite
  • Wednesday, January 20, 2010 6:31 PM
     
     
    Note: You must be logged in with an account that has Administrative Privileges.
    1. Click Start > Control Panel.
    2. Double-click the Regional and Language Options icon.
      The Regional and Language Options dialog box appears.
    3. Click the Advanced tab.
      (If there is no Advanced tab, then you are not logged in with administrative privileges.)
    4. Under the Language for non-Unicode programs section, select the desired language from the drop down menu.
    Windows XP: Select desired language
    1. Click OK.
      The system displays a dialog box asking whether to use existing files or to install from the operating system CD. Ensure that you have the CD ready.
    2. Follow the guided instructions to install the files.
    3. Restart the computer after the installation is complete.

    Reference: http://www.java.com/en/download/help/locale.xml

    "Sweet are the uses of adversity,
     Which like the toad, ugly and venomous,
     Wears yet a precious jewel in his head;
     And this our life, exempt from public haunt,
     Finds tongues in trees,
     Books in the running brooks,
     Sermons in stones, and good in everything" - William Shakespeare


  • Wednesday, January 20, 2010 10:11 PM
     
     
    Ok, that worked, sort of.....

    It only worked for the Japanese text. Nothing else, so it really isn't a true fix. I need it to be able to work with pretty much any Unicode font.

    -Elmernite
  • Thursday, January 21, 2010 9:41 AM
     
     
    Great Louis.fr
    I do following and it figure out the problem less or more:
    Control Panel-->Regional and Language Options-->Administrative Tab(4th Tab)-->Change System Locale(Button)-->selecting Japanese language for text in programs which do not support unicode

    But why it woks with Persian - Persian is unicode and i did not select persian language for text in programs which do not support unicode?
    In other word
    Why GetTextWindow method don't return unicode for Japanese strings and return unicode for Persian strings?
    As i told above, i don't know "Why GetTextWindow method don't return unicode for some strings and return unicode for another strings"?
    It is strange! Persian string works for me.

    OUT OF LINE REQUEST: elmernite, i am interesting in what does mean "スクリーン下部のメニューを選択してください。" in english?
    Thanks.


    "Sweet are the uses of adversity,
     Which like the toad, ugly and venomous,
     Wears yet a precious jewel in his head;
     And this our life, exempt from public haunt,
     Finds tongues in trees,
     Books in the running brooks,
     Sermons in stones, and good in everything" - William Shakespeare

  • Thursday, January 21, 2010 1:42 PM
     
     
    Hmm... That is extremely strange. And I don't guess there is any alternative method to read the text from another application other than these calls?



    OUT OF LINE ANSWER: This is not perhaps an exact translation, (I don't speak Japanese) But in the program that I am trying to read the text from, the English equivalent is "Please Select the function desired by pressing the corresponding button at the bottom of the screen."

    And Google Translates it as.
    "Please select the menu at the bottom of the screen."


    -Elmernite
    • Edited by elmernite Thursday, January 21, 2010 1:44 PM Added the fancy yellow box
    •