Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7058525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:07:23+00:00 2026-05-28T04:07:23+00:00

I am reading text from a notepad opened by my program.and this is my

  • 0

I am reading text from a notepad opened by my program.and this is my code

const int WM_GETTEXT = 0x000D;
const int WM_GETTEXTLENGTH = 0x000E;

[DllImport("User32.dll", EntryPoint = "SendMessage")]
extern static int SendMessageGetTextLength(IntPtr hWnd, int msg, IntPtr wParam,     IntPtr lParam);
[DllImport("User32.dll", EntryPoint = "SendMessage", CharSet = CharSet.Auto)]
extern static IntPtr SendMessageGetText(IntPtr hWnd, int msg, IntPtr wParam, [Out] StringBuilder lParam);

[DllImport("user32.dll", EntryPoint = "FindWindowEx")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

public static string GetText(IntPtr hwnd)
{
    if (hwnd == IntPtr.Zero)
        throw new ArgumentNullException("hwnd");
    IntPtr handler = FindWindowEx(hwnd, new IntPtr(0), "Edit", null);
    int length = SendMessageGetTextLength(handler, WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero);
    if (length > 0 && length < int.MaxValue)
    {
        length++;
        StringBuilder sb = new StringBuilder(length);

        SendMessageGetText(handler, WM_GETTEXT, (IntPtr)sb.Length, sb);
        return sb.ToString();
    }
    return String.Empty;
}

It is getting the text but in a special encoding.
For example, if the text entered is ‘hello’ it gets ‘興梀㇨ȿڳㇺ’.
What is the encoding of this text so I can decode it to ASCII?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:07:23+00:00Added an answer on May 28, 2026 at 4:07 am

    Your problem is in fact that you are passing sb.Length in the WM_GETTEXT message, when in fact you should be passing sb.Capacity or even just length.

    I would do it like this:

    if (length > 0 && length < int.MaxValue)
    {
        StringBuilder sb = new StringBuilder(length+1);
        SendMessageGetText(handler, WM_GETTEXT, (IntPtr)length+1, sb);
        return sb.ToString();
    }
    

    I’d also point out that WM_GETTEXT will not return more than 64k characters to the length < int.MaxValue isn’t what you need.


    Of course, in the longer run it may be better to use the Unicode throughout so that you can support international text.

    I personally would always opt for using the Unicode APIs and use the following p/invoke declarations:

    [DllImport("User32.dll", EntryPoint = "SendMessage", 
        CharSet = CharSet.Unicode, SetLastError = true)]
    extern static int SendMessageGetTextLength(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
    [DllImport("User32.dll", EntryPoint = "SendMessage",
        CharSet = CharSet.Unicode, SetLastError = true)]
    extern static IntPtr SendMessageGetText(IntPtr hWnd, int msg, IntPtr wParam, StringBuilder lParam);
    [DllImport("user32.dll", EntryPoint = "FindWindowEx",
        CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am learning the basics of reading from text files. I have code that
I'm reading text from a flat file in c# and need to test whether
I was reading the following text from Stanford's Programming Paradigms class , and I
I'm having a problem where instead of reading a text file from the location
When reading lines from a text file using python, the end-line character often needs
I have a large text file I am reading from and I need to
I am reading a text file with this format: grrr,some text,45.4321,54.22134 I just have
I have a simple text reading code for Visual Basic: Dim fileReader As String
I am stuck with the problems like, reading text from a specific location (x=10,
I am reading the dragon book. Quoting the text from the book (3.1.4 Lexical

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.