Click here to Skip to main content
15,916,030 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I have developed an Application in C# which extract text from external application's textbox , I am using user32.dll, The application is working fine but my problem is this - The external application's textbox contains text in unicode format, so whenever I extract text in my application it shows "??????" text. I have tried setting charset.unicode , and also used RichTextBox to show text in my application.
Please let me know how to exract unicode text from external application.



Here is code I am using


C#
private void button1_Click(object sender, EventArgs e)
        { 
IntPtr MytestHandle = new IntPtr(0x00060342);

            HandleRef hrefHWndTarget = new HandleRef(null, MytestHandle);
          
         // encode text into 
            richTextBox1.Text = ModApi.GetText(hrefHWndTarget.Handle);
        }

public static class ModApi
        {
          [DllImport("user32.dll", EntryPoint = "SendMessageTimeout", SetLastError = true, CharSet = CharSet.Unicode)]

         public static extern uint SendMessageTimeoutText(IntPtr hWnd, int Msg, int countOfChars, StringBuilder text, uint flags, uint uTImeoutj, uint result); 
           
                                 
          public static string GetText(IntPtr hwnd)
            {
                var text = new StringBuilder(1024);
                
                if (SendMessageTimeoutText(hwnd, 0xd, 1024, text, 0x2, 1000, 0) != 0)
                {
                    return text.ToString();
                }

                MessageBox.Show(text.ToString());
                return "";
            }
        }
Posted
Updated 13-Aug-13 17:18pm
v5

I'm not sure if this is the only problem, but probably the "real" (mangles) method names are SendMessageTimeoutA and SendMessageTimeoutW for ANSI/Unicode. So, you should either use SendMessageTimeoutW explicitly, or use the parameter CharSet=CharSet.Auto. Such parameter value takes care of mangling automatically, effectively giving you the Unicode version. Please see:
http://www.pinvoke.net/default.aspx/user32.sendmessagetimeout[^].

Your P/Invoke, without it, might be is incorrect, because the default is CharSet.Unicode, and it would require "W".

—SA
 
Share this answer
 
Comments
[no name] 14-Aug-13 1:43am    
I may be wrong but shouldn't SendMessageTimeoutText be Unicode function by default?
Sergey Alexandrovich Kryukov 14-Aug-13 1:46am    
I'm not sure. Not in this default form. Defaults to Unicode with CharSet.Unicode, that's for sure. I used to use APIs with char *, they don't work correctly without "W"...
—SA
shreesoft 14-Aug-13 1:46am    
I think it can work , but could you create little program to describe more.
Sergey Alexandrovich Kryukov 14-Aug-13 1:50am    
Describe what? You can do exact same thing, only add CharSet=CharSet.Auto parameter to your [DLLImport]. That would be the only change. Just spend a minute, try it. And tell us what happens.
—SA
Sergey Alexandrovich Kryukov 14-Aug-13 1:52am    
This is another option, but I would start with adding CharSet=CharSet.Auto...
OP can also try to change signature to that written in pinvoke.net, and marshal to string explicitly...
—SA
You could convert the Unicode to Ascii or vice versa.

http://msdn.microsoft.com/en-us/library/system.text.encoding.convert%28v=vs.71%29.aspx[^]

C#
// Perform the conversion from one encoding to the other.
         byte[] asciiBytes = Encoding.Convert(unicode, ascii, text.toString());
            
         // Convert the new byte[] into a char[] and then into a string.
         // This is a slightly different approach to converting to illustrate
         // the use of GetCharCount/GetChars.
         char[] asciiChars = new char[ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
         ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
         string asciiString = new string(asciiChars);
         return asciiString;
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900