Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. DLLIMPORT - SENDMESSAGE

DLLIMPORT - SENDMESSAGE

Scheduled Pinned Locked Moved C#
csharplinqgraphics
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    M Riaz Bashir
    wrote on last edited by
    #1

    Hi, Please let me know, where I am doing wrong. because following program is not receiving text from notepad. Thanks alot

    using System;
    using System.Diagnostics;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;

    namespace READVALUES
    {
    public partial class Form1 : Form
    {
    [DllImport("user32.dll")]
    static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, long wParam, [MarshalAs(UnmanagedType.LPStr)] StringBuilder lParam);

        public Form1()
        {
            InitializeComponent();
        }
    
        private void Form1\_Load(object sender, EventArgs e)
        {
    
        }
    
        private void button1\_Click(object sender, EventArgs e)
        {
            Process\[\] processes = Process.GetProcessesByName("notepad");
            foreach (Process p in processes)
            {
                string myGotWindow = p.MainWindowTitle;
                IntPtr pFoundWindow = p.MainWindowHandle;
                const uint WM\_GETTEXT = 0x0D;
                StringBuilder sb = new StringBuilder();
                IntPtr retVal = SendMessage(pFoundWindow, WM\_GETTEXT, 100, sb);
                textBox1.Text = sb.ToString();
            }
        }
    }
    

    }

    L T 2 Replies Last reply
    0
    • M M Riaz Bashir

      Hi, Please let me know, where I am doing wrong. because following program is not receiving text from notepad. Thanks alot

      using System;
      using System.Diagnostics;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.Data;
      using System.Drawing;
      using System.Linq;
      using System.Text;
      using System.Windows.Forms;
      using System.Runtime.InteropServices;

      namespace READVALUES
      {
      public partial class Form1 : Form
      {
      [DllImport("user32.dll")]
      static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, long wParam, [MarshalAs(UnmanagedType.LPStr)] StringBuilder lParam);

          public Form1()
          {
              InitializeComponent();
          }
      
          private void Form1\_Load(object sender, EventArgs e)
          {
      
          }
      
          private void button1\_Click(object sender, EventArgs e)
          {
              Process\[\] processes = Process.GetProcessesByName("notepad");
              foreach (Process p in processes)
              {
                  string myGotWindow = p.MainWindowTitle;
                  IntPtr pFoundWindow = p.MainWindowHandle;
                  const uint WM\_GETTEXT = 0x0D;
                  StringBuilder sb = new StringBuilder();
                  IntPtr retVal = SendMessage(pFoundWindow, WM\_GETTEXT, 100, sb);
                  textBox1.Text = sb.ToString();
              }
          }
      }
      

      }

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      I don't think you can use a StringBuilder as an unmanaged type. See http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshalasattribute(v=vs.71).aspx[^].

      Veni, vidi, abiit domum

      1 Reply Last reply
      0
      • M M Riaz Bashir

        Hi, Please let me know, where I am doing wrong. because following program is not receiving text from notepad. Thanks alot

        using System;
        using System.Diagnostics;
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.Data;
        using System.Drawing;
        using System.Linq;
        using System.Text;
        using System.Windows.Forms;
        using System.Runtime.InteropServices;

        namespace READVALUES
        {
        public partial class Form1 : Form
        {
        [DllImport("user32.dll")]
        static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, long wParam, [MarshalAs(UnmanagedType.LPStr)] StringBuilder lParam);

            public Form1()
            {
                InitializeComponent();
            }
        
            private void Form1\_Load(object sender, EventArgs e)
            {
        
            }
        
            private void button1\_Click(object sender, EventArgs e)
            {
                Process\[\] processes = Process.GetProcessesByName("notepad");
                foreach (Process p in processes)
                {
                    string myGotWindow = p.MainWindowTitle;
                    IntPtr pFoundWindow = p.MainWindowHandle;
                    const uint WM\_GETTEXT = 0x0D;
                    StringBuilder sb = new StringBuilder();
                    IntPtr retVal = SendMessage(pFoundWindow, WM\_GETTEXT, 100, sb);
                    textBox1.Text = sb.ToString();
                }
            }
        }
        

        }

        T Offline
        T Offline
        TnTinMn
        wrote on last edited by
        #3

        Give these definitions a try.

        [DllImport("user32.dll")]
        static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, [MarshalAs(UnmanagedType.LPStr)] StringBuilder lParam);

        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);

        The reason for the FindWindowEx is so that you can find the child window that contains the text. If you use the MainWindowHandle, you will retrieve the window title. You need to find the child window whose class name is "Edit". Also give the string builder an ample initial size. The default size is 16 characters. I will leave it to you to write the logic to compare the returned number of characters against the stringbuilder's "Capacity" property to determine if you got all the text. If returned length is Capacity -1, there may be more text that you did not retrieve.

        Process[] processes = Process.GetProcessesByName("notepad");
        foreach (Process p in processes)
        {
        string myGotWindow = p.MainWindowTitle;
        IntPtr pEditWindow = FindWindowEx(p.MainWindowHandle, IntPtr.Zero, "Edit", null);

        const uint WM\_GETTEXT = 0x0D;
        StringBuilder sb =  new StringBuilder(1000);
        IntPtr retVal = SendMessage(pEditWindow, WM\_GETTEXT, (IntPtr)sb.Capacity, sb);
        textBox1.Text = sb.ToString();
        

        }

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups