用下面的代码去另一个进程中一TRichEdit控件中的文本,
句柄已经得到. 可是就是取不出来
private void button1_Click(object sender, System.EventArgs e)
{
   StringBuilder strB = new StringBuilder(100);
   SendMessage(hWnd,WM_GETTEXT,100,strB);
   string a = strB.ToString().Trim();
   MessageBox.Show(a);
}
就是取不到该 TRichEdit 中的文本.
可是用SPY++ 监控该控件的信息 ,每按一次按钮 就有两条WM_GETTEXT 短信如下:<00001> xxxxxxx S WM_GETTEXT cchTextMax:100 lpszText:xxxxxx
<00002> xxxxxxx R WM_GETTEXT cchCopyied:0 lpszText: xxxxxxx ("所需要的字符串出现在这里")说明已经把字符从控件中取出,只是没有返回到C#程序中.第一个帮解决的,给150分
(见另一贴 http://community.csdn.net/Expert/topic/4437/4437601.xml?temp=.586529  中的100分 )

解决方案 »

  1.   

    [DllImport("user32.dll",EntryPoint="SendMessage")]
    public static extern int SendMessage(IntPtr hWnd,int wMsg,IntPtr  wParam,StringBuilder  lParam);
    const int WM_GETTEXT=0x000D;
    private void button1_Click(object sender, System.EventArgs e)
    {
    StringBuilder strB = new StringBuilder(100);
    SendMessage(this.richTextBox1.Handle,WM_GETTEXT,new  IntPtr(255),strB);
    string a = strB.ToString().Trim();
    MessageBox.Show(a);
    }
      

  2.   

    spy++即然能看到,说明 它 还是得到了 该字符,难道是拦截的?
    我不是很熟悉,还请高人出手!
      

  3.   

    private void button1_Click(object sender, System.EventArgs e)
    {
       StringBuilder strB = new StringBuilder(100);
       SendMessage(hWnd,WM_GETTEXT,100,ref strB);
       string a = strB.ToString().Trim();
       MessageBox.Show(a);
    }在下面加ref试试:
    SendMessage(hWnd,WM_GETTEXT,100,ref strB);
      

  4.   

    C# Signature:
    [DllImport("user32.dll")]
    static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);//Overload for string lParam (e.g. WM_GETTEXT)
    [DllImport("user32.dll")]
    static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, [Out] StringBuilder lParam);// Overloads use on toolbar/rebar
    [DllImport("user32.dll", CharSet=CharSet.Auto)]
    static extern void SendMessage(IntPtr hWnd, int msg, int wParam, ref RECT lParam);[DllImport("user32.dll", CharSet=CharSet.Auto)]
    static extern int SendMessage(IntPtr hWnd, int msg, int wParam, ref POINT lParam);[DllImport("user32.dll", CharSet=CharSet.Auto)]
    static extern void SendMessage(IntPtr hWnd, int msg, int wParam, ref TBBUTTON lParam);[DllImport("user32.dll", CharSet=CharSet.Auto)]
    static extern void SendMessage(IntPtr hWnd, int msg, int wParam, ref TBBUTTONINFO lParam);[DllImport("user32.dll", CharSet=CharSet.Auto)]
    static extern void SendMessage(IntPtr hWnd, int msg, int wParam, ref REBARBANDINFO lParam);//C#
    //This samlpe first uses SendMessage and WM_GETTEXTLENGTH
    //to get the length of the text and then it uses that value
    //and SendMessage with WM_GETTEXT and a StringBuilder to
    //extract the text.private const UInt32 WM_GETTEXT        = 0x000D;
    private const UInt32 WM_GETTEXTLENGTH      = 0x000E;public static String GetWindowText(IntPtr hWnd)
    {
         IntPtr txtLength;
         IntPtr retValue;
         IntPtr zeroVal = new IntPtr(0); // use the imutable singelton IntPtr.Zero instead     //Get the length of the text
         txtLength = SendMessage(hWnd, WM_GETTEXTLENGTH, zeroVal, zeroVal);     //Setup the size of the sb
         StringBuilder sb = new StringBuilder(txtLength.ToInt32() + 1);     //Get the text of the window/control t
         retValue =  SendMessage(hWnd,WM_GETTEXT,txtLength,sb); // must be txtLength + 1     //Return a string
         return sb.ToString(); 
    }
      

  5.   

    问题应该不在这里,因为我用上面的代码或者我的代码我都能把 另一TextBox中的文本取出,
    只是取不到 这个 TrichEdit的, 但却可以看到(用spy++) 说明spy++ 取到了,只是没有正确返回.